summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2019-11-08 17:18:37 +0200
committerMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2019-11-12 17:24:50 +0200
commit2edbe916ab33301032c15a45ee9c8b7f37f0aa57 (patch)
treeecffdfd335ee3bc0a6a03d1a1b203ede4c8e4bc8
parentf567a79dd8c198e6ea7bd581c4461f8c6dc16bce (diff)
downloadqtlocation-mapboxgl-2edbe916ab33301032c15a45ee9c8b7f37f0aa57.tar.gz
[android] Add OfflineManager.packDatabase() API
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java44
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/offline/CacheTest.kt16
-rw-r--r--platform/android/src/offline/offline_manager.cpp27
-rw-r--r--platform/android/src/offline/offline_manager.hpp2
4 files changed, 87 insertions, 2 deletions
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 faed46662a..0051c17e03 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
@@ -307,6 +307,47 @@ public class OfflineManager {
}
/**
+ * Packs the existing database file into a minimal amount of disk space.
+ * <p>
+ * When the operation is complete or encounters an error, the given callback will be
+ * executed on the database thread; it is the responsibility of the SDK bindings
+ * to re-execute a user-provided callback on the main thread.
+ * </p>
+ *
+ * @param callback the callback to be invoked when the database was reset or when the operation erred.
+ */
+ public void packDatabase(@Nullable final FileSourceCallback callback) {
+ fileSource.activate();
+ nativePackDatabase(new FileSourceCallback() {
+ @Override
+ public void onSuccess() {
+ handler.post(new Runnable() {
+ @Override
+ public void run() {
+ fileSource.deactivate();
+ if (callback != null) {
+ callback.onSuccess();
+ }
+ }
+ });
+ }
+
+ @Override
+ public void onError(@NonNull final String message) {
+ handler.post(new Runnable() {
+ @Override
+ public void run() {
+ fileSource.deactivate();
+ if (callback != null) {
+ callback.onError(message);
+ }
+ }
+ });
+ }
+ });
+ }
+
+ /**
* Forces re-validation of the ambient cache.
* <p>
* Forces Mapbox GL Native to revalidate resources stored in the ambient
@@ -649,6 +690,9 @@ public class OfflineManager {
private native void nativeResetDatabase(@Nullable FileSourceCallback callback);
@Keep
+ private native void nativePackDatabase(@Nullable FileSourceCallback callback);
+
+ @Keep
private native void nativeInvalidateAmbientCache(@Nullable FileSourceCallback callback);
@Keep
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/offline/CacheTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/offline/CacheTest.kt
index a3214c9f11..da4cfbaf01 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/offline/CacheTest.kt
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/offline/CacheTest.kt
@@ -85,4 +85,20 @@ class CacheTest {
}
countDownLatch.await()
}
+
+ @Test
+ fun testSetPackDatabase() {
+ rule.activity.runOnUiThread {
+ OfflineManager.getInstance(context).packDatabase(object : OfflineManager.FileSourceCallback {
+ override fun onSuccess() {
+ countDownLatch.countDown()
+ }
+
+ override fun onError(message: String) {
+ Assert.assertNull("onError should not be called", message)
+ }
+ })
+ }
+ countDownLatch.await()
+ }
} \ No newline at end of file
diff --git a/platform/android/src/offline/offline_manager.cpp b/platform/android/src/offline/offline_manager.cpp
index 029252f786..8d76f68522 100644
--- a/platform/android/src/offline/offline_manager.cpp
+++ b/platform/android/src/offline/offline_manager.cpp
@@ -8,7 +8,6 @@ namespace mbgl {
namespace android {
// OfflineManager //
-
OfflineManager::OfflineManager(jni::JNIEnv& env, const jni::Object<FileSource>& jFileSource)
: fileSource(std::static_pointer_cast<DefaultFileSource>(mbgl::FileSource::getSharedFileSource(FileSource::getSharedResourceOptions(env, jFileSource)))) {}
@@ -123,6 +122,26 @@ void OfflineManager::resetDatabase(jni::JNIEnv& env_, const jni::Object<FileSour
});
}
+void OfflineManager::packDatabase(jni::JNIEnv& env_, const jni::Object<FileSourceCallback>& callback_) {
+ auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_);
+
+ fileSource->packDatabase(
+ [
+ // Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile
+ callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback))](
+ std::exception_ptr exception) mutable {
+ // Reattach, the callback comes from a different thread
+ android::UniqueEnv env = android::AttachEnv();
+
+ if (exception) {
+ OfflineManager::FileSourceCallback::onError(
+ *env, *callback, jni::Make<jni::String>(*env, mbgl::util::toString(exception)));
+ } else {
+ OfflineManager::FileSourceCallback::onSuccess(*env, *callback);
+ }
+ });
+}
+
void OfflineManager::invalidateAmbientCache(jni::JNIEnv& env_, const jni::Object<FileSourceCallback>& callback_) {
auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_);
@@ -207,7 +226,10 @@ void OfflineManager::registerNative(jni::JNIEnv& env) {
#define METHOD(MethodPtr, name) jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name)
- jni::RegisterNativePeer<OfflineManager>( env, javaClass, "nativePtr",
+ jni::RegisterNativePeer<OfflineManager>(
+ env,
+ javaClass,
+ "nativePtr",
jni::MakePeer<OfflineManager, const jni::Object<FileSource>&>,
"initialize",
"finalize",
@@ -216,6 +238,7 @@ void OfflineManager::registerNative(jni::JNIEnv& env) {
METHOD(&OfflineManager::createOfflineRegion, "createOfflineRegion"),
METHOD(&OfflineManager::mergeOfflineRegions, "mergeOfflineRegions"),
METHOD(&OfflineManager::resetDatabase, "nativeResetDatabase"),
+ METHOD(&OfflineManager::packDatabase, "nativePackDatabase"),
METHOD(&OfflineManager::invalidateAmbientCache, "nativeInvalidateAmbientCache"),
METHOD(&OfflineManager::clearAmbientCache, "nativeClearAmbientCache"),
METHOD(&OfflineManager::setMaximumAmbientCacheSize, "nativeSetMaximumAmbientCacheSize"),
diff --git a/platform/android/src/offline/offline_manager.hpp b/platform/android/src/offline/offline_manager.hpp
index 058cfb5b48..64e00f91fc 100644
--- a/platform/android/src/offline/offline_manager.hpp
+++ b/platform/android/src/offline/offline_manager.hpp
@@ -95,6 +95,8 @@ public:
void resetDatabase(jni::JNIEnv&, const jni::Object<FileSourceCallback>& callback_);
+ void packDatabase(jni::JNIEnv&, const jni::Object<FileSourceCallback>& callback_);
+
void invalidateAmbientCache(jni::JNIEnv&, const jni::Object<FileSourceCallback>& callback_);
void clearAmbientCache(jni::JNIEnv&, const jni::Object<FileSourceCallback>& callback_);