Code example for CheckBoxPreference

Methods: isCheckedsetChecked

0
 
    @Override 
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
            Preference preference) {
        if (preference == mBackup) {
            if (!mBackup.isChecked()) {
                showEraseBackupDialog(); 
            } else { 
                setBackupEnabled(true); 
            } 
        } else if (preference == mAutoRestore) {
            boolean curState = mAutoRestore.isChecked();
            try { 
                mBackupManager.setAutoRestore(curState);
            } catch (RemoteException e) {
                mAutoRestore.setChecked(!curState);
            } 
        } 
        return super.onPreferenceTreeClick(preferenceScreen, preference);
    } 
 
    private void showEraseBackupDialog() { 
        mBackup.setChecked(true);
 
        mDialogType = DIALOG_ERASE_BACKUP;
        CharSequence msg = getResources().getText(R.string.backup_erase_dialog_message);
        // TODO: DialogFragment? 
        mConfirmDialog = new AlertDialog.Builder(getActivity()).setMessage(msg)
                .setTitle(R.string.backup_erase_dialog_title)
                .setIconAttribute(android.R.attr.alertDialogIcon)
                .setPositiveButton(android.R.string.ok, this)
                .setNegativeButton(android.R.string.cancel, this)
                .show(); 
    } 
 
    /* 
     * Creates toggles for each available location provider 
     */ 
    private void updateToggles() { 
        ContentResolver res = getContentResolver();
 
        boolean backupEnabled = false;
        Intent configIntent = null;
        String configSummary = null;
        try { 
            backupEnabled = mBackupManager.isBackupEnabled();
            String transport = mBackupManager.getCurrentTransport();
            configIntent = mBackupManager.getConfigurationIntent(transport);
            configSummary = mBackupManager.getDestinationString(transport);
        } catch (RemoteException e) {
            // leave it 'false' and disable the UI; there's no backup manager 
            mBackup.setEnabled(false);
        } 
        mBackup.setChecked(backupEnabled);
 
        mAutoRestore.setChecked(Settings.Secure.getInt(res,
                Settings.Secure.BACKUP_AUTO_RESTORE, 1) == 1); 
        mAutoRestore.setEnabled(backupEnabled);
 
        final boolean configureEnabled = (configIntent != null) && backupEnabled;
        mConfigure.setEnabled(configureEnabled);
        mConfigure.setIntent(configIntent);
        setConfigureSummary(configSummary);
} 
 
    private void setConfigureSummary(String summary) {
        if (summary != null) {
            mConfigure.setSummary(summary);
        } else { 
            mConfigure.setSummary(R.string.backup_configure_account_default_summary);
        } 
    } 
 
    private void updateConfigureSummary() { 
        try { 
            String transport = mBackupManager.getCurrentTransport();
            String summary = mBackupManager.getDestinationString(transport);
            setConfigureSummary(summary);
        } catch (RemoteException e) {
            // Not much we can do here 
        } 
    } 
 
    public void onClick(DialogInterface dialog, int which) {
        if (which == DialogInterface.BUTTON_POSITIVE) {
            //updateProviders(); 
            if (mDialogType == DIALOG_ERASE_BACKUP) {
                setBackupEnabled(false); 
                updateConfigureSummary(); 
            } 
        } 
        mDialogType = 0;
    } 
 
    /** 
     * Informs the BackupManager of a change in backup state - if backup is disabled, 
     * the data on the server will be erased. 
     * @param enable whether to enable backup 
     */ 
    private void setBackupEnabled(boolean enable) {
        if (mBackupManager != null) {
            try { 
                mBackupManager.setBackupEnabled(enable);
            } catch (RemoteException e) {
                mBackup.setChecked(!enable);
                mAutoRestore.setEnabled(!enable);
                return; 
            } 
        } 
        mBackup.setChecked(enable);
        mAutoRestore.setEnabled(enable);
        mConfigure.setEnabled(enable);
    } 
 
    @Override