OSDN Git Service

Change method for getting external storage on marshmallow marshmallow-x86 nougat-x86 android-x86-6.0-r1 android-x86-6.0-r2 android-x86-6.0-r3
authorPaulo Sergio Travaglia <pstglia@gmail.com>
Sat, 16 Jan 2016 22:43:18 +0000 (20:43 -0200)
committerChih-Wei Huang <cwhuang@linux.org.tw>
Tue, 26 Jan 2016 02:40:26 +0000 (10:40 +0800)
The new components/features introduced with marshmallow
(vold 3.0) required a new method for retrieving
external/removable storage devices

These changes were based and use part of the following code:
- Storage access implemented on Settings (uses VolumeInfo class
  to list all connected disks (external and emulated)
  see refresh method from class StorageSettings
  => packages/apps/Settings/src/com/android/settings/deviceinfo
  =>  StorageSettings.java

- getVolumeList method from MountService class
  (converts VolumeInfo ArrayList item into a StorageVolume
   element, which is used by CMFileManager to iterate
   contents and build bookmarks):
  => frameworks/base/services/core/java/com/android/server/
  => MountService.java

src/com/cyanogenmod/filemanager/util/StorageHelper.java

index ed07fd5..7c653d6 100644 (file)
@@ -19,6 +19,12 @@ import android.content.Context;
 import android.os.Environment;
 import android.os.storage.StorageManager;
 import android.os.storage.StorageVolume;
+import android.os.storage.VolumeInfo;
+import android.os.UserHandle;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 
 import com.cyanogenmod.filemanager.FileManagerApplication;
 import com.cyanogenmod.filemanager.R;
@@ -50,9 +56,22 @@ public final class StorageHelper {
             //IMP!! Android SDK doesn't have a "getVolumeList" but is supported by CM10.
             //Use reflect to get this value (if possible)
             try {
+
+                final ArrayList<StorageVolume> res = new ArrayList<>();
+                final int userId = UserHandle.myUserId();
+
                 StorageManager sm = (StorageManager) ctx.getSystemService(Context.STORAGE_SERVICE);
-                Method method = sm.getClass().getMethod("getVolumeList"); //$NON-NLS-1$
-                sStorageVolumes = (StorageVolume[])method.invoke(sm);
+                final List<VolumeInfo> volumes = sm.getVolumes();
+                Collections.sort(volumes, VolumeInfo.getDescriptionComparator());
+
+                for (VolumeInfo vol : volumes) {
+                     if (vol.getType() == VolumeInfo.TYPE_PUBLIC || vol.getType() == VolumeInfo.TYPE_EMULATED) {
+                         final StorageVolume userVol = vol.buildStorageVolume(ctx, userId, false);
+                         res.add(userVol);
+                     }
+                }
+
+                sStorageVolumes = res.toArray(new StorageVolume[res.size()]);
 
             } catch (Exception ex) {
                 //Ignore. Android SDK StorageManager class doesn't have this method