Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
| 17 | package android.app.slice; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.net.Uri; |
| 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | |
| 24 | /** |
| 25 | * Class describing the structure of the data contained within a slice. |
| 26 | * <p> |
| 27 | * A data version contains a string which describes the type of structure |
| 28 | * and a revision which denotes this specific implementation. Revisions are expected |
| 29 | * to be backwards compatible and monotonically increasing. Meaning if a |
| 30 | * SliceSpec has the same type and an equal or lesser revision, |
| 31 | * it is expected to be compatible. |
| 32 | * <p> |
| 33 | * Apps rendering slices will provide a list of supported versions to the OS which |
| 34 | * will also be given to the app. Apps should only return a {@link Slice} with a |
| 35 | * {@link SliceSpec} that one of the supported {@link SliceSpec}s provided |
| 36 | * {@link #canRender}. |
| 37 | * |
| 38 | * @see Slice |
| 39 | * @see SliceProvider#onBindSlice(Uri) |
| 40 | */ |
| 41 | public final class SliceSpec implements Parcelable { |
| 42 | |
| 43 | private final String mType; |
| 44 | private final int mRevision; |
| 45 | |
| 46 | public SliceSpec(@NonNull String type, int revision) { |
| 47 | mType = type; |
| 48 | mRevision = revision; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @hide |
| 53 | */ |
| 54 | public SliceSpec(Parcel source) { |
| 55 | mType = source.readString(); |
| 56 | mRevision = source.readInt(); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public int describeContents() { |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void writeToParcel(Parcel dest, int flags) { |
| 66 | dest.writeString(mType); |
| 67 | dest.writeInt(mRevision); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Gets the type of the version. |
| 72 | */ |
| 73 | public String getType() { |
| 74 | return mType; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Gets the revision of the version. |
| 79 | */ |
| 80 | public int getRevision() { |
| 81 | return mRevision; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Indicates that this spec can be used to render the specified spec. |
| 86 | * <p> |
| 87 | * Rendering support is not bi-directional (e.g. Spec v3 can render |
| 88 | * Spec v2, but Spec v2 cannot render Spec v3). |
| 89 | * |
| 90 | * @param candidate candidate format of data. |
| 91 | * @return true if versions are compatible. |
| 92 | * @see androidx.app.slice.widget.SliceView |
| 93 | */ |
| 94 | public boolean canRender(@NonNull SliceSpec candidate) { |
| 95 | if (!mType.equals(candidate.mType)) return false; |
| 96 | return mRevision >= candidate.mRevision; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean equals(Object obj) { |
| 101 | if (!(obj instanceof SliceSpec)) return false; |
| 102 | SliceSpec other = (SliceSpec) obj; |
| 103 | return mType.equals(other.mType) && mRevision == other.mRevision; |
| 104 | } |
| 105 | |
| 106 | public static final Creator<SliceSpec> CREATOR = new Creator<SliceSpec>() { |
| 107 | @Override |
| 108 | public SliceSpec createFromParcel(Parcel source) { |
| 109 | return new SliceSpec(source); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public SliceSpec[] newArray(int size) { |
| 114 | return new SliceSpec[size]; |
| 115 | } |
| 116 | }; |
| 117 | } |