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

Use jni.hpp #4193

Merged
merged 6 commits into from
Mar 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function print_flags {
if [ "$(type -t print_${NAME}_flags)" = 'function' ]; then
print_${NAME}_flags
else
local VERSION=`echo "${NAME}_VERSION" | tr "[:lower:]-" "[:upper:]_"`
local VERSION=`echo "${NAME}_VERSION" | tr "[:lower:]-." "[:upper:]__"`
if [ ! -z ${!VERSION:-} ] ; then
mason install ${NAME} ${!VERSION}
for FLAGS in "$@" ; do
Expand Down Expand Up @@ -106,6 +106,7 @@ print_flags rapidjson static_libs cflags ldflags
print_flags gtest static_libs cflags ldflags
print_flags pixelmatch static_libs cflags ldflags
print_flags webp static_libs cflags ldflags
print_flags jni.hpp static_libs cflags ldflags

CONFIG+=" }
}
Expand Down
1 change: 1 addition & 0 deletions gyp/http-android.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'variables': {
'cflags_cc': [
'<@(boost_cflags)',
'<@(jni.hpp_cflags)',
],
'defines': [
'-DMBGL_HTTP_ANDROID'
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.mapbox.mapboxsdk.http;

import android.text.TextUtils;
import android.util.Log;

import com.mapbox.mapboxsdk.constants.MapboxConstants;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.ProtocolException;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.net.ssl.SSLException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

class HTTPRequest implements Callback {
private static OkHttpClient mClient = new OkHttpClient();
private final String LOG_TAG = HTTPRequest.class.getName();

private static final int CONNECTION_ERROR = 0;
private static final int TEMPORARY_ERROR = 1;
private static final int PERMANENT_ERROR = 2;
private static final int CANCELED_ERROR = 3;

private long mNativePtr = 0;

private Call mCall;
private Request mRequest;

private native void nativeOnFailure(int type, String message);
private native void nativeOnResponse(int code, String etag, String modified, String cacheControl, String expires, byte[] body);

private HTTPRequest(long nativePtr, String resourceUrl, String userAgent, String etag, String modified) {
mNativePtr = nativePtr;
Request.Builder builder = new Request.Builder().url(resourceUrl).tag(resourceUrl.toLowerCase(MapboxConstants.MAPBOX_LOCALE)).addHeader("User-Agent", userAgent);
if (etag.length() > 0) {
builder = builder.addHeader("If-None-Match", etag);
} else if (modified.length() > 0) {
builder = builder.addHeader("If-Modified-Since", modified);
}
mRequest = builder.build();
mCall = mClient.newCall(mRequest);
mCall.enqueue(this);
}

public void cancel() {
mCall.cancel();
}

@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
Log.v(LOG_TAG, String.format("[HTTP] Request was successful (code = %d).", response.code()));
} else {
// We don't want to call this unsuccessful because a 304 isn't really an error
String message = !TextUtils.isEmpty(response.message()) ? response.message() : "No additional information";
Log.d(LOG_TAG, String.format(
"[HTTP] Request with response code = %d: %s",
response.code(), message));
}

byte[] body;
try {
body = response.body().bytes();
} catch (IOException e) {
onFailure(null, e);
//throw e;
return;
} finally {
response.body().close();
}

nativeOnResponse(response.code(), response.header("ETag"), response.header("Last-Modified"), response.header("Cache-Control"), response.header("Expires"), body);
}

@Override
public void onFailure(Call call, IOException e) {
Log.w(LOG_TAG, String.format("[HTTP] Request could not be executed: %s", e.getMessage()));

int type = PERMANENT_ERROR;
if ((e instanceof UnknownHostException) || (e instanceof SocketException) || (e instanceof ProtocolException) || (e instanceof SSLException)) {
type = CONNECTION_ERROR;
} else if ((e instanceof InterruptedIOException)) {
type = TEMPORARY_ERROR;
} else if (mCall.isCanceled()) {
type = CANCELED_ERROR;
}

String errorMessage = e.getMessage() != null ? e.getMessage() : "Error processing the request";
nativeOnFailure(type, errorMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public void setBearing(double degrees, long duration) {
}

public void setBearing(double degrees, double cx, double cy) {
nativeSetBearing(mNativeMapViewPtr, degrees, cx, cy);
nativeSetBearingXY(mNativeMapViewPtr, degrees, cx, cy);
}

public double getBearing() {
Expand Down Expand Up @@ -618,8 +618,8 @@ private native void nativeRotateBy(long nativeMapViewPtr, double sx,
private native void nativeSetBearing(long nativeMapViewPtr, double degrees,
long duration);

private native void nativeSetBearing(long nativeMapViewPtr, double degrees,
double cx, double cy);
private native void nativeSetBearingXY(long nativeMapViewPtr, double degrees,
double cx, double cy);

private native double nativeGetBearing(long nativeMapViewPtr);

Expand Down
2 changes: 2 additions & 0 deletions platform/android/mapboxgl-app.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
'sources': [
'./src/native_map_view.cpp',
'./src/jni.cpp',
'./src/attach_env.cpp',
],

'cflags_cc': [
'<@(boost_cflags)',
'<@(variant_cflags)',
'<@(jni.hpp_cflags)',
],
'libraries': [
'<@(libpng_static_libs)',
Expand Down
1 change: 1 addition & 0 deletions platform/android/scripts/configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ LIBZIP_VERSION=0.11.2
GEOJSONVT_VERSION=3.1.0
VARIANT_VERSION=1.0
RAPIDJSON_VERSION=1.0.2
JNI_HPP_VERSION=2.0.0

export MASON_ANDROID_ABI=${MASON_PLATFORM_VERSION}
22 changes: 22 additions & 0 deletions platform/android/src/attach_env.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "attach_env.hpp"
#include "jni.hpp"

namespace mbgl {
namespace android {

UniqueEnv AttachEnv() {
JNIEnv* env = nullptr;
jint err = theJVM->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);

switch (err) {
case JNI_OK:
return UniqueEnv(env, JNIEnvDeleter(*theJVM, false));
case JNI_EDETACHED:
return UniqueEnv(jni::AttachCurrentThread(*theJVM).release(), JNIEnvDeleter(*theJVM, true));
default:
throw std::system_error(err, jni::ErrorCategory());
}
}

}
}
31 changes: 31 additions & 0 deletions platform/android/src/attach_env.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <jni/jni.hpp>

namespace mbgl {
namespace android {

class JNIEnvDeleter {
private:
jni::JavaVM* vm = nullptr;
bool detach = false;

public:
JNIEnvDeleter() = default;
JNIEnvDeleter(jni::JavaVM& v, bool d)
: vm(&v), detach(d) {}

void operator()(jni::JNIEnv* p) const {
if (p && detach) {
assert(vm);
vm->DetachCurrentThread();
}
}
};

using UniqueEnv = std::unique_ptr<jni::JNIEnv, JNIEnvDeleter>;

UniqueEnv AttachEnv();

}
}
Loading