From bc1c4b3ec1893a25cb5f9e52185a19c9b330366e Mon Sep 17 00:00:00 2001 From: Eugene Sandulenko Date: Wed, 25 Sep 2019 20:37:20 +0200 Subject: ANDROID: Initial code for external storage enumerator --- .../org/scummvm/scummvm/ExternalStorage.java | 142 +++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 backends/platform/android/org/scummvm/scummvm/ExternalStorage.java (limited to 'backends/platform/android/org') diff --git a/backends/platform/android/org/scummvm/scummvm/ExternalStorage.java b/backends/platform/android/org/scummvm/scummvm/ExternalStorage.java new file mode 100644 index 0000000000..42ec185928 --- /dev/null +++ b/backends/platform/android/org/scummvm/scummvm/ExternalStorage.java @@ -0,0 +1,142 @@ +package org.scummvm.scummvm; + +import android.os.Environment; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +/** + * Contains helper methods to get list of available media + */ +public class ExternalStorage { + public static final String SD_CARD = "sdCard"; + public static final String EXTERNAL_SD_CARD = "externalSdCard"; + public static final String DATA_DIRECTORY = "ScummVM data directory"; + + /** + * @return True if the external storage is available. False otherwise. + */ + public static boolean isAvailable() { + String state = Environment.getExternalStorageState(); + if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { + return true; + } + return false; + } + + public static String getSdCardPath() { + return Environment.getExternalStorageDirectory().getPath() + "/"; + } + + /** + * @return True if the external storage is writable. False otherwise. + */ + public static boolean isWritable() { + String state = Environment.getExternalStorageState(); + if (Environment.MEDIA_MOUNTED.equals(state)) { + return true; + } + return false; + + } + + /** + * @return A map of all storage locations available + */ + public static Map getAllStorageLocations() { + Map map = new HashMap(10); + + List mMounts = new ArrayList(10); + List mVold = new ArrayList(10); + mMounts.add("/mnt/sdcard"); + mVold.add("/mnt/sdcard"); + + try { + File mountFile = new File("/proc/mounts"); + if (mountFile.exists()){ + Scanner scanner = new Scanner(mountFile); + while (scanner.hasNext()) { + String line = scanner.nextLine(); + if (line.startsWith("/dev/block/vold/")) { + String[] lineElements = line.split(" "); + String element = lineElements[1]; + + // don't add the default mount path + // it's already in the list. + if (!element.equals("/mnt/sdcard")) + mMounts.add(element); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + try { + File voldFile = new File("/system/etc/vold.fstab"); + if (voldFile.exists()){ + Scanner scanner = new Scanner(voldFile); + while (scanner.hasNext()) { + String line = scanner.nextLine(); + if (line.startsWith("dev_mount")) { + String[] lineElements = line.split(" "); + String element = lineElements[2]; + + if (element.contains(":")) + element = element.substring(0, element.indexOf(":")); + if (!element.equals("/mnt/sdcard")) + mVold.add(element); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + + for (int i = 0; i < mMounts.size(); i++) { + String mount = mMounts.get(i); + if (!mVold.contains(mount)) + mMounts.remove(i--); + } + mVold.clear(); + + List mountHash = new ArrayList(10); + + for (String mount : mMounts){ + File root = new File(mount); + if (root.exists() && root.isDirectory() && root.canRead()) { + File[] list = root.listFiles(); + String hash = "["; + if (list != null) { + for (File f : list) { + hash += f.getName().hashCode() + ":" + f.length() + ", "; + } + } + hash += "]"; + if (!mountHash.contains(hash)){ + String key = SD_CARD + "_" + map.size(); + if (map.size() == 0) { + key = SD_CARD; + } else if (map.size() == 1) { + key = EXTERNAL_SD_CARD; + } + mountHash.add(hash); + map.put(key, root.getAbsolutePath()); + } + } + } + + mMounts.clear(); + + map.put(SD_CARD, Environment.getDataDirectory().getAbsolutePath()); + + if (map.isEmpty()) { + map.put(DATA_DIRECTORY, Environment.getExternalStorageDirectory().getAbsolutePath()); + } + return map; + } +} -- cgit v1.2.3