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; 18 import android.app.Dialog; 19 import android.app.DialogFragment; 20 import android.content.Context; 21 import android.content.DialogInterface; 22 import android.content.DialogInterface.OnClickListener; 23 import android.os.Bundle; 24 import android.util.AttributeSet; 25 26 import androidx.preference.ListPreference; 27 import androidx.preference.ListPreferenceDialogFragment; 28 29 public class CustomListPreference extends ListPreference { 30 CustomListPreference(Context context, AttributeSet attrs)31 public CustomListPreference(Context context, AttributeSet attrs) { 32 super(context, attrs); 33 } 34 CustomListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)35 public CustomListPreference(Context context, AttributeSet attrs, int defStyleAttr, 36 int defStyleRes) { 37 super(context, attrs, defStyleAttr, defStyleRes); 38 } 39 onPrepareDialogBuilder(AlertDialog.Builder builder, OnClickListener listener)40 protected void onPrepareDialogBuilder(AlertDialog.Builder builder, 41 OnClickListener listener) { 42 } 43 onDialogClosed(boolean positiveResult)44 protected void onDialogClosed(boolean positiveResult) { 45 } 46 onDialogCreated(DialogFragment fragment, Dialog dialog)47 protected Dialog onDialogCreated(DialogFragment fragment, Dialog dialog) { 48 return dialog; 49 } 50 isAutoClosePreference()51 protected boolean isAutoClosePreference() { 52 return true; 53 } 54 55 /** 56 * Called when a user is about to choose the given value, to determine if we 57 * should show a confirmation dialog. 58 * 59 * @param value the value the user is about to choose 60 * @return the message to show in a confirmation dialog, or {@code null} to 61 * not request confirmation 62 */ getConfirmationMessage(String value)63 protected CharSequence getConfirmationMessage(String value) { 64 return null; 65 } 66 onDialogStateRestored(DialogFragment fragment, Dialog dialog, Bundle savedInstanceState)67 protected void onDialogStateRestored(DialogFragment fragment, Dialog dialog, 68 Bundle savedInstanceState) { 69 } 70 71 public static class CustomListPreferenceDialogFragment extends ListPreferenceDialogFragment { 72 73 private static final String KEY_CLICKED_ENTRY_INDEX 74 = "settings.CustomListPrefDialog.KEY_CLICKED_ENTRY_INDEX"; 75 76 private int mClickedDialogEntryIndex; 77 newInstance(String key)78 public static ListPreferenceDialogFragment newInstance(String key) { 79 final ListPreferenceDialogFragment fragment = new CustomListPreferenceDialogFragment(); 80 final Bundle b = new Bundle(1); 81 b.putString(ARG_KEY, key); 82 fragment.setArguments(b); 83 return fragment; 84 } 85 getCustomizablePreference()86 public CustomListPreference getCustomizablePreference() { 87 return (CustomListPreference) getPreference(); 88 } 89 90 @Override onPrepareDialogBuilder(AlertDialog.Builder builder)91 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { 92 super.onPrepareDialogBuilder(builder); 93 mClickedDialogEntryIndex = getCustomizablePreference() 94 .findIndexOfValue(getCustomizablePreference().getValue()); 95 getCustomizablePreference().onPrepareDialogBuilder(builder, getOnItemClickListener()); 96 if (!getCustomizablePreference().isAutoClosePreference()) { 97 builder.setPositiveButton(com.android.internal.R.string.ok, new OnClickListener() { 98 @Override 99 public void onClick(DialogInterface dialog, int which) { 100 onItemConfirmed(); 101 } 102 }); 103 } 104 } 105 106 @Override onCreateDialog(Bundle savedInstanceState)107 public Dialog onCreateDialog(Bundle savedInstanceState) { 108 Dialog dialog = super.onCreateDialog(savedInstanceState); 109 if (savedInstanceState != null) { 110 mClickedDialogEntryIndex = savedInstanceState.getInt(KEY_CLICKED_ENTRY_INDEX, 111 mClickedDialogEntryIndex); 112 } 113 return getCustomizablePreference().onDialogCreated(this, dialog); 114 } 115 116 @Override onSaveInstanceState(Bundle outState)117 public void onSaveInstanceState(Bundle outState) { 118 super.onSaveInstanceState(outState); 119 outState.putInt(KEY_CLICKED_ENTRY_INDEX, mClickedDialogEntryIndex); 120 } 121 122 @Override onActivityCreated(Bundle savedInstanceState)123 public void onActivityCreated(Bundle savedInstanceState) { 124 super.onActivityCreated(savedInstanceState); 125 getCustomizablePreference().onDialogStateRestored(this, getDialog(), savedInstanceState); 126 } 127 getOnItemClickListener()128 protected OnClickListener getOnItemClickListener() { 129 return new OnClickListener() { 130 @Override 131 public void onClick(DialogInterface dialog, int which) { 132 setClickedDialogEntryIndex(which); 133 if (getCustomizablePreference().isAutoClosePreference()) { 134 onItemConfirmed(); 135 } 136 } 137 }; 138 } 139 setClickedDialogEntryIndex(int which)140 protected void setClickedDialogEntryIndex(int which) { 141 mClickedDialogEntryIndex = which; 142 } 143 getValue()144 private String getValue() { 145 final ListPreference preference = getCustomizablePreference(); 146 if (mClickedDialogEntryIndex >= 0 && preference.getEntryValues() != null) { 147 return preference.getEntryValues()[mClickedDialogEntryIndex].toString(); 148 } else { 149 return null; 150 } 151 } 152 onItemConfirmed()153 protected void onItemConfirmed() { 154 onClick(getDialog(), DialogInterface.BUTTON_POSITIVE); 155 getDialog().dismiss(); 156 } 157 158 @Override onDialogClosed(boolean positiveResult)159 public void onDialogClosed(boolean positiveResult) { 160 getCustomizablePreference().onDialogClosed(positiveResult); 161 final ListPreference preference = getCustomizablePreference(); 162 final String value = getValue(); 163 if (positiveResult && value != null) { 164 if (preference.callChangeListener(value)) { 165 preference.setValue(value); 166 } 167 } 168 } 169 } 170 } 171