Fragmentize "Factory reset" setting screen.

Split the screen into 2 fragments.

Bug: 3148480
diff --git a/src/com/android/settings/ChooseLockSettingsHelper.java b/src/com/android/settings/ChooseLockSettingsHelper.java
index abcfc05..d31fe3b 100644
--- a/src/com/android/settings/ChooseLockSettingsHelper.java
+++ b/src/com/android/settings/ChooseLockSettingsHelper.java
@@ -16,21 +16,28 @@
 
 package com.android.settings;
 
+import com.android.internal.widget.LockPatternUtils;
+
 import android.app.Activity;
+import android.app.Fragment;
 import android.app.admin.DevicePolicyManager;
 import android.content.Intent;
 
-import com.android.internal.widget.LockPatternUtils;
-
 public class ChooseLockSettingsHelper {
     private LockPatternUtils mLockPatternUtils;
     private Activity mActivity;
+    private Fragment mFragment;
 
     public ChooseLockSettingsHelper(Activity activity) {
         mActivity = activity;
         mLockPatternUtils = new LockPatternUtils(activity);
     }
 
+    public ChooseLockSettingsHelper(Activity activity, Fragment fragment) {
+        this(activity);
+        mFragment = fragment;
+    }
+
     public LockPatternUtils utils() {
         return mLockPatternUtils;
     }
@@ -76,7 +83,11 @@
         intent.putExtra(ConfirmLockPattern.HEADER_TEXT, message);
         intent.putExtra(ConfirmLockPattern.FOOTER_TEXT, details);
         intent.setClassName("com.android.settings", "com.android.settings.ConfirmLockPattern");
-        mActivity.startActivityForResult(intent, request);
+        if (mFragment != null) {
+            mFragment.startActivityForResult(intent, request);
+        } else {
+            mActivity.startActivityForResult(intent, request);
+        }
         return true;
     }
 
@@ -89,7 +100,11 @@
         if (!mLockPatternUtils.isLockPasswordEnabled()) return false;
         final Intent intent = new Intent();
         intent.setClassName("com.android.settings", "com.android.settings.ConfirmLockPassword");
-        mActivity.startActivityForResult(intent, request);
+        if (mFragment != null) {
+            mFragment.startActivityForResult(intent, request);
+        } else {
+            mActivity.startActivityForResult(intent, request);
+        }
         return true;
     }
 
diff --git a/src/com/android/settings/MasterClear.java b/src/com/android/settings/MasterClear.java
index e653d90..a2ef7f4 100644
--- a/src/com/android/settings/MasterClear.java
+++ b/src/com/android/settings/MasterClear.java
@@ -16,14 +16,18 @@
 
 package com.android.settings;
 
-import com.android.internal.os.storage.ExternalStorageFormatter;
-import com.android.internal.widget.LockPatternUtils;
+import com.android.settings.R;
 
 import android.app.Activity;
+import android.app.Fragment;
 import android.content.Intent;
+import android.content.res.Resources;
 import android.os.Bundle;
+import android.preference.Preference;
+import android.preference.PreferenceActivity;
 import android.view.LayoutInflater;
 import android.view.View;
+import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.CheckBox;
 
@@ -34,44 +38,20 @@
  * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
  * ON THE PHONE" prompt.  If at any time the phone is allowed to go to sleep, is
  * locked, et cetera, then the confirmation sequence is abandoned.
+ *
+ * This is the initial screen.
  */
