Merge "Add TtsSpan for formatted time." into oc-dev

This commit is contained in:
Lei Yu
2017-04-24 17:01:58 +00:00
committed by Android (Google) Code Review
13 changed files with 142 additions and 63 deletions

View File

@@ -81,6 +81,8 @@ import android.support.v7.preference.PreferenceScreen;
import android.telephony.TelephonyManager;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.style.TtsSpan;
@@ -770,8 +772,9 @@ public final class Utils extends com.android.settingslib.Utils {
* @param withSeconds include seconds?
* @return the formatted elapsed time
*/
public static String formatElapsedTime(Context context, double millis, boolean withSeconds) {
StringBuilder sb = new StringBuilder();
public static CharSequence formatElapsedTime(Context context, double millis,
boolean withSeconds) {
SpannableStringBuilder sb = new SpannableStringBuilder();
int seconds = (int) Math.floor(millis / 1000);
if (!withSeconds) {
// Round up.
@@ -812,9 +815,15 @@ public final class Utils extends com.android.settingslib.Utils {
hours, minutes));
} else {
sb.append(context.getString(R.string.battery_history_minutes_no_seconds, minutes));
// Add ttsSpan if it only have minute value, because it will be read as "meters"
TtsSpan ttsSpan = new TtsSpan.MeasureBuilder().setNumber(minutes)
.setUnit("minute").build();
sb.setSpan(ttsSpan, 0, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return sb.toString();
return sb;
}
/**
@@ -1256,4 +1265,5 @@ public final class Utils extends com.android.settingslib.Utils {
return (volume != null) && (volume.getType() == VolumeInfo.TYPE_PRIVATE)
&& volume.isMountedReadable();
}
}

View File

@@ -28,6 +28,7 @@ import android.os.UserManager;
import android.support.annotation.VisibleForTesting;
import android.support.v14.preference.PreferenceFragment;
import android.support.v7.preference.Preference;
import android.text.TextUtils;
import android.view.View;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -82,6 +83,8 @@ public class AdvancedPowerUsageDetail extends PowerUsageBase implements
ApplicationsState mState;
@VisibleForTesting
ApplicationsState.AppEntry mAppEntry;
@VisibleForTesting
BatteryUtils mBatteryUtils;
private Preference mForegroundPreference;
private Preference mBackgroundPreference;
@@ -137,6 +140,7 @@ public class AdvancedPowerUsageDetail extends PowerUsageBase implements
(DevicePolicyManager) activity.getSystemService(Context.DEVICE_POLICY_SERVICE));
mUserManager = (UserManager) activity.getSystemService(Context.USER_SERVICE);
mPackageManager = activity.getPackageManager();
mBatteryUtils = BatteryUtils.getInstance(getContext());
}
@Override

View File

@@ -15,14 +15,13 @@
*/
package com.android.settings.fuelgauge;
import android.annotation.IntDef;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.BatteryStats;
import android.os.SystemClock;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.text.format.DateUtils;
import android.util.Log;
import com.android.internal.os.BatterySipper;
@@ -64,7 +63,8 @@ public class BatteryUtils {
return sInstance;
}
private BatteryUtils(Context context) {
@VisibleForTesting
BatteryUtils(Context context) {
mPackageManager = context.getPackageManager();
mPowerUsageFeatureProvider = FeatureFactory.getFactory(
context).getPowerUsageFeatureProvider(context);

View File

@@ -79,11 +79,15 @@ public class PowerGaugePreference extends TintablePreference {
return mProgress.toString();
}
public void setSubtitle(String subtitle) {
public void setSubtitle(CharSequence subtitle) {
mProgress = subtitle;
notifyChanged();
}
public CharSequence getSubtitle() {
return mProgress;
}
BatteryEntry getInfo() {
return mInfo;
}

View File

@@ -29,6 +29,7 @@ import android.support.annotation.StringRes;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceGroup;
import android.text.TextUtils;
import com.android.internal.logging.nano.MetricsProto;
import com.android.internal.os.BatterySipper;
@@ -67,13 +68,15 @@ public class PowerUsageAdvanced extends PowerUsageBase {
UsageType.APP,
UsageType.UNACCOUNTED,
UsageType.OVERCOUNTED};
@VisibleForTesting
BatteryUtils mBatteryUtils;
private BatteryHistoryPreference mHistPref;
private PreferenceGroup mUsageListGroup;
private PowerUsageFeatureProvider mPowerUsageFeatureProvider;
private PackageManager mPackageManager;
private UserManager mUserManager;
private Map<Integer, PowerUsageData> mBatteryDataMap;
private BatteryUtils mBatteryUtils;
Handler mHandler = new Handler() {
@@ -270,8 +273,10 @@ public class PowerUsageAdvanced extends PowerUsageBase {
return;
}
if (usageData.usageList.size() <= 1) {
usageData.summary = getString(R.string.battery_used_for,
Utils.formatElapsedTime(getContext(), usageData.totalUsageTimeMs, false));
CharSequence timeSequence = Utils.formatElapsedTime(getContext(),
usageData.totalUsageTimeMs, false);
usageData.summary = TextUtils.expandTemplate(getText(R.string.battery_used_for),
timeSequence);
} else {
BatterySipper sipper = findBatterySipperWithMaxBatteryUsage(usageData.usageList);
BatteryEntry batteryEntry = new BatteryEntry(getContext(), mHandler, mUserManager,
@@ -346,7 +351,7 @@ public class PowerUsageAdvanced extends PowerUsageBase {
@StringRes
public int titleResId;
public String summary;
public CharSequence summary;
public double percentage;
public double totalPowerMah;
public long totalUsageTimeMs;

View File

@@ -418,15 +418,16 @@ public class PowerUsageSummary extends PowerUsageBase {
final TypedValue value = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.colorControlNormal, value, true);
final int colorControl = context.getColor(value.resourceId);
final String usedTime = context.getString(R.string.battery_used_for);
final int dischargeAmount = USE_FAKE_DATA ? 5000
: stats != null ? stats.getDischargeAmount(mStatsType) : 0;
final long runningTime = calculateRunningTimeBasedOnStatsType();
updateScreenPreference();
updateLastFullChargePreference(runningTime);
mAppListGroup.setTitle(getString(R.string.power_usage_list_summary,
Utils.formatElapsedTime(context, runningTime, false)));
final CharSequence timeSequence = Utils.formatElapsedTime(context, runningTime, false);
mAppListGroup.setTitle(
TextUtils.expandTemplate(getText(R.string.power_usage_list_summary), timeSequence));
if (averagePower >= MIN_AVERAGE_POWER_THRESHOLD_MILLI_AMP || USE_FAKE_DATA) {
final List<BatterySipper> usageList = getCoalescedUsageList(
@@ -497,7 +498,7 @@ public class PowerUsageSummary extends PowerUsageBase {
sipper.usageTimeMs = mBatteryUtils.getProcessTimeMs(
BatteryUtils.StatusType.FOREGROUND, sipper.uidObj, mStatsType);
}
setUsageSummary(pref, usedTime, sipper.usageTimeMs);
setUsageSummary(pref, sipper.usageTimeMs);
if ((sipper.drainType != DrainType.APP
|| sipper.uidObj.getUid() == Process.ROOT_UID)
&& sipper.drainType != DrainType.USER) {
@@ -534,16 +535,17 @@ public class PowerUsageSummary extends PowerUsageBase {
void updateScreenPreference() {
final BatterySipper sipper = findBatterySipperByType(
mStatsHelper.getUsageList(), DrainType.SCREEN);
final Context context = getContext();
final long usageTimeMs = sipper != null ? sipper.usageTimeMs : 0;
mScreenUsagePref.setSubtitle(Utils.formatElapsedTime(context, usageTimeMs, false));
mScreenUsagePref.setSubtitle(Utils.formatElapsedTime(getContext(), usageTimeMs, false));
}
@VisibleForTesting
void updateLastFullChargePreference(long timeMs) {
mLastFullChargePref.setSubtitle(getString(R.string.power_last_full_charge_summary,
Utils.formatElapsedTime(getContext(), timeMs, false)));
final CharSequence timeSequence = Utils.formatElapsedTime(getContext(), timeMs, false);
mLastFullChargePref.setSubtitle(
TextUtils.expandTemplate(getText(R.string.power_last_full_charge_summary),
timeSequence));
}
@VisibleForTesting
@@ -583,11 +585,13 @@ public class PowerUsageSummary extends PowerUsageBase {
}
@VisibleForTesting
void setUsageSummary(Preference preference, String usedTimePrefix, long usageTimeMs) {
void setUsageSummary(Preference preference, long usageTimeMs) {
// Only show summary when usage time is longer than one minute
if (usageTimeMs >= DateUtils.MINUTE_IN_MILLIS) {
preference.setSummary(String.format(usedTimePrefix,
Utils.formatElapsedTime(getContext(), usageTimeMs, false)));
final CharSequence timeSequence = Utils.formatElapsedTime(getContext(), usageTimeMs,
false);
preference.setSummary(
TextUtils.expandTemplate(getText(R.string.battery_used_for), timeSequence));
}
}