forked from compnerd/swift-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FirebaseAndroid: introduce the new JNI module for Android support
In order to interact with the C++ Firebase SDK, we need to have access to the JVM and the JavaEnvironment. Introduce a JNI layer which allows us to capture this information when loaded. Additionally, register a `company.thebrowser.Native.RegisterActivity` method to allow us access to the `Activity`. This is required to be called before the Firebase layer is accessed by the application.
- Loading branch information
Showing
7 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
add_library(FirebaseAndroidJNI SHARED | ||
jni.c) | ||
target_include_directories(FirebaseAndroidJNI PUBLIC | ||
include) | ||
target_link_libraries(FirebaseAndroidJNI PRIVATE | ||
log) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* SPDX-License-Identifier: BSD-3-Clause */ | ||
|
||
#ifndef SwiftFirebase_FirebaseAndroidShim_abi_h | ||
#define SwiftFirebase_FirebaseAndroidShim_abi_h | ||
|
||
#if defined(FirebaseAndroidJNI_EXPORTS) | ||
#define FIREBASE_ANDROID_ABI __attribute__((__visibility__("default"))) | ||
#else | ||
#define FIREBASE_ANDROID_ABI __attribute__((__visibility__("default"))) | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* SPDX-License-Identifier: BSD-3-Clause */ | ||
|
||
#ifndef SwiftFirebase_FirebaseAndroidShim_FirebaseAndroid_h | ||
#define SwiftFirebase_FirebaseAndroidShim_FirebaseAndroid_h | ||
|
||
#include <jni.h> | ||
|
||
#if defined(__cplusplus) | ||
extern "C" { | ||
#endif | ||
|
||
jobject SwiftFirebase_GetActivity(void); | ||
JNIEnv *SwiftFirebase_GetJavaEnvironment(void); | ||
JavaVM *SwiftFirebase_GetJVM(void); | ||
|
||
#if defined(__cplusplus) | ||
} | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* SPDX-License-Identifier: BSD-3-Clause */ | ||
|
||
module FirebaseAndroid { | ||
header "FirebaseAndroid.h" | ||
export * | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* SPDX-License-Identifier: BSD-3-Clause */ | ||
|
||
#include "FirebaseAndroid.h" | ||
#include "abi.h" | ||
#include "log.h" | ||
|
||
#include <assert.h> | ||
|
||
static JavaVM *g_VM; | ||
static JNIEnv *g_Env; | ||
static jobject *g_Activity; | ||
|
||
#define N_ELEMENTS(array) (sizeof((array)) / sizeof(*(array))) | ||
|
||
static const char kClassPath[] = "company/thebrowser/Native"; | ||
|
||
static jboolean | ||
SwiftFirebase_RegisterActivity(JNIEnv *env, jobject *this, jobject *activity) | ||
{ | ||
assert(g_Activity == NULL && "re-registeration of activity"); | ||
if (g_Activity) return JNI_FALSE; | ||
|
||
g_Activity = activity; | ||
return JNI_TRUE; | ||
} | ||
|
||
static JNINativeMethod kMethods[] = { | ||
{ "RegisterActivity", "()Z", SwiftFirebase_RegisterActivity }, | ||
}; | ||
|
||
static void | ||
RegisterNativeMethods(JNIEnv *env) | ||
{ | ||
jclass class; | ||
jint result; | ||
|
||
class = (*env)->FindClass(env, kClassPath); | ||
if (class == NULL) { | ||
LOG_ERROR("unable to find class '%s'", kClassPath); | ||
return; | ||
} | ||
LOG_DEBUG("located class path '%s': %p", kClassPath, class); | ||
|
||
result = (*env)->RegisterNatives(env, class, kMethods, N_ELEMENTS(kMethods)); | ||
if (result < 0) { | ||
LOG_ERROR("JVM.RegisterNatives(%s): %u", kClassPath, result); | ||
return; | ||
} | ||
LOG_DEBUG("registered %u methods", N_ELEMENTS(kMethods)); | ||
} | ||
|
||
FIREBASE_ANDROID_ABI | ||
jint JNI_OnLoad(JavaVM *vm, void *reserved) | ||
{ | ||
g_VM = vm; | ||
if ((*g_VM)->GetEnv(g_VM, (void **)&g_Env, JNI_VERSION_1_6) == JNI_OK) | ||
return JNI_VERSION_1_6; | ||
RegisterNativeMethods(g_Env); | ||
return -1; | ||
} | ||
|
||
FIREBASE_ANDROID_ABI | ||
jobject SwiftFirebase_GetActivity(void) | ||
{ | ||
assert(g_Activity && "`GetActivity` invoked before `RegisterActivity`"); | ||
return *g_Activity; | ||
} | ||
|
||
FIREBASE_ANDROID_ABI | ||
JNIEnv *SwiftFirebase_GetJavaEnvironment(void) | ||
{ | ||
return g_Env; | ||
} | ||
|
||
FIREBASE_ANDROID_ABI | ||
JavaVM *SwiftFirebase_GetJVM(void) | ||
{ | ||
return g_VM; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* SPDX-License-Identifier: BSD-3-Clause */ | ||
|
||
#ifndef SwiftFirebase_FirebaseAndroidShim_logging_h | ||
#define SwiftFirebase_FirebaseAndroidShim_logging_h | ||
|
||
#include <android/log.h> | ||
|
||
#define FIREBASE_ANDROID_LOG(level, tag, ...) __android_log_print(level, tag, __VA_ARGS__) | ||
#define FIREBASE_ANDROID_TAG "company.thebrowser.firebase" | ||
|
||
#define LOG_DEBUG(...) FIREBASE_ANDROID_LOG(ANDROID_LOG_DEBUG, FIREBASE_ANDROID_TAG, __VA_ARGS__) | ||
#define LOG_VERBOSE(...) FIREBASE_ANDROID_LOG(ANDROID_LOG_VERBOSE, FIREBASE_ANDROID_TAG, __VA_ARGS__) | ||
#define LOG_INFO(...) FIREBASE_ANDROID_LOG(ANDROID_LOG_INFO, FIREBASE_ANDROID_TAG, __VA_ARGS__) | ||
#define LOG_WARN(...) FIREBASE_ANDROID_LOG(ANDROID_LOG_WARN, FIREBASE_ANDROID_TAG, __VA_ARGS__) | ||
#define LOG_ERROR(...) FIREBASE_ANDROID_LOG(ANDROID_LOG_ERROR, FIREBASE_ANDROID_TAG, __VA_ARGS__) | ||
|
||
#endif |