Skip to content
Merged
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
42 changes: 41 additions & 1 deletion olp-cpp-sdk-core/src/context/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@
#undef max
#elif defined(ANDROID) || defined(ANDROID_HOST)
#include <jni.h>
#ifdef OLP_SDK_ANDROID_AUTO_INITIALIZE_CARES
#include <ares.h>
#include <olp/core/logging/Log.h>
#endif
#endif

#include "ContextInternal.h"

namespace {
std::atomic_flag gDeInitializing = ATOMIC_FLAG_INIT;
}
#ifdef OLP_SDK_ANDROID_AUTO_INITIALIZE_CARES
constexpr auto kLogTag = "Context";
#endif
} // namespace

namespace olp {
namespace context {
Expand Down Expand Up @@ -84,6 +91,11 @@ void Context::init() {
void Context::deinit() {
#if defined(ANDROID) || defined(ANDROID_HOST)
auto cd = Instance();

#ifdef OLP_SDK_ANDROID_AUTO_INITIALIZE_CARES
ares_library_cleanup();
#endif

// Release the global reference of Context.
// Technically not needed if we took the application context,
// but good to release just in case.
Expand Down Expand Up @@ -128,6 +140,34 @@ void Context::init(JavaVM* vm, jobject context) {
assert(0);
}
cd->context = env->NewGlobalRef(context);

#ifdef OLP_SDK_ANDROID_AUTO_INITIALIZE_CARES
{
OLP_SDK_LOG_INFO(kLogTag, "Auto-initializing C-ares");

ares_library_init_jvm(cd->java_vm);

jclass context_class = env->GetObjectClass(context);
jmethodID system_service_method =
env->GetMethodID(context_class, "getSystemService",
"(Ljava/lang/String;)Ljava/lang/Object;");
jclass context_class_static = env->FindClass("android/content/Context");
jfieldID service_field = env->GetStaticFieldID(
context_class_static, "CONNECTIVITY_SERVICE", "Ljava/lang/String;");
jstring connectivity_service =
(jstring)env->GetStaticObjectField(context_class_static, service_field);
jobject connectivity_manager = env->CallObjectMethod(
context, system_service_method, connectivity_service);

if (connectivity_manager != NULL) {
ares_library_init_android(connectivity_manager);
OLP_SDK_LOG_INFO(kLogTag, "Successfuly auto-initialized C-ares");
} else {
OLP_SDK_LOG_WARNING(kLogTag, "Failed to auto-initialize C-ares");
}
}
#endif

initialize();
}

Expand Down
Loading