diff options
author | Alexander Marchuk <alexm@maps.me> | 2016-06-10 20:51:35 +0300 |
---|---|---|
committer | Vladimir Byko-Ianko <v.bykoianko@corp.mail.ru> | 2016-06-24 13:31:42 +0300 |
commit | f9504988c85942805e7afe7ec22bc76950d3929d (patch) | |
tree | b66a77878168e711e7d4d0dad8bef5803fcd50b4 /android | |
parent | 8def243d9952ae6728f6cbe64fafa5602085a628 (diff) |
[android] fix: Try to find map storage if the saved location is invalid.
Diffstat (limited to 'android')
12 files changed, 200 insertions, 102 deletions
diff --git a/android/jni/com/mapswithme/maps/Framework.cpp b/android/jni/com/mapswithme/maps/Framework.cpp index 2c1bcdb635..a748bafa03 100644 --- a/android/jni/com/mapswithme/maps/Framework.cpp +++ b/android/jni/com/mapswithme/maps/Framework.cpp @@ -727,7 +727,7 @@ Java_com_mapswithme_maps_Framework_nativeSetWritableDir(JNIEnv * env, jclass, js { string newPath = jni::ToNativeString(env, jNewPath); g_framework->RemoveLocalMaps(); - android::Platform::Instance().SetStoragePath(newPath); + android::Platform::Instance().SetWritableDir(newPath); g_framework->AddLocalMaps(); } diff --git a/android/jni/com/mapswithme/maps/MwmApplication.cpp b/android/jni/com/mapswithme/maps/MwmApplication.cpp index 27ce1b9b7d..f05cce95b1 100644 --- a/android/jni/com/mapswithme/maps/MwmApplication.cpp +++ b/android/jni/com/mapswithme/maps/MwmApplication.cpp @@ -9,6 +9,14 @@ crashlytics_context_t * g_crashlytics; extern "C" { + // static void nativePreparePlatform(String settingsPath); + JNIEXPORT void JNICALL + Java_com_mapswithme_maps_MwmApplication_nativePreparePlatform(JNIEnv * env, jclass clazz, jstring settingsPath) + { + android::Platform::Instance().SetSettingsDir(jni::ToNativeString(env, settingsPath)); + } + + // void nativeInitPlatform(String apkPath, String storagePath, String tmpPath, String obbGooglePath, String flavorName, String buildType, boolean isTablet); JNIEXPORT void JNICALL Java_com_mapswithme_maps_MwmApplication_nativeInitPlatform(JNIEnv * env, jobject thiz, jstring apkPath, jstring storagePath, jstring tmpPath, jstring obbGooglePath, jstring flavorName, jstring buildType, jboolean isTablet) @@ -16,6 +24,7 @@ extern "C" android::Platform::Instance().Initialize(env, thiz, apkPath, storagePath, tmpPath, obbGooglePath, flavorName, buildType, isTablet); } + // static void nativeInitFramework(); JNIEXPORT void JNICALL Java_com_mapswithme_maps_MwmApplication_nativeInitFramework(JNIEnv * env, jclass clazz) { @@ -23,12 +32,14 @@ extern "C" g_framework = new android::Framework(); } + // static void nativeProcessFunctor(long functorPointer); JNIEXPORT void JNICALL Java_com_mapswithme_maps_MwmApplication_nativeProcessFunctor(JNIEnv * env, jclass clazz, jlong functorPointer) { android::Platform::Instance().ProcessFunctor(functorPointer); } + // static void nativeAddLocalization(String name, String value); JNIEXPORT void JNICALL Java_com_mapswithme_maps_MwmApplication_nativeAddLocalization(JNIEnv * env, jclass clazz, jstring name, jstring value) { @@ -36,6 +47,8 @@ extern "C" jni::ToNativeString(env, value)); } + // @UiThread + // static void nativeInitCrashlytics(); JNIEXPORT void JNICALL Java_com_mapswithme_maps_MwmApplication_nativeInitCrashlytics(JNIEnv * env, jclass clazz) { diff --git a/android/jni/com/mapswithme/platform/Platform.cpp b/android/jni/com/mapswithme/platform/Platform.cpp index c90ae8a37c..6b0b1d76ce 100644 --- a/android/jni/com/mapswithme/platform/Platform.cpp +++ b/android/jni/com/mapswithme/platform/Platform.cpp @@ -111,13 +111,8 @@ namespace android m_isTablet = isTablet; m_resourcesDir = jni::ToNativeString(env, apkPath); - // Settings file should be in a one place always (default external storage). - m_settingsDir = jni::ToNativeString(env, storagePath); m_tmpDir = jni::ToNativeString(env, tmpPath); - - // Custom storage isn't set. Use primary storage. - if (!settings::Get("StoragePath", m_writableDir)) - m_writableDir = m_settingsDir; + m_writableDir = jni::ToNativeString(env, storagePath); string const obbPath = jni::ToNativeString(env, obbGooglePath); Platform::FilesList files; @@ -129,7 +124,6 @@ namespace android LOG(LINFO, ("Apk path = ", m_resourcesDir)); LOG(LINFO, ("Writable path = ", m_writableDir)); LOG(LINFO, ("Temporary path = ", m_tmpDir)); - LOG(LINFO, ("Settings path = ", m_settingsDir)); LOG(LINFO, ("OBB Google path = ", obbPath)); LOG(LINFO, ("OBB Google files = ", files)); @@ -159,10 +153,17 @@ namespace android return m_writableDir.substr(0, i); } - void Platform::SetStoragePath(string const & path) + void Platform::SetWritableDir(string const & dir) { - m_writableDir = path; + m_writableDir = dir; settings::Set("StoragePath", m_writableDir); + LOG(LINFO, ("Writable path = ", m_writableDir)); + } + + void Platform::SetSettingsDir(string const & dir) + { + m_settingsDir = dir; + LOG(LINFO, ("Settings path = ", m_settingsDir)); } bool Platform::HasAvailableSpaceForWriting(uint64_t size) const diff --git a/android/jni/com/mapswithme/platform/Platform.hpp b/android/jni/com/mapswithme/platform/Platform.hpp index 8ee36a66fe..2b11f6c9a5 100644 --- a/android/jni/com/mapswithme/platform/Platform.hpp +++ b/android/jni/com/mapswithme/platform/Platform.hpp @@ -23,7 +23,8 @@ namespace android /// get storage path without ending "/MapsWithMe/" string GetStoragePathPrefix() const; /// assign storage path (should contain ending "/MapsWithMe/") - void SetStoragePath(string const & path); + void SetWritableDir(string const & dir); + void SetSettingsDir(string const & dir); bool HasAvailableSpaceForWriting(uint64_t size) const; void RunOnGuiThread(TFunctor const & fn); diff --git a/android/src/com/mapswithme/maps/MwmApplication.java b/android/src/com/mapswithme/maps/MwmApplication.java index 61e6622b93..24eff6cbd8 100644 --- a/android/src/com/mapswithme/maps/MwmApplication.java +++ b/android/src/com/mapswithme/maps/MwmApplication.java @@ -24,6 +24,7 @@ import com.mapswithme.maps.downloader.MapManager; import com.mapswithme.maps.editor.Editor; import com.mapswithme.maps.location.TrackRecorder; import com.mapswithme.maps.routing.RoutingController; +import com.mapswithme.maps.settings.StoragePathManager; import com.mapswithme.maps.sound.TtsPlayer; import com.mapswithme.util.Config; import com.mapswithme.util.Constants; @@ -31,7 +32,6 @@ import com.mapswithme.util.ThemeSwitcher; import com.mapswithme.util.UiUtils; import com.mapswithme.util.statistics.Statistics; import com.pushwoosh.PushManager; - import io.fabric.sdk.android.Fabric; import net.hockeyapp.android.CrashManager; @@ -96,6 +96,7 @@ public class MwmApplication extends Application return sSelf.mPrefs; } + @SuppressWarnings("ResultOfMethodCallIgnored") @Override public void onCreate() { @@ -106,8 +107,12 @@ public class MwmApplication extends Application initCrashlytics(); initPushWoosh(); - initPaths(); - nativeInitPlatform(getApkPath(), getDataStoragePath(), getTempPath(), getObbGooglePath(), + String settingsPath = getSettingsPath(); + new File(settingsPath).mkdirs(); + new File(getTempPath()).mkdirs(); + + nativePreparePlatform(settingsPath); + nativeInitPlatform(getApkPath(), getStoragePath(settingsPath), getTempPath(), getObbGooglePath(), BuildConfig.FLAVOR, BuildConfig.BUILD_TYPE, UiUtils.isTablet()); mPrefs = getSharedPreferences(getString(R.string.pref_file_name), MODE_PRIVATE); @@ -133,13 +138,6 @@ public class MwmApplication extends Application mIsFrameworkInitialized = true; } - @SuppressWarnings("ResultOfMethodCallIgnored") - private void initPaths() - { - new File(getDataStoragePath()).mkdirs(); - new File(getTempPath()).mkdirs(); - } - private void initNativeStrings() { nativeAddLocalization("country_status_added_to_queue", getString(R.string.country_status_added_to_queue)); @@ -164,7 +162,6 @@ public class MwmApplication extends Application nativeAddLocalization("routing_failed_route_not_found", getString(R.string.routing_failed_route_not_found)); nativeAddLocalization("routing_failed_internal_error", getString(R.string.routing_failed_internal_error)); nativeAddLocalization("place_page_booking_rating", getString(R.string.place_page_booking_rating)); - } private void initHockeyApp() @@ -201,11 +198,28 @@ public class MwmApplication extends Application } } - public static String getDataStoragePath() + public static String getSettingsPath() { return Environment.getExternalStorageDirectory().getAbsolutePath() + Constants.MWM_DIR_POSTFIX; } + private static String getStoragePath(String settingsPath) + { + String path = Config.getStoragePath(); + if (!TextUtils.isEmpty(path)) + { + File f = new File(path); + if (f.exists() && f.isDirectory()) + return path; + + path = new StoragePathManager().findMapsMeStorage(settingsPath); + Config.setStoragePath(path); + return path; + } + + return settingsPath; + } + public String getTempPath() { final File cacheDir = getExternalCacheDir(); @@ -213,7 +227,7 @@ public class MwmApplication extends Application return cacheDir.getAbsolutePath(); return Environment.getExternalStorageDirectory().getAbsolutePath() + - String.format(Constants.STORAGE_PATH, BuildConfig.APPLICATION_ID, Constants.CACHE_DIR); + String.format(Constants.STORAGE_PATH, BuildConfig.APPLICATION_ID, Constants.CACHE_DIR); } private static String getObbGooglePath() @@ -276,17 +290,13 @@ public class MwmApplication extends Application mMainLoopHandler.sendMessage(m); } - /** - * Initializes native Platform with paths. Should be called before usage of any other native components. - */ + private static native void nativePreparePlatform(String settingsPath); private native void nativeInitPlatform(String apkPath, String storagePath, String tmpPath, String obbGooglePath, String flavorName, String buildType, boolean isTablet); private static native void nativeInitFramework(); - - private static native void nativeAddLocalization(String name, String value); - private static native void nativeProcessFunctor(long functorPointer); + private static native void nativeAddLocalization(String name, String value); @UiThread private static native void nativeInitCrashlytics(); diff --git a/android/src/com/mapswithme/maps/settings/MapPrefsFragment.java b/android/src/com/mapswithme/maps/settings/MapPrefsFragment.java index 2603ef78d9..1f783436bd 100644 --- a/android/src/com/mapswithme/maps/settings/MapPrefsFragment.java +++ b/android/src/com/mapswithme/maps/settings/MapPrefsFragment.java @@ -1,7 +1,6 @@ package com.mapswithme.maps.settings; import android.app.Activity; -import android.content.Intent; import android.os.Bundle; import android.preference.ListPreference; import android.preference.Preference; diff --git a/android/src/com/mapswithme/maps/settings/StoragePathAdapter.java b/android/src/com/mapswithme/maps/settings/StoragePathAdapter.java index aad7a903f7..1bb341a949 100644 --- a/android/src/com/mapswithme/maps/settings/StoragePathAdapter.java +++ b/android/src/com/mapswithme/maps/settings/StoragePathAdapter.java @@ -6,20 +6,22 @@ import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckedTextView; -import com.mapswithme.maps.R; +import java.util.ArrayList; import java.util.List; +import com.mapswithme.maps.R; + class StoragePathAdapter extends BaseAdapter { - private StoragePathManager mStoragePathManager; + private final StoragePathManager mStoragePathManager; private final LayoutInflater mInflater; - private List<StorageItem> mItems; + private final List<StorageItem> mItems = new ArrayList<>(); private int mCurrentStorageIndex = -1; private long mSizeNeeded; - public StoragePathAdapter(StoragePathManager storagePathManager, Activity context) + StoragePathAdapter(StoragePathManager storagePathManager, Activity context) { mStoragePathManager = storagePathManager; mInflater = context.getLayoutInflater(); @@ -67,7 +69,8 @@ class StoragePathAdapter extends BaseAdapter public void update(List<StorageItem> items, int currentItemIndex, long dirSize) { mSizeNeeded = dirSize; - mItems = items; + mItems.clear(); + mItems.addAll(items); mCurrentStorageIndex = currentItemIndex; notifyDataSetChanged(); diff --git a/android/src/com/mapswithme/maps/settings/StoragePathFragment.java b/android/src/com/mapswithme/maps/settings/StoragePathFragment.java index d62300c705..4a5a50ed83 100644 --- a/android/src/com/mapswithme/maps/settings/StoragePathFragment.java +++ b/android/src/com/mapswithme/maps/settings/StoragePathFragment.java @@ -10,14 +10,16 @@ import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; + +import java.util.List; +import java.util.Locale; + import com.mapswithme.maps.R; import com.mapswithme.maps.base.OnBackPressListener; import com.mapswithme.maps.widget.BaseShadowController; import com.mapswithme.util.Constants; import com.mapswithme.util.Utils; -import java.util.List; - public class StoragePathFragment extends BaseSettingsFragment implements StoragePathManager.MoveFilesListener, OnBackPressListener @@ -26,7 +28,7 @@ public class StoragePathFragment extends BaseSettingsFragment private ListView mList; private StoragePathAdapter mAdapter; - private StoragePathManager mPathManager = new StoragePathManager(); + private final StoragePathManager mPathManager = new StoragePathManager(); @Override protected int getLayoutRes() @@ -140,7 +142,7 @@ public class StoragePathFragment extends BaseSettingsFragment } // left 1 digit after the comma and add postfix string - return String.format("%.1f %s", (double) size / current, units[i]); + return String.format(Locale.US, "%.1f %s", (double) size / current, units[i]); } @Override diff --git a/android/src/com/mapswithme/maps/settings/StoragePathManager.java b/android/src/com/mapswithme/maps/settings/StoragePathManager.java index 37f5c8f870..ee4b0b2d3f 100644 --- a/android/src/com/mapswithme/maps/settings/StoragePathManager.java +++ b/android/src/com/mapswithme/maps/settings/StoragePathManager.java @@ -15,10 +15,13 @@ import android.support.v7.app.AlertDialog; import android.util.Log; import java.io.File; +import java.io.FileFilter; import java.io.FilenameFilter; import java.io.IOException; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import com.mapswithme.maps.BuildConfig; import com.mapswithme.maps.Framework; @@ -31,7 +34,7 @@ import com.mapswithme.util.concurrency.UiThread; public class StoragePathManager { - static final String[] MOVABLE_EXTS = Framework.nativeGetMovableFilesExts(); + private static final String[] MOVABLE_EXTS = Framework.nativeGetMovableFilesExts(); static final FilenameFilter MOVABLE_FILES_FILTER = new FilenameFilter() { @Override @@ -45,14 +48,14 @@ public class StoragePathManager } }; - public interface MoveFilesListener + interface MoveFilesListener { void moveFilesFinished(String newPath); void moveFilesFailed(int errorCode); } - public interface OnStorageListChangedListener + interface OnStorageListChangedListener { void onStorageListChanged(List<StorageItem> storageItems, int currentStorageIndex); } @@ -71,13 +74,13 @@ public class StoragePathManager private BroadcastReceiver mInternalReceiver; private Activity mActivity; - private List<StorageItem> mItems; + private final List<StorageItem> mItems = new ArrayList<>(); private int mCurrentStorageIndex = -1; /** * Observes status of connected media and retrieves list of available external storages. */ - public void startExternalStorageWatching(Activity activity, final @Nullable OnStorageListChangedListener storagesChangedListener, @Nullable MoveFilesListener moveFilesListener) + void startExternalStorageWatching(Activity activity, final @Nullable OnStorageListChangedListener storagesChangedListener, @Nullable MoveFilesListener moveFilesListener) { mActivity = activity; mStoragesChangedListener = storagesChangedListener; @@ -98,7 +101,7 @@ public class StoragePathManager updateExternalStorages(); } - public static IntentFilter getMediaChangesIntentFilter() + private static IntentFilter getMediaChangesIntentFilter() { final IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_MEDIA_MOUNTED); @@ -115,7 +118,7 @@ public class StoragePathManager return filter; } - public void stopExternalStorageWatching() + void stopExternalStorageWatching() { if (mInternalReceiver != null) { @@ -125,33 +128,38 @@ public class StoragePathManager } } - public boolean hasMoreThanOneStorage() + boolean hasMoreThanOneStorage() { return mItems.size() > 1; } - public List<StorageItem> getStorageItems() + List<StorageItem> getStorageItems() { return mItems; } - public int getCurrentStorageIndex() + int getCurrentStorageIndex() { return mCurrentStorageIndex; } - public void updateExternalStorages() + private void updateExternalStorages() { - List<String> pathsFromConfig = new ArrayList<>(); + updateExternalStorages(StorageUtils.getWritableDirRoot()); + } + + private void updateExternalStorages(String writableDir) + { + Set<String> pathsFromConfig = new HashSet<>(); if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) StorageUtils.parseKitkatStorages(pathsFromConfig); else StorageUtils.parseStorages(pathsFromConfig); - mItems = new ArrayList<>(); + mItems.clear(); - final StorageItem currentStorage = buildStorageItem(StorageUtils.getWritableDirRoot()); + final StorageItem currentStorage = buildStorageItem(writableDir); addStorageItem(currentStorage); addStorageItem(buildStorageItem(Environment.getExternalStorageDirectory().getAbsolutePath())); for (String path : pathsFromConfig) @@ -174,7 +182,7 @@ public class StoragePathManager mItems.add(item); } - private StorageItem buildStorageItem(String path) + private static StorageItem buildStorageItem(String path) { try { @@ -196,7 +204,7 @@ public class StoragePathManager return null; } - protected void changeStorage(int newIndex) + void changeStorage(int newIndex) { final StorageItem oldItem = (mCurrentStorageIndex != -1) ? mItems.get(mCurrentStorageIndex) : null; final StorageItem item = mItems.get(newIndex); @@ -235,11 +243,8 @@ public class StoragePathManager mMoveFilesListener.moveFilesFailed(errorCode); } }, item, oldItem, R.string.wait_several_minutes); - - dlg.dismiss(); } - }) - .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() + }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dlg, int which) @@ -281,6 +286,60 @@ public class StoragePathManager }); } + /** + * Dumb way to determine whether the storage contains Maps.me data. + * <p>The algorithm is quite simple: + * <ul> + * <li>Find all writable storages;</li> + * <li>For each storage list sub-dirs under "MapsWithMe" dir;</li> + * <li>If there is a directory with version-like name (e.g. "160602")…</li> + * <li>…and it is not empty…</li> + * <li>…we got it!</li> + * </ul> + */ + private static boolean containsMapData(String storagePath) + { + File path = new File(storagePath); + File[] candidates = path.listFiles(new FileFilter() + { + @Override + public boolean accept(File pathname) + { + if (!pathname.isDirectory()) + return false; + + try + { + String name = pathname.getName(); + if (name.length() != 6) + return false; + + int version = Integer.valueOf(name); + return (version > 120000 && version <= 999999); + } catch (NumberFormatException ignored) {} + + return false; + } + }); + + return (candidates != null && candidates.length > 0 && + candidates[0].list().length > 0); + } + + public String findMapsMeStorage(String settingsPath) + { + updateExternalStorages(settingsPath); + List<StorageItem> items = getStorageItems(); + + for (StorageItem item : items) + { + if (containsMapData(item.mPath)) + return item.mPath; + } + + return settingsPath; + } + private void checkExternalStoragePathOnKitkat(Context context, MoveFilesListener listener) { final String settingsDir = Framework.nativeGetSettingsDir(); @@ -383,18 +442,17 @@ public class StoragePathManager { for (int i = 0; i < oldFiles.length; ++i) { - if (!MapManager.nativeMoveFile(oldFiles[i].getAbsolutePath(), newFiles[i].getAbsolutePath())) + if (MapManager.nativeMoveFile(oldFiles[i].getAbsolutePath(), newFiles[i].getAbsolutePath())) + { + // No need to delete oldFiles[i] because it was moved to newFiles[i]. + oldFiles[i] = null; + } else { File parent = newFiles[i].getParentFile(); if (parent != null) parent.mkdirs(); StorageUtils.copyFile(oldFiles[i], newFiles[i]); } - else - { - // No need to delete oldFiles[i] because it was moved to newFiles[i]. - oldFiles[i] = null; - } } } catch (IOException e) { diff --git a/android/src/com/mapswithme/maps/settings/StorageUtils.java b/android/src/com/mapswithme/maps/settings/StorageUtils.java index c629b6ba21..c04bd9cc21 100644 --- a/android/src/com/mapswithme/maps/settings/StorageUtils.java +++ b/android/src/com/mapswithme/maps/settings/StorageUtils.java @@ -2,6 +2,7 @@ package com.mapswithme.maps.settings; import android.annotation.TargetApi; import android.os.Build; +import android.text.TextUtils; import android.util.Log; import java.io.BufferedReader; @@ -13,7 +14,8 @@ import java.io.FilenameFilter; import java.io.IOException; import java.nio.channels.FileChannel; import java.util.ArrayList; -import java.util.List; +import java.util.HashSet; +import java.util.Set; import com.mapswithme.maps.BuildConfig; import com.mapswithme.maps.Framework; @@ -37,17 +39,15 @@ final class StorageUtils * @return result */ @SuppressWarnings("ResultOfMethodCallIgnored") - static boolean isDirWritable(String path) + public static boolean isDirWritable(String path) { - final File f = new File(path, "testDir"); + File f = new File(path, "mapsme_test_dir"); f.mkdir(); - if (f.exists()) - { - f.delete(); - return true; - } + if (!f.exists()) + return false; - return false; + f.delete(); + return true; } /** @@ -81,7 +81,7 @@ final class StorageUtils // http://stackoverflow.com/questions/8151779/find-sd-card-volume-label-on-android // http://stackoverflow.com/questions/5694933/find-an-external-sd-card-location // http://stackoverflow.com/questions/14212969/file-canwrite-returns-false-on-some-devices-although-write-external-storage-pe - private static void parseMountFile(String file, int mode, List<String> paths) + private static void parseMountFile(String file, int mode, Set<String> paths) { Log.i(StoragePathManager.TAG, "Parsing " + file); @@ -92,34 +92,35 @@ final class StorageUtils while (true) { - final String line = reader.readLine(); + String line = reader.readLine(); if (line == null) - break; - - // standard regexp for all possible whitespaces (space, tab, etc) - final String[] arr = line.split("\\s+"); - - // split may return empty first strings - int start = 0; - while (start < arr.length && arr[start].length() == 0) - ++start; + return; - if (arr.length - start <= 3) + line = line.trim(); + if (TextUtils.isEmpty(line) || line.startsWith("#")) continue; - if (arr[start].charAt(0) == '#') + // standard regexp for all possible whitespaces (space, tab, etc) + String[] parts = line.split("\\s+"); + + if (parts.length <= 3) continue; if (mode == VOLD_MODE) { - if (arr[start].startsWith("dev_mount")) - paths.add(arr[start + 2]); + if (parts[0].startsWith("dev_mount")) + paths.add(parts[2]); + + continue; } - else + + for (String s : new String[] { "/dev/block/vold", "/dev/fuse", "/mnt/media_rw" }) { - for (final String s : new String[]{"tmpfs", "/dev/block/vold", "/dev/fuse", "/mnt/media_rw"}) - if (arr[start].startsWith(s)) - paths.add(arr[start + 1]); + if (parts[0].startsWith(s)) + { + paths.add(parts[1]); + break; + } } } } catch (final IOException e) @@ -131,7 +132,7 @@ final class StorageUtils } } - static void parseStorages(List<String> paths) + static void parseStorages(Set<String> paths) { parseMountFile("/etc/vold.conf", VOLD_MODE, paths); parseMountFile("/etc/vold.fstab", VOLD_MODE, paths); @@ -141,7 +142,7 @@ final class StorageUtils @TargetApi(Build.VERSION_CODES.KITKAT) @SuppressWarnings("ResultOfMethodCallIgnored") - static void parseKitkatStorages(List<String> paths) + static void parseKitkatStorages(Set<String> paths) { final File primaryStorage = MwmApplication.get().getExternalFilesDir(null); final File[] storages = MwmApplication.get().getExternalFilesDirs(null); @@ -158,9 +159,9 @@ final class StorageUtils } } - final ArrayList<String> testStorages = new ArrayList<>(); + Set<String> testStorages = new HashSet<>(); parseStorages(testStorages); - final String suffix = String.format(Constants.STORAGE_PATH, BuildConfig.APPLICATION_ID, Constants.FILES_DIR); + String suffix = String.format(Constants.STORAGE_PATH, BuildConfig.APPLICATION_ID, Constants.FILES_DIR); for (String testStorage : testStorages) { Log.i(StoragePathManager.TAG, "Test storage from config files : " + testStorage); @@ -176,11 +177,10 @@ final class StorageUtils if (!file.exists()) // create directory for our package if it isn't created by any reason { Log.i(StoragePathManager.TAG, "Try to create MWM path"); - file.mkdirs(); - file = new File(testStorage); - if (file.exists()) + if (file.mkdirs()) Log.i(StoragePathManager.TAG, "Created!"); } + if (isDirWritable(testStorage)) { Log.i(StoragePathManager.TAG, "Found writable storage : " + testStorage); diff --git a/android/src/com/mapswithme/util/Config.java b/android/src/com/mapswithme/util/Config.java index b282ec5cc1..3e8f5af659 100644 --- a/android/src/com/mapswithme/util/Config.java +++ b/android/src/com/mapswithme/util/Config.java @@ -13,6 +13,7 @@ public final class Config private static final String KEY_APP_SESSION_NUMBER = "SessionNumber"; private static final String KEY_APP_LAST_SESSION_TIMESTAMP = "LastSessionTimestamp"; private static final String KEY_APP_FIRST_INSTALL_FLAVOR = "FirstInstallFlavor"; + private static final String KEY_APP_STORAGE = "StoragePath"; private static final String KEY_TTS_ENABLED = "TtsEnabled"; private static final String KEY_TTS_LANGUAGE = "TtsLanguage"; @@ -177,6 +178,16 @@ public final class Config incrementSessionNumber(); } + public static String getStoragePath() + { + return getString(KEY_APP_STORAGE); + } + + public static void setStoragePath(String path) + { + setString(KEY_APP_STORAGE, path); + } + public static boolean isTtsEnabled() { return getBool(KEY_TTS_ENABLED, true); diff --git a/android/src/com/mapswithme/util/Utils.java b/android/src/com/mapswithme/util/Utils.java index 2f7c0c8ba8..d5a99ac40b 100644 --- a/android/src/com/mapswithme/util/Utils.java +++ b/android/src/com/mapswithme/util/Utils.java @@ -197,7 +197,7 @@ public class Utils */ public static String saveLogToFile() { - String fullName = MwmApplication.getDataStoragePath() + "log.txt"; + String fullName = MwmApplication.getSettingsPath() + "log.txt"; File file = new File(fullName); InputStreamReader reader = null; FileWriter writer = null; |