From 43cb1d98d189e6d14ea1d330e3c7736ea4c3782c Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Fri, 23 Oct 2015 16:46:32 +0300 Subject: [PATCH] SD card detection fix --- OsmAnd/src/net/osmand/plus/OsmandSettings.java | 18 +++++++++++++++++- .../osmand/plus/download/DownloadActivity.java | 2 +- .../ui/DataStoragePlaceDialogFragment.java | 8 ++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 958e903f60..6fac692508 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -43,6 +43,8 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Build; import android.os.Environment; +import android.support.annotation.Nullable; +import android.text.TextUtils; public class OsmandSettings { @@ -1307,6 +1309,21 @@ public class OsmandSettings { putInt(EXTERNAL_STORAGE_DIR_TYPE_V19, type). putString(EXTERNAL_STORAGE_DIR_V19, externalStorageDir).commit(); } + + @Nullable + public static File getSecondaryStorage() { + final String value = System.getenv("SECONDARY_STORAGE"); + if (!TextUtils.isEmpty(value)) { + final String[] paths = value.split(":"); + for (String path : paths) { + File file = new File(path); + if (file.isDirectory() && file.canWrite()) { + return file; + } + } + } + return null; + } public void setExternalStorageDirectory(int type, String directory) { if(Build.VERSION.SDK_INT < 19) { @@ -1325,7 +1342,6 @@ public class OsmandSettings { return settingsAPI.edit(globalPreferences).putString(EXTERNAL_STORAGE_DIR, externalStorageDir).commit(); } - public Object getGlobalPreferences() { return globalPreferences; } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index c834c833cc..c61aabd5dc 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -142,7 +142,7 @@ public class DownloadActivity extends ActionBarProgressActivity implements Downl } final boolean firstTime = getMyApplication().getAppInitializer().isFirstTime(null); final boolean externalExists = - DataStoragePlaceDialogFragment.getExternalStorageDirectory(this) != null; + DataStoragePlaceDialogFragment.getExternalStorageDirectory() != null; if (firstTime && externalExists) { new DataStoragePlaceDialogFragment().show(getFragmentManager(), null); } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DataStoragePlaceDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DataStoragePlaceDialogFragment.java index b29bf33888..ae42be5e0f 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DataStoragePlaceDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DataStoragePlaceDialogFragment.java @@ -55,7 +55,7 @@ public class DataStoragePlaceDialogFragment extends DialogFragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { internalStorage = getInternalStorageDirectory(getActivity()); - externalStorage = getExternalStorageDirectory(getActivity()); + externalStorage = getExternalStorageDirectory(); final View view = inflater.inflate(R.layout.fragment_data_storage_place_dialog, container, false); @@ -95,9 +95,8 @@ public class DataStoragePlaceDialogFragment extends DialogFragment { return view; } - public static File getExternalStorageDirectory(Activity activity) { - return ((OsmandApplication) activity.getApplication()).getSettings() - .getExternalStorageDirectory(); + public static File getExternalStorageDirectory() { + return OsmandSettings.getSecondaryStorage(); } public static File getInternalStorageDirectory(Activity activity) { @@ -122,6 +121,7 @@ public class DataStoragePlaceDialogFragment extends DialogFragment { String sz = ""; if (dir.canRead()) { StatFs fs = new StatFs(dir.getAbsolutePath()); + @SuppressWarnings("deprecation") int size = fs.getAvailableBlocks() * fs.getBlockSize(); if (size > 0) { if (size > 1 << 20) {