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:
Leith Bade
2014-11-08 12:49:46 +11:00
parent 9e81ab5228
commit c0bcd1e260
10 changed files with 167 additions and 12 deletions

View File

@@ -87,6 +87,9 @@ android:
$(MAKE) -C build/android BUILDTYPE=$(BUILDTYPE) V=$(V) androidapp && \
mkdir -p android/java/lib/src/main/jniLibs/armeabi-v7a && \
cp build/android/out/$(BUILDTYPE)/lib.target/libmapbox-gl.so android/java/lib/src/main/jniLibs/armeabi-v7a/libmapbox-gl.so && \
mkdir -p android/java/lib/src/main/assets && \
cp build/android/out/$(BUILDTYPE)/ca-bundle.crt android/java/lib/src/main/assets/ca-bundle.crt && \
cp -r build/android/out/$(BUILDTYPE)/styles android/java/lib/src/main/assets/styles && \
cd android/java && \
./gradlew build
@@ -154,7 +157,7 @@ clean: clear_xcode_cache
-rm -rf ./macosx/build/
-rm -rf ./config.gypi ./config-ios.gypi ./config-android.gypi
-rm -rf ./android/java/build ./android/java/app/build ./android/java/lib/build
-rm -rf ./android/java/lib/src/main/jniLibs
-rm -rf ./android/java/lib/src/main/jniLibs ./android/java/lib/src/main/assets
distclean: clean
-rm -rf ./mason_packages

View File

@@ -0,0 +1,23 @@
Copyright (c) 2013 Steve Havelka
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -20,6 +20,8 @@
namespace mbgl {
namespace android {
std::string cache_path;
jmethodID on_map_changed_id = nullptr;
jclass lon_lat_class = nullptr;
@@ -140,8 +142,9 @@ using namespace mbgl::android;
// TODO: wrap C++ exceptions?
// TODO: wrap other sorts of exceptions? eg coffee catch
jlong JNICALL nativeCreate(JNIEnv* env, jobject obj, jstring style_url, jstring api_key) {
jlong JNICALL nativeCreate(JNIEnv* env, jobject obj, jstring cache_path_, jstring style_url, jstring api_key) {
LOG_VERBOSE("nativeCreate");
cache_path = std_string_from_jstring(env, cache_path_);
NativeMapView* native_map_view = new NativeMapView(env, obj, std_string_from_jstring(env, style_url), std_string_from_jstring(env, api_key));
if (native_map_view == nullptr) {
throw_error(env, "Unable to create NativeMapView.");
@@ -684,7 +687,7 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
// NOTE: if you get java.lang.UnsatisfiedLinkError you likely forgot to set the size of the array correctly (too large)
std::array<JNINativeMethod, 50> methods = {{ // Can remove the extra brace in C++14
{ "nativeCreate", "(Ljava/lang/String;Ljava/lang/String;)J", reinterpret_cast<void*>(&nativeCreate) },
{ "nativeCreate", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)J", reinterpret_cast<void*>(&nativeCreate) },
{ "nativeDestroy", "(J)V", reinterpret_cast<void*>(&nativeDestroy) },
{ "nativeInitializeDisplay", "(J)V", reinterpret_cast<void*>(&nativeInitializeDisplay) },
{ "nativeTerminateDisplay", "(J)V", reinterpret_cast<void*>(&nativeTerminateDisplay) },

View File

@@ -14,6 +14,8 @@
namespace mbgl {
namespace android {
extern std::string cache_path;
extern jmethodID on_map_changed_id;
extern jclass lon_lat_class;

View File

@@ -10,5 +10,8 @@ build/
*/build/
*.so
# Lib assets
lib/src/main/assets/
# Local settings
local.properties

View File

@@ -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);
}

View File

@@ -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,

View File

@@ -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);

View File

@@ -1,4 +1,5 @@
#include <mbgl/platform/platform.hpp>
#include "../../android/cpp/NativeMapView.hpp"
namespace mbgl {
namespace platform {
@@ -6,7 +7,7 @@ namespace platform {
// Returns the path to the default cache database on this system.
std::string defaultCacheDatabase() {
// TODO need to use get cache dir from java
return "/data/data/com.mapbox.mapboxgl.app/mbgl-cache.db";
return mbgl::android::cache_path + "/mbgl-cache.db";
}
}

View File

@@ -3,6 +3,7 @@
#include <mbgl/util/std.hpp>
#include <mbgl/util/ptr.hpp>
#include <mbgl/util/time.hpp>
#include "../../android/cpp/NativeMapView.hpp"
#include <uv.h>
#include <curl/curl.h>
@@ -404,11 +405,15 @@ void start_request(void *const ptr) {
baton->response = std::make_unique<Response>();
}
#ifndef __ANDROID__
std::string ca_path = "ca-bundle.crt";
#else
std::string ca_path = mbgl::android::cache_path + "/ca-bundle.crt";
#endif
// Carry on the shared pointer in the private information of the CURL handle.
curl_easy_setopt(handle, CURLOPT_PRIVATE, context.release());
// FIXME temp work around
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
//curl_easy_setopt(handle, CURLOPT_CAINFO, "ca-bundle.crt");
curl_easy_setopt(handle, CURLOPT_CAINFO, ca_path.c_str());
curl_easy_setopt(handle, CURLOPT_URL, baton->path.c_str());
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, curl_write_cb);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &baton->response->data);