1

Setup

I'm on an Android 10 device and have an app (henceforth generator) that generates data it can copy to the device's clipboard (this involves swipes, taps, etc.). I need to do that repeatedly, as many times as possible (hundreds, thousands, whatever), and retrieve that data programmatically (presumably through a bash or perl script wrapped around adb commands).

Question

What would be some effective ways of achieving this?

Elaboration on what I've tried

I actually have a solution, I am just not sure it's the "best" one can do (whatever that means). generator is closed-source so I can't alter its behavior in any way, but I can repeatedly

  1. start generator and/or switch focus to it with
adb shell monkey -p <generator-app> 1
  1. mimic the tapping actions that produce the data and copy it to the clipboard with various
adb shell input tap x y

commands;

  1. open/switch focus to the Termux terminal emulator (on the Android device):
adb shell monkey -p com.termux 1
  1. access the clipboard via its Termux:API add-on, by running the termux-clipboard-get command in the termux terminal (again, on the Android device). I control this through adb with
adb shell input text 'termux-clipboard-get\>\>file-on-android-gadget.txt' && adb shell input keyevent 66

(keyevent 66 means ENTER, and the \>\> means append, >>, with escaped characters).

  1. repeat the whole shebang.

This gives me many instances of the desired output in file-on-android-gadget.txt, saved locally on the phone (I'm OK retrieving it later, etc.).

Alternative routes?

What bothers me is the need to switch back and forth between two apps. I don't know that I have much choice over how I interact with generator, as it's closed-source and does not expose its internals, but perhaps one can do better at "the other end", i.e. when retrieving the clipboard data.

Newer Android APIs have locked down access to the clipboard, rendering it difficult to interact with programmatically, through adb. This SO thread points to a repo for a (Kotlin + gradle) app that sets the clipboard via a service runnable through adb, but my attempts to get the clipboard have been unsuccessful (i.e. getPrimaryClip() returns empty). I believe this is because, as per the docs:

getPrimaryClip

public ClipData getPrimaryClip ()

Returns the current primary clip on the clipboard. If the application is not the default IME or does not have input focus this return null.

So there might just be no way around having to focus on a second app before the clipboard can be retrieved. But I have no experience programming apps, hence the question.

A further reference

During my peregrinations I found another repo for an Android app (Java + maven) that both gets and sets the clipboard. Same caveat though: as of Android 10, it needs to be in focus for the broadcast receiver to retrieve the clipboard data (other than that it works fine; I have it on my Android 10 device as we speak). So this would fare no better than my currently-working termux-based solution detailed above.

grobber
  • 1,083
  • 1
  • 9
  • 20

0 Answers0