Update UsageProgressBarPref icon when device is charging or low battery

- Add new icon for battery low state
 - Update UsageProgressBarPref icon when charging or low battery
 Screenshots:
   https://screenshot.googleplex.com/9HBvV6t6dVsG8eD.png
   https://screenshot.googleplex.com/6TsRu6BTN338FvT.png
   https://screenshot.googleplex.com/nGE29cfsmgKuuaP.png
   https://screenshot.googleplex.com/85NWXSg2PevXAsy.png

Bug: 179237746
Test: make RunSettingsRoboTests -j40
Change-Id: I057874bbf45594c90466cf346b054bf033815dac
This commit is contained in:
Wesley.CW Wang
2021-03-09 17:46:45 +08:00
committed by Wesley Wang
parent 53d7495162
commit e940dcb448
2 changed files with 54 additions and 5 deletions

View File

@@ -0,0 +1,28 @@
<!--
Copyright (C) 2021 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
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?android:attr/colorAccent"
android:pathData="M12,19m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0"/>
<path
android:fillColor="?android:attr/colorAccent"
android:pathData="M10,3h4v12h-4z"/>
</vector>

View File

@@ -21,10 +21,12 @@ import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.graphics.ColorFilter;
import android.icu.text.NumberFormat; import android.icu.text.NumberFormat;
import android.os.BatteryManager; import android.os.BatteryManager;
import android.os.PowerManager; import android.os.PowerManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.widget.ImageView;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceFragmentCompat;
@@ -60,6 +62,7 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
private Activity mActivity; private Activity mActivity;
private PreferenceFragmentCompat mHost; private PreferenceFragmentCompat mHost;
private Lifecycle mLifecycle; private Lifecycle mLifecycle;
private ColorFilter mAccentColorFilter;
private final PowerManager mPowerManager; private final PowerManager mPowerManager;
public BatteryHeaderPreferenceController(Context context, String key) { public BatteryHeaderPreferenceController(Context context, String key) {
@@ -85,6 +88,9 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
mBatteryUsageProgressBarPref = screen.findPreference(getPreferenceKey()); mBatteryUsageProgressBarPref = screen.findPreference(getPreferenceKey());
mAccentColorFilter = com.android.settings.Utils.getAlphaInvariantColorFilterForColor(
com.android.settings.Utils.getColorAttrDefaultColor(
mContext, android.R.attr.colorAccent));
if (com.android.settings.Utils.isBatteryPresent(mContext)) { if (com.android.settings.Utils.isBatteryPresent(mContext)) {
quickUpdateHeaderPreference(); quickUpdateHeaderPreference();
@@ -116,13 +122,12 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
} }
public void updateHeaderPreference(BatteryInfo info) { public void updateHeaderPreference(BatteryInfo info) {
//TODO(b/179237746): Make progress bar widget support battery state icon
mBatteryUsageProgressBarPref.setUsageSummary( mBatteryUsageProgressBarPref.setUsageSummary(
formatBatteryPercentageText(info.batteryLevel)); formatBatteryPercentageText(info.batteryLevel));
mBatteryUsageProgressBarPref.setTotalSummary(generateLabel(info)); mBatteryUsageProgressBarPref.setTotalSummary(generateLabel(info));
mBatteryUsageProgressBarPref.setPercent(info.batteryLevel, BATTERY_MAX_LEVEL); mBatteryUsageProgressBarPref.setPercent(info.batteryLevel, BATTERY_MAX_LEVEL);
mBatteryUsageProgressBarPref.setCustomContent(
getBatteryIcon(!info.discharging, info.batteryLevel));
} }
/** /**
@@ -139,14 +144,30 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
final boolean discharging = final boolean discharging =
batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0; batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0;
//TODO(b/179237746): Make progress bar widget support battery state icon
mBatteryUsageProgressBarPref.setUsageSummary(formatBatteryPercentageText(batteryLevel)); mBatteryUsageProgressBarPref.setUsageSummary(formatBatteryPercentageText(batteryLevel));
mBatteryUsageProgressBarPref.setPercent(batteryLevel, BATTERY_MAX_LEVEL); mBatteryUsageProgressBarPref.setPercent(batteryLevel, BATTERY_MAX_LEVEL);
mBatteryUsageProgressBarPref.setCustomContent(getBatteryIcon(!discharging, batteryLevel));
} }
private CharSequence formatBatteryPercentageText(int batteryLevel) { private CharSequence formatBatteryPercentageText(int batteryLevel) {
return TextUtils.expandTemplate(mContext.getText(R.string.battery_header_title_alternate), return TextUtils.expandTemplate(mContext.getText(R.string.battery_header_title_alternate),
NumberFormat.getIntegerInstance().format(batteryLevel)); NumberFormat.getIntegerInstance().format(batteryLevel));
} }
//TODO(b/179237746): Update the battery icon after receiving final asset
private ImageView getBatteryIcon(boolean isCharging, int batteryLevel) {
ImageView batteryIcon = new ImageView(mContext);
if (batteryLevel <= (mContext.getResources().getInteger(
com.android.internal.R.integer.config_lowBatteryWarningLevel))) {
batteryIcon.setImageResource(R.drawable.ic_battery_low);
} else if (isCharging) {
batteryIcon.setColorFilter(mAccentColorFilter);
batteryIcon.setImageResource(R.drawable.ic_battery_charging_full);
} else {
batteryIcon = null;
}
return batteryIcon;
}
} }