Skip to content
Closed
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
28 changes: 28 additions & 0 deletions packages/react-native/ReactCxxPlatform/react/jni/JniHelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <fbjni/Context.h>
#include <fbjni/fbjni.h>

namespace facebook::react {

jobject getApplication(JNIEnv* env) {
auto activityThreadClass = env->FindClass("android/app/ActivityThread");
auto currentApplicationMethodID = env->GetStaticMethodID(
activityThreadClass, "currentApplication", "()Landroid/app/Application;");
return env->CallStaticObjectMethod(
activityThreadClass, currentApplicationMethodID);
}

jni::alias_ref<jni::AContext> getContext() {
auto env = facebook::jni::Environment::ensureCurrentThreadIsAttached();
auto application = getApplication(env);
return facebook::jni::wrap_alias(
static_cast<facebook::jni::AContext::javaobject>(application));
}

} // namespace facebook::react
19 changes: 19 additions & 0 deletions packages/react-native/ReactCxxPlatform/react/jni/JniHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <fbjni/Context.h>
#include <fbjni/fbjni.h>

namespace facebook::react {

jobject getApplication(JNIEnv* env);

jni::alias_ref<jni::AContext> getContext();

} // namespace facebook::react
Loading