OLD | NEW |
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 1895 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1906 device_repr = self._device[-4:] | 1906 device_repr = self._device[-4:] |
1907 else: | 1907 else: |
1908 device_repr = '????' | 1908 device_repr = '????' |
1909 for line in out: | 1909 for line in out: |
1910 logging.info('[%s]> %s', device_repr, line) | 1910 logging.info('[%s]> %s', device_repr, line) |
1911 | 1911 |
1912 def _GetControlUsbChargingCommand(self): | 1912 def _GetControlUsbChargingCommand(self): |
1913 if self._control_usb_charging_command['cached']: | 1913 if self._control_usb_charging_command['cached']: |
1914 return self._control_usb_charging_command['command'] | 1914 return self._control_usb_charging_command['command'] |
1915 self._control_usb_charging_command['cached'] = True | 1915 self._control_usb_charging_command['cached'] = True |
| 1916 if not self.IsRootEnabled(): |
| 1917 return None |
1916 for command in CONTROL_USB_CHARGING_COMMANDS: | 1918 for command in CONTROL_USB_CHARGING_COMMANDS: |
1917 # Assert command is valid. | 1919 # Assert command is valid. |
1918 assert 'disable_command' in command | 1920 assert 'disable_command' in command |
1919 assert 'enable_command' in command | 1921 assert 'enable_command' in command |
1920 assert 'witness_file' in command | 1922 assert 'witness_file' in command |
1921 witness_file = command['witness_file'] | 1923 witness_file = command['witness_file'] |
1922 if self.FileExistsOnDevice(witness_file): | 1924 if self.FileExistsOnDevice(witness_file): |
1923 self._control_usb_charging_command['command'] = command | 1925 self._control_usb_charging_command['command'] = command |
1924 return command | 1926 return command |
1925 return None | 1927 return None |
1926 | 1928 |
1927 def CanControlUsbCharging(self): | 1929 def CanControlUsbCharging(self): |
1928 return self._GetControlUsbChargingCommand() is not None | 1930 return self._GetControlUsbChargingCommand() is not None |
1929 | 1931 |
1930 def DisableUsbCharging(self): | 1932 def DisableUsbCharging(self, timeout=10): |
1931 command = self._GetControlUsbChargingCommand() | 1933 command = self._GetControlUsbChargingCommand() |
1932 if not command: | 1934 if not command: |
1933 raise Exception('Unable to act on usb charging.') | 1935 raise Exception('Unable to act on usb charging.') |
1934 disable_command = command['disable_command'] | 1936 disable_command = command['disable_command'] |
| 1937 t0 = time.time() |
1935 # Do not loop directly on self.IsDeviceCharging to cut the number of calls | 1938 # Do not loop directly on self.IsDeviceCharging to cut the number of calls |
1936 # to the device. | 1939 # to the device. |
1937 while True: | 1940 while True: |
| 1941 if t0 + timeout - time.time() < 0: |
| 1942 raise pexpect.TIMEOUT('Unable to enable USB charging in time.') |
1938 self.RunShellCommand(disable_command) | 1943 self.RunShellCommand(disable_command) |
1939 if not self.IsDeviceCharging(): | 1944 if not self.IsDeviceCharging(): |
1940 break | 1945 break |
1941 | 1946 |
1942 def EnableUsbCharging(self): | 1947 def EnableUsbCharging(self, timeout=10): |
1943 command = self._GetControlUsbChargingCommand() | 1948 command = self._GetControlUsbChargingCommand() |
1944 if not command: | 1949 if not command: |
1945 raise Exception('Unable to act on usb charging.') | 1950 raise Exception('Unable to act on usb charging.') |
1946 disable_command = command['enable_command'] | 1951 disable_command = command['enable_command'] |
| 1952 t0 = time.time() |
1947 # Do not loop directly on self.IsDeviceCharging to cut the number of calls | 1953 # Do not loop directly on self.IsDeviceCharging to cut the number of calls |
1948 # to the device. | 1954 # to the device. |
1949 while True: | 1955 while True: |
| 1956 if t0 + timeout - time.time() < 0: |
| 1957 raise pexpect.TIMEOUT('Unable to enable USB charging in time.') |
1950 self.RunShellCommand(disable_command) | 1958 self.RunShellCommand(disable_command) |
1951 if self.IsDeviceCharging(): | 1959 if self.IsDeviceCharging(): |
1952 break | 1960 break |
1953 | 1961 |
1954 def IsDeviceCharging(self): | 1962 def IsDeviceCharging(self): |
1955 for line in self.RunShellCommand('dumpsys battery'): | 1963 for line in self.RunShellCommand('dumpsys battery'): |
1956 if 'powered: ' in line: | 1964 if 'powered: ' in line: |
1957 if line.split('powered: ')[1] == 'true': | 1965 if line.split('powered: ')[1] == 'true': |
1958 return True | 1966 return True |
1959 | 1967 |
1960 | 1968 |
1961 class NewLineNormalizer(object): | 1969 class NewLineNormalizer(object): |
1962 """A file-like object to normalize EOLs to '\n'. | 1970 """A file-like object to normalize EOLs to '\n'. |
1963 | 1971 |
1964 Pexpect runs adb within a pseudo-tty device (see | 1972 Pexpect runs adb within a pseudo-tty device (see |
1965 http://www.noah.org/wiki/pexpect), so any '\n' printed by adb is written | 1973 http://www.noah.org/wiki/pexpect), so any '\n' printed by adb is written |
1966 as '\r\n' to the logfile. Since adb already uses '\r\n' to terminate | 1974 as '\r\n' to the logfile. Since adb already uses '\r\n' to terminate |
1967 lines, the log ends up having '\r\r\n' at the end of each line. This | 1975 lines, the log ends up having '\r\r\n' at the end of each line. This |
1968 filter replaces the above with a single '\n' in the data stream. | 1976 filter replaces the above with a single '\n' in the data stream. |
1969 """ | 1977 """ |
1970 def __init__(self, output): | 1978 def __init__(self, output): |
1971 self._output = output | 1979 self._output = output |
1972 | 1980 |
1973 def write(self, data): | 1981 def write(self, data): |
1974 data = data.replace('\r\r\n', '\n') | 1982 data = data.replace('\r\r\n', '\n') |
1975 self._output.write(data) | 1983 self._output.write(data) |
1976 | 1984 |
1977 def flush(self): | 1985 def flush(self): |
1978 self._output.flush() | 1986 self._output.flush() |
OLD | NEW |