blob: c09cec80139c7c9f7d5020e377425f731f13fd32 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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
17package android.preference;
18
19
Mathew Inwoodf2217132018-08-17 13:41:55 +010020import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Context;
22import android.content.SharedPreferences;
23import android.content.res.TypedArray;
24import android.os.Parcel;
25import android.os.Parcelable;
26import android.text.TextUtils;
27import android.util.AttributeSet;
28import android.view.View;
29import android.view.ViewGroup;
30import android.view.ViewParent;
31import android.widget.EditText;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
33/**
34 * A {@link Preference} that allows for string
35 * input.
36 * <p>
37 * It is a subclass of {@link DialogPreference} and shows the {@link EditText}
38 * in a dialog. This {@link EditText} can be modified either programmatically
39 * via {@link #getEditText()}, or through XML by setting any EditText
40 * attributes on the EditTextPreference.
41 * <p>
42 * This preference will store a string into the SharedPreferences.
43 * <p>
44 * See {@link android.R.styleable#EditText EditText Attributes}.
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +000045 *
46 * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
47 * <a href="{@docRoot}reference/androidx/preference/package-summary.html">
48 * Preference Library</a> for consistent behavior across all devices. For more information on
49 * using the AndroidX Preference Library see
50 * <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 */
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +000052@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053public class EditTextPreference extends DialogPreference {
54 /**
55 * The edit text shown in the dialog.
56 */
Mathew Inwoodf2217132018-08-17 13:41:55 +010057 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 private EditText mEditText;
59
60 private String mText;
mariagpuyol3dc88c82016-03-08 10:37:50 -080061 private boolean mTextSet;
Alan Viverette617feb92013-09-09 18:09:13 -070062
63 public EditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
64 super(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
66 mEditText = new EditText(context, attrs);
67
68 // Give it an ID so it can be saved/restored
69 mEditText.setId(com.android.internal.R.id.edit);
70
71 /*
72 * The preference framework and view framework both have an 'enabled'
73 * attribute. Most likely, the 'enabled' specified in this XML is for
74 * the preference framework, but it was also given to the view framework.
75 * We reset the enabled state.
76 */
77 mEditText.setEnabled(true);
78 }
79
Alan Viverette599d2a42013-09-16 13:48:29 -070080 public EditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
81 this(context, attrs, defStyleAttr, 0);
82 }
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 public EditTextPreference(Context context, AttributeSet attrs) {
85 this(context, attrs, com.android.internal.R.attr.editTextPreferenceStyle);
86 }
87
88 public EditTextPreference(Context context) {
89 this(context, null);
90 }
91
92 /**
93 * Saves the text to the {@link SharedPreferences}.
94 *
95 * @param text The text to save
96 */
97 public void setText(String text) {
mariagpuyol3dc88c82016-03-08 10:37:50 -080098 // Always persist/notify the first time.
99 final boolean changed = !TextUtils.equals(mText, text);
100 if (changed || !mTextSet) {
101 mText = text;
102 mTextSet = true;
103 persistString(text);
104 if(changed) {
105 notifyDependencyChange(shouldDisableDependents());
106 notifyChanged();
107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 }
109 }
110
111 /**
112 * Gets the text from the {@link SharedPreferences}.
113 *
114 * @return The current preference value.
115 */
116 public String getText() {
117 return mText;
118 }
119
120 @Override
121 protected void onBindDialogView(View view) {
122 super.onBindDialogView(view);
123
124 EditText editText = mEditText;
125 editText.setText(getText());
126
127 ViewParent oldParent = editText.getParent();
128 if (oldParent != view) {
129 if (oldParent != null) {
130 ((ViewGroup) oldParent).removeView(editText);
131 }
132 onAddEditTextToDialogView(view, editText);
133 }
134 }
135
136 /**
137 * Adds the EditText widget of this preference to the dialog's view.
138 *
139 * @param dialogView The dialog view.
140 */
141 protected void onAddEditTextToDialogView(View dialogView, EditText editText) {
142 ViewGroup container = (ViewGroup) dialogView
143 .findViewById(com.android.internal.R.id.edittext_container);
144 if (container != null) {
Romain Guy980a9382010-01-08 15:06:28 -0800145 container.addView(editText, ViewGroup.LayoutParams.MATCH_PARENT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 ViewGroup.LayoutParams.WRAP_CONTENT);
147 }
148 }
149
150 @Override
151 protected void onDialogClosed(boolean positiveResult) {
152 super.onDialogClosed(positiveResult);
153
154 if (positiveResult) {
155 String value = mEditText.getText().toString();
156 if (callChangeListener(value)) {
157 setText(value);
158 }
159 }
160 }
161
162 @Override
163 protected Object onGetDefaultValue(TypedArray a, int index) {
164 return a.getString(index);
165 }
166
167 @Override
168 protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
169 setText(restoreValue ? getPersistedString(mText) : (String) defaultValue);
170 }
171
172 @Override
173 public boolean shouldDisableDependents() {
174 return TextUtils.isEmpty(mText) || super.shouldDisableDependents();
175 }
176
177 /**
178 * Returns the {@link EditText} widget that will be shown in the dialog.
179 *
180 * @return The {@link EditText} widget that will be shown in the dialog.
181 */
182 public EditText getEditText() {
183 return mEditText;
184 }
185
Amith Yamasani1d458572009-09-15 10:25:51 -0700186 /** @hide */
187 @Override
188 protected boolean needInputMethod() {
189 // We want the input method to show, if possible, when dialog is displayed
190 return true;
191 }
192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 @Override
194 protected Parcelable onSaveInstanceState() {
195 final Parcelable superState = super.onSaveInstanceState();
196 if (isPersistent()) {
197 // No need to save instance state since it's persistent
198 return superState;
199 }
200
201 final SavedState myState = new SavedState(superState);
202 myState.text = getText();
203 return myState;
204 }
205
206 @Override
207 protected void onRestoreInstanceState(Parcelable state) {
208 if (state == null || !state.getClass().equals(SavedState.class)) {
209 // Didn't save state for us in onSaveInstanceState
210 super.onRestoreInstanceState(state);
211 return;
212 }
213
214 SavedState myState = (SavedState) state;
215 super.onRestoreInstanceState(myState.getSuperState());
216 setText(myState.text);
217 }
218
219 private static class SavedState extends BaseSavedState {
220 String text;
221
222 public SavedState(Parcel source) {
223 super(source);
224 text = source.readString();
225 }
226
227 @Override
228 public void writeToParcel(Parcel dest, int flags) {
229 super.writeToParcel(dest, flags);
230 dest.writeString(text);
231 }
232
233 public SavedState(Parcelable superState) {
234 super(superState);
235 }
236
237 public static final Parcelable.Creator<SavedState> CREATOR =
238 new Parcelable.Creator<SavedState>() {
239 public SavedState createFromParcel(Parcel in) {
240 return new SavedState(in);
241 }
242
243 public SavedState[] newArray(int size) {
244 return new SavedState[size];
245 }
246 };
247 }
248
249}