From 38037850b66b5295f2dd199af170948366952e0a Mon Sep 17 00:00:00 2001
From: Valentin Schneider <valentin.schneider@arm.com>
Date: Wed, 20 Jun 2018 17:49:20 +0100
Subject: [PATCH] AndroidTarget: Add force_new parameter to open_url

When True, this will force whatever application Android deems best
for viewing that url to be relaunched.

This can be useful when trying to open e.g. Google Maps search URLs,
as the actual search result can be influenced by the location being
currently displayed. Forcing the app the be relaunched allows us to
have reproducible behaviours.
---
 devlib/target.py | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/devlib/target.py b/devlib/target.py
index 75b6592..4a2cfbb 100644
--- a/devlib/target.py
+++ b/devlib/target.py
@@ -16,7 +16,7 @@ from devlib.module import get_module
 from devlib.platform import Platform
 from devlib.exception import TargetError, TargetNotRespondingError, TimeoutError
 from devlib.utils.ssh import SshConnection
-from devlib.utils.android import AdbConnection, AndroidProperties, LogcatMonitor, adb_command, adb_disconnect
+from devlib.utils.android import AdbConnection, AndroidProperties, LogcatMonitor, adb_command, adb_disconnect, INTENT_FLAGS
 from devlib.utils.misc import memoized, isiterable, convert_new_lines
 from devlib.utils.misc import commonprefix, escape_double_quotes, merge_lists
 from devlib.utils.misc import ABI_MAP, get_cpu_name, ranges_to_list
@@ -1423,8 +1423,23 @@ class AndroidTarget(Target):
         cmd = 'settings put system user_rotation {}'
         self.execute(cmd.format(rotation))
 
-    def open_url(self, url):
+    def open_url(self, url, force_new=False):
+        """
+        Start a view activity by specifying an URL
+
+        :param url: URL of the item to display
+        :type url: str
+
+        :param force_new: Force the viewing application to be relaunched
+            if it is already running
+        :type force_new: bool
+        """
         cmd = 'am start -a android.intent.action.VIEW -d "{}"'
+
+        if force_new:
+            cmd = cmd + ' -f {}'.format(INTENT_FLAGS['ACTIVITY_NEW_TASK'] |
+                                        INTENT_FLAGS['ACTIVITY_CLEAR_TASK'])
+
         self.execute(cmd.format(escape_double_quotes(url)))
 
     def homescreen(self):