Skip to content

Commit

Permalink
Lazily initialize AdbCallParser in DeviceSettings.
Browse files Browse the repository at this point in the history
We only need `AdbCallParser` _after_ `DeviceSettings.__init__()` in `update()`
or `get_orientation()`, so we construct it lazily. This allows the simulator to
be launched after `DeviceSettings` is constructed.

PiperOrigin-RevId: 689057999
  • Loading branch information
kenjitoyama authored and copybara-github committed Oct 23, 2024
1 parent 6e73777 commit 746e0e9
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions android_env/components/device_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

"""Sets and gets some global settings on an Android device."""

from typing import Final
from unittest import mock

from absl import logging
from android_env.components import adb_call_parser
from android_env.components import config_classes
Expand All @@ -23,14 +26,24 @@
import numpy as np


# The internal `AdbCallParser` instance is lazily instantiated within
# `DeviceSettings`. If we make it optional (i.e. `| None`), pytype will think
# that it could be `None`, requiring either explicit runtime checks or escape
# hatches in every actual call, even if it's never actually `None` if reached
# via the public API.
# The trick here is to create this dummy instance of the right type that's used
# as a sentinel to indicate that it hasn't been initialized yet.
_PLACEHOLDER_ADB_CALL_PARSER: Final[adb_call_parser.AdbCallParser] = (
mock.create_autospec(adb_call_parser.AdbCallParser)
)


class DeviceSettings:
"""An abstraction for general properties and settings of an Android device."""

def __init__(self, simulator: base_simulator.BaseSimulator):
self._simulator = simulator
self._adb_call_parser = adb_call_parser.AdbCallParser(
adb_controller=self._simulator.create_adb_controller()
)
self._adb_call_parser = _PLACEHOLDER_ADB_CALL_PARSER

# The size of the device screen in pixels.
self._screen_width: int = 0
Expand All @@ -41,6 +54,11 @@ def __init__(self, simulator: base_simulator.BaseSimulator):
def update(self, config: config_classes.DeviceSettingsConfig) -> None:
"""Sets the configuration of the device according to `config`."""

if self._adb_call_parser is _PLACEHOLDER_ADB_CALL_PARSER:
self._adb_call_parser = adb_call_parser.AdbCallParser(
adb_controller=self._simulator.create_adb_controller()
)

self._update_screen_size()
self._set_show_touches(config.show_touches)
self._set_show_pointer_location(config.show_pointer_location)
Expand All @@ -61,6 +79,11 @@ def screen_height(self) -> int:
def get_orientation(self) -> np.ndarray:
"""Returns the device orientation. Please see specs.py for details."""

if self._adb_call_parser is _PLACEHOLDER_ADB_CALL_PARSER:
self._adb_call_parser = adb_call_parser.AdbCallParser(
adb_controller=self._simulator.create_adb_controller()
)

self._update_orientation()
return self._orientation

Expand Down

0 comments on commit 746e0e9

Please sign in to comment.