am 02e852f9: Merge "Make ProgressDialog a little less lame." into honeycomb

* commit '02e852f968bac83947cd97a5fe7c7a7064774c5a':
  Make ProgressDialog a little less lame.
This commit is contained in:
Dianne Hackborn
2011-01-12 13:01:42 -08:00
committed by Android Git Automerger
3 changed files with 74 additions and 19 deletions

View File

@@ -34814,6 +34814,32 @@
<parameter name="d" type="android.graphics.drawable.Drawable">
</parameter>
</method>
<method name="setProgressNumberFormat"
return="void"
abstract="false"
native="false"
synchronized="false"
static="false"
final="false"
deprecated="not deprecated"
visibility="public"
>
<parameter name="format" type="java.lang.String">
</parameter>
</method>
<method name="setProgressPercentFormat"
return="void"
abstract="false"
native="false"
synchronized="false"
static="false"
final="false"
deprecated="not deprecated"
visibility="public"
>
<parameter name="format" type="java.text.NumberFormat">
</parameter>
</method>
<method name="setProgressStyle"
return="void"
abstract="false"

View File

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

View File

@@ -40,9 +40,9 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="12dip"
android:layout_marginLeft="50dip"
android:layout_marginRight="15dip"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_alignParentRight="true"
android:layout_below="@id/progress"
/>
</RelativeLayout>