Skip to content

Commit afa5e9a

Browse files
committed
Refactor emulator start and stop functions for clarity and efficiency.
1 parent 85780c3 commit afa5e9a

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

tools/python/util/android/android.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
_log = get_logger("util.android")
1818

19-
SdkToolPaths = collections.namedtuple("SdkToolPaths",
20-
["emulator", "adb", "sdkmanager", "avdmanager"])
19+
20+
SdkToolPaths = collections.namedtuple("SdkToolPaths", ["emulator", "adb", "sdkmanager", "avdmanager"])
2121

2222

2323
def get_sdk_tool_paths(sdk_root: str):
@@ -34,18 +34,15 @@ def filename(name, windows_extension):
3434
emulator=str((sdk_root / "emulator" / filename("emulator", "exe")).resolve(strict=True)),
3535
adb=str((sdk_root / "platform-tools" / filename("adb", "exe")).resolve(strict=True)),
3636
sdkmanager=str(
37-
(sdk_root / "cmdline-tools" / "latest" / "bin" / filename("sdkmanager", "bat")).resolve(
38-
strict=True)
37+
(sdk_root / "cmdline-tools" / "latest" / "bin" / filename("sdkmanager", "bat")).resolve(strict=True)
3938
),
4039
avdmanager=str(
41-
(sdk_root / "cmdline-tools" / "latest" / "bin" / filename("avdmanager", "bat")).resolve(
42-
strict=True)
40+
(sdk_root / "cmdline-tools" / "latest" / "bin" / filename("avdmanager", "bat")).resolve(strict=True)
4341
),
4442
)
4543

4644

47-
def create_virtual_device(sdk_tool_paths: SdkToolPaths, system_image_package_name: str,
48-
avd_name: str):
45+
def create_virtual_device(sdk_tool_paths: SdkToolPaths, system_image_package_name: str, avd_name: str):
4946
run(sdk_tool_paths.sdkmanager, "--install", system_image_package_name, input=b"y")
5047

5148
run(
@@ -108,13 +105,11 @@ def _stop_process_with_pid(pid: int):
108105

109106

110107
def start_emulator(
111-
sdk_tool_paths: SdkToolPaths, avd_name: str,
112-
extra_args: typing.Optional[typing.Sequence[str]] = None
108+
sdk_tool_paths: SdkToolPaths, avd_name: str, extra_args: typing.Optional[typing.Sequence[str]] = None
113109
) -> subprocess.Popen:
114110
if is_emulator_running_by_avd(avd_name=avd_name):
115111
raise RuntimeError(
116112
f"An emulator with avd_name{avd_name} is already running. Please close it before starting a new one.")
117-
118113
with contextlib.ExitStack() as emulator_stack, contextlib.ExitStack() as waiter_stack:
119114
emulator_args = [
120115
sdk_tool_paths.emulator,
@@ -210,19 +205,16 @@ def start_emulator(
210205
elif datetime.datetime.now() > end_time:
211206
raise RuntimeError("Emulator startup timeout. sys.boot_completed was not set.")
212207

213-
_log.debug(
214-
f"sys.boot_completed='{getprop_value}'. Sleeping for {sleep_interval_seconds} before retrying.")
208+
_log.debug(f"sys.boot_completed='{getprop_value}'. Sleeping for {sleep_interval_seconds} before retrying.")
215209
time.sleep(sleep_interval_seconds)
216210
# Verify if the emulator is now running
217211
if not is_emulator_running_by_avd(avd_name=avd_name):
218212
raise RuntimeError("Emulator failed to start.")
219213
return emulator_process
220214

221-
222215
def is_emulator_running_by_avd(avd_name: str) -> bool:
223216
"""
224217
Check if an emulator is running based on the provided AVD name.
225-
226218
:param avd_name: Name of the Android Virtual Device (AVD) to check.
227219
:return: True if an emulator with the given AVD name is running, False otherwise.
228220
"""
@@ -270,7 +262,6 @@ def is_emulator_running_by_pid(emulator_pid: int) -> bool:
270262
def stop_emulator_by_proc(emulator_proc: subprocess.Popen, timeout: int = 120):
271263
"""
272264
Stops the emulator process using a subprocess.Popen instance.
273-
274265
:param emulator_proc: The emulator process as a subprocess.Popen instance.
275266
:param timeout: Maximum time (in seconds) to wait for the emulator to stop.
276267
"""
@@ -298,7 +289,6 @@ def stop_emulator_by_proc(emulator_proc: subprocess.Popen, timeout: int = 120):
298289
def stop_emulator_by_pid(emulator_pid: int, timeout: int = 120):
299290
"""
300291
Stops the emulator process using a PID.
301-
302292
:param emulator_pid: The emulator process PID.
303293
:param timeout: Maximum time (in seconds) to wait for the emulator to stop.
304294
"""
@@ -326,7 +316,6 @@ def stop_emulator_by_pid(emulator_pid: int, timeout: int = 120):
326316
def stop_emulator(emulator_proc_or_pid: typing.Union[subprocess.Popen, int], timeout: int = 120):
327317
"""
328318
Stops the emulator process, checking its running status before and after stopping.
329-
330319
:param emulator_proc_or_pid: The emulator process (subprocess.Popen) or PID (int).
331320
:param timeout: Maximum time (in seconds) to wait for the emulator to stop.
332321
"""

0 commit comments

Comments
 (0)