1 /*
2  * Copyright (C) 2021 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.settings.library;
18 
19 import android.annotation.SystemApi;
20 import android.content.Intent;
21 import android.graphics.drawable.Drawable;
22 import android.os.Bundle;
23 
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.stream.IntStream;
28 
29 /**
30  *  @hide
31  *  Hold the data of a Settings Preference.
32  */
33 @SystemApi
34 public class PreferenceCompat {
35     public static final byte TYPE_PREFERENCE = 0;
36     public static final byte TYPE_PREFERENCE_CATEGORY = 1;
37     public static final byte TYPE_PREFERENCE_ACCESS_POINT = 2;
38     public static final byte TYPE_PREFERENCE_COLLAPSE_CATEGORY = 3;
39     public static final byte TYPE_LIST = 4;
40     public static final byte TYPE_SWITCH = 5;
41     public static final byte TYPE_RADIO = 6;
42     public static final byte TYPE_DIALOG = 7;
43 
44     public static final byte STATUS_UNASSIGNED = 0;
45     public static final byte STATUS_OFF = 1;
46     public static final byte STATUS_ON = 2;
47 
48     private final String[] mKey;
49     private String mTitle;
50     private String mSummary;
51     private String mContentDescription;
52     private Bundle mExtras;
53     private Intent mIntent;
54     private Drawable mIcon;
55     private CharSequence[] mEntries;
56     private CharSequence[] mEntryValues;
57     private int mValueIndex;
58     private String mValue;
59     private boolean mHasSlice;
60     private String mSliceUri;
61     private String mMessage;
62     private String mNeutralButtonText;
63     private String mNegativeButtonText;
64     private String mPositiveButtonText;
65 
66     // 0 : preference, 1 : preferenceCategory, 2 : AccessPointPreference
67     private byte mType;
68 
69     // 0 : not updated, 1 : unchecked, 2 : checked
70     private byte mChecked;
71 
72     // 0 : not updated, 1 : invisible, 2: visible
73     private byte mVisible;
74 
75     // 0: not updated, 1 :not selectable, 2: selectable
76     private byte mSelectable;
77 
78     // 0: not updated, 1 :not selectable, 2: selectable
79     private byte mEnabled;
80 
81     // 0: not updated, 1 :not focused, 2: focused
82     private boolean mIsFocused;
83 
84     private boolean mShouldRemove;
85 
86     // Indicate whether there is on preference change listener
87     private boolean mHasOnPreferenceChangeListener;
88 
89     // Indicates whether the preference is a restricted preference.
90     private boolean mIsRestricted;
91 
92     // Indicates whether the preference is disabled by admin.
93     private boolean mIsDisabledByAdmin;
94 
95     private byte mPersistent;
96 
97     private String mRadioGroup;
98 
99     // Next state of the current state, -1 to indicate there is no next state.
100     private Integer mNextState;
101 
102     private List<PreferenceCompat> mChildPrefCompats;
103 
104     /** @hide */
105     @SystemApi
setChildPrefCompats( List<PreferenceCompat> childPrefCompats)106     public void setChildPrefCompats(
107             List<PreferenceCompat> childPrefCompats) {
108         this.mChildPrefCompats = childPrefCompats;
109     }
110 
111     /** @hide */
112     @SystemApi
getChildPrefCompats()113     public List<PreferenceCompat> getChildPrefCompats() {
114         if (mChildPrefCompats == null) {
115             mChildPrefCompats = new ArrayList<>();
116         }
117         return mChildPrefCompats;
118     }
119 
120     /** @hide */
121     @SystemApi
getChildPrefsCount()122     public int getChildPrefsCount() {
123         return mChildPrefCompats == null ? 0 : mChildPrefCompats.size();
124     }
125 
126     /** @hide */
127     @SystemApi
clearChildPrefCompats()128     public void clearChildPrefCompats() {
129         mChildPrefCompats = new ArrayList<>();
130     }
131 
132     /** @hide */
133     @SystemApi
findChildPreferenceCompat(String[] prefKey)134     public PreferenceCompat findChildPreferenceCompat(String[] prefKey) {
135         if (prefKey == null || prefKey.length != this.mKey.length + 1) {
136             return null;
137         }
138         if (IntStream.range(0, mKey.length).anyMatch(i -> !(mKey[i].equals(prefKey[i])))) {
139             return null;
140         }
141         if (mChildPrefCompats != null) {
142             return mChildPrefCompats.stream()
143                     .filter(preferenceParcelable ->
144                             preferenceParcelable.getKey()[preferenceParcelable.getKey().length - 1]
145                                     .equals(prefKey[prefKey.length - 1]))
146                     .findFirst()
147                     .orElse(null);
148         }
149         return null;
150     }
151 
152     /** @hide */
153     @SystemApi
PreferenceCompat(String key)154     public PreferenceCompat(String key) {
155         this.mKey = new String[]{key};
156     }
157 
158     /** @hide */
159     @SystemApi
PreferenceCompat(String[] key)160     public PreferenceCompat(String[] key) {
161         this.mKey = key;
162     }
163 
164     /** @hide */
165     @SystemApi
PreferenceCompat(String[] key, String title)166     public PreferenceCompat(String[] key, String title) {
167         this.mKey = key;
168         this.mTitle = title;
169     }
170 
171     /** @hide */
172     @SystemApi
PreferenceCompat(String[] key, String title, String summary)173     public PreferenceCompat(String[] key, String title, String summary) {
174         this(key, title);
175         this.mSummary = summary;
176     }
177 
178     /** @hide */
179     @SystemApi
getKey()180     public String[] getKey() {
181         return mKey;
182     }
183 
184     /** @hide */
185     @SystemApi
getTitle()186     public String getTitle() {
187         return mTitle;
188     }
189 
190     /** @hide */
191     @SystemApi
setTitle(String title)192     public void setTitle(String title) {
193         this.mTitle = title;
194     }
195 
196     /** @hide */
197     @SystemApi
getSummary()198     public String getSummary() {
199         return mSummary;
200     }
201 
202     /** @hide */
203     @SystemApi
setSummary(String summary)204     public void setSummary(String summary) {
205         this.mSummary = summary;
206     }
207 
208     /** @hide */
209     @SystemApi
getIcon()210     public Drawable getIcon() {
211         return mIcon;
212     }
213 
214     /** @hide */
215     @SystemApi
setIcon(Drawable icon)216     public void setIcon(Drawable icon) {
217         this.mIcon = icon;
218     }
219 
220     /** @hide */
221     @SystemApi
getContentDescription()222     public String getContentDescription() {
223         return mContentDescription;
224     }
225 
226     /** @hide */
227     @SystemApi
getMessage()228     public String getMessage() {
229         return mMessage;
230     }
231 
232     /** @hide */
233     @SystemApi
setMessage(String message)234     public void setMessage(String message) {
235         this.mMessage = message;
236     }
237 
238     /** @hide */
239     @SystemApi
getNeutralButtonText()240     public String getNeutralButtonText() {
241         return mNeutralButtonText;
242     }
243 
244     /** @hide */
245     @SystemApi
setNeutralButtonText(String neutralButtonText)246     public void setNeutralButtonText(String neutralButtonText) {
247         this.mNeutralButtonText = neutralButtonText;
248     }
249 
250     /** @hide */
251     @SystemApi
getNegativeButtonText()252     public String getNegativeButtonText() {
253         return mNegativeButtonText;
254     }
255 
256     /** @hide */
257     @SystemApi
setNegativeButtonText(String negativeButtonText)258     public void setNegativeButtonText(String negativeButtonText) {
259         this.mNegativeButtonText = negativeButtonText;
260     }
261 
262     /** @hide */
263     @SystemApi
getPositiveButtonText()264     public String getPositiveButtonText() {
265         return mPositiveButtonText;
266     }
267 
268     /** @hide */
269     @SystemApi
setPositiveButtonText(String positiveButtonText)270     public void setPositiveButtonText(String positiveButtonText) {
271         this.mPositiveButtonText = positiveButtonText;
272     }
273 
274     /** @hide */
275     @SystemApi
getType()276     public int getType() {
277         return mType;
278     }
279 
280     /** @hide */
281     @SystemApi
setType(byte type)282     public void setType(byte type) {
283         this.mType = type;
284     }
285 
286     /** @hide */
287     @SystemApi
getEntries()288     public CharSequence[] getEntries() {
289         return mEntries;
290     }
291 
292     /** @hide */
293     @SystemApi
setEntries(CharSequence[] entries)294     public void setEntries(CharSequence[] entries) {
295         mEntries = entries;
296     }
297 
298     /** @hide */
299     @SystemApi
getEntryValues()300     public CharSequence[] getEntryValues() {
301         return mEntryValues;
302     }
303 
304     /** @hide */
305     @SystemApi
setEntryValues(CharSequence[] entryValues)306     public void setEntryValues(CharSequence[] entryValues) {
307         mEntryValues = entryValues;
308     }
309 
310     /** @hide */
311     @SystemApi
getValueIndex()312     public int getValueIndex() {
313         return mValueIndex;
314     }
315 
316     /** @hide */
317     @SystemApi
setValueIndex(int valueIndex)318     public void setValueIndex(int valueIndex) {
319         mValueIndex = valueIndex;
320     }
321 
322     /** @hide */
323     @SystemApi
getValue()324     public String getValue() {
325         return mValue;
326     }
327 
328     /** @hide */
329     @SystemApi
setValue(String value)330     public void setValue(String value) {
331         mValue = value;
332     }
333 
334     /** @hide */
335     @SystemApi
hasSlice()336     public boolean hasSlice() {
337         return mHasSlice;
338     }
339 
340     /** @hide */
341     @SystemApi
setHasSlice(boolean hasSlice)342     public void setHasSlice(boolean hasSlice) {
343         mHasSlice = hasSlice;
344     }
345 
346     /** @hide */
347     @SystemApi
getSliceUri()348     public String getSliceUri() {
349         return mSliceUri;
350     }
351 
352     /** @hide */
353     @SystemApi
setSliceUri(String sliceUri)354     public void setSliceUri(String sliceUri) {
355         mSliceUri = sliceUri;
356     }
357 
358     /** @hide */
359     @SystemApi
shouldRemove()360     public boolean shouldRemove() {
361         return mShouldRemove;
362     }
363 
364     /** @hide */
365     @SystemApi
setShouldRemove(boolean shouldRemove)366     public void setShouldRemove(boolean shouldRemove) {
367         mShouldRemove = shouldRemove;
368     }
369 
370     /** @hide */
371     @SystemApi
hasOnPreferenceChangeListener()372     public boolean hasOnPreferenceChangeListener() {
373         return mHasOnPreferenceChangeListener;
374     }
375 
376     /** @hide */
377     @SystemApi
setHasOnPreferenceChangeListener(boolean hasOnPreferenceChangeListener)378     public void setHasOnPreferenceChangeListener(boolean hasOnPreferenceChangeListener) {
379         mHasOnPreferenceChangeListener = hasOnPreferenceChangeListener;
380     }
381 
382     /** @hide */
383     @SystemApi
getNextState()384     public Integer getNextState() {
385         return mNextState;
386     }
387 
388     /** @hide */
389     @SystemApi
setNextState(Integer nextState)390     public void setNextState(Integer nextState) {
391         mNextState = nextState;
392     }
393 
394     /** @hide */
395     @SystemApi
setContentDescription(String contentDescription)396     public void setContentDescription(String contentDescription) {
397         this.mContentDescription = contentDescription;
398     }
399 
400     /** @hide */
401     @SystemApi
getChecked()402     public byte getChecked() {
403         return mChecked;
404     }
405 
406     /** @hide */
407     @SystemApi
setChecked(byte checked)408     public void setChecked(byte checked) {
409         this.mChecked = checked;
410     }
411 
412     /** @hide */
413     @SystemApi
setChecked(boolean checked)414     public void setChecked(boolean checked) {
415         setChecked(ManagerUtil.getChecked(checked));
416     }
417 
418     /** @hide */
419     @SystemApi
setVisible(boolean visible)420     public void setVisible(boolean visible) {
421         setVisible(ManagerUtil.getVisible(visible));
422     }
423 
424     /** @hide */
425     @SystemApi
getVisible()426     public byte getVisible() {
427         return mVisible;
428     }
429 
430     /** @hide */
431     @SystemApi
setVisible(byte visible)432     public void setVisible(byte visible) {
433         this.mVisible = visible;
434     }
435 
436     /** @hide */
437     @SystemApi
getSelectable()438     public byte getSelectable() {
439         return mSelectable;
440     }
441 
442     /** @hide */
443     @SystemApi
setSelectable(byte selectable)444     public void setSelectable(byte selectable) {
445         this.mSelectable = selectable;
446     }
447 
448     /** @hide */
449     @SystemApi
setSelectable(boolean selectable)450     public void setSelectable(boolean selectable) {
451         this.mSelectable = ManagerUtil.getSelectable(selectable);
452     }
453 
454     /** @hide */
455     @SystemApi
getEnabled()456     public byte getEnabled() {
457         return mEnabled;
458     }
459 
460     /** @hide */
461     @SystemApi
setEnabled(byte enabled)462     public void setEnabled(byte enabled) {
463         this.mEnabled = enabled;
464     }
465 
466     /** @hide */
467     @SystemApi
isRestricted()468     public boolean isRestricted() {
469         return mIsRestricted;
470     }
471 
472     /** @hide */
473     @SystemApi
setRestricted(boolean restricted)474     public void setRestricted(boolean restricted) {
475         mIsRestricted = restricted;
476     }
477 
478     /** @hide */
479     @SystemApi
isDisabledByAdmin()480     public boolean isDisabledByAdmin() {
481         return mIsDisabledByAdmin;
482     }
483 
484     /** @hide */
485     @SystemApi
setDisabledByAdmin(boolean disabledByAdmin)486     public void setDisabledByAdmin(boolean disabledByAdmin) {
487         mIsDisabledByAdmin = disabledByAdmin;
488     }
489 
490     /** @hide */
491     @SystemApi
getPersistent()492     public byte getPersistent() {
493         return mPersistent;
494     }
495 
496     /** @hide */
497     @SystemApi
setPersistent(byte persistent)498     public void setPersistent(byte persistent) {
499         mPersistent = persistent;
500     }
501 
502     /** @hide */
503     @SystemApi
setPersistent(boolean persistent)504     public void setPersistent(boolean persistent) {
505         setPersistent(ManagerUtil.getPersistent(persistent));
506     }
507 
508     /** @hide */
509     @SystemApi
getRadioGroup()510     public String getRadioGroup() {
511         return mRadioGroup;
512     }
513 
514     /** @hide */
515     @SystemApi
setRadioGroup(String radioGroup)516     public void setRadioGroup(String radioGroup) {
517         mRadioGroup = radioGroup;
518     }
519 
520     /** @hide */
521     @SystemApi
setEnabled(boolean enabled)522     public void setEnabled(boolean enabled) {
523         setEnabled(ManagerUtil.getEnabled(enabled));
524     }
525 
526     /** @hide */
527     @SystemApi
isFocused()528     public boolean isFocused() {
529         return mIsFocused;
530     }
531 
532     /** @hide */
533     @SystemApi
setFocused(boolean focused)534     public void setFocused(boolean focused) {
535         mIsFocused = focused;
536     }
537 
538     /** @hide */
539     @SystemApi
getExtras()540     public Bundle getExtras() {
541         return mExtras;
542     }
543 
544     /** @hide */
545     @SystemApi
setExtras(Bundle extras)546     public void setExtras(Bundle extras) {
547         this.mExtras = extras;
548     }
549 
550     /** @hide */
551     @SystemApi
addInfo(String key, Object value)552     public void addInfo(String key, Object value) {
553         if (mExtras == null) {
554             mExtras = new Bundle();
555         }
556         mExtras.putObject(key, value);
557     }
558 
559     /** @hide */
560     @SystemApi
getInfo(String key)561     public Object getInfo(String key) {
562         if (mExtras != null) {
563             return mExtras.get(key);
564         }
565         return null;
566     }
567 
568     /** @hide */
569     @SystemApi
getInfo(String key, int value)570     public void getInfo(String key, int value) {
571         if (mExtras == null) {
572             mExtras = new Bundle();
573         }
574         mExtras.putInt(key, value);
575     }
576 
577     /** @hide */
578     @SystemApi
getIntent()579     public Intent getIntent() {
580         return mIntent;
581     }
582 
583     /** @hide */
584     @SystemApi
setIntent(Intent intent)585     public void setIntent(Intent intent) {
586         this.mIntent = intent;
587     }
588 
589     /** @hide */
590     @SystemApi
initChildPreferences()591     public void initChildPreferences() {
592         mChildPrefCompats = new ArrayList<>();
593     }
594 
595     /** @hide */
596     @SystemApi
addChildPrefCompat(PreferenceCompat childPrefCompat)597     public void addChildPrefCompat(PreferenceCompat childPrefCompat) {
598         if (mChildPrefCompats == null) {
599             mChildPrefCompats = new ArrayList<>();
600         }
601         mChildPrefCompats.add(childPrefCompat);
602     }
603 
604     @Override
toString()605     public String toString() {
606         return "PreferenceCompat{"
607                 + "mKey=" + Arrays.toString(mKey)
608                 + ", mTitle='" + mTitle + '\''
609                 + ", mSummary='" + mSummary + '\''
610                 + ", mContentDescription='" + mContentDescription
611                 + '\'' + ", mExtras=" + mExtras
612                 + ", mIntent=" + mIntent
613                 + ", mIcon=" + mIcon
614                 + ", mValue='" + mValue + '\''
615                 + ", mType=" + mType
616                 + ", mChecked=" + mChecked
617                 + ", mVisible=" + mVisible
618                 + ", mSelectable=" + mSelectable
619                 + ", mEnabled=" + mEnabled
620                 + ", mShouldRemove=" + mShouldRemove
621                 + ", mHasOnPreferenceChangeListener=" + mHasOnPreferenceChangeListener
622                 + ", mNextState=" + mNextState
623                 + ", mChildPrefCompats=" + mChildPrefCompats
624                 + '}';
625     }
626 }
627