2727 execute_adb_command2 ,
2828 execute_adb_shell_command ,
2929 execute_adb_shell_command2 ,
30+ execute_adb_shell_command3 ,
3031 execute_file_related_adb_shell_command ,
3132 get_adb_shell_property ,
3233 get_device_android_api_version ,
4950 execute_adb_command2 ,
5051 execute_adb_shell_command ,
5152 execute_adb_shell_command2 ,
53+ execute_adb_shell_command3 ,
5254 execute_file_related_adb_shell_command ,
5355 get_adb_shell_property ,
5456 get_device_android_api_version ,
@@ -432,13 +434,12 @@ def handle_list_devices() -> None:
432434
433435def _get_device_serials () -> list [str ]:
434436 cmd = "devices -l"
435- return_code , stdout , stderr = execute_adb_command2 (cmd )
436- if return_code != 0 :
437- print_error_and_exit (f"Failed to execute command { cmd } , error: { stderr } " )
437+ result = execute_adb_command2 (cmd )
438+ assert result .return_code == 0 , f"Failed to execute command { cmd } , error: { result .stderr } "
438439
439440 device_serials = []
440441 # Skip the first line, it says "List of devices attached"
441- device_infos = stdout .split ("\n " )[1 :]
442+ device_infos = result . stdout .split ("\n " )[1 :]
442443
443444 if len (device_infos ) == 0 or (
444445 len (device_infos ) == 1 and len (device_infos [0 ]) == 0 ):
@@ -522,18 +523,16 @@ def dump_ui(xml_file: str) -> None:
522523 cmd3 = f"rm { tmp_file } "
523524
524525 print_verbose (f"Writing UI to { tmp_file } " )
525- return_code , _ , stderr = execute_adb_shell_command2 (cmd1 )
526- if return_code != 0 :
527- print_error_and_exit (f'Failed to execute "{ cmd1 } ", stderr: "{ stderr } "' )
526+ result = execute_adb_shell_command3 (cmd1 )
527+ assert result .return_code == 0 , f"Failed to execute command { cmd1 } , error: { result .stderr } "
528528
529529 print_verbose (f"Pulling file { xml_file } " )
530- return_code , _ , stderr = execute_adb_command2 (cmd2 )
530+ result = execute_adb_command2 (cmd2 )
531+ assert result .return_code == 0 , f"Failed to execute command { cmd2 } , error: { result .stderr } "
531532 print_verbose (f"Deleting file { tmp_file } " )
532- execute_adb_shell_command2 (cmd3 )
533- if return_code != 0 :
534- print_error_and_exit (f"Failed to fetch file { tmp_file } " )
535- else :
536- print_message (f'XML UI dumped to { xml_file } , you might want to format it using "xmllint --format { xml_file } "' )
533+ result = execute_adb_shell_command3 (cmd3 )
534+ assert result .return_code == 0 , f"Failed to execute command { cmd3 } , error: { result .stderr } "
535+ print_message (f'XML UI dumped to { xml_file } , you might want to format it using "xmllint --format { xml_file } "' )
537536
538537
539538@ensure_package_exists
@@ -611,14 +610,14 @@ def force_rtl(*, turn_on: bool) -> None:
611610def dump_screenshot (filepath : str ) -> None :
612611 screenshot_file_path_on_device = _create_tmp_file ("screenshot" , "png" )
613612 dump_cmd = f"screencap -p { screenshot_file_path_on_device } "
614- return_code , stdout , stderr = execute_adb_shell_command2 (dump_cmd )
615- if return_code != 0 :
616- print_error_and_exit (
617- f"Failed to capture the screenshot: (stdout: { stdout } , stderr: { stderr } )" )
613+ result = execute_adb_shell_command3 (dump_cmd )
614+ assert result .return_code == 0 , f"Failed to capture screenshot, error: { result .stderr } "
618615 pull_cmd = f"pull { screenshot_file_path_on_device } { filepath } "
619- execute_adb_command2 (pull_cmd )
616+ result = execute_adb_command2 (pull_cmd )
617+ assert result .return_code == 0 , f"Failed to pull screenshot file, error: { result .stderr } "
620618 del_cmd = f"rm { screenshot_file_path_on_device } "
621- execute_adb_shell_command2 (del_cmd )
619+ result = execute_adb_shell_command3 (del_cmd )
620+ assert result .return_code == 0 , f"Failed to delete screenshot file from device, error: { result .stderr } "
622621
623622
624623def dump_screenrecord (filepath : str ) -> None :
@@ -632,12 +631,10 @@ def dump_screenrecord(filepath: str) -> None:
632631
633632 original_sigint_handler = None
634633
635- def _start_recording () -> str :
634+ def _start_recording (screen_record_file_path : str ) -> None :
636635 print_message ("Recording video, press Ctrl+C to end..." )
637- tmp_file_path = _create_tmp_file ("screenrecord" , "mp4" )
638- dump_cmd = f"screenrecord --verbose { tmp_file_path } "
636+ dump_cmd = f"screenrecord --verbose { screen_record_file_path } "
639637 execute_adb_shell_command2 (dump_cmd )
640- return tmp_file_path
641638
642639 def _pull_and_delete_file_from_device (screen_record_file_path : str ) -> None :
643640 print_message (f"Saving recording to { filepath } " )
@@ -674,7 +671,8 @@ def signal_handler(_sig: int, _frame: Any) -> None:
674671 original_sigint_handler = signal .getsignal (signal .SIGINT )
675672 signal .signal (signal .SIGINT , signal_handler )
676673
677- screen_record_file_path_on_device = _start_recording ()
674+ screen_record_file_path_on_device = _create_tmp_file ("screenrecord" , "mp4" )
675+ _start_recording (screen_record_file_path_on_device )
678676
679677
680678def get_mobile_data_saver_state () -> str :
@@ -1388,9 +1386,9 @@ def push_file(local_file_path: str, remote_file_path: str) -> None:
13881386 cp_cmd = f"cp { tmp_file } { remote_file_path } "
13891387 rm_cmd = f"rm { tmp_file } "
13901388
1391- return_code , _ , stderr = execute_adb_command2 (push_cmd )
1392- if return_code != 0 :
1393- print_error_and_exit (f"Failed to push file, error: { stderr } " )
1389+ result = execute_adb_command2 (push_cmd )
1390+ if result . return_code != 0 :
1391+ print_error_and_exit (f"Failed to push file, error: { result . stderr } " )
13941392 return
13951393
13961394 execute_file_related_adb_shell_command (cp_cmd , remote_file_path )
@@ -1590,9 +1588,9 @@ def print_app_signature(app_name: str) -> None:
15901588 with tempfile .NamedTemporaryFile (prefix = app_name , suffix = ".apk" ) as tmp_apk_file :
15911589 tmp_apk_file_name = tmp_apk_file .name
15921590 adb_cmd = f"pull { apk_path } { tmp_apk_file_name } "
1593- return_code , _ , stderr = execute_adb_command2 (adb_cmd )
1594- if return_code != 0 :
1595- print_error_and_exit (f"Failed to pull file { apk_path } , stderr: { stderr } " )
1591+ result = execute_adb_command2 (adb_cmd )
1592+ if result . return_code != 0 :
1593+ print_error_and_exit (f"Failed to pull file { apk_path } , stderr: { result . stderr } " )
15961594 return
15971595
15981596 dir_of_this_script = os .path .split (__file__ )[0 ]
@@ -1669,9 +1667,9 @@ def backup_func() -> None:
16691667def perform_install (file_path : str ) -> None :
16701668 print_verbose (f"Installing { file_path } " )
16711669 # -r: replace existing application
1672- return_code , _ , stderr = execute_adb_command2 (f"install -r { file_path } " )
1673- if return_code != 0 :
1674- print_error (f"Failed to install { file_path } , stderr: { stderr } " )
1670+ result = execute_adb_command2 (f"install -r { file_path } " )
1671+ if result . return_code != 0 :
1672+ print_error (f"Failed to install { file_path } , stderr: { result . stderr } " )
16751673
16761674
16771675@ensure_package_exists
@@ -1697,23 +1695,9 @@ def perform_uninstall(app_name: str, first_user: bool) -> None:
16971695 print_error (f"Failed to uninstall { app_name } , stderr: { stderr } " )
16981696
16991697
1700- def _get_window_size () -> tuple [int , int ]:
1701- adb_cmd = "shell wm size"
1702- _ , result , _ = execute_adb_command2 (adb_cmd )
1703-
1704- if result is None :
1705- return - 1 , - 1
1706-
1707- regex_data = re .search (r"size: ([0-9]+)x([0-9]+)" , result )
1708- if regex_data is None :
1709- return - 1 , - 1
1710-
1711- return int (regex_data .group (1 )), int (regex_data .group (2 ))
1712-
1713-
17141698def _perform_tap (x : int , y : int ) -> None :
17151699 adb_shell_cmd = f"input tap { x :d} { y :d} "
1716- execute_adb_shell_command2 (adb_shell_cmd )
1700+ execute_adb_shell_command3 (adb_shell_cmd )
17171701
17181702
17191703# Deprecated
@@ -1771,14 +1755,14 @@ def enable_wireless_debug() -> bool:
17711755
17721756 ip = matching [0 ]
17731757
1774- code , _ , stderr = execute_adb_command2 ("tcpip 5555" )
1775- if code != 0 :
1758+ result = execute_adb_command2 ("tcpip 5555" )
1759+ if result . return_code != 0 :
17761760 print_error_and_exit (f"Failed to switch device { ip } to wireless debug mode, "
1777- f"stderr: { stderr } " )
1761+ f"stderr: { result . stderr } " )
17781762
1779- code , _ , stderr = execute_adb_command2 (f"connect { ip } " )
1780- if code != 0 :
1781- print_error_and_exit (f"Cannot enable wireless debugging. Error: { stderr } " )
1763+ result = execute_adb_command2 (f"connect { ip } " )
1764+ if result . return_code != 0 :
1765+ print_error_and_exit (f"Cannot enable wireless debugging. Error: { result . stderr } " )
17821766 return False
17831767 print_message (f"Connected via IP now you can disconnect the cable\n IP: { ip } " )
17841768 return True
@@ -1805,9 +1789,9 @@ def disable_wireless_debug() -> None:
18051789 result = True
18061790
18071791 for ip in ip_list :
1808- code , _ , stderr = execute_adb_command2 (f"disconnect { ip } " )
1809- if code != 0 :
1810- print_error (f"Failed to disconnect { ip } : { stderr } " )
1792+ result = execute_adb_command2 (f"disconnect { ip } " )
1793+ if result . return_code != 0 :
1794+ print_error (f"Failed to disconnect { ip } : { result . stderr } " )
18111795 result = False
18121796 else :
18131797 print_message (f"Disconnected { ip } " )
@@ -2090,10 +2074,10 @@ def print_state_change_info(state_name: str, old_state: str | int | bool, new_st
20902074
20912075def print_display_size () -> None :
20922076 # Ref: https://developer.android.com/training/multiscreen/screendensities
2093- cmd = "shell wm density"
2094- return_code , stdout , stderr = execute_adb_command2 (cmd )
2095- assert return_code == 0 , f"Failed to get display size, stderr: { stderr } "
2096- display_size = re .search (r"Physical density: (\d+)" , stdout )
2077+ cmd = "wm density"
2078+ result = execute_adb_shell_command3 (cmd )
2079+ assert result . return_code == 0 , f"Failed to get display size, stderr: { result . stderr } "
2080+ display_size = re .search (r"Physical density: (\d+)" , result . stdout )
20972081 if display_size is None :
20982082 print_error_and_exit ("Failed to get display size" )
20992083
0 commit comments