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
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.vpn;
import com.android.settings.R;
import android.content.Context;
import android.net.vpn.VpnProfile;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceGroup;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
/**
* The common class for editing {@link VpnProfile}.
*/
class VpnProfileEditor {
static final String SECRET_SET_INDICATOR =
new String(new byte[] {(byte) 1, (byte) 0});
private static final String KEY_VPN_NAME = "vpn_name";
private EditTextPreference mName;
private EditTextPreference mServerName;
private EditTextPreference mDomainSuffices;
private VpnProfile mProfile;
public VpnProfileEditor(VpnProfile p) {
mProfile = p;
}
//@Override
public VpnProfile getProfile() {
return mProfile;
}
/**
* Adds the preferences to the panel. Subclasses should override
* {@link #loadExtraPreferencesTo(PreferenceGroup)} instead of this method.
*/
public void loadPreferencesTo(PreferenceGroup subpanel) {
Context c = subpanel.getContext();
mName = (EditTextPreference) subpanel.findPreference(KEY_VPN_NAME);
mName.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(
Preference pref, Object newValue) {
setName((String) newValue);
return true;
}
});
setName(getProfile().getName());
subpanel.addPreference(createServerNamePreference(c));
loadExtraPreferencesTo(subpanel);
subpanel.addPreference(createDomainSufficesPreference(c));
}
/**
* Adds the extra preferences to the panel. Subclasses should add
* additional preferences in this method.
*/
protected void loadExtraPreferencesTo(PreferenceGroup subpanel) {
}
/**
* Validates the inputs in the preferences.
*
* @return an error message that is ready to be displayed in a dialog; or
* null if all the inputs are valid
*/
public String validate() {
String result = validate(mName, R.string.vpn_a_name);
return ((result != null)
? result
: validate(mServerName, R.string.vpn_a_vpn_server));
}
/**
* Creates a preference for users to input domain suffices.
*/
protected EditTextPreference createDomainSufficesPreference(Context c) {
EditTextPreference pref = mDomainSuffices = createEditTextPreference(c,
R.string.vpn_dns_search_list_title,
R.string.vpn_dns_search_list,
mProfile.getDomainSuffices(),
new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(
Preference pref, Object newValue) {
String v = ((String) newValue).trim();
mProfile.setDomainSuffices(v);
setSummary(pref, R.string.vpn_dns_search_list, v, false);
return true;
}
});
pref.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_URI);
return pref;
}
private Preference createServerNamePreference(Context c) {
EditTextPreference pref = mServerName = createEditTextPreference(c,
R.string.vpn_vpn_server_title,
R.string.vpn_vpn_server,
mProfile.getServerName(),
new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(
Preference pref, Object newValue) {
String v = ((String) newValue).trim();
mProfile.setServerName(v);
setSummary(pref, R.string.vpn_vpn_server, v);
return true;
}
});
pref.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_URI);
return pref;
}
protected EditTextPreference createEditTextPreference(Context c, int titleId,
int prefNameId, String value,
Preference.OnPreferenceChangeListener listener) {
EditTextPreference pref = new EditTextPreference(c);
pref.setTitle(titleId);
pref.setDialogTitle(titleId);
setSummary(pref, prefNameId, value);
pref.setText(value);
pref.setPersistent(true);
pref.setOnPreferenceChangeListener(listener);
return pref;
}
protected EditTextPreference createSecretPreference(Context c, int titleId,
int fieldNameId, String value,
Preference.OnPreferenceChangeListener listener) {
EditTextPreference pref = new EditTextPreference(c);
pref.setTitle(titleId);
pref.setDialogTitle(titleId);
pref.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
pref.getEditText().setTransformationMethod(
new PasswordTransformationMethod());
pref.setText(TextUtils.isEmpty(value) ? "" : SECRET_SET_INDICATOR);
setSecretSummary(pref, fieldNameId, value);
pref.setPersistent(true);
pref.setOnPreferenceChangeListener(listener);
return pref;
}
protected String validate(Preference pref, int fieldNameId) {
Context c = pref.getContext();
String value = (pref instanceof EditTextPreference)
? ((EditTextPreference) pref).getText()
: ((ListPreference) pref).getValue();
String formatString = (pref instanceof EditTextPreference)
? c.getString(R.string.vpn_error_miss_entering)
: c.getString(R.string.vpn_error_miss_selecting);
return (TextUtils.isEmpty(value)
? String.format(formatString, c.getString(fieldNameId))
: null);
}
protected void setSummary(Preference pref, int fieldNameId, String v) {
setSummary(pref, fieldNameId, v, true);
}
protected void setSummary(Preference pref, int fieldNameId, String v,
boolean required) {
Context c = pref.getContext();
String formatString = required
? c.getString(R.string.vpn_field_not_set)
: c.getString(R.string.vpn_field_not_set_optional);
pref.setSummary(TextUtils.isEmpty(v)
? String.format(formatString, c.getString(fieldNameId))
: v);
}
protected void setSecretSummary(Preference pref, int fieldNameId,
String value) {
Context c = pref.getContext();
String formatString = TextUtils.isEmpty(value)
? c.getString(R.string.vpn_field_not_set)
: c.getString(R.string.vpn_field_is_set);
pref.setSummary(String.format(formatString, c.getString(fieldNameId)));
}
protected void setSecretTitle(
CheckBoxPreference pref, int fieldNameId, boolean enabled) {
Context c = pref.getContext();
String formatString = enabled
? c.getString(R.string.vpn_disable_field)
: c.getString(R.string.vpn_enable_field);
pref.setTitle(String.format(formatString, c.getString(fieldNameId)));
}
private void setName(String newName) {
newName = (newName == null) ? "" : newName.trim();
mName.setText(newName);
getProfile().setName(newName);
setSummary(mName, R.string.vpn_name, newName);
}
}
|