PrivacySettings.java revision 4f33878f208e729cf96b708c57686555d2493129
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.settings; 18 19import android.app.AlertDialog; 20import android.app.Dialog; 21import android.app.backup.IBackupManager; 22import android.content.ContentResolver; 23import android.content.Context; 24import android.content.DialogInterface; 25import android.content.Intent; 26import android.os.Bundle; 27import android.os.RemoteException; 28import android.os.ServiceManager; 29import android.preference.CheckBoxPreference; 30import android.preference.Preference; 31import android.preference.PreferenceScreen; 32import android.provider.Settings; 33 34/** 35 * Gesture lock pattern settings. 36 */ 37public class PrivacySettings extends SettingsPreferenceFragment implements 38 DialogInterface.OnClickListener { 39 40 // Vendor specific 41 private static final String GSETTINGS_PROVIDER = "com.google.settings"; 42 private static final String BACKUP_CATEGORY = "backup_category"; 43 private static final String BACKUP_DATA = "backup_data"; 44 private static final String AUTO_RESTORE = "auto_restore"; 45 private static final String CONFIGURE_ACCOUNT = "configure_account"; 46 private static final String LOCAL_BACKUP_PASSWORD = "local_backup_password"; 47 private IBackupManager mBackupManager; 48 private CheckBoxPreference mBackup; 49 private CheckBoxPreference mAutoRestore; 50 private Dialog mConfirmDialog; 51 private PreferenceScreen mConfigure; 52 private PreferenceScreen mPassword; 53 54 private static final int DIALOG_ERASE_BACKUP = 2; 55 private int mDialogType; 56 57 @Override 58 public void onCreate(Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 addPreferencesFromResource(R.xml.privacy_settings); 61 final PreferenceScreen screen = getPreferenceScreen(); 62 63 mBackupManager = IBackupManager.Stub.asInterface( 64 ServiceManager.getService(Context.BACKUP_SERVICE)); 65 66 mBackup = (CheckBoxPreference) screen.findPreference(BACKUP_DATA); 67 mAutoRestore = (CheckBoxPreference) screen.findPreference(AUTO_RESTORE); 68 mConfigure = (PreferenceScreen) screen.findPreference(CONFIGURE_ACCOUNT); 69 mPassword = (PreferenceScreen) screen.findPreference(LOCAL_BACKUP_PASSWORD); 70 71 // Vendor specific 72 if (getActivity().getPackageManager(). 73 resolveContentProvider(GSETTINGS_PROVIDER, 0) == null) { 74 screen.removePreference(findPreference(BACKUP_CATEGORY)); 75 } 76 updateToggles(); 77 } 78 79 @Override 80 public void onResume() { 81 super.onResume(); 82 83 // Refresh UI 84 updateToggles(); 85 } 86 87 @Override 88 public void onStop() { 89 if (mConfirmDialog != null && mConfirmDialog.isShowing()) { 90 mConfirmDialog.dismiss(); 91 } 92 mConfirmDialog = null; 93 mDialogType = 0; 94 super.onStop(); 95 } 96 97 @Override 98 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 99 Preference preference) { 100 if (preference == mBackup) { 101 if (!mBackup.isChecked()) { 102 showEraseBackupDialog(); 103 } else { 104 setBackupEnabled(true); 105 } 106 } else if (preference == mAutoRestore) { 107 IBackupManager bm = IBackupManager.Stub.asInterface( 108 ServiceManager.getService(Context.BACKUP_SERVICE)); 109 if (bm != null) { 110 // TODO: disable via the backup manager interface 111 boolean curState = mAutoRestore.isChecked(); 112 try { 113 bm.setAutoRestore(curState); 114 } catch (RemoteException e) { 115 mAutoRestore.setChecked(!curState); 116 } 117 } 118 } 119 return super.onPreferenceTreeClick(preferenceScreen, preference); 120 } 121 122 private void showEraseBackupDialog() { 123 mBackup.setChecked(true); 124 125 mDialogType = DIALOG_ERASE_BACKUP; 126 CharSequence msg = getResources().getText(R.string.backup_erase_dialog_message); 127 // TODO: DialogFragment? 128 mConfirmDialog = new AlertDialog.Builder(getActivity()).setMessage(msg) 129 .setTitle(R.string.backup_erase_dialog_title) 130 .setIcon(android.R.drawable.ic_dialog_alert) 131 .setPositiveButton(android.R.string.ok, this) 132 .setNegativeButton(android.R.string.cancel, this) 133 .show(); 134 } 135 136 /* 137 * Creates toggles for each available location provider 138 */ 139 private void updateToggles() { 140 ContentResolver res = getContentResolver(); 141 142 boolean backupEnabled = false; 143 Intent configIntent = null; 144 String configSummary = null; 145 try { 146 backupEnabled = mBackupManager.isBackupEnabled(); 147 String transport = mBackupManager.getCurrentTransport(); 148 configIntent = mBackupManager.getConfigurationIntent(transport); 149 configSummary = mBackupManager.getDestinationString(transport); 150 } catch (RemoteException e) { 151 // leave it 'false' and disable the UI; there's no backup manager 152 mBackup.setEnabled(false); 153 } 154 mBackup.setChecked(backupEnabled); 155 156 mAutoRestore.setChecked(Settings.Secure.getInt(res, 157 Settings.Secure.BACKUP_AUTO_RESTORE, 1) == 1); 158 mAutoRestore.setEnabled(backupEnabled); 159 160 final boolean configureEnabled = (configIntent != null) && backupEnabled; 161 mConfigure.setEnabled(configureEnabled); 162 mConfigure.setIntent(configIntent); 163 setConfigureSummary(configSummary); 164 165 updatePasswordSummary(); 166} 167 168 private void setConfigureSummary(String summary) { 169 if (summary != null) { 170 mConfigure.setSummary(summary); 171 } else { 172 mConfigure.setSummary(R.string.backup_configure_account_default_summary); 173 } 174 } 175 176 private void updateConfigureSummary() { 177 try { 178 String transport = mBackupManager.getCurrentTransport(); 179 String summary = mBackupManager.getDestinationString(transport); 180 setConfigureSummary(summary); 181 } catch (RemoteException e) { 182 // Not much we can do here 183 } 184 } 185 186 private void updatePasswordSummary() { 187 try { 188 if (mBackupManager.hasBackupPassword()) { 189 mPassword.setSummary(R.string.local_backup_password_summary_change); 190 } else { 191 mPassword.setSummary(R.string.local_backup_password_summary_none); 192 } 193 } catch (RemoteException e) { 194 // Not much we can do here 195 } 196 } 197 198 public void onClick(DialogInterface dialog, int which) { 199 if (which == DialogInterface.BUTTON_POSITIVE) { 200 //updateProviders(); 201 if (mDialogType == DIALOG_ERASE_BACKUP) { 202 setBackupEnabled(false); 203 updateConfigureSummary(); 204 } 205 } 206 mDialogType = 0; 207 } 208 209 /** 210 * Informs the BackupManager of a change in backup state - if backup is disabled, 211 * the data on the server will be erased. 212 * @param enable whether to enable backup 213 */ 214 private void setBackupEnabled(boolean enable) { 215 if (mBackupManager != null) { 216 try { 217 mBackupManager.setBackupEnabled(enable); 218 } catch (RemoteException e) { 219 mBackup.setChecked(!enable); 220 mAutoRestore.setEnabled(!enable); 221 return; 222 } 223 } 224 mBackup.setChecked(enable); 225 mAutoRestore.setEnabled(enable); 226 mConfigure.setEnabled(enable); 227 } 228} 229