aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2014-02-12 20:33:15 -0800
committerScott Jackson <daneren2005@gmail.com>2014-02-12 20:33:15 -0800
commit6429e37c07e98a464a6eaad90b5808185f5ff0a4 (patch)
treef8c5c89e4a92856548c66593447eb79424ca3fec /src
parentabbc878070d98b1275292fb44a6be77cd74532ad (diff)
downloaddsub-6429e37c07e98a464a6eaad90b5808185f5ff0a4.tar.gz
dsub-6429e37c07e98a464a6eaad90b5808185f5ff0a4.tar.bz2
dsub-6429e37c07e98a464a6eaad90b5808185f5ff0a4.zip
Minor improvements to background tasks
Diffstat (limited to 'src')
-rw-r--r--src/github/daneren2005/dsub/activity/SettingsActivity.java2
-rw-r--r--src/github/daneren2005/dsub/util/BackgroundTask.java14
-rw-r--r--src/github/daneren2005/dsub/util/LoadingTask.java6
-rw-r--r--src/github/daneren2005/dsub/util/TabBackgroundTask.java2
-rw-r--r--src/github/daneren2005/dsub/view/ErrorDialog.java18
5 files changed, 23 insertions, 19 deletions
diff --git a/src/github/daneren2005/dsub/activity/SettingsActivity.java b/src/github/daneren2005/dsub/activity/SettingsActivity.java
index 40ca5570..d496a201 100644
--- a/src/github/daneren2005/dsub/activity/SettingsActivity.java
+++ b/src/github/daneren2005/dsub/activity/SettingsActivity.java
@@ -557,7 +557,7 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
}
@Override
- protected void cancel() {
+ public void cancel() {
super.cancel();
Util.setActiveServer(SettingsActivity.this, previousInstance);
}
diff --git a/src/github/daneren2005/dsub/util/BackgroundTask.java b/src/github/daneren2005/dsub/util/BackgroundTask.java
index 77331c2f..9f5688bc 100644
--- a/src/github/daneren2005/dsub/util/BackgroundTask.java
+++ b/src/github/daneren2005/dsub/util/BackgroundTask.java
@@ -25,6 +25,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.atomic.AtomicBoolean;
import org.xmlpull.v1.XmlPullParserException;
@@ -42,11 +43,11 @@ 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;
+ protected boolean cancelled = false;
+ protected Task task;
private static final int DEFAULT_CONCURRENCY = 5;
- private static final Collection<Thread> threads = Collections.synchronizedCollection(new ArrayList<Thread>(DEFAULT_CONCURRENCY));
+ private static final Collection<Thread> threads = Collections.synchronizedCollection(new ArrayList<Thread>());
protected static final BlockingQueue<BackgroundTask.Task> queue = new LinkedBlockingQueue<BackgroundTask.Task>(10);
private static final Handler handler = new Handler();
@@ -86,7 +87,10 @@ public abstract class BackgroundTask<T> implements ProgressListener {
protected void error(Throwable error) {
Log.w(TAG, "Got exception: " + error, error);
- new ErrorDialog(context, getErrorMessage(error), true);
+ Activity activity = getActivity();
+ if(activity != null) {
+ new ErrorDialog(activity, getErrorMessage(error), true);
+ }
}
protected String getErrorMessage(Throwable error) {
@@ -136,7 +140,7 @@ public abstract class BackgroundTask<T> implements ProgressListener {
private Thread thread;
private AtomicBoolean taskStart = new AtomicBoolean(true);
- private void execute() {
+ private void execute() throws Exception {
if(!taskStart.get()) {
return;
}
diff --git a/src/github/daneren2005/dsub/util/LoadingTask.java b/src/github/daneren2005/dsub/util/LoadingTask.java
index 02668535..c47931ee 100644
--- a/src/github/daneren2005/dsub/util/LoadingTask.java
+++ b/src/github/daneren2005/dsub/util/LoadingTask.java
@@ -38,13 +38,13 @@ public abstract class LoadingTask<T> extends BackgroundTask<T> {
queue.offer(task = new Task() {
@Override
public void onDone(T result) {
- loading.cancel();
+ loading.dismiss();
done(result);
}
@Override
public void onError(Throwable t) {
- loading.cancel();
+ loading.dismiss();
error(t);
}
});
@@ -52,7 +52,7 @@ public abstract class LoadingTask<T> extends BackgroundTask<T> {
@Override
protected boolean isCancelled() {
- return (tabActivity instanceof SubsonicActivity && ((SubsonicActivity)tabActivity).isDestroyed()) || cancelled;
+ return (tabActivity instanceof SubsonicActivity && tabActivity.isDestroyed()) || cancelled;
}
@Override
diff --git a/src/github/daneren2005/dsub/util/TabBackgroundTask.java b/src/github/daneren2005/dsub/util/TabBackgroundTask.java
index c26f2b0a..bd76a2d4 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(tab = new Task() {
+ queue.offer(task = new Task() {
@Override
public void onDone(T result) {
tabFragment.setProgressVisible(false);
diff --git a/src/github/daneren2005/dsub/view/ErrorDialog.java b/src/github/daneren2005/dsub/view/ErrorDialog.java
index bb70ba7b..e72b1934 100644
--- a/src/github/daneren2005/dsub/view/ErrorDialog.java
+++ b/src/github/daneren2005/dsub/view/ErrorDialog.java
@@ -33,13 +33,13 @@ import github.daneren2005.dsub.util.Util;
*/
public class ErrorDialog {
- public ErrorDialog(Context context, int messageId, boolean finishActivityOnCancel) {
- this(context, context.getResources().getString(messageId), finishActivityOnCancel);
+ public ErrorDialog(Activity activity, int messageId, boolean finishActivityOnCancel) {
+ this(activity, activity.getResources().getString(messageId), finishActivityOnCancel);
}
- public ErrorDialog(final Context context, String message, final boolean finishActivityOnClose) {
+ public ErrorDialog(final Activity activity, String message, final boolean finishActivityOnClose) {
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
+ AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setTitle(R.string.error_label);
builder.setMessage(message);
@@ -48,7 +48,7 @@ public class ErrorDialog {
@Override
public void onCancel(DialogInterface dialogInterface) {
if (finishActivityOnClose) {
- restart(context);
+ restart(activity);
}
}
});
@@ -56,7 +56,7 @@ public class ErrorDialog {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (finishActivityOnClose) {
- restart(context);
+ restart(activity);
}
}
});
@@ -68,9 +68,9 @@ public class ErrorDialog {
}
}
- private void restart(Context context) {
- Intent intent = new Intent(context, SubsonicFragmentActivity.class);
+ private void restart(Activity activity) {
+ Intent intent = new Intent(activity, SubsonicFragmentActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- Util.startActivityWithoutTransition(context, intent);
+ Util.startActivityWithoutTransition(activity, intent);
}
}