/**
  * Request a download from the given url, or if a streaming viewer is available stream the content
  * into the viewer.
  *
  * @param downloadInfo Information about the download.
  */
 @Override
 public void requestHttpGetDownload(DownloadInfo downloadInfo) {
   // If we're dealing with A/V content that's not explicitly marked for download, check if it
   // is streamable.
   if (!DownloadManagerService.isAttachment(downloadInfo.getContentDisposition())) {
     // Query the package manager to see if there's a registered handler that matches.
     Intent intent = new Intent(Intent.ACTION_VIEW);
     intent.setDataAndType(Uri.parse(downloadInfo.getUrl()), downloadInfo.getMimeType());
     // If the intent is resolved to ourselves, we don't want to attempt to load the url
     // only to try and download it again.
     if (DownloadManagerService.openIntent(mContext, intent, false)) {
       return;
     }
   }
   onDownloadStartNoStream(downloadInfo);
 }
Ejemplo n.º 2
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);
 }