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

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

[Sb] Properly inflate the estimate view

The previous CL created the estimate view, but never added it to the
parent view, and thus was never shown.

Also, this CL slightly reworks the logic for showing percent, since the
slow case is still done on the main thread, but we can skip it in some
circumstances.

Test: manual
Test: BatteryMeterViewTest
Bug: 314812750
Flag: ACONFIG com.android.settingslib.flags.new_status_bar_icons DEVELOPMENT
Change-Id: I5ad19ac9d95a81a0b149dbf4b0b420d47092f979
parent c2044fe5
Loading
Loading
Loading
Loading
+34 −26
Original line number Diff line number Diff line
@@ -345,11 +345,25 @@ public class BatteryMeterView extends LinearLayout implements DarkReceiver {
        }
    }

    private TextView loadPercentView() {
    private TextView inflatePercentView() {
        return (TextView) LayoutInflater.from(getContext())
                .inflate(R.layout.battery_percentage_view, null);
    }

    private void addPercentView(TextView inflatedPercentView) {
        mBatteryPercentView = inflatedPercentView;

        if (mPercentageStyleId != 0) { // Only set if specified as attribute
            mBatteryPercentView.setTextAppearance(mPercentageStyleId);
        }
        float fontHeight = mBatteryPercentView.getPaint().getFontMetricsInt(null);
        mBatteryPercentView.setLineHeight(TypedValue.COMPLEX_UNIT_PX, fontHeight);
        if (mTextColor != 0) mBatteryPercentView.setTextColor(mTextColor);
        addView(mBatteryPercentView, new LayoutParams(
                LayoutParams.WRAP_CONTENT,
                (int) Math.ceil(fontHeight)));
    }

    /**
     * Updates percent view by removing old one and reinflating if necessary
     */
@@ -388,7 +402,9 @@ public class BatteryMeterView extends LinearLayout implements DarkReceiver {
            mBatteryEstimateFetcher.fetchBatteryTimeRemainingEstimate(
                    (String estimate) -> {
                        if (mBatteryPercentView == null) {
                            mBatteryPercentView = loadPercentView();
                            // Similar to the legacy behavior, inflate and add the view. We will
                            // only use it for the estimate text
                            addPercentView(inflatePercentView());
                        }
                        if (estimate != null && mShowPercentMode == MODE_ESTIMATE) {
                            mEstimateText = estimate;
@@ -401,6 +417,10 @@ public class BatteryMeterView extends LinearLayout implements DarkReceiver {
                        }
                    });
        } else {
            if (mBatteryPercentView != null) {
                mEstimateText = null;
                mBatteryPercentView.setText(null);
            }
            updateContentDescription();
        }
    }
@@ -485,21 +505,18 @@ public class BatteryMeterView extends LinearLayout implements DarkReceiver {
            return;
        }

        if (mUnifiedBattery == null) {
            return;
        }
        if (!mShowPercentAvailable || mUnifiedBattery == null) return;

        boolean shouldShow = mShowPercentMode == MODE_ON || mShowPercentMode == MODE_ESTIMATE;
        if (!mBatteryStateUnknown && !shouldShow && (mShowPercentMode != MODE_OFF)) {
            // Slow case: fall back to the system setting
            // TODO(b/140051051)
        final boolean systemSetting = 0 != whitelistIpcs(() -> Settings.System
            shouldShow = 0 != whitelistIpcs(() -> Settings.System
                    .getIntForUser(getContext().getContentResolver(),
                    SHOW_BATTERY_PERCENT, getContext().getResources().getBoolean(
                    com.android.internal.R.bool.config_defaultBatteryPercentageSetting)
                    ? 1 : 0, UserHandle.USER_CURRENT));

        boolean shouldShow =
                (mShowPercentAvailable && systemSetting && mShowPercentMode != MODE_OFF)
                        || mShowPercentMode == MODE_ON;
        shouldShow = shouldShow && !mBatteryStateUnknown;
        }

        setBatteryDrawableState(
                new BatteryDrawableState(
@@ -534,17 +551,8 @@ public class BatteryMeterView extends LinearLayout implements DarkReceiver {

        if (shouldShow) {
            if (!showing) {
                mBatteryPercentView = loadPercentView();
                if (mPercentageStyleId != 0) { // Only set if specified as attribute
                    mBatteryPercentView.setTextAppearance(mPercentageStyleId);
                }
                float fontHeight = mBatteryPercentView.getPaint().getFontMetricsInt(null);
                mBatteryPercentView.setLineHeight(TypedValue.COMPLEX_UNIT_PX, fontHeight);
                if (mTextColor != 0) mBatteryPercentView.setTextColor(mTextColor);
                addPercentView(inflatePercentView());
                updatePercentText();
                addView(mBatteryPercentView, new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        (int) Math.ceil(fontHeight)));
            }
        } else {
            if (showing) {
+28 −0
Original line number Diff line number Diff line
@@ -192,6 +192,34 @@ class BatteryMeterViewTest : SysuiTestCase() {
        assertThat(mBatteryMeterView.unifiedBatteryState.showPercent).isTrue()
    }

    @Test
    @EnableFlags(FLAG_NEW_STATUS_BAR_ICONS)
    fun modeEstimate_batteryPercentView_isNotNull_flagOn() {
        mBatteryMeterView.onBatteryLevelChanged(15, false)
        mBatteryMeterView.setPercentShowMode(BatteryMeterView.MODE_ESTIMATE)
        mBatteryMeterView.setBatteryEstimateFetcher(Fetcher())

        mBatteryMeterView.updatePercentText()

        // New battery icon only uses the percent view for the estimate text
        assertThat(mBatteryMeterView.batteryPercentView).isNotNull()
        // Make sure that it was added to the view hierarchy
        assertThat(mBatteryMeterView.batteryPercentView.parent).isNotNull()
    }

    @Test
    @EnableFlags(FLAG_NEW_STATUS_BAR_ICONS)
    fun modePercent_batteryPercentView_isNull_flagOn() {
        mBatteryMeterView.onBatteryLevelChanged(15, false)
        mBatteryMeterView.setPercentShowMode(BatteryMeterView.MODE_ON)
        mBatteryMeterView.setBatteryEstimateFetcher(Fetcher())

        mBatteryMeterView.updatePercentText()

        // New battery icon only uses the percent view for the estimate text
        assertThat(mBatteryMeterView.batteryPercentView).isNull()
    }

    @Test
    fun contentDescription_manyUpdates_alwaysUpdated() {
        // BatteryDefender