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

Commit

Permalink
[android] - replace platform default implementation using nunicode fo…
Browse files Browse the repository at this point in the history
…r uppercasing an lowercasing with an Android specific String.java equivalent
  • Loading branch information
tobrun committed Jul 27, 2018
1 parent 94ad8e2 commit afc8838
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion platform/android/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ macro(mbgl_platform_core)
PRIVATE platform/android/src/text/local_glyph_rasterizer_jni.hpp
PRIVATE platform/android/src/logging_android.cpp
PRIVATE platform/android/src/thread.cpp
PRIVATE platform/default/string_stdlib.cpp
PRIVATE platform/android/src/string_stdlib.cpp
PRIVATE platform/default/bidi.cpp
PRIVATE platform/default/thread_local.cpp
PRIVATE platform/default/unaccent.cpp
Expand Down
24 changes: 24 additions & 0 deletions platform/android/src/java/lang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ namespace android {
namespace java {
namespace lang {

// String

jni::Object<String> String::New(JNIEnv& env, jni::String value) {
static auto constructor = String::javaClass.GetConstructor<jni::String>(env);
return String::javaClass.New(env, constructor, value);
}

jni::String String::toUpperCase(JNIEnv& env, jni::String value) {
static auto method = javaClass.GetMethod<jni::String ()>(env, "toUpperCase");
return String::New(env, value).Call(env, method);
}

jni::String String::toLowerCase(JNIEnv& env, jni::String value) {
static auto method = javaClass.GetMethod<jni::String ()>(env, "toLowerCase");
return String::New(env, value).Call(env, method);
}

void String::registerNative(jni::JNIEnv& env) {
// Lookup the class
javaClass = *jni::Class<String>::Find(env).NewGlobalRef(env).release();
}

jni::Class<String> String::javaClass;

// Float

jni::Object<Float> Float::valueOf(JNIEnv &env, jfloat value) {
Expand Down
15 changes: 15 additions & 0 deletions platform/android/src/java/lang.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ namespace android {
namespace java {
namespace lang {

class String : private mbgl::util::noncopyable {
public:
static constexpr auto Name() { return "java/lang/String"; };

static jni::Object<String> New(JNIEnv&, jni::String);

static jni::String toUpperCase(JNIEnv&, jni::String);

static jni::String toLowerCase(JNIEnv&, jni::String);

static jni::Class<String> javaClass;

static void registerNative(jni::JNIEnv&);
};

class Float : private mbgl::util::noncopyable {
public:
static constexpr auto Name() { return "java/lang/Float"; };
Expand Down
1 change: 1 addition & 0 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ void registerNatives(JavaVM *vm) {
java::lang::Boolean::registerNative(env);
java::lang::Double::registerNative(env);
java::lang::Long::registerNative(env);
java::lang::String::registerNative(env);

// GeoJSON
geojson::Feature::registerNative(env);
Expand Down
21 changes: 21 additions & 0 deletions platform/android/src/string_stdlib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <mbgl/util/platform.hpp>
#include "java/lang.hpp"
#include "attach_env.hpp"

namespace mbgl {
namespace platform {

std::string uppercase(const std::string& str) {
auto env{ android::AttachEnv() };
auto jstring = jni::Make<jni::String>(*env, str.c_str());
return jni::Make<std::string>(*env, mbgl::android::java::lang::String::toUpperCase(*env, jstring));
}

std::string lowercase(const std::string& str) {
auto env{ android::AttachEnv() };
auto jstring = jni::Make<jni::String>(*env, str.c_str());
return jni::Make<std::string>(*env, mbgl::android::java::lang::String::toLowerCase(*env, jstring));
}

} // namespace platform
} // namespace mbgl

0 comments on commit afc8838

Please sign in to comment.