From a31c59f6d19bb5fbf15fba475f6858d7fbfa5c44 Mon Sep 17 00:00:00 2001 From: Dmitry Yunitsky Date: Mon, 20 Oct 2014 17:23:17 +0300 Subject: [PATCH] Fix for bug with SD cards on Kitkat. --- .../maps/settings/StoragePathManager.java | 92 +++++++++++-------- .../src/com/mapswithme/util/Constants.java | 1 + 2 files changed, 54 insertions(+), 39 deletions(-) diff --git a/android/src/com/mapswithme/maps/settings/StoragePathManager.java b/android/src/com/mapswithme/maps/settings/StoragePathManager.java index 08e8616fa6..4d79c269c7 100644 --- a/android/src/com/mapswithme/maps/settings/StoragePathManager.java +++ b/android/src/com/mapswithme/maps/settings/StoragePathManager.java @@ -14,6 +14,7 @@ import android.os.Environment; import android.os.StatFs; import android.util.Log; +import com.mapswithme.maps.BuildConfig; import com.mapswithme.maps.Framework; import com.mapswithme.maps.MWMApplication; import com.mapswithme.maps.MapStorage; @@ -33,6 +34,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedHashSet; +import java.util.List; import java.util.Map; import java.util.Set; @@ -120,42 +122,35 @@ public class StoragePathManager return mItems.size() > 1; } + public ArrayList getStorageItems() + { + return mItems; + } + + public int getCurrentStorageIndex() + { + return mCurrentStorageIndex; + } + public void updateExternalStorages() { ArrayList paths = new ArrayList(); if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) - { - File[] files = MWMApplication.get().getExternalFilesDirs(null); - if (files != null) - { - File primaryStorageDir = MWMApplication.get().getExternalFilesDir(null); - for (File f : files) - { - // add only secondary dirs - if (f != null && !f.equals(primaryStorageDir)) - { - Log.i(TAG, "Additional storage path: " + f.getPath()); - paths.add(f.getPath()); - } - } - } - } + parseKitkatStorages(paths); + else + parseStorages(paths); - // Do it even on KitKat due to some bugs with getExternalFilesDirs() - parseMountFiles(paths); - - Map pathsSizesMap = new HashMap(); + Map pathsSizesMap = new HashMap<>(); // Add current path first! final String currentStorageDir = getWritableDirRoot(); addStoragePathWithSize(currentStorageDir, pathsSizesMap); - for (String path : paths) addStoragePathWithSize(path, pathsSizesMap); addStoragePathWithSize(Environment.getExternalStorageDirectory().getAbsolutePath(), pathsSizesMap); - mItems = new ArrayList(); + mItems = new ArrayList<>(); mCurrentStorageIndex = -1; for (Map.Entry entry : pathsSizesMap.entrySet()) { @@ -172,17 +167,40 @@ public class StoragePathManager } } - public ArrayList getStorageItems() + private static void parseKitkatStorages(List paths) { - return mItems; + File primaryStorage = MWMApplication.get().getExternalFilesDir(null); + File[] storages = MWMApplication.get().getExternalFilesDirs(null); + if (storages != null) + { + for (File f : storages) + { + // add only secondary dirs + if (f != null && !f.equals(primaryStorage)) + { + Log.i(TAG, "Additional storage path: " + f.getPath()); + paths.add(f.getPath()); + } + } + } + + ArrayList testStorages = new ArrayList<>(); + parseStorages(testStorages); + final String suffix = String.format(Constants.STORAGE_PATH, BuildConfig.PACKAGE_NAME, Constants.FILES_DIR); + for (String testStorage : testStorages) + { + if (isDirWritable(testStorage)) + paths.add(testStorage); + else + { + testStorage += suffix; + if (isDirWritable(testStorage)) + paths.add(testStorage); + } + } } - public int getCurrentStorageIndex() - { - return mCurrentStorageIndex; - } - - private static void parseMountFiles(ArrayList paths) + private static void parseStorages(ArrayList paths) { parseMountFile("/etc/vold.conf", VOLD_MODE, paths); parseMountFile("/etc/vold.fstab", VOLD_MODE, paths); @@ -215,7 +233,7 @@ public class StoragePathManager { ArrayList pathes = new ArrayList(); if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) - parseMountFiles(pathes); + parseStorages(pathes); ArrayList approvedPathes = new ArrayList(); for (String path : pathes) @@ -462,8 +480,7 @@ public class StoragePathManager if (!MapStorage.nativeMoveFile(moveFile.getAbsolutePath(), fullNewPath + moveFile.getName())) copyFile(moveFile, new File(fullNewPath + moveFile.getName())); } - } - catch (IOException e) + } catch (IOException e) { for (File moveFile : internalFiles) new File(fullNewPath + moveFile.getName()).delete(); @@ -559,13 +576,10 @@ public class StoragePathManager { final long size = getFreeBytesAtPath(path); - if (size > 0) + if (size > 0 && !sizesPaths.containsKey(size)) { - if (!sizesPaths.containsKey(size)) - { - Log.i(TAG, "Path added: " + path + ", size = " + size); - sizesPaths.put(size, path); - } + Log.i(TAG, "Path added: " + path + ", size = " + size); + sizesPaths.put(size, path); } } } catch (final IllegalArgumentException ex) diff --git a/android/src/com/mapswithme/util/Constants.java b/android/src/com/mapswithme/util/Constants.java index 1c8d03b6d6..377fab6c59 100644 --- a/android/src/com/mapswithme/util/Constants.java +++ b/android/src/com/mapswithme/util/Constants.java @@ -48,6 +48,7 @@ public class Constants public static final String MWM_DIR_POSTFIX = "/MapsWithMe/"; public static final String DEVICE_YOTAPHONE = "yotaphone"; public static final String CACHE_DIR = "cache"; + public static final String FILES_DIR = "files"; private Constants() {} }