1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.backup.IBackupManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.provider.Settings;
/**
* Gesture lock pattern settings.
*/
public class PrivacySettings extends SettingsPreferenceFragment implements
DialogInterface.OnClickListener {
// Vendor specific
private static final String GSETTINGS_PROVIDER = "com.google.settings";
private static final String BACKUP_CATEGORY = "backup_category";
private static final String BACKUP_DATA = "backup_data";
private static final String AUTO_RESTORE = "auto_restore";
private CheckBoxPreference mBackup;
private CheckBoxPreference mAutoRestore;
private Dialog mConfirmDialog;
private static final int DIALOG_ERASE_BACKUP = 2;
private int mDialogType;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.privacy_settings);
final PreferenceScreen screen = getPreferenceScreen();
mBackup = (CheckBoxPreference) screen.findPreference(BACKUP_DATA);
mAutoRestore = (CheckBoxPreference) screen.findPreference(AUTO_RESTORE);
// Vendor specific
if (getActivity().getPackageManager().
resolveContentProvider(GSETTINGS_PROVIDER, 0) == null) {
screen.removePreference(findPreference(BACKUP_CATEGORY));
}
updateToggles();
}
@Override
public void onStop() {
if (mConfirmDialog != null && mConfirmDialog.isShowing()) {
mConfirmDialog.dismiss();
}
mConfirmDialog = null;
mDialogType = 0;
super.onStop();
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
if (preference == mBackup) {
if (!mBackup.isChecked()) {
showEraseBackupDialog();
} else {
setBackupEnabled(true);
}
} else if (preference == mAutoRestore) {
IBackupManager bm = IBackupManager.Stub.asInterface(
ServiceManager.getService(Context.BACKUP_SERVICE));
if (bm != null) {
// TODO: disable via the backup manager interface
boolean curState = mAutoRestore.isChecked();
try {
bm.setAutoRestore(curState);
} catch (RemoteException e) {
mAutoRestore.setChecked(!curState);
}
}
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
private void showEraseBackupDialog() {
mBackup.setChecked(true);
mDialogType = DIALOG_ERASE_BACKUP;
CharSequence msg = getResources().getText(R.string.backup_erase_dialog_message);
// TODO: DialogFragment?
mConfirmDialog = new AlertDialog.Builder(getActivity()).setMessage(msg)
.setTitle(R.string.backup_erase_dialog_title)
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.ok, this)
.setNegativeButton(android.R.string.cancel, this)
.show();
}
/*
* Creates toggles for each available location provider
*/
private void updateToggles() {
ContentResolver res = getContentResolver();
final boolean backupEnabled = Settings.Secure.getInt(res,
Settings.Secure.BACKUP_ENABLED, 0) == 1;
mBackup.setChecked(backupEnabled);
mAutoRestore.setChecked(Settings.Secure.getInt(res,
Settings.Secure.BACKUP_AUTO_RESTORE, 1) == 1);
mAutoRestore.setEnabled(backupEnabled);
}
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
//updateProviders();
if (mDialogType == DIALOG_ERASE_BACKUP) {
setBackupEnabled(false);
}
} else {
if (mDialogType == DIALOG_ERASE_BACKUP) {
mBackup.setChecked(true);
mAutoRestore.setEnabled(true);
}
}
mDialogType = 0;
}
/**
* Informs the BackupManager of a change in backup state - if backup is disabled,
* the data on the server will be erased.
* @param enable whether to enable backup
*/
private void setBackupEnabled(boolean enable) {
IBackupManager bm = IBackupManager.Stub.asInterface(
ServiceManager.getService(Context.BACKUP_SERVICE));
if (bm != null) {
try {
bm.setBackupEnabled(enable);
} catch (RemoteException e) {
mBackup.setChecked(!enable);
mAutoRestore.setEnabled(!enable);
return;
}
}
mBackup.setChecked(enable);
mAutoRestore.setEnabled(enable);
}
}
|