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

Side by Side Diff: build/android/pylib/android_commands.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 unified diff | Download patch
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Provides an interface to communicate with the device via the adb command. 5 """Provides an interface to communicate with the device via the adb command.
6 6
7 Assumes adb binary is currently on system path. 7 Assumes adb binary is currently on system path.
8 """ 8 """
9 # pylint: disable-all 9 # pylint: disable-all
10 10
(...skipping 740 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 last_line = lines[-1] 751 last_line = lines[-1]
752 status_pos = last_line.rfind('%') 752 status_pos = last_line.rfind('%')
753 assert status_pos >= 0 753 assert status_pos >= 0
754 status = int(last_line[status_pos + 1:]) 754 status = int(last_line[status_pos + 1:])
755 if status_pos == 0: 755 if status_pos == 0:
756 lines = lines[:-1] 756 lines = lines[:-1]
757 else: 757 else:
758 lines = lines[:-1] + [last_line[:status_pos]] 758 lines = lines[:-1] + [last_line[:status_pos]]
759 return (status, lines) 759 return (status, lines)
760 760
761 def KillAll(self, process, signum=9, with_su=False): 761 def KillAll(self, process, signum=signal.SIGKILL, with_su=False):
762 """Android version of killall, connected via adb. 762 """Android version of killall, connected via adb.
763 763
764 Args: 764 Args:
765 process: name of the process to kill off. 765 process: name of the process to kill off.
766 signum: signal to use, 9 (SIGKILL) by default. 766 signum: signal to use, 9 (SIGKILL) by default.
767 with_su: wether or not to use su to kill the processes. 767 with_su: wether or not to use su to kill the processes.
768 768
769 Returns: 769 Returns:
770 the number of processes killed 770 the number of processes killed
771 """ 771 """
772 pids = self.ExtractPid(process) 772 pids = self.ExtractPid(process)
773 if pids: 773 if pids:
774 cmd = 'kill -%d %s' % (signum, ' '.join(pids)) 774 cmd = 'kill -%d %s' % (signum, ' '.join(pids))
775 if with_su: 775 if with_su:
776 self.RunShellCommandWithSU(cmd) 776 self.RunShellCommandWithSU(cmd)
777 else: 777 else:
778 self.RunShellCommand(cmd) 778 self.RunShellCommand(cmd)
779 return len(pids) 779 return len(pids)
780 780
781 def KillAllBlocking(self, process, timeout_sec): 781 def KillAllBlocking(self, process, timeout_sec, signum=signal.SIGKILL,
782 with_su=False):
782 """Blocking version of killall, connected via adb. 783 """Blocking version of killall, connected via adb.
783 784
784 This waits until no process matching the corresponding name appears in ps' 785 This waits until no process matching the corresponding name appears in ps'
785 output anymore. 786 output anymore.
786 787
787 Args: 788 Args:
788 process: name of the process to kill off 789 process: name of the process to kill off
789 timeout_sec: the timeout in seconds 790 timeout_sec: the timeout in seconds
790 791 signum: same as |KillAll|
792 with_su: same as |KillAll|
791 Returns: 793 Returns:
792 the number of processes killed 794 the number of processes killed
793 """ 795 """
794 processes_killed = self.KillAll(process) 796 processes_killed = self.KillAll(process, signum=signum, with_su=with_su)
795 if processes_killed: 797 if processes_killed:
796 elapsed = 0 798 elapsed = 0
797 wait_period = 0.1 799 wait_period = 0.1
798 # Note that this doesn't take into account the time spent in ExtractPid(). 800 # Note that this doesn't take into account the time spent in ExtractPid().
799 while self.ExtractPid(process) and elapsed < timeout_sec: 801 while self.ExtractPid(process) and elapsed < timeout_sec:
800 time.sleep(wait_period) 802 time.sleep(wait_period)
801 elapsed += wait_period 803 elapsed += wait_period
802 if elapsed >= timeout_sec: 804 if elapsed >= timeout_sec:
803 return 0 805 return processes_killed - self.ExtractPid(process)
804 return processes_killed 806 return processes_killed
805 807
806 @staticmethod 808 @staticmethod
807 def _GetActivityCommand(package, activity, wait_for_completion, action, 809 def _GetActivityCommand(package, activity, wait_for_completion, action,
808 category, data, extras, trace_file_name, force_stop, 810 category, data, extras, trace_file_name, force_stop,
809 flags): 811 flags):
810 """Creates command to start |package|'s activity on the device. 812 """Creates command to start |package|'s activity on the device.
811 813
812 Args - as for StartActivity 814 Args - as for StartActivity
813 815
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
856 activity: Name of activity (e.g. '.Main' or 858 activity: Name of activity (e.g. '.Main' or
857 'com.google.android.apps.chrome.Main'). 859 'com.google.android.apps.chrome.Main').
858 wait_for_completion: wait for the activity to finish launching (-W flag). 860 wait_for_completion: wait for the activity to finish launching (-W flag).
859 action: string (e.g. "android.intent.action.MAIN"). Default is VIEW. 861 action: string (e.g. "android.intent.action.MAIN"). Default is VIEW.
860 category: string (e.g. "android.intent.category.HOME") 862 category: string (e.g. "android.intent.category.HOME")
861 data: Data string to pass to activity (e.g. 'http://www.example.com/'). 863 data: Data string to pass to activity (e.g. 'http://www.example.com/').
862 extras: Dict of extras to pass to activity. Values are significant. 864 extras: Dict of extras to pass to activity. Values are significant.
863 trace_file_name: If used, turns on and saves the trace to this file name. 865 trace_file_name: If used, turns on and saves the trace to this file name.
864 force_stop: force stop the target app before starting the activity (-S 866 force_stop: force stop the target app before starting the activity (-S
865 flag). 867 flag).
868 Returns:
869 The output of the underlying command as a list of lines.
866 """ 870 """
867 cmd = self._GetActivityCommand(package, activity, wait_for_completion, 871 cmd = self._GetActivityCommand(package, activity, wait_for_completion,
868 action, category, data, extras, 872 action, category, data, extras,
869 trace_file_name, force_stop, flags) 873 trace_file_name, force_stop, flags)
870 self.RunShellCommand(cmd) 874 return self.RunShellCommand(cmd)
871 875
872 def StartActivityTimed(self, package, activity, wait_for_completion=False, 876 def StartActivityTimed(self, package, activity, wait_for_completion=False,
873 action='android.intent.action.VIEW', 877 action='android.intent.action.VIEW',
874 category=None, data=None, 878 category=None, data=None,
875 extras=None, trace_file_name=None, 879 extras=None, trace_file_name=None,
876 force_stop=False, flags=None): 880 force_stop=False, flags=None):
877 """Starts |package|'s activity on the device, returning the start time 881 """Starts |package|'s activity on the device, returning the start time
878 882
879 Args - as for StartActivity 883 Args - as for StartActivity
880 884
881 Returns: 885 Returns:
882 a timestamp string for the time at which the activity started 886 A tuple containing:
887 - the output of the underlying command as a list of lines, and
888 - a timestamp string for the time at which the activity started
883 """ 889 """
884 cmd = self._GetActivityCommand(package, activity, wait_for_completion, 890 cmd = self._GetActivityCommand(package, activity, wait_for_completion,
885 action, category, data, extras, 891 action, category, data, extras,
886 trace_file_name, force_stop, flags) 892 trace_file_name, force_stop, flags)
887 self.StartMonitoringLogcat() 893 self.StartMonitoringLogcat()
888 self.RunShellCommand('log starting activity; ' + cmd) 894 out = self.RunShellCommand('log starting activity; ' + cmd)
889 activity_started_re = re.compile('.*starting activity.*') 895 activity_started_re = re.compile('.*starting activity.*')
890 m = self.WaitForLogMatch(activity_started_re, None) 896 m = self.WaitForLogMatch(activity_started_re, None)
891 assert m 897 assert m
892 start_line = m.group(0) 898 start_line = m.group(0)
893 return GetLogTimestamp(start_line, self.GetDeviceYear()) 899 return (out, GetLogTimestamp(start_line, self.GetDeviceYear()))
894 900
895 def StartCrashUploadService(self, package): 901 def StartCrashUploadService(self, package):
896 # TODO(frankf): We really need a python wrapper around Intent 902 # TODO(frankf): We really need a python wrapper around Intent
897 # to be shared with StartActivity/BroadcastIntent. 903 # to be shared with StartActivity/BroadcastIntent.
898 cmd = ( 904 cmd = (
899 'am startservice -a %s.crash.ACTION_FIND_ALL -n ' 905 'am startservice -a %s.crash.ACTION_FIND_ALL -n '
900 '%s/%s.crash.MinidumpUploadService' % 906 '%s/%s.crash.MinidumpUploadService' %
901 (constants.PACKAGE_INFO['chrome'].package, 907 (constants.PACKAGE_INFO['chrome'].package,
902 package, 908 package,
903 constants.PACKAGE_INFO['chrome'].package)) 909 constants.PACKAGE_INFO['chrome'].package))
(...skipping 1079 matching lines...) Expand 10 before | Expand all | Expand 10 after
1983 """ 1989 """
1984 def __init__(self, output): 1990 def __init__(self, output):
1985 self._output = output 1991 self._output = output
1986 1992
1987 def write(self, data): 1993 def write(self, data):
1988 data = data.replace('\r\r\n', '\n') 1994 data = data.replace('\r\r\n', '\n')
1989 self._output.write(data) 1995 self._output.write(data)
1990 1996
1991 def flush(self): 1997 def flush(self):
1992 self._output.flush() 1998 self._output.flush()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698