summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/backup
diff options
context:
space:
mode:
authorMatthew Williams <mjwilliams@google.com>2015-05-15 21:14:14 -0700
committerMatthew Williams <mjwilliams@google.com>2015-05-18 18:16:09 -0700
commit6ee5a4d0ef0386d56c72a8ae81e65443fe5a2979 (patch)
treeae971b1c7662642d574420e45d7ebc1af9bb0fea /src/com/android/settings/backup
parent16a45bafa78f3a2cf2b37879c57441c608b03ab7 (diff)
downloadpackages_apps_Settings-6ee5a4d0ef0386d56c72a8ae81e65443fe5a2979.zip
packages_apps_Settings-6ee5a4d0ef0386d56c72a8ae81e65443fe5a2979.tar.gz
packages_apps_Settings-6ee5a4d0ef0386d56c72a8ae81e65443fe5a2979.tar.bz2
Update Backup/reset UI
BUG: 20489775 Change-Id: I2c97fdf240ddd3e9f0a518478d5da91b91abe394
Diffstat (limited to 'src/com/android/settings/backup')
-rw-r--r--src/com/android/settings/backup/ToggleBackupSettingFragment.java174
1 files changed, 174 insertions, 0 deletions
diff --git a/src/com/android/settings/backup/ToggleBackupSettingFragment.java b/src/com/android/settings/backup/ToggleBackupSettingFragment.java
new file mode 100644
index 0000000..e30266f
--- /dev/null
+++ b/src/com/android/settings/backup/ToggleBackupSettingFragment.java
@@ -0,0 +1,174 @@
+package com.android.settings.backup;
+
+import android.app.AlertDialog;
+import android.app.backup.IBackupManager;
+import android.app.Dialog;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.os.Bundle;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.preference.Preference;
+import android.preference.PreferenceScreen;
+import android.util.Log;
+import android.view.View;
+import android.widget.TextView;
+
+import com.android.internal.logging.MetricsLogger;
+import com.android.settings.R;
+import com.android.settings.SettingsActivity;
+import com.android.settings.SettingsPreferenceFragment;
+import com.android.settings.widget.SwitchBar;
+import com.android.settings.widget.ToggleSwitch;
+
+/**
+ * Fragment to display a bunch of text about backup and restore, and allow the user to enable/
+ * disable it.
+ */
+public class ToggleBackupSettingFragment extends SettingsPreferenceFragment
+ implements DialogInterface.OnClickListener {
+ private static final String TAG = "ToggleBackupSettingFragment";
+
+ private static final String BACKUP_TOGGLE = "toggle_backup";
+
+ private IBackupManager mBackupManager;
+
+ protected SwitchBar mSwitchBar;
+ protected ToggleSwitch mToggleSwitch;
+
+ private Preference mSummaryPreference;
+
+ private Dialog mConfirmDialog;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ mBackupManager = IBackupManager.Stub.asInterface(
+ ServiceManager.getService(Context.BACKUP_SERVICE));
+
+ PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(
+ getActivity());
+ setPreferenceScreen(preferenceScreen);
+ mSummaryPreference = new Preference(getActivity()) {
+ @Override
+ protected void onBindView(View view) {
+ super.onBindView(view);
+ final TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
+ summaryView.setText(getSummary());
+ }
+ };
+ mSummaryPreference.setPersistent(false);
+ mSummaryPreference.setLayoutResource(R.layout.text_description_preference);
+ preferenceScreen.addPreference(mSummaryPreference);
+ }
+
+ @Override
+ public void onViewCreated(View view, Bundle savedInstanceState) {
+ super.onViewCreated(view, savedInstanceState);
+
+ SettingsActivity activity = (SettingsActivity) getActivity();
+ mSwitchBar = activity.getSwitchBar();
+ mToggleSwitch = mSwitchBar.getSwitch();
+
+ // Set up UI.
+ mSummaryPreference.setSummary(R.string.fullbackup_data_summary);
+ try {
+ boolean backupEnabled = mBackupManager == null ?
+ false : mBackupManager.isBackupEnabled();
+ mSwitchBar.setCheckedInternal(backupEnabled);
+ } catch (RemoteException e) {
+ // The world is aflame, turn it off.
+ mSwitchBar.setEnabled(false);
+ }
+ getActivity().setTitle(R.string.backup_data_title);
+ }
+
+ @Override
+ public void onDestroyView() {
+ super.onDestroyView();
+
+ mToggleSwitch.setOnBeforeCheckedChangeListener(null);
+ mSwitchBar.hide();
+ }
+
+ @Override
+ public void onActivityCreated(Bundle savedInstanceState) {
+ super.onActivityCreated(savedInstanceState);
+
+ // Set up toggle listener. We need this b/c we have to intercept the toggle event in order
+ // to pop up the dialogue.
+ mToggleSwitch.setOnBeforeCheckedChangeListener(
+ new ToggleSwitch.OnBeforeCheckedChangeListener() {
+ @Override
+ public boolean onBeforeCheckedChanged(
+ ToggleSwitch toggleSwitch, boolean checked) {
+ if (!checked) {
+ // Don't change Switch status until user makes choice in dialog
+ // so return false here.
+ showEraseBackupDialog();
+ return false;
+ } else {
+ setBackupEnabled(true);
+ mSwitchBar.setCheckedInternal(true);
+ return true;
+ }
+ }
+ });
+ mSwitchBar.show();
+ }
+
+ /** Get rid of the dialog if it's still showing. */
+ @Override
+ public void onStop() {
+ if (mConfirmDialog != null && mConfirmDialog.isShowing()) {
+ mConfirmDialog.dismiss();
+ }
+ mConfirmDialog = null;
+ super.onStop();
+ }
+
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ // Accept turning off backup
+ if (which == DialogInterface.BUTTON_POSITIVE) {
+ setBackupEnabled(false);
+ mSwitchBar.setCheckedInternal(false);
+ } else if (which == DialogInterface.BUTTON_NEGATIVE) {
+ // Reject turning off backup
+ setBackupEnabled(true);
+ mSwitchBar.setCheckedInternal(true);
+ }
+ }
+
+ private void showEraseBackupDialog() {
+ CharSequence msg = getResources().getText(R.string.fullbackup_erase_dialog_message);
+ // TODO: DialogFragment?
+ mConfirmDialog = new AlertDialog.Builder(getActivity()).setMessage(msg)
+ .setTitle(R.string.backup_erase_dialog_title)
+ .setPositiveButton(android.R.string.ok, this)
+ .setNegativeButton(android.R.string.cancel, this)
+ .show();
+ }
+
+ @Override
+ protected int getMetricsCategory() {
+ return MetricsLogger.PRIVACY;
+ }
+
+ /**
+ * Informs the BackupManager of a change in backup state - if backup is disabled,
+ * the data on the server will be erased.
+ * @param enable whether to enable backup
+ */
+ private void setBackupEnabled(boolean enable) {
+ if (mBackupManager != null) {
+ try {
+ mBackupManager.setBackupEnabled(enable);
+ } catch (RemoteException e) {
+ Log.e(TAG, "Error communicating with BackupManager", e);
+ return;
+ }
+ }
+ }
+}