Karsten Tausche | 81ffea2 | 2018-06-19 15:34:01 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | from android_adb_wrapper import * |
| 4 | import argparse |
| 5 | import sys |
| 6 | from uiautomator import Device |
| 7 | |
| 8 | |
| 9 | def set_wifi_state(dut, turn_on): |
| 10 | """Turn WiFi on or off. |
| 11 | |
| 12 | This checks the current WiFi settings and turns it on or off. It does |
| 13 | nothing if the settings are already in the desired state. |
| 14 | |
| 15 | Parameters: |
| 16 | dut (Device): The device object. |
| 17 | enabled: Boolean, true for on, false for off |
| 18 | Raises: |
| 19 | DeviceCommandError: If the UI automation fails. |
| 20 | """ |
| 21 | # Open the Wi-Fi settings |
| 22 | adb( |
| 23 | "shell", |
| 24 | ("am start -a android.settings.WIFI_SETTINGS " "--activity-clear-task"), |
| 25 | serial=dut.serial, |
| 26 | ) |
| 27 | |
| 28 | # Check if there is an option to turn WiFi on or off |
Benjamin Copeland | 15d743e | 2021-02-22 08:35:10 +0000 | [diff] [blame^] | 29 | wifi_enabler = dut(text="OFF", resourceId="com.android.settings:id/switch_widget") |
| 30 | wifi_disabler = dut(text="ON", resourceId="com.android.settings:id/switch_widget") |
Karsten Tausche | 81ffea2 | 2018-06-19 15:34:01 +0200 | [diff] [blame] | 31 | |
| 32 | if not wifi_enabler.exists and not wifi_disabler.exists: |
| 33 | raise DeviceCommandError( |
| 34 | dut, |
| 35 | "UI: set Wi-Fi state", |
| 36 | "Neither switch for turning Wi-Fi on nor for turning it off are present.", |
| 37 | ) |
| 38 | if wifi_enabler.exists and wifi_disabler.exists: |
| 39 | raise DeviceCommandError( |
| 40 | dut, |
| 41 | "UI: set Wi-Fi state", |
| 42 | "Unexpected UI: Both, a switch for turning Wi-Fi on and for turning it off are present.", |
| 43 | ) |
| 44 | |
| 45 | if turn_on: |
| 46 | if wifi_enabler.exists: |
| 47 | wifi_enabler.click() |
| 48 | else: |
| 49 | print("Wi-Fi is already enabled.") |
| 50 | else: |
| 51 | if wifi_disabler.exists: |
| 52 | wifi_disabler.click() |
| 53 | else: |
| 54 | print("Wi-Fi is already disabled.") |
| 55 | |
| 56 | # Leave the settings |
| 57 | dut.press.back() |
| 58 | |
| 59 | |
| 60 | def main(): |
| 61 | parser = argparse.ArgumentParser() |
| 62 | parser.add_argument( |
| 63 | "-a", |
| 64 | dest="ACTION", |
| 65 | required=True, |
| 66 | nargs="+", |
| 67 | help="Action to perform. Following action is currently implemented: \ |
| 68 | set_wifi_state <on|off>", |
| 69 | ) |
| 70 | parser.add_argument( |
| 71 | "-s", |
| 72 | dest="SERIALS", |
| 73 | nargs="+", |
| 74 | help="Serial numbers of devices to configure. \ |
| 75 | If not present, all available devices will be configured.", |
| 76 | ) |
| 77 | args = parser.parse_args() |
| 78 | |
Benjamin Copeland | 15d743e | 2021-02-22 08:35:10 +0000 | [diff] [blame^] | 79 | if args.ACTION[0] != "set_wifi_state" or args.ACTION[1] not in ("on", "off",): |
Karsten Tausche | 81ffea2 | 2018-06-19 15:34:01 +0200 | [diff] [blame] | 80 | print( |
| 81 | "ERROR: Specified ACTION is not supported: {}".format(args.ACTION), |
| 82 | file=sys.stderr, |
| 83 | ) |
| 84 | sys.exit(1) |
| 85 | |
| 86 | serials = args.SERIALS if args.SERIALS is not None else list_devices() |
| 87 | |
| 88 | for serial in serials: |
| 89 | print("Configuring device {}…".format(serial)) |
| 90 | |
| 91 | dut = Device(serial) |
| 92 | # Work around the not-so-easy Device class |
| 93 | dut.serial = serial |
| 94 | |
| 95 | try: |
| 96 | unlock(dut) |
| 97 | |
| 98 | set_wifi_state(dut, args.ACTION[1] == "on") |
| 99 | |
| 100 | except DeviceCommandError as e: |
| 101 | print("ERROR {}".format(e), file=sys.stderr) |
| 102 | |
| 103 | |
| 104 | if __name__ == "__main__": |
| 105 | main() |