コード例 #1
0
 @Override
 public void onConfirmInfoBarButtonClicked(ConfirmInfoBar infoBar, boolean confirm) {
   if (mPendingRequest.hasDownloadId()) {
     nativeDangerousDownloadValidated(mTab, mPendingRequest.getDownloadId(), confirm);
     if (confirm) {
       showDownloadStartNotification();
     }
     closeBlankTab();
   } else if (confirm) {
     // User confirmed the download.
     if (mPendingRequest.isGETRequest()) {
       enqueueDownloadManagerRequest(mPendingRequest);
     } else {
       DownloadInfo newDownloadInfo =
           DownloadInfo.Builder.fromDownloadInfo(mPendingRequest).setIsSuccessful(true).build();
       DownloadManagerService.getDownloadManagerService(mContext)
           .onDownloadCompleted(newDownloadInfo);
     }
   } else {
     // User did not accept the download, discard the file if it is a POST download.
     if (!mPendingRequest.isGETRequest()) {
       discardFile(mPendingRequest.getFilePath());
     }
   }
   mPendingRequest = null;
   infoBar.dismissJavaOnlyInfoBar();
 }
  @Override
  public void onConfirmInfoBarButtonClicked(ConfirmInfoBar infoBar, boolean confirm) {
    assert mTab != null;
    if (mPendingRequest.hasDownloadId()) {
      nativeDangerousDownloadValidated(mTab, mPendingRequest.getDownloadId(), confirm);
      if (confirm) {
        showDownloadStartNotification();
      }
      closeBlankTab();
    } else if (confirm) {
      // User confirmed the download.
      if (mPendingRequest.isGETRequest()) {
        final DownloadInfo info = mPendingRequest;
        new AsyncTask<Void, Void, Pair<String, String>>() {
          @Override
          protected Pair<String, String> doInBackground(Void... params) {
            Pair<String, String> result = getDownloadDirectoryNameAndFullPath();
            String fullDirPath = result.second;
            return doesFileAlreadyExists(fullDirPath, info.getFileName()) ? result : null;
          }

          @Override
          protected void onPostExecute(Pair<String, String> result) {
            if (result != null) {
              // File already exists.
              String dirName = result.first;
              String fullDirPath = result.second;
              launchDownloadInfoBar(info, dirName, fullDirPath);
            } else {
              enqueueDownloadManagerRequest(info);
            }
          }
        }.execute();
      } else {
        DownloadInfo newDownloadInfo =
            DownloadInfo.Builder.fromDownloadInfo(mPendingRequest).setIsSuccessful(true).build();
        DownloadManagerService.getDownloadManagerService(mContext)
            .onDownloadCompleted(newDownloadInfo);
      }
    } else {
      // User did not accept the download, discard the file if it is a POST download.
      if (!mPendingRequest.isGETRequest()) {
        discardFile(mPendingRequest.getFilePath());
      }
    }
    mPendingRequest = null;
    infoBar.dismissJavaOnlyInfoBar();
  }
 /**
  * Alerts user of download failure.
  *
  * @param fileName Name of the download file.
  */
 private void alertDownloadFailure(String fileName) {
   DownloadManagerService.getDownloadManagerService(mContext.getApplicationContext())
       .onDownloadFailed(fileName);
 }
 /**
  * Enqueue a request to download a file using Android DownloadManager.
  *
  * @param info Download information about the download.
  */
 private void enqueueDownloadManagerRequest(final DownloadInfo info) {
   DownloadManagerService.getDownloadManagerService(mContext.getApplicationContext())
       .enqueueDownloadManagerRequest(info, true);
 }