Skip to content

Commit 6368696

Browse files
JeffLIrionmartomo
andauthored
Improve support for Sony TV devices (#236)
* Improve extraction of current application identifier from dumpsys output. * Support HDMI input detection for Sony TV devices. * Return hardware identifier for external input instead of HDMI port number. To ensure consistent hdmi_input values between devices running Android TV, besides the fact that they do not match (e.g. port number 1 might equal HW5). Additionally hardware identifiers are more in compliance with the Android TV Input Framework. Each vendor can access the hardware definition as specified in tv_input module (TV Input HAL) through their implementation of the TV Input Manager service to access TV-specific hardware. * Use string replacement for hdmi_input test cases instead of splitting constant. * Added hdmi_input test cases for non hardware definition and non TV Input Manager service activities. * Update the commands checked in 'test_constants' * Rename constants * ' -> " * Add comments (for better git diffs) * Checkout test_constants.py from master * Add comments (for better git diffs) * Checkout test_constants.py from this branch * Update androidtv/constants.py Co-authored-by: Martijn Dijkhuizen <1850135+martomo@users.noreply.github.com> * Update androidtv/constants.py Co-authored-by: Martijn Dijkhuizen <1850135+martomo@users.noreply.github.com> * Rename CMD_GET_CURRENT_APP constant to CMD_DEFINE_CURRENT_APP_VARIABLE. (#237) * Add ' && ' * Reinstate match limit and rename test helper (#238) * Reinstate maximum match limit in HDMI input command. * Rename test helper to properly reflect command being called. Co-authored-by: Martijn Dijkhuizen <git@martomo.nl> Co-authored-by: Martijn Dijkhuizen <1850135+martomo@users.noreply.github.com>
1 parent dfb353c commit 6368696

File tree

6 files changed

+465
-36
lines changed

6 files changed

+465
-36
lines changed

androidtv/constants.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,32 @@
2828
#: Determine whether the device is awake
2929
CMD_AWAKE = "dumpsys power | grep mWakefulness | grep -q Awake"
3030

31-
#: Get the current app
32-
CMD_CURRENT_APP = "CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP"
31+
#: Parse current application identifier from dumpsys output and assign it to ``CURRENT_APP`` variable (assumes dumpsys output is momentarily set to ``CURRENT_APP`` variable)
32+
CMD_PARSE_CURRENT_APP = 'CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\\}*}'
3333

34-
#: Get the current app for a Google TV device
35-
CMD_CURRENT_APP_GOOGLE_TV = "CURRENT_APP=$(dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP"
34+
#: Assign focused application identifier to ``CURRENT_APP`` variable
35+
CMD_DEFINE_CURRENT_APP_VARIABLE = "CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && " + CMD_PARSE_CURRENT_APP
36+
37+
#: Output identifier for current/focused application
38+
CMD_CURRENT_APP = CMD_DEFINE_CURRENT_APP_VARIABLE + ' && echo $CURRENT_APP'
39+
40+
#: Assign focused application identifier to ``CURRENT_APP`` variable (for a Google TV device)
41+
CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV = 'CURRENT_APP=$(dumpsys activity a . | grep mResumedActivity) && ' + CMD_PARSE_CURRENT_APP
42+
43+
#: Output identifier for current/focused application (for a Google TV device)
44+
CMD_CURRENT_APP_GOOGLE_TV = CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV + ' && echo $CURRENT_APP'
3645

3746
#: Get the HDMI input
38-
CMD_HDMI_INPUT = "dumpsys activity starter | grep -o 'HDMIInputService\\/HW[0-9]' -m 1 | grep -o 'HW[0-9]'"
47+
CMD_HDMI_INPUT = "dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]'"
48+
49+
#: Launch an app if it is not already the current app (assumes the variable ``CURRENT_APP`` has already been set)
50+
CMD_LAUNCH_APP_CONDITION = "if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + " --pct-syskeys 0 1; fi"
3951

4052
#: Launch an app if it is not already the current app
41-
CMD_LAUNCH_APP = "CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${{CURRENT_APP#*{{* * }} && CURRENT_APP=${{CURRENT_APP%%/*}} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + " --pct-syskeys 0 1; fi"
53+
CMD_LAUNCH_APP = CMD_DEFINE_CURRENT_APP_VARIABLE.replace('{', '{{').replace('}', '}}') + ' && ' + CMD_LAUNCH_APP_CONDITION
4254

43-
#: Launch an app if it is not already the current app (for Google TV devices)
44-
CMD_LAUNCH_APP_GOOGLE_TV = "CURRENT_APP=$(dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${{CURRENT_APP%%/*}} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + " --pct-syskeys 0 1; fi"
55+
#: Launch an app on a Google TV device
56+
CMD_LAUNCH_APP_GOOGLE_TV = CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV.replace('{', '{{').replace('}', '}}') + ' && ' + CMD_LAUNCH_APP_CONDITION
4557

4658
#: Get the state from ``dumpsys media_session``; this assumes that the variable ``CURRENT_APP`` has been defined
4759
CMD_MEDIA_SESSION_STATE = "dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {'"

tests/test_androidtv_sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
GET_PROPERTIES_OUTPUT3D = GET_PROPERTIES_OUTPUT3.splitlines()[0]
119119
GET_PROPERTIES_OUTPUT3E = '\n'.join(GET_PROPERTIES_OUTPUT3.splitlines()[:2])
120120
GET_PROPERTIES_OUTPUT3F = '\n'.join(GET_PROPERTIES_OUTPUT3.splitlines()[:3])
121-
GET_PROPERTIES_OUTPUT3G = '\n'.join(GET_PROPERTIES_OUTPUT3.splitlines()[:4]) + "HDMI"
121+
GET_PROPERTIES_OUTPUT3G = '\n'.join(GET_PROPERTIES_OUTPUT3.splitlines()[:4]) + "HW2"
122122

123123
GET_PROPERTIES_DICT3A = {'screen_on': True,
124124
'awake': False,
@@ -196,7 +196,7 @@
196196
'is_volume_muted': None,
197197
'volume': None,
198198
'running_apps': None,
199-
'hdmi_input': 'HDMI'}
199+
'hdmi_input': 'HW2'}
200200

