diff options
Diffstat (limited to 'src/com/android/settings/ApnEditor.java')
-rw-r--r-- | src/com/android/settings/ApnEditor.java | 136 |
1 files changed, 132 insertions, 4 deletions
diff --git a/src/com/android/settings/ApnEditor.java b/src/com/android/settings/ApnEditor.java index 8299c67..2ed7438 100644 --- a/src/com/android/settings/ApnEditor.java +++ b/src/com/android/settings/ApnEditor.java @@ -30,7 +30,6 @@ import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.MultiSelectListPreference; import android.preference.Preference; -import android.preference.PreferenceActivity; import android.preference.SwitchPreference; import android.provider.Telephony; import android.telephony.ServiceState; @@ -43,7 +42,10 @@ import android.view.Menu; import android.view.MenuItem; import com.android.internal.logging.MetricsLogger; +import java.util.ArrayList; import java.util.HashSet; +import java.util.Iterator; +import java.util.List; import java.util.Set; public class ApnEditor extends InstrumentedPreferenceActivity @@ -60,10 +62,13 @@ public class ApnEditor extends InstrumentedPreferenceActivity private final static String KEY_BEARER_MULTI = "bearer_multi"; private final static String KEY_MVNO_TYPE = "mvno_type"; + private final static String PROTOCOL_IPV4V6= "IPV4V6"; + private static final int MENU_DELETE = Menu.FIRST; private static final int MENU_SAVE = Menu.FIRST + 1; private static final int MENU_CANCEL = Menu.FIRST + 2; private static final int ERROR_DIALOG_ID = 0; + private static final int DUPLICATE_DIALOG_ID = 1; private static String sNotSet; private EditTextPreference mName; @@ -86,9 +91,11 @@ public class ApnEditor extends InstrumentedPreferenceActivity private MultiSelectListPreference mBearerMulti; private ListPreference mMvnoType; private EditTextPreference mMvnoMatchData; + private EditTextPreference mPppNumber; private String mCurMnc; private String mCurMcc; + private boolean mDisableEditor = false; private Uri mUri; private Cursor mCursor; @@ -127,7 +134,8 @@ public class ApnEditor extends InstrumentedPreferenceActivity Telephony.Carriers.BEARER_BITMASK, // 19 Telephony.Carriers.ROAMING_PROTOCOL, // 20 Telephony.Carriers.MVNO_TYPE, // 21 - Telephony.Carriers.MVNO_MATCH_DATA // 22 + Telephony.Carriers.MVNO_MATCH_DATA, // 22 + "ppp_number" // 23 }; private static final int ID_INDEX = 0; @@ -152,6 +160,7 @@ public class ApnEditor extends InstrumentedPreferenceActivity private static final int ROAMING_PROTOCOL_INDEX = 20; private static final int MVNO_TYPE_INDEX = 21; private static final int MVNO_MATCH_DATA_INDEX = 22; + private static final int PPP_NUMBER_INDEX = 23; @Override @@ -174,6 +183,7 @@ public class ApnEditor extends InstrumentedPreferenceActivity mMcc = (EditTextPreference) findPreference("apn_mcc"); mMnc = (EditTextPreference) findPreference("apn_mnc"); mApnType = (EditTextPreference) findPreference("apn_type"); + mPppNumber = (EditTextPreference) findPreference("apn_ppp_number"); mAuthType = (ListPreference) findPreference(KEY_AUTH_TYPE); mAuthType.setOnPreferenceChangeListener(this); @@ -199,6 +209,11 @@ public class ApnEditor extends InstrumentedPreferenceActivity final String action = intent.getAction(); mSubId = intent.getIntExtra(ApnSettings.SUB_ID, SubscriptionManager.INVALID_SUBSCRIPTION_ID); + mDisableEditor = intent.getBooleanExtra("DISABLE_EDITOR", false); + if (mDisableEditor) { + getPreferenceScreen().setEnabled(false); + Log.d(TAG, "ApnEditor form is disabled."); + } mFirstTime = icicle == null; @@ -206,7 +221,10 @@ public class ApnEditor extends InstrumentedPreferenceActivity mUri = intent.getData(); } else if (action.equals(Intent.ACTION_INSERT)) { if (mFirstTime || icicle.getInt(SAVED_POS) == 0) { - mUri = getContentResolver().insert(intent.getData(), new ContentValues()); + ContentValues values = new ContentValues(); + values.put(Telephony.Carriers.PROTOCOL, PROTOCOL_IPV4V6); + values.put(Telephony.Carriers.ROAMING_PROTOCOL, PROTOCOL_IPV4V6); + mUri = getContentResolver().insert(intent.getData(), values); } else { mUri = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI, icicle.getInt(SAVED_POS)); @@ -263,6 +281,7 @@ public class ApnEditor extends InstrumentedPreferenceActivity private void fillUi() { if (mFirstTime) { mFirstTime = false; + String numeric = mTelephonyManager.getSimOperator(mSubId); // Fill in all the values from the db in both text editor and summary mName.setText(mCursor.getString(NAME_INDEX)); mApn.setText(mCursor.getString(APN_INDEX)); @@ -278,7 +297,6 @@ public class ApnEditor extends InstrumentedPreferenceActivity mMnc.setText(mCursor.getString(MNC_INDEX)); mApnType.setText(mCursor.getString(TYPE_INDEX)); if (mNewApn) { - String numeric = mTelephonyManager.getSimOperator(mSubId); // MCC is first 3 chars and then in 2 - 3 chars of MNC if (numeric != null && numeric.length() > 4) { // Country code @@ -291,6 +309,7 @@ public class ApnEditor extends InstrumentedPreferenceActivity mCurMnc = mnc; mCurMcc = mcc; } + mApnType.setText(checkNull(getString(R.string.config_default_new_apn_type))); } int authVal = mCursor.getInt(AUTH_TYPE_INDEX); if (authVal != -1) { @@ -334,6 +353,16 @@ public class ApnEditor extends InstrumentedPreferenceActivity mMvnoType.setValue(mMvnoTypeStr); mMvnoMatchData.setText(mMvnoMatchDataStr); } + + String pppNumber = mCursor.getString(PPP_NUMBER_INDEX); + mPppNumber.setText(pppNumber); + if (pppNumber == null) { + if (!mNewApn) { + getPreferenceScreen().removePreference(mPppNumber); + } else if (getResources().getBoolean(R.bool.config_ppp_enabled)) { + getPreferenceScreen().removePreference(mPppNumber); + } + } } mName.setSummary(checkNull(mName.getText())); @@ -350,6 +379,13 @@ public class ApnEditor extends InstrumentedPreferenceActivity mMnc.setSummary(checkNull(mMnc.getText())); mApnType.setSummary(checkNull(mApnType.getText())); + String pppNumber = mPppNumber.getText(); + if (pppNumber != null) { + // Remove this preference if PPP number is not present + // in the APN settings + mPppNumber.setSummary(checkNull(pppNumber)); + } + String authVal = mAuthType.getValue(); if (authVal != null) { int authValIndex = Integer.parseInt(authVal); @@ -516,6 +552,10 @@ public class ApnEditor extends InstrumentedPreferenceActivity @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); + if (mDisableEditor) { + Log.d(TAG, "Form is disabled. Do not create the options menu."); + return true; + } // If it's a new APN, then cancel will delete the new entry in onPause if (!mNewApn) { menu.add(0, MENU_DELETE, 0, R.string.menu_delete) @@ -577,6 +617,13 @@ public class ApnEditor extends InstrumentedPreferenceActivity * @return true if the data was saved */ private boolean validateAndSave(boolean force) { + + // If the form is not editable, do nothing and return. + if (mDisableEditor){ + Log.d(TAG, "Form is disabled. Nothing to save."); + return true; + } + String name = checkNotSet(mName.getText()); String apn = checkNotSet(mApn.getText()); String mcc = checkNotSet(mMcc.getText()); @@ -630,6 +677,11 @@ public class ApnEditor extends InstrumentedPreferenceActivity values.put(Telephony.Carriers.NUMERIC, mcc + mnc); + String pppNumber = mPppNumber.getText(); + if (pppNumber != null) { + values.put(getResources().getString(R.string.ppp_number), pppNumber); + } + if (mCurMnc != null && mCurMcc != null) { if (mCurMnc.equals(mnc) && mCurMcc.equals(mcc)) { values.put(Telephony.Carriers.CURRENT, 1); @@ -665,11 +717,81 @@ public class ApnEditor extends InstrumentedPreferenceActivity values.put(Telephony.Carriers.MVNO_MATCH_DATA, checkNotSet(mMvnoMatchData.getText())); values.put(Telephony.Carriers.CARRIER_ENABLED, mCarrierEnabled.isChecked() ? 1 : 0); + + if (isDuplicate(values)) { + showDialog(DUPLICATE_DIALOG_ID); + return false; + } + getContentResolver().update(mUri, values, null, null); return true; } + private boolean isDuplicate(ContentValues row) { + if (!getResources().getBoolean(R.bool.config_enable_duplicate_apn_checking)) { + return false; + } + + final Set<String> keys = row.keySet(); + + StringBuilder queryBuilder = new StringBuilder(); + List<String> selectionArgsList = new ArrayList<>(); + + final Iterator<String> iterator = keys.iterator(); + while (iterator.hasNext()) { + final String key = iterator.next(); + + if (!keyForDuplicateCheck(key) || row.getAsString(key).isEmpty()) { + // Skip keys which don't interest us for the duplicate query. + // Or if the user hasn't yet filled a field in (empty value), skip it. + continue; + } + + queryBuilder.append(key); + queryBuilder.append("=?"); + queryBuilder.append(" AND "); + + selectionArgsList.add(row.getAsString(key)); + } + // remove extra AND at the end + queryBuilder.delete(queryBuilder.length() - " AND ".length(), queryBuilder.length()); + + String[] selectionArgs = new String[selectionArgsList.size()]; + selectionArgsList.toArray(selectionArgs); + + try (Cursor query = getContentResolver().query(Telephony.Carriers.CONTENT_URI, + sProjection, queryBuilder.toString(), selectionArgs, null)) { + return query.getCount() > (mNewApn ? 0 : 1); + } catch (Exception e) { + Log.e(TAG, "error querying for duplicates", e); + return false; + } + } + + /** + * Helper method to decide what columns should be considered valid when checking for + * potential duplicate APNs before allowing the user to add a new one. + * + * @param key the column of the row we want to check + * @return whether to include this key-value pair in the duplicate query + */ + private static boolean keyForDuplicateCheck(String key) { + switch (key) { + case Telephony.Carriers.APN: + case Telephony.Carriers.MMSPROXY: + case Telephony.Carriers.MMSPORT: + case Telephony.Carriers.MMSC: + case Telephony.Carriers.TYPE: + case Telephony.Carriers.MCC: + case Telephony.Carriers.MNC: + case Telephony.Carriers.NUMERIC: + return true; + default: + return false; + } + } + private String getErrorMsg() { String errorMsg = null; @@ -702,6 +824,12 @@ public class ApnEditor extends InstrumentedPreferenceActivity .setPositiveButton(android.R.string.ok, null) .setMessage(msg) .create(); + } else if (id == DUPLICATE_DIALOG_ID) { + return new AlertDialog.Builder(this) + .setTitle(R.string.duplicate_apn_error_title) + .setPositiveButton(android.R.string.ok, null) + .setMessage(getString(R.string.duplicate_apn_error_message)) + .create(); } return super.onCreateDialog(id); |