Start cleaning up percentage formatting in Settings.

This fixes some of the percentage formatting issues, but there are
still about ten strings with hard-coded %%s in them.

Bug: 15476051
Change-Id: I668b6b16e598425f6006f6de0005c980f613f5b2
This commit is contained in:
Elliott Hughes
2014-09-02 17:10:14 -07:00
parent b58b512704
commit 7253df35a0
9 changed files with 95 additions and 114 deletions

View File

@@ -3804,8 +3804,6 @@
<string name="power_discharge_remaining"><xliff:g id="remain">%1$s</xliff:g> remaining</string> <string name="power_discharge_remaining"><xliff:g id="remain">%1$s</xliff:g> remaining</string>
<!-- Display time remaining until battery is charged [CHAR_LIMIT=60] --> <!-- Display time remaining until battery is charged [CHAR_LIMIT=60] -->
<string name="power_charge_remaining"><xliff:g id="until_charged">%1$s</xliff:g> to charge</string> <string name="power_charge_remaining"><xliff:g id="until_charged">%1$s</xliff:g> to charge</string>
<!-- [CHAR_LIMIT=40] Label for battery level chart when discharging -->
<string name="power_discharging"><xliff:g id="level">%1$d</xliff:g>%%</string>
<!-- [CHAR_LIMIT=40] Label for battery level chart when discharging with duration --> <!-- [CHAR_LIMIT=40] Label for battery level chart when discharging with duration -->
<string name="power_discharging_duration"><xliff:g id="level">%1$d</xliff:g>%% <string name="power_discharging_duration"><xliff:g id="level">%1$d</xliff:g>%%
- approx. <xliff:g id="time">%2$s</xliff:g> left</string> - approx. <xliff:g id="time">%2$s</xliff:g> left</string>
@@ -5147,9 +5145,6 @@
<!-- Button label for cancelling the new-password operation and retaining the user's previous full-backup password --> <!-- Button label for cancelling the new-password operation and retaining the user's previous full-backup password -->
<string name="backup_pw_cancel_button_text">Cancel</string> <string name="backup_pw_cancel_button_text">Cancel</string>
<!-- Representation of a numerical percentage. [CHAR LIMIT=8] -->
<string name="percentage"><xliff:g id="number" example="30">%d</xliff:g>%%</string>
<!-- A menu item in "About phone" that allows the user to update the phone with settings <!-- A menu item in "About phone" that allows the user to update the phone with settings
from their cell phone carrier. The use of the string is similar to the string from their cell phone carrier. The use of the string is similar to the string
"system_update_settings_list_item_title" in this project. [CHAR LIMIT=25] --> "system_update_settings_list_item_title" in this project. [CHAR LIMIT=25] -->

View File

