2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.doze;
19 import android.content.Context;
20 import android.hardware.Sensor;
21 import android.hardware.SensorEvent;
22 import android.hardware.SensorEventListener;
23 import android.hardware.SensorManager;
24 import android.os.Handler;
26 import com.android.internal.annotations.VisibleForTesting;
27 import com.android.systemui.R;
30 * Controls the screen brightness when dozing.
32 public class DozeScreenBrightness implements DozeMachine.Part, SensorEventListener {
33 private final Context mContext;
34 private final DozeMachine.Service mDozeService;
35 private final DozeHost mDozeHost;
36 private final Handler mHandler;
37 private final SensorManager mSensorManager;
38 private final Sensor mLightSensor;
39 private final int[] mSensorToBrightness;
40 private final int[] mSensorToScrimOpacity;
41 private boolean mRegistered;
43 public DozeScreenBrightness(Context context, DozeMachine.Service service,
44 SensorManager sensorManager, Sensor lightSensor, DozeHost host,
47 mDozeService = service;
48 mSensorManager = sensorManager;
49 mLightSensor = lightSensor;
53 mSensorToBrightness = context.getResources().getIntArray(
54 R.array.config_doze_brightness_sensor_to_brightness);
55 mSensorToScrimOpacity = context.getResources().getIntArray(
56 R.array.config_doze_brightness_sensor_to_scrim_opacity);
60 public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
63 resetBrightnessToDefault();
66 case DOZE_REQUEST_PULSE:
67 setLightSensorEnabled(true);
71 setLightSensorEnabled(false);
72 resetBrightnessToDefault();
75 setLightSensorEnabled(false);
81 public void onSensorChanged(SensorEvent event) {
83 int sensorValue = (int) event.values[0];
84 int brightness = computeBrightness(sensorValue);
86 mDozeService.setDozeScreenBrightness(brightness);
89 int scrimOpacity = computeScrimOpacity(sensorValue);
90 if (scrimOpacity >= 0) {
91 mDozeHost.setAodDimmingScrim(scrimOpacity / 255f);
96 private int computeScrimOpacity(int sensorValue) {
97 if (sensorValue < 0 || sensorValue >= mSensorToScrimOpacity.length) {
100 return mSensorToScrimOpacity[sensorValue];
103 private int computeBrightness(int sensorValue) {
104 if (sensorValue < 0 || sensorValue >= mSensorToBrightness.length) {
107 return mSensorToBrightness[sensorValue];
111 public void onAccuracyChanged(Sensor sensor, int accuracy) {
114 private void resetBrightnessToDefault() {
115 mDozeService.setDozeScreenBrightness(mContext.getResources().getInteger(
116 com.android.internal.R.integer.config_screenBrightnessDoze));
119 private void setLightSensorEnabled(boolean enabled) {
120 if (enabled && !mRegistered && mLightSensor != null) {
121 mRegistered = mSensorManager.registerListener(this, mLightSensor,
122 SensorManager.SENSOR_DELAY_NORMAL, mHandler);
123 } else if (!enabled && mRegistered) {
124 mSensorManager.unregisterListener(this);