Fix threading issue in download code
This commit is contained in:
parent
5711421784
commit
e7cbd3e2ed
@ -1,6 +1,5 @@
|
|||||||
package acr.browser.lightning.async;
|
package acr.browser.lightning.async;
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
|
@ -9,9 +9,11 @@ import android.os.Environment;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
import android.util.Log;
|
||||||
import android.webkit.MimeTypeMap;
|
import android.webkit.MimeTypeMap;
|
||||||
import android.webkit.URLUtil;
|
import android.webkit.URLUtil;
|
||||||
|
|
||||||
|
import com.anthonycr.bonsai.Schedulers;
|
||||||
import com.squareup.otto.Bus;
|
import com.squareup.otto.Bus;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -32,14 +34,12 @@ import acr.browser.lightning.bus.BrowserEvents;
|
|||||||
*/
|
*/
|
||||||
class FetchUrlMimeType extends Thread {
|
class FetchUrlMimeType extends Thread {
|
||||||
|
|
||||||
|
private static final String TAG = FetchUrlMimeType.class.getSimpleName();
|
||||||
|
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
|
|
||||||
private final DownloadManager.Request mRequest;
|
private final DownloadManager.Request mRequest;
|
||||||
|
|
||||||
private final String mUri;
|
private final String mUri;
|
||||||
|
|
||||||
private final String mCookies;
|
private final String mCookies;
|
||||||
|
|
||||||
private final String mUserAgent;
|
private final String mUserAgent;
|
||||||
|
|
||||||
public FetchUrlMimeType(Context context, DownloadManager.Request request, String uri,
|
public FetchUrlMimeType(Context context, DownloadManager.Request request, String uri,
|
||||||
@ -95,9 +95,9 @@ class FetchUrlMimeType extends Thread {
|
|||||||
String filename = "";
|
String filename = "";
|
||||||
if (mimeType != null) {
|
if (mimeType != null) {
|
||||||
if (mimeType.equalsIgnoreCase("text/plain")
|
if (mimeType.equalsIgnoreCase("text/plain")
|
||||||
|| mimeType.equalsIgnoreCase("application/octet-stream")) {
|
|| mimeType.equalsIgnoreCase("application/octet-stream")) {
|
||||||
String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
|
String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
|
||||||
MimeTypeMap.getFileExtensionFromUrl(mUri));
|
MimeTypeMap.getFileExtensionFromUrl(mUri));
|
||||||
if (newMimeType != null) {
|
if (newMimeType != null) {
|
||||||
mRequest.setMimeType(newMimeType);
|
mRequest.setMimeType(newMimeType);
|
||||||
}
|
}
|
||||||
@ -108,11 +108,31 @@ class FetchUrlMimeType extends Thread {
|
|||||||
|
|
||||||
// Start the download
|
// Start the download
|
||||||
DownloadManager manager = (DownloadManager) mContext
|
DownloadManager manager = (DownloadManager) mContext
|
||||||
.getSystemService(Context.DOWNLOAD_SERVICE);
|
.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||||
manager.enqueue(mRequest);
|
try {
|
||||||
Handler handler = new Handler(Looper.getMainLooper());
|
manager.enqueue(mRequest);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
// Probably got a bad URL or something
|
||||||
|
Log.e(TAG, "Unable to enqueue request", e);
|
||||||
|
Schedulers.main().execute(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
eventBus.post(new BrowserEvents.ShowSnackBarMessage(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
|
||||||
|
Schedulers.main().execute(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
eventBus.post(new BrowserEvents.ShowSnackBarMessage(R.string.problem_location_download));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
final String file = filename;
|
final String file = filename;
|
||||||
handler.post(new Runnable() {
|
Schedulers.main().execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
eventBus.post(new BrowserEvents.ShowSnackBarMessage(mContext.getString(R.string.download_pending) + ' ' + file));
|
eventBus.post(new BrowserEvents.ShowSnackBarMessage(mContext.getString(R.string.download_pending) + ' ' + file));
|
||||||
|
@ -49,7 +49,6 @@ public class LightningDownloadListener implements DownloadListener {
|
|||||||
DownloadHandler.onDownloadStart(mActivity, mPreferenceManager, url, userAgent,
|
DownloadHandler.onDownloadStart(mActivity, mPreferenceManager, url, userAgent,
|
||||||
contentDisposition, mimetype);
|
contentDisposition, mimetype);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DialogInterface.BUTTON_NEGATIVE:
|
case DialogInterface.BUTTON_NEGATIVE:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user