# 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 pathlib
import re
import subprocess
import sys
import threading
import time
import pexpect
from typing import List, Optional, Sequence, Tuple
from absl import logging
from android_env.components import errors
from android_env.proto import adb_pb2
      raise error

  def _execute_shell_command(
      self,
      args: List[str],
      timeout: Optional[float] = None,
      max_num_retries: int = 3) -> Optional[bytes]:
    """Execute shell command."""

    timeout = self._resolve_timeout(timeout)
    if not self._shell_is_ready:
      self._init_shell(timeout=timeout)
    shell_args = ' '.join(args)
    logging.info('Executing ADB shell command: %s', shell_args)

    num_tries = 0
    while num_tries < max_num_retries:
      num_tries += 1
      try:
        self._adb_shell.sendline(shell_args)
        self._adb_shell.expect(self._prompt, timeout=timeout)
        logging.info('Done executing ADB shell command: %s', shell_args)
        output = self._adb_shell.before.partition('\n'.encode('utf-8'))[2]
        return output
      except (pexpect.exceptions.EOF, pexpect.exceptions.TIMEOUT):
        logging.exception('Shell command failed. Reinitializing the shell.')
        logging.warning('self._adb_shell.before: %r', self._adb_shell.before)
        self._init_shell(timeout=timeout)

    logging.exception('Reinitializing the shell did not solve the issue.')

# 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/adb_controller.py
import os
import pathlib
import re
import subprocess
import sys
import threading
import time
import pexpect
from typing import List, Optional, Sequence, Tuple
from absl import logging
from android_env.components import errors
from android_env.proto import adb_pb2

      raise error

  def _execute_shell_command(
      self,
      args: List[str],
      timeout: Optional[float] = None,
      max_num_retries: int = 3) -> Optional[bytes]:
    """Execute shell command."""

    timeout = self._resolve_timeout(timeout)
    if not self._shell_is_ready:
      self._init_shell(timeout=timeout)
    shell_args = ' '.join(args)
    logging.info('Executing ADB shell command: %s', shell_args)

    num_tries = 0
    while num_tries < max_num_retries:
      num_tries += 1
      try:
        self._adb_shell.sendline(shell_args)
        self._adb_shell.expect(self._prompt, timeout=timeout)
        logging.info('Done executing ADB shell command: %s', shell_args)
        output = self._adb_shell.before.partition('\n'.encode('utf-8'))[2]
        return output
      except (pexpect.exceptions.EOF, pexpect.exceptions.TIMEOUT):
        logging.exception('Shell command failed. Reinitializing the shell.')
        logging.warning('self._adb_shell.before: %r', self._adb_shell.before)
        self._init_shell(timeout=timeout)

    logging.exception('Reinitializing the shell did not solve the issue.')

    raise errors.AdbControllerPexpectError()