1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.settingslib; 17 18 import android.app.AlertDialog; 19 import android.app.Dialog; 20 import android.content.Context; 21 import android.content.DialogInterface; 22 import android.os.Bundle; 23 import android.util.AttributeSet; 24 import android.view.View; 25 26 import androidx.preference.DialogPreference; 27 import androidx.preference.PreferenceDialogFragment; 28 29 /** 30 * Framework version is deprecated, use the compat version instead. 31 * 32 * @deprecated 33 */ 34 @Deprecated 35 public class CustomDialogPreference extends DialogPreference { 36 37 private CustomPreferenceDialogFragment mFragment; 38 private DialogInterface.OnShowListener mOnShowListener; 39 CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)40 public CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr, 41 int defStyleRes) { 42 super(context, attrs, defStyleAttr, defStyleRes); 43 } 44 CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr)45 public CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr) { 46 super(context, attrs, defStyleAttr); 47 } 48 CustomDialogPreference(Context context, AttributeSet attrs)49 public CustomDialogPreference(Context context, AttributeSet attrs) { 50 super(context, attrs); 51 } 52 CustomDialogPreference(Context context)53 public CustomDialogPreference(Context context) { 54 super(context); 55 } 56 isDialogOpen()57 public boolean isDialogOpen() { 58 return getDialog() != null && getDialog().isShowing(); 59 } 60 getDialog()61 public Dialog getDialog() { 62 return mFragment != null ? mFragment.getDialog() : null; 63 } 64 setOnShowListener(DialogInterface.OnShowListener listner)65 public void setOnShowListener(DialogInterface.OnShowListener listner) { 66 mOnShowListener = listner; 67 } 68 onPrepareDialogBuilder(AlertDialog.Builder builder, DialogInterface.OnClickListener listener)69 protected void onPrepareDialogBuilder(AlertDialog.Builder builder, 70 DialogInterface.OnClickListener listener) { 71 } 72 onDialogClosed(boolean positiveResult)73 protected void onDialogClosed(boolean positiveResult) { 74 } 75 onClick(DialogInterface dialog, int which)76 protected void onClick(DialogInterface dialog, int which) { 77 } 78 onBindDialogView(View view)79 protected void onBindDialogView(View view) { 80 } 81 setFragment(CustomPreferenceDialogFragment fragment)82 private void setFragment(CustomPreferenceDialogFragment fragment) { 83 mFragment = fragment; 84 } 85 getOnShowListener()86 private DialogInterface.OnShowListener getOnShowListener() { 87 return mOnShowListener; 88 } 89 90 public static class CustomPreferenceDialogFragment extends PreferenceDialogFragment { 91 newInstance(String key)92 public static CustomPreferenceDialogFragment newInstance(String key) { 93 final CustomPreferenceDialogFragment fragment = new CustomPreferenceDialogFragment(); 94 final Bundle b = new Bundle(1); 95 b.putString(ARG_KEY, key); 96 fragment.setArguments(b); 97 return fragment; 98 } 99 getCustomizablePreference()100 private CustomDialogPreference getCustomizablePreference() { 101 return (CustomDialogPreference) getPreference(); 102 } 103 104 @Override onPrepareDialogBuilder(AlertDialog.Builder builder)105 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { 106 super.onPrepareDialogBuilder(builder); 107 getCustomizablePreference().setFragment(this); 108 getCustomizablePreference().onPrepareDialogBuilder(builder, this); 109 } 110 111 @Override onDialogClosed(boolean positiveResult)112 public void onDialogClosed(boolean positiveResult) { 113 getCustomizablePreference().onDialogClosed(positiveResult); 114 } 115 116 @Override onBindDialogView(View view)117 protected void onBindDialogView(View view) { 118 super.onBindDialogView(view); 119 getCustomizablePreference().onBindDialogView(view); 120 } 121 122 @Override onCreateDialog(Bundle savedInstanceState)123 public Dialog onCreateDialog(Bundle savedInstanceState) { 124 final Dialog dialog = super.onCreateDialog(savedInstanceState); 125 dialog.setOnShowListener(getCustomizablePreference().getOnShowListener()); 126 return dialog; 127 } 128 129 @Override onClick(DialogInterface dialog, int which)130 public void onClick(DialogInterface dialog, int which) { 131 super.onClick(dialog, which); 132 getCustomizablePreference().onClick(dialog, which); 133 } 134 } 135 } 136