FileDocCategorySizeDatePackage
SimLockSettings.javaAPI DocAndroid 1.5 API11380Wed May 06 22:42:48 BST 2009com.android.settings

SimLockSettings

public class SimLockSettings extends android.preference.PreferenceActivity implements EditPinPreference.OnPinEnteredListener
Implements the preference screen to enable/disable SIM lock and also the dialogs to change the SIM PIN. In the former case, enabling/disabling the SIM lock will prompt the user for the current PIN. In the Change PIN case, it prompts the user for old pin, new pin and new pin again before attempting to change it. Calls the SimCard interface to execute these operations.

Fields Summary
private static final int
OFF_MODE
private static final int
SIM_LOCK_MODE
private static final int
SIM_OLD_MODE
private static final int
SIM_NEW_MODE
private static final int
SIM_REENTER_MODE
private static final String
PIN_DIALOG
private static final String
PIN_TOGGLE
private static final String
DIALOG_STATE
private static final String
DIALOG_PIN
private static final String
DIALOG_ERROR
private static final String
ENABLE_TO_STATE
private static final int
MIN_PIN_LENGTH
private static final int
MAX_PIN_LENGTH
private int
mDialogState
private String
mPin
private String
mOldPin
private String
mNewPin
private String
mError
private boolean
mToState
private com.android.internal.telephony.Phone
mPhone
private EditPinPreference
mPinDialog
private android.preference.CheckBoxPreference
mPinToggle
private android.content.res.Resources
mRes
private static final int
ENABLE_SIM_PIN_COMPLETE
private static final int
CHANGE_SIM_PIN_COMPLETE
private android.os.Handler
mHandler
Constructors Summary
Methods Summary
static java.lang.StringgetSummary(android.content.Context context)

        Resources res = context.getResources();
        String summary = isSimLockEnabled() 
                ? res.getString(R.string.sim_lock_on) 
                : res.getString(R.string.sim_lock_off);
        return summary;
    
static booleanisSimLockEnabled()

    
    // For top-level settings screen to query
       
        return PhoneFactory.getDefaultPhone().getSimCard().getSimLockEnabled();
    
protected voidonCreate(android.os.Bundle savedInstanceState)

        super.onCreate(savedInstanceState);
    
        addPreferencesFromResource(R.xml.sim_lock_settings);
        
        mPinDialog = (EditPinPreference) findPreference(PIN_DIALOG);
        mPinToggle = (CheckBoxPreference) findPreference(PIN_TOGGLE);
        if (savedInstanceState != null && savedInstanceState.containsKey(DIALOG_STATE)) {
            mDialogState = savedInstanceState.getInt(DIALOG_STATE);
            mPin = savedInstanceState.getString(DIALOG_PIN);
            mError = savedInstanceState.getString(DIALOG_ERROR);
            mToState = savedInstanceState.getBoolean(ENABLE_TO_STATE);
        }

        mPinDialog.setOnPinEnteredListener(this);
        
        // Don't need any changes to be remembered
        getPreferenceScreen().setPersistent(false);
        
        mPhone = PhoneFactory.getDefaultPhone();
        mRes = getResources();
    
public voidonPinEntered(EditPinPreference preference, boolean positiveResult)

        if (!positiveResult) {
            resetDialogState();
            return;
        }
        
        mPin = preference.getText();
        if (!reasonablePin(mPin)) {
            // inject error message and display dialog again
            mError = mRes.getString(R.string.sim_bad_pin);
            showPinDialog();
            return;
        }
        switch (mDialogState) {
            case SIM_LOCK_MODE:
                tryChangeSimLockState();
                break;
            case SIM_OLD_MODE:
                mOldPin = mPin;
                mDialogState = SIM_NEW_MODE;
                mError = null;
                mPin = null;
                showPinDialog();
                break;
            case SIM_NEW_MODE:
                mNewPin = mPin;
                mDialogState = SIM_REENTER_MODE;
                mPin = null;
                showPinDialog();
                break;
            case SIM_REENTER_MODE:
                if (!mPin.equals(mNewPin)) {
                    mError = mRes.getString(R.string.sim_pins_dont_match);
                    mDialogState = SIM_NEW_MODE;
                    mPin = null;
                    showPinDialog();
                } else {
                    mError = null;
                    tryChangePin();
                }
                break;
        }
    
