PrivacySettings.java revision aa1f8aeeea1c27fe88270cc8b5df970d9f292385
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
17package com.android.settings;
18
19import android.app.AlertDialog;
20import android.app.Dialog;
21import android.app.backup.IBackupManager;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.DialogInterface;
25import android.content.SharedPreferences;
26import android.content.pm.PackageManager.NameNotFoundException;
27import android.os.Bundle;
28import android.os.RemoteException;
29import android.os.ServiceManager;
30import android.preference.CheckBoxPreference;
31import android.preference.Preference;
32import android.preference.PreferenceActivity;
33import android.preference.PreferenceScreen;
34import android.provider.Settings;
35import android.text.method.LinkMovementMethod;
36import android.widget.TextView;
37
38/**
39 * Gesture lock pattern settings.
40 */
41public class PrivacySettings extends PreferenceActivity implements
42        DialogInterface.OnClickListener {
43
44    // Vendor specific
45    private static final String GSETTINGS_PROVIDER = "com.google.settings";
46    private static final String BACKUP_CATEGORY = "backup_category";
47    private static final String BACKUP_DATA = "backup_data";
48    private static final String AUTO_RESTORE = "auto_restore";
49    private CheckBoxPreference mBackup;
50    private CheckBoxPreference mAutoRestore;
51    private Dialog mConfirmDialog;
52
53    private static final int DIALOG_ERASE_BACKUP = 2;
54    private int     mDialogType;
55
56    @Override
57    protected void onCreate(Bundle savedInstanceState) {
58        super.onCreate(savedInstanceState);
59        addPreferencesFromResource(R.xml.privacy_settings);
60        final PreferenceScreen screen = getPreferenceScreen();
61
62        mBackup = (CheckBoxPreference) screen.findPreference(BACKUP_DATA);
63        mAutoRestore = (CheckBoxPreference) screen.findPreference(AUTO_RESTORE);
64
65        // Vendor specific
66        if (getPackageManager().resolveContentProvider(GSETTINGS_PROVIDER, 0) == null) {
67            screen.removePreference(findPreference(BACKUP_CATEGORY));
68        }
69        updateToggles();
70    }
71
72    @Override
73    public void onStop() {
74        if (mConfirmDialog != null && mConfirmDialog.isShowing()) {
75            mConfirmDialog.dismiss();
76        }
77        mConfirmDialog = null;
78        mDialogType = 0;
79        super.onStop();
80    }
81
82    @Override
83    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
84            Preference preference) {
85        if (preference == mBackup) {
86            if (!mBackup.isChecked()) {
87                showEraseBackupDialog();
88            } else {
89                setBackupEnabled(true);
90            }
91        } else if (preference == mAutoRestore) {
92            IBackupManager bm = IBackupManager.Stub.asInterface(
93                    ServiceManager.getService(Context.BACKUP_SERVICE));
94            if (bm != null) {
95                // TODO: disable via the backup manager interface
96                boolean curState = mAutoRestore.isChecked();
97                try {
98                    bm.setAutoRestore(curState);
99                } catch (RemoteException e) {
100                    mAutoRestore.setChecked(!curState);
101                }
102            }
103        }
104
105        return false;
106    }
107
108    private void showEraseBackupDialog() {
109        mBackup.setChecked(true);
110
111        mDialogType = DIALOG_ERASE_BACKUP;
112        CharSequence msg = getResources().getText(R.string.backup_erase_dialog_message);
113        mConfirmDialog = new AlertDialog.Builder(this).setMessage(msg)
114                .setTitle(R.string.backup_erase_dialog_title)
115                .setIcon(android.R.drawable.ic_dialog_alert)
116                .setPositiveButton(android.R.string.ok, this)
117                .setNegativeButton(android.R.string.cancel, this)
118                .show();
119    }
120
121    /*
122     * Creates toggles for each available location provider
123     */
124    private void updateToggles() {
125        ContentResolver res = getContentResolver();
126
127        final boolean backupEnabled = Settings.Secure.getInt(res,
128                Settings.Secure.BACKUP_ENABLED, 0) == 1;
129        mBackup.setChecked(backupEnabled);
130
131        mAutoRestore.setChecked(Settings.Secure.getInt(res,
132                Settings.Secure.BACKUP_AUTO_RESTORE, 1) == 1);
133        mAutoRestore.setEnabled(backupEnabled);
134    }
135
136    public void onClick(DialogInterface dialog, int which) {
137        if (which == DialogInterface.BUTTON_POSITIVE) {
138            //updateProviders();
139            if (mDialogType == DIALOG_ERASE_BACKUP) {
140                setBackupEnabled(false);
141            }
142        } else {
143            if (mDialogType == DIALOG_ERASE_BACKUP) {
144                mBackup.setChecked(true);
145                mAutoRestore.setEnabled(true);
146            }
147        }
148        mDialogType = 0;
149    }
150
151    /**
152     * Informs the BackupManager of a change in backup state - if backup is disabled,
153     * the data on the server will be erased.
154     * @param enable whether to enable backup
155     */
156    private void setBackupEnabled(boolean enable) {
157        IBackupManager bm = IBackupManager.Stub.asInterface(
158                ServiceManager.getService(Context.BACKUP_SERVICE));
159        if (bm != null) {
160            try {
161                bm.setBackupEnabled(enable);
162            } catch (RemoteException e) {
163                mBackup.setChecked(!enable);
164                mAutoRestore.setEnabled(!enable);
165                return;
166            }
167        }
168        mBackup.setChecked(enable);
169        mAutoRestore.setEnabled(enable);
170    }
171}
172