Skip to content

Commit

Permalink
Merge 902e2fb into 11b01d6
Browse files Browse the repository at this point in the history
  • Loading branch information
deviant-aut committed Mar 26, 2022
2 parents 11b01d6 + 902e2fb commit 76d143b
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
17 changes: 17 additions & 0 deletions androidtv/basetv/basetv.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ def _cmd_audio_state(self):
if constants.CUSTOM_AUDIO_STATE in self._custom_commands:
return self._custom_commands[constants.CUSTOM_AUDIO_STATE]

# Is this an Android 11 device?
if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11":
return constants.CMD_AUDIO_STATE11
return constants.CMD_AUDIO_STATE

def _cmd_current_app(self):
Expand All @@ -149,6 +152,9 @@ def _cmd_current_app(self):
and "Chromecast" in self.device_properties.get("model", "")
):
return constants.CMD_CURRENT_APP_GOOGLE_TV
# Is this an Android 11 device?
if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11":
return constants.CMD_CURRENT_APP11

return constants.CMD_CURRENT_APP

Expand All @@ -171,6 +177,9 @@ def _cmd_current_app_media_session_state(self):
and "Chromecast" in self.device_properties.get("model", "")
):
return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV
# Is this an Android 11 device?
if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11":
return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE11

return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE

Expand All @@ -186,6 +195,10 @@ def _cmd_hdmi_input(self):
if constants.CUSTOM_HDMI_INPUT in self._custom_commands:
return self._custom_commands[constants.CUSTOM_HDMI_INPUT]

# Is this an Android 11 device?
if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11":
return constants.CMD_HDMI_INPUT11

return constants.CMD_HDMI_INPUT

def _cmd_launch_app(self, app):
Expand Down Expand Up @@ -216,6 +229,10 @@ def _cmd_launch_app(self, app):
if self.DEVICE_ENUM == constants.DeviceEnum.FIRETV:
return constants.CMD_LAUNCH_APP_FIRETV.format(app)

# Is this an Android 11 device?
if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11":
return constants.CMD_LAUNCH_APP11.format(app)

return constants.CMD_LAUNCH_APP.format(app)

def _cmd_running_apps(self):
Expand Down
31 changes: 31 additions & 0 deletions androidtv/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,37 @@ class DeviceEnum(IntEnum):
#: Get the audio state
CMD_AUDIO_STATE = r"dumpsys audio | grep paused | grep -qv 'Buffer Queue' && echo -e '1\c' || (dumpsys audio | grep started | grep -qv 'Buffer Queue' && echo '2\c' || echo '0\c')"

#: Get the audio state for an Android 11 device
CMD_AUDIO_STATE11 = (
"CURRENT_AUDIO_STATE=$(dumpsys audio | sed -r -n '/[0-9]{2}-[0-9]{2}.*player piid:.*state:.*$/h; ${x;p;}') && "
+ r"echo $CURRENT_AUDIO_STATE | grep -q paused && echo -e '1\c' || { echo $CURRENT_AUDIO_STATE | grep -q started && echo '2\c' || echo '0\c' ; }"
)

#: Determine whether the device is awake
CMD_AWAKE = "dumpsys power | grep mWakefulness | grep -q Awake"

#: Parse current application identifier from dumpsys output and assign it to ``CURRENT_APP`` variable (assumes dumpsys output is momentarily set to ``CURRENT_APP`` variable)
CMD_PARSE_CURRENT_APP = "CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\\}*}"

#: Parse current application for an Android 11 device
CMD_PARSE_CURRENT_APP11 = "CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* }"

#: Assign focused application identifier to ``CURRENT_APP`` variable
CMD_DEFINE_CURRENT_APP_VARIABLE = (
"CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && " + CMD_PARSE_CURRENT_APP
)
#: Assign focused application identifier to ``CURRENT_APP`` variable for an Android 11 device
CMD_DEFINE_CURRENT_APP_VARIABLE11 = (
"CURRENT_APP=$(dumpsys window windows | grep 'Window #1') && " + CMD_PARSE_CURRENT_APP11
)


#: Output identifier for current/focused application
CMD_CURRENT_APP = CMD_DEFINE_CURRENT_APP_VARIABLE + " && echo $CURRENT_APP"

#: Output identifier for current/focused application for an Android 11 device
CMD_CURRENT_APP11 = CMD_DEFINE_CURRENT_APP_VARIABLE11 + " && echo $CURRENT_APP"

#: Assign focused application identifier to ``CURRENT_APP`` variable (for a Google TV device)
CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV = (
"CURRENT_APP=$(dumpsys activity a . | grep mResumedActivity) && " + CMD_PARSE_CURRENT_APP
Expand All @@ -102,6 +119,12 @@ class DeviceEnum(IntEnum):
"dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]'"
)

