Skip to content

Commit

Permalink
PowerManagerService: Allow to light up buttons only when pressed
Browse files Browse the repository at this point in the history
Author: Anas Karbila <[email protected]>
Date:   Sat Jun 3 03:21:32 2017 +0200

    PowerManagerService: Allow to light up buttons only when pressed

     * Right now capactive, lit hardware keys are being
       lit every time you either touch them or the screen.

       But some devices handle this differently on stock:

       Display touch => buttons not lit
       Buttons touch => buttons lit

     * Thus, add a setting in order to allow the user
       to choose the preferred behavior.

    Change-Id: I35ac71a8274568901f962c9692788d1c682a98dd

Author: Corinna Vinschen <[email protected]>
Date:   Sun Aug 6 15:05:54 2017 +0200

    PowerManagerService: fix HW button illumination timeout

    Change I35ac71a8274568901f962c9692788d1c682a98dd, introducing hardware
    button backlight on button keypress only, also introduced a bug:

    When touching a button and then performing display activity while
    the buttons are still on, the buttons would keep lightened up until
    the next user interaction, potentially only switched off at the next
    screen off timeout.  Also, the buttons were not illuminated on
    device wakeup.

    This patch fixes it, together with another, long-standing problem:

    When touching a hardware button, nextTimeout was set to
    now + mButtonTimeout, even if mButtonTimeout is longer than the timeout
    determined by the screen off timeout.  To wit, if screen timeout is set
    to 15 secs, but button timeout to values > 15 secs.

    Change-Id: I8a56f1d1e0138c38ed6fe294e4816a9f7f744f1e
    Signed-off-by: Corinna Vinschen <[email protected]>

Change-Id: I5b486e65a5b7d9d16590941df0af4d9c604dedc4
  • Loading branch information
