def _PollEmulatorStatus()

in tools/android/emulator/emulated_device.py [0:0]


  def _PollEmulatorStatus(self, timer=None, loading_from_snapshot=False):
    """Blocks until the emulator is fully launched.

    Args:
      timer: stopwatch to measure how long each check takes
      loading_from_snapshot: Is the emulator loaded from a snapshot

    Raises:
      Exception: if the emulator dies or doesn't become lively in a reasonable
      timeframe.
    """
    if not timer:
      timer = stopwatch.StopWatch()

    fully_booted = False
    system_server_running = False
    pm_running = False
    adb_listening = False
    adb_connected = False
    sd_card_mounted = False
    external_storage = None
    boot_complete_present = False
    launcher_started = False
    dpi_ok = False
    pipe_traversal_ready = False
    waterfall_listening = False
    logcat_enabled = False

    interval = self._connect_poll_interval
    max_attempts = self._connect_max_attempts

    attempter = Attempter(timer, interval)

    while not fully_booted:
      if (attempter.total_attempts > max_attempts or
          time.time() > self._time_out_time):
        self._reporter.ReportFailure(
            'tools.android.emulator.boot.DeviceNotReady', {
                'attempts': attempter.total_attempts,
                'start_time': self._start_time,
                'time_out_time': self._time_out_time,
                'system_server_running': system_server_running,
                'pm_running': pm_running,
                'adb_listening': adb_listening,
                'adb_connected': adb_connected,
                'sd_card_mounted': sd_card_mounted,
                'external_storage': external_storage,
                'boot_complete_present': boot_complete_present,
                'launcher_started': launcher_started,
                'dpi_ok': dpi_ok,
            })

        self._ShowEmulatorLog()
        if adb_listening:
          log = self.ExecOnDevice(['logcat', '-v', 'threadtime', '-d'])
          logging.info('Android logcat below ' + '=' * 50 + '\n%s', log)
          logging.info('Android logcat end ' + '=' * 50)
        self._reporter.ReportFailure(
            'tools.android.emulator.adb.AdbNotListening',
            {'attempts': attempter.total_attempts})
        raise Exception('Haven\'t been able to connect to device after %s'
                        ' attempts.' % attempter.total_attempts)

      logging.info('system: %s pm: %s adb: %s sdcard: %s '
                   'boot_complete: %s launcher: %s pipes: %s '
                   'current step attempts: %s total attempts: %s',
                   system_server_running,
                   pm_running,
                   adb_listening,
                   sd_card_mounted,
                   boot_complete_present,
                   launcher_started,
                   pipe_traversal_ready,
                   attempter.step_attempts,
                   attempter.total_attempts)
      self._EnsureEmuRunning()

      if not adb_listening:
        adb_listening = attempter.AttemptStep(self._AdbListeningStep,
                                              'Checking if adb is listening.',
                                              _ADB_LISTENING_CHECK,
                                              _ADB_LISTENING_CHECK_FAIL_SLEEP)
        if not adb_listening:
          continue

      if not adb_connected and self._use_real_adb:
        if not self.ConnectDevice():
          raise Exception('Unable to connect to adbd')
        wait_args = [self.android_platform.real_adb, '-s',
                     'localhost:%s' % self.emulator_adb_port, 'wait-for-device']
        common.SpawnAndWaitWithRetry(wait_args, retries=2, timeout_seconds=30,
                                     exec_env=self._AdbEnv())
        adb_connected = True

      if not pipe_traversal_ready:
        pipe_traversal_ready = attempter.AttemptStep(
            self._PipeTraversalRestoreStep,
            'Checking Pipe Traversal.',
            _PIPE_TRAVERSAL_CHECK,
            _PIPE_TRAVERSAL_CHECK_FAIL_SLEEP)
        if not pipe_traversal_ready:
          continue

      if not waterfall_listening and self._use_waterfall:
        waterfall_listening = attempter.AttemptStep(
            self._WaterfallListeningStep, 'Checking if waterfall is listening.',
            _WATERFALL_LISTENING_CHECK, _WATERFALL_LISTENING_CHECK_FAIL_SLEEP)
        if not waterfall_listening:
          continue

      if not logcat_enabled:
        self.EnableLogcat()
        logcat_enabled = True

        emu_type = self._metadata_pb.emulator_type
        self._reporter.ReportDeviceProperties(emu_type, self._Props())

      # If we are running in dex2oat mode, stop the device once
      # pipe_traversal is ready.
      if self._mini_boot:
        self.ExecOnDevice(['stop'])
        self._WaitUntilDataPartitionMounted()
        return

      self._DetectFSErrors()

      if not system_server_running:
        system_server_running = attempter.AttemptStep(
            self._CheckSystemServerProcess,
            'Checking System Server',
            _SYS_SERVER_CHECK,
            _SYS_SERVER_CHECK_FAIL_SLEEP)
        if not system_server_running:
          continue

      self._KillCrashedProcesses()

      if not pm_running:
        pm_running = attempter.AttemptStep(self._CheckPackageManagerRunning,
                                           'Checking package manager',
                                           _PM_CHECK,
                                           _PM_CHECK_FAIL_SLEEP)
        if not pm_running:
          continue

      if not sd_card_mounted:
        if not external_storage:
          external_storage = ('%s %s %s' % (
              self._GetEnvironmentVar('EMULATED_STORAGE_SOURCE'),
              self._GetEnvironmentVar('EXTERNAL_STORAGE'),
              self._GetEnvironmentVar('ANDROID_STORAGE'))).split()

        def _ExternalStorageReady():
          return external_storage and self._CheckMount(external_storage)
        sd_card_mounted = attempter.AttemptStep(_ExternalStorageReady,
                                                'Checking external storage',
                                                _SD_CARD_MOUNT_CHECK,
                                                _SD_CARD_MOUNT_CHECK_FAIL_SLEEP)
        if not sd_card_mounted:
          perc_steps_spent = float(
              attempter.step_attempts) / max_attempts
          perc_steps_spent *= 100
          if perc_steps_spent > 20:
            self._TransientDeath('SDCard mount issues. This is a transient KI.')
          continue

      if not boot_complete_present:
        boot_complete_present = attempter.AttemptStep(
            self._CheckBootComplete,
            'Checking for boot complete',
            _BOOT_COMPLETE_PRESENT,
            _BOOT_COMPLETE_FAIL_SLEEP)
        if not boot_complete_present:
          continue
        if not self._direct_boot:
          self._direct_boot = ('1' in
                               self.ExecOnDevice(['getprop', DIRECT_BOOT_PROP]))

      if not loading_from_snapshot:
        if not dpi_ok:
          if not attempter.step_attempts:
            if not self.IsInstalled(_BOOTSTRAP_PKG):
              self.InstallApk(
                  resources.GetResourceFilename(_BOOTSTRAP_PATH),
                  grant_runtime_permissions=True)

          if not self._direct_boot:
            self._UnlockScreen()
          dpi_ok = attempter.AttemptStep(self._CheckDpi,
                                         'Checking DPI',
                                         _CHECK_DPI,
                                         _CHECK_DPI_FAIL_SLEEP)
          if not dpi_ok:
            if attempter.step_attempts > 4:
              self._TransientDeath('Haven\'t been able to read  '
                                   'correct DPI values in '
                                   ' %s attempts.' % attempter.step_attempts)
            continue


      if not self._direct_boot and not launcher_started:
        if attempter.step_attempts > 0:
          self._UnlockScreen()

        if attempter.step_attempts > 2 and not self._kicked_launcher:
          # sometimes the handoff to start the launcher fails. doing
          # am start -a android.intent.action.MAIN \
          # -c android.intent.category.HOME can't hurt.
          self._KickLauncher()
        launcher_started = attempter.AttemptStep(self._CheckLauncherStarted,
                                                 'Checking launcher app.',
                                                 _LAUNCHER_STARTED,
                                                 _LAUNCHER_STARTED_FAIL_SLEEP)
        if not launcher_started:
          continue
      fully_booted = True

    self._running = True
    self._KillCrashedProcesses()