#: Get the HDMI input for an Android 11 device
CMD_HDMI_INPUT11 = (
"(HDMI=$(dumpsys tv_input | grep 'ResourceClientProfile {.*}' | grep -o -E '(hdmi_port=[0-9]|TV)') && { echo ${HDMI/hdmi_port=/HW} | cut -d' ' -f1 ; }) || "
+ CMD_HDMI_INPUT
)

#: Launch an app if it is not already the current app (assumes the variable ``CURRENT_APP`` has already been set)
CMD_LAUNCH_APP_CONDITION = (
"if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + " --pct-syskeys 0 1; fi"
Expand All @@ -117,6 +140,11 @@ class DeviceEnum(IntEnum):
CMD_DEFINE_CURRENT_APP_VARIABLE.replace("{", "{{").replace("}", "}}") + " && " + CMD_LAUNCH_APP_CONDITION
)

#: Launch an app if it is not already the current app on an Android 11 device
CMD_LAUNCH_APP11 = (
CMD_DEFINE_CURRENT_APP_VARIABLE11.replace("{", "{{").replace("}", "}}") + " && " + CMD_LAUNCH_APP_CONDITION
)

#: Launch an app on a Fire TV device
CMD_LAUNCH_APP_FIRETV = (
CMD_DEFINE_CURRENT_APP_VARIABLE.replace("{", "{{").replace("}", "}}") + " && " + CMD_LAUNCH_APP_CONDITION_FIRETV
Expand All @@ -133,6 +161,9 @@ class DeviceEnum(IntEnum):
#: Determine the current app and get the state from ``dumpsys media_session``
CMD_CURRENT_APP_MEDIA_SESSION_STATE = CMD_CURRENT_APP + " && " + CMD_MEDIA_SESSION_STATE

#: Determine the current app and get the state from ``dumpsys media_session`` for an Android 11 device
CMD_CURRENT_APP_MEDIA_SESSION_STATE11 = CMD_CURRENT_APP11 + " && " + CMD_MEDIA_SESSION_STATE

#: Determine the current app and get the state from ``dumpsys media_session`` for a Google TV device
CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV = CMD_CURRENT_APP_GOOGLE_TV + " && " + CMD_MEDIA_SESSION_STATE

Expand Down
2 changes: 2 additions & 0 deletions tests/generate_test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"CMD_SUCCESS1",
"CMD_SUCCESS1_FAILURE0",
"CMD_PARSE_CURRENT_APP",
"CMD_PARSE_CURRENT_APP11",
"CMD_DEFINE_CURRENT_APP_VARIABLE",
"CMD_DEFINE_CURRENT_APP_VARIABLE11",
"CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV",
"CMD_LAUNCH_APP_CONDITION",
"CMD_LAUNCH_APP_CONDITION_FIRETV",
Expand Down
54 changes: 54 additions & 0 deletions tests/test_basetv_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@
"ethmac": "ab:cd:ef:gh:ij:kl",
}

DEVICE_PROPERTIES_OUTPUT_SHIELD_TV_11 = """NVIDIA
SHIELD Android TV
0123456789012
11
"""

WIFIMAC_SHIELD_TV_11 = " link/ether 11:22:33:44:55:66 brd ff:ff:ff:ff:ff:ff"
ETHMAC_SHIELD_TV_11 = " link/ether ab:cd:ef:gh:ij:kl brd ff:ff:ff:ff:ff:ff"

DEVICE_PROPERTIES_DICT_SHIELD_TV_11 = {
"manufacturer": "NVIDIA",
"model": "SHIELD Android TV",
"serialno": "0123456789012",
"sw_version": "11",
"wifimac": "11:22:33:44:55:66",
"ethmac": "ab:cd:ef:gh:ij:kl",
}

INSTALLED_APPS_OUTPUT_1 = """package:org.example.app
package:org.example.launcher
"""
Expand Down Expand Up @@ -498,6 +516,42 @@ def test_get_device_properties(self):
device_properties = self.btv.get_device_properties()
self.assertDictEqual(DEVICE_PROPERTIES_DICT_SONY_TV, device_properties)

