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