OSDN Git Service

Merge "Fix crash in ConfirmDeviceCredentials" into nyc-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / ApnEditor.java
1 /*
2  * Copyright (C) 2006 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 com.android.settings;
18
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.content.ContentUris;
22 import android.content.ContentValues;
23 import android.content.Intent;
24 import android.content.res.Resources;
25 import android.database.Cursor;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.provider.Telephony;
29 import android.support.v14.preference.MultiSelectListPreference;
30 import android.support.v14.preference.SwitchPreference;
31 import android.support.v7.preference.EditTextPreference;
32 import android.support.v7.preference.ListPreference;
33 import android.support.v7.preference.Preference;
34 import android.telephony.ServiceState;
35 import android.telephony.SubscriptionManager;
36 import android.telephony.TelephonyManager;
37 import android.text.TextUtils;
38 import android.util.Log;
39 import android.view.KeyEvent;
40 import android.view.Menu;
41 import android.view.MenuItem;
42 import com.android.internal.logging.MetricsProto.MetricsEvent;
43
44 import java.util.HashSet;
45 import java.util.Set;
46
47 public class ApnEditor extends InstrumentedPreferenceActivity
48         implements Preference.OnPreferenceChangeListener {
49
50     private final static String TAG = ApnEditor.class.getSimpleName();
51
52     private final static String SAVED_POS = "pos";
53     private final static String KEY_AUTH_TYPE = "auth_type";
54     private final static String KEY_PROTOCOL = "apn_protocol";
55     private final static String KEY_ROAMING_PROTOCOL = "apn_roaming_protocol";
56     private final static String KEY_CARRIER_ENABLED = "carrier_enabled";
57     private final static String KEY_BEARER_MULTI = "bearer_multi";
58     private final static String KEY_MVNO_TYPE = "mvno_type";
59
60     private static final int MENU_DELETE = Menu.FIRST;
61     private static final int MENU_SAVE = Menu.FIRST + 1;
62     private static final int MENU_CANCEL = Menu.FIRST + 2;
63     private static final int ERROR_DIALOG_ID = 0;
64
65     private static String sNotSet;
66     private EditTextPreference mName;
67     private EditTextPreference mApn;
68     private EditTextPreference mProxy;
69     private EditTextPreference mPort;
70     private EditTextPreference mUser;
71     private EditTextPreference mServer;
72     private EditTextPreference mPassword;
73     private EditTextPreference mMmsc;
74     private EditTextPreference mMcc;
75     private EditTextPreference mMnc;
76     private EditTextPreference mMmsProxy;
77     private EditTextPreference mMmsPort;
78     private ListPreference mAuthType;
79     private EditTextPreference mApnType;
80     private ListPreference mProtocol;
81     private ListPreference mRoamingProtocol;
82     private SwitchPreference mCarrierEnabled;
83     private MultiSelectListPreference mBearerMulti;
84     private ListPreference mMvnoType;
85     private EditTextPreference mMvnoMatchData;
86
87     private String mCurMnc;
88     private String mCurMcc;
89
90     private Uri mUri;
91     private Cursor mCursor;
92     private boolean mNewApn;
93     private boolean mFirstTime;
94     private int mSubId;
95     private Resources mRes;
96     private TelephonyManager mTelephonyManager;
97     private int mBearerInitialVal = 0;
98     private String mMvnoTypeStr;
99     private String mMvnoMatchDataStr;
100
101     /**
102      * Standard projection for the interesting columns of a normal note.
103      */
104     private static final String[] sProjection = new String[] {
105             Telephony.Carriers._ID,     // 0
106             Telephony.Carriers.NAME,    // 1
107             Telephony.Carriers.APN,     // 2
108             Telephony.Carriers.PROXY,   // 3
109             Telephony.Carriers.PORT,    // 4
110             Telephony.Carriers.USER,    // 5
111             Telephony.Carriers.SERVER,  // 6
112             Telephony.Carriers.PASSWORD, // 7
113             Telephony.Carriers.MMSC, // 8
114             Telephony.Carriers.MCC, // 9
115             Telephony.Carriers.MNC, // 10
116             Telephony.Carriers.NUMERIC, // 11
117             Telephony.Carriers.MMSPROXY,// 12
118             Telephony.Carriers.MMSPORT, // 13
119             Telephony.Carriers.AUTH_TYPE, // 14
120             Telephony.Carriers.TYPE, // 15
121             Telephony.Carriers.PROTOCOL, // 16
122             Telephony.Carriers.CARRIER_ENABLED, // 17
123             Telephony.Carriers.BEARER, // 18
124             Telephony.Carriers.BEARER_BITMASK, // 19
125             Telephony.Carriers.ROAMING_PROTOCOL, // 20
126             Telephony.Carriers.MVNO_TYPE,   // 21
127             Telephony.Carriers.MVNO_MATCH_DATA  // 22
128     };
129
130     private static final int ID_INDEX = 0;
131     private static final int NAME_INDEX = 1;
132     private static final int APN_INDEX = 2;
133     private static final int PROXY_INDEX = 3;
134     private static final int PORT_INDEX = 4;
135     private static final int USER_INDEX = 5;
136     private static final int SERVER_INDEX = 6;
137     private static final int PASSWORD_INDEX = 7;
138     private static final int MMSC_INDEX = 8;
139     private static final int MCC_INDEX = 9;
140     private static final int MNC_INDEX = 10;
141     private static final int MMSPROXY_INDEX = 12;
142     private static final int MMSPORT_INDEX = 13;
143     private static final int AUTH_TYPE_INDEX = 14;
144     private static final int TYPE_INDEX = 15;
145     private static final int PROTOCOL_INDEX = 16;
146     private static final int CARRIER_ENABLED_INDEX = 17;
147     private static final int BEARER_INDEX = 18;
148     private static final int BEARER_BITMASK_INDEX = 19;
149     private static final int ROAMING_PROTOCOL_INDEX = 20;
150     private static final int MVNO_TYPE_INDEX = 21;
151     private static final int MVNO_MATCH_DATA_INDEX = 22;
152
153
154     @Override
155     protected void onCreate(Bundle icicle) {
156         super.onCreate(icicle);
157
158         addPreferencesFromResource(R.xml.apn_editor);
159
160         sNotSet = getResources().getString(R.string.apn_not_set);
161         mName = (EditTextPreference) findPreference("apn_name");
162         mApn = (EditTextPreference) findPreference("apn_apn");
163         mProxy = (EditTextPreference) findPreference("apn_http_proxy");
164         mPort = (EditTextPreference) findPreference("apn_http_port");
165         mUser = (EditTextPreference) findPreference("apn_user");
166         mServer = (EditTextPreference) findPreference("apn_server");
167         mPassword = (EditTextPreference) findPreference("apn_password");
168         mMmsProxy = (EditTextPreference) findPreference("apn_mms_proxy");
169         mMmsPort = (EditTextPreference) findPreference("apn_mms_port");
170         mMmsc = (EditTextPreference) findPreference("apn_mmsc");
171         mMcc = (EditTextPreference) findPreference("apn_mcc");
172         mMnc = (EditTextPreference) findPreference("apn_mnc");
173         mApnType = (EditTextPreference) findPreference("apn_type");
174
175         mAuthType = (ListPreference) findPreference(KEY_AUTH_TYPE);
176         mAuthType.setOnPreferenceChangeListener(this);
177
178         mProtocol = (ListPreference) findPreference(KEY_PROTOCOL);
179         mProtocol.setOnPreferenceChangeListener(this);
180
181         mRoamingProtocol = (ListPreference) findPreference(KEY_ROAMING_PROTOCOL);
182         mRoamingProtocol.setOnPreferenceChangeListener(this);
183
184         mCarrierEnabled = (SwitchPreference) findPreference(KEY_CARRIER_ENABLED);
185
186         mBearerMulti = (MultiSelectListPreference) findPreference(KEY_BEARER_MULTI);
187         mBearerMulti.setOnPreferenceChangeListener(this);
188
189         mMvnoType = (ListPreference) findPreference(KEY_MVNO_TYPE);
190         mMvnoType.setOnPreferenceChangeListener(this);
191         mMvnoMatchData = (EditTextPreference) findPreference("mvno_match_data");
192
193         mRes = getResources();
194
195         final Intent intent = getIntent();
196         final String action = intent.getAction();
197         mSubId = intent.getIntExtra(ApnSettings.SUB_ID,
198                 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
199
200         mFirstTime = icicle == null;
201
202         if (action.equals(Intent.ACTION_EDIT)) {
203             Uri uri = intent.getData();
204             if (!uri.isPathPrefixMatch(Telephony.Carriers.CONTENT_URI)) {
205                 Log.e(TAG, "Edit request not for carrier table. Uri: " + uri);
206                 finish();
207                 return;
208             }
209             mUri = uri;
210         } else if (action.equals(Intent.ACTION_INSERT)) {
211             if (mFirstTime || icicle.getInt(SAVED_POS) == 0) {
212                 Uri uri = intent.getData();
213                 if (!uri.isPathPrefixMatch(Telephony.Carriers.CONTENT_URI)) {
214                     Log.e(TAG, "Insert request not for carrier table. Uri: " + uri);
215                     finish();
216                     return;
217                 }
218                 mUri = getContentResolver().insert(uri, new ContentValues());
219             } else {
220                 mUri = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI,
221                         icicle.getInt(SAVED_POS));
222             }
223             mNewApn = true;
224             mMvnoTypeStr = intent.getStringExtra(ApnSettings.MVNO_TYPE);
225             mMvnoMatchDataStr = intent.getStringExtra(ApnSettings.MVNO_MATCH_DATA);
226             // If we were unable to create a new note, then just finish
227             // this activity.  A RESULT_CANCELED will be sent back to the
228             // original activity if they requested a result.
229             if (mUri == null) {
230                 Log.w(TAG, "Failed to insert new telephony provider into "
231                         + getIntent().getData());
232                 finish();
233                 return;
234             }
235
236             // The new entry was created, so assume all will end well and
237             // set the result to be returned.
238             setResult(RESULT_OK, (new Intent()).setAction(mUri.toString()));
239
240         } else {
241             finish();
242             return;
243         }
244
245         mCursor = managedQuery(mUri, sProjection, null, null);
246         mCursor.moveToFirst();
247
248         mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
249
250         for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
251             getPreferenceScreen().getPreference(i).setOnPreferenceChangeListener(this);
252         }
253
254         fillUi();
255     }
256
257     @Override
258     protected int getMetricsCategory() {
259         return MetricsEvent.APN_EDITOR;
260     }
261
262     @Override
263     public void onResume() {
264         super.onResume();
265     }
266
267     @Override
268     public void onPause() {
269         super.onPause();
270     }
271
272     private void fillUi() {
273         if (mFirstTime) {
274             mFirstTime = false;
275             // Fill in all the values from the db in both text editor and summary
276             mName.setText(mCursor.getString(NAME_INDEX));
277             mApn.setText(mCursor.getString(APN_INDEX));
278             mProxy.setText(mCursor.getString(PROXY_INDEX));
279             mPort.setText(mCursor.getString(PORT_INDEX));
280             mUser.setText(mCursor.getString(USER_INDEX));
281             mServer.setText(mCursor.getString(SERVER_INDEX));
282             mPassword.setText(mCursor.getString(PASSWORD_INDEX));
283             mMmsProxy.setText(mCursor.getString(MMSPROXY_INDEX));
284             mMmsPort.setText(mCursor.getString(MMSPORT_INDEX));
285             mMmsc.setText(mCursor.getString(MMSC_INDEX));
286             mMcc.setText(mCursor.getString(MCC_INDEX));
287             mMnc.setText(mCursor.getString(MNC_INDEX));
288             mApnType.setText(mCursor.getString(TYPE_INDEX));
289             if (mNewApn) {
290                 String numeric = mTelephonyManager.getSimOperator(mSubId);
291                 // MCC is first 3 chars and then in 2 - 3 chars of MNC
292                 if (numeric != null && numeric.length() > 4) {
293                     // Country code
294                     String mcc = numeric.substring(0, 3);
295                     // Network code
296                     String mnc = numeric.substring(3);
297                     // Auto populate MNC and MCC for new entries, based on what SIM reports
298                     mMcc.setText(mcc);
299                     mMnc.setText(mnc);
300                     mCurMnc = mnc;
301                     mCurMcc = mcc;
302                 }
303             }
304             int authVal = mCursor.getInt(AUTH_TYPE_INDEX);
305             if (authVal != -1) {
306                 mAuthType.setValueIndex(authVal);
307             } else {
308                 mAuthType.setValue(null);
309             }
310
311             mProtocol.setValue(mCursor.getString(PROTOCOL_INDEX));
312             mRoamingProtocol.setValue(mCursor.getString(ROAMING_PROTOCOL_INDEX));
313             mCarrierEnabled.setChecked(mCursor.getInt(CARRIER_ENABLED_INDEX)==1);
314             mBearerInitialVal = mCursor.getInt(BEARER_INDEX);
315
316             HashSet<String> bearers = new HashSet<String>();
317             int bearerBitmask = mCursor.getInt(BEARER_BITMASK_INDEX);
318             if (bearerBitmask == 0) {
319                 if (mBearerInitialVal == 0) {
320                     bearers.add("" + 0);
321                 }
322             } else {
323                 int i = 1;
324                 while (bearerBitmask != 0) {
325                     if ((bearerBitmask & 1) == 1) {
326                         bearers.add("" + i);
327                     }
328                     bearerBitmask >>= 1;
329                     i++;
330                 }
331             }
332
333             if (mBearerInitialVal != 0 && bearers.contains("" + mBearerInitialVal) == false) {
334                 // add mBearerInitialVal to bearers
335                 bearers.add("" + mBearerInitialVal);
336             }
337             mBearerMulti.setValues(bearers);
338
339             mMvnoType.setValue(mCursor.getString(MVNO_TYPE_INDEX));
340             mMvnoMatchData.setEnabled(false);
341             mMvnoMatchData.setText(mCursor.getString(MVNO_MATCH_DATA_INDEX));
342             if (mNewApn && mMvnoTypeStr != null && mMvnoMatchDataStr != null) {
343                 mMvnoType.setValue(mMvnoTypeStr);
344                 mMvnoMatchData.setText(mMvnoMatchDataStr);
345             }
346         }
347
348         mName.setSummary(checkNull(mName.getText()));
349         mApn.setSummary(checkNull(mApn.getText()));
350         mProxy.setSummary(checkNull(mProxy.getText()));
351         mPort.setSummary(checkNull(mPort.getText()));
352         mUser.setSummary(checkNull(mUser.getText()));
353         mServer.setSummary(checkNull(mServer.getText()));
354         mPassword.setSummary(starify(mPassword.getText()));
355         mMmsProxy.setSummary(checkNull(mMmsProxy.getText()));
356         mMmsPort.setSummary(checkNull(mMmsPort.getText()));
357         mMmsc.setSummary(checkNull(mMmsc.getText()));
358         mMcc.setSummary(checkNull(mMcc.getText()));
359         mMnc.setSummary(checkNull(mMnc.getText()));
360         mApnType.setSummary(checkNull(mApnType.getText()));
361
362         String authVal = mAuthType.getValue();
363         if (authVal != null) {
364             int authValIndex = Integer.parseInt(authVal);
365             mAuthType.setValueIndex(authValIndex);
366
367             String []values = mRes.getStringArray(R.array.apn_auth_entries);
368             mAuthType.setSummary(values[authValIndex]);
369         } else {
370             mAuthType.setSummary(sNotSet);
371         }
372
373         mProtocol.setSummary(
374                 checkNull(protocolDescription(mProtocol.getValue(), mProtocol)));
375         mRoamingProtocol.setSummary(
376                 checkNull(protocolDescription(mRoamingProtocol.getValue(), mRoamingProtocol)));
377         mBearerMulti.setSummary(
378                 checkNull(bearerMultiDescription(mBearerMulti.getValues())));
379         mMvnoType.setSummary(
380                 checkNull(mvnoDescription(mMvnoType.getValue())));
381         mMvnoMatchData.setSummary(checkNull(mMvnoMatchData.getText()));
382         // allow user to edit carrier_enabled for some APN
383         boolean ceEditable = getResources().getBoolean(R.bool.config_allow_edit_carrier_enabled);
384         if (ceEditable) {
385             mCarrierEnabled.setEnabled(true);
386         } else {
387             mCarrierEnabled.setEnabled(false);
388         }
389     }
390
391     /**
392      * Returns the UI choice (e.g., "IPv4/IPv6") corresponding to the given
393      * raw value of the protocol preference (e.g., "IPV4V6"). If unknown,
394      * return null.
395      */
396     private String protocolDescription(String raw, ListPreference protocol) {
397         int protocolIndex = protocol.findIndexOfValue(raw);
398         if (protocolIndex == -1) {
399             return null;
400         } else {
401             String[] values = mRes.getStringArray(R.array.apn_protocol_entries);
402             try {
403                 return values[protocolIndex];
404             } catch (ArrayIndexOutOfBoundsException e) {
405                 return null;
406             }
407         }
408     }
409
410     private String bearerDescription(String raw) {
411         int mBearerIndex = mBearerMulti.findIndexOfValue(raw);
412         if (mBearerIndex == -1) {
413             return null;
414         } else {
415             String[] values = mRes.getStringArray(R.array.bearer_entries);
416             try {
417                 return values[mBearerIndex];
418             } catch (ArrayIndexOutOfBoundsException e) {
419                 return null;
420             }
421         }
422     }
423
424     private String bearerMultiDescription(Set<String> raw) {
425         String[] values = mRes.getStringArray(R.array.bearer_entries);
426         StringBuilder retVal = new StringBuilder();
427         boolean first = true;
428         for (String bearer : raw) {
429             int bearerIndex = mBearerMulti.findIndexOfValue(bearer);
430             try {
431                 if (first) {
432                     retVal.append(values[bearerIndex]);
433                     first = false;
434                 } else {
435                     retVal.append(", " + values[bearerIndex]);
436                 }
437             } catch (ArrayIndexOutOfBoundsException e) {
438                 // ignore
439             }
440         }
441         String val = retVal.toString();
442         if (!TextUtils.isEmpty(val)) {
443             return val;
444         }
445         return null;
446     }
447
448     private String mvnoDescription(String newValue) {
449         int mvnoIndex = mMvnoType.findIndexOfValue(newValue);
450         String oldValue = mMvnoType.getValue();
451
452         if (mvnoIndex == -1) {
453             return null;
454         } else {
455             String[] values = mRes.getStringArray(R.array.mvno_type_entries);
456             mMvnoMatchData.setEnabled(mvnoIndex != 0);
457             if (newValue != null && newValue.equals(oldValue) == false) {
458                 if (values[mvnoIndex].equals("SPN")) {
459                     mMvnoMatchData.setText(mTelephonyManager.getSimOperatorName());
460                 } else if (values[mvnoIndex].equals("IMSI")) {
461                     String numeric = mTelephonyManager.getSimOperator(mSubId);
462                     mMvnoMatchData.setText(numeric + "x");
463                 } else if (values[mvnoIndex].equals("GID")) {
464                     mMvnoMatchData.setText(mTelephonyManager.getGroupIdLevel1());
465                 }
466             }
467
468             try {
469                 return values[mvnoIndex];
470             } catch (ArrayIndexOutOfBoundsException e) {
471                 return null;
472             }
473         }
474     }
475
476     public boolean onPreferenceChange(Preference preference, Object newValue) {
477         String key = preference.getKey();
478         if (KEY_AUTH_TYPE.equals(key)) {
479             try {
480                 int index = Integer.parseInt((String) newValue);
481                 mAuthType.setValueIndex(index);
482
483                 String[] values = mRes.getStringArray(R.array.apn_auth_entries);
484                 mAuthType.setSummary(values[index]);
485             } catch (NumberFormatException e) {
486                 return false;
487             }
488         } else if (KEY_PROTOCOL.equals(key)) {
489             String protocol = protocolDescription((String) newValue, mProtocol);
490             if (protocol == null) {
491                 return false;
492             }
493             mProtocol.setSummary(protocol);
494             mProtocol.setValue((String) newValue);
495         } else if (KEY_ROAMING_PROTOCOL.equals(key)) {
496             String protocol = protocolDescription((String) newValue, mRoamingProtocol);
497             if (protocol == null) {
498                 return false;
499             }
500             mRoamingProtocol.setSummary(protocol);
501             mRoamingProtocol.setValue((String) newValue);
502         } else if (KEY_BEARER_MULTI.equals(key)) {
503             String bearer = bearerMultiDescription((Set<String>) newValue);
504             if (bearer == null) {
505                 return false;
506             }
507             mBearerMulti.setValues((Set<String>) newValue);
508             mBearerMulti.setSummary(bearer);
509         } else if (KEY_MVNO_TYPE.equals(key)) {
510             String mvno = mvnoDescription((String) newValue);
511             if (mvno == null) {
512                 return false;
513             }
514             mMvnoType.setValue((String) newValue);
515             mMvnoType.setSummary(mvno);
516         }
517         if (preference.equals(mPassword)) {
518             preference.setSummary(starify(newValue != null ? String.valueOf(newValue) : ""));
519         } else if (preference.equals(mCarrierEnabled) || preference.equals(mBearerMulti)) {
520             // do nothing
521         } else {
522             preference.setSummary(checkNull(newValue != null ? String.valueOf(newValue) : null));
523         }
524
525         return true;
526     }
527
528     @Override
529     public boolean onCreateOptionsMenu(Menu menu) {
530         super.onCreateOptionsMenu(menu);
531         // If it's a new APN, then cancel will delete the new entry in onPause
532         if (!mNewApn) {
533             menu.add(0, MENU_DELETE, 0, R.string.menu_delete)
534                 .setIcon(R.drawable.ic_menu_delete);
535         }
536         menu.add(0, MENU_SAVE, 0, R.string.menu_save)
537             .setIcon(android.R.drawable.ic_menu_save);
538         menu.add(0, MENU_CANCEL, 0, R.string.menu_cancel)
539             .setIcon(android.R.drawable.ic_menu_close_clear_cancel);
540         return true;
541     }
542
543     @Override
544     public boolean onOptionsItemSelected(MenuItem item) {
545         switch (item.getItemId()) {
546         case MENU_DELETE:
547             deleteApn();
548             return true;
549         case MENU_SAVE:
550             if (validateAndSave(false)) {
551                 finish();
552             }
553             return true;
554         case MENU_CANCEL:
555             if (mNewApn) {
556                 getContentResolver().delete(mUri, null, null);
557             }
558             finish();
559             return true;
560         }
561         return super.onOptionsItemSelected(item);
562     }
563
564     @Override
565     public boolean onKeyDown(int keyCode, KeyEvent event) {
566         switch (keyCode) {
567             case KeyEvent.KEYCODE_BACK: {
568                 if (validateAndSave(false)) {
569                     finish();
570                 }
571                 return true;
572             }
573         }
574         return super.onKeyDown(keyCode, event);
575     }
576
577     @Override
578     protected void onSaveInstanceState(Bundle icicle) {
579         super.onSaveInstanceState(icicle);
580         if (validateAndSave(true)) {
581             icicle.putInt(SAVED_POS, mCursor.getInt(ID_INDEX));
582         }
583     }
584
585     /**
586      * Check the key fields' validity and save if valid.
587      * @param force save even if the fields are not valid, if the app is
588      *        being suspended
589      * @return true if the data was saved
590      */
591     private boolean validateAndSave(boolean force) {
592         String name = checkNotSet(mName.getText());
593         String apn = checkNotSet(mApn.getText());
594         String mcc = checkNotSet(mMcc.getText());
595         String mnc = checkNotSet(mMnc.getText());
596
597         if (getErrorMsg() != null && !force) {
598             showDialog(ERROR_DIALOG_ID);
599             return false;
600         }
601
602         if (!mCursor.moveToFirst()) {
603             Log.w(TAG,
604                     "Could not go to the first row in the Cursor when saving data.");
605             return false;
606         }
607
608         // If it's a new APN and a name or apn haven't been entered, then erase the entry
609         if (force && mNewApn && name.length() < 1 && apn.length() < 1) {
610             getContentResolver().delete(mUri, null, null);
611             return false;
612         }
613
614         ContentValues values = new ContentValues();
615
616         // Add a dummy name "Untitled", if the user exits the screen without adding a name but
617         // entered other information worth keeping.
618         values.put(Telephony.Carriers.NAME,
619                 name.length() < 1 ? getResources().getString(R.string.untitled_apn) : name);
620         values.put(Telephony.Carriers.APN, apn);
621         values.put(Telephony.Carriers.PROXY, checkNotSet(mProxy.getText()));
622         values.put(Telephony.Carriers.PORT, checkNotSet(mPort.getText()));
623         values.put(Telephony.Carriers.MMSPROXY, checkNotSet(mMmsProxy.getText()));
624         values.put(Telephony.Carriers.MMSPORT, checkNotSet(mMmsPort.getText()));
625         values.put(Telephony.Carriers.USER, checkNotSet(mUser.getText()));
626         values.put(Telephony.Carriers.SERVER, checkNotSet(mServer.getText()));
627         values.put(Telephony.Carriers.PASSWORD, checkNotSet(mPassword.getText()));
628         values.put(Telephony.Carriers.MMSC, checkNotSet(mMmsc.getText()));
629
630         String authVal = mAuthType.getValue();
631         if (authVal != null) {
632             values.put(Telephony.Carriers.AUTH_TYPE, Integer.parseInt(authVal));
633         }
634
635         values.put(Telephony.Carriers.PROTOCOL, checkNotSet(mProtocol.getValue()));
636         values.put(Telephony.Carriers.ROAMING_PROTOCOL, checkNotSet(mRoamingProtocol.getValue()));
637
638         values.put(Telephony.Carriers.TYPE, checkNotSet(mApnType.getText()));
639
640         values.put(Telephony.Carriers.MCC, mcc);
641         values.put(Telephony.Carriers.MNC, mnc);
642
643         values.put(Telephony.Carriers.NUMERIC, mcc + mnc);
644
645         if (mCurMnc != null && mCurMcc != null) {
646             if (mCurMnc.equals(mnc) && mCurMcc.equals(mcc)) {
647                 values.put(Telephony.Carriers.CURRENT, 1);
648             }
649         }
650
651         Set<String> bearerSet = mBearerMulti.getValues();
652         int bearerBitmask = 0;
653         for (String bearer : bearerSet) {
654             if (Integer.parseInt(bearer) == 0) {
655                 bearerBitmask = 0;
656                 break;
657             } else {
658                 bearerBitmask |= ServiceState.getBitmaskForTech(Integer.parseInt(bearer));
659             }
660         }
661         values.put(Telephony.Carriers.BEARER_BITMASK, bearerBitmask);
662
663         int bearerVal;
664         if (bearerBitmask == 0 || mBearerInitialVal == 0) {
665             bearerVal = 0;
666         } else if (ServiceState.bitmaskHasTech(bearerBitmask, mBearerInitialVal)) {
667             bearerVal = mBearerInitialVal;
668         } else {
669             // bearer field was being used but bitmask has changed now and does not include the
670             // initial bearer value -- setting bearer to 0 but maybe better behavior is to choose a
671             // random tech from the new bitmask??
672             bearerVal = 0;
673         }
674         values.put(Telephony.Carriers.BEARER, bearerVal);
675
676         values.put(Telephony.Carriers.MVNO_TYPE, checkNotSet(mMvnoType.getValue()));
677         values.put(Telephony.Carriers.MVNO_MATCH_DATA, checkNotSet(mMvnoMatchData.getText()));
678
679         values.put(Telephony.Carriers.CARRIER_ENABLED, mCarrierEnabled.isChecked() ? 1 : 0);
680         getContentResolver().update(mUri, values, null, null);
681
682         return true;
683     }
684
685     private String getErrorMsg() {
686         String errorMsg = null;
687
688         String name = checkNotSet(mName.getText());
689         String apn = checkNotSet(mApn.getText());
690         String mcc = checkNotSet(mMcc.getText());
691         String mnc = checkNotSet(mMnc.getText());
692
693         if (name.length() < 1) {
694             errorMsg = mRes.getString(R.string.error_name_empty);
695         } else if (apn.length() < 1) {
696             errorMsg = mRes.getString(R.string.error_apn_empty);
697         } else if (mcc.length() != 3) {
698             errorMsg = mRes.getString(R.string.error_mcc_not3);
699         } else if ((mnc.length() & 0xFFFE) != 2) {
700             errorMsg = mRes.getString(R.string.error_mnc_not23);
701         }
702
703         return errorMsg;
704     }
705
706     @Override
707     protected Dialog onCreateDialog(int id) {
708
709         if (id == ERROR_DIALOG_ID) {
710             String msg = getErrorMsg();
711
712             return new AlertDialog.Builder(this)
713                     .setTitle(R.string.error_title)
714                     .setPositiveButton(android.R.string.ok, null)
715                     .setMessage(msg)
716                     .create();
717         }
718
719         return super.onCreateDialog(id);
720     }
721
722     @Override
723     protected void onPrepareDialog(int id, Dialog dialog) {
724         super.onPrepareDialog(id, dialog);
725
726         if (id == ERROR_DIALOG_ID) {
727             String msg = getErrorMsg();
728
729             if (msg != null) {
730                 ((AlertDialog)dialog).setMessage(msg);
731             }
732         }
733     }
734
735     private void deleteApn() {
736         getContentResolver().delete(mUri, null, null);
737         finish();
738     }
739
740     private String starify(String value) {
741         if (value == null || value.length() == 0) {
742             return sNotSet;
743         } else {
744             char[] password = new char[value.length()];
745             for (int i = 0; i < password.length; i++) {
746                 password[i] = '*';
747             }
748             return new String(password);
749         }
750     }
751
752     private String checkNull(String value) {
753         if (value == null || value.length() == 0) {
754             return sNotSet;
755         } else {
756             return value;
757         }
758     }
759
760     private String checkNotSet(String value) {
761         if (value == null || value.equals(sNotSet)) {
762             return "";
763         } else {
764             return value;
765         }
766     }
767
768 }