Skip to content

Commit

Permalink
Remove pointless dynamic use of ASensorManager.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DanAlbert committed Apr 30, 2024
1 parent 9375824 commit b9a0f76
Showing 1 changed file with 1 addition and 45 deletions.
46 changes: 1 addition & 45 deletions native-activity/app/src/main/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <dlfcn.h>
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
Expand All @@ -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(
Expand Down

0 comments on commit b9a0f76

Please sign in to comment.