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.backup.IBackupManager;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.content.DialogInterface;
25 import android.content.SharedPreferences;
26 import android.content.pm.PackageManager.NameNotFoundException;
27 import android.os.Bundle;
28 import android.os.RemoteException;
29 import android.os.ServiceManager;
30 import android.preference.CheckBoxPreference;
31 import android.preference.Preference;
32 import android.preference.PreferenceActivity;
33 import android.preference.PreferenceScreen;
34 import android.provider.Settings;
35 import android.text.method.LinkMovementMethod;
36 import android.widget.TextView;
39 * Gesture lock pattern settings.
41 public class PrivacySettings extends PreferenceActivity implements
42 DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
44 private static final String PREFS_NAME = "location_prefs";
45 private static final String PREFS_USE_LOCATION = "use_location";
48 private static final String GSETTINGS_PROVIDER = "com.google.settings";
49 private static final String LOCATION_CATEGORY = "location_category";
50 private static final String BACKUP_CATEGORY = "backup_category";
51 private static final String USE_LOCATION = "use_location";
52 private static final String BACKUP_SETTINGS = "backup_settings";
53 private static final String AUTO_RESTORE = "auto_restore";
54 private static final String KEY_DONE_USE_LOCATION = "doneLocation";
55 private CheckBoxPreference mUseLocation;
56 private CheckBoxPreference mBackup;
57 private CheckBoxPreference mAutoRestore;
58 private boolean mOkClicked;
59 private Dialog mConfirmDialog;
61 private static final int DIALOG_USE_LOCATION = 1;
62 private static final int DIALOG_ERASE_BACKUP = 2;
63 private int mDialogType;
66 protected void onCreate(Bundle savedInstanceState) {
67 super.onCreate(savedInstanceState);
68 addPreferencesFromResource(R.xml.privacy_settings);
69 final PreferenceScreen screen = getPreferenceScreen();
71 mUseLocation = (CheckBoxPreference) screen.findPreference(USE_LOCATION);
72 mBackup = (CheckBoxPreference) screen.findPreference(BACKUP_SETTINGS);
73 mAutoRestore = (CheckBoxPreference) screen.findPreference(AUTO_RESTORE);
76 if (getPackageManager().resolveContentProvider(GSETTINGS_PROVIDER, 0) == null) {
77 screen.removePreference(findPreference(LOCATION_CATEGORY));
78 screen.removePreference(findPreference(BACKUP_CATEGORY));
82 boolean doneUseLocation = savedInstanceState == null
83 ? false : savedInstanceState.getBoolean(KEY_DONE_USE_LOCATION, true);
84 if (!doneUseLocation && (getIntent().getBooleanExtra("SHOW_USE_LOCATION", false)
85 || savedInstanceState != null)) {
86 showUseLocationDialog(true);
91 public void onStop() {
92 if (mConfirmDialog != null && mConfirmDialog.isShowing()) {
93 mConfirmDialog.dismiss();
95 mConfirmDialog = null;
101 public void onSaveInstanceState(Bundle icicle) {
102 if (mConfirmDialog != null && mConfirmDialog.isShowing()
103 && mDialogType == DIALOG_USE_LOCATION) {
104 icicle.putBoolean(KEY_DONE_USE_LOCATION, false);
106 super.onSaveInstanceState(icicle);
110 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
111 Preference preference) {
112 if (preference == mUseLocation) {
113 //normally called on the toggle click
114 if (mUseLocation.isChecked()) {
115 showUseLocationDialog(false);
119 } else if (preference == mBackup) {
120 if (!mBackup.isChecked()) {
121 showEraseBackupDialog();
123 setBackupEnabled(true);
125 } else if (preference == mAutoRestore) {
126 IBackupManager bm = IBackupManager.Stub.asInterface(
127 ServiceManager.getService(Context.BACKUP_SERVICE));
129 // TODO: disable via the backup manager interface
130 boolean curState = mAutoRestore.isChecked();
132 bm.setAutoRestore(curState);
133 } catch (RemoteException e) {
134 mAutoRestore.setChecked(!curState);
142 private void showUseLocationDialog(boolean force) {
143 // Show a warning to the user that location data will be shared
146 mUseLocation.setChecked(true);
149 if (hasAgreedToUseLocation()) {
154 mDialogType = DIALOG_USE_LOCATION;
155 CharSequence msg = getResources().getText(R.string.use_location_warning_message);
156 mConfirmDialog = new AlertDialog.Builder(this).setMessage(msg)
157 .setTitle(R.string.use_location_title)
158 .setIcon(android.R.drawable.ic_dialog_alert)
159 .setPositiveButton(R.string.agree, this)
160 .setNegativeButton(R.string.disagree, this)
162 ((TextView)mConfirmDialog.findViewById(android.R.id.message))
163 .setMovementMethod(LinkMovementMethod.getInstance());
164 mConfirmDialog.setOnDismissListener(this);
167 private void showEraseBackupDialog() {
168 // Show a warning to the user that location data will be shared
170 mBackup.setChecked(true);
172 mDialogType = DIALOG_ERASE_BACKUP;
173 CharSequence msg = getResources().getText(R.string.backup_erase_dialog_message);
174 mConfirmDialog = new AlertDialog.Builder(this).setMessage(msg)
175 .setTitle(R.string.backup_erase_dialog_title)
176 .setIcon(android.R.drawable.ic_dialog_alert)
177 .setPositiveButton(android.R.string.ok, this)
178 .setNegativeButton(android.R.string.cancel, this)
180 mConfirmDialog.setOnDismissListener(this);
184 * Creates toggles for each available location provider
186 private void updateToggles() {
187 ContentResolver res = getContentResolver();
188 mUseLocation.setChecked(Settings.Secure.getInt(res,
189 Settings.Secure.USE_LOCATION_FOR_SERVICES, 2) == 1);
191 final boolean backupEnabled = Settings.Secure.getInt(res,
192 Settings.Secure.BACKUP_ENABLED, 0) == 1;
193 mBackup.setChecked(backupEnabled);
195 mAutoRestore.setChecked(Settings.Secure.getInt(res,
196 Settings.Secure.BACKUP_AUTO_RESTORE, 0) == 1);
197 mAutoRestore.setEnabled(backupEnabled);
200 private void updateUseLocation() {
201 boolean use = mUseLocation.isChecked();
202 Settings.Secure.putInt(getContentResolver(),
203 Settings.Secure.USE_LOCATION_FOR_SERVICES, use ? 1 : 0);
206 public void onClick(DialogInterface dialog, int which) {
207 if (which == DialogInterface.BUTTON_POSITIVE) {
210 if (mDialogType == DIALOG_USE_LOCATION) {
211 setAgreedToUseLocation(true);
212 } else if (mDialogType == DIALOG_ERASE_BACKUP) {
213 setBackupEnabled(false);
216 if (mDialogType == DIALOG_USE_LOCATION) {
218 mUseLocation.setChecked(false);
219 } else if (mDialogType == DIALOG_ERASE_BACKUP) {
220 mBackup.setChecked(true);
221 mAutoRestore.setEnabled(true);
228 public void onDismiss(DialogInterface dialog) {
229 // Assuming that onClick gets called first
231 if (mDialogType == DIALOG_USE_LOCATION) {
232 mUseLocation.setChecked(false);
238 * Checks if the user has agreed to the dialog in the past.
240 private boolean hasAgreedToUseLocation() {
241 SharedPreferences sp = getSharedPreferences(PREFS_NAME, 0);
245 return sp.getBoolean(PREFS_USE_LOCATION, false);
249 * Notes that the user has agreed to the dialog and won't need to be prompted in the
252 private void setAgreedToUseLocation(boolean agreed) {
254 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
255 SharedPreferences.Editor editor = settings.edit();
256 editor.putBoolean(PREFS_USE_LOCATION, true);
262 * Informs the BackupManager of a change in backup state - if backup is disabled,
263 * the data on the server will be erased.
264 * @param enable whether to enable backup
266 private void setBackupEnabled(boolean enable) {
267 IBackupManager bm = IBackupManager.Stub.asInterface(
268 ServiceManager.getService(Context.BACKUP_SERVICE));
271 bm.setBackupEnabled(enable);
272 } catch (RemoteException e) {
273 mBackup.setChecked(!enable);
274 mAutoRestore.setEnabled(!enable);
278 mBackup.setChecked(enable);
279 mAutoRestore.setEnabled(enable);