| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.preference; |
| 18 |
|
| 19 |
|
| 20 |
import android.annotation.CallSuper; |
| 21 |
import android.annotation.DrawableRes; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.annotation.StringRes; |
| 24 |
import android.app.AlertDialog; |
| 25 |
import android.app.Dialog; |
| 26 |
import android.compat.annotation.UnsupportedAppUsage; |
| 27 |
import android.content.Context; |
| 28 |
import android.content.DialogInterface; |
| 29 |
import android.content.SharedPreferences; |
| 30 |
import android.content.res.TypedArray; |
| 31 |
import android.graphics.drawable.Drawable; |
| 32 |
import android.os.Bundle; |
| 33 |
import android.os.Parcel; |
| 34 |
import android.os.Parcelable; |
| 35 |
import android.text.TextUtils; |
| 36 |
import android.util.AttributeSet; |
| 37 |
import android.view.LayoutInflater; |
| 38 |
import android.view.View; |
| 39 |
import android.widget.TextView; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
@Deprecated |
| 60 |
public abstract class DialogPreference extends Preference implements |
| 61 |
DialogInterface.OnClickListener, DialogInterface.OnDismissListener, |
| 62 |
PreferenceManager.OnActivityDestroyListener { |
| 63 |
@UnsupportedAppUsage |
| 64 |
private AlertDialog.Builder mBuilder; |
| 65 |
|
| 66 |
@UnsupportedAppUsage |
| 67 |
private CharSequence mDialogTitle; |
| 68 |
@UnsupportedAppUsage |
| 69 |
private CharSequence mDialogMessage; |
| 70 |
@UnsupportedAppUsage |
| 71 |
private Drawable mDialogIcon; |
| 72 |
@UnsupportedAppUsage |
| 73 |
private CharSequence mPositiveButtonText; |
| 74 |
@UnsupportedAppUsage |
| 75 |
private CharSequence mNegativeButtonText; |
| 76 |
private int mDialogLayoutResId; |
| 77 |
|
| 78 |
|
| 79 |
@UnsupportedAppUsage |
| 80 |
private Dialog mDialog; |
| 81 |
|
| 82 |
|
| 83 |
@UnsupportedAppUsage |
| 84 |
private int mWhichButtonClicked; |
| 85 |
|
| 86 |
|
| 87 |
private final Runnable mDismissRunnable = new Runnable() { |
| 88 |
@Override |
| 89 |
public void run() { |
| 90 |
mDialog.dismiss(); |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
public DialogPreference( |
| 95 |
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 96 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 97 |
|
| 98 |
final TypedArray a = context.obtainStyledAttributes(attrs, |
| 99 |
com.android.internal.R.styleable.DialogPreference, defStyleAttr, defStyleRes); |
| 100 |
mDialogTitle = a.getString(com.android.internal.R.styleable.DialogPreference_dialogTitle); |
| 101 |
if (mDialogTitle == null) { |
| 102 |
|
| 103 |
|
| 104 |
mDialogTitle = getTitle(); |
| 105 |
} |
| 106 |
mDialogMessage = a.getString(com.android.internal.R.styleable.DialogPreference_dialogMessage); |
| 107 |
mDialogIcon = a.getDrawable(com.android.internal.R.styleable.DialogPreference_dialogIcon); |
| 108 |
mPositiveButtonText = a.getString(com.android.internal.R.styleable.DialogPreference_positiveButtonText); |
| 109 |
mNegativeButtonText = a.getString(com.android.internal.R.styleable.DialogPreference_negativeButtonText); |
| 110 |
mDialogLayoutResId = a.getResourceId(com.android.internal.R.styleable.DialogPreference_dialogLayout, |
| 111 |
mDialogLayoutResId); |
| 112 |
a.recycle(); |
| 113 |
} |
| 114 |
|
| 115 |
public DialogPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 116 |
this(context, attrs, defStyleAttr, 0); |
| 117 |
} |
| 118 |
|
| 119 |
public DialogPreference(Context context, AttributeSet attrs) { |
| 120 |
this(context, attrs, com.android.internal.R.attr.dialogPreferenceStyle); |
| 121 |
} |
| 122 |
|
| 123 |
public DialogPreference(Context context) { |
| 124 |
this(context, null); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public void setDialogTitle(CharSequence dialogTitle) { |
| 133 |
mDialogTitle = dialogTitle; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public void setDialogTitle(int dialogTitleResId) { |
| 141 |
setDialogTitle(getContext().getString(dialogTitleResId)); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public CharSequence getDialogTitle() { |
| 149 |
return mDialogTitle; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public void setDialogMessage(CharSequence dialogMessage) { |
| 163 |
mDialogMessage = dialogMessage; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public void setDialogMessage(int dialogMessageResId) { |
| 171 |
setDialogMessage(getContext().getString(dialogMessageResId)); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public CharSequence getDialogMessage() { |
| 179 |
return mDialogMessage; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public void setDialogIcon(Drawable dialogIcon) { |
| 188 |
mDialogIcon = dialogIcon; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public void setDialogIcon(@DrawableRes int dialogIconRes) { |
| 198 |
mDialogIcon = getContext().getDrawable(dialogIconRes); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public Drawable getDialogIcon() { |
| 206 |
return mDialogIcon; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public void setPositiveButtonText(CharSequence positiveButtonText) { |
| 216 |
mPositiveButtonText = positiveButtonText; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public void setPositiveButtonText(@StringRes int positiveButtonTextResId) { |
| 224 |
setPositiveButtonText(getContext().getString(positiveButtonTextResId)); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public CharSequence getPositiveButtonText() { |
| 234 |
return mPositiveButtonText; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public void setNegativeButtonText(CharSequence negativeButtonText) { |
| 244 |
mNegativeButtonText = negativeButtonText; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public void setNegativeButtonText(@StringRes int negativeButtonTextResId) { |
| 252 |
setNegativeButtonText(getContext().getString(negativeButtonTextResId)); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public CharSequence getNegativeButtonText() { |
| 262 |
return mNegativeButtonText; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public void setDialogLayoutResource(int dialogLayoutResId) { |
| 273 |
mDialogLayoutResId = dialogLayoutResId; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public int getDialogLayoutResource() { |
| 283 |
return mDialogLayoutResId; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { |
| 294 |
} |
| 295 |
|
| 296 |
@Override |
| 297 |
protected void onClick() { |
| 298 |
if (mDialog != null && mDialog.isShowing()) return; |
| 299 |
|
| 300 |
showDialog(null); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
protected void showDialog(Bundle state) { |
| 311 |
Context context = getContext(); |
| 312 |
|
| 313 |
mWhichButtonClicked = DialogInterface.BUTTON_NEGATIVE; |
| 314 |
|
| 315 |
mBuilder = new AlertDialog.Builder(context) |
| 316 |
.setTitle(mDialogTitle) |
| 317 |
.setIcon(mDialogIcon) |
| 318 |
.setPositiveButton(mPositiveButtonText, this) |
| 319 |
.setNegativeButton(mNegativeButtonText, this); |
| 320 |
|
| 321 |
View contentView = onCreateDialogView(); |
| 322 |
if (contentView != null) { |
| 323 |
onBindDialogView(contentView); |
| 324 |
mBuilder.setView(contentView); |
| 325 |
} else { |
| 326 |
mBuilder.setMessage(mDialogMessage); |
| 327 |
} |
| 328 |
|
| 329 |
onPrepareDialogBuilder(mBuilder); |
| 330 |
|
| 331 |
getPreferenceManager().registerOnActivityDestroyListener(this); |
| 332 |
|
| 333 |
|
| 334 |
final Dialog dialog = mDialog = mBuilder.create(); |
| 335 |
if (state != null) { |
| 336 |
dialog.onRestoreInstanceState(state); |
| 337 |
} |
| 338 |
dialog.setOnShowListener(new DialogInterface.OnShowListener() { |
| 339 |
@Override |
| 340 |
public void onShow(DialogInterface dialog) { |
| 341 |
removeDismissCallbacks(); |
| 342 |
} |
| 343 |
}); |
| 344 |
dialog.setOnDismissListener(this); |
| 345 |
dialog.show(); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
@Nullable |
| 354 |
private View getDecorView() { |
| 355 |
if (mDialog != null && mDialog.getWindow() != null) { |
| 356 |
return mDialog.getWindow().getDecorView(); |
| 357 |
} |
| 358 |
return null; |
| 359 |
} |
| 360 |
|
| 361 |
void postDismiss() { |
| 362 |
removeDismissCallbacks(); |
| 363 |
View decorView = getDecorView(); |
| 364 |
if (decorView != null) { |
| 365 |
|
| 366 |
decorView.post(mDismissRunnable); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
private void removeDismissCallbacks() { |
| 371 |
View decorView = getDecorView(); |
| 372 |
if (decorView != null) { |
| 373 |
decorView.removeCallbacks(mDismissRunnable); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
protected View onCreateDialogView() { |
| 386 |
if (mDialogLayoutResId == 0) { |
| 387 |
return null; |
| 388 |
} |
| 389 |
|
| 390 |
LayoutInflater inflater = LayoutInflater.from(mBuilder.getContext()); |
| 391 |
return inflater.inflate(mDialogLayoutResId, null); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
@CallSuper |
| 402 |
protected void onBindDialogView(View view) { |
| 403 |
View dialogMessageView = view.findViewById(com.android.internal.R.id.message); |
| 404 |
|
| 405 |
if (dialogMessageView != null) { |
| 406 |
final CharSequence message = getDialogMessage(); |
| 407 |
int newVisibility = View.GONE; |
| 408 |
|
| 409 |
if (!TextUtils.isEmpty(message)) { |
| 410 |
if (dialogMessageView instanceof TextView) { |
| 411 |
((TextView) dialogMessageView).setText(message); |
| 412 |
} |
| 413 |
|
| 414 |
newVisibility = View.VISIBLE; |
| 415 |
} |
| 416 |
|
| 417 |
if (dialogMessageView.getVisibility() != newVisibility) { |
| 418 |
dialogMessageView.setVisibility(newVisibility); |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
public void onClick(DialogInterface dialog, int which) { |
| 424 |
mWhichButtonClicked = which; |
| 425 |
} |
| 426 |
|
| 427 |
@Override |
| 428 |
public void onDismiss(DialogInterface dialog) { |
| 429 |
removeDismissCallbacks(); |
| 430 |
getPreferenceManager().unregisterOnActivityDestroyListener(this); |
| 431 |
|
| 432 |
mDialog = null; |
| 433 |
onDialogClosed(mWhichButtonClicked == DialogInterface.BUTTON_POSITIVE); |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
protected void onDialogClosed(boolean positiveResult) { |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
public Dialog getDialog() { |
| 452 |
return mDialog; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
public void onActivityDestroy() { |
| 459 |
|
| 460 |
if (mDialog == null || !mDialog.isShowing()) { |
| 461 |
return; |
| 462 |
} |
| 463 |
|
| 464 |
mDialog.dismiss(); |
| 465 |
} |
| 466 |
|
| 467 |
@Override |
| 468 |
protected Parcelable onSaveInstanceState() { |
| 469 |
final Parcelable superState = super.onSaveInstanceState(); |
| 470 |
if (mDialog == null || !mDialog.isShowing()) { |
| 471 |
return superState; |
| 472 |
} |
| 473 |
|
| 474 |
final SavedState myState = new SavedState(superState); |
| 475 |
myState.isDialogShowing = true; |
| 476 |
myState.dialogBundle = mDialog.onSaveInstanceState(); |
| 477 |
return myState; |
| 478 |
} |
| 479 |
|
| 480 |
@Override |
| 481 |
protected void onRestoreInstanceState(Parcelable state) { |
| 482 |
if (state == null || !state.getClass().equals(SavedState.class)) { |
| 483 |
|
| 484 |
super.onRestoreInstanceState(state); |
| 485 |
return; |
| 486 |
} |
| 487 |
|
| 488 |
SavedState myState = (SavedState) state; |
| 489 |
super.onRestoreInstanceState(myState.getSuperState()); |
| 490 |
if (myState.isDialogShowing) { |
| 491 |
showDialog(myState.dialogBundle); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
private static class SavedState extends BaseSavedState { |
| 496 |
boolean isDialogShowing; |
| 497 |
Bundle dialogBundle; |
| 498 |
|
| 499 |
public SavedState(Parcel source) { |
| 500 |
super(source); |
| 501 |
isDialogShowing = source.readInt() == 1; |
| 502 |
dialogBundle = source.readBundle(); |
| 503 |
} |
| 504 |
|
| 505 |
@Override |
| 506 |
public void writeToParcel(Parcel dest, int flags) { |
| 507 |
super.writeToParcel(dest, flags); |
| 508 |
dest.writeInt(isDialogShowing ? 1 : 0); |
| 509 |
dest.writeBundle(dialogBundle); |
| 510 |
} |
| 511 |
|
| 512 |
public SavedState(Parcelable superState) { |
| 513 |
super(superState); |
| 514 |
} |
| 515 |
|
| 516 |
public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR = |
| 517 |
new Parcelable.Creator<SavedState>() { |
| 518 |
public SavedState createFromParcel(Parcel in) { |
| 519 |
return new SavedState(in); |
| 520 |
} |
| 521 |
|
| 522 |
public SavedState[] newArray(int size) { |
| 523 |
return new SavedState[size]; |
| 524 |
} |
| 525 |
}; |
| 526 |
} |
| 527 |
|
| 528 |
} |