1 /*
2  * Copyright (C) 2017 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.systemui.doze;
18 
19 import static android.os.PowerManager.GO_TO_SLEEP_REASON_TIMEOUT;
20 
21 import static com.android.systemui.keyguard.WakefulnessLifecycle.WAKEFULNESS_GOING_TO_SLEEP;
22 
23 import android.content.BroadcastReceiver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.hardware.Sensor;
27 import android.hardware.SensorEvent;
28 import android.hardware.SensorEventListener;
29 import android.hardware.SensorManager;
30 import android.os.Handler;
31 import android.os.PowerManager;
32 import android.os.SystemProperties;
33 import android.os.Trace;
34 import android.os.UserHandle;
35 import android.provider.Settings;
36 import android.util.IndentingPrintWriter;
37 
38 import com.android.internal.R;
39 import com.android.systemui.doze.dagger.BrightnessSensor;
40 import com.android.systemui.doze.dagger.DozeScope;
41 import com.android.systemui.doze.dagger.WrappedService;
42 import com.android.systemui.keyguard.WakefulnessLifecycle;
43 import com.android.systemui.statusbar.phone.DozeParameters;
44 import com.android.systemui.statusbar.policy.DevicePostureController;
45 import com.android.systemui.util.sensors.AsyncSensorManager;
46 import com.android.systemui.util.settings.SystemSettings;
47 
48 import java.io.PrintWriter;
49 import java.util.Objects;
50 import java.util.Optional;
51 
52 import javax.inject.Inject;
53 
54 /**
55  * Controls the screen brightness when dozing.
56  */
57 @DozeScope
58 public class DozeScreenBrightness extends BroadcastReceiver implements DozeMachine.Part,
59         SensorEventListener {
60     private static final boolean DEBUG_AOD_BRIGHTNESS = SystemProperties
61             .getBoolean("debug.aod_brightness", false);
62     protected static final String ACTION_AOD_BRIGHTNESS =
63             "com.android.systemui.doze.AOD_BRIGHTNESS";
64     protected static final String BRIGHTNESS_BUCKET = "brightness_bucket";
65 
66     /**
67      * Just before the screen times out from user inactivity, DisplayPowerController dims the screen
68      * brightness to the lower of {@link #mScreenBrightnessDim}, or the current brightness minus
69      * this amount.
70      */
71     private final float mScreenBrightnessMinimumDimAmountFloat;
72     private final Context mContext;
73     private final DozeMachine.Service mDozeService;
74     private final DozeHost mDozeHost;
75     private final Handler mHandler;
76     private final SensorManager mSensorManager;
77     private final Optional<Sensor>[] mLightSensorOptional; // light sensors to use per posture
78     private final WakefulnessLifecycle mWakefulnessLifecycle;
79     private final DozeParameters mDozeParameters;
80     private final DevicePostureController mDevicePostureController;
81     private final DozeLog mDozeLog;
82     private final SystemSettings mSystemSettings;
83     private final int[] mSensorToBrightness;
84     private final int[] mSensorToScrimOpacity;
85     private final int mScreenBrightnessDim;
86 
87     @DevicePostureController.DevicePostureInt
88     private int mDevicePosture;
89     private boolean mRegistered;
90     private int mDefaultDozeBrightness;
91     private boolean mPaused = false;
92     private boolean mScreenOff = false;
93     private int mLastSensorValue = -1;
94     private DozeMachine.State mState = DozeMachine.State.UNINITIALIZED;
95 
96     /**
97      * Debug value used for emulating various display brightness buckets:
98      *
99      * {@code am broadcast -p com.android.systemui -a com.android.systemui.doze.AOD_BRIGHTNESS
100      * --ei brightness_bucket 1}
101      */
102     private int mDebugBrightnessBucket = -1;
103 
104     @Inject
DozeScreenBrightness( Context context, @WrappedService DozeMachine.Service service, AsyncSensorManager sensorManager, @BrightnessSensor Optional<Sensor>[] lightSensorOptional, DozeHost host, Handler handler, AlwaysOnDisplayPolicy alwaysOnDisplayPolicy, WakefulnessLifecycle wakefulnessLifecycle, DozeParameters dozeParameters, DevicePostureController devicePostureController, DozeLog dozeLog, SystemSettings systemSettings)105     public DozeScreenBrightness(
106             Context context,
107             @WrappedService DozeMachine.Service service,
108             AsyncSensorManager sensorManager,
109             @BrightnessSensor Optional<Sensor>[] lightSensorOptional,
110             DozeHost host, Handler handler,
111             AlwaysOnDisplayPolicy alwaysOnDisplayPolicy,
112             WakefulnessLifecycle wakefulnessLifecycle,
113             DozeParameters dozeParameters,
114             DevicePostureController devicePostureController,
115             DozeLog dozeLog,
116             SystemSettings systemSettings) {
117         mContext = context;
118         mDozeService = service;
119         mSensorManager = sensorManager;
120         mLightSensorOptional = lightSensorOptional;
121         mDevicePostureController = devicePostureController;
122         mDevicePosture = mDevicePostureController.getDevicePosture();
123         mWakefulnessLifecycle = wakefulnessLifecycle;
124         mDozeParameters = dozeParameters;
125         mDozeHost = host;
126         mHandler = handler;
127         mDozeLog = dozeLog;
128         mSystemSettings = systemSettings;
129 
130         mScreenBrightnessMinimumDimAmountFloat = context.getResources().getFloat(
131                 R.dimen.config_screenBrightnessMinimumDimAmountFloat);
132 
133         mDefaultDozeBrightness = alwaysOnDisplayPolicy.defaultDozeBrightness;
134         mScreenBrightnessDim = alwaysOnDisplayPolicy.dimBrightness;
135         mSensorToBrightness = alwaysOnDisplayPolicy.screenBrightnessArray;
136         mSensorToScrimOpacity = alwaysOnDisplayPolicy.dimmingScrimArray;
137 
138         mDevicePostureController.addCallback(mDevicePostureCallback);
139     }
140 
141     @Override
transitionTo(DozeMachine.State oldState, DozeMachine.State newState)142     public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
143         mState = newState;
144         switch (newState) {
145             case INITIALIZED:
146                 resetBrightnessToDefault();
147                 break;
148             case DOZE_AOD:
149             case DOZE_REQUEST_PULSE:
150             case DOZE_AOD_DOCKED:
151                 setLightSensorEnabled(true);
152                 break;
153             case DOZE:
154             case DOZE_SUSPEND_TRIGGERS:
155                 setLightSensorEnabled(false);
156                 resetBrightnessToDefault();
157                 break;
158             case DOZE_AOD_PAUSED:
159                 setLightSensorEnabled(false);
160                 break;
161             case FINISH:
162                 onDestroy();
163                 break;
164         }
165         if (newState != DozeMachine.State.FINISH) {
166             setScreenOff(newState == DozeMachine.State.DOZE);
167             setPaused(newState == DozeMachine.State.DOZE_AOD_PAUSED);
168         }
169     }
170 
onDestroy()171     private void onDestroy() {
172         setLightSensorEnabled(false);
173         mDevicePostureController.removeCallback(mDevicePostureCallback);
174     }
175 
176     @Override
onSensorChanged(SensorEvent event)177     public void onSensorChanged(SensorEvent event) {
178         if (Trace.isEnabled()) {
179             Trace.traceBegin(
180                     Trace.TRACE_TAG_APP, "DozeScreenBrightness.onSensorChanged" + event.values[0]);
181         }
182         try {
183             if (mRegistered) {
184                 mLastSensorValue = (int) event.values[0];
185                 updateBrightnessAndReady(false /* force */);
186             }
187         } finally {
188             Trace.endSection();
189         }
190     }
191 
updateBrightnessAndReady(boolean force)192     public void updateBrightnessAndReady(boolean force) {
193         if (force || mRegistered || mDebugBrightnessBucket != -1) {
194             int sensorValue = mDebugBrightnessBucket == -1
195                     ? mLastSensorValue : mDebugBrightnessBucket;
196             int brightness = computeBrightness(sensorValue);
197             boolean brightnessReady = brightness > 0;
198             if (brightnessReady) {
199                 mDozeService.setDozeScreenBrightness(
200                         clampToDimBrightnessForScreenOff(clampToUserSetting(brightness)));
201             }
202 
203             int scrimOpacity = -1;
204             if (!isLightSensorPresent()) {
205                 // No light sensor, scrims are always transparent.
206                 scrimOpacity = 0;
207             } else if (brightnessReady) {
208                 // Only unblank scrim once brightness is ready.
209                 scrimOpacity = computeScrimOpacity(sensorValue);
210             }
211             if (scrimOpacity >= 0) {
212                 mDozeHost.setAodDimmingScrim(scrimOpacity / 255f);
213             }
214         }
215     }
216 
lightSensorSupportsCurrentPosture()217     private boolean lightSensorSupportsCurrentPosture() {
218         return mLightSensorOptional != null
219                 && mDevicePosture < mLightSensorOptional.length;
220     }
221 
isLightSensorPresent()222     private boolean isLightSensorPresent() {
223         if (!lightSensorSupportsCurrentPosture()) {
224             return mLightSensorOptional != null && mLightSensorOptional[0].isPresent();
225         }
226 
227         return mLightSensorOptional[mDevicePosture].isPresent();
228     }
229 
getLightSensor()230     private Sensor getLightSensor() {
231         if (!lightSensorSupportsCurrentPosture()) {
232             return null;
233         }
234 
235         return mLightSensorOptional[mDevicePosture].get();
236     }
237 
computeScrimOpacity(int sensorValue)238     private int computeScrimOpacity(int sensorValue) {
239         if (sensorValue < 0 || sensorValue >= mSensorToScrimOpacity.length) {
240             return -1;
241         }
242         return mSensorToScrimOpacity[sensorValue];
243     }
244 
computeBrightness(int sensorValue)245     private int computeBrightness(int sensorValue) {
246         if (sensorValue < 0 || sensorValue >= mSensorToBrightness.length) {
247             return -1;
248         }
249         return mSensorToBrightness[sensorValue];
250     }
251 
252     @Override
onAccuracyChanged(Sensor sensor, int accuracy)253     public void onAccuracyChanged(Sensor sensor, int accuracy) {
254     }
255 
resetBrightnessToDefault()256     private void resetBrightnessToDefault() {
257         mDozeService.setDozeScreenBrightness(
258                 clampToDimBrightnessForScreenOff(
259                         clampToUserSetting(mDefaultDozeBrightness)));
260         mDozeHost.setAodDimmingScrim(0f);
261     }
262     //TODO: brightnessfloat change usages to float.
clampToUserSetting(int brightness)263     private int clampToUserSetting(int brightness) {
264         int screenBrightnessModeSetting = mSystemSettings.getIntForUser(
265                 Settings.System.SCREEN_BRIGHTNESS_MODE,
266                 Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL, UserHandle.USER_CURRENT);
267         if (screenBrightnessModeSetting == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
268             return brightness;
269         }
270 
271         int userSetting = mSystemSettings.getIntForUser(
272                 Settings.System.SCREEN_BRIGHTNESS, Integer.MAX_VALUE,
273                 UserHandle.USER_CURRENT);
274         return Math.min(brightness, userSetting);
275     }
276 
277     /**
278      * Clamp the brightness to the dim brightness value used by PowerManagerService just before the
279      * device times out and goes to sleep, if we are sleeping from a timeout. This ensures that we
280      * don't raise the brightness back to the user setting before or during the screen off
281      * animation.
282      */
clampToDimBrightnessForScreenOff(int brightness)283     private int clampToDimBrightnessForScreenOff(int brightness) {
284         final boolean screenTurningOff =
285                 (mDozeParameters.shouldClampToDimBrightness()
286                         || mWakefulnessLifecycle.getWakefulness() == WAKEFULNESS_GOING_TO_SLEEP)
287                 && mState == DozeMachine.State.INITIALIZED;
288         if (screenTurningOff
289                 && mWakefulnessLifecycle.getLastSleepReason() == GO_TO_SLEEP_REASON_TIMEOUT) {
290             return Math.max(
291                     PowerManager.BRIGHTNESS_OFF,
292                     // Use the lower of either the dim brightness, or the current brightness reduced
293                     // by the minimum dim amount. This is the same logic used in
294                     // DisplayPowerController#updatePowerState to apply a minimum dim amount.
295                     Math.min(
296                             brightness - (int) Math.floor(
297                                     mScreenBrightnessMinimumDimAmountFloat * 255),
298                             mScreenBrightnessDim));
299         } else {
300             return brightness;
301         }
302     }
303 
setLightSensorEnabled(boolean enabled)304     private void setLightSensorEnabled(boolean enabled) {
305         if (enabled && !mRegistered && isLightSensorPresent()) {
306             // Wait until we get an event from the sensor until indicating ready.
307             mRegistered = mSensorManager.registerListener(this, getLightSensor(),
308                     SensorManager.SENSOR_DELAY_NORMAL, mHandler);
309             mLastSensorValue = -1;
310         } else if (!enabled && mRegistered) {
311             mSensorManager.unregisterListener(this);
312             mRegistered = false;
313             mLastSensorValue = -1;
314             // Sensor is not enabled, hence we use the default brightness and are always ready.
315         }
316     }
317 
setPaused(boolean paused)318     private void setPaused(boolean paused) {
319         if (mPaused != paused) {
320             mPaused = paused;
321             updateBrightnessAndReady(false /* force */);
322         }
323     }
324 
setScreenOff(boolean screenOff)325     private void setScreenOff(boolean screenOff) {
326         if (mScreenOff != screenOff) {
327             mScreenOff = screenOff;
328             updateBrightnessAndReady(true /* force */);
329         }
330     }
331 
332     @Override
onReceive(Context context, Intent intent)333     public void onReceive(Context context, Intent intent) {
334         mDebugBrightnessBucket = intent.getIntExtra(BRIGHTNESS_BUCKET, -1);
335         updateBrightnessAndReady(false /* force */);
336     }
337 
338     /** Dump current state */
dump(PrintWriter pw)339     public void dump(PrintWriter pw) {
340         pw.println("DozeScreenBrightness:");
341         IndentingPrintWriter idpw = new IndentingPrintWriter(pw);
342         idpw.increaseIndent();
343         idpw.println("registered=" + mRegistered);
344         idpw.println("posture=" + DevicePostureController.devicePostureToString(mDevicePosture));
345     }
346 
347     private final DevicePostureController.Callback mDevicePostureCallback =
348             new DevicePostureController.Callback() {
349         @Override
350         public void onPostureChanged(int posture) {
351             if (mDevicePosture == posture
352                     || mLightSensorOptional.length < 2
353                     || posture >= mLightSensorOptional.length) {
354                 return;
355             }
356 
357             final Sensor oldSensor = mLightSensorOptional[mDevicePosture].get();
358             final Sensor newSensor = mLightSensorOptional[posture].get();
359             if (Objects.equals(oldSensor, newSensor)) {
360                 mDevicePosture = posture;
361                 // uses the same sensor for the new posture
362                 return;
363             }
364 
365             // cancel the previous sensor:
366             if (mRegistered) {
367                 setLightSensorEnabled(false);
368                 mDevicePosture = posture;
369                 setLightSensorEnabled(true);
370             } else {
371                 mDevicePosture = posture;
372             }
373             mDozeLog.tracePostureChanged(mDevicePosture, "DozeScreenBrightness swap "
374                     + "{" + oldSensor + "} => {" + newSensor + "}, mRegistered=" + mRegistered);
375         }
376     };
377 }
378