-
Notifications
You must be signed in to change notification settings - Fork 31
/
ValidatedEditTextPreference.java
286 lines (236 loc) · 7.02 KB
/
ValidatedEditTextPreference.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package de.viktorreiser.toolbox.preference;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.EditTextPreference;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
/**
* Ordinary {@link EditTextPreference} which extends {@link ValidatedDialogPreference}.<br>
* <br>
* <i>Depends on</i>: {@link ValidatedDialogPreference}
*
* @author Viktor Reiser <<a href="mailto:viktorreiser@gmx.de">viktorreiser@gmx.de</a>>
*/
public class ValidatedEditTextPreference extends ValidatedDialogPreference {
// PRIVATE ====================================================================================
/** Edit text view which contains input. */
private EditText mEditText;
/** Layout which contains edit text view and placed as margin layout into dialog. */
private LinearLayout mEditTextlayout;
/** Value of preference. */
private String mText;
// PUBLIC =====================================================================================
/**
* Get {@link EditText} containing input of preference.
*
* @return {@link EditText} containing input of preference
*/
public EditText getEditText() {
return mEditText;
}
/**
* Saves text to {@link SharedPreferences}.
*
* @param text
* text to save
*/
public void setText(String text) {
if (text == null) {
text = "";
}
final boolean wasBlocking = shouldDisableDependents();
mText = text;
persistString(text);
final boolean isBlocking = shouldDisableDependents();
if (isBlocking != wasBlocking) {
notifyDependencyChange(isBlocking);
}
}
/**
* Gets current set text of preference.<br>
* <br>
* This might be the given default value for this preference ({@code null} when none given) or
* the persisted {@link SharedPreferences} value.
*
* @return current preference value
*/
public String getText() {
return mText;
}
// OVERRIDDEN =================================================================================
public ValidatedEditTextPreference(Context context) {
super(context, null);
initialize(null);
}
public ValidatedEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(attrs);
}
public ValidatedEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize(attrs);
}
/**
* <i>Overridden for internal use!</i><br>
* <br>
* Add path auto complete view to dialog.
*/
@Override
protected View onCreateDialogView() {
ViewParent parent = mEditTextlayout.getParent();
if (parent != null) {
((ViewGroup) parent).removeView(mEditTextlayout);
}
return mEditTextlayout;
}
/**
* <i>Overridden for internal use!</i><br>
* <br>
* Set edit text value on bind.
*/
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
mEditText.setText(getText());
}
/**
* <i>Overridden for internal use!</i><br>
* <br>
* Show keyboard on dialog show.
*/
@Override
protected void showDialog(Bundle bundle) {
super.showDialog(bundle);
// display soft keyboard on show
getDialog().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
/**
* <i>Overridden for internal use!</i><br>
* <br>
* Change dependents when entered text is empty.
*/
@Override
public boolean shouldDisableDependents() {
return TextUtils.isEmpty(mText) || super.shouldDisableDependents();
}
/**
* <i>Overridden for internal use!</i><br>
* <br>
* Get default value.
*/
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getString(index);
}
/**
* <i>Overridden for internal use!</i><br>
* <br>
* Set initial value.
*/
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
setText(restoreValue ? getPersistedString(null) : (String) defaultValue);
}
/**
* <i>Overridden for internal use!</i><br>
* <br>
* Save state.
*/
@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
if (isPersistent() || mText == null) {
return superState;
}
final SavedState myState = new SavedState(superState);
myState.text = getText();
return myState;
}
/**
* <i>Overridden for internal use!</i><br>
* <br>
* Restore state.
*/
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state == null || !state.getClass().equals(SavedState.class)) {
super.onRestoreInstanceState(state);
return;
}
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
setText(myState.text);
}
/**
* <i>Overridden for internal use!</i><br>
* <br>
* Persist value (if desired).
*/
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
String result = mEditText.getText().toString();
if (callChangeListener(result)) {
setText(result);
}
}
}
// PRIVATE ====================================================================================
private void initialize(AttributeSet attrs) {
// setup edit text
mEditText = new EditText(getContext(), attrs);
mEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
mEditText.setEnabled(true);
mEditText.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
// setup layout for edit text
int dip = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
getContext().getResources().getDisplayMetrics()) + 0.5f);
mEditTextlayout = new LinearLayout(getContext());
mEditTextlayout.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
mEditTextlayout.setPadding(dip, dip, dip, dip);
mEditTextlayout.addView(mEditText);
}
private static class SavedState extends BaseSavedState {
String text;
public SavedState(Parcel source) {
super(source);
text = source.readString();
}
public SavedState(Parcelable superState) {
super(superState);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeString(text);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() {
@Override
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
@Override
public SavedState [] newArray(int size) {
return new SavedState [size];
}
};
}
}