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 com.android.tv.twopanelsettings.slices.compat; 18 19 import android.net.Uri; 20 import androidx.annotation.NonNull; 21 22 /** 23 * Class describing the structure of the data contained within a slice. 24 * 25 * <p>A data version contains a string which describes the type of structure and a revision which 26 * denotes this specific implementation. Revisions are expected to be backwards compatible and 27 * monotonically increasing. Meaning if a SliceSpec has the same type and an equal or lesser 28 * revision, it is expected to be compatible. 29 * 30 * <p>Apps rendering slices will provide a list of supported versions to the OS which will also be 31 * given to the app. Apps should only return a {@link Slice} with a {@link SliceSpec} that one of 32 * the supported {@link SliceSpec}s provided {@link #canRender}. 33 * 34 * @see Slice 35 * @see SliceProvider#onBindSlice(Uri) 36 */ 37 public final class SliceSpec { 38 39 String mType; 40 41 int mRevision = 1; 42 SliceSpec(@onNull String type, int revision)43 public SliceSpec(@NonNull String type, int revision) { 44 mType = type; 45 mRevision = revision; 46 } 47 48 /** Gets the type of the version. */ 49 @NonNull getType()50 public String getType() { 51 return mType; 52 } 53 54 /** Gets the revision of the version. */ getRevision()55 public int getRevision() { 56 return mRevision; 57 } 58 59 /** 60 * Indicates that this spec can be used to render the specified spec. 61 * 62 * <p>Rendering support is not bi-directional (e.g. Spec v3 can render Spec v2, but Spec v2 cannot 63 * render Spec v3). 64 * 65 * @param candidate candidate format of data. 66 * @return true if versions are compatible. 67 * @see com.android.tv.twopanelsettings.slices.compat.widget.SliceView 68 */ canRender(@onNull SliceSpec candidate)69 public boolean canRender(@NonNull SliceSpec candidate) { 70 if (!mType.equals(candidate.mType)) { 71 return false; 72 } 73 return mRevision >= candidate.mRevision; 74 } 75 76 @Override equals(Object obj)77 public boolean equals(Object obj) { 78 if (!(obj instanceof SliceSpec)) { 79 return false; 80 } 81 SliceSpec other = (SliceSpec) obj; 82 return mType.equals(other.mType) && mRevision == other.mRevision; 83 } 84 85 @Override hashCode()86 public int hashCode() { 87 return mType.hashCode() + mRevision; 88 } 89 90 @Override toString()91 public String toString() { 92 return String.format("SliceSpec{%s,%d}", mType, mRevision); 93 } 94 } 95