Search in sources :

Example 1 with DownloadInfo

use of org.mozilla.focus.download.DownloadInfo in project Rocket by mozilla-tw.

the class DownloadListAdapter method hideItem.

private void hideItem(long rowId) {
    for (int i = 0; i < mDownloadInfo.size(); i++) {
        DownloadInfo downloadInfo = mDownloadInfo.get(i);
        if (rowId == downloadInfo.getRowId()) {
            mDownloadInfo.remove(downloadInfo);
            this.notifyItemRemoved(i);
            break;
        }
    }
}
Also used : DownloadInfo(org.mozilla.focus.download.DownloadInfo)

Example 2 with DownloadInfo

use of org.mozilla.focus.download.DownloadInfo in project Rocket by mozilla-tw.

the class BrowserFragment method queueDownload.

/**
 * Use Android's Download Manager to queue this download.
 */
private void queueDownload(Download download) {
    if (download == null) {
        return;
    }
    final Context context = getContext();
    if (context == null) {
        return;
    }
    final String cookie = CookieManager.getInstance().getCookie(download.getUrl());
    final String fileName = URLUtil.guessFileName(download.getUrl(), download.getContentDisposition(), download.getMimeType());
    // so far each download always return null even for an image.
    // But we might move downloaded file to another directory.
    // So, for now we always save file to DIRECTORY_DOWNLOADS
    final String dir = Environment.DIRECTORY_DOWNLOADS;
    if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        Toast.makeText(getContext(), R.string.message_storage_unavailable_cancel_download, Toast.LENGTH_LONG).show();
        return;
    }
    // block non-http/https download links
    if (!URLUtil.isNetworkUrl(download.getUrl())) {
        Toast.makeText(getContext(), R.string.download_file_not_supported, Toast.LENGTH_LONG).show();
        return;
    }
    final DownloadManager.Request request = new DownloadManager.Request(Uri.parse(download.getUrl())).addRequestHeader("User-Agent", download.getUserAgent()).addRequestHeader("Cookie", cookie).addRequestHeader("Referer", getUrl()).setDestinationInExternalPublicDir(dir, fileName).setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED).setMimeType(download.getMimeType());
    request.allowScanningByMediaScanner();
    final DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    final Long downloadId = manager.enqueue(request);
    DownloadInfo downloadInfo = new DownloadInfo();
    downloadInfo.setDownloadId(downloadId);
    // file is not moved.)
    if (!DownloadInfoManager.getInstance().recordExists(downloadId)) {
        DownloadInfoManager.getInstance().insert(downloadInfo, new DownloadInfoManager.AsyncInsertListener() {

            @Override
            public void onInsertComplete(long id) {
                DownloadInfoManager.notifyRowUpdated(getContext(), id);
            }
        });
    } else {
        DownloadInfoManager.getInstance().queryByDownloadId(downloadId, new DownloadInfoManager.AsyncQueryListener() {

            @Override
            public void onQueryComplete(List downloadInfoList) {
                if (!downloadInfoList.isEmpty()) {
                    DownloadInfo info = (DownloadInfo) downloadInfoList.get(0);
                    DownloadInfoManager.getInstance().delete(info.getRowId(), null);
                    DownloadInfoManager.getInstance().insert(info, new DownloadInfoManager.AsyncInsertListener() {

                        @Override
                        public void onInsertComplete(long id) {
                            DownloadInfoManager.notifyRowUpdated(getContext(), id);
                            final Intent broadcastIntent = new Intent(Constants.ACTION_NOTIFY_RELOCATE_FINISH);
                            broadcastIntent.addCategory(Constants.CATEGORY_FILE_OPERATION);
                            broadcastIntent.putExtra(Constants.EXTRA_ROW_ID, id);
                            LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(broadcastIntent);
                        }
                    });
                }
            }
        });
    }
    if (!download.isStartFromContextMenu()) {
        Toast.makeText(getContext(), R.string.download_started, Toast.LENGTH_LONG).show();
    }
}
Also used : Context(android.content.Context) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) DownloadManager(android.app.DownloadManager) DownloadInfoManager(org.mozilla.focus.download.DownloadInfoManager) DownloadInfo(org.mozilla.focus.download.DownloadInfo) WebBackForwardList(android.webkit.WebBackForwardList) List(java.util.List)

