@UiThread
public class OfflineManager
extends java.lang.Object
It'll help you list and create offline regions.
Modifier and Type | Class and Description |
---|---|
static interface |
OfflineManager.CreateOfflineRegionCallback
This callback receives an asynchronous response containing the newly created
OfflineRegion in the database or an error message otherwise.
|
static interface |
OfflineManager.FileSourceCallback
This callback receives an asynchronous response indicating if an operation has succeeded or failed.
|
static interface |
OfflineManager.ListOfflineRegionsCallback
This callback receives an asynchronous response containing a list of all
OfflineRegion in the database or an error message otherwise.
|
static interface |
OfflineManager.MergeOfflineRegionsCallback
This callback receives an asynchronous response containing a list of all
OfflineRegion added to the database during the merge.
|
static interface |
OfflineManager.PrefetchAmbientCacheCallback
This callback receives an asynchronous response containing a the request ID
of the prefetch ambient cache request.
|
Modifier and Type | Method and Description |
---|---|
void |
cancelPrefetchAmbientCacheRequest(long requestId)
Cancel the prefetch ambient cache request.
|
void |
clearAmbientCache(OfflineManager.FileSourceCallback callback)
Erase resources from the ambient cache, freeing storage space.
|
void |
createOfflineRegion(OfflineRegionDefinition definition,
byte[] metadata,
OfflineManager.CreateOfflineRegionCallback callback)
Creates an offline region in the database by downloading the resources needed to use
the given region offline.
|
protected void |
finalize() |
static OfflineManager |
getInstance(android.content.Context context)
Get the single instance of offline manager.
|
void |
invalidateAmbientCache(OfflineManager.FileSourceCallback callback)
Forces re-validation of the ambient cache.
|
void |
listOfflineRegions(OfflineManager.ListOfflineRegionsCallback callback)
Retrieve all regions in the offline database.
|
void |
mergeOfflineRegions(java.lang.String path,
OfflineManager.MergeOfflineRegionsCallback callback)
Merge offline regions from a secondary database into the main offline database.
|
void |
packDatabase(OfflineManager.FileSourceCallback callback)
Packs the existing database file into a minimal amount of disk space.
|
long |
prefetchAmbientCache(CacheAreaDefinition definition,
OfflineManager.PrefetchAmbientCacheCallback callback)
Pre-fetch resources from network and populates the ambient cache.
|
void |
putResourceWithUrl(java.lang.String url,
byte[] data,
long modified,
long expires,
java.lang.String etag,
boolean mustRevalidate)
Insert the provided resource into the ambient cache
This method mimics the caching that would take place if the equivalent
resource were requested in the process of map rendering.
|
void |
resetDatabase(OfflineManager.FileSourceCallback callback)
Delete existing database and re-initialize.
|
void |
runPackDatabaseAutomatically(boolean autopack)
Sets whether database file packing occurs automatically.
|
void |
setMaximumAmbientCacheSize(long size,
OfflineManager.FileSourceCallback callback)
Sets the maximum size in bytes for the ambient cache.
|
void |
setOfflineMapboxTileCountLimit(long limit)
Sets the maximum number of Mapbox-hosted tiles that may be downloaded and stored on the current device.
|
public static OfflineManager getInstance(@NonNull android.content.Context context)
context
- the context used to host the offline managerpublic void listOfflineRegions(@NonNull OfflineManager.ListOfflineRegionsCallback callback)
The query will be executed asynchronously and the results passed to the given callback on the main thread.
callback
- the callback to be invokedpublic void mergeOfflineRegions(@NonNull java.lang.String path, @NonNull OfflineManager.MergeOfflineRegionsCallback callback)
When the merge is completed, or fails, the OfflineManager.MergeOfflineRegionsCallback
will be invoked on the main thread.
The callback reference is strongly kept throughout the process,
so it needs to be wrapped in a weak reference or released on the client side if necessary.
The secondary database may need to be upgraded to the latest schema. This is done in-place and requires write-access to the provided path. If the app's process doesn't have write-access to the provided path, the file will be copied to the temporary, internal directory for the duration of the merge.
Only resources and tiles that belong to a region will be copied over. Identical regions will be flattened into a single new region in the main database.
The operation will be aborted and OfflineManager.MergeOfflineRegionsCallback.onError(String)
with an appropriate message
will be invoked if the merge would result in the offline tile count limit being exceeded.
Merged regions may not be in a completed status if the secondary database does not contain all the tiles or resources required by the region definition.
path
- secondary database writable pathcallback
- completion/error callbackpublic void resetDatabase(@Nullable OfflineManager.FileSourceCallback callback)
When the operation is complete or encounters an error, the given callback will be executed on the database thread; it is the responsibility of the SDK bindings to re-execute a user-provided callback on the main thread.
callback
- the callback to be invoked when the database was reset or when the operation erred.public void packDatabase(@Nullable OfflineManager.FileSourceCallback callback)
This operation has a performance impact as it will vacuum the database, forcing it to move pages on the filesystem.
When the operation is complete or encounters an error, the given callback will be executed on the database thread; it is the responsibility of the SDK bindings to re-execute a user-provided callback on the main thread.
callback
- the callback to be invoked when the database was reset or when the operation erred.public void invalidateAmbientCache(@Nullable OfflineManager.FileSourceCallback callback)
Forces Mapbox GL Native to revalidate resources stored in the ambient cache with the tile server before using them, making sure they are the latest version. This is more efficient than cleaning the cache because if the resource is considered valid after the server lookup, it will not get downloaded again.
Resources overlapping with offline regions will not be affected by this call.
callback
- the callback to be invoked when the ambient cache was invalidated or when the operation erred.public void clearAmbientCache(@Nullable OfflineManager.FileSourceCallback callback)
Erases the ambient cache, freeing resources.
Note that this operation can be potentially slow if packing the database
occurs automatically (runPackDatabaseAutomatically(boolean)
).
Resources overlapping with offline regions will not be affected by this call.
callback
- the callback to be invoked when the ambient cache was cleared or when the operation erred.public void setMaximumAmbientCacheSize(long size, @Nullable OfflineManager.FileSourceCallback callback)
This call is potentially expensive because it will try to trim the data in case the database is larger than the size defined. The size of offline regions are not affected by this settings, but the ambient cache will always try to not exceed the maximum size defined, taking into account the current size for the offline regions.
Note that if you use the SDK's offline functionality, your ability to set the ambient cache size will be limited. Space that offline regions take up detract from the space available for ambient caching, and the ambient cache size does not block offline downloads. For example: if the maximum cache size is set to 50 MB and 40 MB are already used by offline regions, the ambient cache size will effectively be 10 MB.
Setting the size to 0 will disable the cache if there is no offline region on the database.
<[This method should always be called at the start of an app, before setting the style and loading a map. Otherwise, the map will instantiate with the default cache size of 50 MB.
size
- the maximum size of the ambient cachecallback
- the callback to be invoked when the the maximum size has been set or when the operation erred.public void createOfflineRegion(@NonNull OfflineRegionDefinition definition, @NonNull byte[] metadata, @NonNull OfflineManager.CreateOfflineRegionCallback callback)
As of version 8.3.0 of the Maps SDK for Android, offline tile requests are no longer exempt from billing on mobile. Developers are subject to separate Vector Tile or Raster Tile API pricing for offline use. See our pricing page for more information.
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.
definition
- the offline region definitionmetadata
- the metadata in bytescallback
- the callback to be invokedpublic long prefetchAmbientCache(@NonNull CacheAreaDefinition definition, @NonNull OfflineManager.PrefetchAmbientCacheCallback callback)
definition
- the cache area definitioncallback
- the callback to be invokedpublic void cancelPrefetchAmbientCacheRequest(long requestId)
requestId
- the id of the prefetch ambient cache requestpublic void setOfflineMapboxTileCountLimit(long limit)
Once this limit is reached, OfflineRegion.OfflineRegionObserver.mapboxTileCountLimitExceeded(long)
fires every additional attempt to download additional tiles until already downloaded tiles are removed
by calling OfflineRegion.delete(OfflineRegion.OfflineRegionDeleteCallback)
.
limit
- the maximum number of tiles allowed to be downloadedpublic void runPackDatabaseAutomatically(boolean autopack)
If packing is enabled, database file packing occurs automatically
after an offline region is deleted by calling
OfflineRegion.delete(OfflineRegion.OfflineRegionDeleteCallback)
or the ambient cache is cleared by calling OfflineManager#clearAmbientCache()
.
If packing is disabled, disk space will not be freed after
resources are removed unless OfflineManager#packDatabase()
is explicitly called.
autopack
- flag setting the automatic database file packing.protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
public void putResourceWithUrl(java.lang.String url, byte[] data, long modified, long expires, java.lang.String etag, boolean mustRevalidate)
This call is asynchronous: the data may not be immediately available for in-progress requests, although subsequent requests should have access to the cached data.
url
- The URL of the resource to insertdata
- Response data to store for this resource. Data is expected to be uncompressed;
internally, the cache will compress data as necessary.modified
- Optional "modified" response header, in seconds since 1970, or 0 if not setexpires
- Optional "expires" response header, in seconds since 1970, or 0 if not setetag
- Optional "entity tag" response headermustRevalidate
- Indicates whether response can be used after it's stale