-public class MasterClear extends Activity {
+public class MasterClear extends Fragment {
 
     private static final int KEYGUARD_REQUEST = 55;
 
-    private LayoutInflater mInflater;
-    private LockPatternUtils mLockUtils;
+    static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
 
-    private View mInitialView;
+    private View mContentView;
     private Button mInitiateButton;
     private View mExternalStorageContainer;
     private CheckBox mExternalStorage;
 
-    private View mFinalView;
-    private Button mFinalButton;
-
-    /**
-     * The user has gone through the multiple confirmation, so now we go ahead
-     * and invoke the Checkin Service to reset the device to its factory-default
-     * state (rebooting in the process).
-     */
-    private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
-            public void onClick(View v) {
-                if (Utils.isMonkeyRunning()) {
-                    return;
-                }
-
-                if (mExternalStorage.isChecked()) {
-                    Intent intent = new Intent(ExternalStorageFormatter.FORMAT_AND_FACTORY_RESET);
-                    intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
-                    startService(intent);
-                } else {
-                    sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
-                    // Intent handling is asynchronous -- assume it will happen soon.
-                }
-            }
-        };
-
     /**
      * Keyguard validation is run using the standard {@link ConfirmLockPattern}
      * component as a subactivity
@@ -79,14 +59,15 @@
      * @return true if confirmation launched
      */
     private boolean runKeyguardConfirmation(int request) {
-        return new ChooseLockSettingsHelper(this)
+        Resources res = getActivity().getResources();
+        return new ChooseLockSettingsHelper(getActivity(), this)
                 .launchConfirmationActivity(request,
-                        getText(R.string.master_clear_gesture_prompt),
-                        getText(R.string.master_clear_gesture_explanation));
+                        res.getText(R.string.master_clear_gesture_prompt),
+                        res.getText(R.string.master_clear_gesture_explanation));
     }
 
     @Override
-    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+    public void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
 
         if (requestCode != KEYGUARD_REQUEST) {
@@ -96,40 +77,33 @@
         // If the user entered a valid keyguard trace, present the final
         // confirmation prompt; otherwise, go back to the initial state.
         if (resultCode == Activity.RESULT_OK) {
-            establishFinalConfirmationState();
-        } else if (resultCode == Activity.RESULT_CANCELED) {
-            finish();
+            showFinalConfirmation();
         } else {
             establishInitialState();
         }
     }
 
+    private void showFinalConfirmation() {
+        Preference preference = new Preference(getActivity());
+        preference.setFragment(MasterClearConfirm.class.getName());
+        preference.setTitle(R.string.master_clear_confirm_title);
+        preference.getExtras().putBoolean(ERASE_EXTERNAL_EXTRA, mExternalStorage.isChecked());
+        ((PreferenceActivity) getActivity()).onPreferenceStartFragment(null, preference);
+    }
+
     /**
      * If the user clicks to begin the reset sequence, we next require a
      * keyguard confirmation if the user has currently enabled one.  If there
      * is no keyguard available, we simply go to the final confirmation prompt.
      */
     private Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
-            public void onClick(View v) {
-                if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
-                    establishFinalConfirmationState();
-                }
+
+        public void onClick(View v) {
+            if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
+                showFinalConfirmation();
             }
-        };
-
-    /**
-     * Configure the UI for the final confirmation interaction
-     */
-    private void establishFinalConfirmationState() {
-        if (mFinalView == null) {
-            mFinalView = mInflater.inflate(R.layout.master_clear_final, null);
-            mFinalButton =
-                    (Button) mFinalView.findViewById(R.id.execute_master_clear);
-            mFinalButton.setOnClickListener(mFinalClickListener);
         }
-
-        setContentView(mFinalView);
-    }
+    };
 
     /**
      * In its initial state, the activity presents a button for the user to
@@ -144,48 +118,25 @@
      * to change contents.
      */
     private void establishInitialState() {
-        if (mInitialView == null) {
-            mInitialView = mInflater.inflate(R.layout.master_clear_primary, null);
-            mInitiateButton =
-                    (Button) mInitialView.findViewById(R.id.initiate_master_clear);
-            mInitiateButton.setOnClickListener(mInitiateListener);
-            mExternalStorageContainer =
-                mInitialView.findViewById(R.id.erase_external_container);
-            mExternalStorage =
-                    (CheckBox) mInitialView.findViewById(R.id.erase_external);
-            mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
-                @Override
-                public void onClick(View v) {
-                    mExternalStorage.toggle();
-                }
-            });
-        }
+        mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
+        mInitiateButton.setOnClickListener(mInitiateListener);
+        mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);
+        mExternalStorage = (CheckBox) mContentView.findViewById(R.id.erase_external);
 
