Exemple #1
0
  private void checkAndSendReports() {
    TLog.d(TCrash.TAG, "#checkAndSendReports - start");
    CrashReportFinder reportFinder = new CrashReportFinder();
    String[] reportFiles = reportFinder.getCrashReportFiles();
    Arrays.sort(reportFiles);

    int reportsSentCount = 0;

    for (String curFileName : reportFiles)
      if (reportsSentCount > 1) {
        deleteFile(curFileName);
      } else {
        TLog.i(TCrash.TAG, "Sending file " + curFileName);
        try {
          TCrashReportPersister persister = new TCrashReportPersister();
          CrashReportData previousCrashReport = persister.load(curFileName);
          sendCrashReport(previousCrashReport);
          deleteFile(curFileName);
        } catch (RuntimeException e) {
          TLog.e(TCrash.TAG, "Failed to send crash reports for " + curFileName + e.getMessage());
          deleteFile(curFileName);
          break;
        } catch (IOException e) {
          TLog.e(TCrash.TAG, "Failed to load crash report for " + curFileName + e.getMessage());
          deleteFile(curFileName);
          break;
        } catch (ReportSenderException e) {
          TLog.e(TCrash.TAG, "Failed to send crash report for " + curFileName + e.getMessage());
          break;
        }

        reportsSentCount++;
      }
    TLog.d(TCrash.TAG, "#checkAndSendReports - finish");
  }
  private long downloadFile(TTask.Task task) throws Exception {
    this.mAndroidHttpClient = AndroidHttpClient.newInstance(this.TAG);
    HttpGet httpGet = new HttpGet(this.mUrl);
    HttpResponse response = this.mAndroidHttpClient.execute(httpGet);
    this.mTotalSize = response.getEntity().getContentLength();

    File file = new File(this.mFilePath, this.mFileName);

    if ((file.length() > 0L) && (this.mTotalSize > 0L) && (this.mTotalSize > file.length())) {
      httpGet.addHeader("Range", "bytes=" + file.length() + "-");
      this.mPreviousFileSize = file.length();

      this.mAndroidHttpClient.close();
      this.mAndroidHttpClient = AndroidHttpClient.newInstance("DownloadTask");
      response = this.mAndroidHttpClient.execute(httpGet);
      TLog.v(this.TAG, "File is not complete, .");
      TLog.v(
          this.TAG, "download now,File length:" + file.length() + " totalSize:" + this.mTotalSize);
    } else if ((file.exists()) && (this.mTotalSize == file.length())) {
      TLog.v(this.TAG, "Output file already exists. Skipping download.");
      return 0L;
    }

    long storage = TStorageUtils.getAvailableStorage();
    TLog.i(this.TAG, "storage:" + storage + " totalSize:" + this.mTotalSize);
    if (this.mTotalSize - file.length() > storage) {
      setErrorCode(1);
      task.stopTask();
      this.mAndroidHttpClient.close();
      return 0L;
    }
    try {
      this.mRandomAccessFile = new ProgressReportingRandomAccessFile(file, "rw");
    } catch (FileNotFoundException e) {
      TLog.v(this.TAG, "OutputStream Error");
    }

    InputStream input = null;
    try {
      input = response.getEntity().getContent();
    } catch (IOException ex) {
      setErrorCode(3);
      this.mAndroidHttpClient.close();
      TLog.v(this.TAG, "InputStream Error" + ex.getMessage());
      return 0L;
    }

    int bytesCopied = copy(input, this.mRandomAccessFile, task);

    if ((this.mPreviousFileSize + bytesCopied != this.mTotalSize)
        && (this.mTotalSize != -1L)
        && (!task.isCancel())) {
      throw new IOException("Download incomplete: " + bytesCopied + " != " + this.mTotalSize);
    }

    this.mRandomAccessFile.close();
    this.mAndroidHttpClient.close();
    this.mAndroidHttpClient = null;
    this.mRandomAccessFile = null;
    TLog.v(this.TAG, "Download completed successfully.");
    return bytesCopied;
  }