@@ -61,6 +61,8 @@ import android.provider.ContactsContract.Profile;
import android.provider.ContactsContract.RawContacts; import android.provider.ContactsContract.RawContacts;
import android.service.persistentdata.PersistentDataBlockManager; import android.service.persistentdata.PersistentDataBlockManager;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.text.BidiFormatter;
import android.text.TextDirectionHeuristics;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
@@ -76,6 +78,7 @@ import com.android.settings.drawable.CircleFramedDrawable;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@@ -123,6 +126,10 @@ public final class Utils {
private static final String SETTINGS_PACKAGE_NAME = "com.android.settings"; private static final String SETTINGS_PACKAGE_NAME = "com.android.settings";
private static final int SECONDS_PER_MINUTE = 60;
private static final int SECONDS_PER_HOUR = 60 * 60;
private static final int SECONDS_PER_DAY = 24 * 60 * 60;
/** /**
* Finds a matching activity for a preference's intent. If a matching * Finds a matching activity for a preference's intent. If a matching
* activity is not found, it will remove the preference. * activity is not found, it will remove the preference.
@@ -314,12 +321,28 @@ public final class Utils {
} }
} }
/** Formats the ratio of amount/total as a percentage. */
public static String formatPercentage(long amount, long total) {
return formatPercentage(((double) amount) / total);
}
/** Formats an integer from 0..100 as a percentage. */
public static String formatPercentage(int percentage) {
return formatPercentage(((double) percentage) / 100.0);
}
/** Formats a double from 0.0..1.0 as a percentage. */
private static String formatPercentage(double percentage) {
BidiFormatter bf = BidiFormatter.getInstance();
return bf.unicodeWrap(NumberFormat.getPercentInstance().format(percentage));
}
public static boolean isBatteryPresent(Intent batteryChangedIntent) { public static boolean isBatteryPresent(Intent batteryChangedIntent) {
return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true); return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
} }
public static String getBatteryPercentage(Intent batteryChangedIntent) { public static String getBatteryPercentage(Intent batteryChangedIntent) {
return String.valueOf(getBatteryLevel(batteryChangedIntent)) + "%"; return formatPercentage(getBatteryLevel(batteryChangedIntent));
} }
public static int getBatteryLevel(Intent batteryChangedIntent) { public static int getBatteryLevel(Intent batteryChangedIntent) {
@@ -871,4 +894,58 @@ public final class Utils {
return null; return null;
} }
/**
* Returns elapsed time for the given millis, in the following format:
* 2d 5h 40m 29s
* @param context the application context
* @param millis the elapsed time in milli seconds
* @param withSeconds include seconds?
* @return the formatted elapsed time
*/
public static String formatElapsedTime(Context context, double millis, boolean withSeconds) {
StringBuilder sb = new StringBuilder();
int seconds = (int) Math.floor(millis / 1000);
if (!withSeconds) {
// Round up.
seconds += 30;
}
int days = 0, hours = 0, minutes = 0;
if (seconds >= SECONDS_PER_DAY) {
days = seconds / SECONDS_PER_DAY;
seconds -= days * SECONDS_PER_DAY;
}
if (seconds >= SECONDS_PER_HOUR) {
hours = seconds / SECONDS_PER_HOUR;
seconds -= hours * SECONDS_PER_HOUR;
}
if (seconds >= SECONDS_PER_MINUTE) {
minutes = seconds / SECONDS_PER_MINUTE;
seconds -= minutes * SECONDS_PER_MINUTE;
}
if (withSeconds) {
if (days > 0) {
sb.append(context.getString(R.string.battery_history_days,
days, hours, minutes, seconds));
} else if (hours > 0) {
sb.append(context.getString(R.string.battery_history_hours,
hours, minutes, seconds));
} else if (minutes > 0) {
sb.append(context.getString(R.string.battery_history_minutes, minutes, seconds));
} else {
sb.append(context.getString(R.string.battery_history_seconds, seconds));
}
} else {
if (days > 0) {
sb.append(context.getString(R.string.battery_history_days_no_seconds,
days, hours, minutes));
} else if (hours > 0) {
sb.append(context.getString(R.string.battery_history_hours_no_seconds,
hours, minutes));
} else {
sb.append(context.getString(R.string.battery_history_minutes_no_seconds, minutes));
}
}
return sb.toString();
}
} }

View File

@@ -39,6 +39,7 @@ import android.widget.ImageView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@@ -72,11 +73,6 @@ public class ProcessStatsDetail extends Fragment implements Button.OnClickListen
private ViewGroup mDetailsParent; private ViewGroup mDetailsParent;
private ViewGroup mServicesParent; private ViewGroup mServicesParent;
public static String makePercentString(Resources res, long amount, long total) {
final double percent = (((double)amount) / total) * 100;
return res.getString(R.string.percentage, (int) Math.round(percent));
}
@Override @Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
super.onCreate(icicle); super.onCreate(icicle);
@@ -116,7 +112,7 @@ public class ProcessStatsDetail extends Fragment implements Button.OnClickListen
final double percentOfWeight = (((double)mEntry.mWeight) / mMaxWeight) * 100; final double percentOfWeight = (((double)mEntry.mWeight) / mMaxWeight) * 100;
int appLevel = (int) Math.ceil(percentOfWeight); int appLevel = (int) Math.ceil(percentOfWeight);
String appLevelText = makePercentString(getResources(), mEntry.mDuration, mTotalTime); String appLevelText = Utils.formatPercentage(mEntry.mDuration, mTotalTime);
// Set all values in the header. // Set all values in the header.
final TextView summary = (TextView) mRootView.findViewById(android.R.id.summary); final TextView summary = (TextView) mRootView.findViewById(android.R.id.summary);
@@ -203,7 +199,7 @@ public class ProcessStatsDetail extends Fragment implements Button.OnClickListen
Formatter.formatShortFileSize(getActivity(), Formatter.formatShortFileSize(getActivity(),
(mUseUss ? mEntry.mMaxUss : mEntry.mMaxPss) * 1024)); (mUseUss ? mEntry.mMaxUss : mEntry.mMaxPss) * 1024));
addDetailsItem(mDetailsParent, getResources().getText(R.string.process_stats_run_time), addDetailsItem(mDetailsParent, getResources().getText(R.string.process_stats_run_time),
makePercentString(getResources(), mEntry.mDuration, mTotalTime)); Utils.formatPercentage(mEntry.mDuration, mTotalTime));
} }
final static Comparator<ProcStatsEntry.Service> sServiceCompare final static Comparator<ProcStatsEntry.Service> sServiceCompare
@@ -265,10 +261,8 @@ public class ProcessStatsDetail extends Fragment implements Button.OnClickListen
if (tail >= 0 && tail < (label.length()-1)) { if (tail >= 0 && tail < (label.length()-1)) {
label = label.substring(tail+1); label = label.substring(tail+1);
} }
long duration = service.mDuration; String percentage = Utils.formatPercentage(service.mDuration, mTotalTime);
final double percentOfTime = (((double)duration) / mTotalTime) * 100; addDetailsItem(mServicesParent, label, percentage);
addDetailsItem(mServicesParent, label, getActivity().getResources().getString(
R.string.percentage, (int) Math.ceil(percentOfTime)));
} }
} }
} }

