1

I can open "Use USB to" system dialog by:

val p = Runtime.getRuntime().exec(arrayOf("su", "-c", "system/bin/sh"))
val stdout = DataOutputStream(p.outputStream)
stdout.writeBytes("am start com.android.settings/.UsbSettings")
stdout.flush()
stdout.close()

I also would like to select any of choice programmatically (for example "Transfer files")?

Is this possible with rooted Android?

enter image description here

Update

Seems I can play with input keyevent https://stackoverflow.com/a/28969112/9766649 (e.g. 61 --> "KEYCODE_TAB" and 62 --> "KEYCODE_SPACE" are useful)

user155
  • 775
  • 1
  • 7
  • 25

1 Answers1

0

Solved it!

For example to select fourth item ("Charge this phone") from dialog:

val process = Runtime.getRuntime().exec(arrayOf("su", "-c", "system/bin/sh"))
val stream = DataOutputStream(process.outputStream)
stream.writeBytes("am start com.android.settings/.UsbSettings\n")
stream.writeBytes("input keyevent 21\n") // KEYCODE_DPAD_LEFT
stream.writeBytes("input keyevent 20\n") // KEYCODE_DPAD_DOWN
stream.writeBytes("input keyevent 20\n") // KEYCODE_DPAD_DOWN
stream.writeBytes("input keyevent 20\n") // KEYCODE_DPAD_DOWN
stream.writeBytes("input keyevent 66\n") // KEYCODE_ENTER
stream.flush()
stream.close()

(to select first item, just delete all lines with KEYCODE_DPAD_DOWN)

user155
  • 775
  • 1
  • 7
  • 25