/**
   * Collect number of successful calls and failure reason
   *
   * @param listener
   */
  private int logOutputFile(ITestInvocationListener listener) throws DeviceNotAvailableException {
    File resFile = null;
    int calls = 0;
    resFile = mTestDevice.pullFile(mOutputFile);
    try {
      if (resFile == null) {
        // test failed without writing any results
        // either system crash, or other fails, treat as one failed iteration
        return 1;
      }
      BufferedReader br = new BufferedReader(new FileReader(resFile));
      String line = br.readLine();

      // The output file should only include one line
      if (line == null) {
        return 0;
      }

      // Get number of calls and failure reason;
      String[] res = line.split(" ");
      calls = Integer.parseInt(res[0]);
      int reason = Integer.parseInt(res[1]);
      callStatus[reason]++;
    } catch (IOException e) {
      CLog.e("IOException while reading outputfile %s", resFile.getAbsolutePath());
    }

    if (resFile != null) {
      resFile.delete();
    }
    return calls;
  }
  /**
   * Enable btsnoop logging by changing the BtSnoopLogOutput line in /etc/bluetooth/bt_stack.conf to
   * true.
   */
  private boolean enableBtsnoopLogging() throws DeviceNotAvailableException {
    File confFile = mTestDevice.pullFile(BTSNOOP_CONF_FILE);
    if (confFile == null) {
      return false;
    }

    BufferedReader confReader = null;
    try {
      confReader = new BufferedReader(new FileReader(confFile));
      StringBuilder newConf = new StringBuilder();
      String line;
      while ((line = confReader.readLine()) != null) {
        if (line.startsWith("BtSnoopLogOutput=")) {
          newConf.append("BtSnoopLogOutput=true\n");
        } else {
          newConf.append(line).append("\n");
        }
      }
      mTestDevice.executeAdbCommand("remount");
      return mTestDevice.pushString(newConf.toString(), BTSNOOP_CONF_FILE);
    } catch (IOException e) {
      return false;
    } finally {
      confFile.delete();
      StreamUtil.close(confReader);
    }
  }
  /**
   * Parses and logs the output file.
   *
   * @param listener the {@link ITestInvocationListener}
   * @throws DeviceNotAvailableException If the device is not available.
   */
  private void logOutputFile(ITestInvocationListener listener) throws DeviceNotAvailableException {
    File outputFile = null;
    InputStreamSource outputSource = null;

    try {
      outputFile = mTestDevice.pullFile(mAppOutputPath);
      if (outputFile != null) {
        outputSource = new SnapshotInputStreamSource(new FileInputStream(outputFile));
        listener.testLog(LAUNCH_TIME_NAME, LogDataType.TEXT, outputSource);
        parseOutputFile(
            StreamUtil.getStringFromStream(
                new BufferedInputStream(new FileInputStream(outputFile))));
      }
    } catch (IOException e) {
      CLog.e("Got IOException: %s", e);
    } finally {
      if (outputFile != null) {
        outputFile.delete();
      }
      if (outputSource != null) {
        outputSource.cancel();
      }
    }

    if (shouldTakeBugreport()) {
      InputStreamSource bugreport = mTestDevice.getBugreport();
      try {
        listener.testLog(BUGREPORT_NAME, LogDataType.TEXT, bugreport);
      } finally {
        bugreport.cancel();
      }
    }

    reportMetrics(listener);
  }