senzhk / ADBKeyBoard

Android Virtual Keyboard Input via ADB (Useful for Test Automation)
GNU General Public License v2.0
1.22k stars 299 forks source link

Problems with ADB_INPUT_TEXT #67

Closed chrisgeli closed 11 months ago

chrisgeli commented 11 months ago
$ adb shell am broadcast -a ADB_INPUT_TEXT --es msg 'hello? Hello?'
Broadcasting: Intent { act=ADB_INPUT_TEXT flg=0x400000 pkg=Hello? (has extras) }
Broadcast completed: result=0

This didn't seem to do anything on the device. The pkg=Hello? bit in the adb shell output didn't seem right, so I tried

$ adb shell am broadcast -a ADB_INPUT_TEXT --es msg 'hello?'
Broadcasting: Intent { act=ADB_INPUT_TEXT flg=0x400000 (has extras) }
Broadcast completed: result=0

instead, which did indeed show the message on the device but also caused some exception visible via adb logcat (and left the AdbIME in a weird state:

11-23 11:35:07.201  9975  9975 E AndroidRuntime: FATAL EXCEPTION: main
11-23 11:35:07.201  9975  9975 E AndroidRuntime: Process: com.android.adbkeyboard, PID: 9975
11-23 11:35:07.201  9975  9975 E AndroidRuntime: java.lang.RuntimeException: Error receiving broadcast Intent { act=ADB_INPUT_TEXT flg=0x400010 (has extras) } in com.android.adbkeyboard.AdbIME$AdbReceiver@b33f63e
11-23 11:35:07.201  9975  9975 E AndroidRuntime:        at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$android-app-LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1800)
11-23 11:35:07.201  9975  9975 E AndroidRuntime:        at android.app.LoadedApk$ReceiverDispatcher$Args$$ExternalSyntheticLambda0.run(Unknown Source:2)
11-23 11:35:07.201  9975  9975 E AndroidRuntime:        at android.os.Handler.handleCallback(Handler.java:942)
11-23 11:35:07.201  9975  9975 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 11:35:07.201  9975  9975 E AndroidRuntime:        at android.os.Looper.loopOnce(Looper.java:201)
11-23 11:35:07.201  9975  9975 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:288)
11-23 11:35:07.201  9975  9975 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:7924)
11-23 11:35:07.201  9975  9975 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
11-23 11:35:07.201  9975  9975 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
11-23 11:35:07.201  9975  9975 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
11-23 11:35:07.201  9975  9975 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] java.lang.String.split(java.lang.String)' on a null object reference
11-23 11:35:07.201  9975  9975 E AndroidRuntime:        at com.android.adbkeyboard.AdbIME$AdbReceiver.onReceive(AdbIME.java:102)
11-23 11:35:07.201  9975  9975 E AndroidRuntime:        at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$android-app-LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1790)
11-23 11:35:07.201  9975  9975 E AndroidRuntime:        ... 9 more
11-23 11:35:07.236   694   694 E BpTransactionCompletedListener: Failed to transact (-32)

Looking at line 102, it seems that msg was null.

I think that part of the code

            if (intent.getAction().equals(IME_MESSAGE)) {
                String msg = intent.getStringExtra("mcode"); // Get message.
                String[] mcodes = msg.split(","); // Get mcodes in string.
                if (mcodes != null) {
                    int i;

should probably be changed to this:

            if (intent.getAction().equals(IME_MESSAGE)) {
                String msg = intent.getStringExtra("mcode"); // Get message.
                if (msg != null) {
                                        String[] mcodes = msg.split(","); // Get mcodes in string.
                    int i;

to avoid raising an exception if the mcode string is missing.

Trying an empty mcode in addition to msg still caused the exception:

$ adb shell am broadcast -a ADB_INPUT_TEXT --es msg "working?" -es mcode ""
Broadcasting: Intent { act=ADB_INPUT_TEXT flg=0x400000 (has extras) }
Broadcast completed: result=0

The "working?" text was still submitted.

This is on Android 13 (LineageOS).

$ adb version 
Android Debug Bridge version 1.0.41
Version 29.0.6-debian
Installed as /usr/lib/android-sdk/platform-tools/adb
senzhk commented 11 months ago

fixed mcode issue.