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

Commit a5a73c5a authored by Evan Laird's avatar Evan Laird
Browse files

Move battery % down in QS, fix a lot of things

- Move the battery percentage next to the system icons in quick settings
- Always show the percentage, and optionally show the estimate text
based on battery settings
- Move the estimate text into the BatteryMeterView
- Move the fetching of the estimate off of the main thread

Test: visual
Change-Id: Ie37952079b2394c67464f69eb8a2f0089b08875d
Fixes: 119799219 - padding
Fixes: 120996084 - moving estimate fetch off main thread
Bug: 116481529
parent 5af6efd9
Loading
Loading
Loading
Loading
+0 −7
Original line number Diff line number Diff line
@@ -51,11 +51,4 @@
        android:layout_width="wrap_content"
        android:paddingEnd="2dp" />

    <TextView
        android:id="@+id/batteryRemainingText"
        android:textAppearance="@style/TextAppearance.QS.TileLabel"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:gravity="center_vertical" />

</LinearLayout>
+0 −6
Original line number Diff line number Diff line
@@ -64,11 +64,5 @@

    <include layout="@layout/ongoing_privacy_chip" />

    <com.android.systemui.BatteryMeterView
        android:id="@+id/battery"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:gravity="center_vertical|end"
        android:layout_gravity="center_vertical|end" />
    </LinearLayout>
</LinearLayout>
+17 −4
Original line number Diff line number Diff line
@@ -71,11 +71,12 @@ public class BatteryMeterView extends LinearLayout implements


    @Retention(SOURCE)
    @IntDef({MODE_DEFAULT, MODE_ON, MODE_OFF})
    @IntDef({MODE_DEFAULT, MODE_ON, MODE_OFF, MODE_ESTIMATE})
    public @interface BatteryPercentMode {}
    public static final int MODE_DEFAULT = 0;
    public static final int MODE_ON = 1;
    public static final int MODE_OFF = 2;
    public static final int MODE_ESTIMATE = 3;

    private final BatteryMeterDrawableBase mDrawable;
    private final String mSlotBattery;
@@ -93,6 +94,7 @@ public class BatteryMeterView extends LinearLayout implements
    // Some places may need to show the battery conditionally, and not obey the tuner
    private boolean mIgnoreTunerUpdates;
    private boolean mIsSubscribedForTunerUpdates;
    private boolean mCharging;

    private int mDarkModeBackgroundColor;
    private int mDarkModeFillColor;
