[android] Add OfflineManager.packDatabase() API
This commit is contained in:
@@ -306,6 +306,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>
|
||||
@@ -648,6 +689,9 @@ public class OfflineManager {
|
||||
@Keep
|
||||
private native void nativeResetDatabase(@Nullable FileSourceCallback callback);
|
||||
|
||||
@Keep
|
||||
private native void nativePackDatabase(@Nullable FileSourceCallback callback);
|
||||
|
||||
@Keep
|
||||
private native void nativeInvalidateAmbientCache(@Nullable FileSourceCallback callback);
|
||||
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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"),
|
||||
|
||||
@@ -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_);
|
||||
|
||||
Reference in New Issue
Block a user