blob: 05d70ff9d7a3dbb0938f6a1c04670c4f07a2304a [file] [log] [blame]
Joseph Pirozzo317343d2016-01-25 10:22:37 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.car;
18
19import android.app.UiModeManager;
Keun-young Parke54ac272016-02-16 19:02:18 -080020import android.car.Car;
21import android.car.CarNotConnectedException;
22import android.car.hardware.CarSensorEvent;
23import android.car.hardware.CarSensorManager;
24import android.car.hardware.ICarSensorEventListener;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080025import android.content.Context;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080026import android.util.Log;
27
28import java.io.PrintWriter;
29import java.util.List;
30
31
32public class CarNightService implements CarServiceBase {
33
Keun-young Park9ec05472016-04-26 21:18:18 -070034 public static final boolean DBG = false;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080035 private int mNightSetting = UiModeManager.MODE_NIGHT_YES;
36 private final Context mContext;
37 private final UiModeManager mUiModeManager;
38 private CarSensorService mCarSensorService;
39
40 private final ICarSensorEventListener mICarSensorEventListener =
41 new ICarSensorEventListener.Stub() {
42 @Override
43 public void onSensorChanged(List<CarSensorEvent> events) {
44 if (!events.isEmpty()) {
45 CarSensorEvent event = events.get(events.size() - 1);
46 handleSensorEvent(event);
47 }
48 }
49 };
50
51 public synchronized void handleSensorEvent(CarSensorEvent event) {
52 if (event == null) {
53 return;
54 }
55 if (event.sensorType == CarSensorManager.SENSOR_TYPE_NIGHT) {
56 if (event.intValues[0] == 1) {
57 mNightSetting = UiModeManager.MODE_NIGHT_YES;
58 }
59 else {
60 mNightSetting = UiModeManager.MODE_NIGHT_NO;
61 }
62 if (mUiModeManager != null) {
63 mUiModeManager.setNightMode(mNightSetting);
64 }
65 }
66 }
67
68 CarNightService(Context context) {
69 mContext = context;
70 mUiModeManager = (UiModeManager) mContext.getSystemService(Context.UI_MODE_SERVICE);
71 if (mUiModeManager == null) {
72 Log.w(CarLog.TAG_SENSOR,"Failed to get UI_MODE_SERVICE");
73 }
74 }
75
76 @Override
77 public synchronized void init() {
78 if (DBG) {
79 Log.d(CarLog.TAG_SENSOR,"CAR dayNight init.");
80 }
81 mCarSensorService = (CarSensorService) ICarImpl.getInstance(mContext).getCarService(
82 Car.SENSOR_SERVICE);
83 mCarSensorService.registerOrUpdateSensorListener(CarSensorManager.SENSOR_TYPE_NIGHT,
Keun-young Parke54ac272016-02-16 19:02:18 -080084 CarSensorManager.SENSOR_RATE_NORMAL, mICarSensorEventListener);
Joseph Pirozzo317343d2016-01-25 10:22:37 -080085 CarSensorEvent currentState = mCarSensorService.getLatestSensorEvent(
86 CarSensorManager.SENSOR_TYPE_NIGHT);
87 handleSensorEvent(currentState);
88 }
89
90 @Override
91 public synchronized void release() {
92 }
93
94 @Override
95 public synchronized void dump(PrintWriter writer) {
96 writer.println("*DAY NIGHT POLICY*");
Keun-young Park05f44812016-02-10 15:32:48 -080097 writer.println("Mode:" + ((mNightSetting == UiModeManager.MODE_NIGHT_YES) ? "night" : "day")
98 );
Joseph Pirozzo317343d2016-01-25 10:22:37 -080099 }
100}