forked from LordAmit/mobile-monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adb_settings.py
303 lines (256 loc) · 8.21 KB
/
adb_settings.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import subprocess
from enum import Enum, auto
import config_reader as config
import api_commands
import util
ADB = config.adb
PRINT_FLAG = False
class KeyboardEvent(Enum):
'''
Keyboard events
'''
KEYCODE_0 = 0
KEYCODE_1 = auto()
KEYCODE_2 = auto()
KEYCODE_3 = auto()
KEYCODE_4 = auto()
KEYCODE_5 = auto()
KEYCODE_6 = auto()
KEYCODE_7 = auto()
KEYCODE_8 = auto()
KEYCODE_9 = auto()
KEYCODE_A = auto()
KEYCODE_B = auto()
KEYCODE_C = auto()
KEYCODE_D = auto()
KEYCODE_E = auto()
KEYCODE_F = auto()
KEYCODE_G = auto()
KEYCODE_H = auto()
KEYCODE_I = auto()
KEYCODE_J = auto()
KEYCODE_K = auto()
KEYCODE_L = auto()
KEYCODE_M = auto()
KEYCODE_N = auto()
KEYCODE_O = auto()
KEYCODE_P = auto()
KEYCODE_Q = auto()
KEYCODE_R = auto()
KEYCODE_S = auto()
KEYCODE_T = auto()
KEYCODE_U = auto()
KEYCODE_V = auto()
KEYCODE_W = auto()
KEYCODE_X = auto()
KEYCODE_Y = auto()
KEYCODE_Z = auto()
KEYCODE_STAR = auto()
KEYCODE_POUND = auto()
KEYCODE_COMMA = auto()
KEYCODE_PERIOD = auto()
KEYCODE_PLUS = auto()
KEYCODE_MINUS = auto()
KEYCODE_EQUALS = auto()
KEYCODE_LEFT_BRACKET = auto()
KEYCODE_RIGHT_BRACKET = auto()
KEYCODE_BACKSLASH = auto()
KEYCODE_SEMICOLON = auto()
KEYCODE_APOSTROPHE = auto()
KEYCODE_SLASH = auto()
KEYCODE_AT = auto()
# KEYCODE_NUM = auto()
KEYCODE_SPACE = auto()
class KeyEvent(Enum):
'''
Key events
'''
# KEYCODE_UNKNOWN = ()
# KEYCODE_SOFT_RIGHT = ()
# KEYCODE_HOME = ()
# KEYCODE_BACK = ()
# KEYCODE_CALL = ()
# KEYCODE_ENDCALL = auto()
# KEYCODE_DPAD_UP = auto()
# KEYCODE_DPAD_DOWN = auto()
# KEYCODE_DPAD_LEFT = auto()
# KEYCODE_DPAD_RIGHT = auto()
# KEYCODE_DPAD_CENTER = auto()
KEYCODE_ENTER = auto()
KEYCODE_DEL = auto()
KEYCODE_VOLUME_UP = auto()
KEYCODE_VOLUME_DOWN = auto()
# KEYCODE_POWER = auto()
# KEYCODE_CAMERA = auto()
KEYCODE_CLEAR = auto()
# KEYCODE_ALT_LEFT = auto()
# KEYCODE_ALT_RIGHT = auto()
# KEYCODE_SHIFT_LEFT = auto()
# KEYCODE_SHIFT_RIGHT = auto()
# KEYCODE_TAB = auto()
# KEYCODE_SYM = auto()
# KEYCODE_EXPLORER = auto()
# KEYCODE_ENVELOPE = auto()
# KEYCODE_GRAVE = auto()
# KEYCODE_HEADSETHOOK = auto()
# KEYCODE_FOCUS = auto()
# KEYCODE_MENU = auto()
# KEYCODE_NOTIFICATION = auto()
# KEYCODE_SEARCH = auto()
# TAG_LAST_KEYCOD = auto()
class UserRotation(Enum):
'''
possible values of User_Rotation
'''
ROTATION_POTRAIT = 0
ROTATION_LANDSCAPE = 1
ROTATION_REVERSE_POTRAIT = 2
ROTATION_REVERSE_LANDSCAPE = 3
class Namespace(Enum):
'''
possible values of namespace
'''
SYSTEM = 'system'
GLOBAL = 'global'
SECURE = 'secure'
class Airplane(Enum):
'''
possible values of Airplane mode
'''
MODE_ON = 1
MODE_OFF = 0
class AdbSettings:
'''
AdbSettings api - for controlling adb devices
'''
emulator_name = ''
# TODO: Fix so that emulator_device is replaced by emulator.
def __init__(self, emulator_device: str)-> None:
emulators = api_commands.adb_list_avd_devices()
if emulator_device not in emulators:
util.detail_print(emulator_device)
print('error. possible choices are: ')
util.detail_print(emulators)
raise ValueError()
self.emulator_name = emulator_device
def subprocess_call_set(self, value: str, namespace: Namespace):
'''
calls subprocess to set `value`
'''
options = [ADB, '-s', self.emulator_name,
'shell', 'settings', 'put', namespace.value]
values = value.split(' ')
options = options + values
# print(options)
try:
subprocess.check_output(options)
except subprocess.CalledProcessError as exception:
print(exception)
def adb_send_key_event(self, event: KeyEvent):
'''
sends key event to the emulator
'''
subprocess.check_output(
[ADB, '-s', self.emulator_name, 'shell', 'input', 'keyevent',
str(event.name)])
# http://stackoverflow.com/questions/6236340/how-to-limit-speed-of-internet-connection-on-android-emulator
def adb_send_key_event_test(self, event_name: str):
'''
sends key event to the emulator for testing purpose
'''
subprocess.check_output(
[ADB, '-s', self.emulator_name, 'shell', 'input', 'keyevent',
event_name])
def subprocess_call_get(self, value, namespace: Namespace):
'''
sets a value. for example,
>>> adb -s emulator_name shell settings get system values:
'''
# adb shell settings get system accelerometer_rotation
options = [ADB, '-s', self.emulator_name,
'shell', 'settings', 'get', namespace.value]
values = value.split(' ')
options = options + values
util.debug_print(options, flag=PRINT_FLAG)
try:
value = subprocess.check_output(options).decode().strip()
print(value)
return value
except subprocess.CalledProcessError as exception:
print(exception)
def reset_all(self):
'''
defaults to settings.
accelerometer_rotation on
airplane_mode_off
user_rotation POTRAIT
'''
print("setting airplane mode to False")
self.set_airplane_mode(False)
print("setting user rotation to potrait")
self.set_user_rotation(UserRotation.ROTATION_POTRAIT)
print("setting accelerometer rotation to True")
self.set_accelerometer_rotation(True)
# def adb_start_server_safe(self):
# '''
# checks if `adb server` is running. if not, starts it.
# '''
# try:
# status = subprocess.check_output(['pidof', ADB])
# print('adb already running in PID: ' + status.decode())
# return True
# except subprocess.CalledProcessError as exception:
# print('adb is not running, returned status: ' +
# str(exception.returncode))
# print('adb was not started. starting...')
# try:
# subprocess.check_output([ADB, 'start-server'])
# return True
# except subprocess.SubprocessError as exception:
# print('something disastrous happened. maybe ' +
# ADB + ' was not found')
# return False
def get_accelerometer_rotation(self):
'''
gets the accelerometer rotation value
'''
return self.subprocess_call_get('accelerometer_rotation',
Namespace.SYSTEM)
def set_accelerometer_rotation(self, value: bool):
'''
sets the accelerometer rotation value
'''
if value:
self.subprocess_call_set(
'accelerometer_rotation 1', Namespace.SYSTEM)
else:
self.subprocess_call_set(
'accelerometer_rotation 0', Namespace.SYSTEM)
def set_user_rotation(self, rotation: UserRotation):
'''
sets user rotation based on rotation
'''
self.set_accelerometer_rotation(False)
self.subprocess_call_set(
'user_rotation ' + str(rotation.value), Namespace.SYSTEM)
def get_airplane_mode(self) -> bool:
'''
gets airplane mode by `mood`
'''
if self.subprocess_call_get('airplane_mode_on',
Namespace.GLOBAL) == '0':
return False
else:
return True
def set_airplane_mode(self, mode: Airplane):
'''
sets airplane mode by `mode`
'''
if mode == Airplane.MODE_ON:
self.subprocess_call_set('airplane_mode_on 1',
Namespace.GLOBAL)
else:
self.subprocess_call_set(
'airplane_mode_on 0', Namespace.GLOBAL)
# TODO: WIFI On:
# https://developer.android.com/reference/android/provider/Settings.Global.html#WIFI_ON