use of acr.browser.lightning.database.downloads.DownloadEntry in project Lightning-Browser by anthonycr.
the class DownloadHandler method onDownloadStartNoStream.
/**
* Notify the host application a download should be done, even if there is a
* streaming viewer available for thise type.
*
* @param context The context in which the download is requested.
* @param url The full url to the content that should be downloaded
* @param userAgent User agent of the downloading application.
* @param contentDisposition Content-disposition http header, if present.
* @param mimetype The mimetype of the content reported by the server
* @param contentSize The size of the content
*/
/* package */
private void onDownloadStartNoStream(@NonNull final Activity context, @NonNull UserPreferences preferences, @NonNull String url, String userAgent, String contentDisposition, @Nullable String mimetype, @NonNull String contentSize) {
final String filename = URLUtil.guessFileName(url, contentDisposition, mimetype);
// Check to see if we have an SDCard
String status = Environment.getExternalStorageState();
if (!status.equals(Environment.MEDIA_MOUNTED)) {
int title;
String msg;
// Check to see if the SDCard is busy, same as the music app
if (status.equals(Environment.MEDIA_SHARED)) {
msg = context.getString(R.string.download_sdcard_busy_dlg_msg);
title = R.string.download_sdcard_busy_dlg_title;
} else {
msg = context.getString(R.string.download_no_sdcard_dlg_msg);
title = R.string.download_no_sdcard_dlg_title;
}
Dialog dialog = new AlertDialog.Builder(context).setTitle(title).setIcon(android.R.drawable.ic_dialog_alert).setMessage(msg).setPositiveButton(R.string.action_ok, null).show();
BrowserDialog.setDialogSize(context, dialog);
return;
}
// java.net.URI is a lot stricter than KURL so we have to encode some
// extra characters. Fix for b 2538060 and b 1634719
WebAddress webAddress;
try {
webAddress = new WebAddress(url);
webAddress.setPath(encodePath(webAddress.getPath()));
} catch (Exception e) {
// This only happens for very bad urls, we want to catch the
// exception here
logger.log(TAG, "Exception while trying to parse url '" + url + '\'', e);
ActivityExtensions.snackbar(context, R.string.problem_download);
return;
}
String addressString = webAddress.toString();
Uri uri = Uri.parse(addressString);
final DownloadManager.Request request;
try {
request = new DownloadManager.Request(uri);
} catch (IllegalArgumentException e) {
ActivityExtensions.snackbar(context, R.string.cannot_download);
return;
}
// set downloaded file destination to /sdcard/Download.
// or, should it be set to one of several Environment.DIRECTORY* dirs
// depending on mimetype?
String location = preferences.getDownloadDirectory();
location = FileUtils.addNecessarySlashes(location);
Uri downloadFolder = Uri.parse(location);
if (!isWriteAccessAvailable(downloadFolder)) {
ActivityExtensions.snackbar(context, R.string.problem_location_download);
return;
}
String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(Utils.guessFileExtension(filename));
logger.log(TAG, "New mimetype: " + newMimeType);
request.setMimeType(newMimeType);
request.setDestinationUri(Uri.parse(Constants.FILE + location + filename));
// let this downloaded file be scanned by MediaScanner - so that it can
// show up in Gallery app, for example.
request.setVisibleInDownloadsUi(true);
request.allowScanningByMediaScanner();
request.setDescription(webAddress.getHost());
// XXX: Have to use the old url since the cookies were stored using the
// old percent-encoded url.
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader(COOKIE_REQUEST_HEADER, cookies);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// noinspection VariableNotUsedInsideIf
if (mimetype == null) {
logger.log(TAG, "Mimetype is null");
if (TextUtils.isEmpty(addressString)) {
return;
}
// We must have long pressed on a link or image to download it. We
// are not sure of the mimetype in this case, so do a head request
final Disposable disposable = new FetchUrlMimeType(downloadManager, request, addressString, cookies, userAgent).create().subscribeOn(networkScheduler).observeOn(mainScheduler).subscribe(result -> {
switch(result) {
case FAILURE_ENQUEUE:
ActivityExtensions.snackbar(context, R.string.cannot_download);
break;
case FAILURE_LOCATION:
ActivityExtensions.snackbar(context, R.string.problem_location_download);
break;
case SUCCESS:
ActivityExtensions.snackbar(context, R.string.download_pending);
break;
}
});
} else {
logger.log(TAG, "Valid mimetype, attempting to download");
try {
downloadManager.enqueue(request);
} catch (IllegalArgumentException e) {
// Probably got a bad URL or something
logger.log(TAG, "Unable to enqueue request", e);
ActivityExtensions.snackbar(context, R.string.cannot_download);
} catch (SecurityException e) {
// TODO write a download utility that downloads files rather than rely on the system
// because the system can only handle Environment.getExternal... as a path
ActivityExtensions.snackbar(context, R.string.problem_location_download);
}
ActivityExtensions.snackbar(context, context.getString(R.string.download_pending) + ' ' + filename);
}
// save download in database
UIController browserActivity = (UIController) context;
LightningView view = browserActivity.getTabModel().getCurrentTab();
if (view != null && !view.isIncognito()) {
downloadsRepository.addDownloadIfNotExists(new DownloadEntry(url, filename, contentSize)).subscribeOn(databaseScheduler).subscribe(aBoolean -> {
if (!aBoolean) {
logger.log(TAG, "error saving download to database");
}
});
}
}
Aggregations