| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.content.Context; |
| 21 |
import android.content.res.TypedArray; |
| 22 |
import android.graphics.drawable.Drawable; |
| 23 |
import android.os.Build; |
| 24 |
import android.os.Bundle; |
| 25 |
import android.os.Handler; |
| 26 |
import android.os.Message; |
| 27 |
import android.text.Spannable; |
| 28 |
import android.text.SpannableString; |
| 29 |
import android.text.style.StyleSpan; |
| 30 |
import android.view.LayoutInflater; |
| 31 |
import android.view.View; |
| 32 |
import android.widget.ProgressBar; |
| 33 |
import android.widget.TextView; |
| 34 |
|
| 35 |
import com.android.internal.R; |
| 36 |
|
| 37 |
import java.text.NumberFormat; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
@Deprecated |
| 55 |
public class ProgressDialog extends AlertDialog { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static final int STYLE_SPINNER = 0; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public static final int STYLE_HORIZONTAL = 1; |
| 67 |
|
| 68 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 69 |
private ProgressBar mProgress; |
| 70 |
@UnsupportedAppUsage |
| 71 |
private TextView mMessageView; |
| 72 |
|
| 73 |
private int mProgressStyle = STYLE_SPINNER; |
| 74 |
@UnsupportedAppUsage |
| 75 |
private TextView mProgressNumber; |
| 76 |
private String mProgressNumberFormat; |
| 77 |
private TextView mProgressPercent; |
| 78 |
private NumberFormat mProgressPercentFormat; |
| 79 |
|
| 80 |
private int mMax; |
| 81 |
private int mProgressVal; |
| 82 |
private int mSecondaryProgressVal; |
| 83 |
private int mIncrementBy; |
| 84 |
private int mIncrementSecondaryBy; |
| 85 |
private Drawable mProgressDrawable; |
| 86 |
private Drawable mIndeterminateDrawable; |
| 87 |
private CharSequence mMessage; |
| 88 |
private boolean mIndeterminate; |
| 89 |
|
| 90 |
private boolean mHasStarted; |
| 91 |
private Handler mViewUpdateHandler; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public ProgressDialog(Context context) { |
| 99 |
super(context); |
| 100 |
initFormats(); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public ProgressDialog(Context context, int theme) { |
| 112 |
super(context, theme); |
| 113 |
initFormats(); |
| 114 |
} |
| 115 |
|
| 116 |
private void initFormats() { |
| 117 |
mProgressNumberFormat = "%1d/%2d"; |
| 118 |
mProgressPercentFormat = NumberFormat.getPercentInstance(); |
| 119 |
mProgressPercentFormat.setMaximumFractionDigits(0); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public static ProgressDialog show(Context context, CharSequence title, |
| 131 |
CharSequence message) { |
| 132 |
return show(context, title, message, false); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public static ProgressDialog show(Context context, CharSequence title, |
| 146 |
CharSequence message, boolean indeterminate) { |
| 147 |
return show(context, title, message, indeterminate, false, null); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public static ProgressDialog show(Context context, CharSequence title, |
| 163 |
CharSequence message, boolean indeterminate, boolean cancelable) { |
| 164 |
return show(context, title, message, indeterminate, cancelable, null); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public static ProgressDialog show(Context context, CharSequence title, |
| 182 |
CharSequence message, boolean indeterminate, |
| 183 |
boolean cancelable, OnCancelListener cancelListener) { |
| 184 |
ProgressDialog dialog = new ProgressDialog(context); |
| 185 |
dialog.setTitle(title); |
| 186 |
dialog.setMessage(message); |
| 187 |
dialog.setIndeterminate(indeterminate); |
| 188 |
dialog.setCancelable(cancelable); |
| 189 |
dialog.setOnCancelListener(cancelListener); |
| 190 |
dialog.show(); |
| 191 |
return dialog; |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
protected void onCreate(Bundle savedInstanceState) { |
| 196 |
LayoutInflater inflater = LayoutInflater.from(mContext); |
| 197 |
TypedArray a = mContext.obtainStyledAttributes(null, |
| 198 |
com.android.internal.R.styleable.AlertDialog, |
| 199 |
com.android.internal.R.attr.alertDialogStyle, 0); |
| 200 |
if (mProgressStyle == STYLE_HORIZONTAL) { |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
mViewUpdateHandler = new Handler() { |
| 206 |
@Override |
| 207 |
public void handleMessage(Message msg) { |
| 208 |
super.handleMessage(msg); |
| 209 |
|
| 210 |
|
| 211 |
int progress = mProgress.getProgress(); |
| 212 |
int max = mProgress.getMax(); |
| 213 |
if (mProgressNumberFormat != null) { |
| 214 |
String format = mProgressNumberFormat; |
| 215 |
mProgressNumber.setText(String.format(format, progress, max)); |
| 216 |
} else { |
| 217 |
mProgressNumber.setText(""); |
| 218 |
} |
| 219 |
if (mProgressPercentFormat != null) { |
| 220 |
double percent = (double) progress / (double) max; |
| 221 |
SpannableString tmp = new SpannableString(mProgressPercentFormat.format(percent)); |
| 222 |
tmp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), |
| 223 |
0, tmp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 224 |
mProgressPercent.setText(tmp); |
| 225 |
} else { |
| 226 |
mProgressPercent.setText(""); |
| 227 |
} |
| 228 |
} |
| 229 |
}; |
| 230 |
View view = inflater.inflate(a.getResourceId( |
| 231 |
com.android.internal.R.styleable.AlertDialog_horizontalProgressLayout, |
| 232 |
R.layout.alert_dialog_progress), null); |
| 233 |
mProgress = (ProgressBar) view.findViewById(R.id.progress); |
| 234 |
mProgressNumber = (TextView) view.findViewById(R.id.progress_number); |
| 235 |
mProgressPercent = (TextView) view.findViewById(R.id.progress_percent); |
| 236 |
setView(view); |
| 237 |
} else { |
| 238 |
View view = inflater.inflate(a.getResourceId( |
| 239 |
com.android.internal.R.styleable.AlertDialog_progressLayout, |
| 240 |
R.layout.progress_dialog), null); |
| 241 |
mProgress = (ProgressBar) view.findViewById(R.id.progress); |
| 242 |
mMessageView = (TextView) view.findViewById(R.id.message); |
| 243 |
setView(view); |
| 244 |
} |
| 245 |
a.recycle(); |
| 246 |
if (mMax > 0) { |
| 247 |
setMax(mMax); |
| 248 |
} |
| 249 |
if (mProgressVal > 0) { |
| 250 |
setProgress(mProgressVal); |
| 251 |
} |
| 252 |
if (mSecondaryProgressVal > 0) { |
| 253 |
setSecondaryProgress(mSecondaryProgressVal); |
| 254 |
} |
| 255 |
if (mIncrementBy > 0) { |
| 256 |
incrementProgressBy(mIncrementBy); |
| 257 |
} |
| 258 |
if (mIncrementSecondaryBy > 0) { |
| 259 |
incrementSecondaryProgressBy(mIncrementSecondaryBy); |
| 260 |
} |
| 261 |
if (mProgressDrawable != null) { |
| 262 |
setProgressDrawable(mProgressDrawable); |
| 263 |
} |
| 264 |
if (mIndeterminateDrawable != null) { |
| 265 |
setIndeterminateDrawable(mIndeterminateDrawable); |
| 266 |
} |
| 267 |
if (mMessage != null) { |
| 268 |
setMessage(mMessage); |
| 269 |
} |
| 270 |
setIndeterminate(mIndeterminate); |
| 271 |
onProgressChanged(); |
| 272 |
super.onCreate(savedInstanceState); |
| 273 |
} |
| 274 |
|
| 275 |
@Override |
| 276 |
public void onStart() { |
| 277 |
super.onStart(); |
| 278 |
mHasStarted = true; |
| 279 |
} |
| 280 |
|
| 281 |
@Override |
| 282 |
protected void onStop() { |
| 283 |
super.onStop(); |
| 284 |
mHasStarted = false; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public void setProgress(int value) { |
| 295 |
if (mHasStarted) { |
| 296 |
mProgress.setProgress(value); |
| 297 |
onProgressChanged(); |
| 298 |
} else { |
| 299 |
mProgressVal = value; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
public void setSecondaryProgress(int secondaryProgress) { |
| 312 |
if (mProgress != null) { |
| 313 |
mProgress.setSecondaryProgress(secondaryProgress); |
| 314 |
onProgressChanged(); |
| 315 |
} else { |
| 316 |
mSecondaryProgressVal = secondaryProgress; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
public int getProgress() { |
| 326 |
if (mProgress != null) { |
| 327 |
return mProgress.getProgress(); |
| 328 |
} |
| 329 |
return mProgressVal; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public int getSecondaryProgress() { |
| 338 |
if (mProgress != null) { |
| 339 |
return mProgress.getSecondaryProgress(); |
| 340 |
} |
| 341 |
return mSecondaryProgressVal; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
public int getMax() { |
| 350 |
if (mProgress != null) { |
| 351 |
return mProgress.getMax(); |
| 352 |
} |
| 353 |
return mMax; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
public void setMax(int max) { |
| 360 |
if (mProgress != null) { |
| 361 |
mProgress.setMax(max); |
| 362 |
onProgressChanged(); |
| 363 |
} else { |
| 364 |
mMax = max; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
public void incrementProgressBy(int diff) { |
| 375 |
if (mProgress != null) { |
| 376 |
mProgress.incrementProgressBy(diff); |
| 377 |
onProgressChanged(); |
| 378 |
} else { |
| 379 |
mIncrementBy += diff; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
public void incrementSecondaryProgressBy(int diff) { |
| 390 |
if (mProgress != null) { |
| 391 |
mProgress.incrementSecondaryProgressBy(diff); |
| 392 |
onProgressChanged(); |
| 393 |
} else { |
| 394 |
mIncrementSecondaryBy += diff; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
public void setProgressDrawable(Drawable d) { |
| 406 |
if (mProgress != null) { |
| 407 |
mProgress.setProgressDrawable(d); |
| 408 |
} else { |
| 409 |
mProgressDrawable = d; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
public void setIndeterminateDrawable(Drawable d) { |
| 422 |
if (mProgress != null) { |
| 423 |
mProgress.setIndeterminateDrawable(d); |
| 424 |
} else { |
| 425 |
mIndeterminateDrawable = d; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
public void setIndeterminate(boolean indeterminate) { |
| 442 |
if (mProgress != null) { |
| 443 |
mProgress.setIndeterminate(indeterminate); |
| 444 |
} else { |
| 445 |
mIndeterminate = indeterminate; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
public boolean isIndeterminate() { |
| 455 |
if (mProgress != null) { |
| 456 |
return mProgress.isIndeterminate(); |
| 457 |
} |
| 458 |
return mIndeterminate; |
| 459 |
} |
| 460 |
|
| 461 |
@Override |
| 462 |
public void setMessage(CharSequence message) { |
| 463 |
if (mProgress != null) { |
| 464 |
if (mProgressStyle == STYLE_HORIZONTAL) { |
| 465 |
super.setMessage(message); |
| 466 |
} else { |
| 467 |
mMessageView.setText(message); |
| 468 |
} |
| 469 |
} else { |
| 470 |
mMessage = message; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
public void setProgressStyle(int style) { |
| 486 |
mProgressStyle = style; |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
public void setProgressNumberFormat(String format) { |
| 498 |
mProgressNumberFormat = format; |
| 499 |
onProgressChanged(); |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
public void setProgressPercentFormat(NumberFormat format) { |
| 511 |
mProgressPercentFormat = format; |
| 512 |
onProgressChanged(); |
| 513 |
} |
| 514 |
|
| 515 |
private void onProgressChanged() { |
| 516 |
if (mProgressStyle == STYLE_HORIZONTAL) { |
| 517 |
if (mViewUpdateHandler != null && !mViewUpdateHandler.hasMessages(0)) { |
| 518 |
mViewUpdateHandler.sendEmptyMessage(0); |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
} |