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     @SliceType
77     private final int mSliceType;
78 
79     private final String mUnavailableSliceSubtitle;
80 
81     private final boolean mIsPublicSlice;
82 
getKey()83     public String getKey() {
84         return mKey;
85     }
86 
getTitle()87     public String getTitle() {
88         return mTitle;
89     }
90 
getSummary()91     public String getSummary() {
92         return mSummary;
93     }
94 
getScreenTitle()95     public CharSequence getScreenTitle() {
96         return mScreenTitle;
97     }
98 
getKeywords()99     public String getKeywords() {
100         return mKeywords;
101     }
102 
getIconResource()103     public int getIconResource() {
104         return mIconResource;
105     }
106 
getFragmentClassName()107     public String getFragmentClassName() {
108         return mFragmentClassName;
109     }
110 
getUri()111     public Uri getUri() {
112         return mUri;
113     }
114 
getPreferenceController()115     public String getPreferenceController() {
116         return mPreferenceController;
117     }
118 
getSliceType()119     public int getSliceType() {
120         return mSliceType;
121     }
122 
getUnavailableSliceSubtitle()123     public String getUnavailableSliceSubtitle() {
124         return mUnavailableSliceSubtitle;
125     }
126 
getHighlightMenuRes()127     public int getHighlightMenuRes() {
128         return mHighlightMenuRes;
129     }
130 
isPublicSlice()131     public boolean isPublicSlice() {
132         return mIsPublicSlice;
133     }
134 
SliceData(Builder builder)135     private SliceData(Builder builder) {
136         mKey = builder.mKey;
137         mTitle = builder.mTitle;
138         mSummary = builder.mSummary;
139         mScreenTitle = builder.mScreenTitle;
140         mKeywords = builder.mKeywords;
141         mIconResource = builder.mIconResource;
142         mFragmentClassName = builder.mFragmentClassName;
143         mUri = builder.mUri;
144         mPreferenceController = builder.mPrefControllerClassName;
145         mSliceType = builder.mSliceType;
146         mUnavailableSliceSubtitle = builder.mUnavailableSliceSubtitle;
147         mIsPublicSlice = builder.mIsPublicSlice;
148         mHighlightMenuRes = builder.mHighlightMenuRes;
149     }
150 
151     @Override
hashCode()152     public int hashCode() {
153         return mKey.hashCode();
154     }
155 
156     @Override
equals(Object obj)157     public boolean equals(Object obj) {
158         if (!(obj instanceof SliceData)) {
159             return false;
160         }
161         SliceData newObject = (SliceData) obj;
162         return TextUtils.equals(mKey, newObject.mKey);
163     }
164 
165     static class Builder {
166         private String mKey;
167 
168         private String mTitle;
169 
170         private String mSummary;
171 
172         private CharSequence mScreenTitle;
173 
174         private String mKeywords;
175 
176         private int mIconResource;
177 
178         private String mFragmentClassName;
179 
180         private Uri mUri;
181 
182         private String mPrefControllerClassName;
183 
184         private int mSliceType;
185 
186         private String mUnavailableSliceSubtitle;
187 
188         private int mHighlightMenuRes;
189 
190         private boolean mIsPublicSlice;
191 
setKey(String key)192         public Builder setKey(String key) {
193             mKey = key;
194             return this;
195         }
196 
setTitle(String title)197         public Builder setTitle(String title) {
198             mTitle = title;
199             return this;
200         }
201 
setSummary(String summary)202         public Builder setSummary(String summary) {
203             mSummary = summary;
204             return this;
205         }
206 
setScreenTitle(CharSequence screenTitle)207         public Builder setScreenTitle(CharSequence screenTitle) {
208             mScreenTitle = screenTitle;
209             return this;
210         }
211 
setKeywords(String keywords)212         public Builder setKeywords(String keywords) {
213             mKeywords = keywords;
214             return this;
215         }
216 
setIcon(int iconResource)217         public Builder setIcon(int iconResource) {
218             mIconResource = iconResource;
219             return this;
220         }
221 
setPreferenceControllerClassName(String controllerClassName)222         public Builder setPreferenceControllerClassName(String controllerClassName) {
223             mPrefControllerClassName = controllerClassName;
224             return this;
225         }
226 
setFragmentName(String fragmentClassName)227         public Builder setFragmentName(String fragmentClassName) {
228             mFragmentClassName = fragmentClassName;
229             return this;
230         }
231 
setUri(Uri uri)232         public Builder setUri(Uri uri) {
233             mUri = uri;
234             return this;
235         }
236 
setSliceType(@liceType int sliceType)237         public Builder setSliceType(@SliceType int sliceType) {
238             mSliceType = sliceType;
239             return this;
240         }
241 
setUnavailableSliceSubtitle( String unavailableSliceSubtitle)242         public Builder setUnavailableSliceSubtitle(
243                 String unavailableSliceSubtitle) {
244             mUnavailableSliceSubtitle = unavailableSliceSubtitle;
245             return this;
246         }
247 
setHighlightMenuRes(int highlightMenuRes)248         public Builder setHighlightMenuRes(int highlightMenuRes) {
249             mHighlightMenuRes = highlightMenuRes;
250             return this;
251         }
252 
setIsPublicSlice(boolean isPublicSlice)253         public Builder setIsPublicSlice(boolean isPublicSlice) {
254             mIsPublicSlice = isPublicSlice;
255             return this;
256         }
257 
build()258         public SliceData build() {
259             if (TextUtils.isEmpty(mKey)) {
260                 throw new InvalidSliceDataException("Key cannot be empty");
261             }
262 
263             if (TextUtils.isEmpty(mTitle)) {
264                 throw new InvalidSliceDataException("Title cannot be empty");
265             }
266 
267             if (TextUtils.isEmpty(mFragmentClassName)) {
268                 throw new InvalidSliceDataException("Fragment Name cannot be empty");
269             }
270 
271             if (TextUtils.isEmpty(mPrefControllerClassName)) {
272                 throw new InvalidSliceDataException("Preference Controller cannot be empty");
273             }
274 
275             if (mHighlightMenuRes == 0) {
276                 Log.w(TAG, "Highlight menu key res is empty: " + mPrefControllerClassName);
277             }
278 
279             return new SliceData(this);
280         }
281 
getKey()282         public String getKey() {
283             return mKey;
284         }
285     }
286 
287     public static class InvalidSliceDataException extends RuntimeException {
288 
InvalidSliceDataException(String message)289         public InvalidSliceDataException(String message) {
290             super(message);
291         }
292     }
293 }