diff --git a/android/jni/com/mapswithme/maps/MWMApplication.cpp b/android/jni/com/mapswithme/maps/MWMApplication.cpp index e282ffa971..635f77f6a7 100644 --- a/android/jni/com/mapswithme/maps/MWMApplication.cpp +++ b/android/jni/com/mapswithme/maps/MWMApplication.cpp @@ -82,4 +82,10 @@ extern "C" bool flag = val; (void)Settings::Set(jni::ToNativeString(env, name), flag); } + + JNIEXPORT jboolean JNICALL + Java_com_mapswithme_maps_MWMApplication_hasFreeSpace(JNIEnv * env, jobject thiz, jlong size) + { + return android::Platform::Instance().HasAvailableSpaceForWriting(size); + } } diff --git a/android/jni/com/mapswithme/platform/Platform.cpp b/android/jni/com/mapswithme/platform/Platform.cpp index a2f885be43..9e3abbcf6a 100644 --- a/android/jni/com/mapswithme/platform/Platform.cpp +++ b/android/jni/com/mapswithme/platform/Platform.cpp @@ -92,8 +92,7 @@ namespace android // It stores path to current maps storage. m_settingsDir = jni::ToNativeString(env, storagePath); - if (!Settings::Get("StoragePath", m_writableDir) || - (GetWritableStorageStatus(1024) != ::Platform::STORAGE_OK)) + if (!Settings::Get("StoragePath", m_writableDir) || !HasAvailableSpaceForWriting(1024)) { // If no saved storage path or the storage is unavailable // (disconnected from the last session), assign writable @@ -123,7 +122,7 @@ namespace android m_tmpDir = m_localTmpPath; } - string Platform::GetStoragePathPrefix() + string Platform::GetStoragePathPrefix() const { size_t const count = m_writableDir.size(); ASSERT_GREATER ( count, 2, () ); @@ -140,6 +139,11 @@ namespace android Settings::Set("StoragePath", m_writableDir); } + bool Platform::HasAvailableSpaceForWriting(uint64_t size) const + { + return (GetWritableStorageStatus(size) == ::Platform::STORAGE_OK); + } + Platform & Platform::Instance() { static Platform platform; diff --git a/android/jni/com/mapswithme/platform/Platform.hpp b/android/jni/com/mapswithme/platform/Platform.hpp index 619f7e6837..fdb6172714 100644 --- a/android/jni/com/mapswithme/platform/Platform.hpp +++ b/android/jni/com/mapswithme/platform/Platform.hpp @@ -27,10 +27,12 @@ namespace android void OnExternalStorageStatusChanged(bool isAvailable); /// get storage path without ending "/MapsWithMe/" - string GetStoragePathPrefix(); + string GetStoragePathPrefix() const; /// assign storage path (should contain ending "/MapsWithMe/") void SetStoragePath(string const & path); + bool HasAvailableSpaceForWriting(uint64_t size) const; + static Platform & Instance(); }; } diff --git a/android/src/com/mapswithme/maps/DownloadUI.java b/android/src/com/mapswithme/maps/DownloadUI.java index 3eae60507e..92a6719f52 100644 --- a/android/src/com/mapswithme/maps/DownloadUI.java +++ b/android/src/com/mapswithme/maps/DownloadUI.java @@ -8,8 +8,6 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; -import android.os.Environment; -import android.os.StatFs; import android.provider.Settings; import android.util.Log; import android.view.LayoutInflater; @@ -128,7 +126,7 @@ public class DownloadUI extends ListActivity implements MapStorage.Listener private AlertDialog.Builder m_alert; private DialogInterface.OnClickListener m_alertCancelHandler = - new DialogInterface.OnClickListener() + new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dlg, int which) @@ -211,10 +209,10 @@ public class DownloadUI extends ListActivity implements MapStorage.Listener .show(); } - static private long getFreeSpace() + private boolean hasFreeSpace(long size) { - StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); - return (long)stat.getAvailableBlocks() * (long)stat.getBlockSize(); + MWMApplication app = (MWMApplication) m_context.getApplication(); + return app.hasFreeSpace(size); } private void processCountry(int position) @@ -256,7 +254,7 @@ public class DownloadUI extends ListActivity implements MapStorage.Listener @Override public void onClick(DialogInterface dlg, int which) { - if (remoteSize + MB > getFreeSpace()) + if (!hasFreeSpace(remoteSize + MB)) showNotEnoughFreeSpaceDialog(getSizeString(remoteSize), name); else m_storage.downloadCountry(idx); @@ -281,7 +279,7 @@ public class DownloadUI extends ListActivity implements MapStorage.Listener case MapStorage.NOT_DOWNLOADED: // Check for available free space final long size = m_storage.countryRemoteSizeInBytes(idx); - if (size + MB > getFreeSpace()) + if (!hasFreeSpace(size + MB)) { showNotEnoughFreeSpaceDialog(getSizeString(size), name); } diff --git a/android/src/com/mapswithme/maps/MWMApplication.java b/android/src/com/mapswithme/maps/MWMApplication.java index 285270eb22..f81e6e754f 100644 --- a/android/src/com/mapswithme/maps/MWMApplication.java +++ b/android/src/com/mapswithme/maps/MWMApplication.java @@ -153,6 +153,9 @@ public class MWMApplication extends android.app.Application implements MapStorag return storagePath.concat(String.format("/Android/data/%s/%s/", getPackageName(), folder)); } + /// Check if we have free space on storage (writable path). + public native boolean hasFreeSpace(long size); + public boolean isProVersion() { return m_isProVersion; diff --git a/platform/platform.hpp b/platform/platform.hpp index 0f2a056535..c47ff5beb9 100644 --- a/platform/platform.hpp +++ b/platform/platform.hpp @@ -96,7 +96,7 @@ public: STORAGE_DISCONNECTED, NOT_ENOUGH_SPACE }; - TStorageStatus GetWritableStorageStatus(uint64_t neededSize); + TStorageStatus GetWritableStorageStatus(uint64_t neededSize) const; /// @name Functions for concurrent tasks. //@{ diff --git a/platform/platform_unix_impl.cpp b/platform/platform_unix_impl.cpp index 6f50e7eee0..d16291c1e7 100644 --- a/platform/platform_unix_impl.cpp +++ b/platform/platform_unix_impl.cpp @@ -31,7 +31,7 @@ bool Platform::GetFileSizeByFullPath(string const & filePath, uint64_t & size) else return false; } -Platform::TStorageStatus Platform::GetWritableStorageStatus(uint64_t neededSize) +Platform::TStorageStatus Platform::GetWritableStorageStatus(uint64_t neededSize) const { struct statfs st; int const ret = statfs(m_writableDir.c_str(), &st); diff --git a/platform/platform_win.cpp b/platform/platform_win.cpp index ac57d3ad2e..399caf370a 100644 --- a/platform/platform_win.cpp +++ b/platform/platform_win.cpp @@ -122,7 +122,7 @@ void Platform::RunAsync(TFunctor const & fn, Priority p) fn(); } -Platform::TStorageStatus Platform::GetWritableStorageStatus(uint64_t neededSize) +Platform::TStorageStatus Platform::GetWritableStorageStatus(uint64_t neededSize) const { ULARGE_INTEGER freeSpace; if (0 == ::GetDiskFreeSpaceExA(m_writableDir.c_str(), &freeSpace, NULL, NULL))