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