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 
19 import android.app.UiModeManager;
20 import android.car.Car;
21 import android.car.CarNotConnectedException;
22 import android.car.hardware.CarSensorEvent;
23 import android.car.hardware.CarSensorManager;
24 import android.car.hardware.ICarSensorEventListener;
25 import android.content.Context;
26 import android.util.Log;
27 
28 import java.io.PrintWriter;
29 import java.util.List;
30 
31 
32 public class CarNightService implements CarServiceBase {
33 
34     public static final boolean DBG = true;
35     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 
handleSensorEvent(CarSensorEvent event)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 
CarNightService(Context context)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
init()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,
84                 CarSensorManager.SENSOR_RATE_NORMAL, mICarSensorEventListener);
85         CarSensorEvent currentState = mCarSensorService.getLatestSensorEvent(
86                 CarSensorManager.SENSOR_TYPE_NIGHT);
87         handleSensorEvent(currentState);
88     }
89 
90     @Override
release()91     public synchronized void release() {
92     }
93 
94     @Override
dump(PrintWriter writer)95     public synchronized void dump(PrintWriter writer) {
96         writer.println("*DAY NIGHT POLICY*");
97         writer.println("Mode:" + ((mNightSetting == UiModeManager.MODE_NIGHT_YES) ? "night" : "day")
98                 );
99     }
100 }
101