blob: 4d2ac67e42a87a0cfb28858fc8576e8e8bef7c23 [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 Inwoodeac8d0a2018-08-17 13:51:26 +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}.
45 */
46public class EditTextPreference extends DialogPreference {
47 /**
48 * The edit text shown in the dialog.
49 */
Mathew Inwoodeac8d0a2018-08-17 13:51:26 +010050 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 private EditText mEditText;
52
53 private String mText;
mariagpuyol3dc88c82016-03-08 10:37:50 -080054 private boolean mTextSet;
Alan Viverette617feb92013-09-09 18:09:13 -070055
56 public EditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
57 super(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
59 mEditText = new EditText(context, attrs);
60
61 // Give it an ID so it can be saved/restored
62 mEditText.setId(com.android.internal.R.id.edit);
63
64 /*
65 * The preference framework and view framework both have an 'enabled'
66 * attribute. Most likely, the 'enabled' specified in this XML is for
67 * the preference framework, but it was also given to the view framework.
68 * We reset the enabled state.
69 */
70 mEditText.setEnabled(true);
71 }
72
Alan Viverette599d2a42013-09-16 13:48:29 -070073 public EditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
74 this(context, attrs, defStyleAttr, 0);
75 }
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 public EditTextPreference(Context context, AttributeSet attrs) {
78 this(context, attrs, com.android.internal.R.attr.editTextPreferenceStyle);
79 }
80
81 public EditTextPreference(Context context) {
82 this(context, null);
83 }
84
85 /**
86 * Saves the text to the {@link SharedPreferences}.
87 *
88 * @param text The text to save
89 */
90 public void setText(String text) {
mariagpuyol3dc88c82016-03-08 10:37:50 -080091 // Always persist/notify the first time.
92 final boolean changed = !TextUtils.equals(mText, text);
93 if (changed || !mTextSet) {
94 mText = text;
95 mTextSet = true;
96 persistString(text);
97 if(changed) {
98 notifyDependencyChange(shouldDisableDependents());
99 notifyChanged();
100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 }
102 }
103
104 /**
105 * Gets the text from the {@link SharedPreferences}.
106 *
107 * @return The current preference value.
108 */
109 public String getText() {
110 return mText;
111 }
112
113 @Override
114 protected void onBindDialogView(View view) {
115 super.onBindDialogView(view);
116
117 EditText editText = mEditText;
118 editText.setText(getText());
119
120 ViewParent oldParent = editText.getParent();
121 if (oldParent != view) {
122 if (oldParent != null) {
123 ((ViewGroup) oldParent).removeView(editText);
124 }
125 onAddEditTextToDialogView(view, editText);
126 }
127 }
128
129 /**
130 * Adds the EditText widget of this preference to the dialog's view.
131 *
132 * @param dialogView The dialog view.
133 */
134 protected void onAddEditTextToDialogView(View dialogView, EditText editText) {
135 ViewGroup container = (ViewGroup) dialogView
136 .findViewById(com.android.internal.R.id.edittext_container);
137 if (container != null) {
Romain Guy980a9382010-01-08 15:06:28 -0800138 container.addView(editText, ViewGroup.LayoutParams.MATCH_PARENT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 ViewGroup.LayoutParams.WRAP_CONTENT);
140 }
141 }
142
143 @Override
144 protected void onDialogClosed(boolean positiveResult) {
145 super.onDialogClosed(positiveResult);
146
147 if (positiveResult) {
148 String value = mEditText.getText().toString();
149 if (callChangeListener(value)) {
150 setText(value);
151 }
152 }
153 }
154
155 @Override
156 protected Object onGetDefaultValue(TypedArray a, int index) {
157 return a.getString(index);
158 }
159
160 @Override
161 protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
162 setText(restoreValue ? getPersistedString(mText) : (String) defaultValue);
163 }
164
165 @Override
166 public boolean shouldDisableDependents() {
167 return TextUtils.isEmpty(mText) || super.shouldDisableDependents();
168 }
169
170 /**
171 * Returns the {@link EditText} widget that will be shown in the dialog.
172 *
173 * @return The {@link EditText} widget that will be shown in the dialog.
174 */
175 public EditText getEditText() {
176 return mEditText;
177 }
178
Amith Yamasani1d458572009-09-15 10:25:51 -0700179 /** @hide */
180 @Override
181 protected boolean needInputMethod() {
182 // We want the input method to show, if possible, when dialog is displayed
183 return true;
184 }
185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 @Override
187 protected Parcelable onSaveInstanceState() {
188 final Parcelable superState = super.onSaveInstanceState();
189 if (isPersistent()) {
190 // No need to save instance state since it's persistent
191 return superState;
192 }
193
194 final SavedState myState = new SavedState(superState);
195 myState.text = getText();
196 return myState;
197 }
198
199 @Override
200 protected void onRestoreInstanceState(Parcelable state) {
201 if (state == null || !state.getClass().equals(SavedState.class)) {
202 // Didn't save state for us in onSaveInstanceState
203 super.onRestoreInstanceState(state);
204 return;
205 }
206
207 SavedState myState = (SavedState) state;
208 super.onRestoreInstanceState(myState.getSuperState());
209 setText(myState.text);
210 }
211
212 private static class SavedState extends BaseSavedState {
213 String text;
214
215 public SavedState(Parcel source) {
216 super(source);
217 text = source.readString();
218 }
219
220 @Override
221 public void writeToParcel(Parcel dest, int flags) {
222 super.writeToParcel(dest, flags);
223 dest.writeString(text);
224 }
225
226 public SavedState(Parcelable superState) {
227 super(superState);
228 }
229
230 public static final Parcelable.Creator<SavedState> CREATOR =
231 new Parcelable.Creator<SavedState>() {
232 public SavedState createFromParcel(Parcel in) {
233 return new SavedState(in);
234 }
235
236 public SavedState[] newArray(int size) {
237 return new SavedState[size];
238 }
239 };
240 }
241
242}