diff options
author | Dianne Hackborn <hackbod@google.com> | 2011-01-12 11:17:17 -0800 |
---|---|---|
committer | Dianne Hackborn <hackbod@google.com> | 2011-01-12 11:17:17 -0800 |
commit | 4389434c1d685b5b71de74b0c7718b8ed5b9dcb5 (patch) | |
tree | 7c6d1685b0a44c99be49ff3fac6fa6e8cc1a195d /core/java/android/app | |
parent | 6014527c350895383b99ba91d3d009a32b1d22a6 (diff) | |
download | frameworks_base-4389434c1d685b5b71de74b0c7718b8ed5b9dcb5.zip frameworks_base-4389434c1d685b5b71de74b0c7718b8ed5b9dcb5.tar.gz frameworks_base-4389434c1d685b5b71de74b0c7718b8ed5b9dcb5.tar.bz2 |
Make ProgressDialog a little less lame.
Change-Id: Ic3f1dd8ba32dd02d422ee6f05fff5541e54bb8a1
Diffstat (limited to 'core/java/android/app')
-rw-r--r-- | core/java/android/app/ProgressDialog.java | 61 |
1 files changed, 45 insertions, 16 deletions
diff --git a/core/java/android/app/ProgressDialog.java b/core/java/android/app/ProgressDialog.java index af1b294..d421173 100644 --- a/core/java/android/app/ProgressDialog.java +++ b/core/java/android/app/ProgressDialog.java @@ -18,7 +18,6 @@ package android.app; import android.content.Context; import android.graphics.drawable.Drawable; -import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -75,12 +74,20 @@ public class ProgressDialog extends AlertDialog { public ProgressDialog(Context context) { super(context); + initFormats(); } public ProgressDialog(Context context, int theme) { super(context, theme); + initFormats(); } + private void initFormats() { + mProgressNumberFormat = "%1d/%2d"; + mProgressPercentFormat = NumberFormat.getPercentInstance(); + mProgressPercentFormat.setMaximumFractionDigits(0); + } + public static ProgressDialog show(Context context, CharSequence title, CharSequence message) { return show(context, title, message, false); @@ -125,22 +132,27 @@ public class ProgressDialog extends AlertDialog { /* Update the number and percent */ int progress = mProgress.getProgress(); int max = mProgress.getMax(); - double percent = (double) progress / (double) max; - String format = mProgressNumberFormat; - mProgressNumber.setText(String.format(format, progress, max)); - SpannableString tmp = new SpannableString(mProgressPercentFormat.format(percent)); - tmp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), - 0, tmp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); - mProgressPercent.setText(tmp); + if (mProgressNumberFormat != null) { + String format = mProgressNumberFormat; + mProgressNumber.setText(String.format(format, progress, max)); + } else { + mProgressNumber.setText(""); + } + if (mProgressPercentFormat != null) { + double percent = (double) progress / (double) max; + SpannableString tmp = new SpannableString(mProgressPercentFormat.format(percent)); + tmp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), + 0, tmp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + mProgressPercent.setText(tmp); + } else { + mProgressPercent.setText(""); + } } }; View view = inflater.inflate(R.layout.alert_dialog_progress, null); mProgress = (ProgressBar) view.findViewById(R.id.progress); mProgressNumber = (TextView) view.findViewById(R.id.progress_number); - mProgressNumberFormat = "%d/%d"; mProgressPercent = (TextView) view.findViewById(R.id.progress_percent); - mProgressPercentFormat = NumberFormat.getPercentInstance(); - mProgressPercentFormat.setMaximumFractionDigits(0); setView(view); } else { View view = inflater.inflate(R.layout.progress_dialog, null); @@ -304,19 +316,36 @@ public class ProgressDialog extends AlertDialog { } /** - * Change the format of Progress Number. The default is "current/max". + * Change the format of the small text showing current and maximum units + * of progress. The default is "%1d/%2d". * Should not be called during the number is progressing. - * @param format Should contain two "%d". The first is used for current number - * and the second is used for the maximum. - * @hide + * @param format A string passed to {@link String#format String.format()}; + * use "%1d" for the current number and "%2d" for the maximum. If null, + * nothing will be shown. */ public void setProgressNumberFormat(String format) { mProgressNumberFormat = format; + onProgressChanged(); + } + + /** + * Change the format of the small text showing the percentage of progress. + * The default is + * {@link NumberFormat#getPercentInstance() NumberFormat.getPercentageInstnace().} + * Should not be called during the number is progressing. + * @param format An instance of a {@link NumberFormat} to generate the + * percentage text. If null, nothing will be shown. + */ + public void setProgressPercentFormat(NumberFormat format) { + mProgressPercentFormat = format; + onProgressChanged(); } private void onProgressChanged() { if (mProgressStyle == STYLE_HORIZONTAL) { - mViewUpdateHandler.sendEmptyMessage(0); + if (!mViewUpdateHandler.hasMessages(0)) { + mViewUpdateHandler.sendEmptyMessage(0); + } } } } |