Update Backup/reset UI

BUG: 20489775
Change-Id: I2c97fdf240ddd3e9f0a518478d5da91b91abe394
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 80c20dc..0e2ed8f 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -4595,6 +4595,8 @@
     <string name="backup_erase_dialog_message">Stop backing up your Wi\u2011Fi passwords, bookmarks, other settings, and app data, plus erase all copies on Google servers?</string>
     <!-- Dialog title for confirmation to erase full backup data from server -->
     <string name="fullbackup_erase_dialog_message">Stop backing up device data (such as Wi-Fi passwords and call history) and app data (such as settings and files stored by apps), plus erase all copies on Google Drive?</string>
+    <!-- Summary for explanation of what full app data backup means. Manufacturers may wish to overlay this resource with their own text  -->
+    <string name="fullbackup_data_summary">Automatically back up device data (such as Wi-Fi passwords and call history) and app data (such as settings and files stored by apps) remotely.\n\nWhen you turn on automatic backup, device and app data is periodically saved remotely. App data can be any data that an app has saved (based on developer settings), including potentially sensitive data such as contacts, messages, and photos.</string>
     <!-- Device admin settings screen --><skip />
     <!-- Device admin settings activity title -->
     <string name="device_admin_settings_title">Device administration settings</string>
diff --git a/res/xml/privacy_settings.xml b/res/xml/privacy_settings.xml
index 4607254..f9b4fbe 100644
--- a/res/xml/privacy_settings.xml
+++ b/res/xml/privacy_settings.xml
@@ -19,11 +19,11 @@
     android:title="@string/privacy_settings_title">
 
     <!-- Backup settings -->
-    <SwitchPreference
+    <PreferenceScreen
         android:key="backup_data"
         android:title="@string/backup_data_title"
-        android:summary="@string/backup_data_summary"
-        android:persistent="false" />
+        android:persistent="false"
+        android:fragment="com.android.settings.backup.ToggleBackupSettingFragment"/>
 
     <PreferenceScreen
         android:key="configure_account"
diff --git a/src/com/android/settings/PrivacySettings.java b/src/com/android/settings/PrivacySettings.java
index 6eb22fb..7c911cb 100644
--- a/src/com/android/settings/PrivacySettings.java
+++ b/src/com/android/settings/PrivacySettings.java
@@ -16,12 +16,9 @@
 
 package com.android.settings;
 
-import android.app.AlertDialog;
-import android.app.Dialog;
 import android.app.backup.IBackupManager;
 import android.content.ContentResolver;
 import android.content.Context;
-import android.content.DialogInterface;
 import android.content.Intent;
 import android.os.Bundle;
 import android.os.Process;
@@ -49,8 +46,7 @@
 /**
  * Gesture lock pattern settings.
  */