@@ -308,6 +310,7 @@ public class BatteryMeterView extends LinearLayout implements
    public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
        mDrawable.setBatteryLevel(level);
        mDrawable.setCharging(pluggedIn);
        mCharging = pluggedIn;
        mLevel = level;
        updatePercentText();
        setContentDescription(
@@ -337,11 +340,21 @@ public class BatteryMeterView extends LinearLayout implements
    }

    private void updatePercentText() {
        if (mBatteryController == null) {
            return;
        }

        if (mBatteryPercentView != null) {
            if (mShowPercentMode == MODE_ESTIMATE && !mCharging) {
                mBatteryController.getEstimatedTimeRemainingString((String estimate) -> {
                    mBatteryPercentView.setText(estimate);
                });
            } else {
                mBatteryPercentView.setText(
                        NumberFormat.getPercentInstance().format(mLevel / 100f));
            }
        }
    }

    private void updateShowPercent() {
        final boolean showing = mBatteryPercentView != null;
@@ -350,7 +363,7 @@ public class BatteryMeterView extends LinearLayout implements
                SHOW_BATTERY_PERCENT, 0, mUser);

        if ((mShowPercentAvailable && systemSetting && mShowPercentMode != MODE_OFF)
                || mShowPercentMode == MODE_ON) {
                || mShowPercentMode == MODE_ON || mShowPercentMode == MODE_ESTIMATE) {
            if (!showing) {
                mBatteryPercentView = loadPercentView();
                if (mTextColor != 0) mBatteryPercentView.setTextColor(mTextColor);
+2 −49
Original line number Diff line number Diff line
@@ -141,14 +141,11 @@ public class QuickStatusBarHeader extends RelativeLayout implements
    private View mStatusSeparator;
    private ImageView mRingerModeIcon;
    private TextView mRingerModeTextView;
    private BatteryMeterView mBatteryMeterView;
    private Clock mClockView;
    private DateView mDateView;
    private OngoingPrivacyChip mPrivacyChip;
    private Space mSpace;
    private BatteryMeterView mBatteryRemainingIcon;
    private TextView mBatteryRemainingText;
    private boolean mShowBatteryPercentAndEstimate;

    private PrivacyItemController mPrivacyItemController;
    /** Counts how many times the long press tooltip has been shown to the user. */
@@ -229,13 +226,6 @@ public class QuickStatusBarHeader extends RelativeLayout implements
        // Set the correct tint for the status icons so they contrast
        mIconManager.setTint(fillColor);

        mShowBatteryPercentAndEstimate = mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_battery_percentage_setting_available);

        mBatteryMeterView = findViewById(R.id.battery);
        mBatteryMeterView.setPercentShowMode(mShowBatteryPercentAndEstimate
                ? BatteryMeterView.MODE_ON : BatteryMeterView.MODE_OFF);
        mBatteryMeterView.setOnClickListener(this);
        mClockView = findViewById(R.id.clock);
        mClockView.setOnClickListener(this);
        mDateView = findViewById(R.id.date);
@@ -245,13 +235,8 @@ public class QuickStatusBarHeader extends RelativeLayout implements

        // Tint for the battery icons are handled in setupHost()
        mBatteryRemainingIcon = findViewById(R.id.batteryRemainingIcon);
        mBatteryRemainingIcon.setPercentShowMode(BatteryMeterView.MODE_OFF);
        // Don't need to worry about tuner settings for this icon
        mBatteryRemainingIcon.setIgnoreTunerUpdates(true);

        mBatteryRemainingText = findViewById(R.id.batteryRemainingText);
        mBatteryRemainingText.setTextColor(fillColor);

        updateShowPercent();
    }

@@ -268,10 +253,8 @@ public class QuickStatusBarHeader extends RelativeLayout implements
    }

    private void setChipVisibility(boolean chipVisible) {
        mBatteryMeterView.setVisibility(View.VISIBLE);
        if (chipVisible) {
            mPrivacyChip.setVisibility(View.VISIBLE);
            if (mHasTopCutout) mBatteryMeterView.setVisibility(View.GONE);
        } else {
            mPrivacyChip.setVisibility(View.GONE);
        }
@@ -339,7 +322,6 @@ public class QuickStatusBarHeader extends RelativeLayout implements
        // Update color schemes in landscape to use wallpaperTextColor
        boolean shouldUseWallpaperTextColor =
                newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE;
        mBatteryMeterView.useWallpaperTextColor(shouldUseWallpaperTextColor);
        mClockView.useWallpaperTextColor(shouldUseWallpaperTextColor);
    }

@@ -415,13 +397,6 @@ public class QuickStatusBarHeader extends RelativeLayout implements
                .build();
    }

    private void updateBatteryRemainingText() {
        if (!mShowBatteryPercentAndEstimate) {
            return;
        }
        mBatteryRemainingText.setText(mBatteryController.getEstimatedTimeRemainingString());
    }

    public void setExpanded(boolean expanded) {
        if (mExpanded == expanded) return;
        mExpanded = expanded;
@@ -519,7 +494,6 @@ public class QuickStatusBarHeader extends RelativeLayout implements
            }
        }
        mSpace.setLayoutParams(lp);
        // Decide whether to show BatteryMeterView
        setChipVisibility(mPrivacyChip.getVisibility() == View.VISIBLE);
        return super.onApplyWindowInsets(insets);
    }
