1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.tuner;
16
17import android.app.AlertDialog.Builder;
18import android.app.Dialog;
19import android.app.DialogFragment;
20import android.app.Fragment;
21import android.content.Context;
22import android.content.DialogInterface.OnClickListener;
23import android.os.Bundle;
24import android.support.v14.preference.PreferenceFragment;
25import android.support.v7.preference.Preference;
26import android.support.v7.preference.PreferenceScreen;
27import android.util.AttributeSet;
28import android.util.Log;
29import android.view.View;
30import android.widget.Toolbar;
31
32import com.android.settingslib.Utils;
33import com.android.systemui.fragments.FragmentHostManager;
34import com.android.systemui.R;
35
36import libcore.util.Objects;
37
38public class RadioListPreference extends CustomListPreference {
39
40    private OnClickListener mOnClickListener;
41    private CharSequence mSummary;
42
43    public RadioListPreference(Context context, AttributeSet attrs) {
44        super(context, attrs);
45    }
46
47    @Override
48    protected void onPrepareDialogBuilder(Builder builder, OnClickListener listener) {
49        mOnClickListener = listener;
50    }
51
52    @Override
53    public void setSummary(CharSequence summary) {
54        super.setSummary(summary);
55        mSummary = summary;
56    }
57
58    @Override
59    public CharSequence getSummary() {
60        if (mSummary == null || mSummary.toString().contains("%s")) {
61            return super.getSummary();
62        }
63        return mSummary;
64    }
65
66    @Override
67    protected Dialog onDialogCreated(DialogFragment fragment, Dialog dialog) {
68        Dialog d = new Dialog(getContext(), android.R.style.Theme_DeviceDefault_Settings);
69        Toolbar t = (Toolbar) d.findViewById(com.android.internal.R.id.action_bar);
70        View v = new View(getContext());
71        v.setId(R.id.content);
72        d.setContentView(v);
73        t.setTitle(getTitle());
74        t.setNavigationIcon(Utils.getDrawable(d.getContext(), android.R.attr.homeAsUpIndicator));
75        t.setNavigationOnClickListener(view -> d.dismiss());
76
77        RadioFragment f = new RadioFragment();
78        f.setPreference(this);
79        FragmentHostManager.get(v).getFragmentManager()
80                .beginTransaction()
81                .add(android.R.id.content, f)
82                .commit();
83        return d;
84    }
85
86    @Override
87    protected void onDialogStateRestored(DialogFragment fragment, Dialog dialog,
88            Bundle savedInstanceState) {
89        super.onDialogStateRestored(fragment, dialog, savedInstanceState);
90        View view = dialog.findViewById(R.id.content);
91        RadioFragment radioFragment = (RadioFragment) FragmentHostManager.get(view)
92                .getFragmentManager().findFragmentById(R.id.content);
93        if (radioFragment != null) {
94            radioFragment.setPreference(this);
95        }
96    }
97
98    @Override
99    protected void onDialogClosed(boolean positiveResult) {
100        super.onDialogClosed(positiveResult);
101    }
102
103    public static class RadioFragment extends TunerPreferenceFragment {
104        private RadioListPreference mListPref;
105
106        @Override
107        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
108            Context context = getPreferenceManager().getContext();
109            PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(context);
110            setPreferenceScreen(screen);
111            if (mListPref != null) {
112                update();
113            }
114        }
115
116        private void update() {
117            Context context = getPreferenceManager().getContext();
118
119            CharSequence[] entries = mListPref.getEntries();
120            CharSequence[] values = mListPref.getEntryValues();
121            CharSequence current = mListPref.getValue();
122            for (int i = 0; i < entries.length; i++) {
123                CharSequence entry = entries[i];
124                SelectablePreference pref = new SelectablePreference(context);
125                getPreferenceScreen().addPreference(pref);
126                pref.setTitle(entry);
127                pref.setChecked(Objects.equal(current, values[i]));
128                pref.setKey(String.valueOf(i));
129            }
130        }
131
132        @Override
133        public boolean onPreferenceTreeClick(Preference preference) {
134            mListPref.mOnClickListener.onClick(null, Integer.parseInt(preference.getKey()));
135            return true;
136        }
137
138        public void setPreference(RadioListPreference radioListPreference) {
139            mListPref = radioListPreference;
140            if (getPreferenceManager() != null) {
141                update();
142            }
143        }
144    }
145}
146