Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 34afdf02 authored by Filip Gruszczynski's avatar Filip Gruszczynski Committed by Android (Google) Code Review
Browse files

Merge "Automatic brightness using ALS while dozing." into lmp-mr1-dev

parents 4255ec5c a15aa7d4
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -801,6 +801,13 @@
         that can be set by the user. -->
    <integer name="config_screenBrightnessDoze">1</integer>

    <!-- Allow automatic adjusting of the screen brightness while dozing in low power state. -->
    <bool name="config_allowAutoBrightnessWhileDozing">false</bool>

    <!-- If we allow automatic adjustment of screen brightness while dozing, how many times we want
         to reduce it to preserve the battery. Value of 100% means no scaling. -->
    <fraction name="config_screenAutoBrightnessDozeScaleFactor">100%</fraction>

    <!-- Screen brightness used to dim the screen when the user activity
         timeout expires.  May be less than the minimum allowed brightness setting
         that can be set by the user. -->
+2 −0
Original line number Diff line number Diff line
@@ -1574,6 +1574,7 @@
  <java-symbol type="bool" name="config_enableNetworkLocationOverlay" />
  <java-symbol type="bool" name="config_sf_limitedAlpha" />
  <java-symbol type="bool" name="config_unplugTurnsOnScreen" />
  <java-symbol type="bool" name="config_allowAutoBrightnessWhileDozing" />
  <java-symbol type="bool" name="config_allowTheaterModeWakeFromUnplug" />
  <java-symbol type="bool" name="config_allowTheaterModeWakeFromGesture" />
  <java-symbol type="bool" name="config_allowTheaterModeWakeFromCameraLens" />
@@ -1615,6 +1616,7 @@
  <java-symbol type="id" name="replace_app_icon" />
  <java-symbol type="id" name="replace_message" />
  <java-symbol type="fraction" name="config_dimBehindFadeDuration" />
  <java-symbol type="fraction" name="config_screenAutoBrightnessDozeScaleFactor" />
  <java-symbol type="integer" name="config_carDockKeepsScreenOn" />
  <java-symbol type="integer" name="config_criticalBatteryWarningLevel" />
  <java-symbol type="integer" name="config_datause_notification_type" />
+19 −5
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import com.android.server.twilight.TwilightListener;
import com.android.server.twilight.TwilightManager;
import com.android.server.twilight.TwilightState;

