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

Commit a28787b2 authored by László Dávid's avatar László Dávid Committed by Gerrit Code Review
Browse files

Status Bar Brightness Control: Set brightness by long press the status bar

Sliding your finger on the status bar will continue to function.
See: http://review.cyanogenmod.com/#/c/18625
See: http://review.cyanogenmod.com/#/c/18870

Change-Id: I7bd7c64dc25038e1151b83ec07cc2c7c856f9359
parent 02afc54a
Loading
Loading
Loading
Loading
+68 −29
Original line number Diff line number Diff line
@@ -60,11 +60,13 @@ import android.util.Slog;
import android.view.Choreographer;
import android.view.Display;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.IWindowManager;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
@@ -130,9 +132,10 @@ public class PhoneStatusBar extends BaseStatusBar {
    private static final boolean CLOSE_PANEL_WHEN_EMPTIED = true;

    private static final float BRIGHTNESS_CONTROL_PADDING = 0.15f;
    private static final int BRIGHTNESS_CONTROL_LONG_PRESS_TIMEOUT = 750; // ms
    private static final int BRIGHTNESS_CONTROL_LINGER_THRESHOLD = 20;

    private boolean mBrightnessControl;
    private boolean mAutoBrightness;

    private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10; // see NotificationManagerService
    private static final int HIDE_ICONS_BELOW_SCORE = Notification.PRIORITY_LOW * NOTIFICATION_PRIORITY_MULTIPLIER;
@@ -251,7 +254,9 @@ public class PhoneStatusBar extends BaseStatusBar {
    float mFlingVelocity;
    int mFlingY;
    int[] mAbsPos = new int[2];
    int mLinger = 0;
    int mLinger;
    int mInitialTouchX;
    int mInitialTouchY;
    Runnable mPostCollapseCleanup = null;

    // last theme that was applied in order to detect theme change (as opposed
@@ -270,6 +275,15 @@ public class PhoneStatusBar extends BaseStatusBar {

    DisplayMetrics mDisplayMetrics = new DisplayMetrics();

    Runnable mLongPressBrightnessChange = new Runnable() {
        @Override
        public void run() {
            mStatusBarView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
            adjustBrightness(mInitialTouchX);
            mLinger = BRIGHTNESS_CONTROL_LINGER_THRESHOLD + 1;
        }
    };

    class SettingsObserver extends ContentObserver {
        SettingsObserver(Handler handler) {
            super(handler);
@@ -291,11 +305,11 @@ public class PhoneStatusBar extends BaseStatusBar {

        public void update() {
            ContentResolver resolver = mContext.getContentResolver();
            mBrightnessControl = Settings.System.getInt(resolver,
                    Settings.System.STATUS_BAR_BRIGHTNESS_CONTROL, 0) != 0;
            mAutoBrightness = Settings.System.getInt(resolver,
                    Settings.System.SCREEN_BRIGHTNESS_MODE, 0) ==
            boolean autoBrightness = Settings.System.getInt(
                    resolver, Settings.System.SCREEN_BRIGHTNESS_MODE, 0) ==
                    Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
            mBrightnessControl = !autoBrightness && Settings.System.getInt(
                    resolver, Settings.System.STATUS_BAR_BRIGHTNESS_CONTROL, 0) == 1;
        }
    }

@@ -1665,6 +1679,34 @@ public class PhoneStatusBar extends BaseStatusBar {
        stopTracking();
    }

    private void adjustBrightness(int x) {
        float raw = ((float) x) / mScreenWidth;

        // Add a padding to the brightness control on both sides to
        // make it easier to reach min/max brightness
        float padded = Math.min(1.0f - BRIGHTNESS_CONTROL_PADDING,
                Math.max(BRIGHTNESS_CONTROL_PADDING, raw));
        float value = (padded - BRIGHTNESS_CONTROL_PADDING) /
                (1 - (2.0f * BRIGHTNESS_CONTROL_PADDING));

        int newBrightness = mMinBrightness + (int) Math.round(value *
                (android.os.PowerManager.BRIGHTNESS_ON - mMinBrightness));
        newBrightness = Math.min(newBrightness, android.os.PowerManager.BRIGHTNESS_ON);
        newBrightness = Math.max(newBrightness, mMinBrightness);

        try {
            IPowerManager power = IPowerManager.Stub.asInterface(
                    ServiceManager.getService("power"));
            if (power != null) {
                power.setBacklightBrightness(newBrightness);
                Settings.System.putInt(mContext.getContentResolver(),
                        Settings.System.SCREEN_BRIGHTNESS, newBrightness);
            }
        } catch (RemoteException e) {
            Slog.w(TAG, "Setting Brightness failed: " + e);
        }
    }

    boolean interceptTouchEvent(MotionEvent event) {
        if (SPEW) {
            Slog.d(TAG, "Touch: rawY=" + event.getRawY() + " event=" + event + " mDisabled="
@@ -1685,12 +1727,16 @@ public class PhoneStatusBar extends BaseStatusBar {
        final int action = event.getAction();
        final int statusBarSize = getStatusBarHeight();
        final int hitSize = statusBarSize*2;
        final int x = (int)event.getRawX();
        final int y = (int)event.getRawY();
        if (action == MotionEvent.ACTION_DOWN) {
            if (!areLightsOn()) {
                setLightsOn(true);
            }
            mLinger = 0;
            mInitialTouchX = x;
            mInitialTouchY = y;

            if (!mExpanded) {
                mViewDelta = statusBarSize - y;
            } else {
@@ -1707,54 +1753,47 @@ public class PhoneStatusBar extends BaseStatusBar {
                // We drop events at the edge of the screen to make the windowshade come
                // down by accident less, especially when pushing open a device with a keyboard
                // that rotates (like g1 and droid)
                int x = (int)event.getRawX();

                final int edgeBorder = mEdgeBorder;
                if (x >= edgeBorder && x < mDisplayMetrics.widthPixels - edgeBorder) {
                    prepareTracking(y, !mExpanded);// opening if we're not already fully visible
                    trackMovement(event);
                }
            }
            if (mTracking && mBrightnessControl) {
                mHandler.removeCallbacks(mLongPressBrightnessChange);
                mHandler.postDelayed(mLongPressBrightnessChange,
                        BRIGHTNESS_CONTROL_LONG_PRESS_TIMEOUT);
            }
        } else if (mTracking) {
            trackMovement(event);
            if (action == MotionEvent.ACTION_MOVE) {
                if (mAnimatingReveal && (y + mViewDelta) < mNotificationPanelMinHeight) {
                    if (mBrightnessControl && !mAutoBrightness) {
                    if (mBrightnessControl) {
                        mVelocityTracker.computeCurrentVelocity(1000);
                        float yVel = mVelocityTracker.getYVelocity();
                        yVel = Math.abs(yVel);
                        if (yVel < 50.0f) {
                            if (mLinger > 20) {
                                float x = (float) event.getRawX();
                                float raw = (x / mScreenWidth);
                                // Add a padding to the brightness control on both sides to make it easier
                                // to reach min/max brightness
                                float padded = Math.min(1.0f - BRIGHTNESS_CONTROL_PADDING, Math.max(BRIGHTNESS_CONTROL_PADDING, raw));
                                float value = (padded - BRIGHTNESS_CONTROL_PADDING) / (1 - (2.0f * BRIGHTNESS_CONTROL_PADDING));
                                int newBrightness = mMinBrightness + (int) Math.round(value *
                                        (android.os.PowerManager.BRIGHTNESS_ON - mMinBrightness));
                                newBrightness = Math.min(newBrightness, android.os.PowerManager.BRIGHTNESS_ON);
                                newBrightness = Math.max(newBrightness, mMinBrightness);
                                try {
                                    IPowerManager power = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
                                    if (power != null) {
                                        power.setBacklightBrightness(newBrightness);
                                        Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS,
                                                newBrightness);
                                    }
                                } catch (RemoteException e) {
                                    Slog.w(TAG, "Setting Brightness failed: " + e);
                                }
                            if (mLinger > BRIGHTNESS_CONTROL_LINGER_THRESHOLD) {
                                adjustBrightness(x);
                            } else {
                                mLinger++;
                            }
                        }
                        int touchSlop = ViewConfiguration.get(mContext).getScaledTouchSlop();
                        if (Math.abs(x - mInitialTouchX) > touchSlop ||
                                Math.abs(y - mInitialTouchY) > touchSlop) {
                            mHandler.removeCallbacks(mLongPressBrightnessChange);
                        }
                    }
                } else  {
                    mHandler.removeCallbacks(mLongPressBrightnessChange);
                    mAnimatingReveal = false;
                    updateExpandedViewPos(y + mViewDelta);
                }
            } else if (action == MotionEvent.ACTION_UP
                    || action == MotionEvent.ACTION_CANCEL) {
                mHandler.removeCallbacks(mLongPressBrightnessChange);
                mLinger = 0;
                mVelocityTracker.computeCurrentVelocity(1000);