private synchronized void runEmulatorLaunchScript()

in tools/device_broker/java/com/google/android/apps/common/testing/broker/WrappedEmulatedDeviceBroker.java [436:558]


  private synchronized void runEmulatorLaunchScript(
      ScriptAction action,
      int adbServerPort,
      int emulatorAdbPort,
      int emulatorTelnetPort,
      String logcatFilePath,
      @Nullable List<LogcatFilter> logcatFilters,
      @Nullable String exportedMetadataPath,
      int vncServerPort) {

    List<String> allApksToInstall = apksToInstall;
    if (installTestServices) {
      allApksToInstall.addAll(testServicesApksToInstall);
    }
    List<String> command =
        Lists.newArrayList(
            emulatorLauncherPath,
            "--action=" + action.getCommandName(),
            "--adb_server_port=" + adbServerPort,
            "--adb_port=" + emulatorAdbPort,
            "--emulator_port=" + emulatorTelnetPort,
            "--logcat_path=" + logcatFilePath,
            "--apks=" + SPACE_SEP.join(allApksToInstall),
            "--system_apks=" + SPACE_SEP.join(systemApksToInstall),
            "--subprocess_log_dir=" + makeUnifiedLauncherLogDir(),
            "--nolaunch_in_seperate_session",
            "--start_vnc_on_port=" + vncServerPort,
            "--initial_locale=" + initialLocale,
            "--initial_ime=" + initialIME,
            "--kvm_device=" + kvmDevice,
            "--grant_runtime_permissions=" + grantRuntimePermissions);

    if (useWaterfall) {
      command.add("--use_h2o=" + useWaterfall);
      command.add("--adb_bin=" + adbPath);
    }

    if (logcatFilters != null && !logcatFilters.isEmpty()) {
      command.add("--logcat_filter='" + Joiner.on(" ").join(logcatFilters) + "'");
    }

    if (openGlDriver !=  OpenGlDriver.DEFAULT) {
      command.add("--open_gl_driver=" + openGlDriver.name().toLowerCase());
    }


    if (!isNullOrEmpty(simAccessRulesFile)) {
      command.add("--sim_access_rules_file=" + simAccessRulesFile);
    }

    command.add(enableDisplay ? "--enable_display" : "--noenable_display");
    command.add(enablePreverify ? "--preverify_apks" : "--nopreverify_apks");
    command.add(enableConsoleAuth ? "--enable_console_auth" : "--noenable_console_auth");
    command.add(enableGps ? "--enable_gps" : "--noenable_gps");

    if (null != exportedMetadataPath) {
      command.add("--export_launch_metadata_path=" + exportedMetadataPath);
    }

    command.add("--net_type=" + networkType.getType());

    if (!extraCerts.isEmpty()) {
      final String rootDir = environment.getRunfilesDir();

      List<String> absPathExtraCertsList =
          Lists.transform(
              extraCerts,
              new Function<String, String>() {
                @Override
                public String apply(String input) {
                  // The extra certs provided via data dependency in tests are provided under
                  // the "android_test_support" directory.
                  return isAbsolutePath(input) ? input : rootDir + "/android_test_support/" + input;
                  // */
                }
              });
      command.add("--extra_certs=" + COMMA_SEP.join(absPathExtraCertsList));
    }

    if (dataPartitionSize > 0) {
      command.add("--data_partition_size=" + dataPartitionSize);
    }

    if (longPressTimeout > 0) {
      command.add("--long_press_timeout=" + longPressTimeout);
    }

    if (numberOfCores.isPresent()) {
      command.add("--cores=" + numberOfCores.get());
    }

    logger.info("Running emulator launch script: " + command.toString());

    Map<String, String> env = newHashMap(environment.asMap());

    // This is probably unnecessary. Track removal in b/37347554
    if (env.containsKey("JAVA_RUNFILES") && !env.containsKey("DEVICE_RUNFILES")) {
      env.put("DEVICE_RUNFILES", env.get("JAVA_RUNFILES"));
    }

    try {
      // put the emulator's temp file in its own sandbox dir.
      File emulatorTemp = environment.createTempDir("launcher_tmp");
      env.put("TMPDIR", emulatorTemp.getPath());
    } catch (IOException ioe) {
      throw new RuntimeException(ioe);
    }

    LineProcessor<String> stderrProcessor = new LastLinesProcessor();
    int exitCode =
        communicatorBuilderProvider
            .get()
            .withArguments(command)
            .withTimeout(emulatorStartupTimeout, TimeUnit.SECONDS)
            .withEnvironment(env)
            .withStderrProcessor(stderrProcessor)
            .build()
            .communicate();

    checkState(0 == exitCode, "Bad exit code: %s. Command: %s.\n"
        + "=====Error begin:\n...\n%s\n=====Error end\n",
        exitCode, command, stderrProcessor.getResult());
  }