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

add FileSource activation/deactivation to MapSnapshotter #10556

Merged
merged 1 commit into from
Dec 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
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;

/**
Expand Down Expand Up @@ -119,39 +117,21 @@ public static boolean isExternalStorageReadable() {
}

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);

public native String getAccessToken();

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>
Expand Down
19 changes: 15 additions & 4 deletions platform/android/src/file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
);
}

Expand Down
1 change: 1 addition & 0 deletions platform/android/src/file_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class FileSource {
static void registerNative(jni::JNIEnv&);

private:
optional<int> activationCounter;
std::unique_ptr<Actor<ResourceTransform>> resourceTransform;
std::unique_ptr<mbgl::DefaultFileSource> fileSource;
};
Expand Down
33 changes: 26 additions & 7 deletions platform/android/src/snapshotter/map_snapshotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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(),
Expand All @@ -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));
}
Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions platform/android/src/snapshotter/map_snapshotter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class MapSnapshotter {
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
Expand Down