summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2017-02-16 16:16:47 +0200
committerIvo van Dongen <ivovandongen@users.noreply.github.com>2017-02-24 17:54:20 +0200
commit465c949153a838bb3159204ab268eb551fbd2e6c (patch)
tree250922b879261580b1252a78b461218be9012bc0 /platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineManager.java
parent7d9018093a61d327fa7ca1312845d2a00d928380 (diff)
downloadqtlocation-mapboxgl-465c949153a838bb3159204ab268eb551fbd2e6c.tar.gz
[android] jni high level binding refactor
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.java148
1 files changed, 21 insertions, 127 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
index 09acda792c..cda79e30c6 100644
--- 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
@@ -1,16 +1,11 @@
package com.mapbox.mapboxsdk.offline;
import android.content.Context;
-import android.content.pm.ApplicationInfo;
-import android.content.pm.PackageManager;
-import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
-import com.mapbox.mapboxsdk.Mapbox;
-import com.mapbox.mapboxsdk.constants.MapboxConstants;
-import com.mapbox.mapboxsdk.storage.Resource;
+import com.mapbox.mapboxsdk.storage.FileSource;
import java.io.File;
@@ -30,11 +25,13 @@ public class OfflineManager {
System.loadLibrary("mapbox-gl");
}
- // Default database name
- private static final String DATABASE_NAME = "mbgl-offline.db";
- // Holds the pointer to JNI DefaultFileSource
- private long mDefaultFileSourcePtr = 0;
+ // Native peer pointer
+ private long nativePtr;
+
+ // Reference to the file source to keep it alive for the
+ // lifetime of this object
+ private final FileSource fileSource;
// Makes sure callbacks come back to the main thread
private Handler handler;
@@ -82,94 +79,17 @@ public class OfflineManager {
void onError(String error);
}
- /**
- * This callback allows implementors to transform URLs before they are requested
- * from the internet. This can be used add or remove custom parameters, or reroute
- * certain requests to other servers or endpoints.
- */
- public interface ResourceTransformCallback {
- /**
- * Called whenever a URL needs to be transformed.
- *
- * @param kind The kind of URL to be transformed.
- * @param offlineRegions The original URL to be transformed.
- * @return A URL that will now be downloaded.
- */
- String onURL(@Resource.Kind int kind, String url);
- }
-
/*
- * Constructors
+ * Constructor
*/
private OfflineManager(Context context) {
- // Get a pointer to the DefaultFileSource instance
- String cachePath = getDatabasePath(context) + File.separator + DATABASE_NAME;
- String apkPath = context.getPackageCodePath();
- mDefaultFileSourcePtr = sharedDefaultFileSource(cachePath, apkPath);
- setAccessToken(mDefaultFileSourcePtr, Mapbox.getAccessToken());
+ this.fileSource = FileSource.getInstance(context);
+ initialize(fileSource);
// Delete any existing previous ambient cache database
deleteAmbientDatabase(context);
}
- public static String getDatabasePath(Context context) {
- // Default value
- boolean setStorageExternal = MapboxConstants.DEFAULT_SET_STORAGE_EXTERNAL;
-
- try {
- // Try getting a custom value from the app Manifest
- ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
- context.getPackageName(), PackageManager.GET_META_DATA);
- setStorageExternal = appInfo.metaData.getBoolean(
- MapboxConstants.KEY_META_DATA_SET_STORAGE_EXTERNAL,
- MapboxConstants.DEFAULT_SET_STORAGE_EXTERNAL);
- } catch (PackageManager.NameNotFoundException exception) {
- Timber.e("Failed to read the package metadata: ", exception);
- } catch (Exception exception) {
- Timber.e("Failed to read the storage key: ", exception);
- }
-
- String databasePath = null;
- if (setStorageExternal && isExternalStorageReadable()) {
- try {
- // Try getting the external storage path
- databasePath = context.getExternalFilesDir(null).getAbsolutePath();
- } catch (NullPointerException exception) {
- Timber.e("Failed to obtain the external storage path: ", exception);
- }
- }
-
- if (databasePath == null) {
- // Default to internal storage
- databasePath = context.getFilesDir().getAbsolutePath();
- }
-
- return databasePath;
- }
-
- /**
- * Checks if external storage is available to at least read. In order for this to work, make
- * sure you include &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
- * (or WRITE_EXTERNAL_STORAGE) for API level &lt; 18 in your app Manifest.
- * <p>
- * Code from https://developer.android.com/guide/topics/data/data-storage.html#filesExternal
- * </p>
- *
- * @return true if external storage is readable
- */
- public static boolean isExternalStorageReadable() {
- String state = Environment.getExternalStorageState();
- if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
- return true;
- }
-
- Timber.w("External storage was requested but it isn't readable. For API level < 18"
- + " make sure you've requested READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE"
- + " permissions in your app Manifest (defaulting to internal storage).");
-
- return false;
- }
-
private void deleteAmbientDatabase(final Context context) {
// Delete the file in a separate thread to avoid affecting the UI
new Thread(new Runnable() {
@@ -215,7 +135,8 @@ public class OfflineManager {
* @param callback the callback to be invoked
*/
public void listOfflineRegions(@NonNull final ListOfflineRegionsCallback callback) {
- listOfflineRegions(mDefaultFileSourcePtr, new ListOfflineRegionsCallback() {
+ listOfflineRegions(fileSource, new ListOfflineRegionsCallback() {
+
@Override
public void onList(final OfflineRegion[] offlineRegions) {
getHandler().post(new Runnable() {
@@ -259,7 +180,8 @@ public class OfflineManager {
@NonNull byte[] metadata,
@NonNull final CreateOfflineRegionCallback callback) {
- createOfflineRegion(mDefaultFileSourcePtr, definition, metadata, new CreateOfflineRegionCallback() {
+ createOfflineRegion(fileSource, definition, metadata, new CreateOfflineRegionCallback() {
+
@Override
public void onCreate(final OfflineRegion offlineRegion) {
getHandler().post(new Runnable() {
@@ -282,48 +204,20 @@ public class OfflineManager {
});
}
- /**
- * Sets a callback for transforming URLs requested from the internet
- * <p>
- * The callback will be executed on the main thread once for every requested URL.
- * </p>
- *
- * @param callback the callback to be invoked
- */
- public void setResourceTransform(@NonNull final ResourceTransformCallback callback) {
- setResourceTransform(mDefaultFileSourcePtr, callback);
- }
-
/*
* 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 sharedDefaultFileSource(
- String cachePath, String assetRoot);
-
- private native void setAccessToken(long defaultFileSourcePtr, String accessToken);
-
- private native String getAccessToken(long defaultFileSourcePtr);
+ public native void setOfflineMapboxTileCountLimit(long limit);
- private native void listOfflineRegions(
- long defaultFileSourcePtr, ListOfflineRegionsCallback callback);
+ private native void initialize(FileSource fileSource);
- private native void createOfflineRegion(
- long defaultFileSourcePtr, OfflineRegionDefinition definition,
- byte[] metadata, CreateOfflineRegionCallback callback);
+ @Override
+ protected native void finalize() throws Throwable;
- private native void setResourceTransform(
- long defaultFileSourcePtr, ResourceTransformCallback callback);
+ private native void listOfflineRegions(FileSource fileSource, ListOfflineRegionsCallback callback);
- private native void setOfflineMapboxTileCountLimit(
- long defaultFileSourcePtr, long limit);
+ private native void createOfflineRegion(FileSource fileSource, OfflineRegionDefinition definition,
+ byte[] metadata, CreateOfflineRegionCallback callback);
}