diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java index 20b881cbe63..c6ac6dc6e23 100755 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java @@ -50,8 +50,13 @@ final class NativeMapView { public NativeMapView(MapView mapView) { Context context = mapView.getContext(); - String cachePath = context.getCacheDir().getAbsolutePath(); String dataPath = context.getFilesDir().getAbsolutePath(); + + // With the availability of offline, we're unifying the ambient (cache) and the offline + // databases to be in the same folder, outside cache, to avoid automatic deletion from + // the system + String cachePath = dataPath; + float pixelRatio = context.getResources().getDisplayMetrics().density; String apkPath = context.getPackageCodePath(); int availableProcessors = Runtime.getRuntime().availableProcessors(); diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java index e632bcd765e..5c35c54fe6c 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java @@ -4,6 +4,7 @@ import android.os.Handler; import android.os.Looper; import android.support.annotation.NonNull; +import android.util.Log; import java.io.File; @@ -13,8 +14,10 @@ */ public class OfflineManager { + private final static String LOG_TAG = "OfflineManager"; + // Default database name - private final static String OFFLINE_DATABASE_NAME = "mbgl-offline.db"; + private final static String DATABASE_NAME = "mbgl-offline.db"; /* * The maximumCacheSize parameter is a limit applied to non-offline resources only, @@ -53,8 +56,30 @@ public interface CreateOfflineRegionCallback { private OfflineManager(Context context) { // Get a pointer to the DefaultFileSource instance String assetRoot = context.getFilesDir().getAbsolutePath(); - String cachePath = assetRoot + File.separator + OFFLINE_DATABASE_NAME; + String cachePath = assetRoot + File.separator + DATABASE_NAME; mDefaultFileSourcePtr = createDefaultFileSource(cachePath, assetRoot, DEFAULT_MAX_CACHE_SIZE); + + // Delete any existing previous ambient cache database + deleteAmbientDatabase(context); + } + + private void deleteAmbientDatabase(final Context context) { + // Delete the file in a separate thread to avoid affecting the UI + new Thread(new Runnable() { + @Override + public void run() { + try { + String path = context.getCacheDir().getAbsolutePath() + File.separator + DATABASE_NAME; + File file = new File(path); + if (file.exists()) { + file.delete(); + Log.d(LOG_TAG, "Old ambient cache database deleted to save space: " + path); + } + } catch (Exception e) { + Log.e(LOG_TAG, "Failed to delete old ambient cache database: " + e.getMessage()); + } + } + }).start(); } public static synchronized OfflineManager getInstance(Context context) { diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index 23eb39dab81..58883b23139 100755 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -76,7 +76,7 @@ NativeMapView::NativeMapView(JNIEnv *env, jobject obj_, float pixelRatio_, int a } fileSource = std::make_unique( - mbgl::android::cachePath + "/mbgl-cache.db", + mbgl::android::cachePath + "/mbgl-offline.db", mbgl::android::apkPath); map = std::make_unique(*this, *fileSource, MapMode::Continuous);