2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
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;
35 * Gesture lock pattern settings.
37 public class PrivacySettings extends SettingsPreferenceFragment implements
38 DialogInterface.OnClickListener {
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;
52 private static final int DIALOG_ERASE_BACKUP = 2;
53 private int mDialogType;
56 public void onCreate(Bundle savedInstanceState) {
57 super.onCreate(savedInstanceState);
58 addPreferencesFromResource(R.xml.privacy_settings);
59 final PreferenceScreen screen = getPreferenceScreen();
61 mBackupManager = IBackupManager.Stub.asInterface(
62 ServiceManager.getService(Context.BACKUP_SERVICE));
64 mBackup = (CheckBoxPreference) screen.findPreference(BACKUP_DATA);
65 mAutoRestore = (CheckBoxPreference) screen.findPreference(AUTO_RESTORE);
66 mConfigure = (PreferenceScreen) screen.findPreference(CONFIGURE_ACCOUNT);
69 if (getActivity().getPackageManager().
70 resolveContentProvider(GSETTINGS_PROVIDER, 0) == null) {
71 screen.removePreference(findPreference(BACKUP_CATEGORY));
77 public void onResume() {
85 public void onStop() {
86 if (mConfirmDialog != null && mConfirmDialog.isShowing()) {
87 mConfirmDialog.dismiss();
89 mConfirmDialog = null;
95 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
96 Preference preference) {
97 if (preference == mBackup) {
98 if (!mBackup.isChecked()) {
99 showEraseBackupDialog();
101 setBackupEnabled(true);
103 } else if (preference == mAutoRestore) {
104 boolean curState = mAutoRestore.isChecked();
106 mBackupManager.setAutoRestore(curState);
107 } catch (RemoteException e) {
108 mAutoRestore.setChecked(!curState);
111 return super.onPreferenceTreeClick(preferenceScreen, preference);
114 private void showEraseBackupDialog() {
115 mBackup.setChecked(true);
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 .setIcon(android.R.drawable.ic_dialog_alert)
123 .setPositiveButton(android.R.string.ok, this)
124 .setNegativeButton(android.R.string.cancel, this)
129 * Creates toggles for each available location provider
131 private void updateToggles() {
132 ContentResolver res = getContentResolver();
134 boolean backupEnabled = false;
135 Intent configIntent = null;
136 String configSummary = null;
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);
146 mBackup.setChecked(backupEnabled);
148 mAutoRestore.setChecked(Settings.Secure.getInt(res,
149 Settings.Secure.BACKUP_AUTO_RESTORE, 1) == 1);
150 mAutoRestore.setEnabled(backupEnabled);
152 final boolean configureEnabled = (configIntent != null) && backupEnabled;
153 mConfigure.setEnabled(configureEnabled);
154 mConfigure.setIntent(configIntent);
155 setConfigureSummary(configSummary);
158 private void setConfigureSummary(String summary) {
159 if (summary != null) {
160 mConfigure.setSummary(summary);
162 mConfigure.setSummary(R.string.backup_configure_account_default_summary);
166 private void updateConfigureSummary() {
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
176 public void onClick(DialogInterface dialog, int which) {
177 if (which == DialogInterface.BUTTON_POSITIVE) {
179 if (mDialogType == DIALOG_ERASE_BACKUP) {
180 setBackupEnabled(false);
181 updateConfigureSummary();
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
192 private void setBackupEnabled(boolean enable) {
193 if (mBackupManager != null) {
195 mBackupManager.setBackupEnabled(enable);
196 } catch (RemoteException e) {
197 mBackup.setChecked(!enable);
198 mAutoRestore.setEnabled(!enable);
202 mBackup.setChecked(enable);
203 mAutoRestore.setEnabled(enable);
204 mConfigure.setEnabled(enable);