OSDN Git Service

Add 'restore on app install?" to Privacy settings UI
[android-x86/packages-apps-Settings.git] / src / com / android / settings / PrivacySettings.java
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.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;
37
38 /**
39  * Gesture lock pattern settings.
40  */
41 public class PrivacySettings extends PreferenceActivity implements
42         DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
43
44     private static final String PREFS_NAME = "location_prefs";
45     private static final String PREFS_USE_LOCATION = "use_location";
46
47     // Vendor specific
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;
60
61     private static final int DIALOG_USE_LOCATION = 1;
62     private static final int DIALOG_ERASE_BACKUP = 2;
63     private int     mDialogType;
64
65     @Override
66     protected void onCreate(Bundle savedInstanceState) {
67         super.onCreate(savedInstanceState);
68         addPreferencesFromResource(R.xml.privacy_settings);
69         final PreferenceScreen screen = getPreferenceScreen();
70
71         mUseLocation = (CheckBoxPreference) screen.findPreference(USE_LOCATION);
72         mBackup = (CheckBoxPreference) screen.findPreference(BACKUP_SETTINGS);
73         mAutoRestore = (CheckBoxPreference) screen.findPreference(AUTO_RESTORE);
74
75         // Vendor specific
76         if (getPackageManager().resolveContentProvider(GSETTINGS_PROVIDER, 0) == null) {
77             screen.removePreference(findPreference(LOCATION_CATEGORY));
78             screen.removePreference(findPreference(BACKUP_CATEGORY));
79         }
80         updateToggles();
81
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);
87         }
88     }
89
90     @Override
91     public void onStop() {
92         if (mConfirmDialog != null && mConfirmDialog.isShowing()) {
93             mConfirmDialog.dismiss();
94         }
95         mConfirmDialog = null;
96         mDialogType = 0;
97         super.onStop();
98     }
99
100     @Override
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);
105         }
106         super.onSaveInstanceState(icicle);
107     }
108
109     @Override
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);
116             } else {
117                 updateUseLocation();
118             }
119         } else if (preference == mBackup) {
120             if (!mBackup.isChecked()) {
121                 showEraseBackupDialog();
122             } else {
123                 setBackupEnabled(true);
124             }
125         } else if (preference == mAutoRestore) {
126             IBackupManager bm = IBackupManager.Stub.asInterface(
127                     ServiceManager.getService(Context.BACKUP_SERVICE));
128             if (bm != null) {
129                 // TODO: disable via the backup manager interface
130                 boolean curState = mAutoRestore.isChecked();
131                 try {
132                     bm.setAutoRestore(curState);
133                 } catch (RemoteException e) {
134                     mAutoRestore.setChecked(!curState);
135                 }
136             }
137         }
138
139         return false;
140     }
141
142     private void showUseLocationDialog(boolean force) {
143         // Show a warning to the user that location data will be shared
144         mOkClicked = false;
145         if (force) {
146             mUseLocation.setChecked(true);
147         }
148
149         if (hasAgreedToUseLocation()) {
150             updateUseLocation();
151             return;
152         }
153
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)
161                 .show();
162         ((TextView)mConfirmDialog.findViewById(android.R.id.message))
163                 .setMovementMethod(LinkMovementMethod.getInstance());
164         mConfirmDialog.setOnDismissListener(this);
165     }
166
167     private void showEraseBackupDialog() {
168         // Show a warning to the user that location data will be shared
169         mOkClicked = false;
170         mBackup.setChecked(true);
171
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)
179                 .show();
180         mConfirmDialog.setOnDismissListener(this);
181     }
182
183     /*
184      * Creates toggles for each available location provider
185      */
186     private void updateToggles() {
187         ContentResolver res = getContentResolver();
188         mUseLocation.setChecked(Settings.Secure.getInt(res,
189                 Settings.Secure.USE_LOCATION_FOR_SERVICES, 2) == 1);
190
191         final boolean backupEnabled = Settings.Secure.getInt(res,
192                 Settings.Secure.BACKUP_ENABLED, 0) == 1;
193         mBackup.setChecked(backupEnabled);
194
195         mAutoRestore.setChecked(Settings.Secure.getInt(res,
196                 Settings.Secure.BACKUP_AUTO_RESTORE, 0) == 1);
197         mAutoRestore.setEnabled(backupEnabled);
198     }
199
200     private void updateUseLocation() {
201         boolean use = mUseLocation.isChecked();
202         Settings.Secure.putInt(getContentResolver(),
203                 Settings.Secure.USE_LOCATION_FOR_SERVICES, use ? 1 : 0);
204     }
205
206     public void onClick(DialogInterface dialog, int which) {
207         if (which == DialogInterface.BUTTON_POSITIVE) {
208             //updateProviders();
209             mOkClicked = true;
210             if (mDialogType == DIALOG_USE_LOCATION) {
211                 setAgreedToUseLocation(true);
212             } else if (mDialogType == DIALOG_ERASE_BACKUP) {
213                 setBackupEnabled(false);
214             }
215         } else {
216             if (mDialogType == DIALOG_USE_LOCATION) {
217                 // Reset the toggle
218                 mUseLocation.setChecked(false);
219             } else if (mDialogType == DIALOG_ERASE_BACKUP) {
220                 mBackup.setChecked(true);
221                 mAutoRestore.setEnabled(true);
222             }
223         }
224         updateUseLocation();
225         mDialogType = 0;
226     }
227
228     public void onDismiss(DialogInterface dialog) {
229         // Assuming that onClick gets called first
230         if (!mOkClicked) {
231             if (mDialogType == DIALOG_USE_LOCATION) {
232                 mUseLocation.setChecked(false);
233             }
234         }
235     }
236
237     /**
238      * Checks if the user has agreed to the dialog in the past.
239      */
240     private boolean hasAgreedToUseLocation() {
241         SharedPreferences sp = getSharedPreferences(PREFS_NAME, 0);
242         if (sp == null) {
243             return false;
244         }
245         return sp.getBoolean(PREFS_USE_LOCATION, false);
246     }
247
248     /**
249      * Notes that the user has agreed to the dialog and won't need to be prompted in the
250      * future.
251      */
252     private void setAgreedToUseLocation(boolean agreed) {
253         if (agreed) {
254             SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
255             SharedPreferences.Editor editor = settings.edit();
256             editor.putBoolean(PREFS_USE_LOCATION, true);
257             editor.commit();
258         }
259     }
260
261     /**
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
265      */
266     private void setBackupEnabled(boolean enable) {
267         IBackupManager bm = IBackupManager.Stub.asInterface(
268                 ServiceManager.getService(Context.BACKUP_SERVICE));
269         if (bm != null) {
270             try {
271                 bm.setBackupEnabled(enable);
272             } catch (RemoteException e) {
273                 mBackup.setChecked(!enable);
274                 mAutoRestore.setEnabled(!enable);
275                 return;
276             }
277         }
278         mBackup.setChecked(enable);
279         mAutoRestore.setEnabled(enable);
280     }
281 }