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

Commit ad99e2ef authored by Mill Chen's avatar Mill Chen
Browse files

Update related UI if battery is not present

This change is to update the related UI in the battery page if the
battery is not present. This includes the following updates:
1. Update the summary of battery tile in the Settings homepage
2. Replace the battery level with "Unknown"
3. Replace the summary with help message in the battery page
4. Remove the battery meter icon

Bug: 171368508
Test: verify on an issue device
Change-Id: I892e0d137143160a0bce0c11ce9265120ebb8fd4
parent d682b2cc
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
    style="@style/EntityHeader">

    <LinearLayout
        android:id="@+id/battery_info_layout"
        android:layout_width="170dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="72dp"
+7 −0
Original line number Diff line number Diff line
@@ -5778,6 +5778,11 @@
    <!-- Title to display the battery percentage. [CHAR LIMIT=24] -->
    <string name="battery_header_title_alternate"><xliff:g id="number" example="88">^1</xliff:g><small> <font size="20"><xliff:g id="unit" example="%">%</xliff:g></font></small></string>
    <!-- Summary for top level battery tile if battery is not present. [CHAR LIMIT=NONE] -->
    <string name="battery_missing_message">Problem reading your battery meter</string>
    <!-- Summary to battery page if battery is not present. [CHAR LIMIT=NONE] -->
    <string name="battery_missing_help_message">Problem reading your battery meter. Tap to <annotation id="url">learn more</annotation></string>
    <!-- Title for force stop dialog [CHAR LIMIT=30] -->
    <string name="dialog_stop_title">Stop app?</string>
    <!-- Message body for force stop dialog [CHAR LIMIT=NONE] -->
@@ -7290,6 +7295,8 @@
    <string name="help_uri_sim_combination_warning" translatable="false"></string>
    <!-- url for the NFC and payment settings page -->
    <string name="help_uri_nfc_and_payment_settings" translatable="false"></string>
    <!-- url for battery page if battery is not present -->
    <string name="help_url_battery_missing" translatable="false"></string>
    <!-- User account title [CHAR LIMIT=30] -->
    <string name="user_account_title">Account for content</string>
+9 −0
Original line number Diff line number Diff line
@@ -267,6 +267,15 @@ public final class Utils extends com.android.settingslib.Utils {
        return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
    }

    /**
     * Return true if battery is present.
     */
    public static boolean isBatteryPresent(Context context) {
        Intent batteryBroadcast = context.registerReceiver(null /* receiver */,
                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        return isBatteryPresent(batteryBroadcast);
    }

    public static String getBatteryPercentage(Intent batteryChangedIntent) {
        return formatPercentage(getBatteryLevel(batteryChangedIntent));
    }
+17 −0
Original line number Diff line number Diff line
@@ -21,9 +21,11 @@ import android.content.Context;
import android.provider.Settings;

import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;

import com.android.internal.R;
import com.android.settings.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;

@@ -34,12 +36,27 @@ import com.android.settings.core.PreferenceControllerMixin;
public class BatteryPercentagePreferenceController extends BasePreferenceController implements
        PreferenceControllerMixin, Preference.OnPreferenceChangeListener {

    private Preference mPreference;

    public BatteryPercentagePreferenceController(Context context, String preferenceKey) {
        super(context, preferenceKey);
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mPreference = screen.findPreference(getPreferenceKey());
        if (!Utils.isBatteryPresent(mContext)) {
            // Disable battery percentage
            onPreferenceChange(mPreference, false /* newValue */);
        }
    }

    @Override
    public int getAvailabilityStatus() {
        if (!Utils.isBatteryPresent(mContext)) {
            return CONDITIONALLY_UNAVAILABLE;
        }
        return mContext.getResources().getBoolean(
                R.bool.config_battery_percentage_setting_available) ? AVAILABLE
                : UNSUPPORTED_ON_DEVICE;
+10 −4
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.PowerManager;
import android.util.Log;

import androidx.annotation.IntDef;
import androidx.annotation.VisibleForTesting;
@@ -41,6 +42,7 @@ import java.lang.annotation.RetentionPolicy;
 */
public class BatteryBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "BatteryBroadcastRcvr";
    /**
     * Callback when the following has been changed:
     *
@@ -56,12 +58,14 @@ public class BatteryBroadcastReceiver extends BroadcastReceiver {
    @IntDef({BatteryUpdateType.MANUAL,
            BatteryUpdateType.BATTERY_LEVEL,
            BatteryUpdateType.BATTERY_SAVER,
            BatteryUpdateType.BATTERY_STATUS})
            BatteryUpdateType.BATTERY_STATUS,
            BatteryUpdateType.BATTERY_NOT_PRESENT})
    public @interface BatteryUpdateType {
        int MANUAL = 0;
        int BATTERY_LEVEL = 1;
        int BATTERY_SAVER = 2;
        int BATTERY_STATUS = 3;
        int BATTERY_NOT_PRESENT = 4;
    }

    @VisibleForTesting
@@ -101,9 +105,11 @@ public class BatteryBroadcastReceiver extends BroadcastReceiver {
        if (intent != null && mBatteryListener != null) {
            if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
                final String batteryLevel = Utils.getBatteryPercentage(intent);
                final String batteryStatus = Utils.getBatteryStatus(
                        mContext, intent);
                if (forceUpdate) {
                final String batteryStatus = Utils.getBatteryStatus(mContext, intent);
                if (!Utils.isBatteryPresent(intent)) {
                    Log.w(TAG, "Problem reading the battery meter.");
                    mBatteryListener.onBatteryChanged(BatteryUpdateType.BATTERY_NOT_PRESENT);
                } else if (forceUpdate) {
                    mBatteryListener.onBatteryChanged(BatteryUpdateType.MANUAL);
                } else if(!batteryLevel.equals(mBatteryLevel)) {
                    mBatteryListener.onBatteryChanged(BatteryUpdateType.BATTERY_LEVEL);
Loading