ApnEditorpublic class ApnEditor extends android.preference.PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener
Fields Summary |
---|
private static final String | TAG | private static final String | SAVED_POS | private static final int | MENU_DELETE | private static final int | MENU_SAVE | private static final int | MENU_CANCEL | private static String | sNotSet | private android.preference.EditTextPreference | mName | private android.preference.EditTextPreference | mApn | private android.preference.EditTextPreference | mProxy | private android.preference.EditTextPreference | mPort | private android.preference.EditTextPreference | mUser | private android.preference.EditTextPreference | mServer | private android.preference.EditTextPreference | mPassword | private android.preference.EditTextPreference | mMmsc | private android.preference.EditTextPreference | mMcc | private android.preference.EditTextPreference | mMnc | private android.preference.EditTextPreference | mMmsProxy | private android.preference.EditTextPreference | mMmsPort | private android.preference.EditTextPreference | mApnType | private String | mCurMnc | private String | mCurMcc | private android.net.Uri | mUri | private android.database.Cursor | mCursor | private boolean | mNewApn | private boolean | mFirstTime | private android.content.res.Resources | mRes | private static final String[] | sProjectionStandard projection for the interesting columns of a normal note. | private static final int | ID_INDEX | private static final int | NAME_INDEX | private static final int | APN_INDEX | private static final int | PROXY_INDEX | private static final int | PORT_INDEX | private static final int | USER_INDEX | private static final int | SERVER_INDEX | private static final int | PASSWORD_INDEX | private static final int | MMSC_INDEX | private static final int | MCC_INDEX | private static final int | MNC_INDEX | private static final int | MMSPROXY_INDEX | private static final int | MMSPORT_INDEX | private static final int | TYPE_INDEX |
Methods Summary |
---|
private java.lang.String | checkNotSet(java.lang.String value)
if (value == null || value.equals(sNotSet)) {
return "";
} else {
return value;
}
| private java.lang.String | checkNull(java.lang.String value)
if (value == null || value.length() == 0) {
return sNotSet;
} else {
return value;
}
| private void | deleteApn()
getContentResolver().delete(mUri, null, null);
finish();
| private void | fillUi()
if (mFirstTime) {
mFirstTime = false;
// 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));
mProxy.setText(mCursor.getString(PROXY_INDEX));
mPort.setText(mCursor.getString(PORT_INDEX));
mUser.setText(mCursor.getString(USER_INDEX));
mServer.setText(mCursor.getString(SERVER_INDEX));
mPassword.setText(mCursor.getString(PASSWORD_INDEX));
mMmsProxy.setText(mCursor.getString(MMSPROXY_INDEX));
mMmsPort.setText(mCursor.getString(MMSPORT_INDEX));
mMmsc.setText(mCursor.getString(MMSC_INDEX));
mMcc.setText(mCursor.getString(MCC_INDEX));
mMnc.setText(mCursor.getString(MNC_INDEX));
mApnType.setText(mCursor.getString(TYPE_INDEX));
if (mNewApn) {
String numeric =
SystemProperties.get(TelephonyProperties.PROPERTY_SIM_OPERATOR_NUMERIC);
// MCC is first 3 chars and then in 2 - 3 chars of MNC
if (numeric != null && numeric.length() > 4) {
// Country code
String mcc = numeric.substring(0, 3);
// Network code
String mnc = numeric.substring(3);
// Auto populate MNC and MCC for new entries, based on what SIM reports
mMcc.setText(mcc);
mMnc.setText(mnc);
mCurMnc = mnc;
mCurMcc = mcc;
}
}
}
mName.setSummary(checkNull(mName.getText()));
mApn.setSummary(checkNull(mApn.getText()));
mProxy.setSummary(checkNull(mProxy.getText()));
mPort.setSummary(checkNull(mPort.getText()));
mUser.setSummary(checkNull(mUser.getText()));
mServer.setSummary(checkNull(mServer.getText()));
mPassword.setSummary(starify(mPassword.getText()));
mMmsProxy.setSummary(checkNull(mMmsProxy.getText()));
mMmsPort.setSummary(checkNull(mMmsPort.getText()));
mMmsc.setSummary(checkNull(mMmsc.getText()));
mMcc.setSummary(checkNull(mMcc.getText()));
mMnc.setSummary(checkNull(mMnc.getText()));
mApnType.setSummary(checkNull(mApnType.getText()));
| protected void | onCreate(android.os.Bundle icicle)
super.onCreate(icicle);
addPreferencesFromResource(R.xml.apn_editor);
sNotSet = getResources().getString(R.string.apn_not_set);
mName = (EditTextPreference) findPreference("apn_name");
mApn = (EditTextPreference) findPreference("apn_apn");
mProxy = (EditTextPreference) findPreference("apn_http_proxy");
mPort = (EditTextPreference) findPreference("apn_http_port");
mUser = (EditTextPreference) findPreference("apn_user");
mServer = (EditTextPreference) findPreference("apn_server");
mPassword = (EditTextPreference) findPreference("apn_password");
mMmsProxy = (EditTextPreference) findPreference("apn_mms_proxy");
mMmsPort = (EditTextPreference) findPreference("apn_mms_port");
mMmsc = (EditTextPreference) findPreference("apn_mmsc");
mMcc = (EditTextPreference) findPreference("apn_mcc");
mMnc = (EditTextPreference) findPreference("apn_mnc");
mApnType = (EditTextPreference) findPreference("apn_type");
mRes = getResources();
final Intent intent = getIntent();
final String action = intent.getAction();
mFirstTime = icicle == null;
if (action.equals(Intent.ACTION_EDIT)) {
mUri = intent.getData();
} else if (action.equals(Intent.ACTION_INSERT)) {
if (mFirstTime) {
mUri = getContentResolver().insert(intent.getData(), new ContentValues());
} else {
mUri = ContentUris.withAppendedId(Telephony.Carriers.CONTENT_URI,
icicle.getInt(SAVED_POS));
}
mNewApn = true;
// If we were unable to create a new note, then just finish
// this activity. A RESULT_CANCELED will be sent back to the
// original activity if they requested a result.
if (mUri == null) {
Log.w(TAG, "Failed to insert new telephony provider into "
+ getIntent().getData());
finish();
return;
}
// The new entry was created, so assume all will end well and
// set the result to be returned.
setResult(RESULT_OK, (new Intent()).setAction(mUri.toString()));
} else {
finish();
return;
}
mCursor = managedQuery(mUri, sProjection, null, null);
mCursor.moveToFirst();
fillUi();
| public boolean | onCreateOptionsMenu(android.view.Menu menu)
super.onCreateOptionsMenu(menu);
// 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)
.setIcon(android.R.drawable.ic_menu_delete);
}
menu.add(0, MENU_SAVE, 0, R.string.menu_save)
.setIcon(android.R.drawable.ic_menu_save);
menu.add(0, MENU_CANCEL, 0, R.string.menu_cancel)
.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
return true;
| public boolean | onKeyDown(int keyCode, android.view.KeyEvent event)
switch (keyCode) {
case KeyEvent.KEYCODE_BACK: {
if (validateAndSave(false)) {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
| public boolean | onOptionsItemSelected(android.view.MenuItem item)
switch (item.getItemId()) {
case MENU_DELETE:
deleteApn();
return true;
case MENU_SAVE:
if (validateAndSave(false)) {
finish();
}
return true;
case MENU_CANCEL:
if (mNewApn) {
getContentResolver().delete(mUri, null, null);
}
finish();
return true;
}
return super.onOptionsItemSelected(item);
| public void | onPause()
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
| public void | onResume()
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
| protected void | onSaveInstanceState(android.os.Bundle icicle)
super.onSaveInstanceState(icicle);
validateAndSave(true);
icicle.putInt(SAVED_POS, mCursor.getInt(ID_INDEX));
| public void | onSharedPreferenceChanged(android.content.SharedPreferences sharedPreferences, java.lang.String key)
Preference pref = findPreference(key);
if (pref != null) {
pref.setSummary(checkNull(sharedPreferences.getString(key, "")));
}
| private void | showErrorMessage(java.lang.String message)
new AlertDialog.Builder(this)
.setTitle(R.string.error_title)
.setMessage(message)
.setPositiveButton(android.R.string.ok, null)
.show();
| private java.lang.String | starify(java.lang.String value)
if (value == null || value.length() == 0) {
return sNotSet;
} else {
char[] password = new char[value.length()];
for (int i = 0; i < password.length; i++) {
password[i] = '*";
}
return new String(password);
}
| private boolean | validateAndSave(boolean force)Check the key fields' validity and save if valid.
String name = checkNotSet(mName.getText());
String apn = checkNotSet(mApn.getText());
String mcc = checkNotSet(mMcc.getText());
String mnc = checkNotSet(mMnc.getText());
String errorMsg = null;
if (name.length() < 1) {
errorMsg = mRes.getString(R.string.error_name_empty);
} else if (apn.length() < 1) {
errorMsg = mRes.getString(R.string.error_apn_empty);
} else if (mcc.length() != 3) {
errorMsg = mRes.getString(R.string.error_mcc_not3);
} else if ((mnc.length() & 0xFFFE) != 2) {
errorMsg = mRes.getString(R.string.error_mnc_not23);
}
if (errorMsg != null && !force) {
showErrorMessage(errorMsg);
return false;
}
if (!mCursor.moveToFirst()) {
Log.w(TAG,
"Could not go to the first row in the Cursor when saving data.");
return false;
}
ContentValues values = new ContentValues();
values.put(Telephony.Carriers.NAME, name);
values.put(Telephony.Carriers.APN, apn);
values.put(Telephony.Carriers.PROXY, checkNotSet(mProxy.getText()));
values.put(Telephony.Carriers.PORT, checkNotSet(mPort.getText()));
values.put(Telephony.Carriers.MMSPROXY, checkNotSet(mMmsProxy.getText()));
values.put(Telephony.Carriers.MMSPORT, checkNotSet(mMmsPort.getText()));
values.put(Telephony.Carriers.USER, checkNotSet(mUser.getText()));
values.put(Telephony.Carriers.SERVER, checkNotSet(mServer.getText()));
values.put(Telephony.Carriers.PASSWORD, checkNotSet(mPassword.getText()));
values.put(Telephony.Carriers.MMSC, checkNotSet(mMmsc.getText()));
values.put(Telephony.Carriers.TYPE, checkNotSet(mApnType.getText()));
values.put(Telephony.Carriers.MCC, mcc);
values.put(Telephony.Carriers.MNC, mnc);
values.put(Telephony.Carriers.NUMERIC, mcc + mnc);
if (mCurMnc != null && mCurMcc != null) {
if (mCurMnc.equals(mnc) && mCurMcc.equals(mcc)) {
values.put(Telephony.Carriers.CURRENT, 1);
}
}
getContentResolver().update(mUri, values, null, null);
return true;
|
|