adbutils
adbutils copied to clipboard
Remove temporary file usage in `AdbDevice.screenshot()`
This PR aims to improve adbutils.AdbDevice.screenshot()
by removing usage of temporary screenshots.
Currently, the behaviour of screenshot()
is to write the screenshot to a temp file then pull the screenshot out to load in and return. This behaviour is marked as "not thread-safe", to which I assume is related to the use of temp file.
screencap -p
when not given the FILENAME
argument will output the PNG screenshot to stdout
which can then be read and loaded into PIL.Image
. This behaviour is utilised to eliminate the need of a temp file on both the connected Android machine and the local machine.
As the screenshot is supposed to be raw bytes, using built-in methods such as read_until_close()
with automatic decode to UTF-8 will mess with the bytes, therefore stream=True
is enabled in the .shell()
call, and manually reading the output in a similar fashion to read_until_close()
. While this PR could have included a backward-compatible change in read_until_close()
to allow opting out of decoding, therefore reducing duplicated code, by:
class AdbConnection(object):
...
def read_until_close(self, decode=True) -> Union[str, bytes]:
# Added ^^^^^^^^^^^ ^^^^^^ ^^^^^^
...
return content.decode('utf-8', errors='ignore') if decode else content
# Added ^^^^^^^^^^^^^^^^^^^^^^^
I decided against it as it is out-of-scope for this PR, and might cause breakage somewhere else.