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

Commit f367306b authored by Danny Baumann's avatar Danny Baumann Committed by Bruno Martins
Browse files

PowerManager: Re-integrate button brightness

This is a squash of the following commits:

Author: Danny Baumann <dannybaumann@web.de>
Date:   Thu Aug 22 08:53:24 2013 +0200

    Add PowerManager integration for button and keyboard backlight.

    Allows setting button and keyboard backlight brightness as well as
    button timeout.

    Change-Id: I550cccafc0a8f90d6347de9261adb26b75955cc4

Author: nadlabak <pavel@doshaska.net>
Date:   Sun Jun 7 02:01:05 2015 +0200

    PowerManagerService: Fix updating of mUserActivitySummary

    I7f1fc35a1573717d1ea101a07c4171d6f66d1553 missed the fact that the primary
    purpose of the affected condition block was to update mUserActivitySummary
    and the button/keyboard light handling was just appended to it later.

    This fixes the waking from dream/screensaver by user activity.

    I30c5c8c9c09e3d57ace18cac72b783510b9b3bf3 is removed here as well as it was
    just a band aid.

    jira: NIGHTLIES-1285

    Change-Id: I6b2f6c58e73110787d62e86d4d2ef538638cf491

Author: Bruno Martins <bgcngm@gmail.com>
Date:   Tue Dec 26 17:15:05 2017 +0000

    Forward-port button brightness implementation to O

     * Reworked for the new handler interface, restoring also removed
       methods (partial revert of commit 86c39f9e).

     * Keyboard backlight brightness support left out for now.