-public class PrivacySettings extends SettingsPreferenceFragment implements
-        DialogInterface.OnClickListener, Indexable {
+public class PrivacySettings extends SettingsPreferenceFragment implements Indexable {
 
     // Vendor specific
     private static final String GSETTINGS_PROVIDER = "com.google.settings";
@@ -61,15 +57,11 @@
     private static final String FACTORY_RESET = "factory_reset";
     private static final String TAG = "PrivacySettings";
     private IBackupManager mBackupManager;
-    private SwitchPreference mBackup;
+    private PreferenceScreen mBackup;
     private SwitchPreference mAutoRestore;
-    private Dialog mConfirmDialog;
     private PreferenceScreen mConfigure;
     private boolean mEnabled;
 
-    private static final int DIALOG_ERASE_BACKUP = 2;
-    private int mDialogType;
-
     @Override
     protected int getMetricsCategory() {
         return MetricsLogger.PRIVACY;
@@ -89,8 +81,7 @@
         mBackupManager = IBackupManager.Stub.asInterface(
                 ServiceManager.getService(Context.BACKUP_SERVICE));
 
-        mBackup = (SwitchPreference) screen.findPreference(BACKUP_DATA);
-        mBackup.setOnPreferenceChangeListener(preferenceChangeListener);
+        mBackup = (PreferenceScreen) screen.findPreference(BACKUP_DATA);
 
         mAutoRestore = (SwitchPreference) screen.findPreference(AUTO_RESTORE);
         mAutoRestore.setOnPreferenceChangeListener(preferenceChangeListener);
@@ -120,16 +111,6 @@
         }
     }
 
-    @Override
-    public void onStop() {
-        if (mConfirmDialog != null && mConfirmDialog.isShowing()) {
-            mConfirmDialog.dismiss();
-        }
-        mConfirmDialog = null;
-        mDialogType = 0;
-        super.onStop();
-    }
-
     private OnPreferenceChangeListener preferenceChangeListener = new OnPreferenceChangeListener() {
         @Override
         public boolean onPreferenceChange(Preference preference, Object newValue) {
@@ -138,16 +119,7 @@
             }
             boolean nextValue = (Boolean) newValue;
             boolean result = false;
-            if (preference == mBackup) {
-                if (nextValue == false) {
-                    // Don't change Switch status until user makes choice in dialog
-                    // so return false here.
-                    showEraseBackupDialog();
-                } else {
-                    setBackupEnabled(true);
-                    result = true;
-                }
-            } else if (preference == mAutoRestore) {
+            if (preference == mAutoRestore) {
                 try {
                     mBackupManager.setAutoRestore(nextValue);
                     result = true;
@@ -159,19 +131,9 @@
         }
     };
 
-    private void showEraseBackupDialog() {
-        mDialogType = DIALOG_ERASE_BACKUP;
-        CharSequence msg = getResources().getText(R.string.backup_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();
-    }
 
     /*
-     * Creates toggles for each available location provider
+     * Creates toggles for each backup/reset preference.
      */
     private void updateToggles() {
         ContentResolver res = getContentResolver();
@@ -184,11 +146,14 @@
             String transport = mBackupManager.getCurrentTransport();
             configIntent = mBackupManager.getConfigurationIntent(transport);
             configSummary = mBackupManager.getDestinationString(transport);
+
+            mBackup.setSummary(backupEnabled
+                    ? R.string.accessibility_feature_state_on
+                    : R.string.accessibility_feature_state_off);
         } catch (RemoteException e) {
             // leave it 'false' and disable the UI; there's no backup manager
             mBackup.setEnabled(false);
         }
-        mBackup.setChecked(backupEnabled);
 
         mAutoRestore.setChecked(Settings.Secure.getInt(res,
                 Settings.Secure.BACKUP_AUTO_RESTORE, 1) == 1);
@@ -208,54 +173,6 @@
         }
     }
 
-    private void updateConfigureSummary() {
-        try {
-            String transport = mBackupManager.getCurrentTransport();
-            String summary = mBackupManager.getDestinationString(transport);
-            setConfigureSummary(summary);
-        } catch (RemoteException e) {
-            // Not much we can do here
-        }
-    }
-
-    @Override
-    public void onClick(DialogInterface dialog, int which) {
-        // Dialog is triggered before Switch status change, that means marking the Switch to
-        // true in showEraseBackupDialog() method will be override by following status change.
-        // So we do manual switching here due to users' response.
-        if (mDialogType == DIALOG_ERASE_BACKUP) {
-            // Accept turning off backup
-            if (which == DialogInterface.BUTTON_POSITIVE) {
-                setBackupEnabled(false);
-            } else if (which == DialogInterface.BUTTON_NEGATIVE) {
-                // Reject turning off backup
-                setBackupEnabled(true);
-            }
-            updateConfigureSummary();
-        }
-        mDialogType = 0;
-    }
-
-    /**
-     * 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) {
-                mBackup.setChecked(!enable);
-                mAutoRestore.setEnabled(!enable);
-                return;
-            }
-        }
-        mBackup.setChecked(enable);
-        mAutoRestore.setEnabled(enable);
-        mConfigure.setEnabled(enable);
-    }
-
     @Override
     protected int getHelpResource() {
         return R.string.help_url_backup_reset;
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;
+            }
+        }
+    }
+}