public boolean performCrashDetectingFlow() {
    final File newCrashFile = new File(mApp.getFilesDir(), NEW_CRASH_FILENAME);
    if (newCrashFile.isFile()) {
      String ackReportFilename = getAckReportFilename();
      StringBuilder header = new StringBuilder();
      StringBuilder report = new StringBuilder();
      try (BufferedReader reader =
          new BufferedReader(
              new InputStreamReader(
                  mApp.openFileInput(NEW_CRASH_FILENAME), Charset.forName("UTF-8")))) {
        try (BufferedWriter writer =
            new BufferedWriter(
                new OutputStreamWriter(
                    mApp.openFileOutput(ackReportFilename, Context.MODE_PRIVATE),
                    Charset.forName("UTF-8")))) {
          Logger.i(TAG, "Archiving crash report to %s.", ackReportFilename);
          Logger.d(TAG, "Crash report:");
          String line;
          boolean stillInHeader = true;
          while (null != (line = reader.readLine())) {
            writer.write(line);
            writer.newLine();
            report.append(line).append(NEW_LINE);
            if (line.equals(HEADER_BREAK_LINE)) stillInHeader = false;
            if (stillInHeader) header.append(line).append(NEW_LINE);
            Logger.d(TAG, "err: %s", line);
          }
        }
      } catch (Exception e) {
        Logger.e(TAG, "Failed to write crash report to archive!");
        return false;
      }

      if (!newCrashFile.delete()) {
        Logger.e(TAG, "Failed to delete crash log! %s", newCrashFile.getAbsolutePath());
      }

      sendNotification(
          header.toString(), report.toString(), new File(mApp.getFilesDir(), ackReportFilename));

      return true;
    }
    return false;
  }
@Test
  public void testCallsDetectedIfPreviouslyCrashed() throws Exception {
    Context app = ApplicationProvider.getApplicationContext();
    var notificationDriver = Mockito.mock(NotificationDriver.class);
    var notificationBuilder = Mockito.mock(NotifyBuilder.class);
    Mockito.doReturn(notificationBuilder)
        .when(notificationDriver)
        .buildNotification(Mockito.any(), Mockito.anyInt(), Mockito.anyInt());
    Mockito.doReturn(notificationBuilder).when(notificationBuilder).setContentText(Mockito.any());
    Mockito.doReturn(notificationBuilder).when(notificationBuilder).setColor(Mockito.anyInt());
    Mockito.doReturn(notificationBuilder).when(notificationBuilder).setDefaults(Mockito.anyInt());
    Mockito.doReturn(notificationBuilder).when(notificationBuilder).setContentIntent(Mockito.any());
    Mockito.doReturn(notificationBuilder)
        .when(notificationBuilder)
        .setAutoCancel(Mockito.anyBoolean());
    Mockito.doReturn(notificationBuilder)
        .when(notificationBuilder)
        .setOnlyAlertOnce(Mockito.anyBoolean());

    TestableChewbaccaUncaughtExceptionHandler underTest =
        new TestableChewbaccaUncaughtExceptionHandler(app, null, notificationDriver);
    File newReport =
        new File(app.getFilesDir(), ChewbaccaUncaughtExceptionHandler.NEW_CRASH_FILENAME);
    List<String> reportTextLines =
        Arrays.asList(
            "header text",
            "header 2",
            ChewbaccaUncaughtExceptionHandler.HEADER_BREAK_LINE,
            "report text 1",
            "report text 2");
    Files.write(newReport.toPath(), reportTextLines);
    Assert.assertTrue(newReport.exists());
    Assert.assertTrue(underTest.performCrashDetectingFlow());
    Assert.assertFalse(newReport.exists());
    File[] ackFiles = app.getFilesDir().listFiles();
    Assert.assertEquals(1, ackFiles.length);
    Matcher matcher =
        Pattern.compile(
                ChewbaccaUncaughtExceptionHandler.ACK_CRASH_FILENAME_TEMPLATE.replace(
                    "{TIME}", "\\d+"))
            .matcher(ackFiles[0].getName());
    Assert.assertTrue(ackFiles[0].getName() + " did not match", matcher.find());
    List<String> text = Files.readAllLines(ackFiles[0].toPath());
    Assert.assertEquals(5, text.size());
    for (int lineIndex = 0; lineIndex < reportTextLines.size(); lineIndex++) {
      Assert.assertEquals(
          "line " + lineIndex + " not equals", reportTextLines.get(lineIndex), text.get(lineIndex));
    }

    Mockito.verify(notificationDriver)
        .buildNotification(
            NotificationIds.CrashDetected,
            R.drawable.ic_crash_detected,
            R.string.ime_crashed_title);
    Mockito.verify(notificationDriver).notify(Mockito.notNull(), Mockito.eq(true));

    ArgumentCaptor<PendingIntent> captor = ArgumentCaptor.forClass(PendingIntent.class);
    Mockito.verify(notificationBuilder).setContentIntent(captor.capture());

    Intent savedIntent = Shadows.shadowOf(captor.getValue()).getSavedIntent();
    Assert.assertEquals(
        Intent.FLAG_ACTIVITY_NEW_TASK, savedIntent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
    Assert.assertEquals(Intent.ACTION_VIEW, savedIntent.getAction());
    Assert.assertEquals(Uri.parse("https://example.com"), savedIntent.getData());
    BugReportDetails reportDetails =
        savedIntent.getParcelableExtra(BugReportDetails.EXTRA_KEY_BugReportDetails);
    Assert.assertNotNull(reportDetails);
    Assert.assertEquals(
        reportDetails.crashHeader.trim(), String.join("\n", reportTextLines.subList(0, 2)));
    Assert.assertEquals(reportDetails.crashReportText.trim(), String.join("\n", reportTextLines));
    Assert.assertEquals("file", reportDetails.fullReport.getScheme());
    Assert.assertEquals(Uri.fromFile(ackFiles[0]), reportDetails.fullReport);
  }