Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] #3891 - starting jni wiring
Browse files Browse the repository at this point in the history
  • Loading branch information
zugaldia committed Feb 17, 2016
1 parent ab69346 commit fcea86e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbox.mapboxsdk.offline;

import android.content.Context;
import android.util.Log;

import java.util.List;

Expand All @@ -9,6 +10,11 @@
*/
public class OfflineManager {

private final static String LOG_TAG = "OfflineManager";

// Holds the pointer to JNI DefaultFileSource
private long mDefaultFileSourcePtr = 0;

/*
* Callbacks
*/
Expand All @@ -26,6 +32,14 @@ public interface CreateOfflineRegionCallback {
*/

public OfflineManager(Context context) {
String cachePath = context.getCacheDir().getAbsolutePath();
Log.d(LOG_TAG, "cachePath: " + cachePath);

String assetRoot = context.getFilesDir().getAbsolutePath();
Log.d(LOG_TAG, "assetRoot: " + assetRoot);

// Get pointer to DefaultFileSource instance
mDefaultFileSourcePtr = createDefaultFileSource(cachePath, assetRoot);
}

/*
Expand Down Expand Up @@ -69,4 +83,10 @@ public void createOfflineRegion(

}

/*
* Native methods
*/

private native long createDefaultFileSource(String cachePath, String assetRoot);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.MAIN" />-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<!--<category android:name="android.intent.category.LAUNCHER" />-->
<!--</intent-filter>-->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MapboxMapActivity"
Expand Down Expand Up @@ -95,13 +95,7 @@
android:screenOrientation="portrait" />
<activity
android:name=".OfflineActivity"
android:label="@string/activity_offline">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
android:label="@string/activity_offline" />

<meta-data
android:name="com.mapbox.AccessToken"
Expand Down
41 changes: 41 additions & 0 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ jfieldID customLayerInitializeFunctionId = nullptr;
jfieldID customLayerRenderFunctionId = nullptr;
jfieldID customLayerDeinitializeFunctionId = nullptr;

// Offline declarations start

jclass customOfflineManagerClass = nullptr;

// Offline declarations end

bool throw_jni_error(JNIEnv *env, const char *msg) {
if (env->ThrowNew(runtimeExceptionClass, msg) < 0) {
env->ExceptionDescribe();
Expand Down Expand Up @@ -1620,6 +1626,19 @@ void JNICALL nativeRemoveCustomLayer(JNIEnv *env, jobject obj, jlong nativeMapVi
nativeMapView->getMap().removeCustomLayer(std_string_from_jstring(env, id));
}

// Offline calls begin

jlong JNICALL createDefaultFileSource(JNIEnv *env, jobject obj, jstring cachePath_, jstring assetRoot_) {
mbgl::Log::Debug(mbgl::Event::JNI, "createDefaultFileSource");
std::string cachePath = std_string_from_jstring(env, cachePath_);
std::string assetRoot = std_string_from_jstring(env, assetRoot_);
mbgl::DefaultFileSource *defaultFileSource = new mbgl::DefaultFileSource(cachePath, assetRoot, 50 * 1024 * 1024);
jlong defaultFileSourcePtr = reinterpret_cast<jlong>(defaultFileSource);
return defaultFileSourcePtr;
}

// Offline calls end

}

extern "C" {
Expand Down Expand Up @@ -2003,6 +2022,15 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
env->ExceptionDescribe();
}

// Offline definitions begin

customOfflineManagerClass = env->FindClass("com/mapbox/mapboxsdk/offline/OfflineManager");
if (customOfflineManagerClass == nullptr) {
env->ExceptionDescribe();
}

// Offline definitions end

const std::vector<JNINativeMethod> methods = {
{"nativeCreate", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;FIJ)J",
reinterpret_cast<void *>(&nativeCreate)},
Expand Down Expand Up @@ -2139,6 +2167,19 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return JNI_ERR;
}

// Offline register begin

const std::vector<JNINativeMethod> offlineMethods = {
{"createDefaultFileSource", "(Ljava/lang/String;Ljava/lang/String;)J", reinterpret_cast<void *>(&createDefaultFileSource)}
};

if (env->RegisterNatives(customOfflineManagerClass, offlineMethods.data(), offlineMethods.size()) < 0) {
env->ExceptionDescribe();
return JNI_ERR;
}

// Offline register end

latLngClass = reinterpret_cast<jclass>(env->NewGlobalRef(latLngClass));
if (latLngClass == nullptr) {
env->ExceptionDescribe();
Expand Down

0 comments on commit fcea86e

Please sign in to comment.