blob: 70ab68ab4d45c824aa9568e4696b75c29f561c28 [file] [log] [blame]
Matthew Williams6ee5a4d2015-05-15 21:14:14 -07001package com.android.settings.backup;
2
Matthew Williams6ee5a4d2015-05-15 21:14:14 -07003import android.app.Dialog;
Jason Monk39b46742015-09-10 15:52:51 -04004import android.app.backup.IBackupManager;
Matthew Williams6ee5a4d2015-05-15 21:14:14 -07005import android.content.Context;
6import android.content.DialogInterface;
7import android.os.Bundle;
8import android.os.RemoteException;
9import android.os.ServiceManager;
Sean Francis-Lyonc44d0ef2015-06-12 17:46:41 -070010import android.provider.Settings;
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070011import android.util.Log;
12import android.view.View;
13import android.widget.TextView;
14
Fan Zhang23f8d592018-08-28 15:11:40 -070015import androidx.appcompat.app.AlertDialog;
16import androidx.preference.Preference;
17import androidx.preference.PreferenceScreen;
18import androidx.preference.PreferenceViewHolder;
19
Tamas Berghammer265d3c22016-06-22 15:34:45 +010020import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070021import com.android.settings.R;
22import com.android.settings.SettingsActivity;
23import com.android.settings.SettingsPreferenceFragment;
24import com.android.settings.widget.SwitchBar;
25import com.android.settings.widget.ToggleSwitch;
26
27/**
28 * Fragment to display a bunch of text about backup and restore, and allow the user to enable/
29 * disable it.
30 */
31public class ToggleBackupSettingFragment extends SettingsPreferenceFragment
Sean Francis-Lyon8452a622015-05-20 18:30:00 -070032 implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070033 private static final String TAG = "ToggleBackupSettingFragment";
34
35 private static final String BACKUP_TOGGLE = "toggle_backup";
36
Sean Francis-Lyonc44d0ef2015-06-12 17:46:41 -070037 // System setting that governs whether the user is eligible for full app-data backup,
38 // based on whether they have been presented with the details of what that backup entails
39 // (usually surfaced somewhere like device setup)
40 private static final String USER_FULL_DATA_BACKUP_AWARE = "user_full_data_backup_aware";
41
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070042 private IBackupManager mBackupManager;
43
44 protected SwitchBar mSwitchBar;
45 protected ToggleSwitch mToggleSwitch;
46
47 private Preference mSummaryPreference;
48
49 private Dialog mConfirmDialog;
50
Sean Francis-Lyon8452a622015-05-20 18:30:00 -070051 private boolean mWaitingForConfirmationDialog = false;
52
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070053 @Override
54 public void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
56
57 mBackupManager = IBackupManager.Stub.asInterface(
58 ServiceManager.getService(Context.BACKUP_SERVICE));
59
60 PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(
61 getActivity());
62 setPreferenceScreen(preferenceScreen);
Jason Monk39b46742015-09-10 15:52:51 -040063 mSummaryPreference = new Preference(getPrefContext()) {
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070064 @Override
Jason Monk39b46742015-09-10 15:52:51 -040065 public void onBindViewHolder(PreferenceViewHolder view) {
66 super.onBindViewHolder(view);
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070067 final TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
68 summaryView.setText(getSummary());
69 }
70 };
71 mSummaryPreference.setPersistent(false);
72 mSummaryPreference.setLayoutResource(R.layout.text_description_preference);
73 preferenceScreen.addPreference(mSummaryPreference);
74 }
75
76 @Override
77 public void onViewCreated(View view, Bundle savedInstanceState) {
78 super.onViewCreated(view, savedInstanceState);
79
80 SettingsActivity activity = (SettingsActivity) getActivity();
81 mSwitchBar = activity.getSwitchBar();
82 mToggleSwitch = mSwitchBar.getSwitch();
83
84 // Set up UI.
Sean Francis-Lyonc44d0ef2015-06-12 17:46:41 -070085 // If the user has not seen legal text for full data backup (if they OTA from L to M) then
86 // full data backup will be off and here we want to show the old summary here that does
87 // not mention full data backup
88 if (Settings.Secure.getInt(getContentResolver(), USER_FULL_DATA_BACKUP_AWARE, 0) != 0) {
89 mSummaryPreference.setSummary(R.string.fullbackup_data_summary);
90 } else {
91 mSummaryPreference.setSummary(R.string.backup_data_summary);
92 }
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070093 try {
94 boolean backupEnabled = mBackupManager == null ?
95 false : mBackupManager.isBackupEnabled();
96 mSwitchBar.setCheckedInternal(backupEnabled);
97 } catch (RemoteException e) {
98 // The world is aflame, turn it off.
99 mSwitchBar.setEnabled(false);
100 }
101 getActivity().setTitle(R.string.backup_data_title);
102 }
103
104 @Override
105 public void onDestroyView() {
106 super.onDestroyView();
107
108 mToggleSwitch.setOnBeforeCheckedChangeListener(null);
109 mSwitchBar.hide();
110 }
111
112 @Override
113 public void onActivityCreated(Bundle savedInstanceState) {
114 super.onActivityCreated(savedInstanceState);
115
116 // Set up toggle listener. We need this b/c we have to intercept the toggle event in order
117 // to pop up the dialogue.
118 mToggleSwitch.setOnBeforeCheckedChangeListener(
119 new ToggleSwitch.OnBeforeCheckedChangeListener() {
120 @Override
121 public boolean onBeforeCheckedChanged(
122 ToggleSwitch toggleSwitch, boolean checked) {
123 if (!checked) {
124 // Don't change Switch status until user makes choice in dialog
Sean Francis-Lyon8452a622015-05-20 18:30:00 -0700125 // so return true here.
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700126 showEraseBackupDialog();
Sean Francis-Lyon8452a622015-05-20 18:30:00 -0700127 return true;
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700128 } else {
129 setBackupEnabled(true);
130 mSwitchBar.setCheckedInternal(true);
131 return true;
132 }
133 }
134 });
135 mSwitchBar.show();
136 }
137
138 /** Get rid of the dialog if it's still showing. */
139 @Override
140 public void onStop() {
141 if (mConfirmDialog != null && mConfirmDialog.isShowing()) {
142 mConfirmDialog.dismiss();
143 }
144 mConfirmDialog = null;
145 super.onStop();
146 }
147
148 @Override
149 public void onClick(DialogInterface dialog, int which) {
150 // Accept turning off backup
151 if (which == DialogInterface.BUTTON_POSITIVE) {
Sean Francis-Lyon8452a622015-05-20 18:30:00 -0700152 mWaitingForConfirmationDialog = false;
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700153 setBackupEnabled(false);
154 mSwitchBar.setCheckedInternal(false);
155 } else if (which == DialogInterface.BUTTON_NEGATIVE) {
156 // Reject turning off backup
Sean Francis-Lyon8452a622015-05-20 18:30:00 -0700157 mWaitingForConfirmationDialog = false;
158 setBackupEnabled(true);
159 mSwitchBar.setCheckedInternal(true);
160 }
161 }
162
163 @Override
164 public void onDismiss(DialogInterface dialog) {
165 if (mWaitingForConfirmationDialog) {
166 // dismiss turning off backup
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700167 setBackupEnabled(true);
168 mSwitchBar.setCheckedInternal(true);
169 }
170 }
171
172 private void showEraseBackupDialog() {
Sean Francis-Lyonc44d0ef2015-06-12 17:46:41 -0700173 CharSequence msg;
174
175 // If the user has not seen legal text for full data backup (if they OTA from L to M) then
176 // full data backup will be off and here we want to show the old erase_dialog_message here
177 // that does not mention full data backup
178 if (Settings.Secure.getInt(getContentResolver(), USER_FULL_DATA_BACKUP_AWARE, 0) != 0) {
179 msg = getResources().getText(R.string.fullbackup_erase_dialog_message);
180 } else {
181 msg = getResources().getText(R.string.backup_erase_dialog_message);
182 }
Sean Francis-Lyon8452a622015-05-20 18:30:00 -0700183
184 mWaitingForConfirmationDialog = true;
185
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700186 // TODO: DialogFragment?
187 mConfirmDialog = new AlertDialog.Builder(getActivity()).setMessage(msg)
188 .setTitle(R.string.backup_erase_dialog_title)
189 .setPositiveButton(android.R.string.ok, this)
190 .setNegativeButton(android.R.string.cancel, this)
Sean Francis-Lyon8452a622015-05-20 18:30:00 -0700191 .setOnDismissListener(this)
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700192 .show();
193 }
194
195 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700196 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500197 return MetricsEvent.PRIVACY;
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700198 }
199
200 /**
201 * Informs the BackupManager of a change in backup state - if backup is disabled,
202 * the data on the server will be erased.
203 * @param enable whether to enable backup
204 */
205 private void setBackupEnabled(boolean enable) {
206 if (mBackupManager != null) {
207 try {
208 mBackupManager.setBackupEnabled(enable);
209 } catch (RemoteException e) {
210 Log.e(TAG, "Error communicating with BackupManager", e);
211 return;
212 }
213 }
214 }
215}