Force storage wipe if not removable and encrypted

Bug: 5017638
Change-Id: I43c98359eff7202437249675060ba964e1bd085c
diff --git a/src/com/android/settings/MasterClear.java b/src/com/android/settings/MasterClear.java
index 29a92b1..39b17a9 100644
--- a/src/com/android/settings/MasterClear.java
+++ b/src/com/android/settings/MasterClear.java
@@ -16,8 +16,6 @@
 
 package com.android.settings;
 
-import com.android.settings.R;
-
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.accounts.AuthenticatorDescription;
@@ -30,6 +28,7 @@
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.os.Environment;
+import android.os.SystemProperties;
 import android.preference.Preference;
 import android.preference.PreferenceActivity;
 import android.util.Log;
@@ -107,7 +106,7 @@
      * 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() {
+    private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
 
         public void onClick(View v) {
             if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
@@ -138,9 +137,12 @@
          * If the external storage is emulated, it will be erased with a factory
          * reset at any rate. There is no need to have a separate option until
          * we have a factory reset that only erases some directories and not
-         * others.
+         * others. Likewise, if it's non-removable storage, it could potentially have been
+         * encrypted, and will also need to be wiped.
          */
-        if (Environment.isExternalStorageEmulated()) {
+        boolean isExtStorageEmulated = Environment.isExternalStorageEmulated();
+        if (isExtStorageEmulated
+                || (!Environment.isExternalStorageRemovable() && isExtStorageEncrypted())) {
             mExternalStorageContainer.setVisibility(View.GONE);
 
             final View externalOption = mContentView.findViewById(R.id.erase_external_option_text);
@@ -148,6 +150,10 @@
 
             final View externalAlsoErased = mContentView.findViewById(R.id.also_erases_external);
             externalAlsoErased.setVisibility(View.VISIBLE);
+
+            // If it's not emulated, it is on a separate partition but it means we're doing
+            // a force wipe due to encryption.
+            mExternalStorage.setChecked(!isExtStorageEmulated);
         } else {
             mExternalStorageContainer.setOnClickListener(new View.OnClickListener() {
 
@@ -161,6 +167,11 @@
         loadAccountList();
     }
 
+    private boolean isExtStorageEncrypted() {
+        String state = SystemProperties.get("vold.decrypt");
+        return !"".equals(state);
+    }
+
     private void loadAccountList() {
         View accountsLabel = mContentView.findViewById(R.id.accounts_label);
         LinearLayout contents = (LinearLayout)mContentView.findViewById(R.id.accounts);
diff --git a/src/com/android/settings/deviceinfo/Memory.java b/src/com/android/settings/deviceinfo/Memory.java
index e257f86..728e558 100644
--- a/src/com/android/settings/deviceinfo/Memory.java
+++ b/src/com/android/settings/deviceinfo/Memory.java
@@ -60,7 +60,7 @@
     // one's preference is disabled
     private Preference mLastClickedMountToggle;
     private String mClickedMountPoint;
-    
+
     // Access using getMountService()
     private IMountService mMountService = null;
 
@@ -197,7 +197,7 @@
        }
        return mMountService;
     }
-    
+
     @Override
     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
         for (int i = 0; i < mStorageVolumePreferenceCategories.length; i++) {
@@ -225,7 +225,7 @@
 
         return false;
     }
-     
+
     private final BroadcastReceiver mMediaScannerReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
@@ -267,7 +267,7 @@
             mLastClickedMountToggle.setEnabled(false);
             mLastClickedMountToggle.setTitle(mResources.getString(R.string.sd_ejecting_title));
             mLastClickedMountToggle.setSummary(mResources.getString(R.string.sd_ejecting_summary));
-            mountService.unmountVolume(mClickedMountPoint, true);
+            mountService.unmountVolume(mClickedMountPoint, true, false);
         } catch (RemoteException e) {
             // Informative dialog to user that unmount failed.
             showDialogInner(DLG_ERROR_UNMOUNT);