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

Unverified Commit b82a6651 authored by Cédric Bellegarde's avatar Cédric Bellegarde Committed by Michael Bestas
Browse files

AutoBrightness: Add support for one shot auto-brightness

- Only update auto brightness one time when screen is turned on.
- Can be useful on devices where sensor is not accurate.

Author: Eamon Powell <eamonpowell@outlook.com>
Date:   Sun Aug 29 11:37:44 2021 +1000

    fixup! AutoBrightness: Add support for one shot auto-brightness:

    * Remove unnecessary newlines
    * Fix incorrect indentation
    * Fix typo in comments

    Change-Id: I7d6071f4344605b3dcc3f1c33ac4390f1020e03f

Change-Id: I5187fdb7765550d4eaa999317ab95f487d44c8d8
parent f4c7fb85
Loading
Loading
Loading
Loading
+18 −1
Original line number Diff line number Diff line
@@ -247,6 +247,8 @@ public class AutomaticBrightnessController {
    private Clock mClock;
    private final Injector mInjector;

    private boolean mAutoBrightnessOneShot;

    AutomaticBrightnessController(Callbacks callbacks, Looper looper,
            SensorManager sensorManager, Sensor lightSensor,
            BrightnessMappingStrategy interactiveModeBrightnessMapper,
@@ -410,7 +412,7 @@ public class AutomaticBrightnessController {
    public void configure(int state, @Nullable BrightnessConfiguration configuration,
            float brightness, boolean userChangedBrightness, float adjustment,
            boolean userChangedAutoBrightnessAdjustment, int displayPolicy,
            boolean shouldResetShortTermModel) {
            boolean shouldResetShortTermModel, boolean autoBrightnessOneShot) {
        mState = state;
        // 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
@@ -444,6 +446,8 @@ public class AutomaticBrightnessController {

        if (changed) {
            updateAutoBrightness(false /*sendUpdate*/, userInitiatedChange);
        } else {
            handleSettingsChange(autoBrightnessOneShot);
        }
    }

@@ -487,6 +491,16 @@ public class AutomaticBrightnessController {
        return mFastAmbientLux;
    }

    private void handleSettingsChange(boolean autoBrightnessOneShot) {
        if (mAutoBrightnessOneShot && !autoBrightnessOneShot) {
            mSensorManager.registerListener(mLightSensorListener, mLightSensor,
                    mCurrentLightSensorRate * 1000, mHandler);
        } else if (!mAutoBrightnessOneShot && autoBrightnessOneShot) {
            mSensorManager.unregisterListener(mLightSensorListener);
        }
        mAutoBrightnessOneShot = autoBrightnessOneShot;
    }

    private boolean setDisplayPolicy(int policy) {
        if (mDisplayPolicy == policy) {
            return false;
@@ -976,6 +990,9 @@ public class AutomaticBrightnessController {
                mCallbacks.updateBrightness();
            }
        }
        if (mAutoBrightnessOneShot) {
            mSensorManager.unregisterListener(mLightSensorListener);
        }
    }

    // Clamps values with float range [0.0-1.0]
+1 −1
Original line number Diff line number Diff line
@@ -1663,7 +1663,7 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
                    mLastUserSetScreenBrightness,
                    userSetBrightnessChanged, autoBrightnessAdjustment,
                    autoBrightnessAdjustmentChanged, mPowerRequest.policy,
                    mShouldResetShortTermModel);
                    mShouldResetShortTermModel, false);
            mShouldResetShortTermModel = false;
        }
        mBrightnessRangeController.setAutoBrightnessEnabled(autoBrightnessEnabled
+12 −0
Original line number Diff line number Diff line
@@ -85,6 +85,8 @@ import com.android.server.display.whitebalance.DisplayWhiteBalanceFactory;
import com.android.server.display.whitebalance.DisplayWhiteBalanceSettings;
import com.android.server.policy.WindowManagerPolicy;

import lineageos.providers.LineageSettings;

import java.io.PrintWriter;
import java.util.Objects;

@@ -945,6 +947,9 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        mContext.getContentResolver().registerContentObserver(
                Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE),
                false /*notifyForDescendants*/, mSettingsObserver, UserHandle.USER_ALL);
        mContext.getContentResolver().registerContentObserver(
                LineageSettings.System.getUriFor(LineageSettings.System.AUTO_BRIGHTNESS_ONE_SHOT),
                false /*notifyForDescendants*/, mSettingsObserver, UserHandle.USER_ALL);
        handleBrightnessModeChange();
    }

@@ -2207,6 +2212,8 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
                .setPendingScreenBrightness(mDisplayBrightnessController
                        .getScreenBrightnessSetting());
        mAutomaticBrightnessStrategy.updatePendingAutoBrightnessAdjustments(userSwitch);
        mAutomaticBrightnessStrategy.setAutoBrightnessOneShotEnabled(
                getAutoBrightnessOneShotSetting());
        if (userSwitch) {
            // Don't treat user switches as user initiated change.
            mDisplayBrightnessController
@@ -2231,6 +2238,11 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        }, mClock.uptimeMillis());
    }

    private boolean getAutoBrightnessOneShotSetting() {
        return LineageSettings.System.getIntForUser(
                mContext.getContentResolver(), LineageSettings.System.AUTO_BRIGHTNESS_ONE_SHOT,
                0, UserHandle.USER_CURRENT) == 1;
    }

    @Override
    public float getScreenBrightnessSetting() {
+9 −1
Original line number Diff line number Diff line
@@ -86,6 +86,9 @@ public class AutomaticBrightnessStrategy {
    @Nullable
    private BrightnessConfiguration mBrightnessConfiguration;

    // Whether auto brightness is applied one shot when screen is turned on
    private boolean mAutoBrightnessOneShotEnabled;

    public AutomaticBrightnessStrategy(Context context, int displayId) {
        mContext = context;
        mDisplayId = displayId;
@@ -129,6 +132,10 @@ public class AutomaticBrightnessStrategy {
        return mAutoBrightnessDisabledDueToDisplayOff;
    }

    public void setAutoBrightnessOneShotEnabled(boolean enabled) {
        mAutoBrightnessOneShotEnabled = enabled;
    }

    /**
     * Updates the {@link BrightnessConfiguration} that is currently being used by the associated
     * display.
@@ -375,7 +382,8 @@ public class AutomaticBrightnessStrategy {
                    brightnessConfiguration,
                    lastUserSetScreenBrightness,
                    userSetBrightnessChanged, autoBrightnessAdjustment,
                    mAutoBrightnessAdjustmentChanged, policy, mShouldResetShortTermModel);
                    mAutoBrightnessAdjustmentChanged, policy, mShouldResetShortTermModel,
                    mAutoBrightnessOneShotEnabled);
            mShouldResetShortTermModel = false;
            // We take note if the user brightness point is still being used in the current
            // auto-brightness model.