# Path: android_env/components/errors.py
# class ReadObservationError(Exception):
# class ObservationDecodingError(ReadObservationError):
# class PipeTimedOutError(ReadObservationError):
# class CoordinatorError(Exception):
# class TooManyRestartsError(CoordinatorError):
# class NotAllowedError(Exception):
# class PlayerExitedActivityError(NotAllowedError):
# class PlayerExitedViewHierarchyError(NotAllowedError):
# class AdbControllerError(Exception):
# class AdbControllerShellInitError(AdbControllerError):
# class AdbControllerPexpectError(AdbControllerError):
# class AdbControllerDeviceTimeoutError(AdbControllerError):
# class AdbControllerConnectionError(AdbControllerError):
# class AdbControllerInputDeviceNotFoundError(AdbControllerError):
# class AdbControllerScreenPinningError(AdbControllerError):
# class SimulatorCrashError(Exception):
# class ConsoleConnectionError(Exception):
# class SendActionError(Exception):
# class StepCommandError(Exception):
# class WaitForAppScreenError(StepCommandError):
# class CheckInstallError(StepCommandError):
# class WaitForMessageError(StepCommandError):


import os
import subprocess
import time
import grpc
from typing import Optional
from absl import logging
from android_env.components import errors
from android_env.proto import emulator_controller_pb2
from android_env.proto import emulator_controller_pb2_grpc
from google.protobuf import empty_pb2
    logging.info('Emulator launch command: %s', ' '.join(command))

    # Prepare logfile.
    emulator_logfile = os.path.join(self._local_tmp_dir, 'emulator_output')
    self._emulator_output = open(emulator_logfile, 'wb')

    # Spawn the emulator process.
    self._emulator = subprocess.Popen(
        command,
        env=env_vars,
        stdout=self._emulator_output,
        stderr=self._emulator_output)

    self._emulator_stub = EmulatorLauncher.create_emulator_stub(self._grpc_port)

    # Wait for the emulator to boot.
    start_time = time.time()
    deadline = start_time + self._startup_wait_time_sec
    success = False
    while time.time() < deadline:
      emu_status = self._emulator_stub.getStatus(empty_pb2.Empty())
      logging.info('Waiting for emulator to start. Emulator uptime: %rms',
                   emu_status.uptime)
      if emu_status.booted:
        success = True
        break
      time.sleep(5.0)

    elapsed_time = time.time() - start_time
    if not success:

# Path: android_env/components/errors.py
# class ReadObservationError(Exception):
# class ObservationDecodingError(ReadObservationError):
# class PipeTimedOutError(ReadObservationError):
# class CoordinatorError(Exception):
# class TooManyRestartsError(CoordinatorError):
# class NotAllowedError(Exception):
# class PlayerExitedActivityError(NotAllowedError):
# class PlayerExitedViewHierarchyError(NotAllowedError):
# class AdbControllerError(Exception):
# class AdbControllerShellInitError(AdbControllerError):
# class AdbControllerPexpectError(AdbControllerError):
# class AdbControllerDeviceTimeoutError(AdbControllerError):
# class AdbControllerConnectionError(AdbControllerError):
# class AdbControllerInputDeviceNotFoundError(AdbControllerError):
# class AdbControllerScreenPinningError(AdbControllerError):
# class SimulatorCrashError(Exception):
# class ConsoleConnectionError(Exception):
# class SendActionError(Exception):
# class StepCommandError(Exception):
# class WaitForAppScreenError(StepCommandError):
# class CheckInstallError(StepCommandError):
# class WaitForMessageError(StepCommandError):


# Path: android_env/components/simulators/emulator/emulator_launcher.py
import os
import subprocess
import time
import grpc
from typing import Optional
from absl import logging
from android_env.components import errors
from android_env.proto import emulator_controller_pb2
from android_env.proto import emulator_controller_pb2_grpc
from google.protobuf import empty_pb2

    logging.info('Emulator launch command: %s', ' '.join(command))

    # Prepare logfile.
    emulator_logfile = os.path.join(self._local_tmp_dir, 'emulator_output')
    self._emulator_output = open(emulator_logfile, 'wb')

    # Spawn the emulator process.
    self._emulator = subprocess.Popen(
        command,
        env=env_vars,
        stdout=self._emulator_output,
        stderr=self._emulator_output)

    self._emulator_stub = EmulatorLauncher.create_emulator_stub(self._grpc_port)

    # Wait for the emulator to boot.
    start_time = time.time()
    deadline = start_time + self._startup_wait_time_sec
    success = False
    while time.time() < deadline:
      emu_status = self._emulator_stub.getStatus(empty_pb2.Empty())
      logging.info('Waiting for emulator to start. Emulator uptime: %rms',
                   emu_status.uptime)
      if emu_status.booted:
        success = True
        break
      time.sleep(5.0)

    elapsed_time = time.time() - start_time
    if not success:

      raise errors.SimulatorCrashError(