[android] use shared DefaultFileSource for all maps + OfflineManager

This commit is contained in:
Konstantin Käfer
2017-02-17 15:17:21 +01:00
parent 6c0310209d
commit c86e38e303
7 changed files with 51 additions and 27 deletions

View File

@@ -32,13 +32,6 @@ public class OfflineManager {
// Default database name
private static final String DATABASE_NAME = "mbgl-offline.db";
/*
* The maximumCacheSize parameter is a limit applied to non-offline resources only,
* i.e. resources added to the database for the "ambient use" caching functionality.
* There is no size limit for offline resources.
*/
private static final long DEFAULT_MAX_CACHE_SIZE = 50 * 1024 * 1024;
// Holds the pointer to JNI DefaultFileSource
private long mDefaultFileSourcePtr = 0;
@@ -93,9 +86,9 @@ public class OfflineManager {
*/
private OfflineManager(Context context) {
// Get a pointer to the DefaultFileSource instance
String assetRoot = getDatabasePath(context);
String cachePath = assetRoot + File.separator + DATABASE_NAME;
mDefaultFileSourcePtr = createDefaultFileSource(cachePath, assetRoot, DEFAULT_MAX_CACHE_SIZE);
String cachePath = getDatabasePath(context) + File.separator + DATABASE_NAME;
String apkPath = context.getPackageCodePath();
mDefaultFileSourcePtr = sharedDefaultFileSource(cachePath, apkPath);
setAccessToken(mDefaultFileSourcePtr, Mapbox.getAccessToken());
// Delete any existing previous ambient cache database
@@ -284,8 +277,8 @@ public class OfflineManager {
/*
* Native methods
*/
private native long createDefaultFileSource(
String cachePath, String assetRoot, long maximumCacheSize);
private native long sharedDefaultFileSource(
String cachePath, String assetRoot);
private native void setAccessToken(long defaultFileSourcePtr, String accessToken);

View File

@@ -45,6 +45,8 @@ macro(mbgl_platform_core)
PRIVATE platform/default/default_file_source.cpp
PRIVATE platform/default/local_file_source.cpp
PRIVATE platform/default/online_file_source.cpp
PRIVATE platform/android/src/default_file_source.cpp
PRIVATE platform/android/src/default_file_source.hpp
# Offline
# PRIVATE include/mbgl/storage/offline.hpp

View File

@@ -0,0 +1,20 @@
#include "default_file_source.hpp"
#include <mbgl/util/logging.hpp>
#include <cassert>
namespace mbgl {
namespace android {
DefaultFileSource& defaultFileSource(const std::string& cachePath_, const std::string& assetRoot_) {
static auto cachePath = cachePath_;
assert(cachePath == cachePath_);
static auto assetRoot = assetRoot_;
assert(assetRoot == assetRoot_);
static DefaultFileSource defaultFileSource{ cachePath, assetRoot };
return defaultFileSource;
}
} // namespace android
} // namespace mbgl

View File

@@ -0,0 +1,12 @@
#pragma once
#include <mbgl/storage/default_file_source.hpp>
namespace mbgl {
namespace android {
DefaultFileSource& defaultFileSource(const std::string& cachePath = ":memory:",
const std::string& assetRoot = ".");
} // namespace android
} // namespace mbgl

View File

@@ -14,6 +14,7 @@
#include "bitmap.hpp"
#include "bitmap_factory.hpp"
#include "connectivity_listener.hpp"
#include "default_file_source.hpp"
#include "style/functions/categorical_stops.hpp"
#include "style/functions/exponential_stops.hpp"
#include "style/functions/identity_stops.hpp"
@@ -1290,12 +1291,9 @@ void nativeScheduleTakeSnapshot(JNIEnv *env, jni::jobject* obj, jlong nativeMapV
// Offline calls begin
jlong createDefaultFileSource(JNIEnv *env, jni::jobject* obj, jni::jstring* cachePath_, jni::jstring* assetRoot_, jlong maximumCacheSize) {
std::string cachePath = std_string_from_jstring(env, cachePath_);
std::string assetRoot = std_string_from_jstring(env, assetRoot_);
mbgl::DefaultFileSource *defaultFileSource = new mbgl::DefaultFileSource(cachePath, assetRoot, maximumCacheSize);
jlong defaultFileSourcePtr = reinterpret_cast<jlong>(defaultFileSource);
return defaultFileSourcePtr;
jlong sharedDefaultFileSource(JNIEnv *env, jni::jobject* obj, jni::jstring* cachePath_, jni::jstring* assetRoot_) {
return reinterpret_cast<jlong>(&defaultFileSource(std_string_from_jstring(env, cachePath_),
std_string_from_jstring(env, assetRoot_)));
}
void setAccessToken(JNIEnv *env, jni::jobject* obj, jlong defaultFileSourcePtr, jni::jstring* accessToken_) {
@@ -1984,7 +1982,7 @@ void registerNatives(JavaVM *vm) {
offlineManagerClassPtrId = &jni::GetFieldID(env, offlineManagerClass, "mDefaultFileSourcePtr", "J");
jni::RegisterNatives(env, offlineManagerClass,
MAKE_NATIVE_METHOD(createDefaultFileSource, "(Ljava/lang/String;Ljava/lang/String;J)J"),
MAKE_NATIVE_METHOD(sharedDefaultFileSource, "(Ljava/lang/String;Ljava/lang/String;)J"),
MAKE_NATIVE_METHOD(setAccessToken, "(JLjava/lang/String;)V"),
MAKE_NATIVE_METHOD(getAccessToken, "(J)Ljava/lang/String;"),
MAKE_NATIVE_METHOD(listOfflineRegions, "(JLcom/mapbox/mapboxsdk/offline/OfflineManager$ListOfflineRegionsCallback;)V"),

View File

@@ -1,5 +1,6 @@
#include "native_map_view.hpp"
#include "jni.hpp"
#include "default_file_source.hpp"
#include <cstdlib>
#include <ctime>
@@ -27,6 +28,7 @@ NativeMapView::NativeMapView(JNIEnv *env_, jobject obj_, float pixelRatio, int a
: env(env_),
availableProcessors(availableProcessors_),
totalMemory(totalMemory_),
fileSource(defaultFileSource(mbgl::android::cachePath + "/mbgl-offline.db", mbgl::android::apkPath)),
threadPool(4) {
assert(env_ != nullptr);
@@ -43,13 +45,9 @@ NativeMapView::NativeMapView(JNIEnv *env_, jobject obj_, float pixelRatio, int a
return;
}
fileSource = std::make_unique<mbgl::DefaultFileSource>(
mbgl::android::cachePath + "/mbgl-offline.db",
mbgl::android::apkPath);
map = std::make_unique<mbgl::Map>(
*this, mbgl::Size{ static_cast<uint32_t>(width), static_cast<uint32_t>(height) },
pixelRatio, *fileSource, threadPool, MapMode::Continuous);
pixelRatio, fileSource, threadPool, MapMode::Continuous);
float zoomFactor = map->getMaxZoom() - map->getMinZoom() + 1;
float cpuFactor = availableProcessors;
@@ -71,7 +69,6 @@ NativeMapView::~NativeMapView() {
assert(obj != nullptr);
map.reset();
fileSource.reset();
env->DeleteWeakGlobalRef(obj);
@@ -198,7 +195,9 @@ void NativeMapView::render() {
mbgl::Map &NativeMapView::getMap() { return *map; }
mbgl::DefaultFileSource &NativeMapView::getFileSource() { return *fileSource; }
mbgl::DefaultFileSource& NativeMapView::getFileSource() {
return fileSource;
}
void NativeMapView::initializeDisplay() {
assert(display == EGL_NO_DISPLAY);

View File

@@ -96,7 +96,7 @@ private:
size_t totalMemory = 0;
// Ensure these are initialised last
std::unique_ptr<mbgl::DefaultFileSource> fileSource;
mbgl::DefaultFileSource& fileSource;
mbgl::ThreadPool threadPool;
std::unique_ptr<mbgl::Map> map;
mbgl::EdgeInsets insets;