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

Commit 178eb0bd authored by Zaiyue Xue's avatar Zaiyue Xue
Browse files

Split battery header text to a separated preference

This is a no-op refactor. This change makes the Catalyst migration of battery level bar easy.

Bug: 372774754
Test: manual
Flag: EXEMPT bug fix
Change-Id: I6e64e6d9b34aeca584f4d4e951c58c3e1b361f9d
parent 0a066413
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
<!--
  ~ Copyright (C) 2024 The Android Open Source Project
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginStart="?android:attr/listPreferredItemPaddingStart"
    android:layout_marginEnd="?android:attr/listPreferredItemPaddingEnd"
    android:paddingBottom="16dp">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:textAppearance="@*android:style/TextAppearance.DeviceDefault.Body1"
        android:textSize="14sp"/>
</LinearLayout>
 No newline at end of file
+6 −0
Original line number Diff line number Diff line
@@ -25,8 +25,14 @@
        android:key="battery_header"
        android:title="@string/summary_placeholder"
        android:selectable="false"
        android:paddingBottom="0px"
        settings:controller="com.android.settings.fuelgauge.BatteryHeaderPreferenceController" />

    <com.android.settings.fuelgauge.BatteryHeaderTextPreference
        android:key="battery_header_text"
        android:selectable="false"
        settings:controller="com.android.settings.fuelgauge.BatteryHeaderTextPreferenceController" />

    <com.android.settingslib.widget.LayoutPreference
        android:key="battery_help_message"
        android:layout="@layout/preference_battery_error"
+6 −112
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
@@ -17,51 +18,32 @@ package com.android.settings.fuelgauge;

import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.PowerManager;
import android.util.Log;

import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.Utils;
import com.android.settingslib.widget.UsageProgressBarPreference;

