1 package com.android.settings.backup;
3 import android.app.AlertDialog;
4 import android.app.Dialog;
5 import android.app.backup.IBackupManager;
6 import android.content.Context;
7 import android.content.DialogInterface;
8 import android.os.Bundle;
9 import android.os.RemoteException;
10 import android.os.ServiceManager;
11 import android.provider.Settings;
12 import android.support.v7.preference.Preference;
13 import android.support.v7.preference.PreferenceScreen;
14 import android.support.v7.preference.PreferenceViewHolder;
15 import android.util.Log;
16 import android.view.View;
17 import android.widget.TextView;
19 import com.android.internal.logging.MetricsLogger;
20 import com.android.settings.R;
21 import com.android.settings.SettingsActivity;
22 import com.android.settings.SettingsPreferenceFragment;
23 import com.android.settings.widget.SwitchBar;
24 import com.android.settings.widget.ToggleSwitch;
27 * Fragment to display a bunch of text about backup and restore, and allow the user to enable/
30 public class ToggleBackupSettingFragment extends SettingsPreferenceFragment
31 implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
32 private static final String TAG = "ToggleBackupSettingFragment";
34 private static final String BACKUP_TOGGLE = "toggle_backup";
36 // System setting that governs whether the user is eligible for full app-data backup,
37 // based on whether they have been presented with the details of what that backup entails
38 // (usually surfaced somewhere like device setup)
39 private static final String USER_FULL_DATA_BACKUP_AWARE = "user_full_data_backup_aware";
41 private IBackupManager mBackupManager;
43 protected SwitchBar mSwitchBar;
44 protected ToggleSwitch mToggleSwitch;
46 private Preference mSummaryPreference;
48 private Dialog mConfirmDialog;
50 private boolean mWaitingForConfirmationDialog = false;
53 public void onCreate(Bundle savedInstanceState) {
54 super.onCreate(savedInstanceState);
56 mBackupManager = IBackupManager.Stub.asInterface(
57 ServiceManager.getService(Context.BACKUP_SERVICE));
59 PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(
61 setPreferenceScreen(preferenceScreen);
62 mSummaryPreference = new Preference(getPrefContext()) {
64 public void onBindViewHolder(PreferenceViewHolder view) {
65 super.onBindViewHolder(view);
66 final TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
67 summaryView.setText(getSummary());
70 mSummaryPreference.setPersistent(false);
71 mSummaryPreference.setLayoutResource(R.layout.text_description_preference);
72 preferenceScreen.addPreference(mSummaryPreference);
76 public void onViewCreated(View view, Bundle savedInstanceState) {
77 super.onViewCreated(view, savedInstanceState);
79 SettingsActivity activity = (SettingsActivity) getActivity();
80 mSwitchBar = activity.getSwitchBar();
81 mToggleSwitch = mSwitchBar.getSwitch();
84 // If the user has not seen legal text for full data backup (if they OTA from L to M) then
85 // full data backup will be off and here we want to show the old summary here that does
86 // not mention full data backup
87 if (Settings.Secure.getInt(getContentResolver(), USER_FULL_DATA_BACKUP_AWARE, 0) != 0) {
88 mSummaryPreference.setSummary(R.string.fullbackup_data_summary);
90 mSummaryPreference.setSummary(R.string.backup_data_summary);
93 boolean backupEnabled = mBackupManager == null ?
94 false : mBackupManager.isBackupEnabled();
95 mSwitchBar.setCheckedInternal(backupEnabled);
96 } catch (RemoteException e) {
97 // The world is aflame, turn it off.
98 mSwitchBar.setEnabled(false);
100 getActivity().setTitle(R.string.backup_data_title);
104 public void onDestroyView() {
105 super.onDestroyView();
107 mToggleSwitch.setOnBeforeCheckedChangeListener(null);
112 public void onActivityCreated(Bundle savedInstanceState) {
113 super.onActivityCreated(savedInstanceState);
115 // Set up toggle listener. We need this b/c we have to intercept the toggle event in order
116 // to pop up the dialogue.
117 mToggleSwitch.setOnBeforeCheckedChangeListener(
118 new ToggleSwitch.OnBeforeCheckedChangeListener() {
120 public boolean onBeforeCheckedChanged(
121 ToggleSwitch toggleSwitch, boolean checked) {
123 // Don't change Switch status until user makes choice in dialog
124 // so return true here.
125 showEraseBackupDialog();
128 setBackupEnabled(true);
129 mSwitchBar.setCheckedInternal(true);
137 /** Get rid of the dialog if it's still showing. */
139 public void onStop() {
140 if (mConfirmDialog != null && mConfirmDialog.isShowing()) {
141 mConfirmDialog.dismiss();
143 mConfirmDialog = null;
148 public void onClick(DialogInterface dialog, int which) {
149 // Accept turning off backup
150 if (which == DialogInterface.BUTTON_POSITIVE) {
151 mWaitingForConfirmationDialog = false;
152 setBackupEnabled(false);
153 mSwitchBar.setCheckedInternal(false);
154 } else if (which == DialogInterface.BUTTON_NEGATIVE) {
155 // Reject turning off backup
156 mWaitingForConfirmationDialog = false;
157 setBackupEnabled(true);
158 mSwitchBar.setCheckedInternal(true);
163 public void onDismiss(DialogInterface dialog) {
164 if (mWaitingForConfirmationDialog) {
165 // dismiss turning off backup
166 setBackupEnabled(true);
167 mSwitchBar.setCheckedInternal(true);
171 private void showEraseBackupDialog() {
174 // If the user has not seen legal text for full data backup (if they OTA from L to M) then
175 // full data backup will be off and here we want to show the old erase_dialog_message here
176 // that does not mention full data backup
177 if (Settings.Secure.getInt(getContentResolver(), USER_FULL_DATA_BACKUP_AWARE, 0) != 0) {
178 msg = getResources().getText(R.string.fullbackup_erase_dialog_message);
180 msg = getResources().getText(R.string.backup_erase_dialog_message);
183 mWaitingForConfirmationDialog = true;
185 // TODO: DialogFragment?
186 mConfirmDialog = new AlertDialog.Builder(getActivity()).setMessage(msg)
187 .setTitle(R.string.backup_erase_dialog_title)
188 .setPositiveButton(android.R.string.ok, this)
189 .setNegativeButton(android.R.string.cancel, this)
190 .setOnDismissListener(this)
195 protected int getMetricsCategory() {
196 return MetricsLogger.PRIVACY;
200 * Informs the BackupManager of a change in backup state - if backup is disabled,
201 * the data on the server will be erased.
202 * @param enable whether to enable backup
204 private void setBackupEnabled(boolean enable) {
205 if (mBackupManager != null) {
207 mBackupManager.setBackupEnabled(enable);
208 } catch (RemoteException e) {
209 Log.e(TAG, "Error communicating with BackupManager", e);