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 
15 package com.android.systemui.tuner;
16 
17 import android.app.AlertDialog.Builder;
18 import android.app.Dialog;
19 import android.app.DialogFragment;
20 import android.app.Fragment;
21 import android.content.Context;
22 import android.content.DialogInterface.OnClickListener;
23 import android.os.Bundle;
24 import android.support.v14.preference.PreferenceFragment;
25 import android.support.v7.preference.Preference;
26 import android.support.v7.preference.PreferenceScreen;
27 import android.util.AttributeSet;
28 import android.util.Log;
29 import android.view.View;
30 import android.widget.Toolbar;
31 
32 import com.android.settingslib.Utils;
33 import com.android.systemui.fragments.FragmentHostManager;
34 import com.android.systemui.R;
35 
36 import java.util.Objects;
37 
38 public class RadioListPreference extends CustomListPreference {
39 
40     private OnClickListener mOnClickListener;
41     private CharSequence mSummary;
42 
RadioListPreference(Context context, AttributeSet attrs)43     public RadioListPreference(Context context, AttributeSet attrs) {
44         super(context, attrs);
45     }
46 
47     @Override
onPrepareDialogBuilder(Builder builder, OnClickListener listener)48     protected void onPrepareDialogBuilder(Builder builder, OnClickListener listener) {
49         mOnClickListener = listener;
50     }
51 
52     @Override
setSummary(CharSequence summary)53     public void setSummary(CharSequence summary) {
54         super.setSummary(summary);
55         mSummary = summary;
56     }
57 
58     @Override
getSummary()59     public CharSequence getSummary() {
60         if (mSummary == null || mSummary.toString().contains("%s")) {
61             return super.getSummary();
62         }
63         return mSummary;
64     }
65 
66     @Override
onDialogCreated(DialogFragment fragment, Dialog dialog)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
onDialogStateRestored(DialogFragment fragment, Dialog dialog, Bundle savedInstanceState)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
onDialogClosed(boolean positiveResult)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
onCreatePreferences(Bundle savedInstanceState, String rootKey)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 
update()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.equals(current, values[i]));
128                 pref.setKey(String.valueOf(i));
129             }
130         }
131 
132         @Override
onPreferenceTreeClick(Preference preference)133         public boolean onPreferenceTreeClick(Preference preference) {
134             mListPref.mOnClickListener.onClick(null, Integer.parseInt(preference.getKey()));
135             return true;
136         }
137 
setPreference(RadioListPreference radioListPreference)138         public void setPreference(RadioListPreference radioListPreference) {
139             mListPref = radioListPreference;
140             if (getPreferenceManager() != null) {
141                 update();
142             }
143         }
144     }
145 }
146