diff options
Diffstat (limited to 'src/github/daneren2005/dsub')
4 files changed, 35 insertions, 17 deletions
diff --git a/src/github/daneren2005/dsub/util/BackgroundTask.java b/src/github/daneren2005/dsub/util/BackgroundTask.java index ad713ffd..77331c2f 100644 --- a/src/github/daneren2005/dsub/util/BackgroundTask.java +++ b/src/github/daneren2005/dsub/util/BackgroundTask.java @@ -42,6 +42,8 @@ public abstract class BackgroundTask<T> implements ProgressListener { private static final String TAG = BackgroundTask.class.getSimpleName(); private final Context context; + private boolean cancelled = false; + private Task task; private static final int DEFAULT_CONCURRENCY = 5; private static final Collection<Thread> threads = Collections.synchronizedCollection(new ArrayList<Thread>(DEFAULT_CONCURRENCY)); @@ -112,8 +114,14 @@ public abstract class BackgroundTask<T> implements ProgressListener { return error.getClass().getSimpleName(); } + public void cancel() { + cancelled = true; + if(task != null) { + task.cancel(); + } + } protected boolean isCancelled() { - return false; + return cancelled; } @Override @@ -125,7 +133,15 @@ public abstract class BackgroundTask<T> implements ProgressListener { } protected class Task { + private Thread thread; + private AtomicBoolean taskStart = new AtomicBoolean(true); + private void execute() { + if(!taskStart.get()) { + return; + } + + thread = Thread.currentThread(); try { final T result = doInBackground(); if(isCancelled()) { @@ -138,6 +154,11 @@ public abstract class BackgroundTask<T> implements ProgressListener { onDone(result); } }); + } catch(InterruptedException interrupt) { + if(!isCancelled()) { + // Don't exit root thread if task cancelled + throw interrupt; + } } catch(final Throwable t) { if(isCancelled()) { return; @@ -156,6 +177,12 @@ public abstract class BackgroundTask<T> implements ProgressListener { } } + public void cancel() { + taskStart.set(false); + if(thread != null) { + thread.interrupt(); + } + } public void onDone(T result) { done(result); } diff --git a/src/github/daneren2005/dsub/util/LoadingTask.java b/src/github/daneren2005/dsub/util/LoadingTask.java index 55d67dc9..02668535 100644 --- a/src/github/daneren2005/dsub/util/LoadingTask.java +++ b/src/github/daneren2005/dsub/util/LoadingTask.java @@ -14,9 +14,7 @@ public abstract class LoadingTask<T> extends BackgroundTask<T> { private final Activity tabActivity;
private ProgressDialog loading;
- private Thread thread;
private final boolean cancellable;
- private boolean cancelled = false;
public LoadingTask(Activity activity) {
super(activity);
@@ -37,7 +35,7 @@ public abstract class LoadingTask<T> extends BackgroundTask<T> { }
});
- queue.offer(new Task() {
+ queue.offer(task = new Task() {
@Override
public void onDone(T result) {
loading.cancel();
@@ -52,13 +50,6 @@ public abstract class LoadingTask<T> extends BackgroundTask<T> { });
}
- protected void cancel() {
- cancelled = true;
- if (thread != null) {
- thread.interrupt();
- }
- }
-
@Override
protected boolean isCancelled() {
return (tabActivity instanceof SubsonicActivity && ((SubsonicActivity)tabActivity).isDestroyed()) || cancelled;
diff --git a/src/github/daneren2005/dsub/util/SilentBackgroundTask.java b/src/github/daneren2005/dsub/util/SilentBackgroundTask.java index f361954a..a0db7ca4 100644 --- a/src/github/daneren2005/dsub/util/SilentBackgroundTask.java +++ b/src/github/daneren2005/dsub/util/SilentBackgroundTask.java @@ -18,19 +18,19 @@ */ package github.daneren2005.dsub.util; -import android.app.Activity; +import android.content.Context; /** * @author Sindre Mehus */ public abstract class SilentBackgroundTask<T> extends BackgroundTask<T> { - public SilentBackgroundTask(Activity activity) { - super(activity); + public SilentBackgroundTask(Context context) { + super(context); } @Override public void execute() { - queue.offer(new Task()); + queue.offer(task = new Task()); } @Override diff --git a/src/github/daneren2005/dsub/util/TabBackgroundTask.java b/src/github/daneren2005/dsub/util/TabBackgroundTask.java index 8a243d8e..c26f2b0a 100644 --- a/src/github/daneren2005/dsub/util/TabBackgroundTask.java +++ b/src/github/daneren2005/dsub/util/TabBackgroundTask.java @@ -19,7 +19,7 @@ public abstract class TabBackgroundTask<T> extends BackgroundTask<T> { public void execute() {
tabFragment.setProgressVisible(true);
- queue.offer(new Task() {
+ queue.offer(tab = new Task() {
@Override
public void onDone(T result) {
tabFragment.setProgressVisible(false);
@@ -36,7 +36,7 @@ public abstract class TabBackgroundTask<T> extends BackgroundTask<T> { @Override
protected boolean isCancelled() {
- return !tabFragment.isAdded();
+ return !tabFragment.isAdded() || cancelled;
}
@Override
|