blob: c3ad92fe23b458f744d05c40d8f8faefc459b490 [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;
Fan Zhang31b21002019-01-16 13:49:47 -08005import android.app.settings.SettingsEnums;
Matthew Williams6ee5a4d2015-05-15 21:14:14 -07006import android.content.Context;
7import android.content.DialogInterface;
8import android.os.Bundle;
9import android.os.RemoteException;
10import android.os.ServiceManager;
Sean Francis-Lyonc44d0ef2015-06-12 17:46:41 -070011import android.provider.Settings;
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070012import android.util.Log;
13import android.view.View;
14import android.widget.TextView;
15
Fan Zhang23f8d592018-08-28 15:11:40 -070016import androidx.appcompat.app.AlertDialog;
17import androidx.preference.Preference;
18import androidx.preference.PreferenceScreen;
19import androidx.preference.PreferenceViewHolder;
20
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070021import com.android.settings.R;
22import com.android.settings.SettingsActivity;
23import com.android.settings.SettingsPreferenceFragment;
Stanley Wangb87ddba2020-12-25 16:27:32 +080024import com.android.settings.widget.SettingsMainSwitchBar;
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070025
26/**
27 * Fragment to display a bunch of text about backup and restore, and allow the user to enable/
28 * disable it.
29 */
30public class ToggleBackupSettingFragment extends SettingsPreferenceFragment
Sean Francis-Lyon8452a622015-05-20 18:30:00 -070031 implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070032 private static final String TAG = "ToggleBackupSettingFragment";
33
34 private static final String BACKUP_TOGGLE = "toggle_backup";
35
Sean Francis-Lyonc44d0ef2015-06-12 17:46:41 -070036 // 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";
40
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070041 private IBackupManager mBackupManager;
42
Stanley Wangb87ddba2020-12-25 16:27:32 +080043 protected SettingsMainSwitchBar mSwitchBar;
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070044
45 private Preference mSummaryPreference;
46
47 private Dialog mConfirmDialog;
48
Sean Francis-Lyon8452a622015-05-20 18:30:00 -070049 private boolean mWaitingForConfirmationDialog = false;
50
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070051 @Override
52 public void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
54
55 mBackupManager = IBackupManager.Stub.asInterface(
56 ServiceManager.getService(Context.BACKUP_SERVICE));
57
58 PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(
59 getActivity());
60 setPreferenceScreen(preferenceScreen);
Jason Monk39b46742015-09-10 15:52:51 -040061 mSummaryPreference = new Preference(getPrefContext()) {
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070062 @Override
Jason Monk39b46742015-09-10 15:52:51 -040063 public void onBindViewHolder(PreferenceViewHolder view) {
64 super.onBindViewHolder(view);
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070065 final TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
66 summaryView.setText(getSummary());
67 }
68 };
69 mSummaryPreference.setPersistent(false);
70 mSummaryPreference.setLayoutResource(R.layout.text_description_preference);
71 preferenceScreen.addPreference(mSummaryPreference);
72 }
73
74 @Override
75 public void onViewCreated(View view, Bundle savedInstanceState) {
76 super.onViewCreated(view, savedInstanceState);
77
78 SettingsActivity activity = (SettingsActivity) getActivity();
79 mSwitchBar = activity.getSwitchBar();
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070080
81 // Set up UI.
Sean Francis-Lyonc44d0ef2015-06-12 17:46:41 -070082 // If the user has not seen legal text for full data backup (if they OTA from L to M) then
83 // full data backup will be off and here we want to show the old summary here that does
84 // not mention full data backup
85 if (Settings.Secure.getInt(getContentResolver(), USER_FULL_DATA_BACKUP_AWARE, 0) != 0) {
86 mSummaryPreference.setSummary(R.string.fullbackup_data_summary);
87 } else {
88 mSummaryPreference.setSummary(R.string.backup_data_summary);
89 }
Matthew Williams6ee5a4d2015-05-15 21:14:14 -070090 try {
91 boolean backupEnabled = mBackupManager == null ?
92 false : mBackupManager.isBackupEnabled();
93 mSwitchBar.setCheckedInternal(backupEnabled);
94 } catch (RemoteException e) {
95 // The world is aflame, turn it off.
96 mSwitchBar.setEnabled(false);
97 }
98 getActivity().setTitle(R.string.backup_data_title);
99 }
100
101 @Override
102 public void onDestroyView() {
103 super.onDestroyView();
104
Stanley Wangb87ddba2020-12-25 16:27:32 +0800105 mSwitchBar.setOnBeforeCheckedChangeListener(null);
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700106 mSwitchBar.hide();
107 }
108
109 @Override
110 public void onActivityCreated(Bundle savedInstanceState) {
111 super.onActivityCreated(savedInstanceState);
112
113 // Set up toggle listener. We need this b/c we have to intercept the toggle event in order
114 // to pop up the dialogue.
Stanley Wangb87ddba2020-12-25 16:27:32 +0800115 mSwitchBar.setOnBeforeCheckedChangeListener(
116 new SettingsMainSwitchBar.OnBeforeCheckedChangeListener() {
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700117 @Override
Chaohui Wang08a1c982023-11-07 20:41:11 +0800118 public boolean onBeforeCheckedChanged(boolean checked) {
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700119 if (!checked) {
120 // Don't change Switch status until user makes choice in dialog
Sean Francis-Lyon8452a622015-05-20 18:30:00 -0700121 // so return true here.
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700122 showEraseBackupDialog();
Sean Francis-Lyon8452a622015-05-20 18:30:00 -0700123 return true;
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700124 } else {
125 setBackupEnabled(true);
126 mSwitchBar.setCheckedInternal(true);
127 return true;
128 }
129 }
130 });
131 mSwitchBar.show();
132 }
133
134 /** Get rid of the dialog if it's still showing. */
135 @Override
136 public void onStop() {
137 if (mConfirmDialog != null && mConfirmDialog.isShowing()) {
138 mConfirmDialog.dismiss();
139 }
140 mConfirmDialog = null;
141 super.onStop();
142 }
143
144 @Override
145 public void onClick(DialogInterface dialog, int which) {
146 // Accept turning off backup
147 if (which == DialogInterface.BUTTON_POSITIVE) {
Sean Francis-Lyon8452a622015-05-20 18:30:00 -0700148 mWaitingForConfirmationDialog = false;
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700149 setBackupEnabled(false);
150 mSwitchBar.setCheckedInternal(false);
151 } else if (which == DialogInterface.BUTTON_NEGATIVE) {
152 // Reject turning off backup
Sean Francis-Lyon8452a622015-05-20 18:30:00 -0700153 mWaitingForConfirmationDialog = false;
154 setBackupEnabled(true);
155 mSwitchBar.setCheckedInternal(true);
156 }
157 }
158
159 @Override
160 public void onDismiss(DialogInterface dialog) {
161 if (mWaitingForConfirmationDialog) {
162 // dismiss turning off backup
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700163 setBackupEnabled(true);
164 mSwitchBar.setCheckedInternal(true);
165 }
166 }
167
168 private void showEraseBackupDialog() {
Sean Francis-Lyonc44d0ef2015-06-12 17:46:41 -0700169 CharSequence msg;
170
171 // If the user has not seen legal text for full data backup (if they OTA from L to M) then
172 // full data backup will be off and here we want to show the old erase_dialog_message here
173 // that does not mention full data backup
174 if (Settings.Secure.getInt(getContentResolver(), USER_FULL_DATA_BACKUP_AWARE, 0) != 0) {
175 msg = getResources().getText(R.string.fullbackup_erase_dialog_message);
176 } else {
177 msg = getResources().getText(R.string.backup_erase_dialog_message);
178 }
Sean Francis-Lyon8452a622015-05-20 18:30:00 -0700179
180 mWaitingForConfirmationDialog = true;
181
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700182 // TODO: DialogFragment?
183 mConfirmDialog = new AlertDialog.Builder(getActivity()).setMessage(msg)
184 .setTitle(R.string.backup_erase_dialog_title)
185 .setPositiveButton(android.R.string.ok, this)
186 .setNegativeButton(android.R.string.cancel, this)
Sean Francis-Lyon8452a622015-05-20 18:30:00 -0700187 .setOnDismissListener(this)
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700188 .show();
189 }
190
191 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700192 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800193 return SettingsEnums.PRIVACY;
Matthew Williams6ee5a4d2015-05-15 21:14:14 -0700194 }
195
196 /**
197 * Informs the BackupManager of a change in backup state - if backup is disabled,
198 * the data on the server will be erased.
199 * @param enable whether to enable backup
200 */
201 private void setBackupEnabled(boolean enable) {
202 if (mBackupManager != null) {
203 try {
204 mBackupManager.setBackupEnabled(enable);
205 } catch (RemoteException e) {
206 Log.e(TAG, "Error communicating with BackupManager", e);
207 return;
208 }
209 }
210 }
211}