SecuritySettings: reduce 1 access to KeyStore and clean up.

This commit is contained in:
Chia-chi Yeh
2009-10-06 15:43:55 +08:00
parent 059c3f5d1e
commit 0bc5e275fd

View File

@@ -25,24 +25,17 @@ import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor; import android.database.Cursor;
import android.location.LocationManager; import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory; import android.preference.PreferenceCategory;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.provider.Settings; import android.provider.Settings;
import android.security.Credentials; import android.security.Credentials;
import android.security.KeyStore; import android.security.KeyStore;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
@@ -51,8 +44,6 @@ import android.widget.Toast;
import com.android.internal.widget.LockPatternUtils; import com.android.internal.widget.LockPatternUtils;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable; import java.util.Observable;
import java.util.Observer; import java.util.Observer;
@@ -86,13 +77,6 @@ public class SecuritySettings extends PreferenceActivity {
private static final String ASSISTED_GPS = "assisted_gps"; private static final String ASSISTED_GPS = "assisted_gps";
// Credential storage // Credential storage
private static final int CSTOR_MIN_PASSWORD_LENGTH = 8;
private static final int CSTOR_INIT_DIALOG = 1;
private static final int CSTOR_CHANGE_PASSWORD_DIALOG = 2;
private static final int CSTOR_UNLOCK_DIALOG = 3;
private static final int CSTOR_RESET_DIALOG = 4;
private CredentialStorage mCredentialStorage = new CredentialStorage(); private CredentialStorage mCredentialStorage = new CredentialStorage();
private CheckBoxPreference mNetwork; private CheckBoxPreference mNetwork;
@@ -131,8 +115,6 @@ public class SecuritySettings extends PreferenceActivity {
null); null);
mContentQueryMap = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, null); mContentQueryMap = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, null);
mContentQueryMap.addObserver(new SettingsObserver()); mContentQueryMap.addObserver(new SettingsObserver());
mCredentialStorage.handleIntent(getIntent());
} }
private PreferenceScreen createPreferenceHierarchy() { private PreferenceScreen createPreferenceHierarchy() {
@@ -339,57 +321,37 @@ public class SecuritySettings extends PreferenceActivity {
} }
} }
@Override
protected Dialog onCreateDialog(int id) {
return mCredentialStorage.createDialog(id);
}
private class CredentialStorage implements DialogInterface.OnClickListener, private class CredentialStorage implements DialogInterface.OnClickListener,
DialogInterface.OnDismissListener, Preference.OnPreferenceChangeListener, DialogInterface.OnDismissListener, Preference.OnPreferenceChangeListener,
Preference.OnPreferenceClickListener { Preference.OnPreferenceClickListener {
private static final int MINIMUM_PASSWORD_LENGTH = 8; private static final int MINIMUM_PASSWORD_LENGTH = 8;
private static final int UNLOCK_DIALOG = 1;
private static final int PASSWORD_DIALOG = 2;
private static final int RESET_DIALOG = 3;
private KeyStore mKeyStore = KeyStore.getInstance(); private KeyStore mKeyStore = KeyStore.getInstance();
private int mState = mKeyStore.test(); private int mState;
private int mDialogId; private boolean mSubmit = false;
private boolean mSubmit; private boolean mExternal = false;
private CheckBoxPreference mAccessCheckBox; private CheckBoxPreference mAccessCheckBox;
private Preference mInstallButton; private Preference mInstallButton;
private Preference mPasswordButton; private Preference mPasswordButton;
private Preference mResetButton; private Preference mResetButton;
private Intent mExternalIntent;
void handleIntent(Intent intent) {
if (intent != null) {
if (Credentials.UNLOCK_ACTION.equals(intent.getAction())) {
mExternalIntent = intent;
showDialog((mState == KeyStore.UNINITIALIZED) ?
PASSWORD_DIALOG : UNLOCK_DIALOG);
}
}
}
void resume() { void resume() {
mState = mKeyStore.test(); mState = mKeyStore.test();
updatePreferences(mState); updatePreferences(mState);
}
Dialog createDialog(int id) { Intent intent = getIntent();
mDialogId = id; if (!mExternal && intent != null &&
switch (id) { Credentials.UNLOCK_ACTION.equals(intent.getAction())) {
case UNLOCK_DIALOG: mExternal = true;
return createUnlockDialog(); if (mState == KeyStore.UNINITIALIZED) {
case PASSWORD_DIALOG: showPasswordDialog();
return createPasswordDialog(); } else if (mState == KeyStore.LOCKED) {
case RESET_DIALOG: showUnlockDialog();
return createResetDialog(); } else {
finish();
}
} }
return null;
} }
private void initialize(String password) { private void initialize(String password) {
@@ -420,8 +382,7 @@ public class SecuritySettings extends PreferenceActivity {
public boolean onPreferenceChange(Preference preference, Object value) { public boolean onPreferenceChange(Preference preference, Object value) {
if (preference == mAccessCheckBox) { if (preference == mAccessCheckBox) {
if ((Boolean) value) { if ((Boolean) value) {
showDialog((mState == KeyStore.UNINITIALIZED) ? showUnlockDialog();
PASSWORD_DIALOG : UNLOCK_DIALOG);
} else { } else {
lock(); lock();
} }
@@ -434,9 +395,9 @@ public class SecuritySettings extends PreferenceActivity {
if (preference == mInstallButton) { if (preference == mInstallButton) {
Credentials.getInstance().installFromSdCard(SecuritySettings.this); Credentials.getInstance().installFromSdCard(SecuritySettings.this);
} else if (preference == mPasswordButton) { } else if (preference == mPasswordButton) {
showDialog(PASSWORD_DIALOG); showPasswordDialog();
} else if (preference == mResetButton) { } else if (preference == mResetButton) {
showDialog(RESET_DIALOG); showResetDialog();
} else { } else {
return false; return false;
} }
@@ -454,14 +415,12 @@ public class SecuritySettings extends PreferenceActivity {
if (mSubmit && !isFinishing()) { if (mSubmit && !isFinishing()) {
mSubmit = false; mSubmit = false;
if (!checkPassword((Dialog) dialog)) { if (!checkPassword((Dialog) dialog)) {
showDialog(mDialogId); ((Dialog) dialog).show();
return; return;
} }
} }
removeDialog(mDialogId);
updatePreferences(mState); updatePreferences(mState);
if (mExternalIntent != null) { if (mExternal) {
mExternalIntent = null;
finish(); finish();
} }
} }
@@ -581,12 +540,12 @@ public class SecuritySettings extends PreferenceActivity {
mState = state; mState = state;
} }
private Dialog createUnlockDialog() { private void showUnlockDialog() {
View view = View.inflate(SecuritySettings.this, View view = View.inflate(SecuritySettings.this,
R.layout.credentials_unlock_dialog, null); R.layout.credentials_unlock_dialog, null);
// show extra hint only when the action comes from outside // Show extra hint only when the action comes from outside.
if (mExternalIntent != null) { if (mExternal) {
view.findViewById(R.id.hint).setVisibility(View.VISIBLE); view.findViewById(R.id.hint).setVisibility(View.VISIBLE);
} }
@@ -597,10 +556,10 @@ public class SecuritySettings extends PreferenceActivity {
.setNegativeButton(android.R.string.cancel, this) .setNegativeButton(android.R.string.cancel, this)
.create(); .create();
dialog.setOnDismissListener(this); dialog.setOnDismissListener(this);
return dialog; dialog.show();
} }
private Dialog createPasswordDialog() { private void showPasswordDialog() {
View view = View.inflate(SecuritySettings.this, View view = View.inflate(SecuritySettings.this,
R.layout.credentials_password_dialog, null); R.layout.credentials_password_dialog, null);
@@ -618,17 +577,17 @@ public class SecuritySettings extends PreferenceActivity {
.setNegativeButton(android.R.string.cancel, this) .setNegativeButton(android.R.string.cancel, this)
.create(); .create();
dialog.setOnDismissListener(this); dialog.setOnDismissListener(this);
return dialog; dialog.show();
} }
private Dialog createResetDialog() { private void showResetDialog() {
return new AlertDialog.Builder(SecuritySettings.this) new AlertDialog.Builder(SecuritySettings.this)
.setTitle(android.R.string.dialog_alert_title) .setTitle(android.R.string.dialog_alert_title)
.setIcon(android.R.drawable.ic_dialog_alert) .setIcon(android.R.drawable.ic_dialog_alert)
.setMessage(R.string.credentials_reset_hint) .setMessage(R.string.credentials_reset_hint)
.setNeutralButton(getString(android.R.string.ok), this) .setNeutralButton(getString(android.R.string.ok), this)
.setNegativeButton(getString(android.R.string.cancel), this) .setNegativeButton(getString(android.R.string.cancel), this)
.create(); .create().show();
} }
} }
} }