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.settings; 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.support.v14.preference.EditTextPreferenceDialogFragment; 24 import android.support.v7.preference.EditTextPreference; 25 import android.util.AttributeSet; 26 import android.view.View; 27 import android.widget.EditText; 28 29 public class CustomEditTextPreference extends EditTextPreference { 30 31 private CustomPreferenceDialogFragment mFragment; 32 CustomEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)33 public CustomEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 34 super(context, attrs, defStyleAttr, defStyleRes); 35 } 36 CustomEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr)37 public CustomEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) { 38 super(context, attrs, defStyleAttr); 39 } 40 CustomEditTextPreference(Context context, AttributeSet attrs)41 public CustomEditTextPreference(Context context, AttributeSet attrs) { 42 super(context, attrs); 43 } 44 CustomEditTextPreference(Context context)45 public CustomEditTextPreference(Context context) { 46 super(context); 47 } 48 getEditText()49 public EditText getEditText() { 50 return mFragment != null ? (EditText) mFragment.getDialog().findViewById(android.R.id.edit) 51 : null; 52 } 53 isDialogOpen()54 public boolean isDialogOpen() { 55 return getDialog() != null && getDialog().isShowing(); 56 } 57 getDialog()58 public Dialog getDialog() { 59 return mFragment != null ? mFragment.getDialog() : null; 60 } 61 onPrepareDialogBuilder(AlertDialog.Builder builder, DialogInterface.OnClickListener listener)62 protected void onPrepareDialogBuilder(AlertDialog.Builder builder, 63 DialogInterface.OnClickListener listener) { 64 } 65 onDialogClosed(boolean positiveResult)66 protected void onDialogClosed(boolean positiveResult) { 67 } 68 onClick(DialogInterface dialog, int which)69 protected void onClick(DialogInterface dialog, int which) { 70 } 71 onBindDialogView(View view)72 protected void onBindDialogView(View view) { 73 } 74 setFragment(CustomPreferenceDialogFragment fragment)75 private void setFragment(CustomPreferenceDialogFragment fragment) { 76 mFragment = fragment; 77 } 78 79 public static class CustomPreferenceDialogFragment extends EditTextPreferenceDialogFragment { 80 newInstance(String key)81 public static CustomPreferenceDialogFragment newInstance(String key) { 82 final CustomPreferenceDialogFragment fragment = new CustomPreferenceDialogFragment(); 83 final Bundle b = new Bundle(1); 84 b.putString(ARG_KEY, key); 85 fragment.setArguments(b); 86 return fragment; 87 } 88 getCustomizablePreference()89 private CustomEditTextPreference getCustomizablePreference() { 90 return (CustomEditTextPreference) getPreference(); 91 } 92 93 @Override onBindDialogView(View view)94 protected void onBindDialogView(View view) { 95 super.onBindDialogView(view); 96 getCustomizablePreference().onBindDialogView(view); 97 } 98 99 @Override onPrepareDialogBuilder(AlertDialog.Builder builder)100 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { 101 super.onPrepareDialogBuilder(builder); 102 getCustomizablePreference().setFragment(this); 103 getCustomizablePreference().onPrepareDialogBuilder(builder, this); 104 } 105 106 @Override onDialogClosed(boolean positiveResult)107 public void onDialogClosed(boolean positiveResult) { 108 super.onDialogClosed(positiveResult); 109 getCustomizablePreference().onDialogClosed(positiveResult); 110 } 111 112 @Override onClick(DialogInterface dialog, int which)113 public void onClick(DialogInterface dialog, int which) { 114 super.onClick(dialog, which); 115 getCustomizablePreference().onClick(dialog, which); 116 } 117 } 118 } 119