import android.content.res.Resources;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
@@ -120,6 +119,7 @@ class AutomaticBrightnessController {
    // The minimum and maximum screen brightnesses.
    private final int mScreenBrightnessRangeMinimum;
    private final int mScreenBrightnessRangeMaximum;
    private final float mDozeScaleFactor;

    // Amount of time to delay auto-brightness after screen on while waiting for
    // the light sensor to warm-up in milliseconds.
@@ -171,9 +171,12 @@ class AutomaticBrightnessController {
    // The last screen auto-brightness gamma.  (For printing in dump() only.)
    private float mLastScreenAutoBrightnessGamma = 1.0f;

    // Are we going to adjust brightness while dozing.
    private boolean mDozing;

    public AutomaticBrightnessController(Callbacks callbacks, Looper looper,
            SensorManager sensorManager, Spline autoBrightnessSpline,
            int lightSensorWarmUpTime, int brightnessMin, int brightnessMax) {
            SensorManager sensorManager, Spline autoBrightnessSpline, int lightSensorWarmUpTime,
            int brightnessMin, int brightnessMax, float dozeScaleFactor) {
        mCallbacks = callbacks;
        mTwilight = LocalServices.getService(TwilightManager.class);
        mSensorManager = sensorManager;
@@ -181,6 +184,7 @@ class AutomaticBrightnessController {
        mScreenBrightnessRangeMinimum = brightnessMin;
        mScreenBrightnessRangeMaximum = brightnessMax;
        mLightSensorWarmUpTimeConfig = lightSensorWarmUpTime;
        mDozeScaleFactor = dozeScaleFactor;

        mHandler = new AutomaticBrightnessHandler(looper);
        mAmbientLightRingBuffer = new AmbientLightRingBuffer();
@@ -195,11 +199,20 @@ class AutomaticBrightnessController {
    }

    public int getAutomaticScreenBrightness() {
        if (mDozing) {
            return (int) (mScreenAutoBrightness * mDozeScaleFactor);
        }
        return mScreenAutoBrightness;
    }

    public void configure(boolean enable, float adjustment) {
        boolean changed = setLightSensorEnabled(enable);
    public void configure(boolean enable, float adjustment, boolean dozing) {
        // While dozing, the application processor may be suspended which will prevent us from
        // receiving new information from the light sensor. On some devices, we may be able to
        // switch to a wake-up light sensor instead but for now we will simply disable the sensor
        // and hold onto the last computed screen auto brightness.  We save the dozing flag for
        // debugging purposes.
        mDozing = dozing;
        boolean changed = setLightSensorEnabled(enable && !dozing);
        changed |= setScreenAutoBrightnessAdjustment(adjustment);
        if (changed) {
            updateAutoBrightness(false /*sendUpdate*/);
@@ -230,6 +243,7 @@ class AutomaticBrightnessController {
        pw.println("  mScreenAutoBrightness=" + mScreenAutoBrightness);
        pw.println("  mScreenAutoBrightnessAdjustment=" + mScreenAutoBrightnessAdjustment);
        pw.println("  mLastScreenAutoBrightnessGamma=" + mLastScreenAutoBrightnessGamma);
        pw.println("  mDozing=" + mDozing);
    }

    private boolean setLightSensorEnabled(boolean enable) {
+27 −10
Original line number Diff line number Diff line
@@ -155,6 +155,9 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
    // True if auto-brightness should be used.
    private boolean mUseSoftwareAutoBrightnessConfig;

    // True if should use light sensor to automatically determine doze screen brightness.
    private final boolean mAllowAutoBrightnessWhileDozingConfig;

    // True if we should fade the screen while turning it off, false if we should play
    // a stylish color fade animation instead.
    private boolean mColorFadeFadesConfig;
@@ -295,6 +298,10 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call

        mUseSoftwareAutoBrightnessConfig = resources.getBoolean(
                com.android.internal.R.bool.config_automatic_brightness_available);

        mAllowAutoBrightnessWhileDozingConfig = resources.getBoolean(
                com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing);

        if (mUseSoftwareAutoBrightnessConfig) {
            int[] lux = resources.getIntArray(
                    com.android.internal.R.array.config_autoBrightnessLevels);
@@ -302,6 +309,9 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
                    com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);
            int lightSensorWarmUpTimeConfig = resources.getInteger(
                    com.android.internal.R.integer.config_lightSensorWarmupTime);
            final float dozeScaleFactor = resources.getFraction(
                    com.android.internal.R.fraction.config_screenAutoBrightnessDozeScaleFactor,
                    1, 1);

            Spline screenAutoBrightnessSpline = createAutoBrightnessSpline(lux, screenBrightness);
            if (screenAutoBrightnessSpline == null) {
@@ -326,7 +336,7 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
                mAutomaticBrightnessController = new AutomaticBrightnessController(this,
                        handler.getLooper(), sensorManager, screenAutoBrightnessSpline,
                        lightSensorWarmUpTimeConfig, screenBrightnessRangeMinimum,
                        mScreenBrightnessRangeMaximum);
                        mScreenBrightnessRangeMaximum, dozeScaleFactor);
            }
        }

@@ -523,7 +533,9 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
                } else {
                    state = Display.STATE_DOZE;
                }
                if (!mAllowAutoBrightnessWhileDozingConfig) {
                    brightness = mPowerRequest.dozeScreenBrightness;
                }
                break;
            case DisplayPowerRequest.POLICY_DIM:
            case DisplayPowerRequest.POLICY_BRIGHT:
@@ -577,19 +589,16 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
            brightness = PowerManager.BRIGHTNESS_ON;
        }

        // Use default brightness when dozing unless overridden.
        if (brightness < 0 && (state == Display.STATE_DOZE
                || state == Display.STATE_DOZE_SUSPEND)) {
            brightness = mScreenBrightnessDozeConfig;
        }

        // Configure auto-brightness.
        boolean autoBrightnessEnabled = false;
        if (mAutomaticBrightnessController != null) {
            final boolean autoBrightnessEnabledInDoze = mAllowAutoBrightnessWhileDozingConfig
                    && (state == Display.STATE_DOZE || state == Display.STATE_DOZE_SUSPEND);
            autoBrightnessEnabled = mPowerRequest.useAutoBrightness
                    && state == Display.STATE_ON && brightness < 0;
                    && (state == Display.STATE_ON || autoBrightnessEnabledInDoze)
                    && brightness < 0;
            mAutomaticBrightnessController.configure(autoBrightnessEnabled,
                    mPowerRequest.screenAutoBrightnessAdjustment);
                    mPowerRequest.screenAutoBrightnessAdjustment, state != Display.STATE_ON);
        }

        // Apply auto-brightness.
@@ -612,6 +621,12 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
            mAppliedAutoBrightness = false;
        }

        // Use default brightness when dozing unless overridden.
        if (brightness < 0 && (state == Display.STATE_DOZE
                || state == Display.STATE_DOZE_SUSPEND)) {
            brightness = mScreenBrightnessDozeConfig;
        }

        // Apply manual brightness.
        // Use the current brightness setting from the request, which is expected
        // provide a nominal default value for the case where auto-brightness
@@ -1024,6 +1039,8 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
        pw.println("  mScreenBrightnessRangeMinimum=" + mScreenBrightnessRangeMinimum);
        pw.println("  mScreenBrightnessRangeMaximum=" + mScreenBrightnessRangeMaximum);
        pw.println("  mUseSoftwareAutoBrightnessConfig=" + mUseSoftwareAutoBrightnessConfig);
        pw.println("  mAllowAutoBrightnessWhileDozingConfig=" +
                mAllowAutoBrightnessWhileDozingConfig);
        pw.println("  mColorFadeFadesConfig=" + mColorFadeFadesConfig);

        mHandler.runWithScissors(new Runnable() {