Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3840)

Unified Diff: build/android/pylib/device/device_utils.py

Issue 338353004: [Android] Switch KillAll, StartActivity, and BroadcastIntent to DeviceUtils. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « build/android/pylib/content_settings.py ('k') | build/android/pylib/device/intent.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: build/android/pylib/device/device_utils.py
diff --git a/build/android/pylib/device/device_utils.py b/build/android/pylib/device/device_utils.py
index 0fee10a7fb0c701551179ae6f057ce2729318053..e6f08629c584c0e167e9303f419e3c5128cced72 100644
--- a/build/android/pylib/device/device_utils.py
+++ b/build/android/pylib/device/device_utils.py
@@ -8,6 +8,7 @@ Eventually, this will be based on adb_wrapper.
"""
# pylint: disable=W0613
+import signal
import time
import pylib.android_commands
@@ -244,8 +245,8 @@ class DeviceUtils(object):
['adb', 'install', apk_path], str(e))
@decorators.WithTimeoutAndRetriesFromInstance()
- def RunShellCommand(self, cmd, check_return=False, root=False, timeout=None,
- retries=None):
+ def RunShellCommand(self, cmd, check_return=False, as_root=False,
+ timeout=None, retries=None):
"""Run an ADB shell command.
TODO(jbudorick) Switch the default value of check_return to True after
@@ -255,6 +256,8 @@ class DeviceUtils(object):
cmd: A list containing the command to run on the device and any arguments.
check_return: A boolean indicating whether or not the return code should
be checked.
+ as_root: A boolean indicating whether the shell command should be run
+ with root privileges.
timeout: Same as for |IsOnline|.
retries: Same as for |IsOnline|.
Raises:
@@ -262,10 +265,10 @@ class DeviceUtils(object):
Returns:
The output of the command.
"""
- return self._RunShellCommandImpl(cmd, check_return=check_return, root=root,
- timeout=timeout)
+ return self._RunShellCommandImpl(cmd, check_return=check_return,
+ as_root=as_root, timeout=timeout)
- def _RunShellCommandImpl(self, cmd, check_return=False, root=False,
+ def _RunShellCommandImpl(self, cmd, check_return=False, as_root=False,
timeout=None):
"""Implementation of RunShellCommand.
@@ -278,6 +281,7 @@ class DeviceUtils(object):
Args:
cmd: Same as for |RunShellCommand|.
check_return: Same as for |RunShellCommand|.
+ as_root: Same as for |RunShellCommand|.
timeout: Same as for |IsOnline|.
Raises:
Same as for |RunShellCommand|.
@@ -286,7 +290,7 @@ class DeviceUtils(object):
"""
if isinstance(cmd, list):
cmd = ' '.join(cmd)
- if root and not self.HasRoot():
+ if as_root and not self.HasRoot():
cmd = 'su -c %s' % cmd
if check_return:
code, output = self.old_interface.GetShellCommandStatusAndOutput(
@@ -298,6 +302,81 @@ class DeviceUtils(object):
output = self.old_interface.RunShellCommand(cmd, timeout_time=timeout)
return output
+ @decorators.WithTimeoutAndRetriesFromInstance()
+ def KillAll(self, process_name, signum=signal.SIGKILL, as_root=False,
+ blocking=False, timeout=None, retries=None):
+ """Kill all processes with the given name on the device.
+
frankf 2014/06/20 00:56:56 Update the docstring to include the exception
jbudorick 2014/06/23 16:44:02 Done.
+ Args:
+ process_name: A string containing the name of the process to kill.
+ signum: An integer containing the signal number to send to kill. Defaults
+ to 9 (SIGKILL).
+ as_root: A boolean indicating whether the kill should be executed with
+ root priveleges.
+ blocking: A boolean indicating whether we should wait until all processes
+ with the given |process_name| are dead.
+ timeout: Same as for |IsOnline|.
+ retries: Same as for |IsOnline|.
+ """
+ pids = self.old_interface.ExtractPid(process_name)
+ if len(pids) == 0:
+ raise device_errors.CommandFailedError(
+ ['adb', 'shell', 'kill'], 'No process "%s"' % process_name)
frankf 2014/06/20 00:56:55 Why does this include such a command list? You hav
jbudorick 2014/06/23 16:44:02 Created a new AdbCommandFailedError that derives f
+
+ if blocking:
+ total_killed = self.old_interface.KillAllBlocking(
+ process_name, signum=signum, with_su=as_root, timeout_sec=timeout)
+ else:
+ total_killed = self.old_interface.KillAll(
+ process_name, signum=signum, with_su=as_root)
+ if total_killed == 0:
+ raise device_errors.CommandFailedError(
+ ['adb', 'shell', 'kill'], 'Failed to kill "%s"' % process_name)
+
+ @decorators.WithTimeoutAndRetriesFromInstance()
+ def StartActivity(self, intent, blocking=False, trace_file_name=None,
+ force_stop=False, timeout=None, retries=None):
+ """Start package's activity on the device.
+
+ Args:
+ intent: An Intent to send.
+ blocking: A boolean indicating whether we should wait for the activity to
+ finish launching.
+ trace_file_name: If present, a string that both indicates that we want to
+ profile the activity and contains the path to which the
+ trace should be saved.
+ force_stop: A boolean indicating whether we should stop the activity
+ before starting it.
+ timeout: Same as for |IsOnline|.
+ retries: Same as for |IsOnline|.
+ """
+ output = self.old_interface.StartActivity(
+ intent.package, intent.activity, wait_for_completion=blocking,
+ action=intent.action, category=intent.category, data=intent.data,
+ extras=intent.extras, trace_file_name=trace_file_name,
+ force_stop=force_stop, flags=intent.flags)
+ for l in output:
+ if l.startswith('Error:'):
+ raise device_errors.CommandFailedError(
+ ['adb', 'shell', 'am', 'start'], l)
frankf 2014/06/20 00:56:55 same here
jbudorick 2014/06/23 16:44:02 using the reworked CommandFailedError, as describe
+
+ @decorators.WithTimeoutAndRetriesFromInstance()
+ def BroadcastIntent(self, intent, timeout=None, retries=None):
+ """Send a broadcast intent.
+
+ Args:
+ intent: An Intent to broadcast.
+ timeout: Same as for |IsOnline|.
+ retries: Same as for |IsOnline|.
+ """
+ package, old_intent = intent.action.rsplit('.', 1)
+ if intent.extras is None:
+ args = []
+ else:
+ args = ['-e %s%s' % (k, ' "%s"' % v if v else '')
+ for k, v in intent.extras.items() if len(k) > 0]
+ self.old_interface.BroadcastIntent(package, old_intent, *args)
+
def __str__(self):
"""Returns the device serial."""
return self.old_interface.GetDevice()
« no previous file with comments | « build/android/pylib/content_settings.py ('k') | build/android/pylib/device/intent.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698