Downloads weren't cancelled properly

This commit is contained in:
daniel oeh 2012-08-21 18:37:18 +02:00
parent eab911c2ff
commit 8bca22eff9
3 changed files with 11 additions and 5 deletions

View File

@ -243,7 +243,7 @@ public class DownloadService extends Service {
Log.d(TAG, "Cancelling download with url " + url);
Downloader d = getDownloader(url);
if (d != null) {
d.interrupt();
d.cancel();
removeDownload(d);
} else {
Log.e(TAG, "Could not cancel download with url " + url);
@ -251,7 +251,7 @@ public class DownloadService extends Service {
} else if (intent.getAction().equals(ACTION_CANCEL_ALL_DOWNLOADS)) {
for (Downloader d : downloads) {
d.interrupt();
d.cancel();
DownloadRequester.getInstance().removeDownload(
d.getStatus().getFeedFile());
d.getStatus().getFeedFile().setFile_url(null);

View File

@ -12,6 +12,8 @@ public abstract class Downloader extends Thread {
private DownloadService downloadService;
protected boolean finished;
protected volatile boolean cancelled;
protected volatile DownloadStatus status;
@ -19,6 +21,7 @@ public abstract class Downloader extends Thread {
super();
this.downloadService = downloadService;
this.status = status;
this.cancelled = false;
handler = new Handler();
}
@ -55,5 +58,9 @@ public abstract class Downloader extends Thread {
public DownloadStatus getStatus() {
return status;
}
public void cancel() {
cancelled = true;
}
}

View File

@ -65,14 +65,13 @@ public class HttpDownloader extends Downloader {
publishProgress();
if (AppConfig.DEBUG)
Log.d(TAG, "Starting download");
while ((count = in.read(buffer)) != -1
&& !isInterrupted()) {
while (!cancelled && (count = in.read(buffer)) != -1) {
out.write(buffer, 0, count);
status.setSoFar(status.getSoFar() + count);
status.setProgressPercent((int) (((double) status
.getSoFar() / (double) status.getSize()) * 100));
}
if (isInterrupted()) {
if (cancelled) {
onCancelled();
} else {
onSuccess();