[android] Unify cache and offline databases

See #4362
This commit is contained in:
Antonio Zugaldia
2016-03-18 12:55:32 -04:00
committed by John Firebaugh
parent 90d271709f
commit 682b404bf6
3 changed files with 34 additions and 4 deletions

View File

@@ -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();

View File

@@ -4,6 +4,7 @@ import android.content.Context;
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 @@ import java.io.File;
*/
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 class OfflineManager {
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 + "mbgl-cache.db";
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) {