|
|
@ -19,6 +19,7 @@ import android.support.v7.app.AlertDialog; |
|
|
|
import android.text.TextUtils; |
|
|
|
import android.text.TextUtils; |
|
|
|
import android.util.Log; |
|
|
|
import android.util.Log; |
|
|
|
import android.webkit.CookieManager; |
|
|
|
import android.webkit.CookieManager; |
|
|
|
|
|
|
|
import android.webkit.MimeTypeMap; |
|
|
|
import android.webkit.URLUtil; |
|
|
|
import android.webkit.URLUtil; |
|
|
|
|
|
|
|
|
|
|
|
import com.squareup.otto.Bus; |
|
|
|
import com.squareup.otto.Bus; |
|
|
@ -47,6 +48,14 @@ public class DownloadHandler { |
|
|
|
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) |
|
|
|
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) |
|
|
|
.getPath(); |
|
|
|
.getPath(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
|
|
|
public static String guessFileExtension(@NonNull String filename) { |
|
|
|
|
|
|
|
int lastIndex = filename.lastIndexOf('.') + 1; |
|
|
|
|
|
|
|
if (lastIndex > 0 && filename.length() > lastIndex) { |
|
|
|
|
|
|
|
return filename.substring(lastIndex, filename.length()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Notify the host application a download should be done, or that the data |
|
|
|
* Notify the host application a download should be done, or that the data |
|
|
@ -60,6 +69,12 @@ public class DownloadHandler { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static void onDownloadStart(@NonNull Context context, @NonNull PreferenceManager manager, String url, String userAgent, |
|
|
|
public static void onDownloadStart(@NonNull Context context, @NonNull PreferenceManager manager, String url, String userAgent, |
|
|
|
@Nullable String contentDisposition, String mimetype) { |
|
|
|
@Nullable String contentDisposition, String mimetype) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "DOWNLOAD: Trying to download from URL: " + url); |
|
|
|
|
|
|
|
Log.d(TAG, "DOWNLOAD: Content disposition: " + contentDisposition); |
|
|
|
|
|
|
|
Log.d(TAG, "DOWNLOAD: Mimetype: " + mimetype); |
|
|
|
|
|
|
|
Log.d(TAG, "DOWNLOAD: User agent: " + userAgent); |
|
|
|
|
|
|
|
|
|
|
|
// if we're dealing wih A/V content that's not explicitly marked
|
|
|
|
// if we're dealing wih A/V content that's not explicitly marked
|
|
|
|
// for download, check if it's streamable.
|
|
|
|
// for download, check if it's streamable.
|
|
|
|
if (contentDisposition == null |
|
|
|
if (contentDisposition == null |
|
|
@ -189,11 +204,10 @@ public class DownloadHandler { |
|
|
|
eventBus.post(new BrowserEvents.ShowSnackBarMessage(R.string.cannot_download)); |
|
|
|
eventBus.post(new BrowserEvents.ShowSnackBarMessage(R.string.cannot_download)); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
request.setMimeType(mimetype); |
|
|
|
|
|
|
|
// set downloaded file destination to /sdcard/Download.
|
|
|
|
// set downloaded file destination to /sdcard/Download.
|
|
|
|
// or, should it be set to one of several Environment.DIRECTORY* dirs
|
|
|
|
// or, should it be set to one of several Environment.DIRECTORY* dirs
|
|
|
|
// depending on mimetype?
|
|
|
|
// depending on mimetype?
|
|
|
|
|
|
|
|
|
|
|
|
String location = preferences.getDownloadDirectory(); |
|
|
|
String location = preferences.getDownloadDirectory(); |
|
|
|
Uri downloadFolder; |
|
|
|
Uri downloadFolder; |
|
|
|
location = addNecessarySlashes(location); |
|
|
|
location = addNecessarySlashes(location); |
|
|
@ -210,6 +224,9 @@ public class DownloadHandler { |
|
|
|
eventBus.post(new BrowserEvents.ShowSnackBarMessage(R.string.problem_location_download)); |
|
|
|
eventBus.post(new BrowserEvents.ShowSnackBarMessage(R.string.problem_location_download)); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(guessFileExtension(filename)); |
|
|
|
|
|
|
|
Log.d(TAG, "New mimetype: " + newMimeType); |
|
|
|
|
|
|
|
request.setMimeType(newMimeType); |
|
|
|
request.setDestinationUri(Uri.parse(Constants.FILE + location + filename)); |
|
|
|
request.setDestinationUri(Uri.parse(Constants.FILE + location + filename)); |
|
|
|
// let this downloaded file be scanned by MediaScanner - so that it can
|
|
|
|
// let this downloaded file be scanned by MediaScanner - so that it can
|
|
|
|
// show up in Gallery app, for example.
|
|
|
|
// show up in Gallery app, for example.
|
|
|
@ -221,6 +238,8 @@ public class DownloadHandler { |
|
|
|
String cookies = CookieManager.getInstance().getCookie(url); |
|
|
|
String cookies = CookieManager.getInstance().getCookie(url); |
|
|
|
request.addRequestHeader(COOKIE_REQUEST_HEADER, cookies); |
|
|
|
request.addRequestHeader(COOKIE_REQUEST_HEADER, cookies); |
|
|
|
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); |
|
|
|
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//noinspection VariableNotUsedInsideIf
|
|
|
|
if (mimetype == null) { |
|
|
|
if (mimetype == null) { |
|
|
|
Log.d(TAG, "Mimetype is null"); |
|
|
|
Log.d(TAG, "Mimetype is null"); |
|
|
|
if (TextUtils.isEmpty(addressString)) { |
|
|
|
if (TextUtils.isEmpty(addressString)) { |
|
|
@ -247,7 +266,6 @@ public class DownloadHandler { |
|
|
|
eventBus.post(new BrowserEvents.ShowSnackBarMessage( |
|
|
|
eventBus.post(new BrowserEvents.ShowSnackBarMessage( |
|
|
|
context.getString(R.string.download_pending) + ' ' + filename)); |
|
|
|
context.getString(R.string.download_pending) + ' ' + filename)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static final String sFileName = "test"; |
|
|
|
private static final String sFileName = "test"; |
|
|
|