blob: 0d3af67b4ebb849af30c984bbfc0839e4ec9f692 [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.Car;
Keun-young Parke54ac272016-02-16 19:02:18 -080022import 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;
Yao Chene33f07e2016-07-26 12:02:51 -070026import android.content.pm.PackageManager;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080027import android.util.Log;
28
29import java.io.PrintWriter;
Yao Chene33f07e2016-07-26 12:02:51 -070030import java.lang.annotation.Retention;
31import java.lang.annotation.RetentionPolicy;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080032import java.util.List;
33
34
35public class CarNightService implements CarServiceBase {
36
Keun-young Park9ec05472016-04-26 21:18:18 -070037 public static final boolean DBG = false;
Yao Chene33f07e2016-07-26 12:02:51 -070038
39 @IntDef({FORCED_SENSOR_MODE, FORCED_DAY_MODE, FORCED_NIGHT_MODE})
40 @Retention(RetentionPolicy.SOURCE)
41 public @interface DayNightSensorMode {}
42
43 public static final int FORCED_SENSOR_MODE = 0;
44 public static final int FORCED_DAY_MODE = 1;
45 public static final int FORCED_NIGHT_MODE = 2;
46
Joseph Pirozzo317343d2016-01-25 10:22:37 -080047 private int mNightSetting = UiModeManager.MODE_NIGHT_YES;
Yao Chene33f07e2016-07-26 12:02:51 -070048 private int mForcedMode = FORCED_SENSOR_MODE;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080049 private final Context mContext;
50 private final UiModeManager mUiModeManager;
51 private CarSensorService mCarSensorService;
52
53 private final ICarSensorEventListener mICarSensorEventListener =
54 new ICarSensorEventListener.Stub() {
55 @Override
56 public void onSensorChanged(List<CarSensorEvent> events) {
57 if (!events.isEmpty()) {
58 CarSensorEvent event = events.get(events.size() - 1);
59 handleSensorEvent(event);
60 }
61 }
62 };
63
64 public synchronized void handleSensorEvent(CarSensorEvent event) {
65 if (event == null) {
66 return;
67 }
68 if (event.sensorType == CarSensorManager.SENSOR_TYPE_NIGHT) {
69 if (event.intValues[0] == 1) {
70 mNightSetting = UiModeManager.MODE_NIGHT_YES;
71 }
72 else {
73 mNightSetting = UiModeManager.MODE_NIGHT_NO;
74 }
Yao Chene33f07e2016-07-26 12:02:51 -070075 if (mUiModeManager != null && (mForcedMode == FORCED_SENSOR_MODE)) {
Joseph Pirozzo317343d2016-01-25 10:22:37 -080076 mUiModeManager.setNightMode(mNightSetting);
77 }
78 }
79 }
80
Yao Chene33f07e2016-07-26 12:02:51 -070081 public synchronized int forceDayNightMode(@DayNightSensorMode int mode) {
82 if (mUiModeManager == null) {
83 return -1;
84 }
85 int resultMode;
86 switch (mode) {
87 case FORCED_SENSOR_MODE:
88 resultMode = mNightSetting;
89 mForcedMode = FORCED_SENSOR_MODE;
90 break;
91 case FORCED_DAY_MODE:
92 resultMode = UiModeManager.MODE_NIGHT_NO;
93 mForcedMode = FORCED_DAY_MODE;
94 break;
95 case FORCED_NIGHT_MODE:
96 resultMode = UiModeManager.MODE_NIGHT_YES;
97 mForcedMode = FORCED_NIGHT_MODE;
98 break;
99 default:
100 Log.e(CarLog.TAG_SENSOR, "Unknown forced day/night mode " + mode);
101 return -1;
102 }
103 mUiModeManager.setNightMode(resultMode);
104 return mUiModeManager.getNightMode();
105 }
106
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800107 CarNightService(Context context) {
108 mContext = context;
109 mUiModeManager = (UiModeManager) mContext.getSystemService(Context.UI_MODE_SERVICE);
110 if (mUiModeManager == null) {
111 Log.w(CarLog.TAG_SENSOR,"Failed to get UI_MODE_SERVICE");
112 }
113 }
114
115 @Override
116 public synchronized void init() {
117 if (DBG) {
118 Log.d(CarLog.TAG_SENSOR,"CAR dayNight init.");
119 }
120 mCarSensorService = (CarSensorService) ICarImpl.getInstance(mContext).getCarService(
121 Car.SENSOR_SERVICE);
122 mCarSensorService.registerOrUpdateSensorListener(CarSensorManager.SENSOR_TYPE_NIGHT,
Keun-young Parke54ac272016-02-16 19:02:18 -0800123 CarSensorManager.SENSOR_RATE_NORMAL, mICarSensorEventListener);
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800124 CarSensorEvent currentState = mCarSensorService.getLatestSensorEvent(
125 CarSensorManager.SENSOR_TYPE_NIGHT);
126 handleSensorEvent(currentState);
127 }
128
129 @Override
130 public synchronized void release() {
131 }
132
133 @Override
134 public synchronized void dump(PrintWriter writer) {
135 writer.println("*DAY NIGHT POLICY*");
Keun-young Park05f44812016-02-10 15:32:48 -0800136 writer.println("Mode:" + ((mNightSetting == UiModeManager.MODE_NIGHT_YES) ? "night" : "day")
137 );
Yao Chene33f07e2016-07-26 12:02:51 -0700138 writer.println("Forced Mode? " + (mForcedMode == FORCED_SENSOR_MODE ? "false"
139 : (mForcedMode == FORCED_DAY_MODE ? "day" : "night")));
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800140 }
141}