Thecrazyskull authored and sbwml committed Dec 23, 2019
1 parent a8b027b commit cea7234
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
13 changes: 13 additions & 0 deletions core/java/android/provider/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -4472,6 +4472,17 @@ public boolean validate(@Nullable String value) {
/** @hide */
public static final Validator BUTTON_BACKLIGHT_TIMEOUT_VALIDATOR = NON_NEGATIVE_INTEGER_VALIDATOR;

/**
* Whether the button backlight is only lit when pressed (and not when screen is touched)
* The value is boolean (1 or 0).
*/
public static final String BUTTON_BACKLIGHT_ONLY_WHEN_PRESSED =
"button_backlight_only_when_pressed";

/** @hide */
public static final Validator BUTTON_BACKLIGHT_ONLY_WHEN_PRESSED_VALIDATOR =
BOOLEAN_VALIDATOR;

/**
* The button brightness to be used while the screen is on or after a button press,
* depending on the value of {@link BUTTON_BACKLIGHT_TIMEOUT}.
Expand Down Expand Up @@ -4685,6 +4696,7 @@ public boolean validate(@Nullable String value) {
PRIVATE_SETTINGS.add(SHOW_BATTERY_PERCENT);
PRIVATE_SETTINGS.add(DISPLAY_COLOR_MODE);
PRIVATE_SETTINGS.add(BUTTON_BACKLIGHT_TIMEOUT);
PRIVATE_SETTINGS.add(BUTTON_BACKLIGHT_ONLY_WHEN_PRESSED);
PRIVATE_SETTINGS.add(BUTTON_BRIGHTNESS);
PRIVATE_SETTINGS.add(PROXIMITY_ON_WAKE);
}
Expand Down Expand Up @@ -4783,6 +4795,7 @@ public boolean validate(@Nullable String value) {
VALIDATORS.put(BUTTON_BACKLIGHT_TIMEOUT, BUTTON_BACKLIGHT_TIMEOUT_VALIDATOR);
VALIDATORS.put(BUTTON_BRIGHTNESS, BUTTON_BRIGHTNESS_VALIDATOR);
VALIDATORS.put(PROXIMITY_ON_WAKE, PROXIMITY_ON_WAKE_VALIDATOR);
VALIDATORS.put(BUTTON_BACKLIGHT_ONLY_WHEN_PRESSED, BUTTON_BACKLIGHT_ONLY_WHEN_PRESSED_VALIDATOR);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ public final class PowerManagerService extends SystemService
private int mButtonBrightness;
private int mButtonBrightnessSettingDefault;

private boolean mButtonPressed;
private boolean mButtonOn;
private boolean mButtonLightOnKeypressOnly;

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

// A bitfield that indicates what parts of the power state have
Expand Down Expand Up @@ -313,6 +317,7 @@ public final class PowerManagerService extends SystemService
private int mLastSleepReason;

// Timestamp of the last call to user activity.
private long mLastButtonActivityTime;
private long mLastUserActivityTime;
private long mLastUserActivityTimeNoChangeLights;

Expand Down Expand Up @@ -958,6 +963,9 @@ public void systemReady(IAppOpsService appOps) {
resolver.registerContentObserver(Settings.System.getUriFor(
Settings.System.PROXIMITY_ON_WAKE),
false, mSettingsObserver, UserHandle.USER_ALL);
resolver.registerContentObserver(Settings.System.getUriFor(
Settings.System.BUTTON_BACKLIGHT_ONLY_WHEN_PRESSED),
false, mSettingsObserver, UserHandle.USER_ALL);
IVrManager vrManager = IVrManager.Stub.asInterface(getBinderService(Context.VR_SERVICE));
if (vrManager != null) {
try {
Expand Down Expand Up @@ -1092,6 +1100,9 @@ private void updateSettingsLocked() {
mButtonBrightness = Settings.System.getIntForUser(resolver,
Settings.System.BUTTON_BRIGHTNESS, mButtonBrightnessSettingDefault,
UserHandle.USER_CURRENT);
mButtonLightOnKeypressOnly = Settings.System.getIntForUser(resolver,
Settings.System.BUTTON_BACKLIGHT_ONLY_WHEN_PRESSED,
0, UserHandle.USER_CURRENT) == 1;

mProximityWakeEnabled = Settings.System.getInt(resolver,
Settings.System.PROXIMITY_ON_WAKE,
Expand Down Expand Up @@ -1476,6 +1487,12 @@ private boolean userActivityNoUpdateLocked(long eventTime, int event, int flags,
}
} else {
if (eventTime > mLastUserActivityTime) {
mButtonPressed = event == PowerManager.USER_ACTIVITY_EVENT_BUTTON;
if ((mButtonLightOnKeypressOnly && mButtonPressed)
|| eventTime == mLastWakeTime) {
mButtonPressed = true;
mLastButtonActivityTime = eventTime;
}
mLastUserActivityTime = eventTime;
mDirty |= DIRTY_USER_ACTIVITY;
if (event == PowerManager.USER_ACTIVITY_EVENT_BUTTON) {
Expand Down Expand Up @@ -2110,14 +2127,26 @@ private void updateUserActivitySummaryLocked(long now, int dirty) {
buttonBrightness = mButtonBrightness;
}

if (mButtonTimeout != 0 && now > mLastUserActivityTime + mButtonTimeout) {
mLastButtonActivityTime = mButtonLightOnKeypressOnly ?
mLastButtonActivityTime : mLastUserActivityTime;
if (mButtonTimeout != 0 &&
now > mLastButtonActivityTime + mButtonTimeout) {
mButtonsLight.setBrightness(0);
mButtonOn = false;
} else {
if (!mProximityPositive) {
if ((!mButtonLightOnKeypressOnly || mButtonPressed) &&
!mProximityPositive) {
mButtonsLight.setBrightness(buttonBrightness);
mButtonPressed = false;
if (buttonBrightness != 0 && mButtonTimeout != 0) {
nextTimeout = now + mButtonTimeout;
mButtonOn = true;
if (now + mButtonTimeout < nextTimeout) {
nextTimeout = now + mButtonTimeout;
}
}
} else if (mButtonLightOnKeypressOnly && mButtonOn &&
mLastButtonActivityTime + mButtonTimeout < nextTimeout) {
nextTimeout = mLastButtonActivityTime + mButtonTimeout;
}
}
}
Expand All @@ -2127,6 +2156,7 @@ private void updateUserActivitySummaryLocked(long now, int dirty) {
mUserActivitySummary = USER_ACTIVITY_SCREEN_DIM;
if (mWakefulness == WAKEFULNESS_AWAKE) {
mButtonsLight.setBrightness(0);
mButtonOn = false;
}
}
}
Expand Down

0 comments on commit cea7234

Please sign in to comment.