-        setContentView(mInitialView);
+        mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
+
+            @Override
+            public void onClick(View v) {
+                mExternalStorage.toggle();
+            }
+        });
     }
 
     @Override
-    protected void onCreate(Bundle savedState) {
-        super.onCreate(savedState);
-
-        mInitialView = null;
-        mFinalView = null;
-        mInflater = LayoutInflater.from(this);
-        mLockUtils = new LockPatternUtils(this);
-
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+            Bundle savedInstanceState) {
+        mContentView = inflater.inflate(R.layout.master_clear, null);
         establishInitialState();
-    }
-
-    /** Abandon all progress through the confirmation sequence by returning
-     * to the initial view any time the activity is interrupted (e.g. by
-     * idle timeout).
-     */
-    @Override
-    public void onPause() {
-        super.onPause();
-
-        if (!isFinishing()) {
-            establishInitialState();
-        }
+        return mContentView;
     }
 }
diff --git a/src/com/android/settings/MasterClearConfirm.java b/src/com/android/settings/MasterClearConfirm.java
new file mode 100644
index 0000000..9c15c73
--- /dev/null
+++ b/src/com/android/settings/MasterClearConfirm.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings;
+
+import com.android.internal.os.storage.ExternalStorageFormatter;
+import com.android.internal.widget.LockPatternUtils;
+
+import android.app.Activity;
+import android.app.Fragment;
+import android.content.Intent;
+import android.content.res.Resources;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.Button;
+import android.widget.CheckBox;
+
+/**
+ * Confirm and execute a reset of the device to a clean "just out of the box"
+ * state.  Multiple confirmations are required: first, a general "are you sure
+ * you want to do this?" prompt, followed by a keyguard pattern trace if the user
+ * has defined one, followed by a final strongly-worded "THIS WILL ERASE EVERYTHING
+ * ON THE PHONE" prompt.  If at any time the phone is allowed to go to sleep, is
+ * locked, et cetera, then the confirmation sequence is abandoned.
+ *
+ * This is the confirmation screen.
+ */
+public class MasterClearConfirm extends Fragment {
+
+    private View mContentView;
+    private boolean mEraseSdCard;
+    private Button mFinalButton;
+
+    /**
+     * The user has gone through the multiple confirmation, so now we go ahead
+     * and invoke the Checkin Service to reset the device to its factory-default
+     * state (rebooting in the process).
+     */
+    private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
+
+        public void onClick(View v) {
+            if (Utils.isMonkeyRunning()) {
+                return;
+            }
+
+            if (mEraseSdCard) {
+                Intent intent = new Intent(ExternalStorageFormatter.FORMAT_AND_FACTORY_RESET);
+                intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
+                getActivity().startService(intent);
+            } else {
+                getActivity().sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
+                // Intent handling is asynchronous -- assume it will happen soon.
+            }
+        }
+    };
+
+    /**
+     * Configure the UI for the final confirmation interaction
+     */
+    private void establishFinalConfirmationState() {
+        mFinalButton = (Button) mContentView.findViewById(R.id.execute_master_clear);
+        mFinalButton.setOnClickListener(mFinalClickListener);
+    }
+
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+            Bundle savedInstanceState) {
+        mContentView = inflater.inflate(R.layout.master_clear_confirm, null);
+        establishFinalConfirmationState();
+        return mContentView;
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        Bundle args = getArguments();
+        mEraseSdCard = args != null ? args.getBoolean(MasterClear.ERASE_EXTERNAL_EXTRA) : false;
+    }
+}
diff --git a/src/com/android/settings/PrivacySettings.java b/src/com/android/settings/PrivacySettings.java
index 826c9cf..3f62f19 100644
--- a/src/com/android/settings/PrivacySettings.java
+++ b/src/com/android/settings/PrivacySettings.java
@@ -98,7 +98,7 @@
             }
         }
 
-        return false;
+        return super.onPreferenceTreeClick(preferenceScreen, preference);
     }
 
     private void showEraseBackupDialog() {