From 3ef6b3a76adfcd8e915b9c3ad2a421ffc92dcbdb Mon Sep 17 00:00:00 2001 From: Anthony Calosa Date: Fri, 17 Jun 2016 16:58:15 +0800 Subject: [PATCH] Force Mounting 2 If you have root then most likely you have write access to secondary storage. If not then use the app-specific folder. You may create the folder via built-in file manager > Android/data/net.wagic.app/files/Wagic/Res --- .../src/net/wagic/utils/StorageOptions.java | 118 ++++++++++++++++-- 1 file changed, 110 insertions(+), 8 deletions(-) diff --git a/projects/mtg/Android/src/net/wagic/utils/StorageOptions.java b/projects/mtg/Android/src/net/wagic/utils/StorageOptions.java index e97ac68f1..95b712017 100644 --- a/projects/mtg/Android/src/net/wagic/utils/StorageOptions.java +++ b/projects/mtg/Android/src/net/wagic/utils/StorageOptions.java @@ -61,7 +61,7 @@ public class StorageOptions if (line.startsWith("/")) { String[] lineElements = line.split("\\s+"); - if ("vfat".equals(lineElements[2]) || "fuse".equals(lineElements[2])) + if ("vfat".equals(lineElements[2]) || "fuse".equals(lineElements[2]) || "sdcardfs".equals(lineElements[2])) { File mountPoint = new File(lineElements[1]); if (!lineElements[1].equals(defaultMountPoint)) @@ -201,13 +201,43 @@ public class StorageOptions mMounts.remove(i--); } - if (t == 0 && Build.VERSION.SDK_INT >= 19) - {//If none is found and build version is kitkat or higher - File root = new File("/storage/sdcard0"); - if (root.exists() && root.isDirectory() && root.canWrite()) - mMounts.add("/storage/sdcard0"); - if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) - mMounts.add("/storage/sdcard1"); + if (t == 0 && Build.VERSION.SDK_INT >= 16) + {//if none is found lets force it for Jellybean and above... + if (System.getenv("EXTERNAL_STORAGE") != null) + { + File root = new File(System.getenv("EXTERNAL_STORAGE")); + if (root.exists() && root.isDirectory() && root.canWrite()) + { + if(!isRooted()) + { + File folder = new File(System.getenv("EXTERNAL_STORAGE")+"/Android/data/net.wagic.app/files"); + folder.mkdirs(); + mMounts.add(folder.toString()); + } + else + { + mMounts.add(System.getenv("EXTERNAL_STORAGE")); + } + } + } + + if (System.getenv("SECONDARY_STORAGE") != null) + { + File root = new File(System.getenv("SECONDARY_STORAGE")); + if (root.exists() && root.isDirectory() && root.canWrite()) + { + if(!isRooted()) + { + File folder = new File(System.getenv("SECONDARY_STORAGE")+"/Android/data/net.wagic.app/files"); + folder.mkdirs(); + mMounts.add(folder.toString()); + } + else + { + mMounts.add(System.getenv("SECONDARY_STORAGE")); + } + } + } } } @@ -224,6 +254,8 @@ public class StorageOptions { // TODO: /mnt/sdcard is assumed to always mean internal storage. Use this comparison until there is a better way to do this if ("/mnt/sdcard".equalsIgnoreCase(path) || "/storage/sdcard0".equalsIgnoreCase(path)) mLabels.add("Internal SD " + "[" + path + "]"); + else if (path.contains("emulated")) + mLabels.add("Emulated SD " + " [" + path + "]"); else mLabels.add("External SD " + " [" + path + "]"); } @@ -256,4 +288,74 @@ public class StorageOptions } return false; } + + /** + * Checks if the device is rooted. + * + * @return true if the device is rooted, false otherwise. + */ + public static boolean isRooted() { + + // get from build info + String buildTags = android.os.Build.TAGS; + if (buildTags != null && buildTags.contains("test-keys")) { + return true; + } + + // check if /system/app/Superuser.apk is present + try { + File file = new File("/system/app/Superuser.apk"); + if (file.exists()) { + return true; + } + } + catch (Exception e1) { + // ignore + } + try { + File file = new File("/system/app/Superuser/Superuser.apk"); + if (file.exists()) { + return true; + } + } + catch (Exception e1) { + // ignore + } + //SuperSU + try { + File file = new File("/system/app/SuperSU.apk"); + if (file.exists()) { + return true; + } + } + catch (Exception e1) { + // ignore + } + try { + File file = new File("/system/app/SuperSU/SuperSU.apk"); + if (file.exists()) { + return true; + } + } + catch (Exception e1) { + // ignore + } + // try executing commands + return canExecuteCommand("/system/xbin/which su") + || canExecuteCommand("/system/bin/which su") || canExecuteCommand("which su"); + } + + // executes a command on the system + private static boolean canExecuteCommand(String command) { + boolean executedSuccesfully; + try { + Runtime.getRuntime().exec(command); + executedSuccesfully = true; + } + catch (Exception e) { + executedSuccesfully = false; + } + + return executedSuccesfully; + } }