blob: dae9a141f879a4fa715130884d42e931c6bed458 [file] [log] [blame]
keunyoungca515072015-07-10 12:21:47 -07001/*
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
17package com.android.car;
18
Keun-young Parke54ac272016-02-16 19:02:18 -080019import android.car.hardware.CarSensorEvent;
20import android.car.hardware.CarSensorManager;
keunyoungca515072015-07-10 12:21:47 -070021import android.content.Context;
Keun-young Park05f44812016-02-10 15:32:48 -080022import android.util.Log;
keunyoungca515072015-07-10 12:21:47 -070023
Enrico Granata7096f5e2017-02-03 14:54:20 -080024import com.android.car.hal.SensorHalService.SensorListener;
keunyoungca515072015-07-10 12:21:47 -070025import java.io.PrintWriter;
26
Keun-young Parkf9215202016-10-10 12:34:08 -070027//TODO implement default one based on time or other sensors. bug: 32066909
Enrico Granata7096f5e2017-02-03 14:54:20 -080028public class DayNightModePolicy extends CarSensorService.LogicalSensor {
keunyoungca515072015-07-10 12:21:47 -070029
30 private final Context mContext;
31 private SensorListener mSensorListener;
32 private boolean mIsReady = false;
33 private boolean mStarted = false;
34
Keun-young Parkf9215202016-10-10 12:34:08 -070035 private static final int[] SUPPORTED_SENSORS = { CarSensorManager.SENSOR_TYPE_NIGHT };
36
keunyoungca515072015-07-10 12:21:47 -070037 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() {
keunyoungca515072015-07-10 12:21:47 -070048 }
49
keunyoung1ab8e182015-09-24 09:25:22 -070050 public static CarSensorEvent getDefaultValue(int sensorType) {
Pavel Maltsevdfccc5a2016-04-11 21:56:04 -070051 // 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 */);
keunyoungca515072015-07-10 12:21:47 -070055 }
56
keunyoungca515072015-07-10 12:21:47 -070057 public synchronized void registerSensorListener(SensorListener listener) {
58 mSensorListener = listener;
keunyoungca515072015-07-10 12:21:47 -070059 }
60
61 @Override
62 public synchronized void onSensorServiceReady() {
keunyoungca515072015-07-10 12:21:47 -070063 }
64
65 @Override
66 public synchronized boolean isReady() {
67 return mIsReady;
68 }
69
70 @Override
71 public synchronized int[] getSupportedSensors() {
Keun-young Parkf9215202016-10-10 12:34:08 -070072 return SUPPORTED_SENSORS;
keunyoungca515072015-07-10 12:21:47 -070073 }
74
75 @Override
76 public synchronized boolean requestSensorStart(int sensorType, int rate) {
77 mStarted = true;
Keun-young Park05f44812016-02-10 15:32:48 -080078 Log.w(CarLog.TAG_SENSOR,
79 "DayNightModePolicy.requestSensorStart, default policy not implemented");
keunyoungca515072015-07-10 12:21:47 -070080 return false;
81 }
82
83 @Override
84 public synchronized void requestSensorStop(int sensorType) {
keunyoungca515072015-07-10 12:21:47 -070085 }
86
Pavel Maltsevdfccc5a2016-04-11 21:56:04 -070087 private static CarSensorEvent createEvent(boolean isNight, long timestamp) {
keunyoung1ab8e182015-09-24 09:25:22 -070088 CarSensorEvent event = new CarSensorEvent(CarSensorManager.SENSOR_TYPE_NIGHT,
Pavel Maltsevdfccc5a2016-04-11 21:56:04 -070089 timestamp, 0, 1);
keunyoung1ab8e182015-09-24 09:25:22 -070090 event.intValues[0] = isNight ? 1 : 0;
91 return event;
92 }
93
keunyoungca515072015-07-10 12:21:47 -070094 @Override
95 public synchronized void dump(PrintWriter writer) {
keunyoungca515072015-07-10 12:21:47 -070096 }
97}