1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.content.pm;
import static android.content.res.Resources.ID_NULL;
import android.annotation.DrawableRes;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.StringRes;
import android.annotation.SystemApi;
import android.content.res.ResourceId;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.PersistableBundle;
import android.util.Slog;
import android.util.TypedXmlPullParser;
import android.util.TypedXmlSerializer;
import com.android.internal.util.Preconditions;
import com.android.internal.util.XmlUtils;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Locale;
import java.util.Objects;
/**
* A container to describe the dialog to be shown when the user tries to launch a suspended
* application. The suspending app can customize the dialog's following attributes:
* <ul>
* <li>The dialog icon, by providing a resource id.
* <li>The title text, by providing a resource id.
* <li>The text of the dialog's body, by providing a resource id or a string.
* <li>The text on the neutral button by providing a resource id.
* <li>The action performed on tapping the neutral button. Only {@link #BUTTON_ACTION_UNSUSPEND}
* and {@link #BUTTON_ACTION_MORE_DETAILS} are currently supported.
* </ul>
* System defaults are used whenever any of these are not provided, or any of the provided resource
* ids cannot be resolved at the time of displaying the dialog.
*
* @hide
* @see PackageManager#setPackagesSuspended(String[], boolean, PersistableBundle, PersistableBundle,
* SuspendDialogInfo)
* @see Builder
*/
@SystemApi
public final class SuspendDialogInfo implements Parcelable {
private static final String TAG = SuspendDialogInfo.class.getSimpleName();
private static final String XML_ATTR_ICON_RES_ID = "iconResId";
private static final String XML_ATTR_TITLE_RES_ID = "titleResId";
private static final String XML_ATTR_TITLE = "title";
private static final String XML_ATTR_DIALOG_MESSAGE_RES_ID = "dialogMessageResId";
private static final String XML_ATTR_DIALOG_MESSAGE = "dialogMessage";
private static final String XML_ATTR_BUTTON_TEXT_RES_ID = "buttonTextResId";
private static final String XML_ATTR_BUTTON_TEXT = "buttonText";
private static final String XML_ATTR_BUTTON_ACTION = "buttonAction";
private final int mIconResId;
private final int mTitleResId;
private final String mTitle;
private final int mDialogMessageResId;
private final String mDialogMessage;
private final int mNeutralButtonTextResId;
private final String mNeutralButtonText;
private final int mNeutralButtonAction;
/**
* Used with {@link Builder#setNeutralButtonAction(int)} to create a neutral button that
* starts the {@link android.content.Intent#ACTION_SHOW_SUSPENDED_APP_DETAILS} activity.
* @see Builder#setNeutralButtonAction(int)
*/
public static final int BUTTON_ACTION_MORE_DETAILS = 0;
/**
* Used with {@link Builder#setNeutralButtonAction(int)} to create a neutral button that
* unsuspends the app that the user was trying to launch and continues with the launch. The
* system also sends the broadcast
* {@link android.content.Intent#ACTION_PACKAGE_UNSUSPENDED_MANUALLY} to the suspending app
* when this happens.
* @see Builder#setNeutralButtonAction(int)
* @see android.content.Intent#ACTION_PACKAGE_UNSUSPENDED_MANUALLY
*/
public static final int BUTTON_ACTION_UNSUSPEND = 1;
/**
* Button actions to specify what happens when the user taps on the neutral button.
* To be used with {@link Builder#setNeutralButtonAction(int)}.
*
* @hide
* @see Builder#setNeutralButtonAction(int)
*/
@IntDef(flag = true, prefix = {"BUTTON_ACTION_"}, value = {
BUTTON_ACTION_MORE_DETAILS,
BUTTON_ACTION_UNSUSPEND
})
@Retention(RetentionPolicy.SOURCE)
public @interface ButtonAction {
}
/**
* @return the resource id of the icon to be used with the dialog
* @hide
*/
@DrawableRes
public int getIconResId() {
return mIconResId;
}
/**
* @return the resource id of the title to be used with the dialog
* @hide
*/
@StringRes
public int getTitleResId() {
return mTitleResId;
}
/**
* @return the title to be shown on the dialog. Returns {@code null} if {@link #getTitleResId()}
* returns a valid resource id
* @hide
*/
@Nullable
public String getTitle() {
return mTitle;
}
/**
* @return the resource id of the text to be shown in the dialog's body
* @hide
*/
@StringRes
public int getDialogMessageResId() {
return mDialogMessageResId;
}
/**
* @return the text to be shown in the dialog's body. Returns {@code null} if {@link
* #getDialogMessageResId()} returns a valid resource id
* @hide
*/
@Nullable
public String getDialogMessage() {
return mDialogMessage;
}
/**
* @return the text to be shown on the neutral button
* @hide
*/
@StringRes
public int getNeutralButtonTextResId() {
return mNeutralButtonTextResId;
}
/**
* @return the text to be shown on the neutral button. Returns {@code null} if
* {@link #getNeutralButtonTextResId()} returns a valid resource id
* @hide
*/
@Nullable
public String getNeutralButtonText() {
return mNeutralButtonText;
}
/**
* @return The {@link ButtonAction} that happens on tapping this button
* @hide
*/
@ButtonAction
public int getNeutralButtonAction() {
return mNeutralButtonAction;
}
/**
* @hide
*/
public void saveToXml(TypedXmlSerializer out) throws IOException {
if (mIconResId != ID_NULL) {
out.attributeInt(null, XML_ATTR_ICON_RES_ID, mIconResId);
}
if (mTitleResId != ID_NULL) {
out.attributeInt(null, XML_ATTR_TITLE_RES_ID, mTitleResId);
} else {
XmlUtils.writeStringAttribute(out, XML_ATTR_TITLE, mTitle);
}
if (mDialogMessageResId != ID_NULL) {
out.attributeInt(null, XML_ATTR_DIALOG_MESSAGE_RES_ID, mDialogMessageResId);
} else {
XmlUtils.writeStringAttribute(out, XML_ATTR_DIALOG_MESSAGE, mDialogMessage);
}
if (mNeutralButtonTextResId != ID_NULL) {
out.attributeInt(null, XML_ATTR_BUTTON_TEXT_RES_ID, mNeutralButtonTextResId);
} else {
XmlUtils.writeStringAttribute(out, XML_ATTR_BUTTON_TEXT, mNeutralButtonText);
}
out.attributeInt(null, XML_ATTR_BUTTON_ACTION, mNeutralButtonAction);
}
/**
* @hide
*/
public static SuspendDialogInfo restoreFromXml(TypedXmlPullParser in) {
final SuspendDialogInfo.Builder dialogInfoBuilder = new SuspendDialogInfo.Builder();
try {
final int iconId = in.getAttributeInt(null, XML_ATTR_ICON_RES_ID, ID_NULL);
final int titleId = in.getAttributeInt(null, XML_ATTR_TITLE_RES_ID, ID_NULL);
final String title = XmlUtils.readStringAttribute(in, XML_ATTR_TITLE);
final int buttonTextId =
in.getAttributeInt(null, XML_ATTR_BUTTON_TEXT_RES_ID, ID_NULL);
final String buttonText = XmlUtils.readStringAttribute(in, XML_ATTR_BUTTON_TEXT);
final int buttonAction =
in.getAttributeInt(null, XML_ATTR_BUTTON_ACTION, BUTTON_ACTION_MORE_DETAILS);
final int dialogMessageResId =
in.getAttributeInt(null, XML_ATTR_DIALOG_MESSAGE_RES_ID, ID_NULL);
final String dialogMessage = XmlUtils.readStringAttribute(in, XML_ATTR_DIALOG_MESSAGE);
if (iconId != ID_NULL) {
dialogInfoBuilder.setIcon(iconId);
}
if (titleId != ID_NULL) {
dialogInfoBuilder.setTitle(titleId);
} else if (title != null) {
dialogInfoBuilder.setTitle(title);
}
if (buttonTextId != ID_NULL) {
dialogInfoBuilder.setNeutralButtonText(buttonTextId);
} else if (buttonText != null) {
dialogInfoBuilder.setNeutralButtonText(buttonText);
}
if (dialogMessageResId != ID_NULL) {
dialogInfoBuilder.setMessage(dialogMessageResId);
} else if (dialogMessage != null) {
dialogInfoBuilder.setMessage(dialogMessage);
}
dialogInfoBuilder.setNeutralButtonAction(buttonAction);
} catch (Exception e) {
Slog.e(TAG, "Exception while parsing from xml. Some fields may default", e);
}
return dialogInfoBuilder.build();
}
@Override
public int hashCode() {
int hashCode = mIconResId;
hashCode = 31 * hashCode + mTitleResId;
hashCode = 31 * hashCode + Objects.hashCode(mTitle);
hashCode = 31 * hashCode + mNeutralButtonTextResId;
hashCode = 31 * hashCode + Objects.hashCode(mNeutralButtonText);
hashCode = 31 * hashCode + mDialogMessageResId;
hashCode = 31 * hashCode + Objects.hashCode(mDialogMessage);
hashCode = 31 * hashCode + mNeutralButtonAction;
return hashCode;
}
@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof SuspendDialogInfo)) {
return false;
}
final SuspendDialogInfo otherDialogInfo = (SuspendDialogInfo) obj;
return mIconResId == otherDialogInfo.mIconResId
&& mTitleResId == otherDialogInfo.mTitleResId
&& Objects.equals(mTitle, otherDialogInfo.mTitle)
&& mDialogMessageResId == otherDialogInfo.mDialogMessageResId
&& Objects.equals(mDialogMessage, otherDialogInfo.mDialogMessage)
&& mNeutralButtonTextResId == otherDialogInfo.mNeutralButtonTextResId
&& Objects.equals(mNeutralButtonText, otherDialogInfo.mNeutralButtonText)
&& mNeutralButtonAction == otherDialogInfo.mNeutralButtonAction;
}
@NonNull
@Override
public String toString() {
final StringBuilder builder = new StringBuilder("SuspendDialogInfo: {");
if (mIconResId != ID_NULL) {
builder.append("mIconId = 0x");
builder.append(Integer.toHexString(mIconResId));
builder.append(" ");
}
if (mTitleResId != ID_NULL) {
builder.append("mTitleResId = 0x");
builder.append(Integer.toHexString(mTitleResId));
builder.append(" ");
} else if (mTitle != null) {
builder.append("mTitle = \"");
builder.append(mTitle);
builder.append("\"");
}
if (mNeutralButtonTextResId != ID_NULL) {
builder.append("mNeutralButtonTextResId = 0x");
builder.append(Integer.toHexString(mNeutralButtonTextResId));
builder.append(" ");
} else if (mNeutralButtonText != null) {
builder.append("mNeutralButtonText = \"");
builder.append(mNeutralButtonText);
builder.append("\"");
}
if (mDialogMessageResId != ID_NULL) {
builder.append("mDialogMessageResId = 0x");
builder.append(Integer.toHexString(mDialogMessageResId));
builder.append(" ");
} else if (mDialogMessage != null) {
builder.append("mDialogMessage = \"");
builder.append(mDialogMessage);
builder.append("\" ");
}
builder.append("mNeutralButtonAction = ");
builder.append(mNeutralButtonAction);
builder.append("}");
return builder.toString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int parcelableFlags) {
dest.writeInt(mIconResId);
dest.writeInt(mTitleResId);
dest.writeString(mTitle);
dest.writeInt(mDialogMessageResId);
dest.writeString(mDialogMessage);
dest.writeInt(mNeutralButtonTextResId);
dest.writeString(mNeutralButtonText);
dest.writeInt(mNeutralButtonAction);
}
private SuspendDialogInfo(Parcel source) {
mIconResId = source.readInt();
mTitleResId = source.readInt();
mTitle = source.readString();
mDialogMessageResId = source.readInt();
mDialogMessage = source.readString();
mNeutralButtonTextResId = source.readInt();
mNeutralButtonText = source.readString();
mNeutralButtonAction = source.readInt();
}
SuspendDialogInfo(Builder b) {
mIconResId = b.mIconResId;
mTitleResId = b.mTitleResId;
mTitle = (mTitleResId == ID_NULL) ? b.mTitle : null;
mDialogMessageResId = b.mDialogMessageResId;
mDialogMessage = (mDialogMessageResId == ID_NULL) ? b.mDialogMessage : null;
mNeutralButtonTextResId = b.mNeutralButtonTextResId;
mNeutralButtonText = (mNeutralButtonTextResId == ID_NULL) ? b.mNeutralButtonText : null;
mNeutralButtonAction = b.mNeutralButtonAction;
}
public static final @NonNull Creator<SuspendDialogInfo> CREATOR =
new Creator<SuspendDialogInfo>() {
@Override
public SuspendDialogInfo createFromParcel(Parcel source) {
return new SuspendDialogInfo(source);
}
@Override
public SuspendDialogInfo[] newArray(int size) {
return new SuspendDialogInfo[size];
}
};
/**
* Builder to build a {@link SuspendDialogInfo} object.
*/
public static final class Builder {
private int mDialogMessageResId = ID_NULL;
private String mDialogMessage;
private int mTitleResId = ID_NULL;
private String mTitle;
private int mIconResId = ID_NULL;
private int mNeutralButtonTextResId = ID_NULL;
private String mNeutralButtonText;
private int mNeutralButtonAction = BUTTON_ACTION_MORE_DETAILS;
/**
* Set the resource id of the icon to be used. If not provided, no icon will be shown.
*
* @param resId The resource id of the icon.
* @return this builder object.
*/
@NonNull
public Builder setIcon(@DrawableRes int resId) {
Preconditions.checkArgument(ResourceId.isValid(resId), "Invalid resource id provided");
mIconResId = resId;
return this;
}
/**
* Set the resource id of the title text to be displayed. If this is not provided, the
* system will use a default title.
*
* @param resId The resource id of the title.
* @return this builder object.
*/
@NonNull
public Builder setTitle(@StringRes int resId) {
Preconditions.checkArgument(ResourceId.isValid(resId), "Invalid resource id provided");
mTitleResId = resId;
return this;
}
/**
* Set the title text of the dialog. Ignored if a resource id is set via
* {@link #setTitle(int)}
*
* @param title The title of the dialog.
* @return this builder object.
* @see #setTitle(int)
*/
@NonNull
public Builder setTitle(@NonNull String title) {
Preconditions.checkStringNotEmpty(title, "Title cannot be null or empty");
mTitle = title;
return this;
}
/**
* Set the text to show in the body of the dialog. Ignored if a resource id is set via
* {@link #setMessage(int)}.
* <p>
* The system will use {@link String#format(Locale, String, Object...) String.format} to
* insert the suspended app name into the message, so an example format string could be
* {@code "The app %1$s is currently suspended"}. This is optional - if the string passed in
* {@code message} does not accept an argument, it will be used as is.
*
* @param message The dialog message.
* @return this builder object.
* @see #setMessage(int)
*/
@NonNull
public Builder setMessage(@NonNull String message) {
Preconditions.checkStringNotEmpty(message, "Message cannot be null or empty");
mDialogMessage = message;
return this;
}
/**
* Set the resource id of the dialog message to be shown. If no dialog message is provided
* via either this method or {@link #setMessage(String)}, the system will use a default
* message.
* <p>
* The system will use {@link android.content.res.Resources#getString(int, Object...)
* getString} to insert the suspended app name into the message, so an example format string
* could be {@code "The app %1$s is currently suspended"}. This is optional - if the string
* referred to by {@code resId} does not accept an argument, it will be used as is.
*
* @param resId The resource id of the dialog message.
* @return this builder object.
* @see #setMessage(String)
*/
@NonNull
public Builder setMessage(@StringRes int resId) {
Preconditions.checkArgument(ResourceId.isValid(resId), "Invalid resource id provided");
mDialogMessageResId = resId;
return this;
}
/**
* Set the resource id of text to be shown on the neutral button. Tapping this button would
* perform the {@link ButtonAction action} specified through
* {@link #setNeutralButtonAction(int)}. If this is not provided, the system will use a
* default text.
*
* @param resId The resource id of the button text
* @return this builder object.
*/
@NonNull
public Builder setNeutralButtonText(@StringRes int resId) {
Preconditions.checkArgument(ResourceId.isValid(resId), "Invalid resource id provided");
mNeutralButtonTextResId = resId;
return this;
}
/**
* Set the text to be shown on the neutral button. Ignored if a resource id is set via
* {@link #setNeutralButtonText(int)}
*
* @param neutralButtonText The title of the dialog.
* @return this builder object.
* @see #setNeutralButtonText(int)
*/
@NonNull
public Builder setNeutralButtonText(@NonNull String neutralButtonText) {
Preconditions.checkStringNotEmpty(neutralButtonText,
"Button text cannot be null or empty");
mNeutralButtonText = neutralButtonText;
return this;
}
/**
* Set the action expected to happen on neutral button tap. Defaults to
* {@link #BUTTON_ACTION_MORE_DETAILS} if this is not provided.
*
* @param buttonAction Either {@link #BUTTON_ACTION_MORE_DETAILS} or
* {@link #BUTTON_ACTION_UNSUSPEND}.
* @return this builder object
*/
@NonNull
public Builder setNeutralButtonAction(@ButtonAction int buttonAction) {
Preconditions.checkArgument(buttonAction == BUTTON_ACTION_MORE_DETAILS
|| buttonAction == BUTTON_ACTION_UNSUSPEND, "Invalid button action");
mNeutralButtonAction = buttonAction;
return this;
}
/**
* Build the final object based on given inputs.
*
* @return The {@link SuspendDialogInfo} object built using this builder.
*/
@NonNull
public SuspendDialogInfo build() {
return new SuspendDialogInfo(this);
}
}
}
|