diff options
author | Scott Jackson <daneren2005@gmail.com> | 2014-02-12 15:41:39 -0800 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2014-02-12 15:41:39 -0800 |
commit | 788d8208e0ca487d0549bb07e2cf75ee472134a6 (patch) | |
tree | f341c4f1347e30e0ee3cebeb5e3c3036249b8728 /src | |
parent | 1503da8ce1e116332bd81abc25fa550da13a43cb (diff) | |
download | dsub-788d8208e0ca487d0549bb07e2cf75ee472134a6.tar.gz dsub-788d8208e0ca487d0549bb07e2cf75ee472134a6.tar.bz2 dsub-788d8208e0ca487d0549bb07e2cf75ee472134a6.zip |
BackgroundTask should reference context instead of Activity
Diffstat (limited to 'src')
-rw-r--r-- | src/github/daneren2005/dsub/util/BackgroundTask.java | 23 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/view/ErrorDialog.java | 15 |
2 files changed, 20 insertions, 18 deletions
diff --git a/src/github/daneren2005/dsub/util/BackgroundTask.java b/src/github/daneren2005/dsub/util/BackgroundTask.java index 4e71188f..ad713ffd 100644 --- a/src/github/daneren2005/dsub/util/BackgroundTask.java +++ b/src/github/daneren2005/dsub/util/BackgroundTask.java @@ -29,6 +29,7 @@ import java.util.concurrent.LinkedBlockingQueue; import org.xmlpull.v1.XmlPullParserException; import android.app.Activity; +import android.content.Context; import android.os.Handler; import android.util.Log; import github.daneren2005.dsub.R; @@ -40,15 +41,15 @@ import github.daneren2005.dsub.view.ErrorDialog; public abstract class BackgroundTask<T> implements ProgressListener { private static final String TAG = BackgroundTask.class.getSimpleName(); - private final Activity activity; + private final Context context; private static final int DEFAULT_CONCURRENCY = 5; private static final Collection<Thread> threads = Collections.synchronizedCollection(new ArrayList<Thread>(DEFAULT_CONCURRENCY)); protected static final BlockingQueue<BackgroundTask.Task> queue = new LinkedBlockingQueue<BackgroundTask.Task>(10); private static final Handler handler = new Handler(); - public BackgroundTask(Activity activity) { - this.activity = activity; + public BackgroundTask(Context context) { + this.context = context; if(threads.isEmpty()) { for(int i = 0; i < DEFAULT_CONCURRENCY; i++) { @@ -68,7 +69,7 @@ public abstract class BackgroundTask<T> implements ProgressListener { } protected Activity getActivity() { - return activity; + return (context instanceof Activity) ? ((Activity) context) : null; } protected Handler getHandler() { @@ -83,25 +84,25 @@ public abstract class BackgroundTask<T> implements ProgressListener { protected void error(Throwable error) { Log.w(TAG, "Got exception: " + error, error); - new ErrorDialog(activity, getErrorMessage(error), true); + new ErrorDialog(context, getErrorMessage(error), true); } protected String getErrorMessage(Throwable error) { - if (error instanceof IOException && !Util.isNetworkConnected(activity)) { - return activity.getResources().getString(R.string.background_task_no_network); + if (error instanceof IOException && !Util.isNetworkConnected(context)) { + return context.getResources().getString(R.string.background_task_no_network); } if (error instanceof FileNotFoundException) { - return activity.getResources().getString(R.string.background_task_not_found); + return context.getResources().getString(R.string.background_task_not_found); } if (error instanceof IOException) { - return activity.getResources().getString(R.string.background_task_network_error); + return context.getResources().getString(R.string.background_task_network_error); } if (error instanceof XmlPullParserException) { - return activity.getResources().getString(R.string.background_task_parse_error); + return context.getResources().getString(R.string.background_task_parse_error); } String message = error.getMessage(); @@ -120,7 +121,7 @@ public abstract class BackgroundTask<T> implements ProgressListener { @Override public void updateProgress(int messageId) { - updateProgress(activity.getResources().getString(messageId)); + updateProgress(context.getResources().getString(messageId)); } protected class Task { diff --git a/src/github/daneren2005/dsub/view/ErrorDialog.java b/src/github/daneren2005/dsub/view/ErrorDialog.java index 246b3756..bb70ba7b 100644 --- a/src/github/daneren2005/dsub/view/ErrorDialog.java +++ b/src/github/daneren2005/dsub/view/ErrorDialog.java @@ -20,6 +20,7 @@ package github.daneren2005.dsub.view; import android.app.Activity; import android.app.AlertDialog; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -32,13 +33,13 @@ import github.daneren2005.dsub.util.Util; */ public class ErrorDialog { - public ErrorDialog(Activity activity, int messageId, boolean finishActivityOnCancel) { - this(activity, activity.getResources().getString(messageId), finishActivityOnCancel); + public ErrorDialog(Context context, int messageId, boolean finishActivityOnCancel) { + this(context, context.getResources().getString(messageId), finishActivityOnCancel); } - public ErrorDialog(final Activity activity, String message, final boolean finishActivityOnClose) { + public ErrorDialog(final Context context, String message, final boolean finishActivityOnClose) { - AlertDialog.Builder builder = new AlertDialog.Builder(activity); + AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setTitle(R.string.error_label); builder.setMessage(message); @@ -47,7 +48,7 @@ public class ErrorDialog { @Override public void onCancel(DialogInterface dialogInterface) { if (finishActivityOnClose) { - restart(activity); + restart(context); } } }); @@ -55,7 +56,7 @@ public class ErrorDialog { @Override public void onClick(DialogInterface dialogInterface, int i) { if (finishActivityOnClose) { - restart(activity); + restart(context); } } }); @@ -67,7 +68,7 @@ public class ErrorDialog { } } - private void restart(Activity context) { + private void restart(Context context) { Intent intent = new Intent(context, SubsonicFragmentActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); Util.startActivityWithoutTransition(context, intent); |