Skip to content

Commit b42cad0

Browse files
authored
chore: add var types in a lot of places (#299)
1 parent bfedfd8 commit b42cad0

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

adbe/adb_enhanced.py

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def get_battery_reset_cmd() -> str:
384384

385385

386386
@ensure_package_exists
387-
def handle_get_jank(app_name) -> None:
387+
def handle_get_jank(app_name: str) -> None:
388388
running = _is_app_running(app_name)
389389
if not running:
390390
# Jank information cannot be fetched unless the app is running
@@ -463,7 +463,7 @@ def _get_device_serials() -> list[str]:
463463
return device_serials
464464

465465

466-
def _print_device_info(device_serial=None) -> None:
466+
def _print_device_info(device_serial: str | None = None) -> None:
467467
manufacturer = get_adb_shell_property("ro.product.manufacturer", device_serial=device_serial)
468468
model = get_adb_shell_property("ro.product.model", device_serial=device_serial)
469469
# This worked on 4.4.3 API 19 Moto E
@@ -507,7 +507,7 @@ def _get_top_activity_data():
507507
if regex_result is None:
508508
continue
509509
app_name, activity_name = regex_result.group(1), regex_result.group(2)
510-
# If activity name is a short hand then complete it.
510+
# If activity name is a shorthand then complete it.
511511
if activity_name.startswith("."):
512512
activity_name = f"{app_name}{activity_name}"
513513
return app_name, activity_name
@@ -538,7 +538,7 @@ def dump_ui(xml_file) -> None:
538538

539539

540540
@ensure_package_exists
541-
def force_stop(app_name) -> None:
541+
def force_stop(app_name: str) -> None:
542542
cmd = f"am force-stop {app_name}"
543543
return_code, stdout, _ = execute_adb_shell_command2(cmd)
544544
if return_code != 0:
@@ -548,7 +548,7 @@ def force_stop(app_name) -> None:
548548

549549

550550
@ensure_package_exists
551-
def clear_disk_data(app_name) -> None:
551+
def clear_disk_data(app_name: str) -> None:
552552
cmd = f"pm clear {app_name}"
553553
return_code, _, _ = execute_adb_shell_command2(cmd)
554554
if return_code != 0:
@@ -1165,7 +1165,7 @@ def print_list_debug_apps() -> None:
11651165
print("\n".join(get_list_debug_apps()))
11661166

11671167

1168-
def _is_debug_package(app_name):
1168+
def _is_debug_package(app_name: str) -> tuple[str | None, bool]:
11691169
"""Return true if the application have the debug flag set to true else return false
11701170
:returns: None
11711171
WHERE
@@ -1208,11 +1208,11 @@ def print_allow_backup_apps() -> None:
12081208
print("\n".join(list_allow_backup_apps()))
12091209

12101210

1211-
def _is_allow_backup_package(app_name):
1211+
def _is_allow_backup_package(app_name: str):
12121212
return _package_contains_flag(app_name, _REGEX_BACKUP_ALLOWED)
12131213

12141214

1215-
def _package_contains_flag(app_name, flag_regex):
1215+
def _package_contains_flag(app_name: str, flag_regex: str) -> tuple[str | None, bool]:
12161216
pm_cmd = f"dumpsys package {app_name}"
12171217
grep_cmd = f"(grep -c -E '{flag_regex}' || true)"
12181218
app_info_dump = execute_adb_shell_command(pm_cmd, piped_into_cmd=grep_cmd)
@@ -1291,7 +1291,7 @@ def list_directory(file_path: str, *, long_format: bool, recursive: bool, includ
12911291
print_message(execute_file_related_adb_shell_command(cmd, file_path))
12921292

12931293

1294-
def delete_file(file_path, force, recursive) -> None:
1294+
def delete_file(file_path: str, force: bool, recursive: bool) -> None:
12951295
cmd_prefix = "rm"
12961296
if force:
12971297
cmd_prefix += " -f"
@@ -1304,7 +1304,7 @@ def delete_file(file_path, force, recursive) -> None:
13041304
# Limitation: This command will only do run-as for the src file so, if a file is being copied from pkg1 to pkg2
13051305
# on a non-rooted device with both pkg1 and pkg2 being debuggable, this will fail. This can be improved by
13061306
# first copying the file to /data/local/tmp but as of now, I don't think that's required.
1307-
def move_file(src_path, dest_path, force) -> None:
1307+
def move_file(src_path: str, dest_path: str, force: bool) -> None:
13081308
cmd_prefix = "mv"
13091309
if force:
13101310
cmd_prefix += "-f"
@@ -1326,7 +1326,7 @@ def move_file(src_path, dest_path, force) -> None:
13261326

13271327
# Copies from remote_file_path on Android to local_file_path on the disk
13281328
# local_file_path can be None
1329-
def pull_file(remote_file_path: str, local_file_path: str, *, copy_ancillary=False) -> None:
1329+
def pull_file(remote_file_path: str, local_file_path: str, *, copy_ancillary: bool = False) -> None:
13301330
if not _file_exists(remote_file_path):
13311331
print_error_and_exit(f"File {remote_file_path} does not exist")
13321332

@@ -1373,7 +1373,7 @@ def pull_file(remote_file_path: str, local_file_path: str, *, copy_ancillary=Fal
13731373

13741374
# Limitation: It seems that pushing to a directory on some versions of Android fail silently.
13751375
# It is safer to push to a full path containing the filename.
1376-
def push_file(local_file_path, remote_file_path) -> None:
1376+
def push_file(local_file_path: str, remote_file_path: str) -> None:
13771377
if not Path(local_file_path).exists():
13781378
print_error_and_exit(f"Local file {local_file_path} does not exist")
13791379
if Path(local_file_path).is_dir():
@@ -1396,7 +1396,7 @@ def push_file(local_file_path, remote_file_path) -> None:
13961396
execute_adb_shell_command(rm_cmd)
13971397

13981398

1399-
def cat_file(file_path) -> None:
1399+
def cat_file(file_path: str) -> None:
14001400
cmd_prefix = "cat"
14011401
cmd = f"{cmd_prefix} {file_path}"
14021402
cat_stdout = execute_file_related_adb_shell_command(cmd, file_path)
@@ -1407,13 +1407,13 @@ def cat_file(file_path) -> None:
14071407

14081408
# Source: https://stackoverflow.com/a/25398877
14091409
@ensure_package_exists
1410-
def launch_app(app_name) -> None:
1410+
def launch_app(app_name: str) -> None:
14111411
adb_shell_cmd = f"monkey -p {app_name} -c android.intent.category.LAUNCHER 1"
14121412
execute_adb_shell_command(adb_shell_cmd)
14131413

14141414

14151415
@ensure_package_exists
1416-
def stop_app(app_name) -> None:
1416+
def stop_app(app_name: str) -> None:
14171417
# Below API 21, stop does not kill app in the foreground.
14181418
# Above API 21, it seems it does.
14191419
if get_device_android_api_version() < 21:
@@ -1423,7 +1423,7 @@ def stop_app(app_name) -> None:
14231423
execute_adb_shell_command(adb_shell_cmd)
14241424

14251425

1426-
def _regex_extract(regex, data):
1426+
def _regex_extract(regex: str, data: str) -> str | None:
14271427
regex_object = re.search(regex, data, re.IGNORECASE)
14281428
if regex_object:
14291429
return regex_object.group(1)
@@ -1433,7 +1433,7 @@ def _regex_extract(regex, data):
14331433
# adb shell pm dump <app_name> produces about 1200 lines, mostly useless,
14341434
# compared to this.
14351435
@ensure_package_exists
1436-
def print_app_info(app_name) -> None:
1436+
def print_app_info(app_name: str) -> None:
14371437
app_info_dump = execute_adb_shell_command(f"dumpsys package {app_name}")
14381438
version_code = _regex_extract("versionCode=(\\d+)?", app_info_dump)
14391439
version_name = _regex_extract("versionName=([\\d.]+)?", app_info_dump)
@@ -1466,7 +1466,7 @@ def print_app_info(app_name) -> None:
14661466

14671467

14681468
# API < 23 have no runtime permissions
1469-
def _get_permissions_info_below_api_23(app_info_dump):
1469+
def _get_permissions_info_below_api_23(app_info_dump: str) -> str:
14701470
install_time_permissions_regex = re.search(r"grantedPermissions:(.*)", app_info_dump,
14711471
re.IGNORECASE | re.DOTALL)
14721472
install_time_permissions_string = [] if install_time_permissions_regex is None else install_time_permissions_regex.group(1).split("\n")
@@ -1482,7 +1482,7 @@ def _get_permissions_info_below_api_23(app_info_dump):
14821482

14831483

14841484
# API 23 and have runtime permissions
1485-
def _get_permissions_info_above_api_23(app_info_dump: str):
1485+
def _get_permissions_info_above_api_23(app_info_dump: str) -> str:
14861486
requested_permissions = _extract_requested_permissions_above_api_23(app_info_dump)
14871487

14881488
install_time_permissions_string = _extract_install_time_permissions_above_api_23(app_info_dump)
@@ -1569,21 +1569,21 @@ def _extract_install_time_permissions_above_api_23(app_info_dump: str) -> list[s
15691569
return list(filter(None, install_time_permissions_string))
15701570

15711571

1572-
def _get_apk_path(app_name):
1572+
def _get_apk_path(app_name: str) -> str:
15731573
adb_shell_cmd = f"pm path {app_name}"
15741574
result = execute_adb_shell_command(adb_shell_cmd)
15751575
return result.split(":", 2)[1]
15761576

15771577

15781578
@ensure_package_exists
1579-
def print_app_path(app_name) -> None:
1579+
def print_app_path(app_name: str) -> None:
15801580
apk_path = _get_apk_path(app_name)
15811581
print_verbose(f"Path for {app_name} is {apk_path}")
15821582
print_message(apk_path)
15831583

15841584

15851585
@ensure_package_exists
1586-
def print_app_signature(app_name) -> None:
1586+
def print_app_signature(app_name: str) -> None:
15871587
apk_path = _get_apk_path(app_name)
15881588
# Copy apk to a temp file on the disk
15891589
with tempfile.NamedTemporaryFile(prefix=app_name, suffix=".apk") as tmp_apk_file:
@@ -1612,7 +1612,7 @@ def print_app_signature(app_name) -> None:
16121612

16131613
# Uses abe.jar taken from https://sourceforge.net/projects/adbextractor/
16141614
@ensure_package_exists
1615-
def perform_app_backup(app_name, backup_tar_file) -> None:
1615+
def perform_app_backup(app_name: str, backup_tar_file: str) -> None:
16161616
# TODO: Add a check to ensure that the screen is unlocked
16171617
password = "00"
16181618
print_verbose("Performing backup to backup.ab file")
@@ -1667,7 +1667,7 @@ def backup_func() -> None:
16671667
ps2.communicate()
16681668

16691669

1670-
def perform_install(file_path) -> None:
1670+
def perform_install(file_path: str) -> None:
16711671
print_verbose(f"Installing {file_path}")
16721672
# -r: replace existing application
16731673
return_code, _, stderr = execute_adb_command2(f"install -r {file_path}")
@@ -1676,11 +1676,11 @@ def perform_install(file_path) -> None:
16761676

16771677

16781678
@ensure_package_exists
1679-
def perform_uninstall(app_name, first_user) -> None:
1679+
def perform_uninstall(app_name: str, first_user: bool) -> None:
16801680
print_verbose(f"Uninstalling {app_name}")
16811681
cmd = ""
16821682
if first_user:
1683-
# For system apps, that cannot uninstalled,
1683+
# For system apps, that cannot be uninstalled,
16841684
# this command uninstalls them for user 0 without doing a system uninstall
16851685
# since that would fail.
16861686
# https://www.xda-developers.com/uninstall-carrier-oem-bloatware-without-root-access/
@@ -1698,7 +1698,7 @@ def perform_uninstall(app_name, first_user) -> None:
16981698
print_error(f"Failed to uninstall {app_name}, stderr: {stderr}")
16991699

17001700

1701-
def _get_window_size():
1701+
def _get_window_size() -> tuple[int, int]:
17021702
adb_cmd = "shell wm size"
17031703
_, result, _ = execute_adb_command2(adb_cmd)
17041704

@@ -1712,39 +1712,39 @@ def _get_window_size():
17121712
return int(regex_data.group(1)), int(regex_data.group(2))
17131713

17141714

1715-
def _perform_tap(x, y) -> None:
1715+
def _perform_tap(x: int, y: int) -> None:
17161716
adb_shell_cmd = f"input tap {x:d} {y:d}"
17171717
execute_adb_shell_command2(adb_shell_cmd)
17181718

17191719

17201720
# Deprecated
1721-
def execute_adb_shell_settings_command(settings_cmd, device_serial=None):
1721+
def execute_adb_shell_settings_command(settings_cmd: str, device_serial: str | None = None) -> str:
17221722
_error_if_min_version_less_than(19, device_serial=device_serial)
17231723
return execute_adb_shell_command(f"settings {settings_cmd}", device_serial=device_serial)
17241724

17251725

1726-
def execute_adb_shell_settings_command2(settings_cmd, device_serial=None):
1726+
def execute_adb_shell_settings_command2(settings_cmd: str, device_serial: str | None = None) -> tuple[int, str | None, str]:
17271727
_error_if_min_version_less_than(19)
17281728
return execute_adb_shell_command2(f"settings {settings_cmd}", device_serial)
17291729

17301730

1731-
def execute_adb_shell_settings_command_and_poke_activity_service(settings_cmd):
1731+
def execute_adb_shell_settings_command_and_poke_activity_service(settings_cmd: str) -> str:
17321732
return_value = execute_adb_shell_settings_command(settings_cmd)
17331733
_poke_activity_service()
17341734
return return_value
17351735

17361736

1737-
def execute_adb_shell_command_and_poke_activity_service(adb_cmd):
1737+
def execute_adb_shell_command_and_poke_activity_service(adb_cmd: str) -> str:
17381738
return_value = execute_adb_shell_command(adb_cmd)
17391739
_poke_activity_service()
17401740
return return_value
17411741

17421742

1743-
def _poke_activity_service():
1743+
def _poke_activity_service() -> str:
17441744
return execute_adb_shell_command(get_update_activity_service_cmd())
17451745

17461746

1747-
def _error_if_min_version_less_than(min_acceptable_version, device_serial=None) -> None:
1747+
def _error_if_min_version_less_than(min_acceptable_version: int, device_serial: str | None = None) -> None:
17481748
api_version = get_device_android_api_version(device_serial)
17491749
if api_version < min_acceptable_version:
17501750
cmd = " ".join(sys.argv[1:])
@@ -1753,7 +1753,7 @@ def _error_if_min_version_less_than(min_acceptable_version, device_serial=None)
17531753
f' your device version is {api_version:d}')
17541754

17551755

1756-
def _is_emulator():
1756+
def _is_emulator() -> bool:
17571757
qemu = get_adb_shell_property("ro.kernel.qemu")
17581758
return qemu is not None and qemu.strip() == "1"
17591759

@@ -1817,7 +1817,7 @@ def disable_wireless_debug() -> None:
18171817
print_error_and_exit("")
18181818

18191819

1820-
def switch_screen(switch_type):
1820+
def switch_screen(switch_type: int) -> str | None:
18211821
if switch_type == SCREEN_TOGGLE:
18221822
c, o, e = toggle_screen()
18231823

@@ -1920,7 +1920,7 @@ class AlarmEnum(Enum):
19201920
ALL = "a"
19211921

19221922

1923-
def print_history_alarms(output_dump_alarm, padding) -> None:
1923+
def print_history_alarms(output_dump_alarm: str, padding: int) -> None:
19241924
print("App Alarm history")
19251925

19261926
pattern_pending_alarm = \
@@ -1944,7 +1944,7 @@ def print_history_alarms(output_dump_alarm, padding) -> None:
19441944
print(f"{padding * 2}history: {history}")
19451945

19461946

1947-
def print_top_alarms(output_dump_alarm, padding) -> None:
1947+
def print_top_alarms(output_dump_alarm: str, padding: int) -> None:
19481948
print("Top Alarms:")
19491949
pattern_top_alarm = re.compile(r"(?<=Top Alarms:\n).*?(?=Alarm Stats:)",
19501950
re.DOTALL)
@@ -1975,7 +1975,7 @@ def print_top_alarms(output_dump_alarm, padding) -> None:
19751975
print(f"{padding * 2}User ID: {uid}")
19761976

19771977

1978-
def print_pending_alarms(output_dump_alarm, padding) -> None:
1978+
def print_pending_alarms(output_dump_alarm: str, padding: int) -> None:
19791979
print("Pending Alarms:")
19801980
pattern_pending_alarm = \
19811981
re.compile(
@@ -2013,7 +2013,7 @@ def print_pending_alarms(output_dump_alarm, padding) -> None:
20132013
print(f"{padding * 2}Package: {info[5]}")
20142014

20152015

2016-
def alarm_manager(param) -> None:
2016+
def alarm_manager(param: AlarmEnum) -> None:
20172017
cmd = "dumpsys alarm"
20182018
api_version = get_device_android_api_version()
20192019
err_msg_api = "Your Android version (API 28 and bellow) does not support listing pending alarm"
@@ -2049,14 +2049,14 @@ def alarm_manager(param) -> None:
20492049
print_error(err_msg_api)
20502050

20512051

2052-
def toggle_location(turn_on) -> None:
2052+
def toggle_location(turn_on: bool) -> None:
20532053
_error_if_min_version_less_than(_MIN_API_FOR_LOCATION)
20542054
cmd = "put secure location_mode 3" if turn_on else "put secure location_mode 0"
20552055
execute_adb_shell_settings_command(cmd)
20562056

20572057

20582058
@ensure_package_exists
2059-
def set_debug_app(app_name, wait_for_debugger, persistent) -> None:
2059+
def set_debug_app(app_name: str, wait_for_debugger: bool, persistent: bool) -> None:
20602060
cmd = "am set-debug-app"
20612061
if wait_for_debugger:
20622062
cmd += " -w"
@@ -2073,7 +2073,7 @@ def clear_debug_app() -> None:
20732073

20742074
# This permissions group seems to have been removed in API 29 and beyond.
20752075
# https://github.com/ashishb/adb-enhanced/runs/1799363523?check_suite_focus=true
2076-
def is_permission_group_unavailable_after_api_29(permission_group):
2076+
def is_permission_group_unavailable_after_api_29(permission_group: str) -> bool:
20772077
return permission_group in {
20782078
"android.permission-group.CONTACTS",
20792079
"android.permission-group.MICROPHONE",
@@ -2082,7 +2082,7 @@ def is_permission_group_unavailable_after_api_29(permission_group):
20822082
}
20832083

20842084

2085-
def print_state_change_info(state_name, old_state, new_state) -> None:
2085+
def print_state_change_info(state_name: str, old_state: str | int | bool, new_state: str | int | bool) -> None:
20862086
if old_state != new_state:
20872087
print_message(f'"{state_name}" state changed from "{old_state}" -> "{new_state}"')
20882088
else:

0 commit comments

Comments
 (0)