• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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 
17 package androidx.preference;
18 
19 import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20 
21 import android.os.Bundle;
22 import android.view.View;
23 import android.widget.EditText;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.RestrictTo;
27 
28 public class EditTextPreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat {
29 
30     private static final String SAVE_STATE_TEXT = "EditTextPreferenceDialogFragment.text";
31 
32     private EditText mEditText;
33 
34     private CharSequence mText;
35 
newInstance(String key)36     public static EditTextPreferenceDialogFragmentCompat newInstance(String key) {
37         final EditTextPreferenceDialogFragmentCompat
38                 fragment = new EditTextPreferenceDialogFragmentCompat();
39         final Bundle b = new Bundle(1);
40         b.putString(ARG_KEY, key);
41         fragment.setArguments(b);
42         return fragment;
43     }
44 
45     @Override
onCreate(Bundle savedInstanceState)46     public void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48         if (savedInstanceState == null) {
49             mText = getEditTextPreference().getText();
50         } else {
51             mText = savedInstanceState.getCharSequence(SAVE_STATE_TEXT);
52         }
53     }
54 
55     @Override
onSaveInstanceState(@onNull Bundle outState)56     public void onSaveInstanceState(@NonNull Bundle outState) {
57         super.onSaveInstanceState(outState);
58         outState.putCharSequence(SAVE_STATE_TEXT, mText);
59     }
60 
61     @Override
onBindDialogView(View view)62     protected void onBindDialogView(View view) {
63         super.onBindDialogView(view);
64 
65         mEditText = view.findViewById(android.R.id.edit);
66         mEditText.requestFocus();
67 
68         if (mEditText == null) {
69             throw new IllegalStateException("Dialog view must contain an EditText with id" +
70                     " @android:id/edit");
71         }
72 
73         mEditText.setText(mText);
74         // Place cursor at the end
75         mEditText.setSelection(mEditText.getText().length());
76     }
77 
getEditTextPreference()78     private EditTextPreference getEditTextPreference() {
79         return (EditTextPreference) getPreference();
80     }
81 
82     /** @hide */
83     @RestrictTo(LIBRARY_GROUP)
84     @Override
needInputMethod()85     protected boolean needInputMethod() {
86         // We want the input method to show, if possible, when dialog is displayed
87         return true;
88     }
89 
90     @Override
onDialogClosed(boolean positiveResult)91     public void onDialogClosed(boolean positiveResult) {
92 
93         if (positiveResult) {
94             String value = mEditText.getText().toString();
95             if (getEditTextPreference().callChangeListener(value)) {
96                 getEditTextPreference().setText(value);
97             }
98         }
99     }
100 
101 }
102