From 88178bfcb78b6ff5541f5257e678b377aa8beadc Mon Sep 17 00:00:00 2001 From: "qsr@chromium.org" Date: Tue, 20 May 2014 15:56:32 +0000 Subject: [PATCH] Add timeout for usb charging commands. Also check that we are root, as non rooted cannot control usb charging. BUG=375178 R=tonyg@chromium.org Review URL: https://codereview.chromium.org/292743005 git-svn-id: http://src.chromium.org/svn/trunk/src/build@271670 4ff67af0-8c30-449e-8e8b-ad334ec8d88c --- android/pylib/android_commands.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/android/pylib/android_commands.py b/android/pylib/android_commands.py index 816b00247..0079051d6 100644 --- a/android/pylib/android_commands.py +++ b/android/pylib/android_commands.py @@ -1913,6 +1913,8 @@ class AndroidCommands(object): if self._control_usb_charging_command['cached']: return self._control_usb_charging_command['command'] self._control_usb_charging_command['cached'] = True + if not self.IsRootEnabled(): + return None for command in CONTROL_USB_CHARGING_COMMANDS: # Assert command is valid. assert 'disable_command' in command @@ -1927,26 +1929,32 @@ class AndroidCommands(object): def CanControlUsbCharging(self): return self._GetControlUsbChargingCommand() is not None - def DisableUsbCharging(self): + def DisableUsbCharging(self, timeout=10): command = self._GetControlUsbChargingCommand() if not command: raise Exception('Unable to act on usb charging.') disable_command = command['disable_command'] + t0 = time.time() # Do not loop directly on self.IsDeviceCharging to cut the number of calls # to the device. while True: + if t0 + timeout - time.time() < 0: + raise pexpect.TIMEOUT('Unable to enable USB charging in time.') self.RunShellCommand(disable_command) if not self.IsDeviceCharging(): break - def EnableUsbCharging(self): + def EnableUsbCharging(self, timeout=10): command = self._GetControlUsbChargingCommand() if not command: raise Exception('Unable to act on usb charging.') disable_command = command['enable_command'] + t0 = time.time() # Do not loop directly on self.IsDeviceCharging to cut the number of calls # to the device. while True: + if t0 + timeout - time.time() < 0: + raise pexpect.TIMEOUT('Unable to enable USB charging in time.') self.RunShellCommand(disable_command) if self.IsDeviceCharging(): break