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

Commit 53ece467 authored by Roman Birg's avatar Roman Birg
Browse files

SystemUI: improve battery saver tile



- Fix the tile not showing, the javadoc states that it will return 0 if
  profiles are not supported, but it was actually returning -1.

- Now disables itself when the device is plugged in to reflect to the user
  that they cannot toggle it on right now.

- Don't wait for power manager to update its internal state before
  updating the visual tile state.

Change-Id: I8ab6adb2bfa728a242368294c1f066d468a86f09
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
parent 9d474093
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -104,6 +104,8 @@
    <string name="quick_settings_profiles_off">Profiles disabled</string>
    <string name="quick_settings_heads_up_label">Heads up</string>
    <string name="quick_settings_battery_saver_label">Battery saver</string>
    <!-- quick settings battery saver label to show when device is charging and tile is disabled -->
    <string name="quick_settings_battery_saver_label_charging">Battery saver (charging)</string>
    <string name="quick_settings_caffeine_label">Caffeine</string>

    <!-- Content description of the sync tile in quick settings when off (not shown on the screen). [CHAR LIMIT=NONE] -->
+2 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.view.ViewGroup;

import android.widget.RemoteViews;
import com.android.systemui.qs.QSTile.State;
import com.android.systemui.statusbar.policy.BatteryController;
import com.android.systemui.statusbar.policy.BluetoothController;
import com.android.systemui.statusbar.policy.CastController;
import com.android.systemui.statusbar.policy.FlashlightController;
@@ -344,6 +345,7 @@ public abstract class QSTile<TState extends State> implements Listenable {
        CastController getCastController();
        FlashlightController getFlashlightController();
        KeyguardMonitor getKeyguardMonitor();
        BatteryController getBatteryController();
        boolean isEditing();
        void setEditing(boolean editing);
        void resetTiles();
+31 −11
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ import android.provider.Settings;

import com.android.systemui.qs.QSTile;
import com.android.systemui.R;
import com.android.systemui.statusbar.policy.BatteryController;
import com.android.systemui.statusbar.policy.BatteryStateRegistar;

import cyanogenmod.power.PerformanceManager;

@@ -36,13 +38,15 @@ public class BatterySaverTile extends QSTile<QSTile.BooleanState> {
    private static final Intent BATTERY_SETTINGS = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);

    private final PowerManager mPm;
    private final PerformanceManager mPerformanceManager;
    private final boolean mHasPowerProfiles;

    private boolean mListening;
    private boolean mPluggedIn;

    public BatterySaverTile(Host host) {
        super(host);
        mPm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        mPerformanceManager = PerformanceManager.getInstance(mContext);
        mHasPowerProfiles = PerformanceManager.getInstance(mContext).getNumberOfProfiles() > 0;
    }

    @Override
@@ -53,7 +57,7 @@ public class BatterySaverTile extends QSTile<QSTile.BooleanState> {
    @Override
    public void handleClick() {
        mPm.setPowerSaveMode(!mState.value);
        refreshState();
        refreshState(!mState.value);
    }

    @Override
@@ -63,8 +67,8 @@ public class BatterySaverTile extends QSTile<QSTile.BooleanState> {

    @Override
    protected void handleUpdateState(BooleanState state, Object arg) {
        state.value = mPm.isPowerSaveMode();
        state.visible = mPerformanceManager.getNumberOfProfiles() == 0;
        state.value = arg instanceof Boolean ? (boolean) arg : mPm.isPowerSaveMode();
        state.visible =  !mHasPowerProfiles;
        state.label = mContext.getString(R.string.quick_settings_battery_saver_label);
        if (state.value) {
            state.icon = ResourceIcon.get(R.drawable.ic_qs_battery_saver_on);
@@ -75,6 +79,11 @@ public class BatterySaverTile extends QSTile<QSTile.BooleanState> {
            state.contentDescription =  mContext.getString(
                    R.string.accessibility_quick_settings_battery_saver_off);
        }

        state.enabled = !mPluggedIn;
        if (mPluggedIn) {
            state.label = mContext.getString(R.string.quick_settings_battery_saver_label_charging);
        }
    }

    @Override
@@ -93,11 +102,24 @@ public class BatterySaverTile extends QSTile<QSTile.BooleanState> {
        return CMMetricsLogger.TILE_BATTERY_SAVER;
    }

    private ContentObserver mObserver = new ContentObserver(mHandler) {
    private BatteryStateRegistar.BatteryStateChangeCallback mBatteryState
            = new BatteryStateRegistar.BatteryStateChangeCallback() {
        @Override
        public void onChange(boolean selfChange, Uri uri) {
        public void onBatteryLevelChanged(boolean present, int level, boolean pluggedIn,
                boolean charging) {
            mPluggedIn = pluggedIn || charging;
            refreshState();
        }

        @Override
        public void onPowerSaveChanged() {
            refreshState();
        }

        @Override
        public void onBatteryStyleChanged(int style, int percentMode) {
            // ignore
        }
    };

    @Override
@@ -106,11 +128,9 @@ public class BatterySaverTile extends QSTile<QSTile.BooleanState> {
        mListening = listening;

        if (listening) {
            mContext.getContentResolver().registerContentObserver(
                    Settings.Global.getUriFor(Settings.Global.LOW_POWER_MODE),
                    false, mObserver);
            getHost().getBatteryController().addStateChangedCallback(mBatteryState);
        } else {
            mContext.getContentResolver().unregisterContentObserver(mObserver);
            getHost().getBatteryController().removeStateChangedCallback(mBatteryState);
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -1181,7 +1181,7 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
                        mNetworkController, mZenModeController, mHotspotController,
                        mCastController, mFlashlightController,
                        mUserSwitcherController, mKeyguardMonitor,
                        mSecurityController);
                        mSecurityController, mBatteryController);
            }
            mQSPanel.setHost(mQSTileHost);
            if (mBrightnessMirrorController == null) {
+9 −1
Original line number Diff line number Diff line
@@ -67,6 +67,7 @@ import com.android.systemui.qs.tiles.UsbTetherTile;
import com.android.systemui.qs.tiles.VolumeTile;
import com.android.systemui.qs.tiles.WifiTile;
import com.android.systemui.statusbar.CustomTileData;
import com.android.systemui.statusbar.policy.BatteryController;
import com.android.systemui.statusbar.policy.BluetoothController;
import com.android.systemui.statusbar.policy.CastController;
import com.android.systemui.statusbar.policy.FlashlightController;
@@ -115,6 +116,7 @@ public class QSTileHost implements QSTile.Host, Tunable {
    private final UserSwitcherController mUserSwitcherController;
    private final KeyguardMonitor mKeyguard;
    private final SecurityController mSecurity;
    private final BatteryController mBattery;

    private CustomTileData mCustomTileData;
    private CustomTileListenerService mCustomTileListenerService;
@@ -127,7 +129,7 @@ public class QSTileHost implements QSTile.Host, Tunable {
            ZenModeController zen, HotspotController hotspot,
            CastController cast, FlashlightController flashlight,
            UserSwitcherController userSwitcher, KeyguardMonitor keyguard,
            SecurityController security) {
            SecurityController security, BatteryController battery) {
        mContext = context;
        mStatusBar = statusBar;
        mBluetooth = bluetooth;
@@ -141,6 +143,7 @@ public class QSTileHost implements QSTile.Host, Tunable {
        mUserSwitcherController = userSwitcher;
        mKeyguard = keyguard;
        mSecurity = security;
        mBattery = battery;
        mCustomTileData = new CustomTileData();

        final HandlerThread ht = new HandlerThread(QSTileHost.class.getSimpleName(),
@@ -281,6 +284,11 @@ public class QSTileHost implements QSTile.Host, Tunable {
        return mKeyguard;
    }

    @Override
    public BatteryController getBatteryController() {
        return mBattery;
    }

    public UserSwitcherController getUserSwitcherController() {
        return mUserSwitcherController;
    }
Loading