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

Unverified Commit 62ed5423 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.

Change-Id: I5187fdb7765550d4eaa999317ab95f487d44c8d8
parent b3426e6c
Loading
Loading
Loading
Loading
+18 −1
Original line number Diff line number Diff line
@@ -279,6 +279,8 @@ public class AutomaticBrightnessController {

    private final DisplayManagerFlags mDisplayManagerFlags;

    private boolean mAutoBrightnessOneShot;

    AutomaticBrightnessController(Callbacks callbacks, Looper looper,
            SensorManager sensorManager, Sensor lightSensor,
            SparseArray<BrightnessMappingStrategy> brightnessMappingStrategyMap,
@@ -442,7 +444,7 @@ public class AutomaticBrightnessController {
    public void configure(int state, @Nullable BrightnessConfiguration configuration,
            float brightness, boolean userChangedBrightness, float adjustment,
            boolean userChangedAutoBrightnessAdjustment, int displayPolicy, int displayState,
            boolean shouldResetShortTermModel) {
            boolean shouldResetShortTermModel, boolean autoBrightnessOneShot) {
        mState = state;
        boolean changed = setBrightnessConfiguration(configuration, shouldResetShortTermModel);
        changed |= setDisplayPolicy(displayPolicy);
@@ -471,6 +473,8 @@ public class AutomaticBrightnessController {

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

@@ -512,6 +516,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;
@@ -1011,6 +1025,9 @@ public class AutomaticBrightnessController {
                mCallbacks.updateBrightness();
            }
        }
        if (mAutoBrightnessOneShot) {
            mSensorManager.unregisterListener(mLightSensorListener);
        }
    }

    // Clamps values with float range [0.0-1.0]
+12 −0
Original line number Diff line number Diff line
@@ -98,6 +98,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;

@@ -1008,6 +1010,9 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
                    Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_FOR_ALS),
                    /* notifyForDescendants= */ false, mSettingsObserver, UserHandle.USER_CURRENT);
        }
        mContext.getContentResolver().registerContentObserver(
                LineageSettings.System.getUriFor(LineageSettings.System.AUTO_BRIGHTNESS_ONE_SHOT),
                false /*notifyForDescendants*/, mSettingsObserver, UserHandle.USER_ALL);
        handleBrightnessModeChange();
    }

@@ -2432,6 +2437,8 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
                .setPendingScreenBrightness(mDisplayBrightnessController
                        .getScreenBrightnessSetting());
        mAutomaticBrightnessStrategy.updatePendingAutoBrightnessAdjustments();
        mAutomaticBrightnessStrategy.setAutoBrightnessOneShotEnabled(
                getAutoBrightnessOneShotSetting());
        sendUpdatePowerState();
    }

@@ -2444,6 +2451,11 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
                == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
    }

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

    @Override
    public float getScreenBrightnessSetting() {
+8 −1
Original line number Diff line number Diff line
@@ -105,6 +105,9 @@ public class AutomaticBrightnessStrategy extends AutomaticBrightnessStrategy2
    // Indicates if the current auto-brightness should be ramped up or down slowly.
    private boolean mIsSlowChange;

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

    @VisibleForTesting
    AutomaticBrightnessStrategy(Context context, int displayId, Injector injector,
            DisplayManagerFlags displayManagerFlags) {
@@ -188,6 +191,10 @@ public class AutomaticBrightnessStrategy extends AutomaticBrightnessStrategy2
        return mAutoBrightnessDisabledDueToDisplayOff;
    }

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

    /**
     * Updates the {@link BrightnessConfiguration} that is currently being used by the associated
     * display.
@@ -480,7 +487,7 @@ public class AutomaticBrightnessStrategy extends AutomaticBrightnessStrategy2
                    lastUserSetScreenBrightness,
                    userSetBrightnessChanged, autoBrightnessAdjustment,
                    mAutoBrightnessAdjustmentChanged, policy, displayState,
                    mShouldResetShortTermModel);
                    mShouldResetShortTermModel, mAutoBrightnessOneShotEnabled);
            mShouldResetShortTermModel = false;
            // We take note if the user brightness point is still being used in the current
            // auto-brightness model.
+8 −1
Original line number Diff line number Diff line
@@ -92,6 +92,9 @@ public class AutomaticBrightnessStrategy2 {
    @Nullable
    private BrightnessConfiguration mBrightnessConfiguration;

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

    public AutomaticBrightnessStrategy2(Context context, int displayId) {
        mContext = context;
        mDisplayId = displayId;
@@ -134,6 +137,10 @@ public class AutomaticBrightnessStrategy2 {
        return mAutoBrightnessDisabledDueToDisplayOff;
    }

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

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