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.content.Context;
21 import android.content.DialogInterface.OnClickListener;
22 import android.os.Bundle;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.widget.Toolbar;
26 
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.settingslib.Utils;
31 import com.android.systemui.R;
32 import com.android.systemui.fragments.FragmentHostManager;
33 
34 import java.util.Objects;
35 
36 public class RadioListPreference extends CustomListPreference {
37 
38     private OnClickListener mOnClickListener;
39     private CharSequence mSummary;
40 
RadioListPreference(Context context, AttributeSet attrs)41     public RadioListPreference(Context context, AttributeSet attrs) {
42         super(context, attrs);
43     }
44 
45     @Override
onPrepareDialogBuilder(Builder builder, OnClickListener listener)46     protected void onPrepareDialogBuilder(Builder builder, OnClickListener listener) {
47         mOnClickListener = listener;
48     }
49 
50     @Override
setSummary(CharSequence summary)51     public void setSummary(CharSequence summary) {
52         super.setSummary(summary);
53         mSummary = summary;
54     }
55 
56     @Override
getSummary()57     public CharSequence getSummary() {
58         if (mSummary == null || mSummary.toString().contains("%s")) {
59             return super.getSummary();
60         }
61         return mSummary;
62     }
63 
64     @Override
onDialogCreated(DialogFragment fragment, Dialog dialog)65     protected Dialog onDialogCreated(DialogFragment fragment, Dialog dialog) {
66         Dialog d = new Dialog(getContext(), android.R.style.Theme_DeviceDefault_Settings);
67         Toolbar t = (Toolbar) d.findViewById(com.android.internal.R.id.action_bar);
68         View v = new View(getContext());
69         v.setId(R.id.content);
70         d.setContentView(v);
71         t.setTitle(getTitle());
72         t.setNavigationIcon(Utils.getDrawable(d.getContext(), android.R.attr.homeAsUpIndicator));
73         t.setNavigationOnClickListener(view -> d.dismiss());
74 
75         RadioFragment f = new RadioFragment();
76         f.setPreference(this);
77         FragmentHostManager.get(v).getFragmentManager()
78                 .beginTransaction()
79                 .add(android.R.id.content, f)
80                 .commit();
81         return d;
82     }
83 
84     @Override
onDialogStateRestored(DialogFragment fragment, Dialog dialog, Bundle savedInstanceState)85     protected void onDialogStateRestored(DialogFragment fragment, Dialog dialog,
86             Bundle savedInstanceState) {
87         super.onDialogStateRestored(fragment, dialog, savedInstanceState);
88         View view = dialog.findViewById(R.id.content);
89         RadioFragment radioFragment = (RadioFragment) FragmentHostManager.get(view)
90                 .getFragmentManager().findFragmentById(R.id.content);
91         if (radioFragment != null) {
92             radioFragment.setPreference(this);
93         }
94     }
95 
96     @Override
onDialogClosed(boolean positiveResult)97     protected void onDialogClosed(boolean positiveResult) {
98         super.onDialogClosed(positiveResult);
99     }
100 
101     public static class RadioFragment extends TunerPreferenceFragment {
102         private RadioListPreference mListPref;
103 
104         @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)105         public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
106             Context context = getPreferenceManager().getContext();
107             PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(context);
108             setPreferenceScreen(screen);
109             if (mListPref != null) {
110                 update();
111             }
112         }
113 
update()114         private void update() {
115             Context context = getPreferenceManager().getContext();
116 
117             CharSequence[] entries = mListPref.getEntries();
118             CharSequence[] values = mListPref.getEntryValues();
119             CharSequence current = mListPref.getValue();
120             for (int i = 0; i < entries.length; i++) {
121                 CharSequence entry = entries[i];
122                 SelectablePreference pref = new SelectablePreference(context);
123                 getPreferenceScreen().addPreference(pref);
124                 pref.setTitle(entry);
125                 pref.setChecked(Objects.equals(current, values[i]));
126                 pref.setKey(String.valueOf(i));
127             }
128         }
129 
130         @Override
onPreferenceTreeClick(Preference preference)131         public boolean onPreferenceTreeClick(Preference preference) {
132             mListPref.mOnClickListener.onClick(null, Integer.parseInt(preference.getKey()));
133             return true;
134         }
135 
setPreference(RadioListPreference radioListPreference)136         public void setPreference(RadioListPreference radioListPreference) {
137             mListPref = radioListPreference;
138             if (getPreferenceManager() != null) {
139                 update();
140             }
141         }
142     }
143 }
144