Slice.java revision e2a04b3ea41ebec00afe50db89846920235f23cf
1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.app.slice; 18 19import android.annotation.NonNull; 20import android.annotation.Nullable; 21import android.annotation.StringDef; 22import android.app.PendingIntent; 23import android.app.RemoteInput; 24import android.graphics.drawable.Icon; 25import android.net.Uri; 26import android.os.Bundle; 27import android.os.Parcel; 28import android.os.Parcelable; 29 30import com.android.internal.util.ArrayUtils; 31 32import java.lang.annotation.Retention; 33import java.lang.annotation.RetentionPolicy; 34import java.util.ArrayList; 35import java.util.Arrays; 36import java.util.List; 37import java.util.Objects; 38 39/** 40 * A slice is a piece of app content and actions that can be surfaced outside of the app. 41 * 42 * <p>They are constructed using {@link Builder} in a tree structure 43 * that provides the OS some information about how the content should be displayed. 44 */ 45public final class Slice implements Parcelable { 46 47 /** 48 * @hide 49 */ 50 @StringDef(prefix = { "HINT_" }, value = { 51 HINT_TITLE, 52 HINT_LIST, 53 HINT_LIST_ITEM, 54 HINT_LARGE, 55 HINT_ACTIONS, 56 HINT_SELECTED, 57 HINT_NO_TINT, 58 HINT_SHORTCUT, 59 HINT_TOGGLE, 60 HINT_HORIZONTAL, 61 HINT_PARTIAL, 62 HINT_SEE_MORE, 63 HINT_KEYWORDS, 64 HINT_ERROR, 65 HINT_TTL, 66 HINT_LAST_UPDATED, 67 HINT_PERMISSION_REQUEST, 68 }) 69 @Retention(RetentionPolicy.SOURCE) 70 public @interface SliceHint {} 71 /** 72 * @hide 73 */ 74 @StringDef(prefix = { "SUBTYPE_" }, value = { 75 SUBTYPE_COLOR, 76 SUBTYPE_CONTENT_DESCRIPTION, 77 SUBTYPE_MAX, 78 SUBTYPE_MESSAGE, 79 SUBTYPE_PRIORITY, 80 SUBTYPE_RANGE, 81 SUBTYPE_SOURCE, 82 SUBTYPE_TOGGLE, 83 SUBTYPE_VALUE, 84 SUBTYPE_LAYOUT_DIRECTION, 85 }) 86 @Retention(RetentionPolicy.SOURCE) 87 public @interface SliceSubtype {} 88 89 /** 90 * Hint that this content is a title of other content in the slice. This can also indicate that 91 * the content should be used in the shortcut representation of the slice (icon, label, action), 92 * normally this should be indicated by adding the hint on the action containing that content. 93 * 94 * @see SliceItem#FORMAT_ACTION 95 */ 96 public static final String HINT_TITLE = "title"; 97 /** 98 * Hint that all sub-items/sub-slices within this content should be considered 99 * to have {@link #HINT_LIST_ITEM}. 100 */ 101 public static final String HINT_LIST = "list"; 102 /** 103 * Hint that this item is part of a list and should be formatted as if is part 104 * of a list. 105 */ 106 public static final String HINT_LIST_ITEM = "list_item"; 107 /** 108 * Hint that this content is important and should be larger when displayed if 109 * possible. 110 */ 111 public static final String HINT_LARGE = "large"; 112 /** 113 * Hint that this slice contains a number of actions that can be grouped together 114 * in a sort of controls area of the UI. 115 */ 116 public static final String HINT_ACTIONS = "actions"; 117 /** 118 * Hint indicating that this item (and its sub-items) are the current selection. 119 */ 120 public static final String HINT_SELECTED = "selected"; 121 /** 122 * Hint to indicate that this content should not be tinted. 123 */ 124 public static final String HINT_NO_TINT = "no_tint"; 125 /** 126 * Hint to indicate that this content should only be displayed if the slice is presented 127 * as a shortcut. 128 */ 129 public static final String HINT_SHORTCUT = "shortcut"; 130 /** 131 * Hint indicating this content should be shown instead of the normal content when the slice 132 * is in small format. 133 */ 134 public static final String HINT_SUMMARY = "summary"; 135 /** 136 * Hint to indicate that this content has a toggle action associated with it. To indicate that 137 * the toggle is on, use {@link #HINT_SELECTED}. When the toggle state changes, the intent 138 * associated with it will be sent along with an extra {@link #EXTRA_TOGGLE_STATE} which can be 139 * retrieved to see the new state of the toggle. 140 * @hide 141 */ 142 public static final String HINT_TOGGLE = "toggle"; 143 /** 144 * Hint that list items within this slice or subslice would appear better 145 * if organized horizontally. 146 */ 147 public static final String HINT_HORIZONTAL = "horizontal"; 148 /** 149 * Hint to indicate that this slice is incomplete and an update will be sent once 150 * loading is complete. Slices which contain HINT_PARTIAL will not be cached by the 151 * OS and should not be cached by apps. 152 */ 153 public static final String HINT_PARTIAL = "partial"; 154 /** 155 * A hint representing that this item should be used to indicate that there's more 156 * content associated with this slice. 157 */ 158 public static final String HINT_SEE_MORE = "see_more"; 159 /** 160 * @see Builder#setCallerNeeded 161 * @hide 162 */ 163 public static final String HINT_CALLER_NEEDED = "caller_needed"; 164 /** 165 * A hint to indicate that the contents of this subslice represent a list of keywords 166 * related to the parent slice. 167 * Expected to be on an item of format {@link SliceItem#FORMAT_SLICE}. 168 */ 169 public static final String HINT_KEYWORDS = "keywords"; 170 /** 171 * A hint to indicate that this slice represents an error. 172 */ 173 public static final String HINT_ERROR = "error"; 174 /** 175 * Hint indicating an item representing a time-to-live for the content. 176 */ 177 public static final String HINT_TTL = "ttl"; 178 /** 179 * Hint indicating an item representing when the content was created or last updated. 180 */ 181 public static final String HINT_LAST_UPDATED = "last_updated"; 182 /** 183 * A hint to indicate that this slice represents a permission request for showing 184 * slices. 185 */ 186 public static final String HINT_PERMISSION_REQUEST = "permission_request"; 187 /** 188 * Subtype to indicate that this item indicates the layout direction for content 189 * in the slice. 190 * Expected to be an item of format {@link SliceItem#FORMAT_INT}. 191 */ 192 public static final String SUBTYPE_LAYOUT_DIRECTION = "layout_direction"; 193 /** 194 * Key to retrieve an extra added to an intent when a control is changed. 195 */ 196 public static final String EXTRA_TOGGLE_STATE = "android.app.slice.extra.TOGGLE_STATE"; 197 /** 198 * Key to retrieve an extra added to an intent when the value of a slider is changed. 199 * @deprecated remove once support lib is update to use EXTRA_RANGE_VALUE instead 200 */ 201 @Deprecated 202 public static final String EXTRA_SLIDER_VALUE = "android.app.slice.extra.SLIDER_VALUE"; 203 /** 204 * Key to retrieve an extra added to an intent when the value of an input range is changed. 205 */ 206 public static final String EXTRA_RANGE_VALUE = "android.app.slice.extra.RANGE_VALUE"; 207 /** 208 * Subtype to indicate that this is a message as part of a communication 209 * sequence in this slice. 210 * Expected to be on an item of format {@link SliceItem#FORMAT_SLICE}. 211 */ 212 public static final String SUBTYPE_MESSAGE = "message"; 213 /** 214 * Subtype to tag the source (i.e. sender) of a {@link #SUBTYPE_MESSAGE}. 215 * Expected to be on an item of format {@link SliceItem#FORMAT_TEXT}, 216 * {@link SliceItem#FORMAT_IMAGE} or an {@link SliceItem#FORMAT_SLICE} containing them. 217 */ 218 public static final String SUBTYPE_SOURCE = "source"; 219 /** 220 * Subtype to tag an item as representing a color. 221 * Expected to be on an item of format {@link SliceItem#FORMAT_INT}. 222 */ 223 public static final String SUBTYPE_COLOR = "color"; 224 /** 225 * Subtype to tag an item as representing a slider. 226 * @deprecated remove once support lib is update to use SUBTYPE_RANGE instead 227 */ 228 @Deprecated 229 public static final String SUBTYPE_SLIDER = "slider"; 230 /** 231 * Subtype to tag an item as representing a range. 232 * Expected to be on an item of format {@link SliceItem#FORMAT_SLICE} containing 233 * a {@link #SUBTYPE_VALUE} and possibly a {@link #SUBTYPE_MAX}. 234 */ 235 public static final String SUBTYPE_RANGE = "range"; 236 /** 237 * Subtype to tag an item as representing the max int value for a {@link #SUBTYPE_RANGE}. 238 * Expected to be on an item of format {@link SliceItem#FORMAT_INT}. 239 */ 240 public static final String SUBTYPE_MAX = "max"; 241 /** 242 * Subtype to tag an item as representing the current int value for a {@link #SUBTYPE_RANGE}. 243 * Expected to be on an item of format {@link SliceItem#FORMAT_INT}. 244 */ 245 public static final String SUBTYPE_VALUE = "value"; 246 /** 247 * Subtype to indicate that this content has a toggle action associated with it. To indicate 248 * that the toggle is on, use {@link #HINT_SELECTED}. When the toggle state changes, the 249 * intent associated with it will be sent along with an extra {@link #EXTRA_TOGGLE_STATE} 250 * which can be retrieved to see the new state of the toggle. 251 */ 252 public static final String SUBTYPE_TOGGLE = "toggle"; 253 /** 254 * Subtype to tag an item representing priority. 255 * Expected to be on an item of format {@link SliceItem#FORMAT_INT}. 256 */ 257 public static final String SUBTYPE_PRIORITY = "priority"; 258 /** 259 * Subtype to tag an item to use as a content description. 260 * Expected to be on an item of format {@link SliceItem#FORMAT_TEXT}. 261 */ 262 public static final String SUBTYPE_CONTENT_DESCRIPTION = "content_description"; 263 /** 264 * Subtype to tag an item as representing a time in milliseconds since midnight, 265 * January 1, 1970 UTC. 266 */ 267 public static final String SUBTYPE_MILLIS = "millis"; 268 269 private final SliceItem[] mItems; 270 private final @SliceHint String[] mHints; 271 private SliceSpec mSpec; 272 private Uri mUri; 273 274 Slice(ArrayList<SliceItem> items, @SliceHint String[] hints, Uri uri, SliceSpec spec) { 275 mHints = hints; 276 mItems = items.toArray(new SliceItem[items.size()]); 277 mUri = uri; 278 mSpec = spec; 279 } 280 281 protected Slice(Parcel in) { 282 mHints = in.readStringArray(); 283 int n = in.readInt(); 284 mItems = new SliceItem[n]; 285 for (int i = 0; i < n; i++) { 286 mItems[i] = SliceItem.CREATOR.createFromParcel(in); 287 } 288 mUri = Uri.CREATOR.createFromParcel(in); 289 mSpec = in.readTypedObject(SliceSpec.CREATOR); 290 } 291 292 /** 293 * @return The spec for this slice 294 */ 295 public @Nullable SliceSpec getSpec() { 296 return mSpec; 297 } 298 299 /** 300 * @return The Uri that this Slice represents. 301 */ 302 public Uri getUri() { 303 return mUri; 304 } 305 306 /** 307 * @return All child {@link SliceItem}s that this Slice contains. 308 */ 309 public List<SliceItem> getItems() { 310 return Arrays.asList(mItems); 311 } 312 313 /** 314 * @return All hints associated with this Slice. 315 */ 316 public @SliceHint List<String> getHints() { 317 return Arrays.asList(mHints); 318 } 319 320 @Override 321 public void writeToParcel(Parcel dest, int flags) { 322 dest.writeStringArray(mHints); 323 dest.writeInt(mItems.length); 324 for (int i = 0; i < mItems.length; i++) { 325 mItems[i].writeToParcel(dest, flags); 326 } 327 mUri.writeToParcel(dest, 0); 328 dest.writeTypedObject(mSpec, flags); 329 } 330 331 @Override 332 public int describeContents() { 333 return 0; 334 } 335 336 /** 337 * @hide 338 */ 339 public boolean hasHint(@SliceHint String hint) { 340 return ArrayUtils.contains(mHints, hint); 341 } 342 343 /** 344 * Returns whether the caller for this slice matters. 345 * @see Builder#setCallerNeeded 346 */ 347 public boolean isCallerNeeded() { 348 return hasHint(HINT_CALLER_NEEDED); 349 } 350 351 /** 352 * A Builder used to construct {@link Slice}s 353 */ 354 public static class Builder { 355 356 private final Uri mUri; 357 private ArrayList<SliceItem> mItems = new ArrayList<>(); 358 private @SliceHint ArrayList<String> mHints = new ArrayList<>(); 359 private SliceSpec mSpec; 360 361 /** 362 * @deprecated TO BE REMOVED 363 */ 364 @Deprecated 365 public Builder(@NonNull Uri uri) { 366 mUri = uri; 367 } 368 369 /** 370 * Create a builder which will construct a {@link Slice} for the given Uri. 371 * @param uri Uri to tag for this slice. 372 * @param spec the spec for this slice. 373 */ 374 public Builder(@NonNull Uri uri, SliceSpec spec) { 375 mUri = uri; 376 mSpec = spec; 377 } 378 379 /** 380 * Create a builder for a {@link Slice} that is a sub-slice of the slice 381 * being constructed by the provided builder. 382 * @param parent The builder constructing the parent slice 383 */ 384 public Builder(@NonNull Slice.Builder parent) { 385 mUri = parent.mUri.buildUpon().appendPath("_gen") 386 .appendPath(String.valueOf(mItems.size())).build(); 387 } 388 389 /** 390 * Tells the system whether for this slice the return value of 391 * {@link SliceProvider#onBindSlice(Uri, List)} may be different depending on 392 * {@link SliceProvider#getCallingPackage()} and should not be cached for multiple 393 * apps. 394 */ 395 public Builder setCallerNeeded(boolean callerNeeded) { 396 if (callerNeeded) { 397 mHints.add(HINT_CALLER_NEEDED); 398 } else { 399 mHints.remove(HINT_CALLER_NEEDED); 400 } 401 return this; 402 } 403 404 /** 405 * Add hints to the Slice being constructed 406 */ 407 public Builder addHints(@SliceHint List<String> hints) { 408 mHints.addAll(hints); 409 return this; 410 } 411 412 /** 413 * @deprecated TO BE REMOVED 414 */ 415 public Builder setSpec(SliceSpec spec) { 416 mSpec = spec; 417 return this; 418 } 419 420 /** 421 * Add a sub-slice to the slice being constructed 422 * @param subType Optional template-specific type information 423 * @see {@link SliceItem#getSubType()} 424 */ 425 public Builder addSubSlice(@NonNull Slice slice, @Nullable @SliceSubtype String subType) { 426 mItems.add(new SliceItem(slice, SliceItem.FORMAT_SLICE, subType, 427 slice.getHints().toArray(new String[slice.getHints().size()]))); 428 return this; 429 } 430 431 /** 432 * Add an action to the slice being constructed 433 * @param subType Optional template-specific type information 434 * @see {@link SliceItem#getSubType()} 435 */ 436 public Slice.Builder addAction(@NonNull PendingIntent action, @NonNull Slice s, 437 @Nullable @SliceSubtype String subType) { 438 List<String> hints = s.getHints(); 439 s.mSpec = null; 440 mItems.add(new SliceItem(action, s, SliceItem.FORMAT_ACTION, subType, hints.toArray( 441 new String[hints.size()]))); 442 return this; 443 } 444 445 /** 446 * Add text to the slice being constructed 447 * @param subType Optional template-specific type information 448 * @see {@link SliceItem#getSubType()} 449 */ 450 public Builder addText(CharSequence text, @Nullable @SliceSubtype String subType, 451 @SliceHint List<String> hints) { 452 mItems.add(new SliceItem(text, SliceItem.FORMAT_TEXT, subType, hints)); 453 return this; 454 } 455 456 /** 457 * Add an image to the slice being constructed 458 * @param subType Optional template-specific type information 459 * @see {@link SliceItem#getSubType()} 460 */ 461 public Builder addIcon(Icon icon, @Nullable @SliceSubtype String subType, 462 @SliceHint List<String> hints) { 463 mItems.add(new SliceItem(icon, SliceItem.FORMAT_IMAGE, subType, hints)); 464 return this; 465 } 466 467 /** 468 * Add remote input to the slice being constructed 469 * @param subType Optional template-specific type information 470 * @see {@link SliceItem#getSubType()} 471 */ 472 public Slice.Builder addRemoteInput(RemoteInput remoteInput, 473 @Nullable @SliceSubtype String subType, 474 @SliceHint List<String> hints) { 475 mItems.add(new SliceItem(remoteInput, SliceItem.FORMAT_REMOTE_INPUT, 476 subType, hints)); 477 return this; 478 } 479 480 /** 481 * Add an integer to the slice being constructed 482 * @param subType Optional template-specific type information 483 * @see {@link SliceItem#getSubType()} 484 */ 485 public Builder addInt(int value, @Nullable @SliceSubtype String subType, 486 @SliceHint List<String> hints) { 487 mItems.add(new SliceItem(value, SliceItem.FORMAT_INT, subType, hints)); 488 return this; 489 } 490 491 /** 492 * @deprecated TO BE REMOVED. 493 */ 494 @Deprecated 495 public Slice.Builder addTimestamp(long time, @Nullable @SliceSubtype String subType, 496 @SliceHint List<String> hints) { 497 return addLong(time, subType, hints); 498 } 499 500 /** 501 * Add a long to the slice being constructed 502 * @param subType Optional template-specific type information 503 * @see {@link SliceItem#getSubType()} 504 */ 505 public Slice.Builder addLong(long value, @Nullable @SliceSubtype String subType, 506 @SliceHint List<String> hints) { 507 mItems.add(new SliceItem(value, SliceItem.FORMAT_LONG, subType, 508 hints.toArray(new String[hints.size()]))); 509 return this; 510 } 511 512 /** 513 * Add a bundle to the slice being constructed. 514 * <p>Expected to be used for support library extension, should not be used for general 515 * development 516 * @param subType Optional template-specific type information 517 * @see {@link SliceItem#getSubType()} 518 */ 519 public Slice.Builder addBundle(Bundle bundle, @Nullable @SliceSubtype String subType, 520 @SliceHint List<String> hints) { 521 mItems.add(new SliceItem(bundle, SliceItem.FORMAT_BUNDLE, subType, 522 hints)); 523 return this; 524 } 525 526 /** 527 * Construct the slice. 528 */ 529 public Slice build() { 530 return new Slice(mItems, mHints.toArray(new String[mHints.size()]), mUri, mSpec); 531 } 532 } 533 534 public static final Creator<Slice> CREATOR = new Creator<Slice>() { 535 @Override 536 public Slice createFromParcel(Parcel in) { 537 return new Slice(in); 538 } 539 540 @Override 541 public Slice[] newArray(int size) { 542 return new Slice[size]; 543 } 544 }; 545 546 /** 547 * @hide 548 * @return A string representation of this slice. 549 */ 550 public String toString() { 551 return toString(""); 552 } 553 554 private String toString(String indent) { 555 StringBuilder sb = new StringBuilder(); 556 for (int i = 0; i < mItems.length; i++) { 557 sb.append(indent); 558 if (Objects.equals(mItems[i].getFormat(), SliceItem.FORMAT_SLICE)) { 559 sb.append("slice:\n"); 560 sb.append(mItems[i].getSlice().toString(indent + " ")); 561 } else if (Objects.equals(mItems[i].getFormat(), SliceItem.FORMAT_TEXT)) { 562 sb.append("text: "); 563 sb.append(mItems[i].getText()); 564 sb.append("\n"); 565 } else { 566 sb.append(mItems[i].getFormat()); 567 sb.append("\n"); 568 } 569 } 570 return sb.toString(); 571 } 572} 573