SettingsLib: refactor for extensibility.
Add short version of charging / discharging labels. Extended getBatteryInfo method to take in additional flag for short version labels. Change-Id: I499ce59493f1345c3084d48829ded5117d470e44 (cherry picked from commit e686bf4514d9c1116a4eaa8af285f44613e23b36)
This commit is contained in:
@@ -733,25 +733,44 @@
|
||||
<!-- [CHAR_LIMIT=40] Label for estimated remaining duration of battery charging/discharging -->
|
||||
<string name="power_remaining_duration_only">Approx. <xliff:g id="time">%1$s</xliff:g> left</string>
|
||||
|
||||
<!-- [CHAR_LIMIT=40] Short label for estimated remaining duration of battery charging/discharging -->
|
||||
<string name="power_remaining_duration_only_short"><xliff:g id="time">%1$s</xliff:g> left</string>
|
||||
|
||||
<!-- [CHAR_LIMIT=40] Label for battery level chart when discharging with duration -->
|
||||
<string name="power_discharging_duration"><xliff:g id="level">%1$s</xliff:g>
|
||||
- approx. <xliff:g id="time">%2$s</xliff:g> left</string>
|
||||
|
||||
<!-- [CHAR_LIMIT=40] Label for battery level chart when discharging with duration -->
|
||||
<string name="power_discharging_duration_short"><xliff:g id="level">%1$s</xliff:g>
|
||||
- <xliff:g id="time">%2$s</xliff:g> left</string>
|
||||
|
||||
<!-- [CHAR_LIMIT=40] Label for battery level chart when charging -->
|
||||
<string name="power_charging"><xliff:g id="level">%1$s</xliff:g> -
|
||||
<xliff:g id="state">%2$s</xliff:g></string>
|
||||
<!-- [CHAR_LIMIT=40] Label for battery level chart when charging with duration -->
|
||||
<string name="power_charging_duration"><xliff:g id="level">%1$s</xliff:g> -
|
||||
<xliff:g id="time">%2$s</xliff:g> until full</string>
|
||||
<!-- [CHAR_LIMIT=40] Short label for battery level chart when charging with duration -->
|
||||
<string name="power_charging_duration_short"><xliff:g id="level">%1$s</xliff:g> -
|
||||
<xliff:g id="time">%2$s</xliff:g></string>
|
||||
<!-- [CHAR_LIMIT=40] Label for battery level chart when charging with duration -->
|
||||
<string name="power_charging_duration_ac"><xliff:g id="level">%1$s</xliff:g> -
|
||||
<xliff:g id="time">%2$s</xliff:g> until full on AC</string>
|
||||
<!-- [CHAR_LIMIT=40] Short label for battery level chart when charging with duration -->
|
||||
<string name="power_charging_duration_ac_short"><xliff:g id="level">%1$s</xliff:g> -
|
||||
<xliff:g id="time">%2$s</xliff:g></string>
|
||||
<!-- [CHAR_LIMIT=40] Label for battery level chart when charging with duration -->
|
||||
<string name="power_charging_duration_usb"><xliff:g id="level">%1$s</xliff:g> -
|
||||
<xliff:g id="time">%2$s</xliff:g> until full over USB</string>
|
||||
<!-- [CHAR_LIMIT=40] Short label for battery level chart when charging with duration -->
|
||||
<string name="power_charging_duration_usb_short"><xliff:g id="level">%1$s</xliff:g> -
|
||||
<xliff:g id="time">%2$s</xliff:g></string>
|
||||
<!-- [CHAR_LIMIT=40] Label for battery level chart when charging with duration -->
|
||||
<string name="power_charging_duration_wireless"><xliff:g id="level">%1$s</xliff:g> -
|
||||
<xliff:g id="time">%2$s</xliff:g> until full from wireless</string>
|
||||
<!-- [CHAR_LIMIT=40] Label for battery level chart when charging with duration -->
|
||||
<string name="power_charging_duration_wireless_short"><xliff:g id="level">%1$s</xliff:g> -
|
||||
<xliff:g id="time">%2$s</xliff:g></string>
|
||||
|
||||
<!-- Battery Info screen. Value for a status item. Used for diagnostic info screens, precise translation isn't needed -->
|
||||
<string name="battery_info_status_unknown">Unknown</string>
|
||||
@@ -759,10 +778,16 @@
|
||||
<string name="battery_info_status_charging">Charging</string>
|
||||
<!-- [CHAR_LIMIT=20] Battery use screen. Battery status shown in chart label when charging on AC. -->
|
||||
<string name="battery_info_status_charging_ac">Charging on AC</string>
|
||||
<!-- [CHAR_LIMIT=20] Battery short status label when charing on AC -->
|
||||
<string name="battery_info_status_charging_ac_short">Charging</string>
|
||||
<!-- [CHAR_LIMIT=20] Battery use screen. Battery status shown in chart label when charging over USB. -->
|
||||
<string name="battery_info_status_charging_usb">Charging over USB</string>
|
||||
<!-- [CHAR_LIMIT=20] Battery short status label when charging over USB. -->
|
||||
<string name="battery_info_status_charging_usb_short">Charging</string>
|
||||
<!-- [CHAR_LIMIT=20] Battery use screen. Battery status shown in chart label when charging over a wireless connection. -->
|
||||
<string name="battery_info_status_charging_wireless">Charging wirelessly</string>
|
||||
<!-- [CHAR_LIMIT=20] Battery short status label when charging wirelessly. -->
|
||||
<string name="battery_info_status_charging_wireless_short">Charging</string>
|
||||
<!-- Battery Info screen. Value for a status item. Used for diagnostic info screens, precise translation isn't needed -->
|
||||
<string name="battery_info_status_discharging">Not charging</string>
|
||||
<!-- Battery Info screen. Value for a status item. Used for diagnostic info screens, precise translation isn't needed -->
|
||||
|
||||
@@ -25,6 +25,7 @@ import android.os.BatteryStats.HistoryItem;
|
||||
import android.os.Bundle;
|
||||
import android.os.SystemClock;
|
||||
import android.text.format.Formatter;
|
||||
import android.util.Log;
|
||||
import android.util.SparseIntArray;
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settingslib.graph.UsageView;
|
||||
@@ -95,6 +96,11 @@ public class BatteryInfo {
|
||||
}
|
||||
|
||||
public static void getBatteryInfo(final Context context, final Callback callback) {
|
||||
BatteryInfo.getBatteryInfo(context, callback, false);
|
||||
}
|
||||
|
||||
public static void getBatteryInfo(final Context context, final Callback callback,
|
||||
boolean shortString) {
|
||||
new AsyncTask<Void, Void, BatteryStats>() {
|
||||
@Override
|
||||
protected BatteryStats doInBackground(Void... params) {
|
||||
@@ -109,14 +115,20 @@ public class BatteryInfo {
|
||||
Intent batteryBroadcast = context.registerReceiver(null,
|
||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
BatteryInfo batteryInfo = BatteryInfo.getBatteryInfo(context,
|
||||
batteryBroadcast, batteryStats, elapsedRealtimeUs);
|
||||
batteryBroadcast, batteryStats, elapsedRealtimeUs, shortString);
|
||||
callback.onBatteryInfoLoaded(batteryInfo);
|
||||
}
|
||||
}.execute();
|
||||
}
|
||||
|
||||
public static BatteryInfo getBatteryInfo(Context context, Intent batteryBroadcast,
|
||||
BatteryStats stats, long elapsedRealtimeUs) {
|
||||
BatteryStats stats, long elapsedRealtimeUs) {
|
||||
return BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats, elapsedRealtimeUs,
|
||||
false);
|
||||
}
|
||||
|
||||
public static BatteryInfo getBatteryInfo(Context context, Intent batteryBroadcast,
|
||||
BatteryStats stats, long elapsedRealtimeUs, boolean shortString) {
|
||||
BatteryInfo info = new BatteryInfo();
|
||||
info.mStats = stats;
|
||||
info.mBatteryLevel = Utils.getBatteryLevel(batteryBroadcast);
|
||||
@@ -129,9 +141,13 @@ public class BatteryInfo {
|
||||
info.remainingTimeUs = drainTime;
|
||||
String timeString = Formatter.formatShortElapsedTime(context,
|
||||
drainTime / 1000);
|
||||
info.remainingLabel = resources.getString(R.string.power_remaining_duration_only,
|
||||
info.remainingLabel = resources.getString(
|
||||
shortString ? R.string.power_remaining_duration_only_short
|
||||
: R.string.power_remaining_duration_only,
|
||||
timeString);
|
||||
info.mChargeLabelString = resources.getString(R.string.power_discharging_duration,
|
||||
info.mChargeLabelString = resources.getString(
|
||||
shortString ? R.string.power_discharging_duration_short
|
||||
: R.string.power_discharging_duration,
|
||||
info.batteryPercentString, timeString);
|
||||
} else {
|
||||
info.remainingLabel = null;
|
||||
@@ -140,7 +156,7 @@ public class BatteryInfo {
|
||||
} else {
|
||||
final long chargeTime = stats.computeChargeTimeRemaining(elapsedRealtimeUs);
|
||||
final String statusLabel = Utils.getBatteryStatus(
|
||||
resources, batteryBroadcast);
|
||||
resources, batteryBroadcast, shortString);
|
||||
final int status = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_STATUS,
|
||||
BatteryManager.BATTERY_STATUS_UNKNOWN);
|
||||
if (chargeTime > 0 && status != BatteryManager.BATTERY_STATUS_FULL) {
|
||||
@@ -151,13 +167,17 @@ public class BatteryInfo {
|
||||
int plugType = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
|
||||
int resId;
|
||||
if (plugType == BatteryManager.BATTERY_PLUGGED_AC) {
|
||||
resId = R.string.power_charging_duration_ac;
|
||||
resId = shortString ? R.string.power_charging_duration_ac_short
|
||||
: R.string.power_charging_duration_ac;
|
||||
} else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
|
||||
resId = R.string.power_charging_duration_usb;
|
||||
resId = shortString ? R.string.power_charging_duration_usb_short
|
||||
: R.string.power_charging_duration_usb;
|
||||
} else if (plugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
|
||||
resId = R.string.power_charging_duration_wireless;
|
||||
resId = shortString ? R.string.power_charging_duration_wireless_short
|
||||
: R.string.power_charging_duration_wireless;
|
||||
} else {
|
||||
resId = R.string.power_charging_duration;
|
||||
resId = shortString ? R.string.power_charging_duration_short
|
||||
: R.string.power_charging_duration;
|
||||
}
|
||||
info.remainingLabel = resources.getString(R.string.power_remaining_duration_only,
|
||||
timeString);
|
||||
|
||||
@@ -112,20 +112,26 @@ public class Utils {
|
||||
}
|
||||
|
||||
public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) {
|
||||
final Intent intent = batteryChangedIntent;
|
||||
return Utils.getBatteryStatus(res, batteryChangedIntent, false);
|
||||
}
|
||||
|
||||
int plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
|
||||
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
|
||||
public static String getBatteryStatus(Resources res, Intent batteryChangedIntent,
|
||||
boolean shortString) {
|
||||
int plugType = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
|
||||
int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS,
|
||||
BatteryManager.BATTERY_STATUS_UNKNOWN);
|
||||
String statusString;
|
||||
if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
|
||||
int resId;
|
||||
if (plugType == BatteryManager.BATTERY_PLUGGED_AC) {
|
||||
resId = R.string.battery_info_status_charging_ac;
|
||||
resId = shortString ? R.string.battery_info_status_charging_ac_short
|
||||
: R.string.battery_info_status_charging_ac;
|
||||
} else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
|
||||
resId = R.string.battery_info_status_charging_usb;
|
||||
resId = shortString ? R.string.battery_info_status_charging_usb_short
|
||||
: R.string.battery_info_status_charging_usb;
|
||||
} else if (plugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
|
||||
resId = R.string.battery_info_status_charging_wireless;
|
||||
resId = shortString ? R.string.battery_info_status_charging_wireless_short
|
||||
: R.string.battery_info_status_charging_wireless;
|
||||
} else {
|
||||
resId = R.string.battery_info_status_charging;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user