def _StartEmulator()

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


  def _StartEmulator(self, timer,
                     net_type, new_process_group, window_scale,
                     with_audio, with_boot_anim,
                     loading_from_snapshot=False):
    """Start emulator or user mode android."""

    if not self.emulator_adb_port:
      self.emulator_adb_port = portpicker.PickUnusedPort()
    if not self.emulator_telnet_port:
      self.emulator_telnet_port = portpicker.PickUnusedPort()
    if not self.device_serial:
      self.device_serial = 'localhost:%s' % self.emulator_adb_port

    emulator_binary = os.path.abspath(
        self.android_platform.GetEmulator(
            self._metadata_pb.emulator_architecture,
            self._metadata_pb.emulator_type))

    services_dir = self._TempDir('emu_services')
    exec_dir = self._SessionImagesDir()
    self._emulator_env = self._MakeEmulatorEnv(os.environ, with_audio)

    if (self._metadata_pb.emulator_type in
        [emulator_meta_data_pb2.EmulatorMetaDataPb.QEMU,
         emulator_meta_data_pb2.EmulatorMetaDataPb.QEMU2]):
      self._PrepareQemuArgs(emulator_binary, net_type, window_scale,
                            with_audio, with_boot_anim)
    else:
      raise Exception('Not known emulator type %d' %
                      self._metadata_pb.emulator_type)

    logging.info('Executing: %s', self._emulator_start_args)
    timer.start(_SPAWN_EMULATOR)

    self._emulator_exec_dir = exec_dir
    self._sockets_dir = os.path.join(exec_dir, 'sockets')
    os.makedirs(self._sockets_dir)

    if self._use_waterfall:
      if not self._forward_bin:
        with contextlib.closing(
            resources.GetResourceAsFile(_FORWARDER_PATH)) as fwdr:
          path = os.path.join(services_dir, _FORWARD_BIN)
          with open(path, 'w+b') as o:
            shutil.copyfileobj(fwdr, o)
            os.chmod(path, stat.S_IRWXU)
            self._forward_bin = path
      if not self._ports_bin:
        with contextlib.closing(
            resources.GetResourceAsFile(_PORTS_PATH)) as ports:
          path = os.path.join(services_dir, _PORTS_BIN)
          with open(path, 'w+b') as o:
            shutil.copyfileobj(ports, o)
            os.chmod(path, stat.S_IRWXU)
            self._ports_bin = path
    else:
      with contextlib.closing(
          resources.GetResourceAsFile(
              'android_test_support/'
              'tools/android/emulator/daemon/x86/pipe_traversal')) as piper:
        with open(os.path.join(services_dir, 'pipe_traversal'), 'w+b') as o:
          shutil.copyfileobj(piper, o)
          os.chmod(os.path.join(services_dir, 'pipe_traversal'), stat.S_IRWXU)

    self._child_will_delete_tmp = self.delete_temp_on_exit
    logging.info('Launching emulator in: %s', exec_dir)

    # Initialize _EmulatorLogFile here by calling it in parent process.
    # Otherwise it would be initialized in child process then we can't get
    # its name.
    with self._EmulatorLogFile('r') as f:
      logging.info('Write emulator log to %s', f.name)

    self._emu_process_pid = self._ForkWatchdog(
        new_process_group, self._emulator_start_args, self._emulator_env,
        exec_dir, services_dir)

    timer.stop(_SPAWN_EMULATOR)

    self._PollEmulatorStatus(timer, loading_from_snapshot=loading_from_snapshot)
    if self._mini_boot:
      return

    # Update the dex2oat binary.
    api_version = self.GetApiVersion()


    self.ExecOnDevice(['setprop', 'qemu.host.socket.dir',
                       str(self._sockets_dir)])
    self.ExecOnDevice(['setprop', 'qemu.host.hostname', socket.gethostname()])

    # TODO: remove once waterfall is default and scuba is fixed
    # permanently
    waterfall_on = '1' if self._use_waterfall else '0'
    self.ExecOnDevice(['setprop', 'mdevx.waterfall', waterfall_on])
    if not loading_from_snapshot:
      # set screen off timeout to 30 minutes.
      self._SetDeviceSetting(self.GetApiVersion(), 'system',
                             'screen_off_timeout', '1800000')
      # disable lockscreen, this works on most api levels.
      if not self._direct_boot:
        self._SetDeviceSetting(self.GetApiVersion(), 'secure',
                               'lockscreen.disabled', '1')
      # disable software keyboard when hardware keyboard is there.
      self._SetDeviceSetting(self.GetApiVersion(), 'secure',
                             'show_ime_with_hard_keyboard', '0')

    if FLAGS.long_press_timeout:
      if self.GetApiVersion() == 10:
        logging.warn('long_press_timeout doesn\'t work on api 10.')
      else:
        self._SetDeviceSetting(self.GetApiVersion(), 'secure',
                               'long_press_timeout',
                               str(FLAGS.long_press_timeout))
      # fix possible stuck keyguardscrim window.
      self._DismissStuckKeyguardScrim()
      # ensure that processes that hang can write to /data/anr/traces.txt
      self.ExecOnDevice(['mkdir -p /data/anr && chmod -R 777 /data/anr'])
      logging.info(self.ExecOnDevice(['getprop']))