11#!/usr/bin/env python3
2-
2+ import dataclasses
33import os
44import re
55import secrets
2626 from adbe .adb_helper import (
2727 CommandResult ,
2828 execute_adb_command2 ,
29- execute_adb_shell_command ,
3029 execute_adb_shell_command2 ,
3130 execute_adb_shell_command3 ,
3231 execute_file_related_adb_shell_command ,
5049 from adb_helper import (
5150 CommandResult ,
5251 execute_adb_command2 ,
53- execute_adb_shell_command ,
5452 execute_adb_shell_command2 ,
5553 execute_adb_shell_command3 ,
5654 execute_file_related_adb_shell_command ,
@@ -142,7 +140,7 @@ def handle_gfx(value: Literal["on", "off", "lines"]) -> None:
142140 print_error_and_exit (f"Unexpected value for gfx { value } " )
143141 return
144142
145- execute_adb_shell_command_and_poke_activity_service (cmd )
143+ _execute_adb_shell_command_and_poke_activity_service (cmd )
146144
147145
148146# Source: https://github.com/dhelleberg/android-scripts/blob/master/src/devtools.groovy
@@ -172,7 +170,7 @@ def handle_overdraw(value: str) -> None:
172170 print_error_and_exit (f"Unexpected value for overdraw { value } " )
173171 return
174172
175- execute_adb_shell_command_and_poke_activity_service (cmd )
173+ _execute_adb_shell_command_and_poke_activity_service (cmd )
176174
177175
178176# Perform screen rotation. Accepts four direction types - left, right, portrait, and landscape.
@@ -222,7 +220,7 @@ def get_current_rotation_direction() -> int:
222220
223221def handle_layout (* , turn_on : bool ) -> None :
224222 cmd = "setprop debug.layout true" if turn_on else "setprop debug.layout false"
225- execute_adb_shell_command_and_poke_activity_service (cmd )
223+ _execute_adb_shell_command_and_poke_activity_service (cmd )
226224
227225
228226# Source: https://stackoverflow.com/questions/10506591/turning-airplane-mode-on-via-adb
@@ -491,15 +489,23 @@ def _print_device_info(device_serial: str | None = None) -> None:
491489 f"Serial ID: { device_serial } \n Manufacturer: { manufacturer } \n Model: { model } ({ display_name } )\n Release: { release } \n SDK version: { sdk } \n CPU: { abi } \n " )
492490
493491
492+ @dataclasses .dataclass
493+ class _TopActivityData :
494+ app_name : str
495+ activity_name : str
496+
497+
494498def print_top_activity () -> None :
495- app_name , activity_name = _get_top_activity_data ()
499+ activity_data = _get_top_activity_data ()
500+ app_name = activity_data .app_name
501+ activity_name = activity_data .activity_name
496502 if app_name :
497503 print_message (f"Application name: { app_name } " )
498504 if activity_name :
499505 print_message (f"Activity name: { activity_name } " )
500506
501507
502- def _get_top_activity_data () -> tuple [ None , None ] :
508+ def _get_top_activity_data () -> _TopActivityData | None :
503509 cmd = "dumpsys window windows"
504510 return_code , output , _ = execute_adb_shell_command2 (cmd )
505511 if return_code != 0 and not output :
@@ -512,10 +518,10 @@ def _get_top_activity_data() -> tuple[None, None]:
512518 # If activity name is a shorthand then complete it.
513519 if activity_name .startswith ("." ):
514520 activity_name = f"{ app_name } { activity_name } "
515- return app_name , activity_name
521+ return _TopActivityData ( app_name = app_name , activity_name = activity_name )
516522
517523 print_error ("Unable to extract activity name" )
518- return None , None
524+ return None
519525
520526
521527def dump_ui (xml_file : str ) -> None :
@@ -606,7 +612,7 @@ def handle_mobile_data(*, turn_on: bool) -> None:
606612def force_rtl (* , turn_on : bool ) -> None :
607613 _error_if_min_version_less_than (19 )
608614 cmd = "put global debug.force_rtl 1" if turn_on else "put global debug.force_rtl 0"
609- execute_adb_shell_settings_command_and_poke_activity_service (cmd )
615+ _execute_adb_shell_settings_command_and_poke_activity_service (cmd )
610616
611617
612618def dump_screenshot (filepath : str ) -> None :
@@ -734,7 +740,7 @@ def handle_dont_keep_activities_in_background(*, turn_on: bool) -> None:
734740 cmd1 = f"put global always_finish_activities { value } "
735741 cmd2 = "service call activity 43 i32 0"
736742 _execute_adb_shell_settings_command3 (cmd1 )
737- execute_adb_shell_command_and_poke_activity_service (cmd2 )
743+ _execute_adb_shell_command_and_poke_activity_service (cmd2 )
738744
739745
740746def toggle_animations (* , turn_on : bool ) -> None :
@@ -795,8 +801,8 @@ def get_stay_awake_while_charging_state() -> str:
795801def stay_awake_while_charging (* , turn_on : bool ) -> None :
796802 # 1 for USB charging, 2 for AC charging, 4 for wireless charging. Add them together to get 7.
797803 value = 7 if turn_on else 0
798- cmd1 = f"put global stay_on_while_plugged_in { value :d} "
799- execute_adb_shell_settings_command_and_poke_activity_service ( cmd1 )
804+ cmd = f"put global stay_on_while_plugged_in { value :d} "
805+ _execute_adb_shell_settings_command_and_poke_activity_service ( cmd )
800806
801807
802808def input_text (text : str ) -> None :
@@ -1002,7 +1008,7 @@ def get_permissions_in_permission_group(permission_group: str) -> list[str] | li
10021008def grant_or_revoke_runtime_permissions (package_name : str , action_type : Literal ["grant" , "revoke" ], permissions : list [str ]) -> None :
10031009 _error_if_min_version_less_than (23 )
10041010
1005- app_info_dump = execute_adb_shell_command (f"dumpsys package { package_name } " )
1011+ app_info_dump = execute_adb_shell_command3 (f"dumpsys package { package_name } " ). stdout
10061012 permissions_formatted_dump = _get_permissions_info_above_api_23 (app_info_dump ).split ("\n " )
10071013
10081014 if action_type == "grant" :
@@ -1022,7 +1028,7 @@ def grant_or_revoke_runtime_permissions(package_name: str, action_type: Literal[
10221028 _error_if_min_version_less_than (33 )
10231029 num_permissions_granted += 1
10241030 print_message (f"{ action_type } { permission } permission to { package_name } " )
1025- execute_adb_shell_command (base_cmd + " " + permission )
1031+ execute_adb_shell_command3 (base_cmd + " " + permission )
10261032 if num_permissions_granted == 0 :
10271033 print_error_and_exit (f"None of these permissions were granted to { package_name } : { permissions } " )
10281034
@@ -1216,7 +1222,7 @@ def _is_allow_backup_package(app_name: str) -> tuple[str | None, bool]:
12161222def _package_contains_flag (app_name : str , flag_regex : str ) -> tuple [str | None , bool ]:
12171223 pm_cmd = f"dumpsys package { app_name } "
12181224 grep_cmd = f"(grep -c -E '{ flag_regex } ' || true)"
1219- app_info_dump = execute_adb_shell_command (pm_cmd , piped_into_cmd = grep_cmd )
1225+ app_info_dump = execute_adb_shell_command3 (pm_cmd , piped_into_cmd = grep_cmd ). stdout
12201226 if app_info_dump is None or app_info_dump .strip () == "0" :
12211227 return app_name , False
12221228 try :
@@ -1243,7 +1249,7 @@ def _package_contains_flag(app_name: str, flag_regex: str) -> tuple[str | None,
12431249def get_standby_bucket (package_name : str ) -> None :
12441250 _error_if_min_version_less_than (28 )
12451251 cmd = f"am get-standby-bucket { package_name } "
1246- result = execute_adb_shell_command (cmd )
1252+ result = execute_adb_shell_command3 (cmd ). stdout
12471253 if result is None :
12481254 print_error_and_exit (_USER_PRINT_VALUE_UNKNOWN )
12491255 print_verbose (f'App standby bucket for "{ package_name } " is { _APP_STANDBY_BUCKETS .get (int (result ), _USER_PRINT_VALUE_UNKNOWN )} ' )
@@ -1254,7 +1260,7 @@ def get_standby_bucket(package_name: str) -> None:
12541260def set_standby_bucket (package_name : str , mode : str ) -> None :
12551261 _error_if_min_version_less_than (28 )
12561262 cmd = f"am set-standby-bucket { package_name } { mode } "
1257- result = execute_adb_shell_command (cmd )
1263+ result = execute_adb_shell_command3 (cmd ). stdout
12581264 if result is not None : # Expected
12591265 print_error_and_exit (result )
12601266
@@ -1277,7 +1283,7 @@ def calculate_standby_mode(args: dict[str, Any]) -> str:
12771283def apply_or_remove_background_restriction (package_name : str , * , set_restriction : bool ) -> None :
12781284 _error_if_min_version_less_than (28 )
12791285 appops_cmd = f"cmd appops set { package_name } RUN_ANY_IN_BACKGROUND { 'ignore' if set_restriction else 'allow' } "
1280- execute_adb_shell_command (appops_cmd )
1286+ execute_adb_shell_command3 (appops_cmd )
12811287
12821288
12831289def list_directory (file_path : str , * , long_format : bool , recursive : bool , include_hidden_files : bool ) -> None :
@@ -1348,7 +1354,7 @@ def pull_file(remote_file_path: str, local_file_path: str, *, copy_ancillary: bo
13481354 pull_cmd = f"pull { tmp_file } { local_file_path } "
13491355 execute_adb_command2 (pull_cmd )
13501356 del_cmd = f"rm -r { tmp_file } "
1351- execute_adb_shell_command (del_cmd )
1357+ execute_adb_shell_command3 (del_cmd )
13521358
13531359 if Path (local_file_path ).exists ():
13541360 print_message (
@@ -1394,7 +1400,7 @@ def push_file(local_file_path: str, remote_file_path: str) -> None:
13941400 return
13951401
13961402 execute_file_related_adb_shell_command (cp_cmd , remote_file_path )
1397- execute_adb_shell_command (rm_cmd )
1403+ execute_adb_shell_command3 (rm_cmd )
13981404
13991405
14001406def cat_file (file_path : str ) -> None :
@@ -1410,7 +1416,7 @@ def cat_file(file_path: str) -> None:
14101416@ensure_package_exists
14111417def launch_app (app_name : str ) -> None :
14121418 adb_shell_cmd = f"monkey -p { app_name } -c android.intent.category.LAUNCHER 1"
1413- execute_adb_shell_command (adb_shell_cmd )
1419+ execute_adb_shell_command3 (adb_shell_cmd )
14141420
14151421
14161422@ensure_package_exists
@@ -1421,7 +1427,7 @@ def stop_app(app_name: str) -> None:
14211427 force_stop (app_name )
14221428 else :
14231429 adb_shell_cmd = f"am kill { app_name } "
1424- execute_adb_shell_command (adb_shell_cmd )
1430+ execute_adb_shell_command3 (adb_shell_cmd )
14251431
14261432
14271433def _regex_extract (regex : str , data : str ) -> str | None :
@@ -1435,7 +1441,7 @@ def _regex_extract(regex: str, data: str) -> str | None:
14351441# compared to this.
14361442@ensure_package_exists
14371443def print_app_info (app_name : str ) -> None :
1438- app_info_dump = execute_adb_shell_command (f"dumpsys package { app_name } " )
1444+ app_info_dump = execute_adb_shell_command3 (f"dumpsys package { app_name } " ). stdout
14391445 version_code = _regex_extract ("versionCode=(\\ d+)?" , app_info_dump )
14401446 version_name = _regex_extract ("versionName=([\\ d.]+)?" , app_info_dump )
14411447 min_sdk_version = _regex_extract ("minSdk=(\\ d+)?" , app_info_dump )
@@ -1572,8 +1578,8 @@ def _extract_install_time_permissions_above_api_23(app_info_dump: str) -> list[s
15721578
15731579def _get_apk_path (app_name : str ) -> str :
15741580 adb_shell_cmd = f"pm path { app_name } "
1575- result = execute_adb_shell_command (adb_shell_cmd )
1576- return result .split (":" , 2 )[1 ]
1581+ result = execute_adb_shell_command3 (adb_shell_cmd )
1582+ return result .stdout . split (":" , 2 )[1 ]
15771583
15781584
15791585@ensure_package_exists
@@ -1713,20 +1719,20 @@ def _execute_adb_shell_settings_command3(settings_cmd: str, device_serial: str |
17131719 return execute_adb_shell_command3 (f"settings { settings_cmd } " , device_serial )
17141720
17151721
1716- def execute_adb_shell_settings_command_and_poke_activity_service (settings_cmd : str ) -> str :
1722+ def _execute_adb_shell_settings_command_and_poke_activity_service (settings_cmd : str ) -> str :
17171723 return_value = _execute_adb_shell_settings_command3 (settings_cmd ).stdout
17181724 _poke_activity_service ()
17191725 return return_value
17201726
17211727
1722- def execute_adb_shell_command_and_poke_activity_service (adb_cmd : str ) -> str :
1723- return_value = execute_adb_shell_command (adb_cmd )
1728+ def _execute_adb_shell_command_and_poke_activity_service (adb_cmd : str ) -> str :
1729+ result = execute_adb_shell_command3 (adb_cmd )
17241730 _poke_activity_service ()
1725- return return_value
1731+ return result . stdout
17261732
17271733
1728- def _poke_activity_service () -> str :
1729- return execute_adb_shell_command (get_update_activity_service_cmd ())
1734+ def _poke_activity_service () -> CommandResult :
1735+ return execute_adb_shell_command3 (get_update_activity_service_cmd ())
17301736
17311737
17321738def _error_if_min_version_less_than (min_acceptable_version : int , device_serial : str | None = None ) -> None :
0 commit comments