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

Commit

Permalink
[android] - use FileSource pause and resume when foregrounding/backgr…
Browse files Browse the repository at this point in the history
…ounding app
  • Loading branch information
tobrun committed Sep 13, 2017
1 parent 8e01f52 commit 21d6313
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.mapbox.mapboxsdk.maps.widgets.MyLocationView;
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
import com.mapbox.mapboxsdk.net.ConnectivityReceiver;
import com.mapbox.mapboxsdk.storage.FileSource;
import com.mapbox.services.android.telemetry.MapboxTelemetry;

import java.lang.annotation.Retention;
Expand Down Expand Up @@ -260,6 +261,7 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
@UiThread
public void onStart() {
ConnectivityReceiver.instance(getContext()).activate();
FileSource.getInstance(getContext()).activate();
if (mapboxMap != null) {
mapboxMap.onStart();
}
Expand Down Expand Up @@ -288,6 +290,7 @@ public void onPause() {
public void onStop() {
mapboxMap.onStop();
ConnectivityReceiver.instance(getContext()).deactivate();
FileSource.getInstance(getContext()).deactivate();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,19 @@ public void run() {

/**
* Pause or resume downloading of regional resources.
* <p>
* After a download has been completed, you are required to reset the state of the region to STATE_INACTIVE.
* </p>
*
* @param state the download state
*/
public void setDownloadState(@DownloadState int state) {
if (state == STATE_ACTIVE) {
fileSource.activate();
} else {
fileSource.deactivate();
}

this.state = state;
setOfflineRegionDownloadState(state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static String getCachePath(Context context) {
MapboxConstants.KEY_META_DATA_SET_STORAGE_EXTERNAL,
MapboxConstants.DEFAULT_SET_STORAGE_EXTERNAL);
} catch (PackageManager.NameNotFoundException exception) {
Timber.e(exception,"Failed to read the package metadata: ");
Timber.e(exception, "Failed to read the package metadata: ");
} catch (Exception exception) {
Timber.e(exception, "Failed to read the storage key: ");
}
Expand Down Expand Up @@ -119,17 +119,39 @@ 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 void deactivate() {
activeCounter--;
if (activeCounter == 0) {
wasPaused = true;
pause();
}
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ public void onStatusChanged(OfflineRegionStatus status) {
if (status.isComplete()) {
// Download complete
endProgress("Region downloaded successfully.");
offlineRegion.setDownloadState(OfflineRegion.STATE_INACTIVE);
offlineRegion.setObserver(null);
return;
} else if (status.isRequiredResourceCountPrecise()) {
Expand All @@ -281,11 +282,13 @@ public void onStatusChanged(OfflineRegionStatus status) {
@Override
public void onError(OfflineRegionError error) {
Timber.e("onError: %s, %s", error.getReason(), error.getMessage());
offlineRegion.setDownloadState(OfflineRegion.STATE_INACTIVE);
}

@Override
public void mapboxTileCountLimitExceeded(long limit) {
Timber.e("Mapbox tile count limit exceeded: %s", limit);
offlineRegion.setDownloadState(OfflineRegion.STATE_INACTIVE);
}
});

Expand Down
14 changes: 11 additions & 3 deletions platform/android/src/file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include "asset_manager_file_source.hpp"
#include "jni/generic_global_ref_deleter.hpp"

#include <string>

namespace mbgl {
namespace android {

Expand Down Expand Up @@ -64,6 +62,14 @@ void FileSource::setResourceTransform(jni::JNIEnv& env, jni::Object<FileSource::
}
}

void FileSource::resume(jni::JNIEnv&) {
fileSource->resume();
}

void FileSource::pause(jni::JNIEnv&) {
fileSource->pause();
}

jni::Class<FileSource> FileSource::javaClass;

FileSource* FileSource::getNativePeer(jni::JNIEnv& env, jni::Object<FileSource> jFileSource) {
Expand Down Expand Up @@ -93,7 +99,9 @@ void FileSource::registerNative(jni::JNIEnv& env) {
METHOD(&FileSource::getAccessToken, "getAccessToken"),
METHOD(&FileSource::setAccessToken, "setAccessToken"),
METHOD(&FileSource::setAPIBaseUrl, "setApiBaseUrl"),
METHOD(&FileSource::setResourceTransform, "setResourceTransform")
METHOD(&FileSource::setResourceTransform, "setResourceTransform"),
METHOD(&FileSource::resume, "resume"),
METHOD(&FileSource::pause, "pause")
);
}

Expand Down
4 changes: 4 additions & 0 deletions platform/android/src/file_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class FileSource {

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

void resume(jni::JNIEnv&);

void pause(jni::JNIEnv&);

static jni::Class<FileSource> javaClass;

static FileSource* getNativePeer(jni::JNIEnv&, jni::Object<FileSource>);
Expand Down

0 comments on commit 21d6313

Please sign in to comment.