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