diff options
author | Antonio Zugaldia <antonio@mapbox.com> | 2016-02-17 10:55:12 -0500 |
---|---|---|
committer | Antonio Zugaldia <antonio@mapbox.com> | 2016-02-26 09:13:17 -0500 |
commit | 01e55f183e4468d040e7b144536ca592c9b64cb5 (patch) | |
tree | f448bd208c3b43650ae5aa3918090f298d708fbf /platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java | |
parent | f683b10e106e7376322b9c67c2baf57f5022cec2 (diff) | |
download | qtlocation-mapboxgl-01e55f183e4468d040e7b144536ca592c9b64cb5.tar.gz |
# This is a combination of 8 commits.
# The first commit's message is:
# This is a combination of 2 commits.
# The first commit's message is:
# This is a combination of 3 commits.
# The first commit's message is:
# This is a combination of 2 commits.
# The first commit's message is:
# This is a combination of 21 commits.
# The first commit's message is:
[android] - Implements Android bindings for offline API
Fixes #3891
# The 2nd commit message will be skipped:
# [android] #3891 - rename OFFLINE_MAX_CACHE_SIZE to DEFAULT_MAX_CACHE_SIZE and adjust value
# The 3rd commit message will be skipped:
# [android] #3891 - makes de documentation more relevant to the current android implementation
# The 4th commit message will be skipped:
# [android] #3891 - rename isRequiredResourceCountIsIndeterminate() to isRequiredResourceCountPrecise()
# The 5th commit message will be skipped:
# [android] #3891 - rename complete() to isComplete()
# The 6th commit message will be skipped:
# [android] #3891 - rename OfflineRegionDefinition to OfflineTilePyramidRegionDefinition and make OfflineRegionDefinition an interface. Docs for corresponding classes updated.
# The 7th commit message will be skipped:
# [android] #3891 - make reason a more idiomatic ErrorReason Android IntDef and remove unnecessary constructor
# The 8th commit message will be skipped:
# [android] #3891 - reuse the calling object instead of creating a new manager
# The 9th commit message will be skipped:
# [android] #3891 - location, location, location
# The 10th commit message will be skipped:
# [android] #3891 - simpler list regions iteration
# The 11th commit message will be skipped:
# [android] #3891 - proper indeterminate -> precise transition
# The 12th commit message will be skipped:
# [android] #3891 - improve description for DEFAULT_MAX_CACHE_SIZE
# The 13th commit message will be skipped:
# [android] #3891 - delete global refs for obj and listCallback
# The 14th commit message will be skipped:
# [android] #3891 - simplify metadata conversion and fix metadata object
# The 15th commit message will be skipped:
# [android] - Implements Android bindings for offline API
# Fixes #3891
# The 16th commit message will be skipped:
# [android] #3891 - avoid exposing the int reason value in the public API
# The 17th commit message will be skipped:
# [android] #3891 - delete global refs for remaining callbacks and observer
# The 18th commit message will be skipped:
# [android] #3891 - remove unused offlineManagerClassConstructorId together with unnecessary private java constructor
# The 19th commit message will be skipped:
# [android] #3891 - remove non-relevant line
# The 20th commit message will be skipped:
# [android] #3891 - handle requiredResourceCountIsIndeterminate -> requiredResourceCountIsPrecise rename
# The 21st commit message will be skipped:
# [android] #3891 - revert map changes to allow rebase
# The 2nd commit message will be skipped:
# [android] #3891 - avoid exposing the int reason value in the public API
# The 2nd commit message will be skipped:
# [android] #3891 - rename complete() to isComplete()
# The 3rd commit message will be skipped:
# [android] #3891 - rename OfflineRegionDefinition to OfflineTilePyramidRegionDefinition and make OfflineRegionDefinition an interface. Docs for corresponding classes updated.
# The 2nd commit message will be skipped:
# [android] #3891 - location, location, location
# The 2nd commit message will be skipped:
# [android] #3891 - improve description for DEFAULT_MAX_CACHE_SIZE
# The 3rd commit message will be skipped:
# [android] #3891 - delete global refs for obj and listCallback
# The 4th commit message will be skipped:
# [android] #3891 - simplify metadata conversion and fix metadata object
# The 5th commit message will be skipped:
# [android] #3891 - delete global refs for remaining callbacks and observer
# The 6th commit message will be skipped:
# [android] #3891 - remove unused offlineManagerClassConstructorId together with unnecessary private java constructor
# The 7th commit message will be skipped:
# [android] #3891 - remove non-relevant line
# The 8th commit message will be skipped:
# [android] #3891 - handle requiredResourceCountIsIndeterminate -> requiredResourceCountIsPrecise rename
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java')
-rw-r--r-- | platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java | 184 |
1 files changed, 184 insertions, 0 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 new file mode 100644 index 0000000000..b56ecfc057 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java @@ -0,0 +1,184 @@ +package com.mapbox.mapboxsdk.offline; + +import android.content.Context; +import android.os.Handler; +import android.os.Looper; +import android.support.annotation.NonNull; + +import java.io.File; + +/** + * The offline manager is the main entry point for offline-related functionality. + * It'll help you list and create offline regions. + */ +public class OfflineManager { + + // Default database name + private final static String OFFLINE_DATABASE_NAME = "mbgl-offline.db"; + + /* + * The maximumCacheSize parameter is a limit applied to non-offline resources only, + * i.e. resources added to the database for the "ambient use" caching functionality. + * There is no size limit for offline resources. + */ + private final static long DEFAULT_MAX_CACHE_SIZE = 50 * 1024 * 1024; + + // Holds the pointer to JNI DefaultFileSource + private long mDefaultFileSourcePtr = 0; + + // Makes sure callbacks come back to the main thread + private Handler handler; + + // This object is implemented as a singleton + private static OfflineManager instance; + + /* + * Callbacks + */ + + public interface ListOfflineRegionsCallback { + void onList(OfflineRegion[] offlineRegions); + void onError(String error); + } + + public interface CreateOfflineRegionCallback { + void onCreate(OfflineRegion offlineRegion); + void onError(String error); + } + + /* + * Constructors + */ + + private OfflineManager(Context context) { + // Get a pointer to the DefaultFileSource instance + String assetRoot = context.getFilesDir().getAbsolutePath(); + String cachePath = assetRoot + File.separator + OFFLINE_DATABASE_NAME; + mDefaultFileSourcePtr = createDefaultFileSource(cachePath, assetRoot, DEFAULT_MAX_CACHE_SIZE); + } + + public static synchronized OfflineManager getInstance(Context context) { + if (instance == null) { + instance = new OfflineManager(context); + } + + return instance; + } + + /* + * Access token getter/setter + */ + public void setAccessToken(String accessToken) { + setAccessToken(mDefaultFileSourcePtr, accessToken); + } + + public String getAccessToken() { + return getAccessToken(mDefaultFileSourcePtr); + } + + private Handler getHandler() { + if (handler == null) { + handler = new Handler(Looper.getMainLooper()); + } + + return handler; + } + + /** + * Retrieve all regions in the offline database. + * + * The query will be executed asynchronously and the results passed to the given + * callback on the main thread. + */ + public void listOfflineRegions(@NonNull final ListOfflineRegionsCallback callback) { + listOfflineRegions(mDefaultFileSourcePtr, new ListOfflineRegionsCallback() { + @Override + public void onList(final OfflineRegion[] offlineRegions) { + getHandler().post(new Runnable() { + @Override + public void run() { + callback.onList(offlineRegions); + } + }); + } + + @Override + public void onError(final String error) { + getHandler().post(new Runnable() { + @Override + public void run() { + callback.onError(error); + } + }); + } + }); + } + + /** + * Create an offline region in the database. + * + * When the initial database queries have completed, the provided callback will be + * executed on the main thread. + * + * Note that the resulting region will be in an inactive download state; to begin + * downloading resources, call `OfflineRegion.setDownloadState(DownloadState.STATE_ACTIVE)`, + * optionally registering an `OfflineRegionObserver` beforehand. + */ + public void createOfflineRegion( + @NonNull OfflineRegionDefinition definition, + @NonNull OfflineRegionMetadata metadata, + @NonNull final CreateOfflineRegionCallback callback) { + + createOfflineRegion(mDefaultFileSourcePtr, definition, metadata, new CreateOfflineRegionCallback() { + @Override + public void onCreate(final OfflineRegion offlineRegion) { + getHandler().post(new Runnable() { + @Override + public void run() { + callback.onCreate(offlineRegion); + } + }); + } + + @Override + public void onError(final String error) { + getHandler().post(new Runnable() { + @Override + public void run() { + callback.onError(error); + } + }); + } + }); + } + + /* + * Changing or bypassing this limit without permission from Mapbox is prohibited + * by the Mapbox Terms of Service. + */ + public void setOfflineMapboxTileCountLimit(long limit) { + setOfflineMapboxTileCountLimit(mDefaultFileSourcePtr, limit); + } + + + /* + * Native methods + */ + + private native long createDefaultFileSource( + String cachePath, String assetRoot, long maximumCacheSize); + + private native void setAccessToken(long defaultFileSourcePtr, String accessToken); + private native String getAccessToken(long defaultFileSourcePtr); + + private native void listOfflineRegions( + long defaultFileSourcePtr, ListOfflineRegionsCallback callback); + + private native void createOfflineRegion( + long defaultFileSourcePtr, OfflineRegionDefinition definition, + OfflineRegionMetadata metadata, CreateOfflineRegionCallback callback); + + private native void setOfflineMapboxTileCountLimit( + long defaultFileSourcePtr, long limit); + +} |