-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy path__init__.py
More file actions
554 lines (444 loc) · 13.2 KB
/
__init__.py
File metadata and controls
554 lines (444 loc) · 13.2 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#!/usr/bin/env python
"""
Communicate with an Amazon Fire TV device via ADB over a network.
ADB Debugging must be enabled.
"""
import errno
import logging
import re
from socket import error as socket_error
from adb import adb_commands
from adb.adb_protocol import InvalidChecksumError
# Matches window windows output for app & activity name gathering
WINDOW_REGEX = re.compile("Window\{(?P<id>.+?) (?P<user>.+) (?P<package>.+?)(?:\/(?P<activity>.+?))?\}$", re.MULTILINE)
# ADB key event codes.
HOME = 3
VOLUME_UP = 24
VOLUME_DOWN = 25
POWER = 26
PLAY_PAUSE = 85
NEXT = 87
PREVIOUS = 88
PLAY = 126
PAUSE = 127
UP = 19
DOWN = 20
LEFT = 21
RIGHT = 22
ENTER = 66
SPACE = 62
BACK = 4
MENU = 1
KEY_0 = 7
KEY_1 = 8
KEY_2 = 9
KEY_3 = 10
KEY_4 = 11
KEY_5 = 12
KEY_6 = 13
KEY_7 = 14
KEY_8 = 15
KEY_9 = 16
KEY_A = 29
KEY_B = 30
KEY_C = 31
KEY_D = 32
KEY_E = 33
KEY_F = 34
KEY_G = 35
KEY_H = 36
KEY_I = 37
KEY_J = 38
KEY_K = 39
KEY_L = 40
KEY_M = 41
KEY_N = 42
KEY_O = 43
KEY_P = 44
KEY_Q = 45
KEY_R = 46
KEY_S = 47
KEY_T = 48
KEY_U = 49
KEY_V = 50
KEY_W = 51
KEY_X = 52
KEY_Y = 53
KEY_Z = 54
# Keyevent Codes for Element FireTV Edition inputs. These should theorhetically work for all FireTV...TVs.
CYCLE_INPUT = 178
COMPONENT1 = 249
#COMPONENT2 = 250
COMPOSITE1 = 247
#COMPOSITE2 = 248
HDMI1 = 243
HDMI2 = 244
HDMI3 = 245
HDMI4 = 246
#VGA = 251
# Fire TV states.
STATE_ON = 'on'
STATE_IDLE = 'idle'
STATE_OFF = 'off'
STATE_PLAYING = 'play'
STATE_PAUSED = 'pause'
STATE_STANDBY = 'standby'
STATE_DISCONNECTED = 'disconnected'
PACKAGE_LAUNCHER = "com.amazon.tv.launcher"
PACKAGE_SETTINGS = "com.amazon.tv.settings"
INTENT_LAUNCH = "android.intent.category.LAUNCHER"
INTENT_HOME = "android.intent.category.HOME"
class FireTV:
""" Represents an Amazon Fire TV device. """
def __init__(self, host):
""" Initialize FireTV object.
:param host: Host in format <address>:port.
"""
self.host = host
self._adb = None
self.connect()
def connect(self):
""" Connect to an Amazon Fire TV device.
Will attempt to establish ADB connection to the given host.
Failure sets state to DISCONNECTED and disables sending actions.
"""
try:
self._adb = adb_commands.AdbCommands.ConnectDevice(
serial=self.host)
except socket_error as serr:
logging.warning("Couldn't connect to host: %s, error: %s", self.host, serr.strerror)
@property
def state(self):
""" Compute and return the device state.
:returns: Device state.
"""
# Check if device is disconnected.
if not self._adb:
return STATE_DISCONNECTED
# Check if device is off.
if not self._screen_on:
return STATE_OFF
# Check if screen saver is on.
if not self._awake:
return STATE_IDLE
# Check if the launcher is active.
if self._launcher or self._settings:
return STATE_STANDBY
# Check for a wake lock (device is playing).
if self._wake_lock:
return STATE_PLAYING
# Otherwise, device is paused.
return STATE_PAUSED
def running_apps(self):
""" Return an array of running user applications """
return self._ps('u0_a')
def app_state(self, app):
""" Informs if application is running """
if self.state == STATE_OFF or self.state == STATE_DISCONNECTED:
return STATE_OFF
if self.current_app["package"] == app:
return STATE_ON
return STATE_OFF
def turn_on(self):
""" Send power action if device is off. """
if self.state == STATE_OFF:
self._power()
def turn_off(self):
""" Send power action if device is not off. """
if self.state != STATE_OFF:
self._power()
def home(self):
""" Send home action. """
self._key(HOME)
def up(self):
""" Send up action. """
self._key(UP)
def down(self):
""" Send down action. """
self._key(DOWN)
def left(self):
""" Send left action. """
self._key(LEFT)
def right(self):
""" Send right action. """
self._key(RIGHT)
def enter(self):
""" Send enter action. """
self._key(ENTER)
def back(self):
""" Send back action. """
self._key(BACK)
def menu(self):
""" Send menu action. """
self._key(MENU)
def volume_up(self):
""" Send volume up action. """
self._key(VOLUME_UP)
def volume_down(self):
""" Send volume down action. """
self._key(VOLUME_DOWN)
def media_play_pause(self):
""" Send media play/pause action. """
self._key(PLAY_PAUSE)
def media_play(self):
""" Send media play action. """
self._key(PLAY)
def media_pause(self):
""" Send media pause action. """
self._key(PAUSE)
def media_next(self):
""" Send media next action (results in fast-forward). """
self._key(NEXT)
def media_previous(self):
""" Send media previous action (results in rewind). """
self._key(PREVIOUS)
def space(self):
""" Send space keypress. """
self._key(SPACE)
def key_0(self):
""" Send 0 keypress. """
self._key(KEY_0)
def key_1(self):
""" Send 1 keypress. """
self._key(KEY_1)
def key_2(self):
""" Send 2 keypress. """
self._key(KEY_2)
def key_3(self):
""" Send 3 keypress. """
self._key(KEY_3)
def key_4(self):
""" Send 4 keypress. """
self._key(KEY_4)
def key_5(self):
""" Send 5 keypress. """
self._key(KEY_5)
def key_6(self):
""" Send 6 keypress. """
self._key(KEY_6)
def key_7(self):
""" Send 7 keypress. """
self._key(KEY_7)
def key_8(self):
""" Send 8 keypress. """
self._key(KEY_8)
def key_9(self):
""" Send 9 keypress. """
self._key(KEY_9)
def key_a(self):
""" Send a keypress. """
self._key(KEY_A)
def key_b(self):
""" Send b keypress. """
self._key(KEY_B)
def key_c(self):
""" Send c keypress. """
self._key(KEY_C)
def key_d(self):
""" Send d keypress. """
self._key(KEY_D)
def key_e(self):
""" Send e keypress. """
self._key(KEY_E)
def key_f(self):
""" Send f keypress. """
self._key(KEY_F)
def key_g(self):
""" Send g keypress. """
self._key(KEY_G)
def key_h(self):
""" Send h keypress. """
self._key(KEY_H)
def key_i(self):
""" Send i keypress. """
self._key(KEY_I)
def key_j(self):
""" Send j keypress. """
self._key(KEY_J)
def key_k(self):
""" Send k keypress. """
self._key(KEY_K)
def key_l(self):
""" Send l keypress. """
self._key(KEY_L)
def key_m(self):
""" Send m keypress. """
self._key(KEY_M)
def key_n(self):
""" Send n keypress. """
self._key(KEY_N)
def key_o(self):
""" Send o keypress. """
self._key(KEY_O)
def key_p(self):
""" Send p keypress. """
self._key(KEY_P)
def key_q(self):
""" Send q keypress. """
self._key(KEY_Q)
def key_r(self):
""" Send r keypress. """
self._key(KEY_R)
def key_s(self):
""" Send s keypress. """
self._key(KEY_S)
def key_t(self):
""" Send t keypress. """
self._key(KEY_T)
def key_u(self):
""" Send u keypress. """
self._key(KEY_U)
def key_v(self):
""" Send v keypress. """
self._key(KEY_V)
def key_w(self):
""" Send w keypress. """
self._key(KEY_W)
def key_x(self):
""" Send x keypress. """
self._key(KEY_X)
def key_y(self):
""" Send y keypress. """
self._key(KEY_Y)
def key_z(self):
""" Send z keypress. """
self._key(KEY_Z)
def hdmi1(self):
""" Switch to HDMI1 """
self._key(HDMI1)
def hdmi2(self):
""" Switch to HDMI2 """
self._key(HDMI2)
def hdmi3(self):
""" Switch to HDMI3 """
self._key(HDMI3)
def hdmi4(self):
""" Switch to HDMI4 """
self._key(HDMI4)
def hdmi4(self):
""" Switch to HDMI4 """
self._key(HDMI4)
def component1(self):
""" Switch to Component 1 """
self._key(COMPONENT1)
# def component2(self):
# """ Switch to Component 2 """
# self._key(COMPONENT2)
def composite1(self):
""" Switch to Component 1 """
self._key(COMPOSITE1)
# def composite2(self):
# """ Switch to Component 2 """
# self._key(COMPOSITE2)
# def vga(self):
# """ Switch to VGA. Step 1: Find VGA cable... """
# self._key(VGA)
def cycle_input(self):
""" Cycle through the available inputs """
self._key(CYCLE_INPUT)
def _send_intent(self, pkg, intent, count=1):
if not self._adb:
return None
cmd = 'monkey -p {} -c {} {}; echo $?'.format(pkg, intent, count)
logging.debug("Sending an intent %s to %s (count: %s)", intent, pkg, count)
# adb shell outputs in weird format, so we cut it into lines,
# separate the retcode and return info to the user
res = self._adb.Shell(cmd).strip().split("\r\n")
retcode = res[-1]
output = "\n".join(res[:-1])
return {"retcode": retcode, "output": output}
def launch_app(self, app):
if not self._adb:
return None
return self._send_intent(app, INTENT_LAUNCH)
def stop_app(self, app):
if not self._adb:
return None
return self._send_intent(PACKAGE_LAUNCHER, INTENT_HOME)
@property
def current_app(self):
current_focus = self._dump("window windows", "mCurrentFocus").replace("\r", "")
#logging.error("Current: %s", current_focus)
#mCurrentFocus = Window{299091cd u0 com.netflix.ninja / com.netflix.ninja.MainActivity}
matches = WINDOW_REGEX.search(current_focus)
if matches:
(pkg, activity) = matches.group('package', 'activity')
return {"package": pkg, "activity": activity}
else:
logging.warning("Couldn't get current app, reply was %s", current_focus)
return None
@property
def _screen_on(self):
""" Check if the screen is on. """
return self._dump_has('power', 'Display Power', 'state=ON')
@property
def _awake(self):
""" Check if the device is awake (screen saver is not running). """
return self._dump_has('power', 'mWakefulness', 'Awake')
@property
def _wake_lock(self):
""" Check for wake locks (device is playing). """
return not self._dump_has('power', 'Locks', 'size=0')
@property
def _launcher(self):
""" Check if the active application is the Amazon TV launcher. """
return self.current_app["package"] == PACKAGE_LAUNCHER
@property
def _settings(self):
""" Check if the active application is the Amazon menu. """
return self.current_app["package"] == PACKAGE_SETTINGS
def _power(self):
""" Send power action. """
self._key(POWER)
def _input(self, cmd):
""" Send input to the device.
:param cmd: Input command.
"""
if not self._adb:
return
self._adb.Shell('input {0}'.format(cmd))
def _key(self, key):
""" Send a key event to device.
:param key: Key constant.
"""
self._input('keyevent {0}'.format(key))
def _dump(self, service, grep=None):
""" Perform a service dump.
:param service: Service to dump.
:param grep: Grep for this string.
:returns: Dump, optionally grepped.
"""
if not self._adb:
return
if grep:
return self._adb.Shell('dumpsys {0} | grep "{1}"'.format(service, grep))
return self._adb.Shell('dumpsys {0}'.format(service))
def _dump_has(self, service, grep, search):
""" Check if a dump has particular content.
:param service: Service to dump.
:param grep: Grep for this string.
:param search: Check for this substring.
:returns: Found or not.
"""
return self._dump(service, grep=grep).strip().find(search) > -1
def _ps(self, search=''):
""" Perform a ps command with optional filtering.
:param search: Check for this substring.
:returns: List of matching fields
"""
if not self._adb:
return
result = []
ps = self._adb.StreamingShell('ps')
try:
for bad_line in ps:
# The splitting of the StreamingShell doesn't always work
# this is to ensure that we get only one line
for line in bad_line.splitlines():
if search in line:
result.append(line.strip().rsplit(' ',1)[-1])
return result
except InvalidChecksumError as e:
print(e)
self.connect()
raise IOError