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 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
|
/*
* Copyright (C) 2017 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.app.slice;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.StringDef;
import android.app.PendingIntent;
import android.app.RemoteInput;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.Preconditions;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* A slice is a piece of app content and actions that can be surfaced outside of the app.
*
* <p>They are constructed using {@link Builder} in a tree structure
* that provides the OS some information about how the content should be displayed.
*/
public final class Slice implements Parcelable {
/**
* @hide
*/
@StringDef(prefix = { "HINT_" }, value = {
HINT_TITLE,
HINT_LIST,
HINT_LIST_ITEM,
HINT_LARGE,
HINT_ACTIONS,
HINT_SELECTED,
HINT_NO_TINT,
HINT_SHORTCUT,
HINT_TOGGLE,
HINT_HORIZONTAL,
HINT_PARTIAL,
HINT_SEE_MORE,
HINT_KEYWORDS,
HINT_ERROR,
HINT_TTL,
HINT_LAST_UPDATED,
HINT_PERMISSION_REQUEST,
})
@Retention(RetentionPolicy.SOURCE)
public @interface SliceHint {}
/**
* @hide
*/
@StringDef(prefix = { "SUBTYPE_" }, value = {
SUBTYPE_COLOR,
SUBTYPE_CONTENT_DESCRIPTION,
SUBTYPE_MAX,
SUBTYPE_MESSAGE,
SUBTYPE_PRIORITY,
SUBTYPE_RANGE,
SUBTYPE_SOURCE,
SUBTYPE_TOGGLE,
SUBTYPE_VALUE,
SUBTYPE_LAYOUT_DIRECTION,
})
@Retention(RetentionPolicy.SOURCE)
public @interface SliceSubtype {}
/**
* Hint that this content is a title of other content in the slice. This can also indicate that
* the content should be used in the shortcut representation of the slice (icon, label, action),
* normally this should be indicated by adding the hint on the action containing that content.
*
* @see SliceItem#FORMAT_ACTION
*/
public static final String HINT_TITLE = "title";
/**
* Hint that all sub-items/sub-slices within this content should be considered
* to have {@link #HINT_LIST_ITEM}.
*/
public static final String HINT_LIST = "list";
/**
* Hint that this item is part of a list and should be formatted as if is part
* of a list.
*/
public static final String HINT_LIST_ITEM = "list_item";
/**
* Hint that this content is important and should be larger when displayed if
* possible.
*/
public static final String HINT_LARGE = "large";
/**
* Hint that this slice contains a number of actions that can be grouped together
* in a sort of controls area of the UI.
*/
public static final String HINT_ACTIONS = "actions";
/**
* Hint indicating that this item (and its sub-items) are the current selection.
*/
public static final String HINT_SELECTED = "selected";
/**
* Hint to indicate that this content should not be tinted.
*/
public static final String HINT_NO_TINT = "no_tint";
/**
* Hint to indicate that this content should only be displayed if the slice is presented
* as a shortcut.
*/
public static final String HINT_SHORTCUT = "shortcut";
/**
* Hint indicating this content should be shown instead of the normal content when the slice
* is in small format.
*/
public static final String HINT_SUMMARY = "summary";
/**
* Hint to indicate that this content has a toggle action associated with it. To indicate that
* the toggle is on, use {@link #HINT_SELECTED}. When the toggle state changes, the intent
* associated with it will be sent along with an extra {@link #EXTRA_TOGGLE_STATE} which can be
* retrieved to see the new state of the toggle.
* @hide
*/
public static final String HINT_TOGGLE = "toggle";
/**
* Hint that list items within this slice or subslice would appear better
* if organized horizontally.
*/
public static final String HINT_HORIZONTAL = "horizontal";
/**
* Hint to indicate that this slice is incomplete and an update will be sent once
* loading is complete. Slices which contain HINT_PARTIAL will not be cached by the
* OS and should not be cached by apps.
*/
public static final String HINT_PARTIAL = "partial";
/**
* A hint representing that this item should be used to indicate that there's more
* content associated with this slice.
*/
public static final String HINT_SEE_MORE = "see_more";
/**
* @see Builder#setCallerNeeded
* @hide
*/
public static final String HINT_CALLER_NEEDED = "caller_needed";
/**
* A hint to indicate that the contents of this subslice represent a list of keywords
* related to the parent slice.
* Expected to be on an item of format {@link SliceItem#FORMAT_SLICE}.
*/
public static final String HINT_KEYWORDS = "keywords";
/**
* A hint to indicate that this slice represents an error.
*/
public static final String HINT_ERROR = "error";
/**
* Hint indicating an item representing a time-to-live for the content.
*/
public static final String HINT_TTL = "ttl";
/**
* Hint indicating an item representing when the content was created or last updated.
*/
public static final String HINT_LAST_UPDATED = "last_updated";
/**
* A hint to indicate that this slice represents a permission request for showing
* slices.
*/
public static final String HINT_PERMISSION_REQUEST = "permission_request";
/**
* Subtype to indicate that this item indicates the layout direction for content
* in the slice.
* Expected to be an item of format {@link SliceItem#FORMAT_INT}.
*/
public static final String SUBTYPE_LAYOUT_DIRECTION = "layout_direction";
/**
* Key to retrieve an extra added to an intent when a control is changed.
*/
public static final String EXTRA_TOGGLE_STATE = "android.app.slice.extra.TOGGLE_STATE";
/**
* Key to retrieve an extra added to an intent when the value of a slider is changed.
* @deprecated remove once support lib is update to use EXTRA_RANGE_VALUE instead
* @removed
*/
@Deprecated
public static final String EXTRA_SLIDER_VALUE = "android.app.slice.extra.SLIDER_VALUE";
/**
* Key to retrieve an extra added to an intent when the value of an input range is changed.
*/
public static final String EXTRA_RANGE_VALUE = "android.app.slice.extra.RANGE_VALUE";
/**
* Subtype to indicate that this is a message as part of a communication
* sequence in this slice.
* Expected to be on an item of format {@link SliceItem#FORMAT_SLICE}.
*/
public static final String SUBTYPE_MESSAGE = "message";
/**
* Subtype to tag the source (i.e. sender) of a {@link #SUBTYPE_MESSAGE}.
* Expected to be on an item of format {@link SliceItem#FORMAT_TEXT},
* {@link SliceItem#FORMAT_IMAGE} or an {@link SliceItem#FORMAT_SLICE} containing them.
*/
public static final String SUBTYPE_SOURCE = "source";
/**
* Subtype to tag an item as representing a color.
* Expected to be on an item of format {@link SliceItem#FORMAT_INT}.
*/
public static final String SUBTYPE_COLOR = "color";
/**
* Subtype to tag an item as representing a slider.
* @deprecated remove once support lib is update to use SUBTYPE_RANGE instead
* @removed
*/
@Deprecated
public static final String SUBTYPE_SLIDER = "slider";
/**
* Subtype to tag an item as representing a range.
* Expected to be on an item of format {@link SliceItem#FORMAT_SLICE} containing
* a {@link #SUBTYPE_VALUE} and possibly a {@link #SUBTYPE_MAX}.
*/
public static final String SUBTYPE_RANGE = "range";
/**
* Subtype to tag an item as representing the max int value for a {@link #SUBTYPE_RANGE}.
* Expected to be on an item of format {@link SliceItem#FORMAT_INT}.
*/
public static final String SUBTYPE_MAX = "max";
/**
* Subtype to tag an item as representing the current int value for a {@link #SUBTYPE_RANGE}.
* Expected to be on an item of format {@link SliceItem#FORMAT_INT}.
*/
public static final String SUBTYPE_VALUE = "value";
/**
* Subtype to indicate that this content has a toggle action associated with it. To indicate
* that the toggle is on, use {@link #HINT_SELECTED}. When the toggle state changes, the
* intent associated with it will be sent along with an extra {@link #EXTRA_TOGGLE_STATE}
* which can be retrieved to see the new state of the toggle.
*/
public static final String SUBTYPE_TOGGLE = "toggle";
/**
* Subtype to tag an item representing priority.
* Expected to be on an item of format {@link SliceItem#FORMAT_INT}.
*/
public static final String SUBTYPE_PRIORITY = "priority";
/**
* Subtype to tag an item to use as a content description.
* Expected to be on an item of format {@link SliceItem#FORMAT_TEXT}.
*/
public static final String SUBTYPE_CONTENT_DESCRIPTION = "content_description";
/**
* Subtype to tag an item as representing a time in milliseconds since midnight,
* January 1, 1970 UTC.
*/
public static final String SUBTYPE_MILLIS = "millis";
private final SliceItem[] mItems;
private final @SliceHint String[] mHints;
private SliceSpec mSpec;
private Uri mUri;
Slice(ArrayList<SliceItem> items, @SliceHint String[] hints, Uri uri, SliceSpec spec) {
mHints = hints;
mItems = items.toArray(new SliceItem[items.size()]);
mUri = uri;
mSpec = spec;
}
protected Slice(Parcel in) {
mHints = in.readStringArray();
int n = in.readInt();
mItems = new SliceItem[n];
for (int i = 0; i < n; i++) {
mItems[i] = SliceItem.CREATOR.createFromParcel(in);
}
mUri = Uri.CREATOR.createFromParcel(in);
mSpec = in.readTypedObject(SliceSpec.CREATOR);
}
/**
* @return The spec for this slice
*/
public @Nullable SliceSpec getSpec() {
return mSpec;
}
/**
* @return The Uri that this Slice represents.
*/
public Uri getUri() {
return mUri;
}
/**
* @return All child {@link SliceItem}s that this Slice contains.
*/
public List<SliceItem> getItems() {
return Arrays.asList(mItems);
}
/**
* @return All hints associated with this Slice.
*/
public @SliceHint List<String> getHints() {
return Arrays.asList(mHints);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(mHints);
dest.writeInt(mItems.length);
for (int i = 0; i < mItems.length; i++) {
mItems[i].writeToParcel(dest, flags);
}
mUri.writeToParcel(dest, 0);
dest.writeTypedObject(mSpec, flags);
}
@Override
public int describeContents() {
return 0;
}
/**
* @hide
*/
public boolean hasHint(@SliceHint String hint) {
return ArrayUtils.contains(mHints, hint);
}
/**
* Returns whether the caller for this slice matters.
* @see Builder#setCallerNeeded
*/
public boolean isCallerNeeded() {
return hasHint(HINT_CALLER_NEEDED);
}
/**
* A Builder used to construct {@link Slice}s
*/
public static class Builder {
private final Uri mUri;
private ArrayList<SliceItem> mItems = new ArrayList<>();
private @SliceHint ArrayList<String> mHints = new ArrayList<>();
private SliceSpec mSpec;
/**
* @deprecated TO BE REMOVED
* @removed
*/
@Deprecated
public Builder(@NonNull Uri uri) {
mUri = uri;
}
/**
* Create a builder which will construct a {@link Slice} for the given Uri.
* @param uri Uri to tag for this slice.
* @param spec the spec for this slice.
*/
public Builder(@NonNull Uri uri, SliceSpec spec) {
mUri = uri;
mSpec = spec;
}
/**
* Create a builder for a {@link Slice} that is a sub-slice of the slice
* being constructed by the provided builder.
* @param parent The builder constructing the parent slice
*/
public Builder(@NonNull Slice.Builder parent) {
mUri = parent.mUri.buildUpon().appendPath("_gen")
.appendPath(String.valueOf(mItems.size())).build();
}
/**
* Tells the system whether for this slice the return value of
* {@link SliceProvider#onBindSlice(Uri, java.util.Set)} may be different depending on
* {@link SliceProvider#getCallingPackage()} and should not be cached for multiple
* apps.
*/
public Builder setCallerNeeded(boolean callerNeeded) {
if (callerNeeded) {
mHints.add(HINT_CALLER_NEEDED);
} else {
mHints.remove(HINT_CALLER_NEEDED);
}
return this;
}
/**
* Add hints to the Slice being constructed
*/
public Builder addHints(@SliceHint List<String> hints) {
mHints.addAll(hints);
return this;
}
/**
* @deprecated TO BE REMOVED
* @removed
*/
public Builder setSpec(SliceSpec spec) {
mSpec = spec;
return this;
}
/**
* Add a sub-slice to the slice being constructed
* @param subType Optional template-specific type information
* @see SliceItem#getSubType()
*/
public Builder addSubSlice(@NonNull Slice slice, @Nullable @SliceSubtype String subType) {
Preconditions.checkNotNull(slice);
mItems.add(new SliceItem(slice, SliceItem.FORMAT_SLICE, subType,
slice.getHints().toArray(new String[slice.getHints().size()])));
return this;
}
/**
* Add an action to the slice being constructed
* @param subType Optional template-specific type information
* @see SliceItem#getSubType()
*/
public Slice.Builder addAction(@NonNull PendingIntent action, @NonNull Slice s,
@Nullable @SliceSubtype String subType) {
Preconditions.checkNotNull(action);
Preconditions.checkNotNull(s);
List<String> hints = s.getHints();
s.mSpec = null;
mItems.add(new SliceItem(action, s, SliceItem.FORMAT_ACTION, subType, hints.toArray(
new String[hints.size()])));
return this;
}
/**
* Add text to the slice being constructed
* @param subType Optional template-specific type information
* @see SliceItem#getSubType()
*/
public Builder addText(CharSequence text, @Nullable @SliceSubtype String subType,
@SliceHint List<String> hints) {
mItems.add(new SliceItem(text, SliceItem.FORMAT_TEXT, subType, hints));
return this;
}
/**
* Add an image to the slice being constructed
* @param subType Optional template-specific type information
* @see SliceItem#getSubType()
*/
public Builder addIcon(Icon icon, @Nullable @SliceSubtype String subType,
@SliceHint List<String> hints) {
Preconditions.checkNotNull(icon);
mItems.add(new SliceItem(icon, SliceItem.FORMAT_IMAGE, subType, hints));
return this;
}
/**
* Add remote input to the slice being constructed
* @param subType Optional template-specific type information
* @see SliceItem#getSubType()
*/
public Slice.Builder addRemoteInput(RemoteInput remoteInput,
@Nullable @SliceSubtype String subType,
@SliceHint List<String> hints) {
Preconditions.checkNotNull(remoteInput);
mItems.add(new SliceItem(remoteInput, SliceItem.FORMAT_REMOTE_INPUT,
subType, hints));
return this;
}
/**
* Add an integer to the slice being constructed
* @param subType Optional template-specific type information
* @see SliceItem#getSubType()
*/
public Builder addInt(int value, @Nullable @SliceSubtype String subType,
@SliceHint List<String> hints) {
mItems.add(new SliceItem(value, SliceItem.FORMAT_INT, subType, hints));
return this;
}
/**
* @deprecated TO BE REMOVED.
* @removed
*/
@Deprecated
public Slice.Builder addTimestamp(long time, @Nullable @SliceSubtype String subType,
@SliceHint List<String> hints) {
return addLong(time, subType, hints);
}
/**
* Add a long to the slice being constructed
* @param subType Optional template-specific type information
* @see SliceItem#getSubType()
*/
public Slice.Builder addLong(long value, @Nullable @SliceSubtype String subType,
@SliceHint List<String> hints) {
mItems.add(new SliceItem(value, SliceItem.FORMAT_LONG, subType,
hints.toArray(new String[hints.size()])));
return this;
}
/**
* Add a bundle to the slice being constructed.
* <p>Expected to be used for support library extension, should not be used for general
* development
* @param subType Optional template-specific type information
* @see SliceItem#getSubType()
*/
public Slice.Builder addBundle(Bundle bundle, @Nullable @SliceSubtype String subType,
@SliceHint List<String> hints) {
Preconditions.checkNotNull(bundle);
mItems.add(new SliceItem(bundle, SliceItem.FORMAT_BUNDLE, subType,
hints));
return this;
}
/**
* Construct the slice.
*/
public Slice build() {
return new Slice(mItems, mHints.toArray(new String[mHints.size()]), mUri, mSpec);
}
}
public static final @android.annotation.NonNull Creator<Slice> CREATOR = new Creator<Slice>() {
@Override
public Slice createFromParcel(Parcel in) {
return new Slice(in);
}
@Override
public Slice[] newArray(int size) {
return new Slice[size];
}
};
/**
* @hide
* @return A string representation of this slice.
*/
public String toString() {
return toString("");
}
private String toString(String indent) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mItems.length; i++) {
sb.append(indent);
if (Objects.equals(mItems[i].getFormat(), SliceItem.FORMAT_SLICE)) {
sb.append("slice:\n");
sb.append(mItems[i].getSlice().toString(indent + " "));
} else if (Objects.equals(mItems[i].getFormat(), SliceItem.FORMAT_TEXT)) {
sb.append("text: ");
sb.append(mItems[i].getText());
sb.append("\n");
} else {
sb.append(mItems[i].getFormat());
sb.append("\n");
}
}
return sb.toString();
}
}
|