This repository was archived by the owner on Dec 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_workbench_android.py
More file actions
173 lines (141 loc) · 5.02 KB
/
_workbench_android.py
File metadata and controls
173 lines (141 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import time
import logging
import os.path as op
import subprocess
import traceback
class WorkbenchAndroid:
"""
Manages `heimdall` and `adb` to flash and securely
erase personal data.
"""
def __init__(self, target=None):
self.logger = logging.getLogger(self.__class__.__name__)
self.adb = adb_commands.AdbCommands()
self._signer = sign_m2crypto.M2CryptoSigner(
op.expanduser('~/.android/adbkey')
)
@staticmethod
def devices_on_bootload():
"""Connect ot the device.
Returns:
bool: True if any device is detected.
"""
return bool(subprocess.run(('heimdall', 'detect')).returncode == 0)
@staticmethod
def flash_device():
"""Connect ot the device.
Returns:
bool: True if any device is detected.
"""
try:
return bool(
subprocess.run(
('heimdall', 'flash', '--RECOVERY', 'recovery.img')
).returncode == 0
)
except Exception:
traceback.print_last() # Show the exception on terminal.
return False
def list_devices(self):
"""Connect ot the device.
Returns:
dict: Dictionary with device serial numbers as str.
"""
return [x.serial_number for x in self.adb.Devices()]
def set_bootloader(self):
"""Set bootloader for all current devices."""
for serial_number in self.list_devices():
self.connect_device(serial_number)
self.adb.RebootBootloader()
self.adb.Close()
def set_recovery(self):
"""Reboot all devices detected into Recovery mode."""
for serial_number in self.list_devices():
self.connect_device(serial_number)
self.adb.RebootBootloader()
self.adb.Close()
def export(self, serial_number):
"""Export device information of given `serial_number`.
print(device.Shell('getprop'))
print(device.Shell('getprop ro.product.model'))
model = device.Shell('getprop ro.product.model').strip()
and usb.idProduct Y, flashing the recovery
get device data
serial number, model, mnaufacturer
can we get imei / meid?
can we get battery status?
android version
print(device.Shell('getprop'))
print(device.Shell('getprop ro.product.model'))
Args:
serial_number (str): Serial number of the device.
"""
outputs = self.shell(
[
'getprop ro.product.model',
'getprop ro.product.brand',
],
target=serial_number
)
return dict(
model=outputs[0],
brand=outputs[1],
)
def shell(self, command, target=None):
"""Automatically start a communication with the device serial number.
Args:
command (str or list): A command or a list of commands to
execute on the device shell.
target (str): The serial number of the device.
Returns:
str or list: A string with the output or a list of strings
with the output of all commands.
"""
self.adb.ConnectDevice(serial=target.encode('UTF-8'))
if isinstance(command, str):
output = self.adb.Shell(command).strip()
elif isinstance(command, list):
output = [self.adb.Shell(execute).strip() for execute in command]
else:
raise NotImplementedError(
'You should send a `str` or `list` command/s.'
)
self.adb.Close()
return output
def connect_device(self, serial_number, kill_retry=True):
"""Connect ot the device.
Args:
serial_number (str): Serial n
mber of the device.
kill_retry (bool): If failed with `USBError` exception, try
to kill-server from adb Linux shell.
Raises:
DeviceNotFoundError: When the device is not found.
"""
try:
self.adb.ConnectDevice(
rsa_keys=[self._signer],
serial=serial_number.encode('UTF-8')
)
except USBError as err:
if kill_retry:
self.logger.info('Trying to killing the adb server...')
subprocess.run(('adb', 'kill-server'))
time.sleep(1)
self.connect_device(serial_number, kill_retry=False)
else:
raise err
def get_all(self, serial_number):
"""Get all information from `getprop`.
Returns:
str: All properties.
"""
return self.shell('getprop', target=serial_number)
def get_model(self, serial_number):
"""Get Model name of the device.
Args:
serial_number (str): Serial number of the device.
Returns:
str: Model name.
"""
return self.shell('getprop ro.product.model', target=serial_number)