Merge "Update UsageProgressBarPref icon when device is charging or low battery" into sc-dev am: 1d5d101aa6

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/13805543

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Id96b38dce8a4dc895bbd2263df16c17294bf0fa4
This commit is contained in:
Wesley Wang
2021-03-11 08:07:53 +00:00
committed by Automerger Merge Worker
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.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;
}
}