Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] Unify cache and offline databases
Browse files Browse the repository at this point in the history
See #4362
  • Loading branch information
zugaldia committed Mar 18, 2016
1 parent 0a26d84 commit 7cdccc3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion platform/android/src/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ NativeMapView::NativeMapView(JNIEnv *env, jobject obj_, float pixelRatio_, int a
}

fileSource = std::make_unique<mbgl::DefaultFileSource>(
mbgl::android::cachePath + "/mbgl-cache.db",
mbgl::android::cachePath + "/mbgl-offline.db",
mbgl::android::apkPath);

map = std::make_unique<mbgl::Map>(*this, *fileSource, MapMode::Continuous);
Expand Down

0 comments on commit 7cdccc3

Please sign in to comment.