Joseph Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.car; |
| 18 | |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Joseph Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 20 | import android.app.UiModeManager; |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 21 | import android.car.hardware.CarSensorEvent; |
| 22 | import android.car.hardware.CarSensorManager; |
| 23 | import android.car.hardware.ICarSensorEventListener; |
Joseph Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Joseph Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
| 27 | import java.io.PrintWriter; |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 28 | import java.lang.annotation.Retention; |
| 29 | import java.lang.annotation.RetentionPolicy; |
Joseph Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 30 | import java.util.List; |
| 31 | |
| 32 | |
| 33 | public class CarNightService implements CarServiceBase { |
| 34 | |
Keun-young Park | 9ec0547 | 2016-04-26 21:18:18 -0700 | [diff] [blame] | 35 | public static final boolean DBG = false; |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 36 | |
| 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 Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 45 | private int mNightSetting = UiModeManager.MODE_NIGHT_YES; |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 46 | private int mForcedMode = FORCED_SENSOR_MODE; |
Joseph Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 47 | 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 Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 73 | if (mUiModeManager != null && (mForcedMode == FORCED_SENSOR_MODE)) { |
Joseph Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 74 | mUiModeManager.setNightMode(mNightSetting); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 79 | 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 Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 105 | CarNightService(Context context, CarSensorService sensorService) { |
Joseph Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 106 | mContext = context; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 107 | mCarSensorService = sensorService; |
Joseph Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 108 | 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 Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 119 | mCarSensorService.registerOrUpdateSensorListener(CarSensorManager.SENSOR_TYPE_NIGHT, |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 120 | CarSensorManager.SENSOR_RATE_NORMAL, mICarSensorEventListener); |
Joseph Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 121 | 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 Park | 05f4481 | 2016-02-10 15:32:48 -0800 | [diff] [blame] | 133 | writer.println("Mode:" + ((mNightSetting == UiModeManager.MODE_NIGHT_YES) ? "night" : "day") |
| 134 | ); |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 135 | writer.println("Forced Mode? " + (mForcedMode == FORCED_SENSOR_MODE ? "false" |
| 136 | : (mForcedMode == FORCED_DAY_MODE ? "day" : "night"))); |
Joseph Pirozzo | 317343d | 2016-01-25 10:22:37 -0800 | [diff] [blame] | 137 | } |
| 138 | } |