blob: 28462efe3d1e67895dc3d4f6aaeb87f768a4435a [file] [log] [blame]
Karsten Tausche81ffea22018-06-19 15:34:01 +02001#!/usr/bin/env python3
2
3from android_adb_wrapper import *
4import argparse
5import sys
6from uiautomator import Device
7
8
9def set_wifi_state(dut, turn_on):
10 """Turn WiFi on or off.
11
Benjamin Copelande7debbe2021-02-22 09:31:14 +000012 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.
Karsten Tausche81ffea22018-06-19 15:34:01 +020014
Benjamin Copelande7debbe2021-02-22 09:31:14 +000015 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.
Karsten Tausche81ffea22018-06-19 15:34:01 +020020 """
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 Copeland15d743e2021-02-22 08:35:10 +000029 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 Tausche81ffea22018-06-19 15:34:01 +020031
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
60def 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 Copelande7debbe2021-02-22 09:31:14 +000079 if args.ACTION[0] != "set_wifi_state" or args.ACTION[1] not in (
80 "on",
81 "off",
82 ):
Karsten Tausche81ffea22018-06-19 15:34:01 +020083 print(
84 "ERROR: Specified ACTION is not supported: {}".format(args.ACTION),
85 file=sys.stderr,
86 )
87 sys.exit(1)
88
89 serials = args.SERIALS if args.SERIALS is not None else list_devices()
90
91 for serial in serials:
92 print("Configuring device {}…".format(serial))
93
94 dut = Device(serial)
95 # Work around the not-so-easy Device class
96 dut.serial = serial
97
98 try:
99 unlock(dut)
100
101 set_wifi_state(dut, args.ACTION[1] == "on")
102
103 except DeviceCommandError as e:
104 print("ERROR {}".format(e), file=sys.stderr)
105
106
107if __name__ == "__main__":
108 main()