Implement issue #3094621 and #3094609 - wipe sd card

3094621: add "wipe sd card" option to factory data reset
3094609: collapse unmount/format into one command

Implements requested UI changes.  Also some final tweaks to
Manage Applications.

Change-Id: I0219195dd0c74d8c003ef1c3f6e09714859d7f89
diff --git a/src/com/android/settings/MasterClear.java b/src/com/android/settings/MasterClear.java
index 4de0e44..e653d90 100644
--- a/src/com/android/settings/MasterClear.java
+++ b/src/com/android/settings/MasterClear.java
@@ -16,20 +16,16 @@
 
 package com.android.settings;
 
+import com.android.internal.os.storage.ExternalStorageFormatter;
 import com.android.internal.widget.LockPatternUtils;
 
 import android.app.Activity;
-import android.app.AlertDialog;
-import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
-import android.os.ServiceManager;
-import android.os.SystemProperties;
-import android.text.TextUtils;
-import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.Button;
+import android.widget.CheckBox;
 
 /**
  * Confirm and execute a reset of the device to a clean "just out of the box"
@@ -48,6 +44,8 @@
 
     private View mInitialView;
     private Button mInitiateButton;
+    private View mExternalStorageContainer;
+    private CheckBox mExternalStorage;
 
     private View mFinalView;
     private Button mFinalButton;
@@ -63,8 +61,14 @@
                     return;
                 }
 
-                sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
-                // Intent handling is asynchronous -- assume it will happen soon.
+                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.
+                }
             }
         };
 
@@ -145,6 +149,16 @@
             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();
+                }
+            });
         }
 
         setContentView(mInitialView);
@@ -170,7 +184,8 @@
     public void onPause() {
         super.onPause();
 
-        establishInitialState();
+        if (!isFinishing()) {
+            establishInitialState();
+        }
     }
-
 }