1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| public class StateBarActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); showUsbDev(); getDiskInfo(); for(DiskInfo info : mDiskList){ Log.e("Seeyou", "Disk Name : " + info.name + "\n" + "Disk diskId : " + info.diskId + "\n"); for(DiskPartition partition : info.diskPartitions) { Log.e("Seeyou", "\tPartition Name : " + partition.name + "\n" + "\tPartition diskId : " + partition.diskId + "\n" + "\tPartition partitionId : " + partition.partitionId + "\n" + "\tPartition Path : " + partition.path + "\n" + "\tPartition Total Size : " + partition.totalSize + "\n" + "\tPartition Avlable Size: " + partition.avlableSize + "\n" ); } }
}
private void showUsbDev() { HashMap<String, UsbDevice> deviceHashMap = ((UsbManager) getSystemService(USB_SERVICE)).getDeviceList(); for (Map.Entry entry : deviceHashMap.entrySet()) { Log.e("Seeyou", "detectUsbDeviceWithUsbManager: \n" + entry.getKey() + "\n, " + entry.getValue()); } }
private List mDiskList = new ArrayList(); public void getDiskInfo() { StorageManager mstorageManager = (StorageManager)this.getApplicationContext().getSystemService(Context.STORAGE_SERVICE);
try { Method methodGetDisks = StorageManager.class.getMethod("getDisks"); Method methodGetStorageVolumes = StorageManager.class.getMethod("getVolumeList"); Method getVolumeById = StorageManager.class.getMethod("findVolumeById", String.class);
StorageVolume[] storageVolumes = (StorageVolume[]) methodGetStorageVolumes.invoke(mstorageManager); List disks = (List) methodGetDisks.invoke(mstorageManager);
Class<?> diskIndoClass = Class.forName("android.os.storage.DiskInfo"); Method mGetDiskId = diskIndoClass.getMethod("getId"); Field diskName = diskIndoClass.getField("label");
Class<?> storageVolumeClass = Class.forName("android.os.storage.StorageVolume"); Method mGetStorageVolId = storageVolumeClass.getMethod("getId"); Method mGetStorageVolDescription = storageVolumeClass.getMethod("getDescription", Context.class); Method mGetStorageVolPath = storageVolumeClass.getMethod("getPath"); Method isRemovable = storageVolumeClass.getMethod("isRemovable"); Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class);
Class<?> volumeClass = Class.forName("android.os.storage.VolumeInfo"); Method volumeDiskId = volumeClass.getMethod("getDiskId");
for(int i = 0; i < disks.size(); i++) { DiskInfo diskInfo = new DiskInfo(); Parcelable parcelable = (Parcelable) disks.get(i); diskInfo.diskId = (String) mGetDiskId.invoke(parcelable); Log.e("Seeyou", "diskid : " + diskInfo.diskId); String des = (String) diskName.get(parcelable); Log.e("Seeyou", "diskName : " + des); diskInfo.name = des; mDiskList.add(diskInfo); }
for(int j = 0; j < storageVolumes.length; j++) { DiskPartition partition = new DiskPartition(); StorageVolume storageVolume = storageVolumes[j]; partition.partitionId = (String) mGetStorageVolId.invoke(storageVolume); if("emulated".equals(partition.partitionId)) { continue; } partition.name = (String) mGetStorageVolDescription.invoke(storageVolume, this); partition.path = (String) mGetStorageVolPath.invoke(storageVolume); Boolean removeAble = ((Boolean) isRemovable.invoke(storageVolume)).booleanValue(); String state = (String) getVolumeState.invoke(mstorageManager, partition.path); if("mounted".equals(state) && removeAble) { partition.diskId = (String) volumeDiskId.invoke(getVolumeById.invoke(mstorageManager, partition.partitionId)); for(DiskInfo diskInfo : mDiskList) { if(diskInfo.diskId.equals(partition.diskId)) { getStorageBlockInfo(partition); diskInfo.diskPartitions.add(partition); } } } } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } }
public class DiskInfo { public String diskId; public String name; public ArrayList diskPartitions = new ArrayList<>(); }
public class DiskPartition{ public String name; public String partitionId; public String diskId; public long totalSize; public long avlableSize; public String path; }
public static void getStorageBlockInfo(DiskPartition info) { if(TextUtils.isEmpty(info.path)) return; android.os.StatFs statfs = new android.os.StatFs(info.path); long nBlocSize = statfs.getBlockSizeLong(); long blockCountLong = statfs.getBlockCountLong(); long nAvailaBlock = statfs.getAvailableBlocksLong(); info.totalSize = blockCountLong * nBlocSize; info.avlableSize = nBlocSize * nAvailaBlock; } }
|