Change-Id: I53f031fa2da394e95a2b29a01eb3c6a8f8132507
parent 427640bb
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -682,6 +682,15 @@ public final class PowerManager {
                com.android.internal.R.integer.config_screenBrightnessForVrSettingDefault);
    }

    /**
     * Gets the default button brightness value.
     * @hide
     */
    public int getDefaultButtonBrightness() {
        return mContext.getResources().getInteger(
                com.android.internal.R.integer.config_buttonBrightnessSettingDefault);
    }

    /**
     * Creates a new wake lock with the specified level and flags.
     * <p>
+10 −0
Original line number Diff line number Diff line
@@ -106,6 +106,16 @@ public abstract class PowerManagerInternal {
     */
    public abstract void setScreenBrightnessOverrideFromWindowManager(int brightness);

    /**
     * Used by the window manager to override the button brightness based on the
     * current foreground activity.
     *
     * This method must only be called by the window manager.
     *
     * @param brightness The overridden brightness, or -1 to disable the override.
     */
    public abstract void setButtonBrightnessOverrideFromWindowManager(int brightness);

    /**
     * Used by the window manager to override the user activity timeout based on the
     * current foreground activity.  It can only be used to make the timeout shorter
+21 −0
Original line number Diff line number Diff line
<!--
     Copyright (C) 2012-2015 The CyanogenMod Project
     Copyright (C) 2017 The LineageOS Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<resources>
    <!-- Button backlight -->
    <integer name="config_buttonBrightnessSettingDefault">255</integer>
    <bool name="config_deviceHasVariableButtonBrightness">false</bool>
</resources>
+21 −0
Original line number Diff line number Diff line
<!--
     Copyright (C) 2012-2015 The CyanogenMod Project
     Copyright (C) 2017 The LineageOS Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<resources>
    <!-- Button backlight -->
    <java-symbol type="integer" name="config_buttonBrightnessSettingDefault" />
    <java-symbol type="bool" name="config_deviceHasVariableButtonBrightness" />
</resources>
+71 −8
Original line number Diff line number Diff line
@@ -101,6 +101,8 @@ import com.android.server.power.batterysaver.BatterySaverController;
import com.android.server.power.batterysaver.BatterySaverStateMachine;
import com.android.server.power.batterysaver.BatterySavingStats;

import lineageos.providers.LineageSettings;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.lang.annotation.Retention;
@@ -222,7 +224,7 @@ public final class PowerManagerService extends SystemService
    // Persistent property for last reboot reason
    private static final String LAST_REBOOT_PROPERTY = "persist.sys.boot.reason";

    private static final int BUTTON_ON_DURATION = 5 * 1000;
    private static final int DEFAULT_BUTTON_ON_DURATION = 5 * 1000;

    private final Context mContext;
    private final ServiceThread mHandlerThread;
@@ -246,6 +248,10 @@ public final class PowerManagerService extends SystemService
    private Light mAttentionLight;
    private Light mButtonsLight;

    private int mButtonTimeout;
    private int mButtonBrightness;
    private int mButtonBrightnessSettingDefault;

    private final Object mLock = LockGuard.installNewLock(LockGuard.INDEX_POWER);

    // A bitfield that indicates what parts of the power state have
@@ -483,6 +489,11 @@ public final class PowerManagerService extends SystemService
    // Set to -1 when not told the user is inactive since the last period spent dozing or asleep.
    private long mOverriddenTimeout = -1;

    // The button brightness setting override from the window manager
    // to allow the current foreground activity to override the button brightness.
    // Use -1 to disable.
    private int mButtonBrightnessOverrideFromWindowManager = -1;

    // The user activity timeout override from the window manager
    // to allow the current foreground activity to override the user activity timeout.
    // Use -1 to disable.
@@ -755,6 +766,7 @@ public final class PowerManagerService extends SystemService
            mBatteryManagerInternal = getLocalService(BatteryManagerInternal.class);

            PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
            mButtonBrightnessSettingDefault = pm.getDefaultButtonBrightness();
            mScreenBrightnessSettingMinimum = pm.getMinimumScreenBrightnessSetting();
            mScreenBrightnessSettingMaximum = pm.getMaximumScreenBrightnessSetting();
            mScreenBrightnessSettingDefault = pm.getDefaultScreenBrightnessSetting();
@@ -837,6 +849,14 @@ public final class PowerManagerService extends SystemService
        resolver.registerContentObserver(Settings.Global.getUriFor(
                Settings.Global.DEVICE_DEMO_MODE),
                false, mSettingsObserver, UserHandle.USER_SYSTEM);

        resolver.registerContentObserver(LineageSettings.Secure.getUriFor(
                LineageSettings.Secure.BUTTON_BRIGHTNESS),
                false, mSettingsObserver, UserHandle.USER_ALL);
        resolver.registerContentObserver(LineageSettings.Secure.getUriFor(
                LineageSettings.Secure.BUTTON_BACKLIGHT_TIMEOUT),
                false, mSettingsObserver, UserHandle.USER_ALL);

        IVrManager vrManager = (IVrManager) getBinderService(Context.VR_SERVICE);
        if (vrManager != null) {
            try {
@@ -953,6 +973,13 @@ public final class PowerManagerService extends SystemService
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL, UserHandle.USER_CURRENT);

        mButtonTimeout = LineageSettings.Secure.getIntForUser(resolver,
                LineageSettings.Secure.BUTTON_BACKLIGHT_TIMEOUT,
                DEFAULT_BUTTON_ON_DURATION, UserHandle.USER_CURRENT);
        mButtonBrightness = LineageSettings.Secure.getIntForUser(resolver,
                LineageSettings.Secure.BUTTON_BRIGHTNESS, mButtonBrightnessSettingDefault,
                UserHandle.USER_CURRENT);

        mDirty |= DIRTY_SETTINGS;
    }

@@ -1961,21 +1988,35 @@ public final class PowerManagerService extends SystemService
                final long nextProfileTimeout = getNextProfileTimeoutLocked(now);

                mUserActivitySummary = 0;
                if (mWakefulness == WAKEFULNESS_AWAKE && mLastUserActivityTime >= mLastWakeTime) {
                if (mLastUserActivityTime >= mLastWakeTime) {
                    nextTimeout = mLastUserActivityTime
                            + screenOffTimeout - screenDimDuration;
                    if (now < nextTimeout) {
                        if (now > mLastUserActivityTime + BUTTON_ON_DURATION) {
                        mUserActivitySummary = USER_ACTIVITY_SCREEN_BRIGHT;
                        if (mWakefulness == WAKEFULNESS_AWAKE) {
                            int buttonBrightness;
                            if (mButtonBrightnessOverrideFromWindowManager >= 0) {
                                buttonBrightness = mButtonBrightnessOverrideFromWindowManager;
                            } else {
                                buttonBrightness = mButtonBrightness;
                            }

                            if (mButtonTimeout != 0 && now > mLastUserActivityTime + mButtonTimeout) {
                                mButtonsLight.setBrightness(0);
                            } else {
                            mButtonsLight.setBrightness(mDisplayPowerRequest.screenBrightness);
                            nextTimeout = now + BUTTON_ON_DURATION;
                                mButtonsLight.setBrightness(buttonBrightness);
                                if (buttonBrightness != 0 && mButtonTimeout != 0) {
                                    nextTimeout = now + mButtonTimeout;
                                }
                            }
                        }
                        mUserActivitySummary = USER_ACTIVITY_SCREEN_BRIGHT;
                    } else {
                        nextTimeout = mLastUserActivityTime + screenOffTimeout;
                        if (now < nextTimeout) {
                            mUserActivitySummary = USER_ACTIVITY_SCREEN_DIM;
                            if (mWakefulness == WAKEFULNESS_AWAKE) {
                                mButtonsLight.setBrightness(0);
                            }
                        }
                    }
                }
@@ -4630,6 +4671,16 @@ public final class PowerManagerService extends SystemService
        }
    }

    private void setButtonBrightnessOverrideFromWindowManagerInternal(int brightness) {
        synchronized (mLock) {
            if (mButtonBrightnessOverrideFromWindowManager != brightness) {
                mButtonBrightnessOverrideFromWindowManager = brightness;
                mDirty |= DIRTY_SETTINGS;
                updatePowerStateLocked();
            }
        }
    }

    private final class LocalService extends PowerManagerInternal {
        @Override
        public void setScreenBrightnessOverrideFromWindowManager(int screenBrightness) {
@@ -4640,6 +4691,18 @@ public final class PowerManagerService extends SystemService
            setScreenBrightnessOverrideFromWindowManagerInternal(screenBrightness);
        }

        @Override
        public void setButtonBrightnessOverrideFromWindowManager(int screenBrightness) {
            mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);

            final long ident = Binder.clearCallingIdentity();
            try {
                setButtonBrightnessOverrideFromWindowManagerInternal(screenBrightness);
            } finally {
                Binder.restoreCallingIdentity(ident);
            }
        }

        @Override
        public void setDozeOverrideFromDreamManager(int screenState, int screenBrightness) {
            switch (screenState) {
Loading