201201
GET_PROPERTIES_OUTPUT4 = """111Wake Locks: size=2
202202
com.amazon.tv.launcher

tests/test_basetv_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ async def test_get_hdmi_input(self):
335335
"""Check that the ``get_hdmi_input`` function works correctly.
336336
337337
"""
338-
with async_patchers.patch_shell("HDMI2")[self.PATCH_KEY]:
338+
with async_patchers.patch_shell("HW2")[self.PATCH_KEY]:
339339
with patch_calls(self.btv, self.btv._get_hdmi_input) as patched:
340340
await self.btv.get_hdmi_input()
341341
assert patched.called

tests/test_basetv_sync.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@
6262
Device "eth0" does not exist.
6363
"""
6464

65+
DEVICE_PROPERTIES_OUTPUT_SONY_TV = """Sony
66+
BRAVIA 4K GB
67+
SERIALNO
68+
8.0.0
69+
link/ether 11:22:33:44:55:66 brd ff:ff:ff:ff:ff:ff
70+
link/ether ab:cd:ef:gh:ij:kl brd ff:ff:ff:ff:ff:ff
71+
"""
72+
73+
DEVICE_PROPERTIES_DICT_SONY_TV = {
74+
'manufacturer': 'Sony',
75+
'model': 'BRAVIA 4K GB',
76+
'serialno': 'SERIALNO',
77+
'sw_version': '8.0.0',
78+
'wifimac': '11:22:33:44:55:66',
79+
'ethmac': 'ab:cd:ef:gh:ij:kl'
80+
}
81+
6582
INSTALLED_APPS_OUTPUT_1 = """package:org.example.app
6683
package:org.example.launcher
6784
"""
@@ -350,6 +367,10 @@ def test_get_device_properties(self):
350367
self.assertEqual(self.btv.device_properties["manufacturer"], "Google")
351368
self.assertEqual(self.btv._cmd_get_properties_lazy_no_running_apps, constants.CMD_GOOGLE_TV_PROPERTIES_LAZY_NO_RUNNING_APPS)
352369

370+
with patchers.patch_shell(DEVICE_PROPERTIES_OUTPUT_SONY_TV)[self.PATCH_KEY]:
371+
device_properties = self.btv.get_device_properties()
372+
self.assertDictEqual(DEVICE_PROPERTIES_DICT_SONY_TV, device_properties)
373+
353374
def test_get_installed_apps(self):
354375
""""Check that `get_installed_apps` works correctly.
355376
@@ -493,14 +514,14 @@ def test_get_hdmi_input(self):
493514
"""Check that the ``get_hdmi_input`` function works correctly.
494515
495516
"""
496-
with patchers.patch_shell("HDMI2")[self.PATCH_KEY]:
497-
self.assertEqual(self.btv.get_hdmi_input(), "HDMI2")
517+
with patchers.patch_shell("HW2")[self.PATCH_KEY]:
518+
self.assertEqual(self.btv.get_hdmi_input(), "HW2")
498519

499-
with patchers.patch_shell("HDMI2\n")[self.PATCH_KEY]:
500-
self.assertEqual(self.btv.get_hdmi_input(), "HDMI2")
520+
with patchers.patch_shell("HW2\n")[self.PATCH_KEY]:
521+
self.assertEqual(self.btv.get_hdmi_input(), "HW2")
501522

502-
with patchers.patch_shell("HDMI2\r\n")[self.PATCH_KEY]:
503-
self.assertEqual(self.btv.get_hdmi_input(), "HDMI2")
523+
with patchers.patch_shell("HW2\r\n")[self.PATCH_KEY]:
524+
self.assertEqual(self.btv.get_hdmi_input(), "HW2")
504525

505526
with patchers.patch_shell("")[self.PATCH_KEY]:
506527
self.assertIsNone(self.btv.get_hdmi_input())

0 commit comments

Comments
 (0)