Beispiel #1
0
 /**
  * Delete pending reports.
  *
  * @param deleteApprovedReports Set to true to delete approved and silent reports.
  * @param deleteNonApprovedReports Set to true to delete non approved/silent reports.
  * @param nbOfLatestToKeep Number of pending reports to retain.
  */
 private void deletePendingReports(
     boolean deleteApprovedReports, boolean deleteNonApprovedReports, int nbOfLatestToKeep) {
   // TODO Check logic and instances where nbOfLatestToKeep = X, because
   // that might stop us from deleting any reports.
   final CrashReportFinder reportFinder = new CrashReportFinder(mContext);
   final String[] filesList = reportFinder.getCrashReportFiles();
   Arrays.sort(filesList);
   for (int iFile = 0; iFile < filesList.length - nbOfLatestToKeep; iFile++) {
     final String fileName = filesList[iFile];
     final boolean isReportApproved = fileNameParser.isApproved(fileName);
     if ((isReportApproved && deleteApprovedReports)
         || (!isReportApproved && deleteNonApprovedReports)) {
       final File fileToDelete = new File(mContext.getFilesDir(), fileName);
       ACRA.log.d(ACRA.LOG_TAG, "Deleting file " + fileName);
       if (!fileToDelete.delete()) {
         Log.e(ACRA.LOG_TAG, "Could not delete report : " + fileToDelete);
       }
     }
   }
 }
Beispiel #2
0
  /**
   * This method looks for pending reports and does the action required depending on the interaction
   * mode set.
   */
  public void checkReportsOnApplicationStart() {

    // Delete any old unsent reports if this is a newer version of the app
    // than when we last started.
    final long lastVersionNr = prefs.getInt(ACRA.PREF_LAST_VERSION_NR, 0);
    final PackageManagerWrapper packageManagerWrapper = new PackageManagerWrapper(mContext);
    final PackageInfo packageInfo = packageManagerWrapper.getPackageInfo();
    final boolean newVersion = (packageInfo != null && packageInfo.versionCode > lastVersionNr);
    if (newVersion) {
      if (ACRA.getConfig().deleteOldUnsentReportsOnApplicationStart()) {
        deletePendingReports();
      }
      final SharedPreferences.Editor prefsEditor = prefs.edit();
      prefsEditor.putInt(ACRA.PREF_LAST_VERSION_NR, packageInfo.versionCode);
      prefsEditor.commit();
    }

    if ((ACRA.getConfig().mode() == ReportingInteractionMode.NOTIFICATION
            || ACRA.getConfig().mode() == ReportingInteractionMode.DIALOG)
        && ACRA.getConfig().deleteUnapprovedReportsOnApplicationStart()) {
      // NOTIFICATION or DIALOG mode, and there are unapproved reports to
      // send (latest notification/dialog has been ignored: neither
      // accepted
      // nor refused). The application developer has decided that
      // these reports should not be renotified ==> destroy them all but
      // one.
      deletePendingNonApprovedReports(true);
    }

    final CrashReportFinder reportFinder = new CrashReportFinder(mContext);
    String[] filesList = reportFinder.getCrashReportFiles();

    if (filesList != null && filesList.length > 0) {
      // Immediately send reports for SILENT and TOAST modes.
      // Immediately send reports in NOTIFICATION mode only if they are
      // all silent or approved.
      // If there is still one unapproved report in NOTIFICATION mode,
      // notify it.
      // If there are unapproved reports in DIALOG mode, show the dialog

      ReportingInteractionMode reportingInteractionMode = ACRA.getConfig().mode();

      filesList = reportFinder.getCrashReportFiles();
      final boolean onlySilentOrApprovedReports = containsOnlySilentOrApprovedReports(filesList);

      if (reportingInteractionMode == ReportingInteractionMode.SILENT
          || reportingInteractionMode == ReportingInteractionMode.TOAST
          || (onlySilentOrApprovedReports
              && (reportingInteractionMode == ReportingInteractionMode.NOTIFICATION
                  || reportingInteractionMode == ReportingInteractionMode.DIALOG))) {

        if (reportingInteractionMode == ReportingInteractionMode.TOAST
            && !onlySilentOrApprovedReports) {
          // Display the Toast in TOAST mode only if there are
          // non-silent reports.
          ToastSender.sendToast(mContext, ACRA.getConfig().resToastText(), Toast.LENGTH_LONG);
        }

        Log.v(
            ACRA.LOG_TAG, "About to start ReportSenderWorker from #checkReportOnApplicationStart");
        startSendingReports(false, false);
      } else if (ACRA.getConfig().mode() == ReportingInteractionMode.NOTIFICATION) {
        // NOTIFICATION mode there are unapproved reports to send
        // Display the notification.
        // The user comment will be associated to the latest report
        notifySendReport(getLatestNonSilentReport(filesList));
      } else if (ACRA.getConfig().mode() == ReportingInteractionMode.DIALOG) {
        // DIALOG mode: the dialog is always displayed because it has
        // been put on the task stack before killing the app.
        // The user can explicitly say Yes or No... or ignore the dialog
        // with the back button.
      }
    }
  }