/**
   * Notify the host application a download should be done, even if there is a streaming viewer
   * available for this type.
   *
   * @param downloadInfo Information about the download.
   */
  protected void onDownloadStartNoStream(final DownloadInfo downloadInfo) {
    final String newMimeType =
        remapGenericMimeType(
            downloadInfo.getMimeType(), downloadInfo.getUrl(), downloadInfo.getFileName());
    final String fileName =
        TextUtils.isEmpty(downloadInfo.getFileName())
            ? fileName(downloadInfo.getUrl(), newMimeType, downloadInfo.getContentDisposition())
            : downloadInfo.getFileName();
    new AsyncTask<Void, Void, Object[]>() {
      @Override
      protected Object[] doInBackground(Void... params) {
        // Check to see if we have an SDCard.
        String status = Environment.getExternalStorageState();
        Pair<String, String> result = getDownloadDirectoryNameAndFullPath();
        String dirName = result.first;
        String fullDirPath = result.second;
        boolean fileExists = doesFileAlreadyExists(fullDirPath, fileName);

        return new Object[] {status, dirName, fullDirPath, fileExists};
      }

      @Override
      protected void onPostExecute(Object[] result) {
        String externalStorageState = (String) result[0];
        String dirName = (String) result[1];
        String fullDirPath = (String) result[2];
        Boolean fileExists = (Boolean) result[3];
        if (!checkExternalStorageAndNotify(fileName, fullDirPath, externalStorageState)) {
          return;
        }
        String url = sanitizeDownloadUrl(downloadInfo);
        if (url == null) return;
        DownloadInfo newInfo =
            DownloadInfo.Builder.fromDownloadInfo(downloadInfo)
                .setUrl(url)
                .setMimeType(newMimeType)
                .setDescription(url)
                .setFileName(fileName)
                .setIsGETRequest(true)
                .build();

        // TODO(acleung): This is a temp fix to disable auto downloading if flash files.
        // We want to avoid downloading flash files when it is linked as an iframe.
        // The proper fix would be to let chrome knows which frame originated the request.
        if ("application/x-shockwave-flash".equals(newInfo.getMimeType())) return;

        if (isDangerousFile(fileName, newMimeType)) {
          confirmDangerousDownload(newInfo);
        } else {
          // Not a dangerous file, proceed.
          if (fileExists) {
            launchDownloadInfoBar(newInfo, dirName, fullDirPath);
          } else {
            enqueueDownloadManagerRequest(newInfo);
          }
        }
      }
    }.execute();
  }
コード例 #2
0
  /**
   * Notify the host application a download should be done, even if there is a streaming viewer
   * available for this type.
   *
   * @param downloadInfo Information about the download.
   */
  protected void onDownloadStartNoStream(DownloadInfo downloadInfo) {
    final String newMimeType =
        remapGenericMimeType(
            downloadInfo.getMimeType(), downloadInfo.getUrl(), downloadInfo.getFileName());
    final String path =
        TextUtils.isEmpty(downloadInfo.getFileName())
            ? fileName(downloadInfo.getUrl(), newMimeType, downloadInfo.getContentDisposition())
            : downloadInfo.getFileName();
    final File file = new File(path);
    final String fileName = file.getName();

    if (!checkExternalStorageAndNotify(downloadPath(fileName))) {
      return;
    }
    String url = sanitizeDownloadUrl(downloadInfo);
    if (url == null) return;
    DownloadInfo newInfo =
        DownloadInfo.Builder.fromDownloadInfo(downloadInfo)
            .setUrl(url)
            .setMimeType(newMimeType)
            .setDescription(url)
            .setFileName(fileName)
            .setIsGETRequest(true)
            .build();

    // TODO(acleung): This is a temp fix to disable auto downloading if flash files.
    // We want to avoid downloading flash files when it is linked as an iframe.
    // The proper fix would be to let chrome knows which frame originated the request.
    if ("application/x-shockwave-flash".equals(newInfo.getMimeType())) return;

    if (isDangerousFile(fileName, newMimeType)) {
      confirmDangerousDownload(newInfo);
    } else {
      // Not a dangerous file, proceed.
      enqueueDownloadManagerRequest(newInfo);
    }
  }
 /**
  * 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);
 }
 /**
  * Sanitize the URL for the download item.
  *
  * @param downloadInfo Information about the download.
  */
 protected String sanitizeDownloadUrl(DownloadInfo downloadInfo) {
   return downloadInfo.getUrl();
 }