@@ -546,7 +520,6 @@ public class QuickStatusBarHeader extends RelativeLayout implements
            mAlarmController.addCallback(this);
            mContext.registerReceiver(mRingerReceiver,
                    new IntentFilter(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION));
            updateBatteryRemainingText();
        } else {
            mZenController.removeCallback(this);
            mAlarmController.removeCallback(this);
@@ -559,9 +532,6 @@ public class QuickStatusBarHeader extends RelativeLayout implements
        if (v == mClockView) {
            mActivityStarter.postStartActivityDismissingKeyguard(new Intent(
                    AlarmClock.ACTION_SHOW_ALARMS),0);
        } else if (v == mBatteryMeterView) {
            mActivityStarter.postStartActivityDismissingKeyguard(new Intent(
                    Intent.ACTION_POWER_USAGE_SUMMARY),0);
        } else if (v == mPrivacyChip) {
            Handler mUiHandler = new Handler(Looper.getMainLooper());
            mUiHandler.post(() -> {
@@ -713,9 +683,6 @@ public class QuickStatusBarHeader extends RelativeLayout implements
        mHeaderQsPanel.setQSPanelAndHeader(mQsPanel, this);
        mHeaderQsPanel.setHost(host, null /* No customization in header */);

        // Use SystemUI context to get battery meter colors, and let it use the default tint (white)
        mBatteryMeterView.setColorsFromContext(mHost.getContext());
        mBatteryMeterView.onDarkChanged(new Rect(), 0, DarkIconDispatcher.DEFAULT_ICON_TINT);

        Rect tintArea = new Rect(0, 0, 0, 0);
        int colorForeground = Utils.getColorAttrDefaultColor(getContext(),
@@ -763,22 +730,8 @@ public class QuickStatusBarHeader extends RelativeLayout implements
                .getIntForUser(getContext().getContentResolver(),
                        SHOW_BATTERY_PERCENT, 0, ActivityManager.getCurrentUser());

        mShowBatteryPercentAndEstimate = systemSetting;

        updateBatteryViews();
    }

    private void updateBatteryViews() {
        if (mShowBatteryPercentAndEstimate) {
            mBatteryMeterView.setPercentShowMode(BatteryMeterView.MODE_ON);
            mBatteryRemainingIcon.setVisibility(View.VISIBLE);
            mBatteryRemainingText.setVisibility(View.VISIBLE);
            updateBatteryRemainingText();
        } else {
            mBatteryMeterView.setPercentShowMode(BatteryMeterView.MODE_OFF);
            mBatteryRemainingIcon.setVisibility(View.GONE);
            mBatteryRemainingText.setVisibility(View.GONE);
        }
        mBatteryRemainingIcon.setPercentShowMode(systemSetting
                ? BatteryMeterView.MODE_ESTIMATE : BatteryMeterView.MODE_ON);
    }

    private final class PercentSettingObserver extends ContentObserver {
+25 −7
Original line number Diff line number Diff line
@@ -48,18 +48,36 @@ public interface BatteryController extends DemoMode, Dumpable,
    }

    /**
     * A listener that will be notified whenever a change in battery level or power save mode
     * has occurred.
     * A listener that will be notified whenever a change in battery level or power save mode has
     * occurred.
     */
    interface BatteryStateChangeCallback {
        default void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {}
        default void onPowerSaveChanged(boolean isPowerSave) {}

        default void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
        }

        default void onPowerSaveChanged(boolean isPowerSave) {
        }
    }

    /**
     * If available, get the estimated battery time remaining as a string.
     *
     * @param completion A lambda that will be called with the result of fetching the estimate. The
     * first time this method is called may need to be dispatched to a background thread. The
     * completion is called on the main thread
     */
    default void getEstimatedTimeRemainingString(EstimateFetchCompletion completion) {}

    /**
     * Callback called when the estimated time remaining text is fetched.
     */
    public interface EstimateFetchCompletion {

        /**
     * If available, get the estimated battery time remaining as a string
         * The callback
         * @param estimate the estimate
         */
    default String getEstimatedTimeRemainingString() {
        return null;
        void onBatteryRemainingEstimateRetrieved(String estimate);
    }
}
Loading