Merge "Start cleaning up percentage formatting in Settings." into lmp-dev

This commit is contained in:
Elliott Hughes
2014-09-08 23:26:20 +00:00
committed by Android (Google) Code Review
9 changed files with 95 additions and 114 deletions

View File

@@ -61,6 +61,8 @@ import android.provider.ContactsContract.Profile;
import android.provider.ContactsContract.RawContacts;
import android.service.persistentdata.PersistentDataBlockManager;
import android.telephony.TelephonyManager;
import android.text.BidiFormatter;
import android.text.TextDirectionHeuristics;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
@@ -76,6 +78,7 @@ import com.android.settings.drawable.CircleFramedDrawable;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Iterator;
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 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
* 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) {
return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
}
public static String getBatteryPercentage(Intent batteryChangedIntent) {
return String.valueOf(getBatteryLevel(batteryChangedIntent)) + "%";
return formatPercentage(getBatteryLevel(batteryChangedIntent));
}
public static int getBatteryLevel(Intent batteryChangedIntent) {
@@ -873,4 +896,58 @@ public final class Utils {
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.TextView;
import com.android.settings.R;
import com.android.settings.Utils;
import java.util.ArrayList;
import java.util.Collections;
@@ -72,11 +73,6 @@ public class ProcessStatsDetail extends Fragment implements Button.OnClickListen
private ViewGroup mDetailsParent;
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
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
@@ -116,7 +112,7 @@ public class ProcessStatsDetail extends Fragment implements Button.OnClickListen
final double percentOfWeight = (((double)mEntry.mWeight) / mMaxWeight) * 100;
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.
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(),
(mUseUss ? mEntry.mMaxUss : mEntry.mMaxPss) * 1024));
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
@@ -265,10 +261,8 @@ public class ProcessStatsDetail extends Fragment implements Button.OnClickListen
if (tail >= 0 && tail < (label.length()-1)) {
label = label.substring(tail+1);
}
long duration = service.mDuration;
final double percentOfTime = (((double)duration) / mTotalTime) * 100;
addDetailsItem(mServicesParent, label, getActivity().getResources().getString(
R.string.percentage, (int) Math.ceil(percentOfTime)));
String percentage = Utils.formatPercentage(service.mDuration, mTotalTime);
addDetailsItem(mServicesParent, label, percentage);
}
}
}

View File

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

View File

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

View File

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

View File

@@ -25,6 +25,7 @@ import android.widget.ProgressBar;
import android.widget.TextView;
import com.android.settings.R;
import com.android.settings.Utils;
/**
* 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) {
mProgress = (int) Math.ceil(percentOfMax);
mProgressText = getContext().getResources().getString(
R.string.percentage, (int) (percentOfTotal+.5));
mProgressText = Utils.formatPercentage((int) (percentOfTotal + 0.5));
notifyChanged();
}

View File

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