/** Controller that update the battery header view */
public class BatteryHeaderPreferenceController extends BasePreferenceController
        implements PreferenceControllerMixin, BatteryPreferenceController {
        implements PreferenceControllerMixin {
    private static final String TAG = "BatteryHeaderPreferenceController";

    @VisibleForTesting static final String KEY_BATTERY_HEADER = "battery_header";
    private static final int BATTERY_MAX_LEVEL = 100;

    @VisibleForTesting BatteryStatusFeatureProvider mBatteryStatusFeatureProvider;
    @VisibleForTesting UsageProgressBarPreference mBatteryUsageProgressBarPref;

    private final PowerManager mPowerManager;
    private final BatterySettingsFeatureProvider mBatterySettingsFeatureProvider;

    private BatteryTip mBatteryTip;

    public BatteryHeaderPreferenceController(Context context, String key) {
        super(context, key);
        mPowerManager = context.getSystemService(PowerManager.class);
        mBatteryStatusFeatureProvider =
                FeatureFactory.getFeatureFactory().getBatteryStatusFeatureProvider();
        mBatterySettingsFeatureProvider =
                FeatureFactory.getFeatureFactory().getBatterySettingsFeatureProvider();
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mBatteryUsageProgressBarPref = screen.findPreference(getPreferenceKey());
        // Set up empty space text first to prevent layout flaky before info loaded.
        // Hide the bottom summary from the progress bar.
        mBatteryUsageProgressBarPref.setBottomSummary("");

        if (com.android.settings.Utils.isBatteryPresent(mContext)) {
@@ -76,104 +58,16 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
        return AVAILABLE_UNSEARCHABLE;
    }

    private CharSequence generateLabel(BatteryInfo info) {
        if (Utils.containsIncompatibleChargers(mContext, TAG)) {
            return mContext.getString(
                    com.android.settingslib.R.string.battery_info_status_not_charging);
        }
        if (BatteryUtils.isBatteryDefenderOn(info)
                || FeatureFactory.getFeatureFactory()
                .getPowerUsageFeatureProvider()
                .isExtraDefend()) {
            return mContext.getString(
                    com.android.settingslib.R.string.battery_info_status_charging_on_hold);
        }
        if (info.remainingLabel != null
                && mBatterySettingsFeatureProvider.isChargingOptimizationMode(mContext)) {
            return info.remainingLabel;
        }
        if (info.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
            return info.statusLabel;
        }
        if (info.pluggedStatus == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
            final CharSequence wirelessChargingLabel =
                    mBatterySettingsFeatureProvider.getWirelessChargingLabel(mContext, info);
            if (wirelessChargingLabel != null) {
                mBatteryUsageProgressBarPref.setBottomSummaryContentDescription(
                        mBatterySettingsFeatureProvider
                                .getWirelessChargingContentDescription(mContext, info));
                return wirelessChargingLabel;
            }
        }
        if (info.remainingLabel == null) {
            return info.statusLabel;
        }
        if (info.statusLabel != null && !info.discharging) {
            // Charging state
            if (com.android.settingslib.fuelgauge.BatteryUtils.isChargingStringV2Enabled()) {
                return info.isFastCharging
                        ? mContext.getString(
                                R.string.battery_state_and_duration,
                                info.statusLabel,
                                info.remainingLabel)
                        : info.remainingLabel;
            }
            return mContext.getString(
                    R.string.battery_state_and_duration, info.statusLabel, info.remainingLabel);
        } else if (mPowerManager.isPowerSaveMode()) {
            // Power save mode is on
            final String powerSaverOn =
                    mContext.getString(R.string.battery_tip_early_heads_up_done_title);
            return mContext.getString(
                    R.string.battery_state_and_duration, powerSaverOn, info.remainingLabel);
        } else if (mBatteryTip != null && mBatteryTip.getType() == BatteryTip.TipType.LOW_BATTERY) {
            // Low battery state
            final String lowBattery = mContext.getString(R.string.low_battery_summary);
            return mContext.getString(
                    R.string.battery_state_and_duration, lowBattery, info.remainingLabel);
        } else {
            // Discharging state
            return info.remainingLabel;
        }
    }

    public void updateHeaderPreference(BatteryInfo info) {
        if (!mBatteryStatusFeatureProvider.triggerBatteryStatusUpdate(this, info)) {
            mBatteryUsageProgressBarPref.setBottomSummary(generateLabel(info));
        }

        mBatteryUsageProgressBarPref.setUsageSummary(
                formatBatteryPercentageText(info.batteryLevel));
        mBatteryUsageProgressBarPref.setPercent(info.batteryLevel, BATTERY_MAX_LEVEL);
    }

    /** Callback which receives text for the summary line. */
    public void updateBatteryStatus(String label, BatteryInfo info) {
        final CharSequence summary = label != null ? label : generateLabel(info);
        mBatteryUsageProgressBarPref.setBottomSummary(summary);
        Log.d(TAG, "updateBatteryStatus: " + label + " summary: " + summary);
    }

    /** Updates {@link UsageProgressBarPreference} information. */
    public void quickUpdateHeaderPreference() {
        Intent batteryBroadcast =
                com.android.settingslib.fuelgauge.BatteryUtils.getBatteryIntent(mContext);
        final int batteryLevel = Utils.getBatteryLevel(batteryBroadcast);
        final boolean discharging =
                batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0;

        mBatteryUsageProgressBarPref.setUsageSummary(formatBatteryPercentageText(batteryLevel));
        mBatteryUsageProgressBarPref.setPercent(batteryLevel, BATTERY_MAX_LEVEL);
    }

    /** Update summary when battery tips changed. */
    public void updateHeaderByBatteryTips(BatteryTip batteryTip, BatteryInfo batteryInfo) {
        mBatteryTip = batteryTip;

        if (mBatteryTip != null && batteryInfo != null) {
            updateHeaderPreference(batteryInfo);
        }
    }

    private CharSequence formatBatteryPercentageText(int batteryLevel) {
        return com.android.settings.Utils.formatPercentage(batteryLevel);
    }
+60 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.fuelgauge;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;

import com.android.settings.R;

/** A preference for battery header text. */
public class BatteryHeaderTextPreference extends Preference {
    private static final String TAG = "BatteryHeaderTextPreference";

    @Nullable private CharSequence mText;
    @Nullable private CharSequence mContentDescription;

    public BatteryHeaderTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayoutResource(R.layout.preference_battery_header_text);
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder view) {
        final TextView textView = (TextView) view.findViewById(R.id.text);
        textView.setText(mText);
        if (!TextUtils.isEmpty(mContentDescription)) {
            textView.setContentDescription(mContentDescription);
        }
    }

    void setText(@Nullable CharSequence text) {
        mText = text;
        notifyChanged();
    }

    void setContentDescription(@Nullable CharSequence contentDescription) {
        mContentDescription = contentDescription;
        notifyChanged();
    }
}
+31 −48
Original line number Diff line number Diff line
@@ -17,11 +17,12 @@
package com.android.settings.fuelgauge;

import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.PowerManager;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceScreen;

@@ -31,23 +32,20 @@ import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.Utils;
import com.android.settingslib.widget.UsageProgressBarPreference;

/** Controller that update the battery header view */
public class BatteryHeaderTextPreferenceController extends BasePreferenceController
        implements PreferenceControllerMixin, BatteryPreferenceController {
    private static final String TAG = "BatteryHeaderPreferenceController";

    @VisibleForTesting static final String KEY_BATTERY_HEADER = "battery_header";
    private static final int BATTERY_MAX_LEVEL = 100;

    @VisibleForTesting BatteryStatusFeatureProvider mBatteryStatusFeatureProvider;
    @VisibleForTesting UsageProgressBarPreference mBatteryUsageProgressBarPref;
    private static final String TAG = "BatteryHeaderTextPreferenceController";

    private final PowerManager mPowerManager;
    private final BatterySettingsFeatureProvider mBatterySettingsFeatureProvider;

    private BatteryTip mBatteryTip;
    @Nullable private BatteryTip mBatteryTip;

    @VisibleForTesting BatteryStatusFeatureProvider mBatteryStatusFeatureProvider;

    @Nullable @VisibleForTesting BatteryHeaderTextPreference mBatteryHeaderTextPreference;

    public BatteryHeaderTextPreferenceController(Context context, String key) {
        super(context, key);
@@ -61,14 +59,11 @@ public class BatteryHeaderTextPreferenceController extends BasePreferenceControl
    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mBatteryUsageProgressBarPref = screen.findPreference(getPreferenceKey());
        // Set up empty space text first to prevent layout flaky before info loaded.
        mBatteryUsageProgressBarPref.setBottomSummary(" ");
        mBatteryHeaderTextPreference = screen.findPreference(getPreferenceKey());

        if (com.android.settings.Utils.isBatteryPresent(mContext)) {
            quickUpdateHeaderPreference();
        } else {
            mBatteryUsageProgressBarPref.setVisible(false);
        if (mBatteryHeaderTextPreference != null
                && !com.android.settings.Utils.isBatteryPresent(mContext)) {
            mBatteryHeaderTextPreference.setVisible(false);
        }
    }

@@ -77,7 +72,8 @@ public class BatteryHeaderTextPreferenceController extends BasePreferenceControl
        return AVAILABLE_UNSEARCHABLE;
    }

    private CharSequence generateLabel(BatteryInfo info) {
    @NonNull
    private CharSequence generateLabel(@NonNull BatteryInfo info) {
        if (Utils.containsIncompatibleChargers(mContext, TAG)) {
            return mContext.getString(
                    com.android.settingslib.R.string.battery_info_status_not_charging);
@@ -99,8 +95,8 @@ public class BatteryHeaderTextPreferenceController extends BasePreferenceControl
        if (info.pluggedStatus == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
            final CharSequence wirelessChargingLabel =
                    mBatterySettingsFeatureProvider.getWirelessChargingLabel(mContext, info);
            if (wirelessChargingLabel != null) {
                mBatteryUsageProgressBarPref.setBottomSummaryContentDescription(
            if (mBatteryHeaderTextPreference != null && wirelessChargingLabel != null) {
                mBatteryHeaderTextPreference.setContentDescription(
                        mBatterySettingsFeatureProvider
                                .getWirelessChargingContentDescription(mContext, info));
                return wirelessChargingLabel;
@@ -138,46 +134,33 @@ public class BatteryHeaderTextPreferenceController extends BasePreferenceControl
        }
    }

    /** Updates the battery header. */
    public void updateHeaderPreference(BatteryInfo info) {
        if (!mBatteryStatusFeatureProvider.triggerBatteryStatusUpdate(this, info)) {
            mBatteryUsageProgressBarPref.setBottomSummary(generateLabel(info));
    /** Updates the battery header text with the given BatteryInfo. */
    public void updateHeaderPreference(@NonNull BatteryInfo info) {
        if (mBatteryHeaderTextPreference != null
                && !mBatteryStatusFeatureProvider.triggerBatteryStatusUpdate(this, info)) {
            mBatteryHeaderTextPreference.setText(generateLabel(info));
        }

        mBatteryUsageProgressBarPref.setUsageSummary(
                formatBatteryPercentageText(info.batteryLevel));
        mBatteryUsageProgressBarPref.setPercent(info.batteryLevel, BATTERY_MAX_LEVEL);
    }

    /** Callback which receives text for the summary line. */
    /** Callback which updates the battery header text with the given label. */
    @Override
    public void updateBatteryStatus(String label, BatteryInfo info) {
        final CharSequence summary = label != null ? label : generateLabel(info);
        mBatteryUsageProgressBarPref.setBottomSummary(summary);
        Log.d(TAG, "updateBatteryStatus: " + label + " summary: " + summary);
        if (mBatteryHeaderTextPreference == null) {
            return;
        }

    /** Updates the battery header quickly. */
    public void quickUpdateHeaderPreference() {
        Intent batteryBroadcast =
                com.android.settingslib.fuelgauge.BatteryUtils.getBatteryIntent(mContext);
        final int batteryLevel = Utils.getBatteryLevel(batteryBroadcast);
        final boolean discharging =
                batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0;

        mBatteryUsageProgressBarPref.setUsageSummary(formatBatteryPercentageText(batteryLevel));
        mBatteryUsageProgressBarPref.setPercent(batteryLevel, BATTERY_MAX_LEVEL);
        final CharSequence summary = label != null ? label : generateLabel(info);
        mBatteryHeaderTextPreference.setText(summary);
        Log.d(TAG, "updateBatteryStatus: " + label + " summary: " + summary);
    }

    /** Update summary when battery tips changed. */
    public void updateHeaderByBatteryTips(BatteryTip batteryTip, BatteryInfo batteryInfo) {
    /** Update summary when battery tips are changed. */
    public void updateHeaderByBatteryTips(
            @Nullable BatteryTip batteryTip, @NonNull BatteryInfo batteryInfo) {
        mBatteryTip = batteryTip;

        if (mBatteryTip != null && batteryInfo != null) {
            updateHeaderPreference(batteryInfo);
        }
    }

    private CharSequence formatBatteryPercentageText(int batteryLevel) {
        return com.android.settings.Utils.formatPercentage(batteryLevel);
    }
}
Loading