aboutsummaryrefslogtreecommitdiff
path: root/TestMediaApp/src/com/android/car/media/testmediaapp/prefs/TmaPrefsFragment.java
blob: 482de164768bb7bb352b03ef2d6bf39f5eeb2255 (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
/*
 * Copyright 2019 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.car.media.testmediaapp.prefs;

import android.content.Context;
import android.os.Bundle;

import androidx.preference.DropDownPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;

import com.android.car.media.testmediaapp.prefs.TmaEnumPrefs.TmaAccountType;
import com.android.car.media.testmediaapp.prefs.TmaEnumPrefs.TmaBrowseNodeType;
import com.android.car.media.testmediaapp.prefs.TmaEnumPrefs.TmaReplyDelay;
import com.android.car.media.testmediaapp.prefs.TmaPrefs.PrefEntry;

public class TmaPrefsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

        Context context = getPreferenceManager().getContext();
        TmaPrefs prefs = TmaPrefs.getInstance(context);

        PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(context);
        screen.addPreference(createEnumPref(context, "Account Type", prefs.mAccountType,
                TmaAccountType.values()));
        screen.addPreference(createEnumPref(context, "Root node type", prefs.mRootNodeType,
                TmaBrowseNodeType.values()));
        screen.addPreference(createEnumPref(context, "Root reply delay", prefs.mRootReplyDelay,
                TmaReplyDelay.values()));
        screen.addPreference(createEnumPref(context, "Asset delay: random value in [v, 2v]",
                prefs.mAssetReplyDelay, TmaReplyDelay.values()));

        setPreferenceScreen(screen);
    }

    private <T extends TmaEnumPrefs.EnumPrefValue> Preference createEnumPref(
            Context context, String title, PrefEntry pref, T[] enumValues) {
        DropDownPreference prefWidget = new DropDownPreference(context);
        prefWidget.setKey(pref.mKey);
        prefWidget.setTitle(title);
        prefWidget.setSummary("%s");
        prefWidget.setPersistent(true);

        int count = enumValues.length;
        CharSequence[] entries = new CharSequence[count];
        CharSequence[] entryValues = new CharSequence[count];
        for (int i = 0; i < count; i++) {
            entries[i] = enumValues[i].getTitle();
            entryValues[i] = enumValues[i].getId();
        }
        prefWidget.setEntries(entries);
        prefWidget.setEntryValues(entryValues);
        return prefWidget;
    }
}