31
31
import android .text .TextUtils ;
32
32
import android .util .Log ;
33
33
import android .view .View ;
34
+ import android .widget .AdapterView ;
35
+ import android .widget .AdapterView .OnItemSelectedListener ;
36
+ import android .widget .ArrayAdapter ;
34
37
import android .widget .EditText ;
38
+ import android .widget .Spinner ;
35
39
import android .widget .Toast ;
36
40
37
41
import com .afwsamples .testdpc .DeviceAdminReceiver ;
@@ -110,6 +114,27 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
110
114
return false ;
111
115
}
112
116
117
+ void setUpSpinner (View dialogView , int viewId , int textArrayId ) {
118
+ final Spinner spinner = (Spinner ) dialogView .findViewById (viewId );
119
+ final ArrayAdapter <CharSequence > adapter = ArrayAdapter .createFromResource (
120
+ getActivity (), textArrayId , android .R .layout .simple_spinner_item );
121
+ adapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
122
+ spinner .setAdapter (adapter );
123
+ }
124
+
125
+ void setUpAllSpinners (View dialogView ) {
126
+ // Set up spinner for auth type.
127
+ setUpSpinner (dialogView , R .id .apn_auth_type , R .array .apn_auth_type_choices );
128
+ // Set up spinner for protocol.
129
+ setUpSpinner (dialogView , R .id .apn_protocol , R .array .apn_protocol_choices );
130
+ // Set up spinner for roaming protocol.
131
+ setUpSpinner (dialogView , R .id .apn_roaming_protocol , R .array .apn_protocol_choices );
132
+ // Set up spinner for mvno type.
133
+ setUpSpinner (dialogView , R .id .apn_mvno_type , R .array .apn_mvno_type_choices );
134
+ // Set up spinner for carrier enabled.
135
+ setUpSpinner (dialogView , R .id .apn_carrier_enabled , R .array .apn_carrier_enabled_choices );
136
+ }
137
+
113
138
void showInsertOverrideApnDialog () {
114
139
if (getActivity () == null || getActivity ().isFinishing ()) {
115
140
return ;
@@ -135,44 +160,39 @@ void showInsertOverrideApnDialog() {
135
160
R .id .apn_user );
136
161
final EditText passwordEditText = (EditText ) dialogView .findViewById (
137
162
R .id .apn_password );
138
- final EditText authTypeEditText = (EditText ) dialogView .findViewById (
139
- R .id .apn_auth_type );
140
163
final EditText typeEditText = (EditText ) dialogView .findViewById (
141
164
R .id .apn_type );
142
165
final EditText numericEditText = (EditText ) dialogView .findViewById (
143
166
R .id .apn_numeric );
144
- final EditText protocolEditText = (EditText ) dialogView .findViewById (
145
- R .id .apn_protocol );
146
- final EditText roamingProtocolEditText = (EditText ) dialogView .findViewById (
147
- R .id .apn_roaming_protocol );
148
- final EditText carrierEnabledEditText = (EditText ) dialogView .findViewById (
149
- R .id .apn_carrier_enabled );
150
167
final EditText networkBitmaskEditText = (EditText ) dialogView .findViewById (
151
168
R .id .apn_network_bitmask );
152
- final EditText mvnoTypeEditText = (EditText ) dialogView .findViewById (
153
- R .id .apn_mvno_type );
169
+ setUpAllSpinners (dialogView );
170
+
171
+ entryNameEditText .setHint (R .string .apn_entry_name_cannot_be_empty );
172
+ apnNameEditText .setHint (R .string .apn_name_cannot_be_empty );
173
+ typeEditText .setHint (R .string .apn_type_cannot_be_zero );
154
174
155
- authTypeEditText .setHint (R .string .apn_auth_type_hint );
156
175
numericEditText .setHint (R .string .apn_numeric_hint );
157
- carrierEnabledEditText .setHint (R .string .apn_carrier_enabled_hint );
158
176
159
177
new AlertDialog .Builder (getActivity ())
160
178
.setTitle (R .string .insert_override_apn )
161
179
.setView (dialogView )
162
180
.setPositiveButton (android .R .string .ok , (dialogInterface , i ) -> {
163
181
final String entryName = entryNameEditText .getText ().toString ();
164
182
if (entryName .isEmpty ()) {
165
- showToast (R .string .apn_no_entry_name );
183
+ showToast (R .string .apn_entry_name_cannot_be_empty );
166
184
return ;
167
185
}
168
186
final String apnName = apnNameEditText .getText ().toString ();
169
187
if (apnName .isEmpty ()) {
170
- showToast (R .string .apn_no_apn_name );
188
+ showToast (R .string .apn_name_cannot_be_empty );
189
+ return ;
190
+ }
191
+ final int apnTypeBitmask = parseInt (typeEditText .getText ().toString (), 0 );
192
+ if (apnTypeBitmask == 0 ) {
193
+ showToast (R .string .apn_type_cannot_be_zero );
171
194
return ;
172
195
}
173
- int authType = parseInt (authTypeEditText .getText ().toString (), 0 );
174
- int enabled = parseInt (carrierEnabledEditText .getText ().toString (), 0 );
175
- int networkbitmask = parseInt (networkBitmaskEditText .getText ().toString (), 0 );
176
196
177
197
ApnSetting apn = makeApnSetting (
178
198
numericEditText .getText ().toString (),
@@ -185,13 +205,23 @@ void showInsertOverrideApnDialog() {
185
205
parseInt (mmsPortEditText .getText ().toString (), -1 ),
186
206
userEditText .getText ().toString (),
187
207
passwordEditText .getText ().toString (),
188
- authType ,
189
- parseInt (typeEditText .getText ().toString (), 0 ),
190
- parseInt (protocolEditText .getText ().toString (), -1 ),
191
- parseInt (roamingProtocolEditText .getText ().toString (), -1 ),
192
- enabled == 1 ,
193
- networkbitmask ,
194
- parseInt (mvnoTypeEditText .getText ().toString (), -1 )
208
+ // -1 here as we have extra default choice "Not specified" in the
209
+ // spinner of auth type, protocol, roaming protocol and mvno type
210
+ // in case user doesn't want to specify these fields. And
211
+ // "Not Specified" should be transformed into "-1" in the builder
212
+ // of ApnSetting.
213
+ ((Spinner )dialogView .findViewById (R .id .apn_auth_type ))
214
+ .getSelectedItemPosition () - 1 ,
215
+ apnTypeBitmask ,
216
+ ((Spinner )dialogView .findViewById (R .id .apn_protocol ))
217
+ .getSelectedItemPosition () - 1 ,
218
+ ((Spinner )dialogView .findViewById (
219
+ R .id .apn_roaming_protocol )).getSelectedItemPosition () - 1 ,
220
+ ((Spinner )dialogView .findViewById (
221
+ R .id .apn_carrier_enabled )).getSelectedItemPosition () == 1 ,
222
+ parseInt (networkBitmaskEditText .getText ().toString (), 0 ),
223
+ ((Spinner )dialogView .findViewById (R .id .apn_mvno_type ))
224
+ .getSelectedItemPosition () - 1
195
225
);
196
226
int insertedId = mDevicePolicyManager .addOverrideApn (mAdminComponentName , apn );
197
227
if (insertedId == -1 ) {
0 commit comments