1e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani/*
2e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani * Copyright (C) 2009 The Android Open Source Project
3e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani *
4e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani * Licensed under the Apache License, Version 2.0 (the "License");
5e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani * you may not use this file except in compliance with the License.
6e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani * You may obtain a copy of the License at
7e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani *
8e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani *      http://www.apache.org/licenses/LICENSE-2.0
9e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani *
10e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani * Unless required by applicable law or agreed to in writing, software
11e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani * distributed under the License is distributed on an "AS IS" BASIS,
12e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani * See the License for the specific language governing permissions and
14e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani * limitations under the License.
15e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani */
16e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani
17e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasanipackage com.android.settings;
18e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani
19e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasaniimport android.app.AlertDialog;
20e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasaniimport android.app.Dialog;
21616c3ee12a65696f43e62931631ba092884ad3fdChristopher Tateimport android.app.backup.IBackupManager;
22e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasaniimport android.content.ContentResolver;
23e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasaniimport android.content.Context;
24e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasaniimport android.content.DialogInterface;
25df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tateimport android.content.Intent;
26e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasaniimport android.os.Bundle;
27e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasaniimport android.os.RemoteException;
28e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasaniimport android.os.ServiceManager;
29e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasaniimport android.preference.CheckBoxPreference;
30e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasaniimport android.preference.Preference;
31e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasaniimport android.preference.PreferenceScreen;
32e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasaniimport android.provider.Settings;
33e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani
34e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani/**
35e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani * Gesture lock pattern settings.
36e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani */
37d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasanipublic class PrivacySettings extends SettingsPreferenceFragment implements
3830e4d965121ae7f049f090862a5c246515a0bf34Bjorn Bringert        DialogInterface.OnClickListener {
39e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani
40e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    // Vendor specific
41ecfeaffdfa19bea836f24fd399376825c377eaa9Bjorn Bringert    private static final String GSETTINGS_PROVIDER = "com.google.settings";
42a5a67306a4c7a066d97802333a473be94661e091Christopher Tate    private static final String BACKUP_CATEGORY = "backup_category";
43aa1f8aeeea1c27fe88270cc8b5df970d9f292385Kenny Root    private static final String BACKUP_DATA = "backup_data";
44a5a67306a4c7a066d97802333a473be94661e091Christopher Tate    private static final String AUTO_RESTORE = "auto_restore";
4558e62926b9ceca72275b657b2fa7b9e54fc1a931Christopher Tate    private static final String CONFIGURE_ACCOUNT = "configure_account";
46df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate    private IBackupManager mBackupManager;
47e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    private CheckBoxPreference mBackup;
48a5a67306a4c7a066d97802333a473be94661e091Christopher Tate    private CheckBoxPreference mAutoRestore;
49e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    private Dialog mConfirmDialog;
50df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate    private PreferenceScreen mConfigure;
51e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani
52e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    private static final int DIALOG_ERASE_BACKUP = 2;
5370bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio    private int mDialogType;
54e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani
55e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    @Override
56d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani    public void onCreate(Bundle savedInstanceState) {
57e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        super.onCreate(savedInstanceState);
58e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        addPreferencesFromResource(R.xml.privacy_settings);
59a5a67306a4c7a066d97802333a473be94661e091Christopher Tate        final PreferenceScreen screen = getPreferenceScreen();
60e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani
61df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate        mBackupManager = IBackupManager.Stub.asInterface(
62df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate                ServiceManager.getService(Context.BACKUP_SERVICE));
63df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate
64aa1f8aeeea1c27fe88270cc8b5df970d9f292385Kenny Root        mBackup = (CheckBoxPreference) screen.findPreference(BACKUP_DATA);
65a5a67306a4c7a066d97802333a473be94661e091Christopher Tate        mAutoRestore = (CheckBoxPreference) screen.findPreference(AUTO_RESTORE);
6658e62926b9ceca72275b657b2fa7b9e54fc1a931Christopher Tate        mConfigure = (PreferenceScreen) screen.findPreference(CONFIGURE_ACCOUNT);
67e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani
68e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        // Vendor specific
69d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani        if (getActivity().getPackageManager().
70d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani                resolveContentProvider(GSETTINGS_PROVIDER, 0) == null) {
71a5a67306a4c7a066d97802333a473be94661e091Christopher Tate            screen.removePreference(findPreference(BACKUP_CATEGORY));
72e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        }
73e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        updateToggles();
74e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    }
75e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani
76e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    @Override
7770bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio    public void onResume() {
7870bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio        super.onResume();
7970bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio
8070bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio        // Refresh UI
8170bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio        updateToggles();
8270bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio    }
8370bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio
8470bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio    @Override
85e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    public void onStop() {
86e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        if (mConfirmDialog != null && mConfirmDialog.isShowing()) {
87e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani            mConfirmDialog.dismiss();
88e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        }
89e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        mConfirmDialog = null;
90e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        mDialogType = 0;
91e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        super.onStop();
92e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    }
93e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani
94e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    @Override
95e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
96e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani            Preference preference) {
9730e4d965121ae7f049f090862a5c246515a0bf34Bjorn Bringert        if (preference == mBackup) {
98e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani            if (!mBackup.isChecked()) {
99e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani                showEraseBackupDialog();
100e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani            } else {
101e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani                setBackupEnabled(true);
102e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani            }
103a5a67306a4c7a066d97802333a473be94661e091Christopher Tate        } else if (preference == mAutoRestore) {
104fac0ea781f0c99a446ab6588006b2ac0330cb774Christopher Tate            boolean curState = mAutoRestore.isChecked();
105fac0ea781f0c99a446ab6588006b2ac0330cb774Christopher Tate            try {
106fac0ea781f0c99a446ab6588006b2ac0330cb774Christopher Tate                mBackupManager.setAutoRestore(curState);
107fac0ea781f0c99a446ab6588006b2ac0330cb774Christopher Tate            } catch (RemoteException e) {
108fac0ea781f0c99a446ab6588006b2ac0330cb774Christopher Tate                mAutoRestore.setChecked(!curState);
109a5a67306a4c7a066d97802333a473be94661e091Christopher Tate            }
110e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        }
111b14e1e04939ca610fd5f2439f879265450b0a6cdAmith Yamasani        return super.onPreferenceTreeClick(preferenceScreen, preference);
112e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    }
113e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani
114e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    private void showEraseBackupDialog() {
115e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        mBackup.setChecked(true);
116e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani
117e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        mDialogType = DIALOG_ERASE_BACKUP;
118e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        CharSequence msg = getResources().getText(R.string.backup_erase_dialog_message);
119d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani        // TODO: DialogFragment?
120d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani        mConfirmDialog = new AlertDialog.Builder(getActivity()).setMessage(msg)
121e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani                .setTitle(R.string.backup_erase_dialog_title)
122e7c53b1c9e48c6f997c47a0ca9a2f190ecbbf586Björn Lundén                .setIconAttribute(android.R.attr.alertDialogIcon)
123e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani                .setPositiveButton(android.R.string.ok, this)
124e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani                .setNegativeButton(android.R.string.cancel, this)
125e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani                .show();
126e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    }
127e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani
128e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    /*
129e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani     * Creates toggles for each available location provider
130e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani     */
131e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    private void updateToggles() {
132e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        ContentResolver res = getContentResolver();
133a5a67306a4c7a066d97802333a473be94661e091Christopher Tate
134df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate        boolean backupEnabled = false;
135df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate        Intent configIntent = null;
136df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate        String configSummary = null;
137df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate        try {
138df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate            backupEnabled = mBackupManager.isBackupEnabled();
139df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate            String transport = mBackupManager.getCurrentTransport();
140df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate            configIntent = mBackupManager.getConfigurationIntent(transport);
141df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate            configSummary = mBackupManager.getDestinationString(transport);
142df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate        } catch (RemoteException e) {
143df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate            // leave it 'false' and disable the UI; there's no backup manager
144df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate            mBackup.setEnabled(false);
145df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate        }
146a5a67306a4c7a066d97802333a473be94661e091Christopher Tate        mBackup.setChecked(backupEnabled);
147a5a67306a4c7a066d97802333a473be94661e091Christopher Tate
148a5a67306a4c7a066d97802333a473be94661e091Christopher Tate        mAutoRestore.setChecked(Settings.Secure.getInt(res,
149c99cffbffcf38f0e6bf10c9ff541b1bb5180316eChristopher Tate                Settings.Secure.BACKUP_AUTO_RESTORE, 1) == 1);
150a5a67306a4c7a066d97802333a473be94661e091Christopher Tate        mAutoRestore.setEnabled(backupEnabled);
151df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate
15270bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio        final boolean configureEnabled = (configIntent != null) && backupEnabled;
15370bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio        mConfigure.setEnabled(configureEnabled);
154df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate        mConfigure.setIntent(configIntent);
15570bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio        setConfigureSummary(configSummary);
1564f33878f208e729cf96b708c57686555d2493129Christopher Tate}
15770bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio
15870bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio    private void setConfigureSummary(String summary) {
15970bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio        if (summary != null) {
16070bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio            mConfigure.setSummary(summary);
161df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate        } else {
16258e62926b9ceca72275b657b2fa7b9e54fc1a931Christopher Tate            mConfigure.setSummary(R.string.backup_configure_account_default_summary);
163df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate        }
164e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    }
165e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani
16670bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio    private void updateConfigureSummary() {
16770bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio        try {
16870bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio            String transport = mBackupManager.getCurrentTransport();
16970bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio            String summary = mBackupManager.getDestinationString(transport);
17070bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio            setConfigureSummary(summary);
17170bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio        } catch (RemoteException e) {
17270bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio            // Not much we can do here
17370bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio        }
17470bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio    }
17570bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio
176e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    public void onClick(DialogInterface dialog, int which) {
177e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        if (which == DialogInterface.BUTTON_POSITIVE) {
178e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani            //updateProviders();
17930e4d965121ae7f049f090862a5c246515a0bf34Bjorn Bringert            if (mDialogType == DIALOG_ERASE_BACKUP) {
180e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani                setBackupEnabled(false);
18170bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio                updateConfigureSummary();
182e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani            }
183e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        }
184e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        mDialogType = 0;
185e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    }
186e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani
187e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    /**
188e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani     * Informs the BackupManager of a change in backup state - if backup is disabled,
189e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani     * the data on the server will be erased.
190e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani     * @param enable whether to enable backup
191e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani     */
192e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    private void setBackupEnabled(boolean enable) {
193df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate        if (mBackupManager != null) {
194e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani            try {
195df6a7e55ccffd4a8b17780ee07b9f11988b98aaaChristopher Tate                mBackupManager.setBackupEnabled(enable);
196e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani            } catch (RemoteException e) {
197e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani                mBackup.setChecked(!enable);
198a5a67306a4c7a066d97802333a473be94661e091Christopher Tate                mAutoRestore.setEnabled(!enable);
199e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani                return;
200e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani            }
201e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        }
202e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani        mBackup.setChecked(enable);
203a5a67306a4c7a066d97802333a473be94661e091Christopher Tate        mAutoRestore.setEnabled(enable);
20470bc797a926abdf14bc0d34d9b49a066663fbca7Fabrice Di Meglio        mConfigure.setEnabled(enable);
205e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani    }
206b0b37ae21c172491bc170659b5f429601858ddc1Amith Yamasani
207b0b37ae21c172491bc170659b5f429601858ddc1Amith Yamasani    @Override
208b0b37ae21c172491bc170659b5f429601858ddc1Amith Yamasani    protected int getHelpResource() {
209b0b37ae21c172491bc170659b5f429601858ddc1Amith Yamasani        return R.string.help_url_backup_reset;
210b0b37ae21c172491bc170659b5f429601858ddc1Amith Yamasani    }
211e65c943f974966e21c6f18dc9a95107ea1f07631Amith Yamasani}
212