keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 19 | import android.car.hardware.CarSensorEvent; |
| 20 | import android.car.hardware.CarSensorManager; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Keun-young Park | 05f4481 | 2016-02-10 15:32:48 -0800 | [diff] [blame] | 22 | import android.util.Log; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 23 | |
Enrico Granata | 7096f5e | 2017-02-03 14:54:20 -0800 | [diff] [blame] | 24 | import com.android.car.hal.SensorHalService.SensorListener; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 25 | import java.io.PrintWriter; |
| 26 | |
Keun-young Park | f921520 | 2016-10-10 12:34:08 -0700 | [diff] [blame] | 27 | //TODO implement default one based on time or other sensors. bug: 32066909 |
Enrico Granata | 7096f5e | 2017-02-03 14:54:20 -0800 | [diff] [blame] | 28 | public class DayNightModePolicy extends CarSensorService.LogicalSensor { |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 29 | |
| 30 | private final Context mContext; |
| 31 | private SensorListener mSensorListener; |
| 32 | private boolean mIsReady = false; |
| 33 | private boolean mStarted = false; |
| 34 | |
Keun-young Park | f921520 | 2016-10-10 12:34:08 -0700 | [diff] [blame] | 35 | private static final int[] SUPPORTED_SENSORS = { CarSensorManager.SENSOR_TYPE_NIGHT }; |
| 36 | |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 37 | public DayNightModePolicy(Context context) { |
| 38 | mContext = context; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public synchronized void init() { |
| 43 | mIsReady = true; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public synchronized void release() { |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 48 | } |
| 49 | |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 50 | public static CarSensorEvent getDefaultValue(int sensorType) { |
Pavel Maltsev | dfccc5a | 2016-04-11 21:56:04 -0700 | [diff] [blame] | 51 | // There's a race condition and timestamp from vehicle HAL could be slightly less |
| 52 | // then current call to SystemClock.elapsedRealtimeNanos() will return. |
| 53 | // We want vehicle HAL value always override this default value so we set timestamp to 0. |
| 54 | return createEvent(true /* isNight */, 0 /* timestamp */); |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 55 | } |
| 56 | |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 57 | public synchronized void registerSensorListener(SensorListener listener) { |
| 58 | mSensorListener = listener; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public synchronized void onSensorServiceReady() { |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public synchronized boolean isReady() { |
| 67 | return mIsReady; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public synchronized int[] getSupportedSensors() { |
Keun-young Park | f921520 | 2016-10-10 12:34:08 -0700 | [diff] [blame] | 72 | return SUPPORTED_SENSORS; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public synchronized boolean requestSensorStart(int sensorType, int rate) { |
| 77 | mStarted = true; |
Keun-young Park | 05f4481 | 2016-02-10 15:32:48 -0800 | [diff] [blame] | 78 | Log.w(CarLog.TAG_SENSOR, |
| 79 | "DayNightModePolicy.requestSensorStart, default policy not implemented"); |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 80 | return false; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public synchronized void requestSensorStop(int sensorType) { |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Pavel Maltsev | dfccc5a | 2016-04-11 21:56:04 -0700 | [diff] [blame] | 87 | private static CarSensorEvent createEvent(boolean isNight, long timestamp) { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 88 | CarSensorEvent event = new CarSensorEvent(CarSensorManager.SENSOR_TYPE_NIGHT, |
Pavel Maltsev | dfccc5a | 2016-04-11 21:56:04 -0700 | [diff] [blame] | 89 | timestamp, 0, 1); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 90 | event.intValues[0] = isNight ? 1 : 0; |
| 91 | return event; |
| 92 | } |
| 93 | |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public synchronized void dump(PrintWriter writer) { |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 96 | } |
| 97 | } |