summaryrefslogtreecommitdiff
path: root/SettingsAPI/java/com/android/tv/settings/library/PreferenceCompat.java
blob: dccd850d9099cd0237c030aebc9ac161b3ff0f72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.settings.library;

import android.annotation.SystemApi;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

/**
 *  @hide
 *  Hold the data of a Settings Preference.
 */
@SystemApi
public class PreferenceCompat {
    public static final byte TYPE_PREFERENCE = 0;
    public static final byte TYPE_PREFERENCE_CATEGORY = 1;
    public static final byte TYPE_PREFERENCE_ACCESS_POINT = 2;
    public static final byte TYPE_PREFERENCE_COLLAPSE_CATEGORY = 3;
    public static final byte TYPE_LIST = 4;
    public static final byte TYPE_SWITCH = 5;
    public static final byte TYPE_RADIO = 6;

    public static final byte STATUS_UNASSIGNED = 0;
    public static final byte STATUS_OFF = 1;
    public static final byte STATUS_ON = 2;

    private final String[] mKey;
    private String mTitle;
    private String mSummary;
    private String mContentDescription;
    private Bundle mExtras;
    private Intent mIntent;
    private Drawable mIcon;
    private CharSequence[] mEntries;
    private CharSequence[] mEntryValues;
    private int mValueIndex;
    private String mValue;
    private boolean mHasSlice;
    private String mSliceUri;

    // 0 : preference, 1 : preferenceCategory, 2 : AccessPointPreference
    private byte mType;

    // 0 : not updated, 1 : unchecked, 2 : checked
    private byte mChecked;

    // 0 : not updated, 1 : invisible, 2: visible
    private byte mVisible;

    // 0: not updated, 1 :not selectable, 2: selectable
    private byte mSelectable;

    // 0: not updated, 1 :not selectable, 2: selectable
    private byte mEnabled;

    // 0: not updated, 1 :not focused, 2: focused
    private boolean mIsFocused;

    private boolean mShouldRemove;

    // Indicate whether there is on preference change listener
    private boolean mHasOnPreferenceChangeListener;

    // Indicates whether the preference is a restricted preference.
    private boolean mIsRestricted;

    // Indicates whether the preference is disabled by admin.
    private boolean mIsDisabledByAdmin;

    private byte mPersistent;

    private String mRadioGroup;

    // Next state of the current state, -1 to indicate there is no next state.
    private Integer mNextState;

    private List<PreferenceCompat> mChildPrefCompats;

    /** @hide */
    @SystemApi
    public void setChildPrefCompats(
            List<PreferenceCompat> childPrefCompats) {
        this.mChildPrefCompats = childPrefCompats;
    }

    /** @hide */
    @SystemApi
    public List<PreferenceCompat> getChildPrefCompats() {
        if (mChildPrefCompats == null) {
            mChildPrefCompats = new ArrayList<>();
        }
        return mChildPrefCompats;
    }

    /** @hide */
    @SystemApi
    public int getChildPrefsCount() {
        return mChildPrefCompats == null ? 0 : mChildPrefCompats.size();
    }

    /** @hide */
    @SystemApi
    public void clearChildPrefCompats() {
        mChildPrefCompats = new ArrayList<>();
    }

    /** @hide */
    @SystemApi
    public PreferenceCompat findChildPreferenceCompat(String[] prefKey) {
        if (prefKey == null || prefKey.length != this.mKey.length + 1) {
            return null;
        }
        if (IntStream.range(0, mKey.length).anyMatch(i -> !(mKey[i].equals(prefKey[i])))) {
            return null;
        }
        if (mChildPrefCompats != null) {
            return mChildPrefCompats.stream()
                    .filter(preferenceParcelable ->
                            preferenceParcelable.getKey()[preferenceParcelable.getKey().length - 1]
                                    .equals(prefKey[prefKey.length - 1]))
                    .findFirst()
                    .orElse(null);
        }
        return null;
    }

    /** @hide */
    @SystemApi
    public PreferenceCompat(String key) {
        this.mKey = new String[]{key};
    }

    /** @hide */
    @SystemApi
    public PreferenceCompat(String[] key) {
        this.mKey = key;
    }

    /** @hide */
    @SystemApi
    public PreferenceCompat(String[] key, String title) {
        this.mKey = key;
        this.mTitle = title;
    }

