Android Knows It - Using ADB to Operate Clipboard

Keywords: shell Android github Mobile

If mobile access is not good, please use - >. Github version

Key words: service call, clipboard, Unknown package

A project in hand needs to perform a sauce operation. One of the steps is to use ADB to operate Clipboard (pasteboard). After searching for a long time, it is found that direct operation is not feasible, exactly when Android API >= 11 is not feasible.

Operating environment: macOS Sierra 10.12.6 + iTerm2, some commands may vary slightly depending on the system, please test yourself.

Pre-knowledge Points

First of all, look at the service in ADB Shell. When you execute it from the command line, you can see the following information:

$ adb shell
$ service
Usage: service [-h|-?]
       service list
       service check SERVICE
       service call SERVICE CODE [i32 N | i64 N | f N | d N | s16 STR ] ...
Options:
   i32: Write the 32-bit integer N into the send parcel.
   i64: Write the 64-bit integer N into the send parcel.
   f:   Write the 32-bit single-precision number N into the send parcel.
   d:   Write the 64-bit double-precision number N into the send parcel.
   s16: Write the UTF-16 string STR into the send parcel.
$ service list | grep clipboard
# If use windows system, use findstr: 
# service list | findstr "clipboard"
63  clipboard: [android.content.IClipboard]

Next, look for CODE to see the source code of Clipboard:

// API < 11
static final int TRANSACTION_getClipboardText 1
static final int TRANSACTION_hasClipboardText 3
static final int TRANSACTION_setClipboardText 2
// API >= 11
static final int TRANSACTION_setPrimaryClip = 1
static final int TRANSACTION_getPrimaryClip = 2
static final int TRANSACTION_getPrimaryClipDescription = 3
static final int TRANSACTION_hasPrimaryClip = 4
static final int TRANSACTION_addPrimaryClipChangedListener = 5
static final int TRANSACTION_removePrimaryClipChangedListener = 6
static final int TRANSACTION_hasClipboardText = 7

Analysis

The Clipboard source indicates what parameters these transactions require. TRANSACTION_setPrimaryClip requires a ClipData, which is not an i32 or s16 and is therefore incompatible with service invocation. More importantly, these transactions need to call the package name as a parameter, and the Clipboard service verifies that the specified package name matches the UID invoked. When using the adb shell, the uid invoked is UID_ROOT or UID_SHELL, and they do not own any packages, so there is no way to pass this check. In short, the new clipboard service cannot be used in this way.

So it is not feasible to use service call clipboard [1 | 2 | 3] [i32 N | i64 N | f N | d N | s16 STR] on API >= 11 system.

So whether the command is correct or not, the Unknown package is prompted:

$ service call clipboard 2 s16 "abc"
Result: Parcel(
  0x00000000: fffffffd 00000013 006e0055 006e006b '........U.n.k.n.'
  0x00000010: 0077006f 0020006e 00610070 006b0063 'o.w.n. .p.a.c.k.'
  0x00000020: 00670061 00200065 00620061 00000063 'a.g.e. .a.b.c...')

summary

As far as Android's current market share is concerned, using ADB to operate Clipboard is not a viable solution.

Reference resources:

If you have any suggestions or questions, you can contact me at any time to discuss your study together.

Posted by envexlabs on Thu, 16 May 2019 19:26:05 -0700