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