public class OfflineManager extends 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.
|
Modifier and Type | Method and Description |
---|---|
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(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(String path,
OfflineManager.MergeOfflineRegionsCallback callback)
Merge offline regions from a secondary database into the main offline database.
|
void |
putResourceWithUrl(String url,
byte[] data,
long modified,
long expires,
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 |
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(Context context)
context
- the context used to host the offline managerpublic void listOfflineRegions(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(String path, 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(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 invalidateAmbientCache(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(OfflineManager.FileSourceCallback callback)
Erases the ambient cache, freeing resources. This operation can be potentially slow because it will trigger a VACUUM on SQLite, forcing the database to move pages on the filesystem.
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, 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(OfflineRegionDefinition definition, byte[] metadata, 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 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.deleteOfflineRegion(OfflineRegion.OfflineRegionDeleteCallback)
.
limit
- the maximum number of tiles allowed to be downloadedprotected void finalize() throws Throwable
public void putResourceWithUrl(String url, byte[] data, long modified, long expires, 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© 2015–2019 Mapbox. All rights reserved.