@@ -214,10 +214,7 @@ def get_current_rotation_direction() -> int:
214214
215215
216216def handle_layout (value : bool ):
217- if value :
218- cmd = "setprop debug.layout true"
219- else :
220- cmd = "setprop debug.layout false"
217+ cmd = "setprop debug.layout true" if value else "setprop debug.layout false"
221218 execute_adb_shell_command_and_poke_activity_service (cmd )
222219
223220
@@ -306,10 +303,7 @@ def get_battery_saver_state() -> str:
306303@partial (print_state_change_decorator , title = "Battery saver" , get_state_func = get_battery_saver_state )
307304def handle_battery_saver (turn_on : bool ):
308305 _error_if_min_version_less_than (19 )
309- if turn_on :
310- cmd = "put global low_power 1"
311- else :
312- cmd = "put global low_power 0"
306+ cmd = "put global low_power 1" if turn_on else "put global low_power 0"
313307
314308 if turn_on :
315309 return_code , _ , _ = execute_adb_shell_command2 (get_battery_unplug_cmd ())
@@ -435,7 +429,7 @@ def handle_list_devices():
435429 _print_device_info (device_serial )
436430
437431
438- def _get_device_serials () -> [str ]:
432+ def _get_device_serials () -> list [str ]:
439433 cmd = "devices -l"
440434 return_code , stdout , stderr = execute_adb_command2 (cmd )
441435 if return_code != 0 :
@@ -475,16 +469,17 @@ def _print_device_info(device_serial=None):
475469 model = get_adb_shell_property ("ro.product.model" , device_serial = device_serial )
476470 # This worked on 4.4.3 API 19 Moto E
477471 display_name = get_adb_shell_property ("ro.product.display" , device_serial = device_serial )
472+
478473 # First fallback: undocumented
479- if not display_name or display_name == "null" :
474+ if ((not display_name or display_name == "null" )
475+ and get_device_android_api_version (device_serial = device_serial ) >= 19 ):
480476 # This works on 4.4.4 API 19 Galaxy Grand Prime
481- if get_device_android_api_version ( device_serial = device_serial ) >= 19 :
482- display_name = execute_adb_shell_settings_command ( "get system device_name" , device_serial = device_serial )
477+ display_name = execute_adb_shell_settings_command ( "get system device_name" , device_serial = device_serial )
478+
483479 # Second fallback, documented to work on API 25 and above
484480 # Source: https://developer.android.com/reference/android/provider/Settings.Global.html#DEVICE_NAME
485- if not display_name or display_name == "null" :
486- if get_device_android_api_version (device_serial = device_serial ) >= 25 :
487- display_name = execute_adb_shell_settings_command ("get global device_name" , device_serial = device_serial )
481+ if (not display_name or display_name == "null" ) and get_device_android_api_version (device_serial = device_serial ) >= 25 :
482+ display_name = execute_adb_shell_settings_command ("get global device_name" , device_serial = device_serial )
488483
489484 # ABI info
490485 abi = get_adb_shell_property ("ro.product.cpu.abi" , device_serial = device_serial )
@@ -595,10 +590,7 @@ def get_wifi_state() -> str:
595590
596591@partial (print_state_change_decorator , title = "Wi-Fi" , get_state_func = get_wifi_state )
597592def set_wifi (turn_on : bool ):
598- if turn_on :
599- cmd = "svc wifi enable"
600- else :
601- cmd = "svc wifi disable"
593+ cmd = "svc wifi enable" if turn_on else "svc wifi disable"
602594 return_code , _ , _ = execute_adb_shell_command2 (cmd )
603595 if return_code != 0 :
604596 print_error_and_exit ("Failed to change Wi-Fi setting" )
@@ -608,21 +600,15 @@ def set_wifi(turn_on: bool):
608600# https://stackoverflow.com/questions/26539445/the-setmobiledataenabled-method-is-no-longer-callable-as-of-android-l-and-later
609601@partial (print_state_change_decorator , title = "Mobile data" , get_state_func = get_mobile_data_state )
610602def handle_mobile_data (turn_on : bool ):
611- if turn_on :
612- cmd = "svc data enable"
613- else :
614- cmd = "svc data disable"
603+ cmd = "svc data enable" if turn_on else "svc data disable"
615604 return_code , _ , _ = execute_adb_shell_command2 (cmd )
616605 if return_code != 0 :
617606 print_error_and_exit ("Failed to change mobile data setting" )
618607
619608
620609def force_rtl (turn_on : bool ):
621610 _error_if_min_version_less_than (19 )
622- if turn_on :
623- cmd = "put global debug.force_rtl 1"
624- else :
625- cmd = "put global debug.force_rtl 0"
611+ cmd = "put global debug.force_rtl 1" if turn_on else "put global debug.force_rtl 0"
626612 execute_adb_shell_settings_command_and_poke_activity_service (cmd )
627613
628614
@@ -710,10 +696,7 @@ def get_mobile_data_saver_state():
710696# https://developer.android.com/training/basics/network-ops/data-saver.html
711697@partial (print_state_change_decorator , title = "Mobile data saver" , get_state_func = get_mobile_data_saver_state )
712698def handle_mobile_data_saver (turn_on : bool ):
713- if turn_on :
714- cmd = "cmd netpolicy set restrict-background true"
715- else :
716- cmd = "cmd netpolicy set restrict-background false"
699+ cmd = "cmd netpolicy set restrict-background true" if turn_on else "cmd netpolicy set restrict-background false"
717700 return_code , _ , _ = execute_adb_shell_command2 (cmd )
718701 if return_code != 0 :
719702 print_error_and_exit ("Failed to modify data saver mode setting" )
@@ -759,10 +742,7 @@ def handle_dont_keep_activities_in_background(turn_on: bool):
759742
760743
761744def toggle_animations (turn_on : bool ):
762- if turn_on :
763- value = 1
764- else :
765- value = 0
745+ value = 1 if turn_on else 0
766746
767747 # Source: https://github.com/jaredsburrows/android-gif-example/blob/824c493285a2a2cf22f085662431cf0a7aa204b8/.travis.yml#L34
768748 cmd1 = "put global window_animation_scale %d" % value
@@ -791,10 +771,7 @@ def get_show_taps_state():
791771
792772@partial (print_state_change_decorator , title = "Show user taps" , get_state_func = get_show_taps_state )
793773def toggle_show_taps (turn_on : bool ):
794- if turn_on :
795- value = 1
796- else :
797- value = 0
774+ value = 1 if turn_on else 0
798775
799776 # Source: https://stackoverflow.com/a/32621809/434196
800777 cmd = "put system show_touches %d" % value
@@ -820,12 +797,8 @@ def get_stay_awake_while_charging_state():
820797 title = "Stay awake while charging" ,
821798 get_state_func = get_stay_awake_while_charging_state )
822799def stay_awake_while_charging (turn_on : bool ):
823- if turn_on :
824- # 1 for USB charging, 2 for AC charging, 4 for wireless charging. Or them together to get 7.
825- value = 7
826- else :
827- value = 0
828-
800+ # 1 for USB charging, 2 for AC charging, 4 for wireless charging. Add them together to get 7.
801+ value = 7 if turn_on else 0
829802 cmd1 = "put global stay_on_while_plugged_in %d" % value
830803 execute_adb_shell_settings_command_and_poke_activity_service (cmd1 )
831804
@@ -868,11 +841,7 @@ def list_permission_groups():
868841
869842def list_permissions (dangerous_only_permissions : bool ):
870843 # -g is to group permissions by permission groups.
871- if dangerous_only_permissions :
872- # -d => dangerous only permissions
873- cmd = "pm list permissions -g -d"
874- else :
875- cmd = "pm list permissions -g"
844+ cmd = "pm list permissions -g -d" if dangerous_only_permissions else "pm list permissions -g"
876845 return_code , stdout , stderr = execute_adb_shell_command2 (cmd )
877846 if return_code != 0 :
878847 print_error_and_exit ("Failed to list permissions: (stdout: %s, stderr: %s)" % (stdout , stderr ))
@@ -1488,10 +1457,7 @@ def print_app_info(app_name):
14881457def _get_permissions_info_below_api_23 (app_info_dump ):
14891458 install_time_permissions_regex = re .search (r"grantedPermissions:(.*)" , app_info_dump ,
14901459 re .IGNORECASE | re .DOTALL )
1491- if install_time_permissions_regex is None :
1492- install_time_permissions_string = []
1493- else :
1494- install_time_permissions_string = install_time_permissions_regex .group (1 ).split ("\n " )
1460+ install_time_permissions_string = [] if install_time_permissions_regex is None else install_time_permissions_regex .group (1 ).split ("\n " )
14951461
14961462 install_time_permissions_string = filter (None , install_time_permissions_string )
14971463 install_time_granted_permissions = list (install_time_permissions_string )
@@ -1912,15 +1878,9 @@ def print_notifications():
19121878 output_for_this_notification = output_for_this_notification .split (notification_records [i + 1 ])[0 ]
19131879 notification_package = re .findall (r"pkg=(\S*)" , notification_record )[0 ]
19141880 titles = re .findall (r"android.title=(.*)" , output_for_this_notification )
1915- if len (titles ) > 0 :
1916- notification_title = titles [0 ]
1917- else :
1918- notification_title = None
1881+ notification_title = titles [0 ] if len (titles ) > 0 else None
19191882 texts = re .findall (r"android.text=(.*)" , output_for_this_notification )
1920- if len (texts ) > 0 :
1921- notification_text = texts [0 ]
1922- else :
1923- notification_text = None
1883+ notification_text = texts [0 ] if len (texts ) > 0 else None
19241884 notification_actions = []
19251885 action_strings = re .findall (r"actions=\{(.*?)\n\}" , output_for_this_notification , re .MULTILINE | re .DOTALL )
19261886 if len (action_strings ) > 0 and (
@@ -2079,10 +2039,7 @@ def alarm_manager(param):
20792039
20802040def toggle_location (turn_on ):
20812041 _error_if_min_version_less_than (_MIN_API_FOR_LOCATION )
2082- if turn_on :
2083- cmd = "put secure location_mode 3"
2084- else :
2085- cmd = "put secure location_mode 0"
2042+ cmd = "put secure location_mode 3" if turn_on else "put secure location_mode 0"
20862043 execute_adb_shell_settings_command (cmd )
20872044
20882045
0 commit comments