The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.preference; |
| 18 | |
| 19 | |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame] | 20 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.SharedPreferences; |
| 23 | import android.content.res.TypedArray; |
| 24 | import android.os.Parcel; |
| 25 | import android.os.Parcelable; |
| 26 | import android.text.TextUtils; |
| 27 | import android.util.AttributeSet; |
| 28 | import android.view.View; |
| 29 | import android.view.ViewGroup; |
| 30 | import android.view.ViewParent; |
| 31 | import android.widget.EditText; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 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 | */ |
| 46 | public class EditTextPreference extends DialogPreference { |
| 47 | /** |
| 48 | * The edit text shown in the dialog. |
| 49 | */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame] | 50 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | private EditText mEditText; |
| 52 | |
| 53 | private String mText; |
mariagpuyol | 3dc88c8 | 2016-03-08 10:37:50 -0800 | [diff] [blame] | 54 | private boolean mTextSet; |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 55 | |
| 56 | public EditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 57 | super(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | |
| 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 Viverette | 599d2a4 | 2013-09-16 13:48:29 -0700 | [diff] [blame] | 73 | public EditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 74 | this(context, attrs, defStyleAttr, 0); |
| 75 | } |
| 76 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | 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) { |
mariagpuyol | 3dc88c8 | 2016-03-08 10:37:50 -0800 | [diff] [blame] | 91 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | } |
| 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 Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 138 | container.addView(editText, ViewGroup.LayoutParams.MATCH_PARENT, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | 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 Yamasani | 1d45857 | 2009-09-15 10:25:51 -0700 | [diff] [blame] | 179 | /** @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | @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 | } |