Skip to content

Commit

Permalink
Merge 20a39b1 into cdc291d
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Jan 3, 2020
2 parents cdc291d + 20a39b1 commit 3116632
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
14 changes: 7 additions & 7 deletions androidtv/basetv.py
Expand Up @@ -805,21 +805,21 @@ def _send_intent(self, pkg, intent, count=1):

return {"output": output, "retcode": retcode}

def launch_app(self, app):
def launch_app(self, app, check_current=True):
"""Launch an app.
Parameters
----------
app : str
The ID of the app that will be launched
Returns
-------
dict
A dictionary with keys ``'output'`` and ``'retcode'``, if they could be determined; otherwise, an empty dictionary
check_current : book
Whether to check that the app is not already the current app before launching it
"""
return self._send_intent(app, constants.INTENT_LAUNCH)
if check_current:
self._adb.shell(constants.CMD_LAUNCH_APP.format(app))
else:
self._send_intent(app, constants.INTENT_LAUNCH)

def stop_app(self, app):
"""Stop an app.
Expand Down
13 changes: 8 additions & 5 deletions androidtv/constants.py
Expand Up @@ -11,6 +11,11 @@
import re


# Intents
INTENT_LAUNCH = "android.intent.category.LAUNCHER"
INTENT_HOME = "android.intent.category.HOME"


# echo '1' if the previous shell command was successful
CMD_SUCCESS1 = r" && echo -e '1\c'"

Expand All @@ -26,6 +31,9 @@
#: Get the current app
CMD_CURRENT_APP = "CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP"

#: Launch an app if it is not already the current app
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 + " 1; fi"

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

Expand Down Expand Up @@ -82,11 +90,6 @@
CMD_MAC_ETH0 = "ip addr show eth0 | grep -m 1 ether"


# Intents
INTENT_LAUNCH = "android.intent.category.LAUNCHER"
INTENT_HOME = "android.intent.category.HOME"


# ADB key event codes
# https://developer.android.com/reference/android/view/KeyEvent
KEY_BACK = 4
Expand Down
14 changes: 8 additions & 6 deletions tests/test_firetv.py
Expand Up @@ -151,15 +151,17 @@ def test_launch_app_stop_app(self):
"""Test that the ``FireTV.launch_app`` and ``FireTV.stop_app`` methods work correctly.
"""
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell('output\r\nretcode')[self.PATCH_KEY]:
result = self.ftv.launch_app("TEST")
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell('output\r\nretcode')[self.PATCH_KEY]:
self.ftv.launch_app("TEST", check_current=False)
self.assertEqual(getattr(self.ftv._adb, self.ADB_ATTR).shell_cmd, "monkey -p TEST -c android.intent.category.LAUNCHER 1; echo $?")
self.assertDictEqual(result, {'output': 'output', 'retcode': 'retcode'})

with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell(None)[self.PATCH_KEY]:
result = self.ftv.launch_app("TEST")
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell(None)[self.PATCH_KEY]:
result = self.ftv.launch_app("TEST", check_current=False)
self.assertEqual(getattr(self.ftv._adb, self.ADB_ATTR).shell_cmd, "monkey -p TEST -c android.intent.category.LAUNCHER 1; echo $?")
self.assertDictEqual(result, {})

with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell(None)[self.PATCH_KEY]:
self.ftv.launch_app("TEST")
self.assertEqual(getattr(self.ftv._adb, self.ADB_ATTR).shell_cmd, constants.CMD_LAUNCH_APP.format("TEST"))

self.ftv.stop_app("TEST")
self.assertEqual(getattr(self.ftv._adb, self.ADB_ATTR).shell_cmd, "am force-stop TEST")
Expand Down

0 comments on commit 3116632

Please sign in to comment.