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

Unverified Commit 04dcb6ce authored by Adrian DC's avatar Adrian DC Committed by Michael Bestas
Browse files

LEDs Brightness: Rewrite the slider control without TunerService



 * In commit Iecafafabdaec82b3b3c72293bea865de48f0e90a,
    the TunerService was introduced to listen the notification
    brightness level and update the local value, however
    this introduces an infinite loop of write / updates
    creating heavy lags and preventing user changes

 * Rewrite the NotificationBrightnessController without
    a TunerService or the original ContentObserver
    in a more simple and logic way

 * On slider creation, registerCallbacks reads the user
    value set to the brightness, stores the maximum for
    preview, allowing user interaction to change the
    local value and notification preview. Then update
    the stored value on exit in unregisterCallbacks

 * Additionaly reduce the notification update delay
    to smoothen the brightness preview on slide

 * Also prevent the LightsService from sending down
    a brightness of 0 if alpha & brightness are low,
    as this defaults to full brightness in liblights
    and could happen with the previous way or if
    an application gives a low alpha channel

Change-Id: Ia795c38d6b0e57476ae633acf1ae1c1e1e220cd2
Signed-off-by: default avatarAdrian DC <radian.dc@gmail.com>
parent 5be28206
Loading
Loading
Loading
Loading
+13 −38
Original line number Diff line number Diff line
@@ -26,25 +26,20 @@ import android.os.Message;
import android.os.UserHandle;

import com.android.systemui.R;
import com.android.systemui.tuner.TunerService;

import java.lang.Exception;
import java.util.ArrayList;

import cyanogenmod.providers.CMSettings;

public class NotificationBrightnessController
        implements ToggleSlider.Listener, TunerService.Tunable {
public class NotificationBrightnessController implements ToggleSlider.Listener {
    private static final String TAG = "StatusBar.NotificationBrightnessController";

    private static final String NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL =
            "cmsystem:" + CMSettings.System.NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL;

    public static final int LIGHT_BRIGHTNESS_MINIMUM = 1;
    public static final int LIGHT_BRIGHTNESS_MAXIMUM = 255;

    // Minimum delay between LED notification updates
    private final static long LED_UPDATE_DELAY_MS = 250;
    private final static long LED_UPDATE_DELAY_MS = 100;

    private int mCurrentBrightness;
    private final int mMinimumBrightness;
@@ -57,7 +52,6 @@ public class NotificationBrightnessController
            new ArrayList<BrightnessStateChangeCallback>();

    private boolean mListening;
    private boolean mExternalChange;

    private boolean mNotificationAllow;
    private final Bundle mNotificationBundle;
@@ -114,12 +108,18 @@ public class NotificationBrightnessController
            return;
        }

        // Read the brightness and set the maximum value for preview
        mCurrentBrightness = CMSettings.System.getIntForUser(mContext.getContentResolver(),
                CMSettings.System.NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL,
                mMaximumBrightness, UserHandle.USER_CURRENT);
        CMSettings.System.putIntForUser(mContext.getContentResolver(),
                CMSettings.System.NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL,
                mMaximumBrightness, UserHandle.USER_CURRENT);

        // Update the slider and mode before attaching the listener so we don't
        // receive the onChanged notifications for the initial values.
        mNotificationAllow = true;
        updateSlider(null);

        TunerService.get(mContext).addTunable(this, NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL);
        updateSlider();

        mControl.setOnChangedListener(this);
        mListening = true;
@@ -132,7 +132,6 @@ public class NotificationBrightnessController
        }

        mNotificationAllow = false;
        TunerService.get(mContext).removeTunable(this);
        mControl.setOnChangedListener(null);
        mNotificationManager.cancel(1);
        mListening = false;
@@ -145,8 +144,6 @@ public class NotificationBrightnessController
    @Override
    public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value,
            boolean stopTracking) {
        if (mExternalChange) return;

        mCurrentBrightness = value + mMinimumBrightness;
        updateNotification();

@@ -156,13 +153,7 @@ public class NotificationBrightnessController
    }

    /** Fetch the brightness from the system settings and update the slider */
    private void updateSlider(final String value) {
        mCurrentBrightness = value == null ? mMaximumBrightness : Integer.parseInt(value);

        CMSettings.System.putIntForUser(mContext.getContentResolver(),
                CMSettings.System.NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL,
                mMaximumBrightness, UserHandle.USER_CURRENT);

    private void updateSlider() {
        mControl.setMax(mMaximumBrightness - mMinimumBrightness);
        mControl.setValue(mCurrentBrightness - mMinimumBrightness);
        updateNotification();
@@ -180,25 +171,9 @@ public class NotificationBrightnessController

            // Instead of canceling the notification, force it to update with the color.
            // Use a white light for a better preview of the brightness.
            int notificationColor = 0xFFFFFF | (mCurrentBrightness << 24);
            int notificationColor = 0x00FFFFFF | (mCurrentBrightness << 24);
            mNotificationBuilder.setLights(notificationColor, 1, 0);
            mNotificationManager.notify(1, mNotificationBuilder.build());
        }
    }

    @Override
    public void onTuningChanged(String key, String newValue) {
        if (!NOTIFICATION_LIGHT_BRIGHTNESS_LEVEL.equals(key)) {
            return;
        }
        try {
            mExternalChange = true;
            updateSlider(newValue);
            for (BrightnessStateChangeCallback cb : mChangeCallbacks) {
                cb.onBrightnessLevelChanged();
            }
        } finally {
            mExternalChange = false;
        }
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -130,6 +130,9 @@ static void setLight_native(JNIEnv* /* env */, jobject /* clazz */, jlong ptr,
            colorAlpha = 0xFF;
        }
        colorAlpha = (colorAlpha * brightnessLevel) / 0xFF;
        if (colorAlpha < 1) {
            colorAlpha = 1;
        }
        colorARGB = (colorAlpha << 24) + (colorARGB & 0x00FFFFFF);
    }