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
17package android.support.v7.preference;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.support.v4.content.res.TypedArrayUtils;
24import android.text.TextUtils;
25import android.util.AttributeSet;
26import android.widget.EditText;
27
28/**
29 * A {@link Preference} that allows for string
30 * input.
31 * <p>
32 * It is a subclass of {@link DialogPreference} and shows the {@link EditText}
33 * in a dialog.
34 * <p>
35 * This preference will store a string into the SharedPreferences.
36 */
37public class EditTextPreference extends DialogPreference {
38    private String mText;
39
40    public EditTextPreference(Context context, AttributeSet attrs, int defStyleAttr,
41            int defStyleRes) {
42        super(context, attrs, defStyleAttr, defStyleRes);
43    }
44
45    public EditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
46        this(context, attrs, defStyleAttr, 0);
47    }
48
49    public EditTextPreference(Context context, AttributeSet attrs) {
50        this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.editTextPreferenceStyle,
51                AndroidResources.ANDROID_R_EDITTEXT_PREFERENCE_STYLE));
52    }
53
54    public EditTextPreference(Context context) {
55        this(context, null);
56    }
57
58    /**
59     * Saves the text to the {@link android.content.SharedPreferences}.
60     *
61     * @param text The text to save
62     */
63    public void setText(String text) {
64        final boolean wasBlocking = shouldDisableDependents();
65
66        mText = text;
67
68        persistString(text);
69
70        final boolean isBlocking = shouldDisableDependents();
71        if (isBlocking != wasBlocking) {
72            notifyDependencyChange(isBlocking);
73        }
74    }
75
76    /**
77     * Gets the text from the {@link android.content.SharedPreferences}.
78     *
79     * @return The current preference value.
80     */
81    public String getText() {
82        return mText;
83    }
84
85    @Override
86    protected Object onGetDefaultValue(TypedArray a, int index) {
87        return a.getString(index);
88    }
89
90    @Override
91    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
92        setText(restoreValue ? getPersistedString(mText) : (String) defaultValue);
93    }
94
95    @Override
96    public boolean shouldDisableDependents() {
97        return TextUtils.isEmpty(mText) || super.shouldDisableDependents();
98    }
99
100    @Override
101    protected Parcelable onSaveInstanceState() {
102        final Parcelable superState = super.onSaveInstanceState();
103        if (isPersistent()) {
104            // No need to save instance state since it's persistent
105            return superState;
106        }
107
108        final SavedState myState = new SavedState(superState);
109        myState.text = getText();
110        return myState;
111    }
112
113    @Override
114    protected void onRestoreInstanceState(Parcelable state) {
115        if (state == null || !state.getClass().equals(SavedState.class)) {
116            // Didn't save state for us in onSaveInstanceState
117            super.onRestoreInstanceState(state);
118            return;
119        }
120
121        SavedState myState = (SavedState) state;
122        super.onRestoreInstanceState(myState.getSuperState());
123        setText(myState.text);
124    }
125
126    private static class SavedState extends BaseSavedState {
127        String text;
128
129        public SavedState(Parcel source) {
130            super(source);
131            text = source.readString();
132        }
133
134        @Override
135        public void writeToParcel(Parcel dest, int flags) {
136            super.writeToParcel(dest, flags);
137            dest.writeString(text);
138        }
139
140        public SavedState(Parcelable superState) {
141            super(superState);
142        }
143
144        public static final Parcelable.Creator<SavedState> CREATOR =
145                new Parcelable.Creator<SavedState>() {
146            @Override
147            public SavedState createFromParcel(Parcel in) {
148                return new SavedState(in);
149            }
150
151            @Override
152            public SavedState[] newArray(int size) {
153                return new SavedState[size];
154            }
155        };
156    }
157
158}
159