private void requestPermission() { try { StorageManager sm = getSystemService(StorageManager.class); StorageVolume volume = sm.getPrimaryStorageVolume(); Intent intent = volume.createAccessIntent(Environment.DIRECTORY_DOWNLOADS); startActivityForResult(intent, REQUEST_CODE); } catch (Exception e) { //Toast.makeText(this, R.string.cannot_request_permission, Toast.LENGTH_LONG).show(); Toast.makeText(this, "Can't use Scoped Directory Access.\nFallback to runtime permission.", Toast.LENGTH_LONG).show(); Log.wtf("FFM", "can't use Scoped Directory Access", e); Crashlytics.logException(e); // fallback to runtime permission if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0); } } }
private static String[] getVolumePaths(Context context) { String[] volumes = null; StorageManager managerStorage = (StorageManager) context.getSystemService("storage"); if (managerStorage == null) { return volumes; } try { return (String[]) managerStorage.getClass().getMethod("getVolumePaths", new Class[0]).invoke(managerStorage, new Object[0]); } catch (NoSuchMethodException e) { e.printStackTrace(); return volumes; } catch (IllegalArgumentException e2) { e2.printStackTrace(); return volumes; } catch (IllegalAccessException e3) { e3.printStackTrace(); return volumes; } catch (InvocationTargetException e4) { e4.printStackTrace(); return volumes; } }
private static int isSdcardMounted(String path, Context context) { StorageManager managerStorage = (StorageManager) context.getSystemService("storage"); if (managerStorage == null) { return -1; } try { Method method = managerStorage.getClass().getDeclaredMethod("getVolumeState", new Class[]{String.class}); method.setAccessible(true); if ("mounted".equalsIgnoreCase((String) method.invoke(managerStorage, new Object[]{path}))) { return 1; } return 0; } catch (NoSuchMethodException e) { e.printStackTrace(); return -1; } catch (IllegalArgumentException e2) { e2.printStackTrace(); return -1; } catch (IllegalAccessException e3) { e3.printStackTrace(); return -1; } catch (InvocationTargetException e4) { e4.printStackTrace(); return -1; } }
public AppPagerAdapter(FragmentManager fm) { super(fm); titles.add("Clone Apps"); dirs.add(null); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Context ctx = VApp.getApp(); StorageManager storage = (StorageManager) ctx.getSystemService(Context.STORAGE_SERVICE); for (StorageVolume volume : storage.getStorageVolumes()) { //Why the fuck are getPathFile and getUserLabel hidden?! //StorageVolume is kinda useless without those... File dir = Reflect.on(volume).call("getPathFile").get(); String label = Reflect.on(volume).call("getUserLabel").get(); if (dir.listFiles() != null) { titles.add(label); dirs.add(dir); } } } else { // Fallback: only support the default storage sources File storageFir = Environment.getExternalStorageDirectory(); if (storageFir.list() != null) { titles.add("Ghost Installation"); dirs.add(storageFir); } } }
public static ArrayList<String> getMountedPoints(Context context) { StorageManager manager = (StorageManager) context.getSystemService(Activity.STORAGE_SERVICE); ArrayList<String> mountedPoints = new ArrayList<String>(); try { Method getVolumePaths = manager.getClass().getMethod("getVolumePaths"); String[] points = (String[]) getVolumePaths.invoke(manager); if (points != null && points.length > 0) { Method getVolumeState = manager.getClass().getMethod("getVolumeState", String.class); for (String point : points) { String state = (String) getVolumeState.invoke(manager, point); if (Environment.MEDIA_MOUNTED.equals(state)) mountedPoints.add(point); } return mountedPoints; } } catch (Exception e) { e.printStackTrace(); } return null; }
/** * Get all available storage directories. * Inspired by CyanogenMod File Manager: * https://github.com/CyanogenMod/android_packages_apps_CMFileManager */ public static Set<File> getStorageDirectories(Context context) { if (storageDirectories == null) { try { // Use reflection to retrieve storage volumes because required classes and methods are hidden in AOSP. StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); Method method = storageManager.getClass().getMethod("getVolumeList"); StorageVolume[] storageVolumes = (StorageVolume[]) method.invoke(storageManager); if (storageVolumes != null && storageVolumes.length > 0) { storageDirectories = new HashSet<>(); for (StorageVolume volume : storageVolumes) { storageDirectories.add(new File(volume.getPath())); } } } catch (Exception e) { Log.e(LOG_TAG, e.getMessage()); } } return storageDirectories; }
private static boolean checkStorageMountState(Context context, String mountPoint) { if (mountPoint == null) { return false; } StorageManager storageManager = (StorageManager) context .getSystemService(Context.STORAGE_SERVICE); try { Method getVolumeState = storageManager.getClass().getMethod( "getVolumeState", String.class); String state = (String) getVolumeState.invoke(storageManager, mountPoint); return Environment.MEDIA_MOUNTED.equals(state); } catch (Exception e) { e.printStackTrace(); } return false; }
public StorageUtils(Context context) { mContext = context; if (mContext != null) { mStorageManager = (StorageManager)mContext.getSystemService(Activity.STORAGE_SERVICE); try { mMethodGetPaths = mStorageManager.getClass().getMethod("getVolumePaths"); } catch (NoSuchMethodException e) { e.printStackTrace(); } } }
@Override public boolean isVulnerable(Context context) throws Exception { int pid = SystemUtils.ProcfindPidFor("/system/bin/vold"); StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); Method getObbPath = sm.getClass().getMethod("getMountedObbPath", String.class); getObbPath.invoke(sm, "AAAA AAAA AAAA AAAA " + "AAAA AAAA AAAA AAAA " + "AAAA AAAA AAAA AAAA " + "AAAA AAAA AAAA AAAA" + "AAAA AAAA AAAA AAAA" + "AAAA AAAA AAAA AAAA" + "AAAA AAAA AAAA AAAA" + "AAAA AAAA AAAA AAAA" + "AAAA AAAA AAAA AAAA" + "AAAA AAAA AAAA AAAA" + "AAAA AAAA AAAA AAAA"); Thread.sleep(2000); // give vold some time to crash return false; }
@TargetApi(Build.VERSION_CODES.N) private static List<MountVolume> getVolumeListApi24(Context context) { StorageManager storageManager = context.getSystemService(StorageManager.class); List<StorageVolume> volumes = storageManager.getStorageVolumes(); List<MountVolume> list = new ArrayList<MountVolume>(volumes.size()); List<String> fileList = getMountsList(); for (StorageVolume volume : volumes) { MountVolume mountVolume = new MountVolume(); mountVolume.setUuid(volume.isPrimary() ? "primary" : volume.getUuid()); mountVolume.setPathFile(volume.isPrimary() ? new File(getFilePath(fileList, "emulated") + "/0") : new File(getFilePath(fileList, volume.getUuid()))); mountVolume.setPrimary(volume.isPrimary()); mountVolume.setEmulated(volume.isEmulated()); mountVolume.setRemovable(volume.isRemovable()); mountVolume.setDescription(volume.getDescription(context)); mountVolume.setState(volume.getState()); list.add(mountVolume); } return list; }
/** * 获取路径根目录 * * @param manager 存储设备管理器 * @param path 路径 * @return 根目录 */ public static String getPathStorage(StorageManager manager, String path) { if (StringUtils.isNullOrEmpty(path)) return null; List<AMStorageManagerCompat.StorageVolumeImpl> volumes = AMStorageManagerCompat.getEmulatedStorageVolumes(manager); if (volumes.size() < 1) return null; String storage = null; for (AMStorageManagerCompat.StorageVolumeImpl volume : volumes) { final String storagePath = volume.getPath(); if (path.startsWith(storagePath)) { storage = storagePath; break; } } return storage; }
String[] getStoragePathsCompat() { try { StorageManager sm = (StorageManager) appContext.getSystemService(Context.STORAGE_SERVICE); Method getVolumeList = StorageManager.class.getDeclaredMethod("getVolumeList"); Class<?> volume = Class.forName("android.os.storage.StorageVolume"); Method getPath = volume.getDeclaredMethod("getPath"); Object[] volumes = (Object[]) getVolumeList.invoke(sm); String[] paths = new String[volumes.length]; for (int ii=0; ii<volumes.length; ii++) { paths[ii] = (String) getPath.invoke(volumes[ii]); } return paths; } catch (Exception e) { if (DUMPSTACKS) Timber.e(e, "getStoragePathsCompat"); } Timber.w("Failed to get storage paths via reflection"); return new String[0]; }
@Override public List<Compilation> performTask() throws Throwable { List<String> pathList = new ArrayList<>(); StorageManager sm = (StorageManager) WikipediaApp.getInstance().getSystemService(Context.STORAGE_SERVICE); try { String[] volumes = (String[]) sm.getClass().getMethod("getVolumePaths").invoke(sm); if (volumes != null && volumes.length > 0) { pathList.addAll(Arrays.asList(volumes)); } } catch (Exception e) { L.e(e); } if (pathList.size() == 0 && Environment.getExternalStorageDirectory() != null) { pathList.add(Environment.getExternalStorageDirectory().getAbsolutePath()); } for (String path : pathList) { findCompilations(new File(path), 0); if (isCancelled()) { break; } } return compilations; }
public static long getInternalStorageSize(Context context) { long j = 0; try { String[] paths = (String[]) StorageManager.class.getMethod("getVolumePaths", new Class[0]).invoke((StorageManager) context.getSystemService("storage"), new Object[0]); if (paths != null && paths.length >= 1) { StatFs statFs = new StatFs(paths[0]); j = ((((long) statFs.getBlockCount()) * ((long) statFs.getBlockSize())) / 1024) / 1024; } } catch (Exception e) { LogTool.e(TAG, "getInternalStorageSize. " + e.toString()); } return j; }
public static long getExternalStorageSize(Context context) { long j = 0; try { String[] paths = (String[]) StorageManager.class.getMethod("getVolumePaths", new Class[0]).invoke((StorageManager) context.getSystemService("storage"), new Object[0]); if (paths != null && paths.length >= 2) { StatFs statFs = new StatFs(paths[1]); j = ((((long) statFs.getBlockCount()) * ((long) statFs.getBlockSize())) / 1024) / 1024; } } catch (Exception e) { LogTool.e(TAG, "getExternalStorageSize. " + e.toString()); } return j; }
public static String[] getAllPoints(Context context) { StorageManager manager = (StorageManager) context.getSystemService(Activity.STORAGE_SERVICE); String[] points = null; try { Method method = manager.getClass().getMethod("getVolumePaths"); points = (String[]) method.invoke(manager); } catch (Exception e) { e.printStackTrace(); } return points; }
public static boolean isMounted(Context context, String point) { if (point == null) return false; StorageManager manager = (StorageManager) context.getSystemService(Activity.STORAGE_SERVICE); try { Method method = manager.getClass().getMethod("getVolumeState", String.class); String state = (String) method.invoke(manager, point); return Environment.MEDIA_MOUNTED.equals(state); } catch (Exception e) { e.printStackTrace(); } return false; }
@Override @TargetApi(26) public ParcelFileDescriptor openProxyFile( String uri, String mode, StorageManager storageManager, ByteBufferPool bufferPool, @Nullable OnTaskFinishedCallback<String> callback) throws IOException { SambaFile file = openFileRaw(uri, mode); return storageManager.openProxyFileDescriptor( ParcelFileDescriptor.parseMode(mode), new SambaProxyFileCallback(uri, file, bufferPool, callback), mHandler); }
@TargetApi(26) ParcelFileDescriptor openProxyFile( String uri, String mode, StorageManager storageManager, ByteBufferPool bufferPool, @Nullable OnTaskFinishedCallback<String> callback) throws IOException;
@Override public boolean onCreate() { final Context context = getContext(); SambaProviderApplication.init(getContext()); mClient = SambaProviderApplication.getSambaClient(context); mCache = SambaProviderApplication.getDocumentCache(context); mTaskManager = SambaProviderApplication.getTaskManager(context); mBufferPool = new ByteBufferPool(); mShareManager = SambaProviderApplication.getServerManager(context); mShareManager.addListener(mShareChangeListener); mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); return mClient != null; }
/** * 获取外置SD卡的路径 */ private static String getRemovableStoragePath(Context context) { StorageManager mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); Class<?> storageVolumeClazz; try { storageVolumeClazz = Class.forName("android.os.storage.StorageVolume"); Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList"); Method getPath = storageVolumeClazz.getMethod("getPath"); Method isRemovable = storageVolumeClazz.getMethod("isRemovable"); Object result = getVolumeList.invoke(mStorageManager); final int length = Array.getLength(result); for (int i = 0; i < length; i++) { Object storageVolumeElement = Array.get(result, i); String path = (String) getPath.invoke(storageVolumeElement); boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement); if (removable) { File file = new File(path); if(!file.exists() || file.length() == 0){ return null; } return path; } } } catch (Exception e) { e.printStackTrace(); } return null; }
private static void printStorageManagerMethods(){ try { Method[] methods = StorageManager.class.getMethods(); for(Method method: methods){ System.out.println("storage_method : methodName = " + method.getName() + " methodParams = " + getParams(method.getParameterTypes()) + " returnType = " + method.getReturnType().getSimpleName()); } }catch (Exception e){ e.printStackTrace(); } }
private static void printStorageVolumeMethods(Context context){ try { StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); Method method = StorageManager.class.getMethod("getVolumeList"); StorageVolume[] storageVolumes = (StorageVolume[]) method.invoke(storageManager); System.out.println("storageVolumes : " + storageVolumes.length); Method[] methods = storageVolumes[0].getClass().getMethods(); for(Method m : methods){ System.out.println("storageVolumes : methodName = " + m.getName() + " methodParams = " + getParams(m.getParameterTypes()) + " returnType = " + m.getReturnType().getSimpleName()); } }catch (Exception e){ e.printStackTrace(); } }
public static StorageVolume[] getStorageVolumes(Context context){ StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); try { Method method = StorageManager.class.getMethod("getVolumeList"); return (StorageVolume[]) method.invoke(storageManager); }catch (Exception e){ e.printStackTrace(); } return null; }
public static void formatSDcard(Context context) { StorageManager storageManager = StorageManager.from(context); final StorageVolume[] storageVolumes = storageManager.getVolumeList(); Intent intent = new Intent(ExternalStorageFormatter.FORMAT_ONLY); intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME); intent.putExtra(StorageVolume.EXTRA_STORAGE_VOLUME, storageVolumes[1]); context.startService(intent); }
/**API14 * * @param context * @return sdlist 0-->inner 1--->external */ private static String[] getSDCards(Context context) { String [] ret = new String [2]; String exteranl_sd="", inner_sd=""; try { Class StorageVolume = Class.forName("android.os.storage.StorageVolume"); StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); Method getVolumeList = storageManager.getClass().getMethod("getVolumeList"); Method isRemovable = StorageVolume.getMethod("isRemovable"); Method getPath = StorageVolume.getMethod("getPath"); Object[] volumes = (Object[]) getVolumeList.invoke(storageManager); if(volumes == null) return ret; for(int i=0; i<volumes.length; i++) { if((Boolean)(isRemovable.invoke(volumes[i]))) { final String temp = (String)getPath.invoke(volumes[i]); if(!temp.contains("usb")) { exteranl_sd = temp; } }else { inner_sd = (String)getPath.invoke(volumes[i]); } } } catch (Exception e) { e.printStackTrace(); } ret[0] = inner_sd; ret[1] = exteranl_sd; return ret; }
/** * 获取当前存储分区的状态 * @param context * @return */ private String getState(Context context){ File file = new File(mPath); String state = EnvironmentCompat.MEDIA_UNKNOWN; if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ // 5.0+ state = Environment.getExternalStorageState(file); }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ // 4.4+ state = Environment.getStorageState(file); }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){ // 4.0-4.4. StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); try{ Method m = sm.getClass().getMethod("getVolumeState", String.class); m.setAccessible(true); state = (String)m.invoke(sm, mPath); }catch (Exception e){ // reflection failed if(file.canRead() && file.getTotalSpace() > 0){ state = Environment.MEDIA_MOUNTED; }else { state = EnvironmentCompat.MEDIA_UNKNOWN; } } } return state; }
private ObbExpansionsManager(Context context, final ObbListener listener) { AliteLog.d(TAG, "Creating new instance..."); packageName = context.getPackageName(); AliteLog.d(TAG, "Package name = " + packageName); packageVersion = AliteConfig.EXTENSION_FILE_VERSION; AliteLog.d(TAG, "Package version = " + packageVersion); this.listener = listener; sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); mainFile = new File(Environment.getExternalStorageDirectory() + "/Android/obb/" + packageName + "/" + "main." + packageVersion + "." + packageName + ".obb"); AliteLog.d(TAG, "Check if main file already mounted: " + sm.isObbMounted(mainFile.getAbsolutePath())); if (sm.isObbMounted(mainFile.getAbsolutePath())) { AliteLog.d(TAG, "Main file already mounted."); main = sm.getMountedObbPath(mainFile.getAbsolutePath()); listener.onMountSuccess(); } else { mountMain(); } if (!mainFile.exists()) { AliteLog.d(TAG, "No expansion files found!"); listener.onFilesNotFound(); } }
public static String getVolumeState(StorageManager manager, String s) { try { Method method = StorageManager.class.getMethod("getVolumeState", String.class); return (String) method.invoke(manager, s); } catch (Exception e) { e.printStackTrace(); } return null; }
public static String[] getVolumes(StorageManager manager) { try { Method method = StorageManager.class.getMethod("getVolumePaths"); return (String[]) method.invoke(manager, (Object[])null); } catch (Exception e) { e.printStackTrace(); } return null; }
public static String[] getMountedVolumes(StorageManager manager) { ArrayList<String> mVols = new ArrayList<>(); String[] vols = getVolumes(manager); for (String str : vols) { if (Environment.MEDIA_MOUNTED.equals(getVolumeState(manager, str))) { mVols.add(str); } } return mVols.toArray(new String[mVols.size()]); }
/** * 获取所有已挂载的存储设备 * * @param manager 存储设备管理器 * @return 所有已挂载的存储设备 */ public static List<StorageVolumeImpl> getEmulatedStorageVolumes(StorageManager manager) { List<StorageVolumeImpl> storageVolumes = getStorageVolumes(manager); if (storageVolumes != null) { for (int i = 0; i < storageVolumes.size(); ) { if (storageVolumes.get(i).isEmulated()) { i++; continue; } storageVolumes.remove(i); } } return storageVolumes; }
@Implementation public Object getSystemService(String name) { if (name.equals(Context.LAYOUT_INFLATER_SERVICE)) { return new RoboLayoutInflater(realContextImpl); } Object service = systemServices.get(name); if (service == null) { String serviceClassName = SYSTEM_SERVICE_MAP.get(name); if (serviceClassName == null) { System.err.println("WARNING: unknown service " + name); return null; } try { if (serviceClassName.equals("android.app.SearchManager")) { service = constructor().withParameterTypes(Context.class, Handler.class).in(SearchManager.class).newInstance(realContextImpl, null); } else if (serviceClassName.equals("android.app.ActivityManager")) { service = constructor().withParameterTypes(Context.class, Handler.class).in(ActivityManager.class).newInstance(realContextImpl, null); } else if (serviceClassName.equals("android.app.admin.DevicePolicyManager")) { service = constructor().withParameterTypes(Context.class, Handler.class).in(DevicePolicyManager.class).newInstance(realContextImpl, null); } else if (serviceClassName.equals("android.os.storage.StorageManager")) { service = constructor().in(StorageManager.class).newInstance(); } else if ((sdkConfig.getApiLevel() >= Build.VERSION_CODES.JELLY_BEAN_MR1) && (serviceClassName.equals("android.view.WindowManagerImpl"))) { Display display = newInstanceOf(Display.class); service = constructor().withParameterTypes(Display.class).in(Class.forName("android.view.WindowManagerImpl")).newInstance(display); } else { service = newInstanceOf(Class.forName(serviceClassName)); } } catch (ClassNotFoundException e) { throw new RuntimeException(e); } systemServices.put(name, service); } return service; }
List<StorageVolume> lookupStorageVolumes() { try { StorageManager sm = (StorageManager) appContext.getSystemService(Context.STORAGE_SERVICE); Method getVolumeList = StorageManager.class.getDeclaredMethod("getVolumeList"); Class<?> volume = Class.forName("android.os.storage.StorageVolume"); Method getPath = volume.getDeclaredMethod("getPath"); Method getDescription; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { getDescription = volume.getDeclaredMethod("getDescription", Context.class); } else { getDescription = volume.getDeclaredMethod("getDescription"); } Method getStorageId = volume.getDeclaredMethod("getStorageId"); Object[] volumes = (Object[]) getVolumeList.invoke(sm); List<StorageVolume> storages = new ArrayList<>(volumes.length); for (Object v : volumes) { StorageVolume storageVolume; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { storageVolume = new StorageVolume((String) getPath.invoke(v), (String) getDescription.invoke(v, appContext), (Integer) getStorageId.invoke(v)); } else { storageVolume = new StorageVolume((String) getPath.invoke(v), (String) getDescription.invoke(v), (Integer) getStorageId.invoke(v)); } Timber.d("Found volume %s", storageVolume); storages.add(storageVolume); } return storages; } catch (Exception e) { if (DUMPSTACKS) Timber.e(e, "lookupStorageVolumes"); } Timber.w("Failed to get storage paths via reflection"); return Collections.emptyList(); }
private String externalStorage(final Context context) { final StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); final String primaryVolumePath = primaryVolumePath(storageManager); final String[] volumePaths = volumePaths(storageManager); final ArrayList<String> volumePathsList = new ArrayList<>(); final String path = Environment.getExternalStorageDirectory().getAbsolutePath(); final int i = volumePaths == null ? 0 : volumePaths.length; for (int j = 0; j < i; j++) { String volumePath = volumePaths[j]; if ((volumePath.equals(System.getenv("EMULATED_STORAGE_SOURCE"))) || (volumePath.equals(System.getenv("EXTERNAL_STORAGE"))) || (volumePath.equals(path)) || (volumePath.equals(primaryVolumePath)) || (volumePath.toLowerCase().contains("usb"))) { continue; } volumePathsList.add(volumePath); } if (volumePathsList.size() == 1) { return volumePathsList.get(0); } return null; }