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 
17 package com.android.settings.slices;
18 
19 import android.annotation.IntDef;
20 import android.net.Uri;
21 import android.text.TextUtils;
22 import android.util.Log;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 
27 /**
28  * Data class representing a slice stored by {@link SlicesIndexer}.
29  * Note that {@link #mKey} is treated as a primary key for this class and determines equality.
30  */
31 public class SliceData {
32     /**
33      * Flags indicating the UI type of the Slice.
34      */
35     @IntDef({SliceType.INTENT, SliceType.SWITCH, SliceType.SLIDER})
36     @Retention(RetentionPolicy.SOURCE)
37     public @interface SliceType {
38         /**
39          * Only supports content intent.
40          */
41         int INTENT = 0;
42 
43         /**
44          * Supports toggle action.
45          */
46         int SWITCH = 1;
47 
48         /**
49          * Supports progress bar.
50          */
51         int SLIDER = 2;
52     }
53 
54     private static final String TAG = "SliceData";
55 
56     private final String mKey;
57 
58     private final String mTitle;
59 
60     private final String mSummary;
61 
62     private final CharSequence mScreenTitle;
63 
64     private final String mKeywords;
65 
66     private final int mIconResource;
67 
68     private final String mFragmentClassName;
69 
70     private final Uri mUri;
71 
72     private final String mPreferenceController;
73 
74     private final int mHighlightMenuRes;
75 
76     private final String mUserRestriction;
77 
78     @SliceType
79     private final int mSliceType;
80 
81     private final String mUnavailableSliceSubtitle;
82 
83     private final boolean mIsPublicSlice;
84 
getKey()85     public String getKey() {
86         return mKey;
87     }
88 
getTitle()89     public String getTitle() {
90         return mTitle;
91     }
92 
getSummary()93     public String getSummary() {
94         return mSummary;
95     }
96 
getScreenTitle()97     public CharSequence getScreenTitle() {
98         return mScreenTitle;
99     }
100 
getKeywords()101     public String getKeywords() {
102         return mKeywords;
103     }
104 
getIconResource()105     public int getIconResource() {
106         return mIconResource;
107     }
108 
getFragmentClassName()109     public String getFragmentClassName() {
110         return mFragmentClassName;
111     }
112 
getUri()113     public Uri getUri() {
114         return mUri;
115     }
116 
getPreferenceController()117     public String getPreferenceController() {
118         return mPreferenceController;
119     }
120 
getSliceType()121     public int getSliceType() {
122         return mSliceType;
123     }
124 
getUnavailableSliceSubtitle()125     public String getUnavailableSliceSubtitle() {
126         return mUnavailableSliceSubtitle;
127     }
128 
getHighlightMenuRes()129     public int getHighlightMenuRes() {
130         return mHighlightMenuRes;
131     }
132 
isPublicSlice()133     public boolean isPublicSlice() {
134         return mIsPublicSlice;
135     }
136 
getUserRestriction()137     public String getUserRestriction() {
138         return mUserRestriction;
139     }
140 
SliceData(Builder builder)141     private SliceData(Builder builder) {
142         mKey = builder.mKey;
143         mTitle = builder.mTitle;
144         mSummary = builder.mSummary;
145         mScreenTitle = builder.mScreenTitle;
146         mKeywords = builder.mKeywords;
147         mIconResource = builder.mIconResource;
148         mFragmentClassName = builder.mFragmentClassName;
149         mUri = builder.mUri;
150         mPreferenceController = builder.mPrefControllerClassName;
151         mSliceType = builder.mSliceType;
152         mUnavailableSliceSubtitle = builder.mUnavailableSliceSubtitle;
153         mIsPublicSlice = builder.mIsPublicSlice;
154         mHighlightMenuRes = builder.mHighlightMenuRes;
155         mUserRestriction = builder.mUserRestriction;
156     }
157 
158     @Override
hashCode()159     public int hashCode() {
160         return mKey.hashCode();
161     }
162 
163     @Override
equals(Object obj)164     public boolean equals(Object obj) {
165         if (!(obj instanceof SliceData)) {
166             return false;
167         }
168         SliceData newObject = (SliceData) obj;
169         return TextUtils.equals(mKey, newObject.mKey);
170     }
171 
172     static class Builder {
173         private String mKey;
174 
175         private String mTitle;
176 
177         private String mSummary;
178 
179         private CharSequence mScreenTitle;
180 
181         private String mKeywords;
182 
183         private int mIconResource;
184 
185         private String mFragmentClassName;
186 
187         private Uri mUri;
188 
189         private String mPrefControllerClassName;
190 
191         private int mSliceType;
192 
193         private String mUnavailableSliceSubtitle;
194 
195         private int mHighlightMenuRes;
196 
197         private boolean mIsPublicSlice;
198 
199         private String mUserRestriction;
200 
setKey(String key)201         public Builder setKey(String key) {
202             mKey = key;
203             return this;
204         }
205 
setTitle(String title)206         public Builder setTitle(String title) {
207             mTitle = title;
208             return this;
209         }
210 
setSummary(String summary)211         public Builder setSummary(String summary) {
212             mSummary = summary;
213             return this;
214         }
215 
setScreenTitle(CharSequence screenTitle)216         public Builder setScreenTitle(CharSequence screenTitle) {
217             mScreenTitle = screenTitle;
218             return this;
219         }
220 
setKeywords(String keywords)221         public Builder setKeywords(String keywords) {
222             mKeywords = keywords;
223             return this;
224         }
225 
setIcon(int iconResource)226         public Builder setIcon(int iconResource) {
227             mIconResource = iconResource;
228             return this;
229         }
230 
setPreferenceControllerClassName(String controllerClassName)231         public Builder setPreferenceControllerClassName(String controllerClassName) {
232             mPrefControllerClassName = controllerClassName;
233             return this;
234         }
235 
setFragmentName(String fragmentClassName)236         public Builder setFragmentName(String fragmentClassName) {
237             mFragmentClassName = fragmentClassName;
238             return this;
239         }
240 
setUri(Uri uri)241         public Builder setUri(Uri uri) {
242             mUri = uri;
243             return this;
244         }
245 
setSliceType(@liceType int sliceType)246         public Builder setSliceType(@SliceType int sliceType) {
247             mSliceType = sliceType;
248             return this;
249         }
250 
setUnavailableSliceSubtitle( String unavailableSliceSubtitle)251         public Builder setUnavailableSliceSubtitle(
252                 String unavailableSliceSubtitle) {
253             mUnavailableSliceSubtitle = unavailableSliceSubtitle;
254             return this;
255         }
256 
setHighlightMenuRes(int highlightMenuRes)257         public Builder setHighlightMenuRes(int highlightMenuRes) {
258             mHighlightMenuRes = highlightMenuRes;
259             return this;
260         }
261 
setIsPublicSlice(boolean isPublicSlice)262         public Builder setIsPublicSlice(boolean isPublicSlice) {
263             mIsPublicSlice = isPublicSlice;
264             return this;
265         }
266 
setUserRestriction(String userRestriction)267         public Builder setUserRestriction(String userRestriction) {
268             mUserRestriction = userRestriction;
269             return this;
270         }
271 
build()272         public SliceData build() {
273             if (TextUtils.isEmpty(mKey)) {
274                 throw new InvalidSliceDataException("Key cannot be empty");
275             }
276 
277             if (TextUtils.isEmpty(mTitle)) {
278                 throw new InvalidSliceDataException("Title cannot be empty");
279             }
280 
281             if (TextUtils.isEmpty(mFragmentClassName)) {
282                 throw new InvalidSliceDataException("Fragment Name cannot be empty");
283             }
284 
285             if (TextUtils.isEmpty(mPrefControllerClassName)) {
286                 throw new InvalidSliceDataException("Preference Controller cannot be empty");
287             }
288 
289             if (mHighlightMenuRes == 0) {
290                 Log.w(TAG, "Highlight menu key res is empty: " + mPrefControllerClassName);
291             }
292 
293             return new SliceData(this);
294         }
295 
getKey()296         public String getKey() {
297             return mKey;
298         }
299     }
300 
301     public static class InvalidSliceDataException extends RuntimeException {
302 
InvalidSliceDataException(String message)303         public InvalidSliceDataException(String message) {
304             super(message);
305         }
306     }
307 }