blob: 497f77cf258c5390028cb5f240d9479dc3356671 [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
Yao Chene33f07e2016-07-26 12:02:51 -070019import android.annotation.IntDef;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080020import android.app.UiModeManager;
Keun-young Parke54ac272016-02-16 19:02:18 -080021import android.car.hardware.CarSensorEvent;
22import android.car.hardware.CarSensorManager;
23import android.car.hardware.ICarSensorEventListener;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080024import android.content.Context;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080025import android.util.Log;
26
27import java.io.PrintWriter;
Yao Chene33f07e2016-07-26 12:02:51 -070028import java.lang.annotation.Retention;
29import java.lang.annotation.RetentionPolicy;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080030import java.util.List;
31
32
33public class CarNightService implements CarServiceBase {
34
Keun-young Park9ec05472016-04-26 21:18:18 -070035 public static final boolean DBG = false;
Yao Chene33f07e2016-07-26 12:02:51 -070036
37 @IntDef({FORCED_SENSOR_MODE, FORCED_DAY_MODE, FORCED_NIGHT_MODE})
38 @Retention(RetentionPolicy.SOURCE)
39 public @interface DayNightSensorMode {}
40
41 public static final int FORCED_SENSOR_MODE = 0;
42 public static final int FORCED_DAY_MODE = 1;
43 public static final int FORCED_NIGHT_MODE = 2;
44
Joseph Pirozzo317343d2016-01-25 10:22:37 -080045 private int mNightSetting = UiModeManager.MODE_NIGHT_YES;
Yao Chene33f07e2016-07-26 12:02:51 -070046 private int mForcedMode = FORCED_SENSOR_MODE;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080047 private final Context mContext;
48 private final UiModeManager mUiModeManager;
49 private CarSensorService mCarSensorService;
50
51 private final ICarSensorEventListener mICarSensorEventListener =
52 new ICarSensorEventListener.Stub() {
53 @Override
54 public void onSensorChanged(List<CarSensorEvent> events) {
55 if (!events.isEmpty()) {
56 CarSensorEvent event = events.get(events.size() - 1);
57 handleSensorEvent(event);
58 }
59 }
60 };
61
62 public synchronized void handleSensorEvent(CarSensorEvent event) {
63 if (event == null) {
64 return;
65 }
66 if (event.sensorType == CarSensorManager.SENSOR_TYPE_NIGHT) {
67 if (event.intValues[0] == 1) {
68 mNightSetting = UiModeManager.MODE_NIGHT_YES;
69 }
70 else {
71 mNightSetting = UiModeManager.MODE_NIGHT_NO;
72 }
Yao Chene33f07e2016-07-26 12:02:51 -070073 if (mUiModeManager != null && (mForcedMode == FORCED_SENSOR_MODE)) {
Joseph Pirozzo317343d2016-01-25 10:22:37 -080074 mUiModeManager.setNightMode(mNightSetting);
75 }
76 }
77 }
78
Yao Chene33f07e2016-07-26 12:02:51 -070079 public synchronized int forceDayNightMode(@DayNightSensorMode int mode) {
80 if (mUiModeManager == null) {
81 return -1;
82 }
83 int resultMode;
84 switch (mode) {
85 case FORCED_SENSOR_MODE:
86 resultMode = mNightSetting;
87 mForcedMode = FORCED_SENSOR_MODE;
88 break;
89 case FORCED_DAY_MODE:
90 resultMode = UiModeManager.MODE_NIGHT_NO;
91 mForcedMode = FORCED_DAY_MODE;
92 break;
93 case FORCED_NIGHT_MODE:
94 resultMode = UiModeManager.MODE_NIGHT_YES;
95 mForcedMode = FORCED_NIGHT_MODE;
96 break;
97 default:
98 Log.e(CarLog.TAG_SENSOR, "Unknown forced day/night mode " + mode);
99 return -1;
100 }
101 mUiModeManager.setNightMode(resultMode);
102 return mUiModeManager.getNightMode();
103 }
104
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700105 CarNightService(Context context, CarSensorService sensorService) {
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800106 mContext = context;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700107 mCarSensorService = sensorService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800108 mUiModeManager = (UiModeManager) mContext.getSystemService(Context.UI_MODE_SERVICE);
109 if (mUiModeManager == null) {
110 Log.w(CarLog.TAG_SENSOR,"Failed to get UI_MODE_SERVICE");
111 }
112 }
113
114 @Override
115 public synchronized void init() {
116 if (DBG) {
117 Log.d(CarLog.TAG_SENSOR,"CAR dayNight init.");
118 }
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800119 mCarSensorService.registerOrUpdateSensorListener(CarSensorManager.SENSOR_TYPE_NIGHT,
Keun-young Parke54ac272016-02-16 19:02:18 -0800120 CarSensorManager.SENSOR_RATE_NORMAL, mICarSensorEventListener);
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800121 CarSensorEvent currentState = mCarSensorService.getLatestSensorEvent(
122 CarSensorManager.SENSOR_TYPE_NIGHT);
123 handleSensorEvent(currentState);
124 }
125
126 @Override
127 public synchronized void release() {
128 }
129
130 @Override
131 public synchronized void dump(PrintWriter writer) {
132 writer.println("*DAY NIGHT POLICY*");
Keun-young Park05f44812016-02-10 15:32:48 -0800133 writer.println("Mode:" + ((mNightSetting == UiModeManager.MODE_NIGHT_YES) ? "night" : "day")
134 );
Yao Chene33f07e2016-07-26 12:02:51 -0700135 writer.println("Forced Mode? " + (mForcedMode == FORCED_SENSOR_MODE ? "false"
136 : (mForcedMode == FORCED_DAY_MODE ? "day" : "night")));
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800137 }
138}