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 16 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 40 Raises: 41 RuntimeError: An error when running adb command. 42 """ 43 adb_command = f'adb -s {device_id} shell {command}' 44 output = subprocess.run(adb_command, capture_output=True, shell=True, 45 check=False) 46 if 'Exception occurred' in str(output): 47 raise RuntimeError(output) 48 49 50def start_its_test_activity(device_id): 51 """Starts ItsTestActivity, waking the device if necessary. 52 53 Args: 54 device_id: str; ID of the device. 55 """ 56 run(f'adb -s {device_id} shell input keyevent KEYCODE_WAKEUP') 57 run(f'adb -s {device_id} shell input keyevent KEYCODE_MENU') 58 run(f'adb -s {device_id} shell am start -n ' 59 f'{ITS_TEST_ACTIVITY} --activity-brought-to-front') 60