| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content.pm; |
| 18 |
|
| 19 |
import static android.content.res.Resources.ID_NULL; |
| 20 |
|
| 21 |
import android.annotation.DrawableRes; |
| 22 |
import android.annotation.IntDef; |
| 23 |
import android.annotation.NonNull; |
| 24 |
import android.annotation.Nullable; |
| 25 |
import android.annotation.StringRes; |
| 26 |
import android.annotation.SystemApi; |
| 27 |
import android.content.res.ResourceId; |
| 28 |
import android.os.Parcel; |
| 29 |
import android.os.Parcelable; |
| 30 |
import android.os.PersistableBundle; |
| 31 |
import android.util.Slog; |
| 32 |
import android.util.TypedXmlPullParser; |
| 33 |
import android.util.TypedXmlSerializer; |
| 34 |
|
| 35 |
import com.android.internal.util.Preconditions; |
| 36 |
import com.android.internal.util.XmlUtils; |
| 37 |
|
| 38 |
import java.io.IOException; |
| 39 |
import java.lang.annotation.Retention; |
| 40 |
import java.lang.annotation.RetentionPolicy; |
| 41 |
import java.util.Locale; |
| 42 |
import java.util.Objects; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
@SystemApi |
| 64 |
public final class SuspendDialogInfo implements Parcelable { |
| 65 |
private static final String TAG = SuspendDialogInfo.class.getSimpleName(); |
| 66 |
private static final String XML_ATTR_ICON_RES_ID = "iconResId"; |
| 67 |
private static final String XML_ATTR_TITLE_RES_ID = "titleResId"; |
| 68 |
private static final String XML_ATTR_TITLE = "title"; |
| 69 |
private static final String XML_ATTR_DIALOG_MESSAGE_RES_ID = "dialogMessageResId"; |
| 70 |
private static final String XML_ATTR_DIALOG_MESSAGE = "dialogMessage"; |
| 71 |
private static final String XML_ATTR_BUTTON_TEXT_RES_ID = "buttonTextResId"; |
| 72 |
private static final String XML_ATTR_BUTTON_TEXT = "buttonText"; |
| 73 |
private static final String XML_ATTR_BUTTON_ACTION = "buttonAction"; |
| 74 |
|
| 75 |
private final int mIconResId; |
| 76 |
private final int mTitleResId; |
| 77 |
private final String mTitle; |
| 78 |
private final int mDialogMessageResId; |
| 79 |
private final String mDialogMessage; |
| 80 |
private final int mNeutralButtonTextResId; |
| 81 |
private final String mNeutralButtonText; |
| 82 |
private final int mNeutralButtonAction; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public static final int BUTTON_ACTION_MORE_DETAILS = 0; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public static final int BUTTON_ACTION_UNSUSPEND = 1; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
@IntDef(flag = true, prefix = {"BUTTON_ACTION_"}, value = { |
| 110 |
BUTTON_ACTION_MORE_DETAILS, |
| 111 |
BUTTON_ACTION_UNSUSPEND |
| 112 |
}) |
| 113 |
@Retention(RetentionPolicy.SOURCE) |
| 114 |
public @interface ButtonAction { |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
@DrawableRes |
| 122 |
public int getIconResId() { |
| 123 |
return mIconResId; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
@StringRes |
| 131 |
public int getTitleResId() { |
| 132 |
return mTitleResId; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
@Nullable |
| 141 |
public String getTitle() { |
| 142 |
return mTitle; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
@StringRes |
| 150 |
public int getDialogMessageResId() { |
| 151 |
return mDialogMessageResId; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
@Nullable |
| 160 |
public String getDialogMessage() { |
| 161 |
return mDialogMessage; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
@StringRes |
| 169 |
public int getNeutralButtonTextResId() { |
| 170 |
return mNeutralButtonTextResId; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
@Nullable |
| 179 |
public String getNeutralButtonText() { |
| 180 |
return mNeutralButtonText; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
@ButtonAction |
| 188 |
public int getNeutralButtonAction() { |
| 189 |
return mNeutralButtonAction; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public void saveToXml(TypedXmlSerializer out) throws IOException { |
| 196 |
if (mIconResId != ID_NULL) { |
| 197 |
out.attributeInt(null, XML_ATTR_ICON_RES_ID, mIconResId); |
| 198 |
} |
| 199 |
if (mTitleResId != ID_NULL) { |
| 200 |
out.attributeInt(null, XML_ATTR_TITLE_RES_ID, mTitleResId); |
| 201 |
} else { |
| 202 |
XmlUtils.writeStringAttribute(out, XML_ATTR_TITLE, mTitle); |
| 203 |
} |
| 204 |
if (mDialogMessageResId != ID_NULL) { |
| 205 |
out.attributeInt(null, XML_ATTR_DIALOG_MESSAGE_RES_ID, mDialogMessageResId); |
| 206 |
} else { |
| 207 |
XmlUtils.writeStringAttribute(out, XML_ATTR_DIALOG_MESSAGE, mDialogMessage); |
| 208 |
} |
| 209 |
if (mNeutralButtonTextResId != ID_NULL) { |
| 210 |
out.attributeInt(null, XML_ATTR_BUTTON_TEXT_RES_ID, mNeutralButtonTextResId); |
| 211 |
} else { |
| 212 |
XmlUtils.writeStringAttribute(out, XML_ATTR_BUTTON_TEXT, mNeutralButtonText); |
| 213 |
} |
| 214 |
out.attributeInt(null, XML_ATTR_BUTTON_ACTION, mNeutralButtonAction); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public static SuspendDialogInfo restoreFromXml(TypedXmlPullParser in) { |
| 221 |
final SuspendDialogInfo.Builder dialogInfoBuilder = new SuspendDialogInfo.Builder(); |
| 222 |
try { |
| 223 |
final int iconId = in.getAttributeInt(null, XML_ATTR_ICON_RES_ID, ID_NULL); |
| 224 |
final int titleId = in.getAttributeInt(null, XML_ATTR_TITLE_RES_ID, ID_NULL); |
| 225 |
final String title = XmlUtils.readStringAttribute(in, XML_ATTR_TITLE); |
| 226 |
final int buttonTextId = |
| 227 |
in.getAttributeInt(null, XML_ATTR_BUTTON_TEXT_RES_ID, ID_NULL); |
| 228 |
final String buttonText = XmlUtils.readStringAttribute(in, XML_ATTR_BUTTON_TEXT); |
| 229 |
final int buttonAction = |
| 230 |
in.getAttributeInt(null, XML_ATTR_BUTTON_ACTION, BUTTON_ACTION_MORE_DETAILS); |
| 231 |
final int dialogMessageResId = |
| 232 |
in.getAttributeInt(null, XML_ATTR_DIALOG_MESSAGE_RES_ID, ID_NULL); |
| 233 |
final String dialogMessage = XmlUtils.readStringAttribute(in, XML_ATTR_DIALOG_MESSAGE); |
| 234 |
|
| 235 |
if (iconId != ID_NULL) { |
| 236 |
dialogInfoBuilder.setIcon(iconId); |
| 237 |
} |
| 238 |
if (titleId != ID_NULL) { |
| 239 |
dialogInfoBuilder.setTitle(titleId); |
| 240 |
} else if (title != null) { |
| 241 |
dialogInfoBuilder.setTitle(title); |
| 242 |
} |
| 243 |
if (buttonTextId != ID_NULL) { |
| 244 |
dialogInfoBuilder.setNeutralButtonText(buttonTextId); |
| 245 |
} else if (buttonText != null) { |
| 246 |
dialogInfoBuilder.setNeutralButtonText(buttonText); |
| 247 |
} |
| 248 |
if (dialogMessageResId != ID_NULL) { |
| 249 |
dialogInfoBuilder.setMessage(dialogMessageResId); |
| 250 |
} else if (dialogMessage != null) { |
| 251 |
dialogInfoBuilder.setMessage(dialogMessage); |
| 252 |
} |
| 253 |
dialogInfoBuilder.setNeutralButtonAction(buttonAction); |
| 254 |
} catch (Exception e) { |
| 255 |
Slog.e(TAG, "Exception while parsing from xml. Some fields may default", e); |
| 256 |
} |
| 257 |
return dialogInfoBuilder.build(); |
| 258 |
} |
| 259 |
|
| 260 |
@Override |
| 261 |
public int hashCode() { |
| 262 |
int hashCode = mIconResId; |
| 263 |
hashCode = 31 * hashCode + mTitleResId; |
| 264 |
hashCode = 31 * hashCode + Objects.hashCode(mTitle); |
| 265 |
hashCode = 31 * hashCode + mNeutralButtonTextResId; |
| 266 |
hashCode = 31 * hashCode + Objects.hashCode(mNeutralButtonText); |
| 267 |
hashCode = 31 * hashCode + mDialogMessageResId; |
| 268 |
hashCode = 31 * hashCode + Objects.hashCode(mDialogMessage); |
| 269 |
hashCode = 31 * hashCode + mNeutralButtonAction; |
| 270 |
return hashCode; |
| 271 |
} |
| 272 |
|
| 273 |
@Override |
| 274 |
public boolean equals(@Nullable Object obj) { |
| 275 |
if (this == obj) { |
| 276 |
return true; |
| 277 |
} |
| 278 |
if (!(obj instanceof SuspendDialogInfo)) { |
| 279 |
return false; |
| 280 |
} |
| 281 |
final SuspendDialogInfo otherDialogInfo = (SuspendDialogInfo) obj; |
| 282 |
return mIconResId == otherDialogInfo.mIconResId |
| 283 |
&& mTitleResId == otherDialogInfo.mTitleResId |
| 284 |
&& Objects.equals(mTitle, otherDialogInfo.mTitle) |
| 285 |
&& mDialogMessageResId == otherDialogInfo.mDialogMessageResId |
| 286 |
&& Objects.equals(mDialogMessage, otherDialogInfo.mDialogMessage) |
| 287 |
&& mNeutralButtonTextResId == otherDialogInfo.mNeutralButtonTextResId |
| 288 |
&& Objects.equals(mNeutralButtonText, otherDialogInfo.mNeutralButtonText) |
| 289 |
&& mNeutralButtonAction == otherDialogInfo.mNeutralButtonAction; |
| 290 |
} |
| 291 |
|
| 292 |
@NonNull |
| 293 |
@Override |
| 294 |
public String toString() { |
| 295 |
final StringBuilder builder = new StringBuilder("SuspendDialogInfo: {"); |
| 296 |
if (mIconResId != ID_NULL) { |
| 297 |
builder.append("mIconId = 0x"); |
| 298 |
builder.append(Integer.toHexString(mIconResId)); |
| 299 |
builder.append(" "); |
| 300 |
} |
| 301 |
if (mTitleResId != ID_NULL) { |
| 302 |
builder.append("mTitleResId = 0x"); |
| 303 |
builder.append(Integer.toHexString(mTitleResId)); |
| 304 |
builder.append(" "); |
| 305 |
} else if (mTitle != null) { |
| 306 |
builder.append("mTitle = \""); |
| 307 |
builder.append(mTitle); |
| 308 |
builder.append("\""); |
| 309 |
} |
| 310 |
if (mNeutralButtonTextResId != ID_NULL) { |
| 311 |
builder.append("mNeutralButtonTextResId = 0x"); |
| 312 |
builder.append(Integer.toHexString(mNeutralButtonTextResId)); |
| 313 |
builder.append(" "); |
| 314 |
} else if (mNeutralButtonText != null) { |
| 315 |
builder.append("mNeutralButtonText = \""); |
| 316 |
builder.append(mNeutralButtonText); |
| 317 |
builder.append("\""); |
| 318 |
} |
| 319 |
if (mDialogMessageResId != ID_NULL) { |
| 320 |
builder.append("mDialogMessageResId = 0x"); |
| 321 |
builder.append(Integer.toHexString(mDialogMessageResId)); |
| 322 |
builder.append(" "); |
| 323 |
} else if (mDialogMessage != null) { |
| 324 |
builder.append("mDialogMessage = \""); |
| 325 |
builder.append(mDialogMessage); |
| 326 |
builder.append("\" "); |
| 327 |
} |
| 328 |
builder.append("mNeutralButtonAction = "); |
| 329 |
builder.append(mNeutralButtonAction); |
| 330 |
builder.append("}"); |
| 331 |
return builder.toString(); |
| 332 |
} |
| 333 |
|
| 334 |
@Override |
| 335 |
public int describeContents() { |
| 336 |
return 0; |
| 337 |
} |
| 338 |
|
| 339 |
@Override |
| 340 |
public void writeToParcel(Parcel dest, int parcelableFlags) { |
| 341 |
dest.writeInt(mIconResId); |
| 342 |
dest.writeInt(mTitleResId); |
| 343 |
dest.writeString(mTitle); |
| 344 |
dest.writeInt(mDialogMessageResId); |
| 345 |
dest.writeString(mDialogMessage); |
| 346 |
dest.writeInt(mNeutralButtonTextResId); |
| 347 |
dest.writeString(mNeutralButtonText); |
| 348 |
dest.writeInt(mNeutralButtonAction); |
| 349 |
} |
| 350 |
|
| 351 |
private SuspendDialogInfo(Parcel source) { |
| 352 |
mIconResId = source.readInt(); |
| 353 |
mTitleResId = source.readInt(); |
| 354 |
mTitle = source.readString(); |
| 355 |
mDialogMessageResId = source.readInt(); |
| 356 |
mDialogMessage = source.readString(); |
| 357 |
mNeutralButtonTextResId = source.readInt(); |
| 358 |
mNeutralButtonText = source.readString(); |
| 359 |
mNeutralButtonAction = source.readInt(); |
| 360 |
} |
| 361 |
|
| 362 |
SuspendDialogInfo(Builder b) { |
| 363 |
mIconResId = b.mIconResId; |
| 364 |
mTitleResId = b.mTitleResId; |
| 365 |
mTitle = (mTitleResId == ID_NULL) ? b.mTitle : null; |
| 366 |
mDialogMessageResId = b.mDialogMessageResId; |
| 367 |
mDialogMessage = (mDialogMessageResId == ID_NULL) ? b.mDialogMessage : null; |
| 368 |
mNeutralButtonTextResId = b.mNeutralButtonTextResId; |
| 369 |
mNeutralButtonText = (mNeutralButtonTextResId == ID_NULL) ? b.mNeutralButtonText : null; |
| 370 |
mNeutralButtonAction = b.mNeutralButtonAction; |
| 371 |
} |
| 372 |
|
| 373 |
public static final @NonNull Creator<SuspendDialogInfo> CREATOR = |
| 374 |
new Creator<SuspendDialogInfo>() { |
| 375 |
@Override |
| 376 |
public SuspendDialogInfo createFromParcel(Parcel source) { |
| 377 |
return new SuspendDialogInfo(source); |
| 378 |
} |
| 379 |
|
| 380 |
@Override |
| 381 |
public SuspendDialogInfo[] newArray(int size) { |
| 382 |
return new SuspendDialogInfo[size]; |
| 383 |
} |
| 384 |
}; |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
public static final class Builder { |
| 390 |
private int mDialogMessageResId = ID_NULL; |
| 391 |
private String mDialogMessage; |
| 392 |
private int mTitleResId = ID_NULL; |
| 393 |
private String mTitle; |
| 394 |
private int mIconResId = ID_NULL; |
| 395 |
private int mNeutralButtonTextResId = ID_NULL; |
| 396 |
private String mNeutralButtonText; |
| 397 |
private int mNeutralButtonAction = BUTTON_ACTION_MORE_DETAILS; |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
@NonNull |
| 406 |
public Builder setIcon(@DrawableRes int resId) { |
| 407 |
Preconditions.checkArgument(ResourceId.isValid(resId), "Invalid resource id provided"); |
| 408 |
mIconResId = resId; |
| 409 |
return this; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
@NonNull |
| 420 |
public Builder setTitle(@StringRes int resId) { |
| 421 |
Preconditions.checkArgument(ResourceId.isValid(resId), "Invalid resource id provided"); |
| 422 |
mTitleResId = resId; |
| 423 |
return this; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
@NonNull |
| 435 |
public Builder setTitle(@NonNull String title) { |
| 436 |
Preconditions.checkStringNotEmpty(title, "Title cannot be null or empty"); |
| 437 |
mTitle = title; |
| 438 |
return this; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
@NonNull |
| 455 |
public Builder setMessage(@NonNull String message) { |
| 456 |
Preconditions.checkStringNotEmpty(message, "Message cannot be null or empty"); |
| 457 |
mDialogMessage = message; |
| 458 |
return this; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
@NonNull |
| 476 |
public Builder setMessage(@StringRes int resId) { |
| 477 |
Preconditions.checkArgument(ResourceId.isValid(resId), "Invalid resource id provided"); |
| 478 |
mDialogMessageResId = resId; |
| 479 |
return this; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
@NonNull |
| 492 |
public Builder setNeutralButtonText(@StringRes int resId) { |
| 493 |
Preconditions.checkArgument(ResourceId.isValid(resId), "Invalid resource id provided"); |
| 494 |
mNeutralButtonTextResId = resId; |
| 495 |
return this; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
@NonNull |
| 507 |
public Builder setNeutralButtonText(@NonNull String neutralButtonText) { |
| 508 |
Preconditions.checkStringNotEmpty(neutralButtonText, |
| 509 |
"Button text cannot be null or empty"); |
| 510 |
mNeutralButtonText = neutralButtonText; |
| 511 |
return this; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
@NonNull |
| 523 |
public Builder setNeutralButtonAction(@ButtonAction int buttonAction) { |
| 524 |
Preconditions.checkArgument(buttonAction == BUTTON_ACTION_MORE_DETAILS |
| 525 |
|| buttonAction == BUTTON_ACTION_UNSUSPEND, "Invalid button action"); |
| 526 |
mNeutralButtonAction = buttonAction; |
| 527 |
return this; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
@NonNull |
| 536 |
public SuspendDialogInfo build() { |
| 537 |
return new SuspendDialogInfo(this); |
| 538 |
} |
| 539 |
} |
| 540 |
} |