Example 3 with DownloadInfo

use of org.mozilla.focus.download.DownloadInfo in project Rocket by mozilla-tw.

the class DownloadsFragment method onQueryComplete.

@Override
public void onQueryComplete(List downloadInfoList) {
    for (int i = 0; i < downloadInfoList.size(); i++) {
        DownloadInfo downloadInfo = (DownloadInfo) downloadInfoList.get(i);
        mDownloadListAdapter.updateItem(downloadInfo);
    }
}
Also used : DownloadInfo(org.mozilla.focus.download.DownloadInfo)

Example 4 with DownloadInfo

use of org.mozilla.focus.download.DownloadInfo in project Rocket by mozilla-tw.

the class DownloadCompleteReceiver method onReceive.

@Override
public void onReceive(final Context context, Intent intent) {
    long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
    if (downloadId == -1) {
        return;
    }
    DownloadInfoManager.getInstance().queryByDownloadId(downloadId, new DownloadInfoManager.AsyncQueryListener() {

        @Override
        public void onQueryComplete(List downloadInfoList) {
            if (downloadInfoList.size() > 0) {
                final DownloadInfo downloadInfo = (DownloadInfo) downloadInfoList.get(0);
                if ((downloadInfo.getStatus() == DownloadManager.STATUS_SUCCESSFUL) && !TextUtils.isEmpty(downloadInfo.getFileUri())) {
                    // have to update, then the fileUri may write into our DB.
                    DownloadInfoManager.getInstance().updateByRowId(downloadInfo, new DownloadInfoManager.AsyncUpdateListener() {

                        @Override
                        public void onUpdateComplete(int result) {
                            final Uri fileUri = Uri.parse(downloadInfo.getFileUri());
                            if ("file".equals(fileUri.getScheme())) {
                                // on some device the uri is "file:///storage/emulated/0/Download/file.png"
                                // but the real path is "file:///storage/emulated/legacy/Download/file.png"
                                // Since we already restrict download folder when we were making request to
                                // DownloadManager, now we only look for the file-name in download folder.
                                final String fileName = (new File(fileUri.getPath())).getName();
                                final String type = Environment.DIRECTORY_DOWNLOADS;
                                final File dir = Environment.getExternalStoragePublicDirectory(type);
                                final File downloadedFile = new File(dir, fileName);
                                if (downloadedFile.exists() && downloadedFile.canWrite()) {
                                    RelocateService.startActionMove(context, downloadInfo.getRowId(), downloadInfo.getDownloadId(), downloadedFile, downloadInfo.getMimeType());
                                }
                            }
                        }
                    });
                }
                // Download canceled
                if (!downloadInfo.existInDownloadManager()) {
                    DownloadInfoManager.getInstance().delete(downloadInfo.getRowId(), null);
                }
            }
        }
    });
}
Also used : DownloadInfo(org.mozilla.focus.download.DownloadInfo) List(java.util.List) Uri(android.net.Uri) File(java.io.File) DownloadInfoManager(org.mozilla.focus.download.DownloadInfoManager)

Aggregations

DownloadInfo (org.mozilla.focus.download.DownloadInfo)4 List (java.util.List)2 DownloadInfoManager (org.mozilla.focus.download.DownloadInfoManager)2 DownloadManager (android.app.DownloadManager)1 PendingIntent (android.app.PendingIntent)1 Context (android.content.Context)1 Intent (android.content.Intent)1 Uri (android.net.Uri)1 WebBackForwardList (android.webkit.WebBackForwardList)1 File (java.io.File)1