From b9a0f76d36cd2436ea63ab73b18aa8be1d2b4322 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Tue, 30 Apr 2024 13:11:30 -0700 Subject: [PATCH] Remove pointless dynamic use of ASensorManager. This was only done so it could use ASensorManager_getInstanceForPackage on newer devices and ASensorManager_getInstance on older devices. We can just use ASensorManager_getInstance on all devices and spare the mess. --- native-activity/app/src/main/cpp/main.cpp | 46 +---------------------- 1 file changed, 1 insertion(+), 45 deletions(-) diff --git a/native-activity/app/src/main/cpp/main.cpp b/native-activity/app/src/main/cpp/main.cpp index f1e8ff0b5..23bac1dc3 100644 --- a/native-activity/app/src/main/cpp/main.cpp +++ b/native-activity/app/src/main/cpp/main.cpp @@ -269,50 +269,6 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) { } } -/* - * AcquireASensorManagerInstance(void) - * Workaround ASensorManager_getInstance() deprecation false alarm - * for Android-N and before, when compiling with NDK-r15 - */ -#include -ASensorManager* AcquireASensorManagerInstance(android_app* app) { - if (!app) return nullptr; - - typedef ASensorManager* (*PF_GETINSTANCEFORPACKAGE)(const char* name); - void* androidHandle = dlopen("libandroid.so", RTLD_NOW); - auto getInstanceForPackageFunc = (PF_GETINSTANCEFORPACKAGE)dlsym( - androidHandle, "ASensorManager_getInstanceForPackage"); - if (getInstanceForPackageFunc) { - JNIEnv* env = nullptr; - app->activity->vm->AttachCurrentThread(&env, nullptr); - - jclass android_content_Context = env->GetObjectClass(app->activity->clazz); - jmethodID midGetPackageName = env->GetMethodID( - android_content_Context, "getPackageName", "()Ljava/lang/String;"); - auto packageName = - (jstring)env->CallObjectMethod(app->activity->clazz, midGetPackageName); - - const char* nativePackageName = - env->GetStringUTFChars(packageName, nullptr); - ASensorManager* mgr = getInstanceForPackageFunc(nativePackageName); - env->ReleaseStringUTFChars(packageName, nativePackageName); - app->activity->vm->DetachCurrentThread(); - if (mgr) { - dlclose(androidHandle); - return mgr; - } - } - - typedef ASensorManager* (*PF_GETINSTANCE)(); - auto getInstanceFunc = - (PF_GETINSTANCE)dlsym(androidHandle, "ASensorManager_getInstance"); - // by all means at this point, ASensorManager_getInstance should be available - assert(getInstanceFunc); - dlclose(androidHandle); - - return getInstanceFunc(); -} - /** * This is the main entry point of a native application that is using * android_native_app_glue. It runs in its own thread, with its own @@ -328,7 +284,7 @@ void android_main(struct android_app* state) { engine.app = state; // Prepare to monitor accelerometer - engine.sensorManager = AcquireASensorManagerInstance(state); + engine.sensorManager = ASensorManager_getInstance(); engine.accelerometerSensor = ASensorManager_getDefaultSensor( engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER); engine.sensorEventQueue = ASensorManager_createEventQueue(