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