| Matthew Williams | 6ee5a4d | 2015-05-15 21:14:14 -0700 | [diff] [blame^] | 1 | package com.android.settings.backup; |
| 2 | |
| 3 | import android.app.AlertDialog; |
| 4 | import android.app.backup.IBackupManager; |
| 5 | import android.app.Dialog; |
| 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.preference.Preference; |
| 12 | import android.preference.PreferenceScreen; |
| 13 | import android.util.Log; |
| 14 | import android.view.View; |
| 15 | import android.widget.TextView; |
| 16 | |
| 17 | import com.android.internal.logging.MetricsLogger; |
| 18 | import com.android.settings.R; |
| 19 | import com.android.settings.SettingsActivity; |
| 20 | import com.android.settings.SettingsPreferenceFragment; |
| 21 | import com.android.settings.widget.SwitchBar; |
| 22 | import com.android.settings.widget.ToggleSwitch; |
| 23 | |
| 24 | /** |
| 25 | * Fragment to display a bunch of text about backup and restore, and allow the user to enable/ |
| 26 | * disable it. |
| 27 | */ |
| 28 | public class ToggleBackupSettingFragment extends SettingsPreferenceFragment |
| 29 | implements DialogInterface.OnClickListener { |
| 30 | private static final String TAG = "ToggleBackupSettingFragment"; |
| 31 | |
| 32 | private static final String BACKUP_TOGGLE = "toggle_backup"; |
| 33 | |
| 34 | private IBackupManager mBackupManager; |
| 35 | |
| 36 | protected SwitchBar mSwitchBar; |
| 37 | protected ToggleSwitch mToggleSwitch; |
| 38 | |
| 39 | private Preference mSummaryPreference; |
| 40 | |
| 41 | private Dialog mConfirmDialog; |
| 42 | |
| 43 | @Override |
| 44 | public void onCreate(Bundle savedInstanceState) { |
| 45 | super.onCreate(savedInstanceState); |
| 46 | |
| 47 | mBackupManager = IBackupManager.Stub.asInterface( |
| 48 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 49 | |
| 50 | PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen( |
| 51 | getActivity()); |
| 52 | setPreferenceScreen(preferenceScreen); |
| 53 | mSummaryPreference = new Preference(getActivity()) { |
| 54 | @Override |
| 55 | protected void onBindView(View view) { |
| 56 | super.onBindView(view); |
| 57 | final TextView summaryView = (TextView) view.findViewById(android.R.id.summary); |
| 58 | summaryView.setText(getSummary()); |
| 59 | } |
| 60 | }; |
| 61 | mSummaryPreference.setPersistent(false); |
| 62 | mSummaryPreference.setLayoutResource(R.layout.text_description_preference); |
| 63 | preferenceScreen.addPreference(mSummaryPreference); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void onViewCreated(View view, Bundle savedInstanceState) { |
| 68 | super.onViewCreated(view, savedInstanceState); |
| 69 | |
| 70 | SettingsActivity activity = (SettingsActivity) getActivity(); |
| 71 | mSwitchBar = activity.getSwitchBar(); |
| 72 | mToggleSwitch = mSwitchBar.getSwitch(); |
| 73 | |
| 74 | // Set up UI. |
| 75 | mSummaryPreference.setSummary(R.string.fullbackup_data_summary); |
| 76 | try { |
| 77 | boolean backupEnabled = mBackupManager == null ? |
| 78 | false : mBackupManager.isBackupEnabled(); |
| 79 | mSwitchBar.setCheckedInternal(backupEnabled); |
| 80 | } catch (RemoteException e) { |
| 81 | // The world is aflame, turn it off. |
| 82 | mSwitchBar.setEnabled(false); |
| 83 | } |
| 84 | getActivity().setTitle(R.string.backup_data_title); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void onDestroyView() { |
| 89 | super.onDestroyView(); |
| 90 | |
| 91 | mToggleSwitch.setOnBeforeCheckedChangeListener(null); |
| 92 | mSwitchBar.hide(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void onActivityCreated(Bundle savedInstanceState) { |
| 97 | super.onActivityCreated(savedInstanceState); |
| 98 | |
| 99 | // Set up toggle listener. We need this b/c we have to intercept the toggle event in order |
| 100 | // to pop up the dialogue. |
| 101 | mToggleSwitch.setOnBeforeCheckedChangeListener( |
| 102 | new ToggleSwitch.OnBeforeCheckedChangeListener() { |
| 103 | @Override |
| 104 | public boolean onBeforeCheckedChanged( |
| 105 | ToggleSwitch toggleSwitch, boolean checked) { |
| 106 | if (!checked) { |
| 107 | // Don't change Switch status until user makes choice in dialog |
| 108 | // so return false here. |
| 109 | showEraseBackupDialog(); |
| 110 | return false; |
| 111 | } else { |
| 112 | setBackupEnabled(true); |
| 113 | mSwitchBar.setCheckedInternal(true); |
| 114 | return true; |
| 115 | } |
| 116 | } |
| 117 | }); |
| 118 | mSwitchBar.show(); |
| 119 | } |
| 120 | |
| 121 | /** Get rid of the dialog if it's still showing. */ |
| 122 | @Override |
| 123 | public void onStop() { |
| 124 | if (mConfirmDialog != null && mConfirmDialog.isShowing()) { |
| 125 | mConfirmDialog.dismiss(); |
| 126 | } |
| 127 | mConfirmDialog = null; |
| 128 | super.onStop(); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void onClick(DialogInterface dialog, int which) { |
| 133 | // Accept turning off backup |
| 134 | if (which == DialogInterface.BUTTON_POSITIVE) { |
| 135 | setBackupEnabled(false); |
| 136 | mSwitchBar.setCheckedInternal(false); |
| 137 | } else if (which == DialogInterface.BUTTON_NEGATIVE) { |
| 138 | // Reject turning off backup |
| 139 | setBackupEnabled(true); |
| 140 | mSwitchBar.setCheckedInternal(true); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private void showEraseBackupDialog() { |
| 145 | CharSequence msg = getResources().getText(R.string.fullbackup_erase_dialog_message); |
| 146 | // TODO: DialogFragment? |
| 147 | mConfirmDialog = new AlertDialog.Builder(getActivity()).setMessage(msg) |
| 148 | .setTitle(R.string.backup_erase_dialog_title) |
| 149 | .setPositiveButton(android.R.string.ok, this) |
| 150 | .setNegativeButton(android.R.string.cancel, this) |
| 151 | .show(); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | protected int getMetricsCategory() { |
| 156 | return MetricsLogger.PRIVACY; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Informs the BackupManager of a change in backup state - if backup is disabled, |
| 161 | * the data on the server will be erased. |
| 162 | * @param enable whether to enable backup |
| 163 | */ |
| 164 | private void setBackupEnabled(boolean enable) { |
| 165 | if (mBackupManager != null) { |
| 166 | try { |
| 167 | mBackupManager.setBackupEnabled(enable); |
| 168 | } catch (RemoteException e) { |
| 169 | Log.e(TAG, "Error communicating with BackupManager", e); |
| 170 | return; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |