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;
}
}
}
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();
}
}
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);
}
}
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);
}
}
}
});
}
Aggregations