Home | History | Annotate | Download | only in car
      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.annotation.IntDef;
     20 import android.app.UiModeManager;
     21 import android.car.hardware.CarSensorEvent;
     22 import android.car.hardware.CarSensorManager;
     23 import android.car.hardware.ICarSensorEventListener;
     24 import android.content.Context;
     25 import android.util.Log;
     26 
     27 import java.io.PrintWriter;
     28 import java.lang.annotation.Retention;
     29 import java.lang.annotation.RetentionPolicy;
     30 import java.util.List;
     31 
     32 
     33 public class CarNightService implements CarServiceBase {
     34 
     35     public static final boolean DBG = false;
     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 
     45     private int mNightSetting = UiModeManager.MODE_NIGHT_YES;
     46     private int mForcedMode = FORCED_SENSOR_MODE;
     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             }
     73             if (mUiModeManager != null && (mForcedMode == FORCED_SENSOR_MODE)) {
     74                 mUiModeManager.setNightMode(mNightSetting);
     75             }
     76         }
     77     }
     78 
     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 
    105     CarNightService(Context context, CarSensorService sensorService) {
    106         mContext = context;
    107         mCarSensorService = sensorService;
    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         }
    119         mCarSensorService.registerOrUpdateSensorListener(CarSensorManager.SENSOR_TYPE_NIGHT,
    120                 CarSensorManager.SENSOR_RATE_NORMAL, mICarSensorEventListener);
    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*");
    133         writer.println("Mode:" + ((mNightSetting == UiModeManager.MODE_NIGHT_YES) ? "night" : "day")
    134                 );
    135         writer.println("Forced Mode? " + (mForcedMode == FORCED_SENSOR_MODE ? "false"
    136                 : (mForcedMode == FORCED_DAY_MODE ? "day" : "night")));
    137     }
    138 }
    139