Browse Source

Fix threading issue in download code

master
Anthony Restaino 8 years ago
parent
commit
e7cbd3e2ed
  1. 1
      app/src/main/java/acr/browser/lightning/async/ImageDownloadTask.java
  2. 40
      app/src/main/java/acr/browser/lightning/download/FetchUrlMimeType.java
  3. 1
      app/src/main/java/acr/browser/lightning/download/LightningDownloadListener.java

1
app/src/main/java/acr/browser/lightning/async/ImageDownloadTask.java

@ -1,6 +1,5 @@ @@ -1,6 +1,5 @@
package acr.browser.lightning.async;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

40
app/src/main/java/acr/browser/lightning/download/FetchUrlMimeType.java

@ -9,9 +9,11 @@ import android.os.Environment; @@ -9,9 +9,11 @@ import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.util.Log;
import android.webkit.MimeTypeMap;
import android.webkit.URLUtil;
import com.anthonycr.bonsai.Schedulers;
import com.squareup.otto.Bus;
import java.io.IOException;
@ -32,14 +34,12 @@ import acr.browser.lightning.bus.BrowserEvents; @@ -32,14 +34,12 @@ import acr.browser.lightning.bus.BrowserEvents;
*/
class FetchUrlMimeType extends Thread {
private final Context mContext;
private static final String TAG = FetchUrlMimeType.class.getSimpleName();
private final Context mContext;
private final DownloadManager.Request mRequest;
private final String mUri;
private final String mCookies;
private final String mUserAgent;
public FetchUrlMimeType(Context context, DownloadManager.Request request, String uri,
@ -95,9 +95,9 @@ class FetchUrlMimeType extends Thread { @@ -95,9 +95,9 @@ class FetchUrlMimeType extends Thread {
String filename = "";
if (mimeType != null) {
if (mimeType.equalsIgnoreCase("text/plain")
|| mimeType.equalsIgnoreCase("application/octet-stream")) {
|| mimeType.equalsIgnoreCase("application/octet-stream")) {
String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
MimeTypeMap.getFileExtensionFromUrl(mUri));
MimeTypeMap.getFileExtensionFromUrl(mUri));
if (newMimeType != null) {
mRequest.setMimeType(newMimeType);
}
@ -108,11 +108,31 @@ class FetchUrlMimeType extends Thread { @@ -108,11 +108,31 @@ class FetchUrlMimeType extends Thread {
// Start the download
DownloadManager manager = (DownloadManager) mContext
.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(mRequest);
Handler handler = new Handler(Looper.getMainLooper());
.getSystemService(Context.DOWNLOAD_SERVICE);
try {
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;
handler.post(new Runnable() {
Schedulers.main().execute(new Runnable() {
@Override
public void run() {
eventBus.post(new BrowserEvents.ShowSnackBarMessage(mContext.getString(R.string.download_pending) + ' ' + file));

1
app/src/main/java/acr/browser/lightning/download/LightningDownloadListener.java

@ -49,7 +49,6 @@ public class LightningDownloadListener implements DownloadListener { @@ -49,7 +49,6 @@ public class LightningDownloadListener implements DownloadListener {
DownloadHandler.onDownloadStart(mActivity, mPreferenceManager, url, userAgent,
contentDisposition, mimetype);
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}

Loading…
Cancel
Save