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

Commit b3feb377 authored by David Morgan's avatar David Morgan
Browse files

Rework multiple value power widgets (1/2)

The current batch of multi-value power widgets use a set of possible combinations
for cylcing, i.e. Auto/Dim/40%/100% for brightness. Instead, we can leverage
the MultiSelectListPreference to allow the user to select which values he or she
would like.

Change-Id: I1fb03a39ab75bd32fbf0a5de9e0802c393603e57
parent 27dc1bcf
Loading
Loading
Loading
Loading
+115 −158
Original line number Diff line number Diff line

package com.android.systemui.statusbar.powerwidget;

import com.android.systemui.R;
@@ -5,106 +6,129 @@ import com.android.systemui.R;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.IPowerManager;
import android.os.Power;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.preference.MultiSelectListPreference;
import android.provider.Settings;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class BrightnessButton extends PowerButton {

    private static final String TAG = "BrightnessButton";

    /**
     * Minimum and maximum brightnesses. Don't go to 0 since that makes the
     * display unusable
     */
    private static final int MIN_BACKLIGHT = Power.BRIGHTNESS_DIM + 10;
    private static final int MAX_BACKLIGHT = Power.BRIGHTNESS_ON;

    // Auto-backlight level
    private static final int AUTO_BACKLIGHT = -1;
    // Mid-range brightness values + thresholds
    private static final int DEFAULT_BACKLIGHT = (int) (android.os.Power.BRIGHTNESS_ON * 0.4f);
    private static final int LOW_BACKLIGHT = (int) (android.os.Power.BRIGHTNESS_ON * 0.25f);
    private static final int MID_BACKLIGHT = (int) (android.os.Power.BRIGHTNESS_ON * 0.5f);
    private static final int HIGH_BACKLIGHT = (int) (android.os.Power.BRIGHTNESS_ON * 0.75f);
    private static final int LOW_BACKLIGHT = (int) (MAX_BACKLIGHT * 0.25f);
    private static final int MID_BACKLIGHT = (int) (MAX_BACKLIGHT * 0.5f);
    private static final int HIGH_BACKLIGHT = (int) (MAX_BACKLIGHT * 0.75f);

    // Defaults for now. MIN_BACKLIGHT will be replaced later
    private static final int[] BACKLIGHTS = new int[] {
            AUTO_BACKLIGHT, MIN_BACKLIGHT, LOW_BACKLIGHT, MID_BACKLIGHT, HIGH_BACKLIGHT,
            MAX_BACKLIGHT
    };

    private static final Uri BRIGHTNESS_URI = Settings.System
            .getUriFor(Settings.System.SCREEN_BRIGHTNESS);
    private static final Uri BRIGHTNESS_MODE_URI = Settings.System
            .getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE);
    private static final List<Uri> OBSERVED_URIS = new ArrayList<Uri>();
    static {
        OBSERVED_URIS.add(BRIGHTNESS_URI);
        OBSERVED_URIS.add(BRIGHTNESS_MODE_URI);
        OBSERVED_URIS.add(Settings.System.getUriFor(Settings.System.LIGHT_SENSOR_CUSTOM));
        OBSERVED_URIS.add(Settings.System.getUriFor(Settings.System.LIGHT_SCREEN_DIM));
        OBSERVED_URIS.add(Settings.System.getUriFor(Settings.System.EXPANDED_BRIGHTNESS_MODE));
    }

    // whether or not backlight is supported
    private static Boolean SUPPORTS_AUTO_BACKLIGHT=null;
    private boolean mAutoBrightnessSupported = false;

    // CM modes of operation
    private static final int CM_MODE_AUTO_MIN_DEF_MAX = 0;
    private static final int CM_MODE_AUTO_MIN_LOW_MID_HIGH_MAX = 1;
    private static final int CM_MODE_AUTO_LOW_MAX = 2;
    private static final int CM_MODE_MIN_MAX = 3;
    private boolean mAutoBrightness = false;

    private static final List<Uri> OBSERVED_URIS = new ArrayList<Uri>();
    static {
        OBSERVED_URIS.add(Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS));
        OBSERVED_URIS.add(Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE));
    private int mCurrentBrightness;

    private int mCurrentBacklightIndex = 0;

    private int[] mBacklightValues = new int[] {
            0, 1, 2, 3, 4, 5
    };

    public BrightnessButton() {
        mType = BUTTON_BRIGHTNESS;
    }

    public BrightnessButton() { mType = BUTTON_BRIGHTNESS; }
    @Override
    protected void setupButton(View view) {
        super.setupButton(view);
        if (mView != null) {
            Context context = mView.getContext();
            mAutoBrightnessSupported = context.getResources().getBoolean(
                    com.android.internal.R.bool.config_automatic_brightness_available);
            updateSettings();
        }
    }

    @Override
    protected void updateState() {
        Context context = mView.getContext();
        if (isBrightnessSetToAutomatic(context)) {
        if (mAutoBrightness) {
            mIcon = R.drawable.stat_brightness_auto;
            mState = STATE_ENABLED;
        } else if (mCurrentBrightness <= LOW_BACKLIGHT) {
            mIcon = R.drawable.stat_brightness_off;
            mState = STATE_DISABLED;
        } else if (mCurrentBrightness <= MID_BACKLIGHT) {
            mIcon = R.drawable.stat_brightness_mid;
            mState = STATE_INTERMEDIATE;
        } else {
            switch(getBrightnessState(context)) {
                case STATE_ENABLED:
            mIcon = R.drawable.stat_brightness_on;
            mState = STATE_ENABLED;
                    break;
                case STATE_TURNING_ON:
                    mIcon = R.drawable.stat_brightness_on;
                    mState = STATE_INTERMEDIATE;
                    break;
                case STATE_TURNING_OFF:
                    mIcon = R.drawable.stat_brightness_off;
                    mState = STATE_INTERMEDIATE;
                    break;
                default:
                    mIcon = R.drawable.stat_brightness_off;
                    mState = STATE_DISABLED;
                    break;
            }
        }
    }

    @Override
    protected void toggleState() {
        Context context = mView.getContext();
        try {
            IPowerManager power = IPowerManager.Stub.asInterface(ServiceManager
                    .getService("power"));
            IPowerManager power = IPowerManager.Stub
                    .asInterface(ServiceManager.getService("power"));
            if (power != null) {
                int brightness = getNextBrightnessValue(context);
                ContentResolver contentResolver = context.getContentResolver();
                ContentResolver resolver = mView.getContext().getContentResolver();
                mCurrentBacklightIndex++;
                if (mCurrentBacklightIndex > mBacklightValues.length - 1) {
                    mCurrentBacklightIndex = 0;
                }
                int backlightIndex = mBacklightValues[mCurrentBacklightIndex];
                int brightness = BACKLIGHTS[backlightIndex];
                if (brightness == AUTO_BACKLIGHT) {
                    Settings.System.putInt(contentResolver,
                            Settings.System.SCREEN_BRIGHTNESS_MODE,
                    Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE,
                            Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
                } else {
                    if (isAutomaticModeSupported(context)) {
                        Settings.System.putInt(contentResolver,
                                Settings.System.SCREEN_BRIGHTNESS_MODE,
                    if (mAutoBrightnessSupported) {
                        Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE,
                                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
                    }
                    power.setBacklightBrightness(brightness);
                    Settings.System.putInt(contentResolver,
                            Settings.System.SCREEN_BRIGHTNESS, brightness);
                    Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
                }
            }
        } catch (RemoteException e) {
            Log.d("PowerWidget", "toggleBrightness: " + e);
            Log.e(TAG, "toggleState()", e);
        }

    }

    @Override
@@ -121,128 +145,61 @@ public class BrightnessButton extends PowerButton {
        return OBSERVED_URIS;
    }

    private static int getMinBacklight(Context context) {
        if (Settings.System.getInt(context.getContentResolver(),
                Settings.System.LIGHT_SENSOR_CUSTOM, 0) != 0) {
            return Settings.System.getInt(context.getContentResolver(),
                    Settings.System.LIGHT_SCREEN_DIM, MIN_BACKLIGHT);
        } else {
            return MIN_BACKLIGHT;
        }
    }

    private static int getNextBrightnessValue(Context context) {
        int brightness = Settings.System.getInt(context.getContentResolver(),
    @Override
    protected void onChangeUri(Uri uri) {
        ContentResolver resolver = mView.getContext().getContentResolver();
        if (BRIGHTNESS_URI.equals(uri)) {
            mCurrentBrightness = Settings.System.getInt(resolver,
                    Settings.System.SCREEN_BRIGHTNESS, 0);
        int currentMode = getCurrentCMMode(context);

        if (isAutomaticModeSupported(context) && isBrightnessSetToAutomatic(context)) {
            if (currentMode == CM_MODE_AUTO_LOW_MAX) {
                return LOW_BACKLIGHT;
        } else if (BRIGHTNESS_MODE_URI.equals(uri)) {
            mAutoBrightness = (Settings.System.getInt(resolver,
                    Settings.System.SCREEN_BRIGHTNESS_MODE, 0) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
        } else {
                return getMinBacklight(context);
            updateSettings();
        }
        } else if (brightness < LOW_BACKLIGHT) {
            if (currentMode == CM_MODE_AUTO_LOW_MAX) {
                return LOW_BACKLIGHT;
            } else if (currentMode == CM_MODE_MIN_MAX) {
                return MAX_BACKLIGHT;
            } else {
                return DEFAULT_BACKLIGHT;
            }
        } else if (brightness < DEFAULT_BACKLIGHT) {
            if (currentMode == CM_MODE_AUTO_MIN_DEF_MAX) {
                return DEFAULT_BACKLIGHT;
            } else if (currentMode == CM_MODE_AUTO_LOW_MAX || currentMode == CM_MODE_MIN_MAX) {
                return MAX_BACKLIGHT;
            } else {
                return MID_BACKLIGHT;
            }
        } else if (brightness < MID_BACKLIGHT) {
            if (currentMode == CM_MODE_AUTO_MIN_LOW_MID_HIGH_MAX) {
                return MID_BACKLIGHT;
            } else {
                return MAX_BACKLIGHT;
            }
        } else if (brightness < HIGH_BACKLIGHT) {
            if (currentMode == CM_MODE_AUTO_MIN_LOW_MID_HIGH_MAX) {
                return HIGH_BACKLIGHT;
            } else {
                return MAX_BACKLIGHT;
            }
        } else if (brightness < MAX_BACKLIGHT) {
            return MAX_BACKLIGHT;
        } else if (isAutomaticModeSupported(context) && currentMode != CM_MODE_MIN_MAX) {
            return AUTO_BACKLIGHT;
        } else if (currentMode == CM_MODE_AUTO_LOW_MAX) {
            return LOW_BACKLIGHT;
        } else {
            return getMinBacklight(context);
    }
    }

    private static int getBrightnessState(Context context) {
        int brightness = Settings.System.getInt(context.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS,0);

        int currentMode = getCurrentCMMode(context);
    private void updateSettings() {
        ContentResolver resolver = mView.getContext().getContentResolver();

        if (brightness < LOW_BACKLIGHT) {
            return PowerButton.STATE_DISABLED;
        } else if (brightness < DEFAULT_BACKLIGHT) {
            return PowerButton.STATE_DISABLED;
        } else if (brightness < MID_BACKLIGHT) {
            if (currentMode == CM_MODE_AUTO_MIN_LOW_MID_HIGH_MAX) {
                return PowerButton.STATE_DISABLED;
        boolean lightSensorCustom = (Settings.System.getInt(resolver,
                Settings.System.LIGHT_SENSOR_CUSTOM, 0) != 0);
        if (lightSensorCustom) {
            BACKLIGHTS[1] = Settings.System.getInt(resolver, Settings.System.LIGHT_SCREEN_DIM,
                    MIN_BACKLIGHT);
        } else {
                return PowerButton.STATE_TURNING_OFF;
            BACKLIGHTS[1] = MIN_BACKLIGHT;
        }
        } else if (brightness < HIGH_BACKLIGHT) {
            if (currentMode == CM_MODE_AUTO_MIN_LOW_MID_HIGH_MAX) {
                return PowerButton.STATE_TURNING_OFF;
            } else {
                return PowerButton.STATE_TURNING_ON;

        String[] modes = MultiSelectListPreference.parseStoredValue(Settings.System.getString(
                resolver, Settings.System.EXPANDED_BRIGHTNESS_MODE));
        if (modes != null) {
            mBacklightValues = new int[modes.length];
            for (int i = 0; i < modes.length; i++) {
                mBacklightValues[i] = Integer.valueOf(modes[i]);
            }
        } else if (brightness < MAX_BACKLIGHT) {
            return PowerButton.STATE_TURNING_ON;
        } else {
            return PowerButton.STATE_ENABLED;
        }
            // If no modes configured default to just MAX_BACKLIGHT
            mBacklightValues = new int[] {
                5
            };
        }

    private static boolean isAutomaticModeSupported(Context context) {
        if (SUPPORTS_AUTO_BACKLIGHT == null) {
            if (context.getResources().getBoolean(
                    com.android.internal.R.bool.config_automatic_brightness_available)) {
                SUPPORTS_AUTO_BACKLIGHT=true;
        mAutoBrightness = (Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE,
                0) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
        if (mAutoBrightness) {
            mCurrentBrightness = AUTO_BACKLIGHT;
        } else {
                SUPPORTS_AUTO_BACKLIGHT=false;
            }
        }

        return SUPPORTS_AUTO_BACKLIGHT;
            mCurrentBrightness = Settings.System.getInt(resolver,
                    Settings.System.SCREEN_BRIGHTNESS, -1);
            for (int i = 0; i < BACKLIGHTS.length; i++) {
                if (mCurrentBrightness == BACKLIGHTS[i]) {
                    mCurrentBacklightIndex = i;
                    break;
                }

    private static boolean isBrightnessSetToAutomatic(Context context) {
        try {
            IPowerManager power = IPowerManager.Stub.asInterface(ServiceManager
                    .getService("power"));
            if (power != null) {
                int brightnessMode = Settings.System.getInt(context
                        .getContentResolver(),
                        Settings.System.SCREEN_BRIGHTNESS_MODE);
                return brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
            }
        } catch (Exception e) {
            Log.d("PowerWidget", "getBrightnessMode: " + e);
        }

        return false;
        updateState();
    }

    private static int getCurrentCMMode(Context context) {
        return Settings.System.getInt(context.getContentResolver(),
                Settings.System.EXPANDED_BRIGHTNESS_MODE,
                CM_MODE_AUTO_MIN_DEF_MAX);
    }
}
+163 −201

File changed.

Preview size limit exceeded, changes collapsed.