    /** @hide */
    @SystemApi
    public PreferenceCompat(String[] key, String title, String summary) {
        this(key, title);
        this.mSummary = summary;
    }

    /** @hide */
    @SystemApi
    public String[] getKey() {
        return mKey;
    }

    /** @hide */
    @SystemApi
    public String getTitle() {
        return mTitle;
    }

    /** @hide */
    @SystemApi
    public void setTitle(String title) {
        this.mTitle = title;
    }

    /** @hide */
    @SystemApi
    public String getSummary() {
        return mSummary;
    }

    /** @hide */
    @SystemApi
    public void setSummary(String summary) {
        this.mSummary = summary;
    }

    /** @hide */
    @SystemApi
    public Drawable getIcon() {
        return mIcon;
    }

    /** @hide */
    @SystemApi
    public void setIcon(Drawable icon) {
        this.mIcon = icon;
    }

    /** @hide */
    @SystemApi
    public String getContentDescription() {
        return mContentDescription;
    }

    /** @hide */
    @SystemApi
    public int getType() {
        return mType;
    }

    /** @hide */
    @SystemApi
    public void setType(byte type) {
        this.mType = type;
    }

    /** @hide */
    @SystemApi
    public CharSequence[] getEntries() {
        return mEntries;
    }

    /** @hide */
    @SystemApi
    public void setEntries(CharSequence[] entries) {
        mEntries = entries;
    }

    /** @hide */
    @SystemApi
    public CharSequence[] getEntryValues() {
        return mEntryValues;
    }

    /** @hide */
    @SystemApi
    public void setEntryValues(CharSequence[] entryValues) {
        mEntryValues = entryValues;
    }

    /** @hide */
    @SystemApi
    public int getValueIndex() {
        return mValueIndex;
    }

    /** @hide */
    @SystemApi
    public void setValueIndex(int valueIndex) {
        mValueIndex = valueIndex;
    }

    /** @hide */
    @SystemApi
    public String getValue() {
        return mValue;
    }

    /** @hide */
    @SystemApi
    public void setValue(String value) {
        mValue = value;
    }

    /** @hide */
    @SystemApi
    public boolean hasSlice() {
        return mHasSlice;
    }

    /** @hide */
    @SystemApi
    public void setHasSlice(boolean hasSlice) {
        mHasSlice = hasSlice;
    }

    /** @hide */
    @SystemApi
    public String getSliceUri() {
        return mSliceUri;
    }

    /** @hide */
    @SystemApi
    public void setSliceUri(String sliceUri) {
        mSliceUri = sliceUri;
    }

    /** @hide */
    @SystemApi
    public boolean shouldRemove() {
        return mShouldRemove;
    }

    /** @hide */
    @SystemApi
    public void setShouldRemove(boolean shouldRemove) {
        mShouldRemove = shouldRemove;
    }

    /** @hide */
    @SystemApi
    public boolean hasOnPreferenceChangeListener() {
        return mHasOnPreferenceChangeListener;
    }

    /** @hide */
    @SystemApi
    public void setHasOnPreferenceChangeListener(boolean hasOnPreferenceChangeListener) {
        mHasOnPreferenceChangeListener = hasOnPreferenceChangeListener;
    }

    /** @hide */
    @SystemApi
    public Integer getNextState() {
        return mNextState;
    }

    /** @hide */
    @SystemApi
    public void setNextState(Integer nextState) {
        mNextState = nextState;
    }

    /** @hide */
    @SystemApi
    public void setContentDescription(String contentDescription) {
        this.mContentDescription = contentDescription;
    }

    /** @hide */
    @SystemApi
    public byte getChecked() {
        return mChecked;
    }

    /** @hide */
    @SystemApi
    public void setChecked(byte checked) {
        this.mChecked = checked;
    }

    /** @hide */
    @SystemApi
    public void setChecked(boolean checked) {
        setChecked(ManagerUtil.getChecked(checked));
    }

    /** @hide */
    @SystemApi
    public void setVisible(boolean visible) {
        setVisible(ManagerUtil.getVisible(visible));
    }

    /** @hide */
    @SystemApi
    public byte getVisible() {
        return mVisible;
    }

    /** @hide */
    @SystemApi
    public void setVisible(byte visible) {
        this.mVisible = visible;
    }

    /** @hide */
    @SystemApi
    public byte getSelectable() {
        return mSelectable;
    }

