Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] resume file source and wait for result to complete resource…
Browse files Browse the repository at this point in the history
…s cache path change
  • Loading branch information
LukasPaczos committed May 16, 2019
1 parent e6365e9 commit f885d46
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,7 @@ public void onWritePermissionGranted() {
Context.MODE_PRIVATE).edit();
editor.putString(MAPBOX_SHARED_PREFERENCE_RESOURCES_CACHE_PATH, path);
editor.apply();
setResourcesCachePath(applicationContext, path);
callback.onSuccess(path);
internalSetResourcesCachePath(applicationContext, path, callback);
}

@Override
Expand All @@ -326,11 +325,27 @@ public void onError() {
}
}

private static void setResourcesCachePath(@NonNull Context context, @NonNull String path) {
resourcesCachePathLoaderLock.lock();
resourcesCachePath = path;
resourcesCachePathLoaderLock.unlock();
getInstance(context).setResourceCachePath(path);
private static void internalSetResourcesCachePath(@NonNull Context context, @NonNull String path,
@NonNull final ResourcesCachePathChangeCallback callback) {
final FileSource fileSource = getInstance(context);
fileSource.setResourceCachePath(path, new ResourcesCachePathChangeCallback() {
@Override
public void onSuccess(@NonNull String path) {
fileSource.deactivate();
resourcesCachePathLoaderLock.lock();
resourcesCachePath = path;
resourcesCachePathLoaderLock.unlock();
callback.onSuccess(path);
}

@Override
public void onError(@NonNull String message) {
// currently unused, we're not expecting to fail internally
fileSource.deactivate();
callback.onError(message);
}
});
fileSource.activate();
}

private static boolean isPathWritable(String path) {
Expand Down Expand Up @@ -388,7 +403,7 @@ private FileSource(String cachePath, AssetManager assetManager) {
public native void setResourceTransform(final ResourceTransformCallback callback);

@Keep
private native void setResourceCachePath(String path);
private native void setResourceCachePath(String path, ResourcesCachePathChangeCallback callback);

@Keep
private native void initialize(String accessToken, String cachePath, AssetManager assetManager);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mapbox.mapboxsdk.testapp.storage

import android.os.Handler
import android.support.test.annotation.UiThreadTest
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
Expand All @@ -9,7 +8,6 @@ import com.mapbox.mapboxsdk.testapp.activity.FeatureOverviewActivity
import org.junit.*
import org.junit.rules.TestName
import org.junit.runner.RunWith
import java.util.concurrent.CountDownLatch

@RunWith(AndroidJUnit4::class)
class FileSourceStandaloneTest {
Expand Down Expand Up @@ -55,17 +53,6 @@ class FileSourceStandaloneTest {
fileSourceTestUtils.changePath(fileSourceTestUtils.testPath)
Assert.assertEquals(fileSourceTestUtils.testPath, FileSource.getResourcesCachePath(rule.activity))

// workaround for https://github.com/mapbox/mapbox-gl-native/issues/14334
val latch = CountDownLatch(1)
rule.activity.runOnUiThread {
fileSource.activate()
Handler().postDelayed({
fileSource.deactivate()
latch.countDown()
}, 2000)
}
latch.await()

fileSourceTestUtils.changePath(fileSourceTestUtils.originalPath)
Assert.assertEquals(fileSourceTestUtils.originalPath, FileSource.getResourcesCachePath(rule.activity))
}
Expand Down
27 changes: 24 additions & 3 deletions platform/android/src/file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,19 @@ void FileSource::setResourceTransform(jni::JNIEnv& env, const jni::Object<FileSo
}
}

void FileSource::setResourceCachePath(jni::JNIEnv& env, const jni::String& path) {
void FileSource::setResourceCachePath(jni::JNIEnv& env, const jni::String& path,
const jni::Object<FileSource::ResourcesCachePathChangeCallback>& _callback) {
std::string newPath = jni::Make<std::string>(env, path);
mapbox::sqlite::setTempPath(newPath);
fileSource->setResourceCachePath(newPath + DATABASE_FILE);

auto global = jni::NewGlobal<jni::EnvAttachingDeleter>(env, _callback);
patchChangeCallback = std::make_unique<Actor<PathChangeCallback>>(*Scheduler::GetCurrent(),
[callback = std::make_shared<decltype(global)>(std::move(global)), newPath]() {
android::UniqueEnv _env = android::AttachEnv();
return FileSource::ResourcesCachePathChangeCallback::onSuccess(*_env, *callback, newPath);
});

fileSource->setResourceCachePath(newPath + DATABASE_FILE, patchChangeCallback->self());
}

void FileSource::resume(jni::JNIEnv&) {
Expand Down Expand Up @@ -123,11 +132,23 @@ mbgl::ResourceOptions FileSource::getSharedResourceOptions(jni::JNIEnv& env, con
return fileSource->resourceOptions.clone();
}

// FileSource::ResourcesCachePathChangeCallback //

void FileSource::ResourcesCachePathChangeCallback::onSuccess(jni::JNIEnv& env,
const jni::Object<FileSource::ResourcesCachePathChangeCallback>& callback,
std::string path) {
static auto& javaClass = jni::Class<FileSource::ResourcesCachePathChangeCallback>::Singleton(env);
static auto method = javaClass.GetMethod<void (jni::String)>(env, "onSuccess");

callback.Call(env, method, jni::Make<jni::String>(env, path));
}

void FileSource::registerNative(jni::JNIEnv& env) {
// Ensure the class for ResourceTransformCallback is cached. If it's requested for the
// Ensure the classes are cached. If they're requested for the
// first time on a background thread, Android's class loader heuristics will fail.
// https://developer.android.com/training/articles/perf-jni#faq_FindClass
jni::Class<ResourceTransformCallback>::Singleton(env);
jni::Class<ResourcesCachePathChangeCallback>::Singleton(env);

static auto& javaClass = jni::Class<FileSource>::Singleton(env);

Expand Down
12 changes: 11 additions & 1 deletion platform/android/src/file_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace mbgl {

template <typename T> class Actor;
class ResourceTransform;
using mbgl::PathChangeCallback;

namespace android {

Expand All @@ -28,6 +29,14 @@ class FileSource {
static std::string onURL(jni::JNIEnv&, const jni::Object<FileSource::ResourceTransformCallback>&, int, std::string);
};

struct ResourcesCachePathChangeCallback {
static constexpr auto Name() { return "com/mapbox/mapboxsdk/storage/FileSource$ResourcesCachePathChangeCallback";}

static void onSuccess(jni::JNIEnv&,
const jni::Object<FileSource::ResourcesCachePathChangeCallback>&,
std::string);
};

FileSource(jni::JNIEnv&, const jni::String&, const jni::String&, const jni::Object<AssetManager>&);

~FileSource();
Expand All @@ -40,7 +49,7 @@ class FileSource {

void setResourceTransform(jni::JNIEnv&, const jni::Object<FileSource::ResourceTransformCallback>&);

void setResourceCachePath(jni::JNIEnv&, const jni::String&);
void setResourceCachePath(jni::JNIEnv&, const jni::String&, const jni::Object<FileSource::ResourcesCachePathChangeCallback>&);

void resume(jni::JNIEnv&);

Expand All @@ -59,6 +68,7 @@ class FileSource {
optional<int> activationCounter;
mbgl::ResourceOptions resourceOptions;
std::unique_ptr<Actor<ResourceTransform>> resourceTransform;
std::unique_ptr<Actor<PathChangeCallback>> patchChangeCallback;
std::shared_ptr<mbgl::DefaultFileSource> fileSource;
};

Expand Down

0 comments on commit f885d46

Please sign in to comment.