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

@@ -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();
}
}