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