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.CarPropertyValue;
     22 import android.car.hardware.property.CarPropertyEvent;
     23 import android.car.hardware.property.ICarPropertyEventListener;
     24 import android.content.Context;
     25 import android.hardware.automotive.vehicle.V2_0.VehicleProperty;
     26 import android.os.RemoteException;
     27 import android.util.Log;
     28 
     29 import java.io.PrintWriter;
     30 import java.lang.annotation.Retention;
     31 import java.lang.annotation.RetentionPolicy;
     32 import java.util.List;
     33 
     34 public class CarNightService implements CarServiceBase {
     35 
     36     public static final boolean DBG = false;
     37 
     38     @IntDef({FORCED_SENSOR_MODE, FORCED_DAY_MODE, FORCED_NIGHT_MODE})
     39     @Retention(RetentionPolicy.SOURCE)
     40     public @interface DayNightSensorMode {}
     41 
     42     public static final int FORCED_SENSOR_MODE = 0;
     43     public static final int FORCED_DAY_MODE = 1;
     44     public static final int FORCED_NIGHT_MODE = 2;
     45 
     46     private int mNightSetting = UiModeManager.MODE_NIGHT_YES;
     47     private int mForcedMode = FORCED_SENSOR_MODE;
     48     private final Context mContext;
     49     private final UiModeManager mUiModeManager;
     50     private CarPropertyService mCarPropertyService;
     51 
     52     private final ICarPropertyEventListener mICarPropertyEventListener =
     53             new ICarPropertyEventListener.Stub() {
     54         @Override
     55         public void onEvent(List<CarPropertyEvent> events) throws RemoteException {
     56             for (CarPropertyEvent event : events) {
     57                 handlePropertyEvent(event);
     58             }
     59         }
     60     };
     61 
     62     /**
     63      * Handle CarPropertyEvents
     64      * @param event
     65      */
     66     public synchronized void handlePropertyEvent(CarPropertyEvent event) {
     67         if (event == null) {
     68             return;
     69         }
     70         if (event.getEventType() == CarPropertyEvent.PROPERTY_EVENT_PROPERTY_CHANGE) {
     71             // Only handle onChange events
     72             CarPropertyValue value = event.getCarPropertyValue();
     73             if (value.getPropertyId() == VehicleProperty.NIGHT_MODE) {
     74                 boolean nightMode = (Boolean) value.getValue();
     75                 if (nightMode) {
     76                     mNightSetting = UiModeManager.MODE_NIGHT_YES;
     77                     if (DBG)  Log.d(CarLog.TAG_SENSOR, "CAR dayNight handleSensorEvent NIGHT");
     78                 } else {
     79                     mNightSetting = UiModeManager.MODE_NIGHT_NO;
     80                     if (DBG)  Log.d(CarLog.TAG_SENSOR, "CAR dayNight handleSensorEvent DAY");
     81                 }
     82                 if (mUiModeManager != null && (mForcedMode == FORCED_SENSOR_MODE)) {
     83                     mUiModeManager.setNightMode(mNightSetting);
     84                     if (DBG)  Log.d(CarLog.TAG_SENSOR, "CAR dayNight handleSensorEvent APPLIED");
     85                 } else {
     86                     if (DBG)  Log.d(CarLog.TAG_SENSOR, "CAR dayNight handleSensorEvent IGNORED");
     87                 }
     88             }
     89         }
     90     }
     91 
     92     public synchronized int forceDayNightMode(@DayNightSensorMode int mode) {
     93         if (mUiModeManager == null) {
     94             return -1;
     95         }
     96         int resultMode;
     97         switch (mode) {
     98             case FORCED_SENSOR_MODE:
     99                 resultMode = mNightSetting;
    100                 mForcedMode = FORCED_SENSOR_MODE;
    101                 break;
    102             case FORCED_DAY_MODE:
    103                 resultMode = UiModeManager.MODE_NIGHT_NO;
    104                 mForcedMode = FORCED_DAY_MODE;
    105                 break;
    106             case FORCED_NIGHT_MODE:
    107                 resultMode = UiModeManager.MODE_NIGHT_YES;
    108                 mForcedMode = FORCED_NIGHT_MODE;
    109                 break;
    110             default:
    111                 Log.e(CarLog.TAG_SENSOR, "Unknown forced day/night mode " + mode);
    112                 return -1;
    113         }
    114         mUiModeManager.setNightMode(resultMode);
    115         return mUiModeManager.getNightMode();
    116     }
    117 
    118     CarNightService(Context context, CarPropertyService propertyService) {
    119         mContext = context;
    120         mCarPropertyService = propertyService;
    121         mUiModeManager = (UiModeManager) mContext.getSystemService(Context.UI_MODE_SERVICE);
    122         if (mUiModeManager == null) {
    123             Log.w(CarLog.TAG_SENSOR, "Failed to get UI_MODE_SERVICE");
    124         }
    125     }
    126 
    127     @Override
    128     public synchronized void init() {
    129         if (DBG) {
    130             Log.d(CarLog.TAG_SENSOR,"CAR dayNight init.");
    131         }
    132         mCarPropertyService.registerListener(VehicleProperty.NIGHT_MODE, 0,
    133                 mICarPropertyEventListener);
    134     }
    135 
    136     @Override
    137     public synchronized void release() {
    138     }
    139 
    140     @Override
    141     public synchronized void dump(PrintWriter writer) {
    142         writer.println("*DAY NIGHT POLICY*");
    143         writer.println("Mode:" +
    144                 ((mNightSetting == UiModeManager.MODE_NIGHT_YES) ? "night" : "day"));
    145         writer.println("Forced Mode? " + (mForcedMode == FORCED_SENSOR_MODE ? "false"
    146                 : (mForcedMode == FORCED_DAY_MODE ? "day" : "night")));
    147     }
    148 }
    149