androidandroid-ndk

How to convert InputQueue to AInputQueue for apilevel 28 to 32


I have custom implementation of NativeActivity (called MyNativeActivity) for which I need to handle onInputQueueCreated(). Vanilla NativeActivity does by calling getNativePtr() directly.

NativeActivity.java

    public void onInputQueueCreated(InputQueue queue) {
        if (!mDestroyed) {
            mCurInputQueue = queue;
            onInputQueueCreatedNative(mNativeHandle, queue.getNativePtr());
        }
    }

But getNativePtr() being hidden member, It can only be used via JNI which I do for apilevel below 28. And for 33 and above I use AInputQueue_fromJava() provided by android-ndk.

Now the issue is for apilevel 28-32, getNativePtr() does not work and always return null.

Accessing hidden method Landroid/view/InputQueue;->getNativePtr()J (dark greylist, JNI)

So can InputQueue be even converted to AInputQueue ?

Edit:

With suggestion from perplexity.ai, I also tried reflection. this too returns null.

import java.lang.reflect.Method;
import sun.misc.Unsafe;
...
    public static long getIQNativePtr(InputQueue queue) {
        try {
            // Access the hidden method
            Method method = InputQueue.class.getDeclaredMethod("getNativePtr");
            method.setAccessible(true); // Bypass access checks
            return (long) method.invoke(queue); // Invoke and return native pointer
        } catch (Exception e) {
            e.printStackTrace();
            return 0; // Handle error appropriately
        }
    }
...

Solution

  • There was no need to handle onInputQueueCreated() and onInputQueueDestroyed() like NativeActivity does. Using onTouchEvent() onKeyUp() and onKeyDown() solved the issue.