• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Utility functions to manage and interact with devices for ITS."""
15
16import logging
17import os
18import subprocess
19
20ITS_TEST_ACTIVITY = 'com.android.cts.verifier/.camera.its.ItsTestActivity'
21
22
23def run(cmd):
24  """Replacement for os.system, with hiding of stdout+stderr messages.
25
26  Args:
27    cmd: Command to be executed in string format.
28  """
29  with open(os.devnull, 'wb') as devnull:
30    subprocess.check_call(cmd.split(), stdout=devnull, stderr=subprocess.STDOUT)
31
32
33def run_adb_shell_command(device_id, command):
34  """Run adb shell command on device.
35
36  Args:
37    device_id: serial id of device.
38    command: adb command to run on device.
39  Returns:
40    output: adb command output
41  Raises:
42    RuntimeError: An error when running adb command.
43  """
44  adb_command = f'adb -s {device_id} shell {command}'
45  output = subprocess.run(adb_command, capture_output=True, shell=True,
46                          check=False)
47  if 'Exception occurred' in str(output):
48    raise RuntimeError(output)
49  return output
50
51
52def is_dut_tablet_or_desktop(device_id):
53  """Checks if the dut is tablet or desktop.
54
55  Args:
56    device_id: serial id of device under test
57  Returns:
58    True, if the device under test is a tablet.
59    False otherwise.
60  """
61  adb_command = 'getprop ro.build.characteristics'
62  output = run_adb_shell_command(device_id, adb_command)
63  logging.debug('adb command output: %s', output)
64  if output is not None and (
65      ('tablet' in str(output).lower()) or
66      ('desktop' in str(output).lower())
67  ):
68    logging.debug('Device under test is a tablet/desktop.')
69    return True
70  logging.debug('Device under test is a phone')
71  return False
72
73
74def start_its_test_activity(device_id):
75  """Starts ItsTestActivity, waking the device if necessary.
76
77  Args:
78    device_id: str; ID of the device.
79  """
80  run(f'adb -s {device_id} shell input keyevent KEYCODE_WAKEUP')
81  run(f'adb -s {device_id} shell input keyevent KEYCODE_MENU')
82  run(f'adb -s {device_id} shell am start -n '
83      f'{ITS_TEST_ACTIVITY} --activity-brought-to-front '
84      '--activity-reorder-to-front')
85