with patch.object(
self.btv._adb,
"shell",
side_effect=(DEVICE_PROPERTIES_OUTPUT_SHIELD_TV_11, ETHMAC_SHIELD_TV_11, WIFIMAC_SHIELD_TV_11),
):
self.btv = AndroidTVSync.from_base(self.btv)
device_properties = self.btv.get_device_properties()
assert self.btv.device_properties.get("sw_version", "") == "11"
assert self.btv.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV
self.assertDictEqual(DEVICE_PROPERTIES_DICT_SHIELD_TV_11, device_properties)
# _cmd_audio_state
self.assertEqual(
self.btv._cmd_audio_state(),
constants.CMD_AUDIO_STATE11,
)
# _cmd_current_app
self.assertEqual(
self.btv._cmd_current_app(),
constants.CMD_CURRENT_APP11,
)
# _cmd_current_app_media_session_state
self.assertEqual(
self.btv._cmd_current_app_media_session_state(),
constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE11,
)
# _cmd_hdmi_input
self.assertEqual(
self.btv._cmd_hdmi_input(),
constants.CMD_HDMI_INPUT11,
)
# _cmd_launch_app
self.assertEqual(
self.btv._cmd_launch_app("TEST"),
constants.CMD_LAUNCH_APP11.format("TEST"),
)

def test_get_installed_apps(self):
""" "Check that `get_installed_apps` works correctly."""
with patchers.patch_shell(INSTALLED_APPS_OUTPUT_1)[self.PATCH_KEY]:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ def test_constants(self):
r"dumpsys audio | grep paused | grep -qv 'Buffer Queue' && echo -e '1\c' || (dumpsys audio | grep started | grep -qv 'Buffer Queue' && echo '2\c' || echo '0\c')",
)

# CMD_AUDIO_STATE11
self.assertCommand(
constants.CMD_AUDIO_STATE11,
r"CURRENT_AUDIO_STATE=$(dumpsys audio | sed -r -n '/[0-9]{2}-[0-9]{2}.*player piid:.*state:.*$/h; ${x;p;}') && echo $CURRENT_AUDIO_STATE | grep -q paused && echo -e '1\c' || { echo $CURRENT_AUDIO_STATE | grep -q started && echo '2\c' || echo '0\c' ; }",
)

# CMD_AWAKE
self.assertCommand(constants.CMD_AWAKE, r"dumpsys power | grep mWakefulness | grep -q Awake")

Expand All @@ -77,6 +83,12 @@ def test_constants(self):
r"CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\}*} && echo $CURRENT_APP",
)

# CMD_CURRENT_APP11
self.assertCommand(
constants.CMD_CURRENT_APP11,
r"CURRENT_APP=$(dumpsys window windows | grep 'Window #1') && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* } && echo $CURRENT_APP",
)

# CMD_CURRENT_APP_GOOGLE_TV
self.assertCommand(
constants.CMD_CURRENT_APP_GOOGLE_TV,
Expand All @@ -89,6 +101,12 @@ def test_constants(self):
r"CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\}*} && echo $CURRENT_APP && dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {'",
)

# CMD_CURRENT_APP_MEDIA_SESSION_STATE11
self.assertCommand(
constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE11,
r"CURRENT_APP=$(dumpsys window windows | grep 'Window #1') && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* } && echo $CURRENT_APP && dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {'",
)

# CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV
self.assertCommand(
constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV,
Expand All @@ -107,6 +125,12 @@ def test_constants(self):
r"dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]'",
)

# CMD_HDMI_INPUT11
self.assertCommand(
constants.CMD_HDMI_INPUT11,
r"(HDMI=$(dumpsys tv_input | grep 'ResourceClientProfile {.*}' | grep -o -E '(hdmi_port=[0-9]|TV)') && { echo ${HDMI/hdmi_port=/HW} | cut -d' ' -f1 ; }) || dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]'",
)

# CMD_INSTALLED_APPS
self.assertCommand(constants.CMD_INSTALLED_APPS, r"pm list packages")

Expand All @@ -116,6 +140,12 @@ def test_constants(self):
r"CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && CURRENT_APP=${{CURRENT_APP#*ActivityRecord{{* * }} && CURRENT_APP=${{CURRENT_APP#*{{* * }} && CURRENT_APP=${{CURRENT_APP%%/*}} && CURRENT_APP=${{CURRENT_APP%\}}*}} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c android.intent.category.LEANBACK_LAUNCHER --pct-syskeys 0 1; fi",
)

# CMD_LAUNCH_APP11
self.assertCommand(
constants.CMD_LAUNCH_APP11,
r"CURRENT_APP=$(dumpsys window windows | grep 'Window #1') && CURRENT_APP=${{CURRENT_APP%%/*}} && CURRENT_APP=${{CURRENT_APP##* }} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c android.intent.category.LEANBACK_LAUNCHER --pct-syskeys 0 1; fi",
)

# CMD_LAUNCH_APP_FIRETV
self.assertCommand(
constants.CMD_LAUNCH_APP_FIRETV,
Expand Down

0 comments on commit 76d143b

Please sign in to comment.