public booleanonPreferenceTreeClick(android.preference.PreferenceScreen preferenceScreen, android.preference.Preference preference)

        if (preference == mPinToggle) {
            // Get the new, preferred state
            mToState = mPinToggle.isChecked();
            // Flip it back and pop up pin dialog  
            mPinToggle.setChecked(!mToState);  
            mDialogState = SIM_LOCK_MODE;
            showPinDialog();
        }
        return true;
    
protected voidonResume()

        super.onResume();
        
        mPinToggle.setChecked(mPhone.getSimCard().getSimLockEnabled());
        
        if (mDialogState != OFF_MODE) {
            showPinDialog();
        } else {
            // Prep for standard click on "Change PIN"
            resetDialogState();
        }
    
protected voidonSaveInstanceState(android.os.Bundle out)

        // Need to store this state for slider open/close
        // There is one case where the dialog is popped up by the preference
        // framework. In that case, let the preference framework store the
        // dialog state. In other cases, where this activity manually launches
        // the dialog, store the state of the dialog.
        if (mPinDialog.isDialogOpen()) {
            out.putInt(DIALOG_STATE, mDialogState);
            out.putString(DIALOG_PIN, mPinDialog.getEditText().getText().toString());
            out.putString(DIALOG_ERROR, mError);
            out.putBoolean(ENABLE_TO_STATE, mToState);
        } else {
            super.onSaveInstanceState(out);
        }
    
private booleanreasonablePin(java.lang.String pin)

        if (pin == null || pin.length() < MIN_PIN_LENGTH || pin.length() > MAX_PIN_LENGTH) {
            return false;
        } else {
            return true;
        }
    
private voidresetDialogState()

        mError = null;
        mDialogState = SIM_OLD_MODE; // Default for when Change PIN is clicked
        mPin = "";
        setDialogValues();
    
private voidsetDialogValues()

        mPinDialog.setText(mPin);
        String message = "";
        switch (mDialogState) {
            case SIM_LOCK_MODE:
                message = mRes.getString(R.string.sim_enter_pin);
                mPinDialog.setDialogTitle(mToState 
                        ? mRes.getString(R.string.sim_enable_sim_lock)
                        : mRes.getString(R.string.sim_disable_sim_lock));
                break;
            case SIM_OLD_MODE:
                message = mRes.getString(R.string.sim_enter_old);
                mPinDialog.setDialogTitle(mRes.getString(R.string.sim_change_pin));
                break;
            case SIM_NEW_MODE:
                message = mRes.getString(R.string.sim_enter_new);
                mPinDialog.setDialogTitle(mRes.getString(R.string.sim_change_pin));
                break;
            case SIM_REENTER_MODE:
                message = mRes.getString(R.string.sim_reenter_new);
                mPinDialog.setDialogTitle(mRes.getString(R.string.sim_change_pin));
                break;
        }
        if (mError != null) {
            message = mError + "\n" + message;
            mError = null;
        }
        mPinDialog.setDialogMessage(message);
    
private voidshowPinDialog()

        if (mDialogState == OFF_MODE) {
            return;
        }
        setDialogValues();
        
        mPinDialog.showPinDialog();
    
private voidsimLockChanged(boolean success)

        if (success) {
            mPinToggle.setChecked(mToState);
        } else {
            // TODO: I18N
            Toast.makeText(this, mRes.getString(R.string.sim_lock_failed), Toast.LENGTH_SHORT)
                    .show();
        }
        resetDialogState();
    
private voidsimPinChanged(boolean success)

        if (!success) {
         // TODO: I18N
            Toast.makeText(this, mRes.getString(R.string.sim_change_failed), 
                    Toast.LENGTH_SHORT)
                    .show();
        } else {
            Toast.makeText(this, mRes.getString(R.string.sim_change_succeeded), 
                    Toast.LENGTH_SHORT)
                    .show();

        }
        resetDialogState();
    
private voidtryChangePin()

        Message callback = Message.obtain(mHandler, CHANGE_SIM_PIN_COMPLETE);
        mPhone.getSimCard().changeSimLockPassword(mOldPin,
                mNewPin, callback);
    
private voidtryChangeSimLockState()

        // Try to change sim lock. If it succeeds, toggle the lock state and 
        // reset dialog state. Else inject error message and show dialog again.
        Message callback = Message.obtain(mHandler, ENABLE_SIM_PIN_COMPLETE);
        mPhone.getSimCard().setSimLockEnabled(mToState, mPin, callback);