Add cache path variable from Java
Use cache path for db location Add Assetbridge to extract asset files to cache path Fix SSL by using extracted ca bundle Use file:// style URL using extracted styles
This commit is contained in:
3
android/java/.gitignore
vendored
3
android/java/.gitignore
vendored
@@ -10,5 +10,8 @@ build/
|
||||
*/build/
|
||||
*.so
|
||||
|
||||
# Lib assets
|
||||
lib/src/main/assets/
|
||||
|
||||
# Local settings
|
||||
local.properties
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package com.arieslabs.assetbridge;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
import android.util.Log;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
|
||||
|
||||
public class Assetbridge {
|
||||
/*static {
|
||||
System.loadLibrary("assetbridge");
|
||||
}*/
|
||||
|
||||
|
||||
// unpack
|
||||
public static void unpack(Context c) {
|
||||
|
||||
try {
|
||||
// first let's get the temp directory
|
||||
String tmpdir = c.getCacheDir().getPath();
|
||||
|
||||
// now we need the assetmanager
|
||||
AssetManager am = c.getAssets();
|
||||
String[] assets = am.list("");
|
||||
|
||||
// iterate on the files...
|
||||
for(String asset : assets) {
|
||||
copyAssetFolder(am, asset, tmpdir + "/" + asset);
|
||||
}
|
||||
|
||||
// last, set the ASSETDIR environment variable for the C
|
||||
// parts of the procee
|
||||
//setassetdir(c.getCacheDir().getPath());
|
||||
|
||||
} catch (IOException e) {
|
||||
Log.e("Assetbridge", "Can't unpack assets from APK", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void copyAssetFolder(AssetManager am, String src, String dest)
|
||||
throws IOException{
|
||||
|
||||
InputStream srcIS = null;
|
||||
File destfh;
|
||||
|
||||
// this is the only way we can tell if this is a file or a
|
||||
// folder - we have to open the asset, and if the open fails,
|
||||
// it's a folder...
|
||||
boolean isDir = false;
|
||||
try {
|
||||
srcIS = am.open(src);
|
||||
} catch (FileNotFoundException e) {
|
||||
isDir = true;
|
||||
}
|
||||
|
||||
// either way, we'll use the dest as a File
|
||||
destfh = new File(dest);
|
||||
|
||||
// and now, depending on ..
|
||||
if(isDir) {
|
||||
|
||||
// If the directory doesn't yet exist, create it
|
||||
if( !destfh.exists() ){
|
||||
destfh.mkdir();
|
||||
}
|
||||
|
||||
// list the assets in the directory...
|
||||
String assets[] = am.list(src);
|
||||
|
||||
// and copy them all using same.
|
||||
for(String asset : assets) {
|
||||
copyAssetFolder(am, src + "/" + asset, dest + "/" + asset);
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
int count, buffer_len = 2048;
|
||||
byte[] data = new byte[buffer_len];
|
||||
|
||||
// copy the file from the assets subsystem to the filesystem
|
||||
FileOutputStream destOS = new FileOutputStream(destfh);
|
||||
|
||||
//copy the file content in bytes
|
||||
while( (count = srcIS.read(data, 0, buffer_len)) != -1) {
|
||||
destOS.write(data, 0, count);
|
||||
}
|
||||
|
||||
// and close the two files
|
||||
srcIS.close();
|
||||
destOS.close();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// the native method to set the environment variable
|
||||
//public static native void setassetdir(String s);
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import android.widget.ZoomButtonsController;
|
||||
|
||||
import com.almeros.android.multitouch.gesturedetectors.RotateGestureDetector;
|
||||
import com.almeros.android.multitouch.gesturedetectors.TwoFingerGestureDetector;
|
||||
import com.arieslabs.assetbridge.Assetbridge;
|
||||
|
||||
// Custom view that shows a Map
|
||||
// Based on SurfaceView as we use OpenGL ES to render
|
||||
@@ -118,12 +119,19 @@ public class MapView extends SurfaceView {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the cache path
|
||||
String cachePath = context.getCacheDir().getPath();
|
||||
|
||||
// Extract the asset files
|
||||
Assetbridge.unpack(context);
|
||||
|
||||
// Load the map style and API key
|
||||
mStyleUrl = "https://mapbox.github.io/mapbox-gl-styles/styles/bright-v6.json";
|
||||
//mStyleUrl = "https://mapbox.github.io/mapbox-gl-styles/styles/bright-v6.json";
|
||||
mStyleUrl = "file://" + cachePath + "/styles/styles/bright-v6.json";
|
||||
mApiKey = "pk.eyJ1IjoibGpiYWRlIiwiYSI6IlJSQ0FEZ2MifQ.7mE4aOegldh3595AG9dxpQ";
|
||||
|
||||
// Create the NativeMapView
|
||||
mNativeMapView = new NativeMapView(this, mStyleUrl, mApiKey);
|
||||
mNativeMapView = new NativeMapView(this, cachePath, mStyleUrl, mApiKey);
|
||||
|
||||
// Load the attributes
|
||||
TypedArray typedArray = context.obtainStyledAttributes(attrs,
|
||||
|
||||
@@ -37,11 +37,11 @@ class NativeMapView {
|
||||
// Constructors
|
||||
//
|
||||
|
||||
public NativeMapView(MapView mapView, String styleUrl, String apiKey) {
|
||||
public NativeMapView(MapView mapView, String cachePath, String styleUrl, String apiKey) {
|
||||
mMapView = mapView;
|
||||
|
||||
// Create the NativeMapView
|
||||
mNativeMapViewPtr = nativeCreate(styleUrl, apiKey);
|
||||
mNativeMapViewPtr = nativeCreate(cachePath, styleUrl, apiKey);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -359,7 +359,7 @@ class NativeMapView {
|
||||
super.finalize();
|
||||
}
|
||||
|
||||
private native long nativeCreate(String styleUrl, String apiKey);
|
||||
private native long nativeCreate(String cachePath, String styleUrl, String apiKey);
|
||||
|
||||
private native void nativeDestroy(long nativeMapViewPtr);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user