1 /*
2  * Copyright (C) 2022 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.server.display.brightness;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.hardware.SensorManager;
22 import android.hardware.display.DisplayManagerInternal;
23 import android.os.Handler;
24 import android.os.HandlerExecutor;
25 import android.os.PowerManager;
26 import android.util.IndentingPrintWriter;
27 import android.view.Display;
28 
29 import com.android.internal.annotations.GuardedBy;
30 import com.android.internal.annotations.VisibleForTesting;
31 import com.android.internal.display.BrightnessSynchronizer;
32 import com.android.server.display.AutomaticBrightnessController;
33 import com.android.server.display.BrightnessMappingStrategy;
34 import com.android.server.display.BrightnessSetting;
35 import com.android.server.display.DisplayBrightnessState;
36 import com.android.server.display.DisplayDeviceConfig;
37 import com.android.server.display.brightness.strategy.AutoBrightnessFallbackStrategy;
38 import com.android.server.display.brightness.strategy.AutomaticBrightnessStrategy2;
39 import com.android.server.display.brightness.strategy.DisplayBrightnessStrategy;
40 import com.android.server.display.feature.DisplayManagerFlags;
41 
42 import java.io.PrintWriter;
43 
44 /**
45  * Deploys different DozeBrightnessStrategy to choose the current brightness for a specified
46  * display. Applies the chosen brightness.
47  */
48 public final class DisplayBrightnessController {
49 
50     // The ID of the display tied to this DisplayBrightnessController
51     private final int mDisplayId;
52 
53     // The lock which is to be used to synchronize the resources being used in this class
54     private final Object mLock = new Object();
55 
56     // The default screen brightness to be used when no value is available in BrightnessSetting.
57     private final float mScreenBrightnessDefault;
58 
59     // This is used to persist the changes happening to the brightness.
60     private final BrightnessSetting mBrightnessSetting;
61 
62     // A runnable to update the clients registered via DisplayManagerGlobal
63     // .EVENT_DISPLAY_BRIGHTNESS_CHANGED about the brightness change. Called when
64     // mCurrentScreenBrightness is updated.
65     private Runnable mOnBrightnessChangeRunnable;
66 
67     // The screen brightness that has changed but not taken effect yet. If this is different
68     // from the current screen brightness then this is coming from something other than us
69     // and should be considered a user interaction.
70     @GuardedBy("mLock")
71     private float mPendingScreenBrightness;
72 
73     // The last observed screen brightness, either set by us or by the settings app on
74     // behalf of the user.
75     @GuardedBy("mLock")
76     private float mCurrentScreenBrightness;
77 
78     // The last brightness that was set by the user and not temporary. Set to
79     // PowerManager.BRIGHTNESS_INVALID_FLOAT when a brightness has yet to be recorded.
80     @GuardedBy("mLock")
81     private float mLastUserSetScreenBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT;
82 
83     // Represents if the system has adjusted the brightness based on the user suggested value. Will
84     // be false if the brightness change is coming from a non-user source
85     private boolean mUserSetScreenBrightnessUpdated;
86 
87     // The listener which is to be notified everytime there is a change in the brightness in the
88     // BrightnessSetting.
89     private BrightnessSetting.BrightnessSettingListener mBrightnessSettingListener;
90 
91     // Selects an appropriate strategy based on the request provided by the clients.
92     @GuardedBy("mLock")
93     private DisplayBrightnessStrategySelector mDisplayBrightnessStrategySelector;
94 
95     // Currently selected DisplayBrightnessStrategy.
96     @GuardedBy("mLock")
97     private DisplayBrightnessStrategy mDisplayBrightnessStrategy;
98 
99     // The executor on which the mOnBrightnessChangeRunnable is executed. This ensures that the
100     // callback is not executed in sync and is not blocking the thread from which it is called.
101     private final HandlerExecutor mBrightnessChangeExecutor;
102 
103     // True if we want to persist the brightness value in nits even if the underlying display
104     // device changes.
105     private final boolean mPersistBrightnessNitsForDefaultDisplay;
106 
107     // The controller for the automatic brightness level.
108     // TODO(b/265415257): Move to the automatic brightness strategy
109     @Nullable
110     @VisibleForTesting
111     AutomaticBrightnessController mAutomaticBrightnessController;
112 
113     /**
114      * The constructor of DisplayBrightnessController.
115      */
DisplayBrightnessController(Context context, Injector injector, int displayId, float defaultScreenBrightness, BrightnessSetting brightnessSetting, Runnable onBrightnessChangeRunnable, HandlerExecutor brightnessChangeExecutor, DisplayManagerFlags flags)116     public DisplayBrightnessController(Context context, Injector injector, int displayId,
117             float defaultScreenBrightness, BrightnessSetting brightnessSetting,
118             Runnable onBrightnessChangeRunnable, HandlerExecutor brightnessChangeExecutor,
119             DisplayManagerFlags flags) {
120         if (injector == null) {
121             injector = new Injector();
122         }
123         mDisplayId = displayId;
124         // TODO: b/186428377 update brightness setting when display changes
125         mBrightnessSetting = brightnessSetting;
126         mPendingScreenBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT;
127         mScreenBrightnessDefault = BrightnessUtils.clampAbsoluteBrightness(defaultScreenBrightness);
128         mCurrentScreenBrightness = getScreenBrightnessSetting();
129         mOnBrightnessChangeRunnable = onBrightnessChangeRunnable;
130         mDisplayBrightnessStrategySelector = injector.getDisplayBrightnessStrategySelector(context,
131                 displayId, flags);
132         mBrightnessChangeExecutor = brightnessChangeExecutor;
133         mPersistBrightnessNitsForDefaultDisplay = context.getResources().getBoolean(
134                 com.android.internal.R.bool.config_persistBrightnessNitsForDefaultDisplay);
135     }
136 
137     /**
138      * Updates the display brightness. This delegates the responsibility of selecting an appropriate
139      * strategy to DisplayBrightnessStrategySelector, which is then applied to evaluate the
140      * DisplayBrightnessState. In the future,
141      * 1. This will account for clamping the brightness if needed.
142      * 2. This will notify the system about the updated brightness
143      *
144      * @param displayPowerRequest The request to update the brightness
145      * @param targetDisplayState  The target display state of the system
146      */
updateBrightness( DisplayManagerInternal.DisplayPowerRequest displayPowerRequest, int targetDisplayState, DisplayManagerInternal.DisplayOffloadSession displayOffloadSession)147     public DisplayBrightnessState updateBrightness(
148             DisplayManagerInternal.DisplayPowerRequest displayPowerRequest,
149             int targetDisplayState,
150             DisplayManagerInternal.DisplayOffloadSession displayOffloadSession) {
151         DisplayBrightnessState state;
152         synchronized (mLock) {
153             mDisplayBrightnessStrategy = mDisplayBrightnessStrategySelector.selectStrategy(
154                     constructStrategySelectionRequest(displayPowerRequest, targetDisplayState,
155                             displayOffloadSession));
156             state = mDisplayBrightnessStrategy
157                         .updateBrightness(constructStrategyExecutionRequest(displayPowerRequest));
158         }
159 
160         // This is a temporary measure until AutomaticBrightnessStrategy works as a traditional
161         // strategy.
162         // TODO: Remove when AutomaticBrightnessStrategy is populating the values directly.
163         if (state != null) {
164             state = addAutomaticBrightnessState(state);
165         }
166         return state;
167     }
168 
169     /**
170      * Sets the temporary brightness
171      */
setTemporaryBrightness(Float temporaryBrightness)172     public void setTemporaryBrightness(Float temporaryBrightness) {
173         synchronized (mLock) {
174             setTemporaryBrightnessLocked(temporaryBrightness);
175         }
176     }
177 
178     /**
179      * Sets the brightness to follow
180      */
setBrightnessToFollow(float brightnessToFollow, boolean slowChange)181     public void setBrightnessToFollow(float brightnessToFollow, boolean slowChange) {
182         synchronized (mLock) {
183             mDisplayBrightnessStrategySelector.getFollowerDisplayBrightnessStrategy()
184                     .setBrightnessToFollow(brightnessToFollow, slowChange);
185         }
186     }
187 
188     /**
189      * Sets the brightness from the offload session.
190      * @return Whether the offload brightness has changed
191      */
setBrightnessFromOffload(float brightness)192     public boolean setBrightnessFromOffload(float brightness) {
193         synchronized (mLock) {
194             if (mDisplayBrightnessStrategySelector.getOffloadBrightnessStrategy() != null
195                     && !BrightnessSynchronizer.floatEquals(mDisplayBrightnessStrategySelector
196                     .getOffloadBrightnessStrategy().getOffloadScreenBrightness(), brightness)) {
197                 mDisplayBrightnessStrategySelector.getOffloadBrightnessStrategy()
198                         .setOffloadScreenBrightness(brightness);
199                 return true;
200             }
201         }
202         return false;
203     }
204 
205     /**
206      * Returns a boolean flag indicating if the light sensor is to be used to decide the screen
207      * brightness when dozing
208      */
isAllowAutoBrightnessWhileDozing()209     public boolean isAllowAutoBrightnessWhileDozing() {
210         synchronized (mLock) {
211             return mDisplayBrightnessStrategySelector.isAllowAutoBrightnessWhileDozing();
212         }
213     }
214 
215     /**
216      * Returns the config value indicating the auto brightness while dozing is to be
217      * allowed ot not. Note that this is a config value, but the actual status can differ from this.
218      */
isAllowAutoBrightnessWhileDozingConfig()219     public boolean isAllowAutoBrightnessWhileDozingConfig() {
220         synchronized (mLock) {
221             return mDisplayBrightnessStrategySelector.isAllowAutoBrightnessWhileDozingConfig();
222         }
223     }
224 
225     /**
226      * Sets the current screen brightness to the supplied value, and notifies all the listeners
227      * requesting for change events on brightness change.
228      */
setAndNotifyCurrentScreenBrightness(float brightnessValue)229     public void setAndNotifyCurrentScreenBrightness(float brightnessValue) {
230         final boolean hasBrightnessChanged;
231         synchronized (mLock) {
232             hasBrightnessChanged = (brightnessValue != mCurrentScreenBrightness);
233             setCurrentScreenBrightnessLocked(brightnessValue);
234         }
235         if (hasBrightnessChanged) {
236             notifyCurrentScreenBrightness();
237         }
238     }
239 
240     /**
241      * Returns the last observed screen brightness.
242      */
getCurrentBrightness()243     public float getCurrentBrightness() {
244         synchronized (mLock) {
245             return mCurrentScreenBrightness;
246         }
247     }
248 
249     /**
250      * Returns the screen brightness which has changed but has not taken any effect so far.
251      */
getPendingScreenBrightness()252     public float getPendingScreenBrightness() {
253         synchronized (mLock) {
254             return mPendingScreenBrightness;
255         }
256     }
257 
258     /**
259      * Sets the pending screen brightness setting, representing a value which is requested, but not
260      * yet processed.
261      * @param brightnessValue The value to which the pending screen brightness is to be set.
262      */
setPendingScreenBrightness(float brightnessValue)263     public void setPendingScreenBrightness(float brightnessValue) {
264         synchronized (mLock) {
265             mPendingScreenBrightness = brightnessValue;
266         }
267     }
268 
269     /**
270      * Returns if the system has adjusted the brightness based on the user suggested value. Will
271      * be false if the brightness change is coming from a non-user source.
272      *
273      * Todo: 294444204 This is a temporary workaround, and should be moved to the manual brightness
274      * strategy once that is introduced
275      */
getIsUserSetScreenBrightnessUpdated()276     public boolean getIsUserSetScreenBrightnessUpdated() {
277         return mUserSetScreenBrightnessUpdated;
278     }
279 
280     /**
281      * Registers the BrightnessSettingListener with the BrightnessSetting, which will be notified
282      * everytime there is a change in the brightness.
283      */
registerBrightnessSettingChangeListener( BrightnessSetting.BrightnessSettingListener brightnessSettingListener)284     public void registerBrightnessSettingChangeListener(
285             BrightnessSetting.BrightnessSettingListener brightnessSettingListener) {
286         mBrightnessSettingListener = brightnessSettingListener;
287         mBrightnessSetting.registerListener(mBrightnessSettingListener);
288     }
289 
290     /**
291      * Returns the last user set brightness which is not temporary.
292      */
getLastUserSetScreenBrightness()293     public float getLastUserSetScreenBrightness() {
294         synchronized (mLock) {
295             return mLastUserSetScreenBrightness;
296         }
297     }
298 
299     /**
300      * Returns the current screen brightnessSetting which is responsible for saving the brightness
301      * in the persistent store
302      */
getScreenBrightnessSetting()303     public float getScreenBrightnessSetting() {
304         float brightness = mBrightnessSetting.getBrightness();
305         synchronized (mLock) {
306             if (Float.isNaN(brightness)) {
307                 brightness = mScreenBrightnessDefault;
308             }
309             return BrightnessUtils.clampAbsoluteBrightness(brightness);
310         }
311     }
312 
313     /**
314      * Notifies the brightnessSetting to persist the supplied brightness value.
315      */
setBrightness(float brightnessValue, float maxBrightness)316     public void setBrightness(float brightnessValue, float maxBrightness) {
317         // Update the setting, which will eventually call back into DPC to have us actually
318         // update the display with the new value.
319         mBrightnessSetting.setBrightness(brightnessValue);
320         if (mDisplayId == Display.DEFAULT_DISPLAY && mPersistBrightnessNitsForDefaultDisplay) {
321             float nits = convertToNits(brightnessValue);
322             float currentlyStoredNits = mBrightnessSetting.getBrightnessNitsForDefaultDisplay();
323             // Don't override settings if the brightness is set to max, but the currently
324             // stored value is greater. On multi-screen device, when switching between a
325             // screen with a wider brightness range and one with a narrower brightness range,
326             // the stored value shouldn't change.
327             if (nits >= 0 && !(brightnessValue == maxBrightness && currentlyStoredNits > nits)) {
328                 mBrightnessSetting.setBrightnessNitsForDefaultDisplay(nits);
329             }
330         }
331     }
332 
333     /**
334      * Notifies the brightnessSetting to persist the supplied brightness value for a user.
335      */
setBrightness(float brightnessValue, int userSerial, float maxBrightness)336     public void setBrightness(float brightnessValue, int userSerial, float maxBrightness) {
337         mBrightnessSetting.setUserSerial(userSerial);
338         setBrightness(brightnessValue, maxBrightness);
339     }
340 
341     /**
342      * Sets the current screen brightness, and notifies the BrightnessSetting about the change.
343      */
updateScreenBrightnessSetting(float brightnessValue, float maxBrightness)344     public void updateScreenBrightnessSetting(float brightnessValue, float maxBrightness) {
345         synchronized (mLock) {
346             if (!BrightnessUtils.isValidBrightnessValue(brightnessValue)
347                     || brightnessValue == mCurrentScreenBrightness) {
348                 return;
349             }
350             setCurrentScreenBrightnessLocked(brightnessValue);
351         }
352         notifyCurrentScreenBrightness();
353         setBrightness(brightnessValue, maxBrightness);
354     }
355 
356     /**
357      * Sets up the auto brightness and the relevant state for the associated display
358      */
setUpAutoBrightness(AutomaticBrightnessController automaticBrightnessController, SensorManager sensorManager, DisplayDeviceConfig displayDeviceConfig, Handler handler, BrightnessMappingStrategy brightnessMappingStrategy, boolean isEnabled, int leadDisplayId)359     public void setUpAutoBrightness(AutomaticBrightnessController automaticBrightnessController,
360             SensorManager sensorManager,
361             DisplayDeviceConfig displayDeviceConfig, Handler handler,
362             BrightnessMappingStrategy brightnessMappingStrategy, boolean isEnabled,
363             int leadDisplayId) {
364         setAutomaticBrightnessController(automaticBrightnessController);
365         setUpAutoBrightnessFallbackStrategy(sensorManager, displayDeviceConfig, handler,
366                 brightnessMappingStrategy, isEnabled, leadDisplayId);
367     }
368 
369     /**
370      * TODO(b/253226419): Remove once auto-brightness is a fully-functioning strategy.
371      */
getAutomaticBrightnessStrategy()372     public AutomaticBrightnessStrategy2 getAutomaticBrightnessStrategy() {
373         return mDisplayBrightnessStrategySelector.getAutomaticBrightnessStrategy();
374     }
375 
376     /**
377      * Convert a brightness float scale value to a nit value. Adjustments, such as RBC, are not
378      * applied. This is used when storing the brightness in nits for the default display and when
379      * passing the brightness value to follower displays.
380      *
381      * @param brightness The float scale value
382      * @return The nit value or {@link BrightnessMappingStrategy.INVALID_NITS} if no conversion is
383      * possible.
384      */
convertToNits(float brightness)385     public float convertToNits(float brightness) {
386         if (mAutomaticBrightnessController == null) {
387             return BrightnessMappingStrategy.INVALID_NITS;
388         }
389         return mAutomaticBrightnessController.convertToNits(brightness);
390     }
391 
392     /**
393      * Convert a brightness float scale value to a nit value. Adjustments, such as RBC are applied.
394      * This is used when sending the brightness value to
395      * {@link com.android.server.display.BrightnessTracker}.
396      *
397      * @param brightness The float scale value
398      * @return The nit value or {@link BrightnessMappingStrategy.INVALID_NITS} if no conversion is
399      * possible.
400      */
convertToAdjustedNits(float brightness)401     public float convertToAdjustedNits(float brightness) {
402         if (mAutomaticBrightnessController == null) {
403             return BrightnessMappingStrategy.INVALID_NITS;
404         }
405         return mAutomaticBrightnessController.convertToAdjustedNits(brightness);
406     }
407 
408     /**
409      * Convert a brightness nit value to a float scale value. It is assumed that the nit value
410      * provided does not have adjustments, such as RBC, applied.
411      *
412      * @param nits The nit value
413      * @return The float scale value or {@link PowerManager.BRIGHTNESS_INVALID_FLOAT} if no
414      * conversion is possible.
415      */
getBrightnessFromNits(float nits)416     public float getBrightnessFromNits(float nits) {
417         if (mAutomaticBrightnessController == null) {
418             return PowerManager.BRIGHTNESS_INVALID_FLOAT;
419         }
420         return mAutomaticBrightnessController.getBrightnessFromNits(nits);
421     }
422 
423     /**
424      * Stops the associated listeners when the display is stopped. Invoked when the {@link
425      * #mDisplayId} is being removed.
426      */
stop()427     public void stop() {
428         if (mBrightnessSetting != null) {
429             mBrightnessSetting.unregisterListener(mBrightnessSettingListener);
430         }
431         AutoBrightnessFallbackStrategy autoBrightnessFallbackStrategy =
432                 getAutoBrightnessFallbackStrategy();
433         if (autoBrightnessFallbackStrategy != null) {
434             autoBrightnessFallbackStrategy.stop();
435         }
436     }
437 
getAutoBrightnessFallbackStrategy()438     private AutoBrightnessFallbackStrategy getAutoBrightnessFallbackStrategy() {
439         synchronized (mLock) {
440             return mDisplayBrightnessStrategySelector.getAutoBrightnessFallbackStrategy();
441         }
442     }
443 
444     /**
445      * Used to dump the state.
446      *
447      * @param writer The PrintWriter used to dump the state.
448      */
dump(PrintWriter writer)449     public void dump(PrintWriter writer) {
450         writer.println();
451         writer.println("DisplayBrightnessController:");
452         writer.println("  mDisplayId=: " + mDisplayId);
453         writer.println("  mScreenBrightnessDefault=" + mScreenBrightnessDefault);
454         writer.println("  mPersistBrightnessNitsForDefaultDisplay="
455                 + mPersistBrightnessNitsForDefaultDisplay);
456         synchronized (mLock) {
457             writer.println("  mPendingScreenBrightness=" + mPendingScreenBrightness);
458             writer.println("  mCurrentScreenBrightness=" + mCurrentScreenBrightness);
459             writer.println("  mLastUserSetScreenBrightness="
460                     + mLastUserSetScreenBrightness);
461             if (mDisplayBrightnessStrategy != null) {
462                 writer.println("  Last selected DisplayBrightnessStrategy= "
463                         + mDisplayBrightnessStrategy.getName());
464             }
465             IndentingPrintWriter ipw = new IndentingPrintWriter(writer, " ");
466             mDisplayBrightnessStrategySelector.dump(ipw);
467         }
468     }
469 
470     /**
471      * We want to return true if the user has set the screen brightness.
472      * RBC on, off, and intensity changes will return false.
473      * Slider interactions whilst in RBC will return true, just as when in non-rbc.
474      */
475     @VisibleForTesting
updateUserSetScreenBrightness()476     boolean updateUserSetScreenBrightness() {
477         mUserSetScreenBrightnessUpdated = false;
478         synchronized (mLock) {
479             if (!BrightnessUtils.isValidBrightnessValue(mPendingScreenBrightness)) {
480                 return false;
481             }
482             if (mCurrentScreenBrightness == mPendingScreenBrightness) {
483                 mPendingScreenBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT;
484                 setTemporaryBrightnessLocked(PowerManager.BRIGHTNESS_INVALID_FLOAT);
485                 return false;
486             }
487             setCurrentScreenBrightnessLocked(mPendingScreenBrightness);
488             mLastUserSetScreenBrightness = mPendingScreenBrightness;
489             mPendingScreenBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT;
490             setTemporaryBrightnessLocked(PowerManager.BRIGHTNESS_INVALID_FLOAT);
491         }
492         notifyCurrentScreenBrightness();
493         mUserSetScreenBrightnessUpdated = true;
494         return true;
495     }
496 
497     @VisibleForTesting
498     static class Injector {
getDisplayBrightnessStrategySelector(Context context, int displayId, DisplayManagerFlags flags)499         DisplayBrightnessStrategySelector getDisplayBrightnessStrategySelector(Context context,
500                 int displayId, DisplayManagerFlags flags) {
501             return new DisplayBrightnessStrategySelector(context, /* injector= */ null, displayId,
502                     flags);
503         }
504     }
505 
506     @VisibleForTesting
getBrightnessSettingListener()507     BrightnessSetting.BrightnessSettingListener getBrightnessSettingListener() {
508         return mBrightnessSettingListener;
509     }
510 
511     /**
512      * Returns the current selected DisplayBrightnessStrategy
513      */
514     @VisibleForTesting
getCurrentDisplayBrightnessStrategy()515     DisplayBrightnessStrategy getCurrentDisplayBrightnessStrategy() {
516         synchronized (mLock) {
517             return mDisplayBrightnessStrategy;
518         }
519     }
520 
521     /**
522      * Set the {@link AutomaticBrightnessController} which is needed to perform nit-to-float-scale
523      * conversion.
524      * @param automaticBrightnessController The ABC
525      */
526     @VisibleForTesting
setAutomaticBrightnessController( AutomaticBrightnessController automaticBrightnessController)527     void setAutomaticBrightnessController(
528             AutomaticBrightnessController automaticBrightnessController) {
529         mAutomaticBrightnessController = automaticBrightnessController;
530         getAutomaticBrightnessStrategy()
531                 .setAutomaticBrightnessController(automaticBrightnessController);
532         loadNitBasedBrightnessSetting();
533     }
534 
setUpAutoBrightnessFallbackStrategy(SensorManager sensorManager, DisplayDeviceConfig displayDeviceConfig, Handler handler, BrightnessMappingStrategy brightnessMappingStrategy, boolean isEnabled, int leadDisplayId)535     private void setUpAutoBrightnessFallbackStrategy(SensorManager sensorManager,
536             DisplayDeviceConfig displayDeviceConfig, Handler handler,
537             BrightnessMappingStrategy brightnessMappingStrategy, boolean isEnabled,
538             int leadDisplayId) {
539         AutoBrightnessFallbackStrategy autoBrightnessFallbackStrategy =
540                 getAutoBrightnessFallbackStrategy();
541         if (autoBrightnessFallbackStrategy != null) {
542             autoBrightnessFallbackStrategy.setupAutoBrightnessFallbackSensor(
543                     sensorManager, displayDeviceConfig, handler, brightnessMappingStrategy,
544                     isEnabled, leadDisplayId);
545         }
546     }
547 
548     /**
549      * TODO(b/253226419): Remove once auto-brightness is a fully-functioning strategy.
550      */
addAutomaticBrightnessState(DisplayBrightnessState state)551     private DisplayBrightnessState addAutomaticBrightnessState(DisplayBrightnessState state) {
552         AutomaticBrightnessStrategy2 autoStrat = getAutomaticBrightnessStrategy();
553 
554         DisplayBrightnessState.Builder builder = DisplayBrightnessState.Builder.from(state);
555         builder.setShouldUseAutoBrightness(
556                 autoStrat != null && autoStrat.shouldUseAutoBrightness());
557         return builder.build();
558     }
559 
560     @GuardedBy("mLock")
setTemporaryBrightnessLocked(float temporaryBrightness)561     private void setTemporaryBrightnessLocked(float temporaryBrightness) {
562         mDisplayBrightnessStrategySelector.getTemporaryDisplayBrightnessStrategy()
563                 .setTemporaryScreenBrightness(temporaryBrightness);
564     }
565 
566     @GuardedBy("mLock")
setCurrentScreenBrightnessLocked(float brightnessValue)567     private void setCurrentScreenBrightnessLocked(float brightnessValue) {
568         if (brightnessValue != mCurrentScreenBrightness) {
569             mCurrentScreenBrightness = brightnessValue;
570         }
571     }
572 
notifyCurrentScreenBrightness()573     private void notifyCurrentScreenBrightness() {
574         mBrightnessChangeExecutor.execute(mOnBrightnessChangeRunnable);
575     }
576 
577     /**
578      * Loads the brightness value. If this is the default display and the config says that we should
579      * persist the nit value, the nit value for the default display will be loaded.
580      */
loadNitBasedBrightnessSetting()581     private void loadNitBasedBrightnessSetting() {
582         float currentBrightnessSetting = Float.NaN;
583         if (mDisplayId == Display.DEFAULT_DISPLAY && mPersistBrightnessNitsForDefaultDisplay) {
584             float brightnessNitsForDefaultDisplay =
585                     mBrightnessSetting.getBrightnessNitsForDefaultDisplay();
586             if (brightnessNitsForDefaultDisplay >= 0) {
587                 float brightnessForDefaultDisplay = getBrightnessFromNits(
588                         brightnessNitsForDefaultDisplay);
589                 if (BrightnessUtils.isValidBrightnessValue(brightnessForDefaultDisplay)) {
590                     mBrightnessSetting.setBrightnessNoNotify(brightnessForDefaultDisplay);
591                     currentBrightnessSetting = brightnessForDefaultDisplay;
592                 }
593             }
594         }
595 
596         if (Float.isNaN(currentBrightnessSetting)) {
597             currentBrightnessSetting = getScreenBrightnessSetting();
598         }
599 
600         synchronized (mLock) {
601             mCurrentScreenBrightness = currentBrightnessSetting;
602         }
603     }
604 
constructStrategySelectionRequest( DisplayManagerInternal.DisplayPowerRequest displayPowerRequest, int targetDisplayState, DisplayManagerInternal.DisplayOffloadSession displayOffloadSession)605     private StrategySelectionRequest constructStrategySelectionRequest(
606             DisplayManagerInternal.DisplayPowerRequest displayPowerRequest,
607             int targetDisplayState,
608             DisplayManagerInternal.DisplayOffloadSession displayOffloadSession) {
609         boolean userSetBrightnessChanged = updateUserSetScreenBrightness();
610         float lastUserSetScreenBrightness;
611         synchronized (mLock) {
612             lastUserSetScreenBrightness = mLastUserSetScreenBrightness;
613         }
614         return new StrategySelectionRequest(displayPowerRequest, targetDisplayState,
615                 lastUserSetScreenBrightness, userSetBrightnessChanged, displayOffloadSession);
616     }
617 
constructStrategyExecutionRequest( DisplayManagerInternal.DisplayPowerRequest displayPowerRequest)618     private StrategyExecutionRequest constructStrategyExecutionRequest(
619             DisplayManagerInternal.DisplayPowerRequest displayPowerRequest) {
620         float currentScreenBrightness = getCurrentBrightness();
621         return new StrategyExecutionRequest(displayPowerRequest, currentScreenBrightness,
622                 mUserSetScreenBrightnessUpdated);
623     }
624 }
625