View File

@@ -26,6 +26,7 @@ import android.view.View;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils;
public class ProcessStatsPreference extends Preference { public class ProcessStatsPreference extends Preference {
private ProcStatsEntry mEntry; private ProcStatsEntry mEntry;
@@ -61,8 +62,7 @@ public class ProcessStatsPreference extends Preference {
public void setPercent(double percentOfWeight, double percentOfTime) { public void setPercent(double percentOfWeight, double percentOfTime) {
mProgress = (int) Math.ceil(percentOfWeight); mProgress = (int) Math.ceil(percentOfWeight);
mProgressText = getContext().getResources().getString( mProgressText = Utils.formatPercentage((int) percentOfTime);
R.string.percentage, (int) Math.round(percentOfTime));
notifyChanged(); notifyChanged();
} }

View File

@@ -43,7 +43,7 @@ import com.android.internal.app.ProcessStats;
import com.android.internal.util.MemInfoReader; import com.android.internal.util.MemInfoReader;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsActivity; import com.android.settings.SettingsActivity;
import com.android.settings.fuelgauge.Utils; import com.android.settings.Utils;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;

View File

@@ -26,6 +26,7 @@ import android.text.format.Formatter;
import android.util.Log; import android.util.Log;
import android.util.TimeUtils; import android.util.TimeUtils;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils;
import android.content.Context; import android.content.Context;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
@@ -489,10 +490,8 @@ public class BatteryHistoryChart extends View {
mCpuRunningLabel = getContext().getString(R.string.battery_stats_wake_lock_label); mCpuRunningLabel = getContext().getString(R.string.battery_stats_wake_lock_label);
mPhoneSignalLabel = getContext().getString(R.string.battery_stats_phone_signal_label); mPhoneSignalLabel = getContext().getString(R.string.battery_stats_phone_signal_label);
mMaxPercentLabelString = getContext().getResources().getString( mMaxPercentLabelString = Utils.formatPercentage(100);
R.string.percentage, 100); mMinPercentLabelString = Utils.formatPercentage(0);
mMinPercentLabelString = getContext().getResources().getString(
R.string.percentage, 0);
mBatteryLevel = com.android.settings.Utils.getBatteryLevel(mBatteryBroadcast); mBatteryLevel = com.android.settings.Utils.getBatteryLevel(mBatteryBroadcast);
long remainingTimeUs = 0; long remainingTimeUs = 0;
@@ -506,8 +505,7 @@ public class BatteryHistoryChart extends View {
mChargeLabelString = getContext().getResources().getString( mChargeLabelString = getContext().getResources().getString(
R.string.power_discharging_duration, mBatteryLevel, timeString); R.string.power_discharging_duration, mBatteryLevel, timeString);
} else { } else {
mChargeLabelString = getContext().getResources().getString( mChargeLabelString = Utils.formatPercentage(mBatteryLevel);
R.string.power_discharging, mBatteryLevel);
} }
} else { } else {
final long chargeTime = mStats.computeChargeTimeRemaining(elapsedRealtimeUs); final long chargeTime = mStats.computeChargeTimeRemaining(elapsedRealtimeUs);

View File

@@ -25,6 +25,7 @@ import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils;
/** /**
* Custom preference for displaying power consumption as a bar and an icon on * Custom preference for displaying power consumption as a bar and an icon on
@@ -47,8 +48,7 @@ public class PowerGaugePreference extends Preference {
public void setPercent(double percentOfMax, double percentOfTotal) { public void setPercent(double percentOfMax, double percentOfTotal) {
mProgress = (int) Math.ceil(percentOfMax); mProgress = (int) Math.ceil(percentOfMax);
mProgressText = getContext().getResources().getString( mProgressText = Utils.formatPercentage((int) (percentOfTotal + 0.5));
R.string.percentage, (int) (percentOfTotal+.5));
notifyChanged(); notifyChanged();
} }

View File

@@ -53,6 +53,7 @@ import com.android.internal.util.FastPrintWriter;
import com.android.settings.DisplaySettings; import com.android.settings.DisplaySettings;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsActivity; import com.android.settings.SettingsActivity;
import com.android.settings.Utils;
import com.android.settings.WirelessSettings; import com.android.settings.WirelessSettings;
import com.android.settings.applications.InstalledAppDetails; import com.android.settings.applications.InstalledAppDetails;
import com.android.settings.bluetooth.BluetoothSettings; import com.android.settings.bluetooth.BluetoothSettings;
@@ -387,7 +388,7 @@ public class PowerUsageDetail extends Fragment implements Button.OnClickListener
mTitleView.setText(mTitle); mTitleView.setText(mTitle);
final TextView text1 = (TextView)mRootView.findViewById(android.R.id.text1); final TextView text1 = (TextView)mRootView.findViewById(android.R.id.text1);
text1.setText(getString(R.string.percentage, percentage)); text1.setText(Utils.formatPercentage(percentage));
mTwoButtonsPanel = (ViewGroup)mRootView.findViewById(R.id.two_buttons_panel); mTwoButtonsPanel = (ViewGroup)mRootView.findViewById(R.id.two_buttons_panel);
mForceStopButton = (Button)mRootView.findViewById(R.id.left_button); mForceStopButton = (Button)mRootView.findViewById(R.id.left_button);
@@ -507,7 +508,7 @@ public class PowerUsageDetail extends Fragment implements Button.OnClickListener
break; break;
case R.string.usage_type_no_coverage: case R.string.usage_type_no_coverage:
final int percentage = (int) Math.floor(mValues[i]); final int percentage = (int) Math.floor(mValues[i]);
value = getActivity().getString(R.string.percentage, percentage); value = Utils.formatPercentage(percentage);
break; break;
case R.string.usage_type_total_battery_capacity: case R.string.usage_type_total_battery_capacity:
case R.string.usage_type_computed_power: case R.string.usage_type_computed_power:

View File

@@ -1,84 +0,0 @@
/*
* Copyright (C) 2009 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.
*/
package com.android.settings.fuelgauge;
import android.content.Context;
import com.android.settings.R;
/**
* Contains utility functions for formatting elapsed time and consumed bytes
*/
public class Utils {
private static final int SECONDS_PER_MINUTE = 60;
private static final int SECONDS_PER_HOUR = 60 * 60;
private static final int SECONDS_PER_DAY = 24 * 60 * 60;
/**
* Returns elapsed time for the given millis, in the following format:
* 2d 5h 40m 29s
* @param context the application context
* @param millis the elapsed time in milli seconds
* @return the formatted elapsed time
*/
public static String formatElapsedTime(Context context, double millis, boolean inclSeconds) {
StringBuilder sb = new StringBuilder();
int seconds = (int) Math.floor(millis / 1000);
if (!inclSeconds) {
// Round up.
seconds += 30;
}
int days = 0, hours = 0, minutes = 0;
if (seconds >= SECONDS_PER_DAY) {
days = seconds / SECONDS_PER_DAY;
seconds -= days * SECONDS_PER_DAY;
}
if (seconds >= SECONDS_PER_HOUR) {
hours = seconds / SECONDS_PER_HOUR;
seconds -= hours * SECONDS_PER_HOUR;
}
if (seconds >= SECONDS_PER_MINUTE) {
minutes = seconds / SECONDS_PER_MINUTE;
seconds -= minutes * SECONDS_PER_MINUTE;
}
if (inclSeconds) {
if (days > 0) {
sb.append(context.getString(R.string.battery_history_days,
days, hours, minutes, seconds));
} else if (hours > 0) {
sb.append(context.getString(R.string.battery_history_hours,
hours, minutes, seconds));
} else if (minutes > 0) {
sb.append(context.getString(R.string.battery_history_minutes, minutes, seconds));
} else {
sb.append(context.getString(R.string.battery_history_seconds, seconds));
}
} else {
if (days > 0) {
sb.append(context.getString(R.string.battery_history_days_no_seconds,
days, hours, minutes));
} else if (hours > 0) {
sb.append(context.getString(R.string.battery_history_hours_no_seconds,
hours, minutes));
} else {
sb.append(context.getString(R.string.battery_history_minutes_no_seconds, minutes));
}
}
return sb.toString();
}
}