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:
committed by
Wesley Wang
parent
53d7495162
commit
e940dcb448
@@ -21,10 +21,12 @@ import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.ColorFilter;
|
||||
import android.icu.text.NumberFormat;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.PowerManager;
|
||||
import android.text.TextUtils;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
@@ -60,6 +62,7 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
|
||||
private Activity mActivity;
|
||||
private PreferenceFragmentCompat mHost;
|
||||
private Lifecycle mLifecycle;
|
||||
private ColorFilter mAccentColorFilter;
|
||||
private final PowerManager mPowerManager;
|
||||
|
||||
public BatteryHeaderPreferenceController(Context context, String key) {
|
||||
@@ -85,6 +88,9 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
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)) {
|
||||
quickUpdateHeaderPreference();
|
||||
@@ -116,13 +122,12 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
|
||||
}
|
||||
|
||||
public void updateHeaderPreference(BatteryInfo info) {
|
||||
|
||||
//TODO(b/179237746): Make progress bar widget support battery state icon
|
||||
|
||||
mBatteryUsageProgressBarPref.setUsageSummary(
|
||||
formatBatteryPercentageText(info.batteryLevel));
|
||||
mBatteryUsageProgressBarPref.setTotalSummary(generateLabel(info));
|
||||
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 =
|
||||
batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0;
|
||||
|
||||
//TODO(b/179237746): Make progress bar widget support battery state icon
|
||||
|
||||
mBatteryUsageProgressBarPref.setUsageSummary(formatBatteryPercentageText(batteryLevel));
|
||||
mBatteryUsageProgressBarPref.setPercent(batteryLevel, BATTERY_MAX_LEVEL);
|
||||
mBatteryUsageProgressBarPref.setCustomContent(getBatteryIcon(!discharging, batteryLevel));
|
||||
}
|
||||
|
||||
private CharSequence formatBatteryPercentageText(int batteryLevel) {
|
||||
return TextUtils.expandTemplate(mContext.getText(R.string.battery_header_title_alternate),
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user