Detect Removable Storage Properly

Bug: 11533662
Change-Id: I619103e4a748d19529faf76e511353a308af3331
diff --git a/Android.mk b/Android.mk
index 2cbb478..7a3de44 100644
--- a/Android.mk
+++ b/Android.mk
@@ -40,7 +40,7 @@
 LOCAL_OVERRIDES_PACKAGES := Exchange
 
 LOCAL_PROGUARD_FLAG_FILES := proguard.flags
-LOCAL_SDK_VERSION := 18
+LOCAL_SDK_VERSION := 19
 
 LOCAL_EMMA_COVERAGE_FILTER += +com.android.exchange.*
 
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index c057dae..0f9392c 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -62,7 +62,7 @@
     <uses-permission
         android:name="com.android.email.permission.ACCESS_PROVIDER"/>
 
-    <uses-sdk android:targetSdkVersion="18" android:minSdkVersion="14" />
+    <uses-sdk android:targetSdkVersion="19" android:minSdkVersion="14" />
 
     <!-- additional uses -->
 
diff --git a/src/com/android/exchange/adapter/ProvisionParser.java b/src/com/android/exchange/adapter/ProvisionParser.java
index d7be0d8..436a0a4 100644
--- a/src/com/android/exchange/adapter/ProvisionParser.java
+++ b/src/com/android/exchange/adapter/ProvisionParser.java
@@ -18,7 +18,8 @@
 import android.app.admin.DevicePolicyManager;
 import android.content.Context;
 import android.content.res.Resources;
-import android.os.storage.StorageManager;
+import android.os.Environment;
+import android.support.v4.content.ContextCompat;
 
 import com.android.emailcommon.provider.Policy;
 import com.android.exchange.Eas;
@@ -31,10 +32,9 @@
 import org.xmlpull.v1.XmlPullParserFactory;
 
 import java.io.ByteArrayInputStream;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
 import java.util.ArrayList;
 
 /**
@@ -616,31 +616,8 @@
      * TODO: Rewrite this when an appropriate API is available from the framework
      */
     private boolean hasRemovableStorage() {
-        try {
-            StorageManager sm = (StorageManager)mContext.getSystemService(Context.STORAGE_SERVICE);
-            Class<?> svClass = Class.forName("android.os.storage.StorageVolume");
-            Class<?> svManager = Class.forName("android.os.storage.StorageManager");
-            Method gvl = svManager.getDeclaredMethod("getVolumeList");
-            Object[] volumeList = (Object[]) gvl.invoke(sm);
-            for (Object volume: volumeList) {
-                Method isRemovable = svClass.getDeclaredMethod("isRemovable");
-                Method getDescription = svClass.getDeclaredMethod("getDescription");
-                String desc = (String)getDescription.invoke(volume);
-                if ((Boolean)isRemovable.invoke(volume)) {
-                    log("Removable: " + desc);
-                    return true;
-                } else {
-                    log("Not Removable: " + desc);
-                }
-            }
-            return false;
-        } catch (ClassNotFoundException e) {
-        } catch (NoSuchMethodException e) {
-        } catch (IllegalArgumentException e) {
-        } catch (IllegalAccessException e) {
-        } catch (InvocationTargetException e) {
-        }
-        // To be safe, we'll always indicate that there IS removable storage
-        return true;
+        final File[] cacheDirs = ContextCompat.getExternalCacheDirs(mContext);
+        return Environment.isExternalStorageRemovable()
+                || (cacheDirs != null && cacheDirs.length > 1);
     }
 }