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 17 package com.android.settings; 18 19 import android.app.AlertDialog; 20 import android.app.Dialog; 21 import android.app.backup.IBackupManager; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.DialogInterface; 25 import android.content.Intent; 26 import android.os.Bundle; 27 import android.os.RemoteException; 28 import android.os.ServiceManager; 29 import android.preference.CheckBoxPreference; 30 import android.preference.Preference; 31 import android.preference.PreferenceScreen; 32 import android.provider.Settings; 33 34 /** 35 * Gesture lock pattern settings. 36 */ 37 public 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 IBackupManager mBackupManager; 47 private CheckBoxPreference mBackup; 48 private CheckBoxPreference mAutoRestore; 49 private Dialog mConfirmDialog; 50 private PreferenceScreen mConfigure; 51 52 private static final int DIALOG_ERASE_BACKUP = 2; 53 private int mDialogType; 54 55 @Override 56 public void onCreate(Bundle savedInstanceState) { 57 super.onCreate(savedInstanceState); 58 addPreferencesFromResource(R.xml.privacy_settings); 59 final PreferenceScreen screen = getPreferenceScreen(); 60 61 mBackupManager = IBackupManager.Stub.asInterface( 62 ServiceManager.getService(Context.BACKUP_SERVICE)); 63 64 mBackup = (CheckBoxPreference) screen.findPreference(BACKUP_DATA); 65 mAutoRestore = (CheckBoxPreference) screen.findPreference(AUTO_RESTORE); 66 mConfigure = (PreferenceScreen) screen.findPreference(CONFIGURE_ACCOUNT); 67 68 // Vendor specific 69 if (getActivity().getPackageManager(). 70 resolveContentProvider(GSETTINGS_PROVIDER, 0) == null) { 71 screen.removePreference(findPreference(BACKUP_CATEGORY)); 72 } 73 updateToggles(); 74 } 75 76 @Override 77 public void onResume() { 78 super.onResume(); 79 80 // Refresh UI 81 updateToggles(); 82 } 83 84 @Override 85 public void onStop() { 86 if (mConfirmDialog != null && mConfirmDialog.isShowing()) { 87 mConfirmDialog.dismiss(); 88 } 89 mConfirmDialog = null; 90 mDialogType = 0; 91 super.onStop(); 92 } 93 94 @Override 95 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 96 Preference preference) { 97 if (preference == mBackup) { 98 if (!mBackup.isChecked()) { 99 showEraseBackupDialog(); 100 } else { 101 setBackupEnabled(true); 102 } 103 } else if (preference == mAutoRestore) { 104 boolean curState = mAutoRestore.isChecked(); 105 try { 106 mBackupManager.setAutoRestore(curState); 107 } catch (RemoteException e) { 108 mAutoRestore.setChecked(!curState); 109 } 110 } 111 return super.onPreferenceTreeClick(preferenceScreen, preference); 112 } 113 114 private void showEraseBackupDialog() { 115 mBackup.setChecked(true); 116 117 mDialogType = DIALOG_ERASE_BACKUP; 118 CharSequence msg = getResources().getText(R.string.backup_erase_dialog_message); 119 // TODO: DialogFragment? 120 mConfirmDialog = new AlertDialog.Builder(getActivity()).setMessage(msg) 121 .setTitle(R.string.backup_erase_dialog_title) 122 .setIconAttribute(android.R.attr.alertDialogIcon) 123 .setPositiveButton(android.R.string.ok, this) 124 .setNegativeButton(android.R.string.cancel, this) 125 .show(); 126 } 127 128 /* 129 * Creates toggles for each available location provider 130 */ 131 private void updateToggles() { 132 ContentResolver res = getContentResolver(); 133 134 boolean backupEnabled = false; 135 Intent configIntent = null; 136 String configSummary = null; 137 try { 138 backupEnabled = mBackupManager.isBackupEnabled(); 139 String transport = mBackupManager.getCurrentTransport(); 140 configIntent = mBackupManager.getConfigurationIntent(transport); 141 configSummary = mBackupManager.getDestinationString(transport); 142 } catch (RemoteException e) { 143 // leave it 'false' and disable the UI; there's no backup manager 144 mBackup.setEnabled(false); 145 } 146 mBackup.setChecked(backupEnabled); 147 148 mAutoRestore.setChecked(Settings.Secure.getInt(res, 149 Settings.Secure.BACKUP_AUTO_RESTORE, 1) == 1); 150 mAutoRestore.setEnabled(backupEnabled); 151 152 final boolean configureEnabled = (configIntent != null) && backupEnabled; 153 mConfigure.setEnabled(configureEnabled); 154 mConfigure.setIntent(configIntent); 155 setConfigureSummary(configSummary); 156 } 157 158 private void setConfigureSummary(String summary) { 159 if (summary != null) { 160 mConfigure.setSummary(summary); 161 } else { 162 mConfigure.setSummary(R.string.backup_configure_account_default_summary); 163 } 164 } 165 166 private void updateConfigureSummary() { 167 try { 168 String transport = mBackupManager.getCurrentTransport(); 169 String summary = mBackupManager.getDestinationString(transport); 170 setConfigureSummary(summary); 171 } catch (RemoteException e) { 172 // Not much we can do here 173 } 174 } 175 176 public void onClick(DialogInterface dialog, int which) { 177 if (which == DialogInterface.BUTTON_POSITIVE) { 178 //updateProviders(); 179 if (mDialogType == DIALOG_ERASE_BACKUP) { 180 setBackupEnabled(false); 181 updateConfigureSummary(); 182 } 183 } 184 mDialogType = 0; 185 } 186 187 /** 188 * Informs the BackupManager of a change in backup state - if backup is disabled, 189 * the data on the server will be erased. 190 * @param enable whether to enable backup 191 */ 192 private void setBackupEnabled(boolean enable) { 193 if (mBackupManager != null) { 194 try { 195 mBackupManager.setBackupEnabled(enable); 196 } catch (RemoteException e) { 197 mBackup.setChecked(!enable); 198 mAutoRestore.setEnabled(!enable); 199 return; 200 } 201 } 202 mBackup.setChecked(enable); 203 mAutoRestore.setEnabled(enable); 204 mConfigure.setEnabled(enable); 205 } 206 207 @Override 208 protected int getHelpResource() { 209 return R.string.help_url_backup_reset; 210 } 211 } 212