[android] - add FileSource activation/deactivation to MapSnapshotter, handle multiple deactivate scenario in FileSource (#10556)
This commit is contained in:
@@ -6,10 +6,8 @@ import android.content.pm.PackageManager;
|
||||
import android.content.res.AssetManager;
|
||||
import android.os.Environment;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.mapbox.mapboxsdk.Mapbox;
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
/**
|
||||
@@ -119,28 +117,14 @@ public class FileSource {
|
||||
}
|
||||
|
||||
private long nativePtr;
|
||||
private long activeCounter;
|
||||
private boolean wasPaused;
|
||||
|
||||
private FileSource(String cachePath, AssetManager assetManager) {
|
||||
initialize(Mapbox.getAccessToken(), cachePath, assetManager);
|
||||
}
|
||||
|
||||
public void activate() {
|
||||
activeCounter++;
|
||||
if (activeCounter == 1 && wasPaused) {
|
||||
wasPaused = false;
|
||||
resume();
|
||||
}
|
||||
}
|
||||
public native void activate();
|
||||
|
||||
public void deactivate() {
|
||||
activeCounter--;
|
||||
if (activeCounter == 0) {
|
||||
wasPaused = true;
|
||||
pause();
|
||||
}
|
||||
}
|
||||
public native void deactivate();
|
||||
|
||||
public native void setAccessToken(@NonNull String accessToken);
|
||||
|
||||
@@ -148,10 +132,6 @@ public class FileSource {
|
||||
|
||||
public native void setApiBaseUrl(String baseUrl);
|
||||
|
||||
private native void resume();
|
||||
|
||||
private native void pause();
|
||||
|
||||
/**
|
||||
* Sets a callback for transforming URLs requested from the internet
|
||||
* <p>
|
||||
|
||||
@@ -63,11 +63,22 @@ void FileSource::setResourceTransform(jni::JNIEnv& env, jni::Object<FileSource::
|
||||
}
|
||||
|
||||
void FileSource::resume(jni::JNIEnv&) {
|
||||
fileSource->resume();
|
||||
if (!activationCounter) {
|
||||
activationCounter = optional<int>(1) ;
|
||||
return;
|
||||
}
|
||||
|
||||
activationCounter.value()++;
|
||||
if (activationCounter == 1) {
|
||||
fileSource->resume();
|
||||
}
|
||||
}
|
||||
|
||||
void FileSource::pause(jni::JNIEnv&) {
|
||||
fileSource->pause();
|
||||
activationCounter.value()--;
|
||||
if (activationCounter == 0) {
|
||||
fileSource->pause();
|
||||
}
|
||||
}
|
||||
|
||||
jni::Class<FileSource> FileSource::javaClass;
|
||||
@@ -100,8 +111,8 @@ void FileSource::registerNative(jni::JNIEnv& env) {
|
||||
METHOD(&FileSource::setAccessToken, "setAccessToken"),
|
||||
METHOD(&FileSource::setAPIBaseUrl, "setApiBaseUrl"),
|
||||
METHOD(&FileSource::setResourceTransform, "setResourceTransform"),
|
||||
METHOD(&FileSource::resume, "resume"),
|
||||
METHOD(&FileSource::pause, "pause")
|
||||
METHOD(&FileSource::resume, "activate"),
|
||||
METHOD(&FileSource::pause, "deactivate")
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -54,6 +54,7 @@ public:
|
||||
static void registerNative(jni::JNIEnv&);
|
||||
|
||||
private:
|
||||
optional<int> activationCounter;
|
||||
std::unique_ptr<Actor<ResourceTransform>> resourceTransform;
|
||||
std::unique_ptr<mbgl::DefaultFileSource> fileSource;
|
||||
};
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace android {
|
||||
|
||||
MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env,
|
||||
jni::Object<MapSnapshotter> _obj,
|
||||
jni::Object<FileSource> jFileSource,
|
||||
jni::Object<FileSource> _jFileSource,
|
||||
jni::jfloat _pixelRatio,
|
||||
jni::jint width,
|
||||
jni::jint height,
|
||||
@@ -34,16 +34,16 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env,
|
||||
return;
|
||||
}
|
||||
|
||||
auto& fileSource = mbgl::android::FileSource::getDefaultFileSource(_env, jFileSource);
|
||||
jFileSource = FileSource::getNativePeer(_env, _jFileSource);
|
||||
auto& fileSource = mbgl::android::FileSource::getDefaultFileSource(_env, _jFileSource);
|
||||
auto size = mbgl::Size { static_cast<uint32_t>(width), static_cast<uint32_t>(height) };
|
||||
auto cameraOptions = position ? CameraPosition::getCameraOptions(_env, position) : CameraOptions();
|
||||
optional<mbgl::LatLngBounds> bounds;
|
||||
if (region) {
|
||||
bounds = LatLngBounds::getLatLngBounds(_env, region);
|
||||
}
|
||||
|
||||
|
||||
showLogo = _showLogo;
|
||||
|
||||
// Create the core snapshotter
|
||||
snapshotter = std::make_unique<mbgl::MapSnapshotter>(fileSource,
|
||||
*threadPool,
|
||||
@@ -58,8 +58,9 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env,
|
||||
|
||||
MapSnapshotter::~MapSnapshotter() = default;
|
||||
|
||||
void MapSnapshotter::start(JNIEnv&) {
|
||||
void MapSnapshotter::start(JNIEnv& env) {
|
||||
MBGL_VERIFY_THREAD(tid);
|
||||
activateFilesource(env);
|
||||
|
||||
snapshotCallback = std::make_unique<Actor<mbgl::MapSnapshotter::Callback>>(
|
||||
*Scheduler::GetCurrent(),
|
||||
@@ -79,17 +80,19 @@ void MapSnapshotter::start(JNIEnv&) {
|
||||
static auto onSnapshotReady = javaClass.GetMethod<void (jni::Object<MapSnapshot>)>(*_env, "onSnapshotReady");
|
||||
javaPeer->Call(*_env, onSnapshotReady, mapSnapshot);
|
||||
}
|
||||
|
||||
deactivateFilesource(*_env);
|
||||
});
|
||||
|
||||
snapshotter->snapshot(snapshotCallback->self());
|
||||
}
|
||||
|
||||
void MapSnapshotter::cancel(JNIEnv&) {
|
||||
void MapSnapshotter::cancel(JNIEnv& env) {
|
||||
MBGL_VERIFY_THREAD(tid);
|
||||
snapshotCallback.reset();
|
||||
deactivateFilesource(env);
|
||||
}
|
||||
|
||||
|
||||
void MapSnapshotter::setStyleUrl(JNIEnv& env, jni::String styleURL) {
|
||||
snapshotter->setStyleURL(jni::Make<std::string>(env, styleURL));
|
||||
}
|
||||
@@ -108,6 +111,22 @@ void MapSnapshotter::setRegion(JNIEnv& env, jni::Object<LatLngBounds> region) {
|
||||
snapshotter->setRegion(LatLngBounds::getLatLngBounds(env, region));
|
||||
}
|
||||
|
||||
// Private methods //
|
||||
|
||||
void MapSnapshotter::activateFilesource(JNIEnv& env) {
|
||||
if (!activatedFilesource) {
|
||||
activatedFilesource = true;
|
||||
jFileSource->resume(env);
|
||||
}
|
||||
}
|
||||
|
||||
void MapSnapshotter::deactivateFilesource(JNIEnv& env) {
|
||||
if (activatedFilesource) {
|
||||
activatedFilesource = false;
|
||||
jFileSource->pause(env);
|
||||
}
|
||||
}
|
||||
|
||||
// Static methods //
|
||||
|
||||
jni::Class<MapSnapshotter> MapSnapshotter::javaClass;
|
||||
|
||||
@@ -65,6 +65,11 @@ private:
|
||||
std::shared_ptr<mbgl::ThreadPool> threadPool;
|
||||
std::unique_ptr<Actor<mbgl::MapSnapshotter::Callback>> snapshotCallback;
|
||||
std::unique_ptr<mbgl::MapSnapshotter> snapshotter;
|
||||
|
||||
FileSource *jFileSource;
|
||||
void activateFilesource(JNIEnv&);
|
||||
void deactivateFilesource(JNIEnv&);
|
||||
bool activatedFilesource = false;
|
||||
};
|
||||
|
||||
} // namespace android
|
||||
|
||||
Reference in New Issue
Block a user