diff options
author | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2019-11-22 18:19:56 +0200 |
---|---|---|
committer | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2019-11-22 18:27:48 +0200 |
commit | 06773d7aeff4131b388ba44a89b4e5c5f3dcc704 (patch) | |
tree | 5de2ee323bd5d316eec66a8a9dea60fd7b49063d | |
parent | 7724d3028daf27734c8fde36bc42193556588581 (diff) | |
download | qtlocation-mapboxgl-upstream/mikhail_clear_ambient_cache_without_pack.tar.gz |
[android] Introduce OfflineManager.clearAmbientCacheAndSkipPackDatabase()upstream/mikhail_clear_ambient_cache_without_pack
3 files changed, 55 insertions, 7 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 0051c17e03..380bba3395 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 @@ -397,8 +397,9 @@ public class OfflineManager { * Erase resources from the ambient cache, freeing storage space. * <p> * Erases the ambient cache, freeing resources. This operation can be - * potentially slow because it will trigger a VACUUM on SQLite, - * forcing the database to move pages on the filesystem. + * potentially slow because it includes database file packing, i.e. it + * will trigger a VACUUM on SQLite, forcing the database to move pages + * on the filesystem. * </p> * <p> * Resources overlapping with offline regions will not be affected @@ -409,7 +410,51 @@ public class OfflineManager { */ public void clearAmbientCache(@Nullable final FileSourceCallback callback) { fileSource.activate(); - nativeClearAmbientCache(new FileSourceCallback() { + nativeClearAmbientCache(true /*pack*/, 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); + } + } + }); + } + }); + } + + /** + * Same as {@link OfflineManager#clearAmbientCache} but skipping database file packing for performance reasons. + * <p> + * Database file packing can be done later with {@link OfflineManager#packDatabase}. + * This method is a useful optimization e.g. when several regions should be deleted in a row. + * </p> + * <p> + * Resources overlapping with offline regions will not be affected + * by this call. + * </p> + * + * @param callback the callback to be invoked when the ambient cache was cleared or when the operation erred. + */ + public void clearAmbientCacheAndSkipPackDatabase(@Nullable final FileSourceCallback callback) { + fileSource.activate(); + nativeClearAmbientCache(false /*pack*/, new FileSourceCallback() { @Override public void onSuccess() { handler.post(new Runnable() { @@ -696,7 +741,7 @@ public class OfflineManager { private native void nativeInvalidateAmbientCache(@Nullable FileSourceCallback callback); @Keep - private native void nativeClearAmbientCache(@Nullable FileSourceCallback callback); + private native void nativeClearAmbientCache(boolean pack, @Nullable FileSourceCallback callback); @Keep private native void nativeSetMaximumAmbientCacheSize(long size, @Nullable FileSourceCallback callback); diff --git a/platform/android/src/offline/offline_manager.cpp b/platform/android/src/offline/offline_manager.cpp index c45386e3a7..9fd36ca083 100644 --- a/platform/android/src/offline/offline_manager.cpp +++ b/platform/android/src/offline/offline_manager.cpp @@ -147,14 +147,17 @@ void OfflineManager::invalidateAmbientCache(jni::JNIEnv& env_, const jni::Object std::exception_ptr exception) mutable { handleException(exception, *callback); }); } -void OfflineManager::clearAmbientCache(jni::JNIEnv& env_, const jni::Object<FileSourceCallback>& callback_) { +void OfflineManager::clearAmbientCache(jni::JNIEnv& env_, + jni::jboolean pack, + const jni::Object<FileSourceCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); fileSource->clearAmbientCache( [ // 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 { handleException(exception, *callback); }); + std::exception_ptr exception) mutable { handleException(exception, *callback); }, + pack); } void OfflineManager::setMaximumAmbientCacheSize(jni::JNIEnv& env_, const jni::jlong size_, const jni::Object<FileSourceCallback>& callback_) { diff --git a/platform/android/src/offline/offline_manager.hpp b/platform/android/src/offline/offline_manager.hpp index 64e00f91fc..e24d979d30 100644 --- a/platform/android/src/offline/offline_manager.hpp +++ b/platform/android/src/offline/offline_manager.hpp @@ -99,7 +99,7 @@ public: void invalidateAmbientCache(jni::JNIEnv&, const jni::Object<FileSourceCallback>& callback_); - void clearAmbientCache(jni::JNIEnv&, const jni::Object<FileSourceCallback>& callback_); + void clearAmbientCache(jni::JNIEnv&, jni::jboolean pack, const jni::Object<FileSourceCallback>& callback_); void setMaximumAmbientCacheSize(jni::JNIEnv&, const jni::jlong size, const jni::Object<FileSourceCallback>& callback_); |