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