    /** @hide */
    @SystemApi
    public void setSelectable(byte selectable) {
        this.mSelectable = selectable;
    }

    /** @hide */
    @SystemApi
    public void setSelectable(boolean selectable) {
        this.mSelectable = ManagerUtil.getSelectable(selectable);
    }

    /** @hide */
    @SystemApi
    public byte getEnabled() {
        return mEnabled;
    }

    /** @hide */
    @SystemApi
    public void setEnabled(byte enabled) {
        this.mEnabled = enabled;
    }

    /** @hide */
    @SystemApi
    public boolean isRestricted() {
        return mIsRestricted;
    }

    /** @hide */
    @SystemApi
    public void setRestricted(boolean restricted) {
        mIsRestricted = restricted;
    }

    /** @hide */
    @SystemApi
    public boolean isDisabledByAdmin() {
        return mIsDisabledByAdmin;
    }

    /** @hide */
    @SystemApi
    public void setDisabledByAdmin(boolean disabledByAdmin) {
        mIsDisabledByAdmin = disabledByAdmin;
    }

    /** @hide */
    @SystemApi
    public byte getPersistent() {
        return mPersistent;
    }

    /** @hide */
    @SystemApi
    public void setPersistent(byte persistent) {
        mPersistent = persistent;
    }

    /** @hide */
    @SystemApi
    public void setPersistent(boolean persistent) {
        setPersistent(ManagerUtil.getPersistent(persistent));
    }

    /** @hide */
    @SystemApi
    public String getRadioGroup() {
        return mRadioGroup;
    }

    /** @hide */
    @SystemApi
    public void setRadioGroup(String radioGroup) {
        mRadioGroup = radioGroup;
    }

    /** @hide */
    @SystemApi
    public void setEnabled(boolean enabled) {
        setEnabled(ManagerUtil.getEnabled(enabled));
    }

    /** @hide */
    @SystemApi
    public boolean isFocused() {
        return mIsFocused;
    }

    /** @hide */
    @SystemApi
    public void setFocused(boolean focused) {
        mIsFocused = focused;
    }

    /** @hide */
    @SystemApi
    public Bundle getExtras() {
        return mExtras;
    }

    /** @hide */
    @SystemApi
    public void setExtras(Bundle extras) {
        this.mExtras = extras;
    }

    /** @hide */
    @SystemApi
    public void addInfo(String key, Object value) {
        if (mExtras == null) {
            mExtras = new Bundle();
        }
        mExtras.putObject(key, value);
    }

    /** @hide */
    @SystemApi
    public Object getInfo(String key) {
        if (mExtras != null) {
            return mExtras.get(key);
        }
        return null;
    }

    /** @hide */
    @SystemApi
    public void getInfo(String key, int value) {
        if (mExtras == null) {
            mExtras = new Bundle();
        }
        mExtras.putInt(key, value);
    }

    /** @hide */
    @SystemApi
    public Intent getIntent() {
        return mIntent;
    }

    /** @hide */
    @SystemApi
    public void setIntent(Intent intent) {
        this.mIntent = intent;
    }

    /** @hide */
    @SystemApi
    public void initChildPreferences() {
        mChildPrefCompats = new ArrayList<>();
    }

    /** @hide */
    @SystemApi
    public void addChildPrefCompat(PreferenceCompat childPrefCompat) {
        if (mChildPrefCompats == null) {
            mChildPrefCompats = new ArrayList<>();
        }
        mChildPrefCompats.add(childPrefCompat);
    }

    @Override
    public String toString() {
        return "PreferenceCompat{"
                + "mKey=" + Arrays.toString(mKey)
                + ", mTitle='" + mTitle + '\''
                + ", mSummary='" + mSummary + '\''
                + ", mContentDescription='" + mContentDescription
                + '\'' + ", mExtras=" + mExtras
                + ", mIntent=" + mIntent
                + ", mIcon=" + mIcon
                + ", mValue='" + mValue + '\''
                + ", mType=" + mType
                + ", mChecked=" + mChecked
                + ", mVisible=" + mVisible
                + ", mSelectable=" + mSelectable
                + ", mEnabled=" + mEnabled
                + ", mShouldRemove=" + mShouldRemove
                + ", mHasOnPreferenceChangeListener=" + mHasOnPreferenceChangeListener
                + ", mNextState=" + mNextState
                + ", mChildPrefCompats=" + mChildPrefCompats
                + '}';
    }
}