aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2014-03-07 14:41:29 -0800
committerScott Jackson <daneren2005@gmail.com>2014-03-07 14:41:29 -0800
commit916f7ebbb9657928f8fc55774cf45db23ac479b9 (patch)
tree252b9af60c75e333b2e6b8271a914937c80c6773
parent44dbfce3af8121df5364ce30e4a98f17d6d0cfcc (diff)
downloaddsub-916f7ebbb9657928f8fc55774cf45db23ac479b9.tar.gz
dsub-916f7ebbb9657928f8fc55774cf45db23ac479b9.tar.bz2
dsub-916f7ebbb9657928f8fc55774cf45db23ac479b9.zip
#167 Finish conversion to SilentBackgroundTask for DownloadFile
-rw-r--r--src/github/daneren2005/dsub/service/CachedMusicService.java4
-rw-r--r--src/github/daneren2005/dsub/service/DownloadFile.java17
-rw-r--r--src/github/daneren2005/dsub/service/MusicService.java4
-rw-r--r--src/github/daneren2005/dsub/service/RESTMusicService.java25
-rw-r--r--src/github/daneren2005/dsub/util/BackgroundTask.java30
-rw-r--r--src/github/daneren2005/dsub/util/CancellableTask.java87
-rw-r--r--src/github/daneren2005/dsub/util/LoadingTask.java2
-rw-r--r--src/github/daneren2005/dsub/util/TabBackgroundTask.java2
8 files changed, 55 insertions, 116 deletions
diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java
index a45f2639..d98a67e1 100644
--- a/src/github/daneren2005/dsub/service/CachedMusicService.java
+++ b/src/github/daneren2005/dsub/service/CachedMusicService.java
@@ -42,7 +42,7 @@ import github.daneren2005.dsub.domain.SearchCritera;
import github.daneren2005.dsub.domain.SearchResult;
import github.daneren2005.dsub.domain.Share;
import github.daneren2005.dsub.domain.Version;
-import github.daneren2005.dsub.util.CancellableTask;
+import github.daneren2005.dsub.util.SilentBackgroundTask;
import github.daneren2005.dsub.util.ProgressListener;
import github.daneren2005.dsub.util.TimeLimitedCache;
import github.daneren2005.dsub.util.FileUtil;
@@ -300,7 +300,7 @@ public class CachedMusicService implements MusicService {
}
@Override
- public HttpResponse getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, CancellableTask task) throws Exception {
+ public HttpResponse getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, SilentBackgroundTask task) throws Exception {
return musicService.getDownloadInputStream(context, song, offset, maxBitrate, task);
}
diff --git a/src/github/daneren2005/dsub/service/DownloadFile.java b/src/github/daneren2005/dsub/service/DownloadFile.java
index d7c3959c..646556df 100644
--- a/src/github/daneren2005/dsub/service/DownloadFile.java
+++ b/src/github/daneren2005/dsub/service/DownloadFile.java
@@ -32,7 +32,7 @@ import android.util.DisplayMetrics;
import android.util.Log;
import github.daneren2005.dsub.domain.MusicDirectory;
import github.daneren2005.dsub.service.parser.SubsonicRESTException;
-import github.daneren2005.dsub.util.CancellableTask;
+import github.daneren2005.dsub.util.SilentBackgroundTask;
import github.daneren2005.dsub.util.FileUtil;
import github.daneren2005.dsub.util.Util;
import github.daneren2005.dsub.util.CacheCleaner;
@@ -151,13 +151,12 @@ public class DownloadFile implements BufferFile {
public synchronized void download() {
preDownload();
- downloadTask.start();
+ downloadTask.execute();
}
public synchronized void downloadNow(MusicService musicService) {
preDownload();
downloadTask.setMusicService(musicService);
- downloadTask.execute();
-
+ downloadTask.doInBackground();
}
private void preDownload() {
FileUtil.createDirectoryForParent(saveFile);
@@ -165,7 +164,7 @@ public class DownloadFile implements BufferFile {
if(!partialFile.exists()) {
bitRate = getActualBitrate();
}
- downloadTask = new DownloadTask();
+ downloadTask = new DownloadTask(context);
}
public synchronized void cancelDownload() {
@@ -320,11 +319,15 @@ public class DownloadFile implements BufferFile {
return "DownloadFile (" + song + ")";
}
- private class DownloadTask extends CancellableTask {
+ private class DownloadTask extends SilentBackgroundTask {
private MusicService musicService;
+ public DownloadTask(Context context) {
+ super(context);
+ }
+
@Override
- public void execute() {
+ public void doInBackground() {
InputStream in = null;
FileOutputStream out = null;
diff --git a/src/github/daneren2005/dsub/service/MusicService.java b/src/github/daneren2005/dsub/service/MusicService.java
index d5fdc251..0522a4be 100644
--- a/src/github/daneren2005/dsub/service/MusicService.java
+++ b/src/github/daneren2005/dsub/service/MusicService.java
@@ -39,7 +39,7 @@ import github.daneren2005.dsub.domain.SearchCritera;
import github.daneren2005.dsub.domain.SearchResult;
import github.daneren2005.dsub.domain.Share;
import github.daneren2005.dsub.domain.Version;
-import github.daneren2005.dsub.util.CancellableTask;
+import github.daneren2005.dsub.util.SilentBackgroundTask;
import github.daneren2005.dsub.util.ProgressListener;
/**
@@ -95,7 +95,7 @@ public interface MusicService {
Bitmap getCoverArt(Context context, MusicDirectory.Entry entry, int size, ProgressListener progressListener) throws Exception;
- HttpResponse getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, CancellableTask task) throws Exception;
+ HttpResponse getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, SilentBackgroundTask task) throws Exception;
String getMusicUrl(Context context, MusicDirectory.Entry song, int maxBitrate) throws Exception;
diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java
index 6c4872e9..d08f0808 100644
--- a/src/github/daneren2005/dsub/service/RESTMusicService.java
+++ b/src/github/daneren2005/dsub/service/RESTMusicService.java
@@ -91,7 +91,7 @@ import github.daneren2005.dsub.service.parser.StarredListParser;
import github.daneren2005.dsub.service.parser.VersionParser;
import github.daneren2005.dsub.service.ssl.SSLSocketFactory;
import github.daneren2005.dsub.service.ssl.TrustSelfSignedStrategy;
-import github.daneren2005.dsub.util.CancellableTask;
+import github.daneren2005.dsub.util.SilentBackgroundTask;
import github.daneren2005.dsub.util.Constants;
import github.daneren2005.dsub.util.FileUtil;
import github.daneren2005.dsub.util.ProgressListener;
@@ -686,7 +686,7 @@ public class RESTMusicService implements MusicService {
}
@Override
- public HttpResponse getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, CancellableTask task) throws Exception {
+ public HttpResponse getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, SilentBackgroundTask task) throws Exception {
String url = getRestUrl(context, "stream");
@@ -1328,7 +1328,7 @@ public class RESTMusicService implements MusicService {
private HttpResponse getResponseForURL(Context context, String url, HttpParams requestParams,
List<String> parameterNames, List<Object> parameterValues,
- List<Header> headers, ProgressListener progressListener, CancellableTask task) throws Exception {
+ List<Header> headers, ProgressListener progressListener, SilentBackgroundTask task) throws Exception {
Log.d(TAG, "Connections in pool: " + connManager.getConnectionsInPool());
// If not too many parameters, extract them to the URL rather than relying on the HTTP POST request being
@@ -1351,7 +1351,7 @@ public class RESTMusicService implements MusicService {
private HttpResponse executeWithRetry(Context context, String url, String originalUrl, HttpParams requestParams,
List<String> parameterNames, List<Object> parameterValues,
- List<Header> headers, ProgressListener progressListener, CancellableTask task) throws IOException {
+ List<Header> headers, ProgressListener progressListener, SilentBackgroundTask task) throws IOException {
// Strip out sensitive information from log
Log.i(TAG, stripUrlInfo(url));
@@ -1370,19 +1370,14 @@ public class RESTMusicService implements MusicService {
if (task != null) {
// Attempt to abort the HTTP request if the task is cancelled.
- task.setOnCancelListener(new CancellableTask.OnCancelListener() {
+ task.setOnCancelListener(new SilentBackgroundTask.OnCancelListener() {
@Override
public void onCancel() {
- new Thread(new Runnable() {
- public void run() {
- try {
- cancelled.set(true);
- request.abort();
- } catch(Exception e) {
- Log.e(TAG, "Failed to stop http task");
- }
- }
- }).start();
+ try {
+ request.abort();
+ } catch(Exception e) {
+ Log.e(TAG, "Failed to stop http task");
+ }
}
});
}
diff --git a/src/github/daneren2005/dsub/util/BackgroundTask.java b/src/github/daneren2005/dsub/util/BackgroundTask.java
index fbeaea74..4760577f 100644
--- a/src/github/daneren2005/dsub/util/BackgroundTask.java
+++ b/src/github/daneren2005/dsub/util/BackgroundTask.java
@@ -45,6 +45,7 @@ public abstract class BackgroundTask<T> implements ProgressListener {
private final Context context;
protected boolean cancelled = false;
+ protected OnCancelListener cancelListener;
protected Task task;
private static final int DEFAULT_CONCURRENCY = 5;
@@ -138,10 +139,24 @@ public abstract class BackgroundTask<T> implements ProgressListener {
if(task != null) {
task.cancel();
}
+ if(cancelListener != null) {
+ cancelListener.onCancel();
+ }
}
- protected boolean isCancelled() {
+ public boolean isCancelled() {
return cancelled;
}
+ public void setOnCancelListener(OnCancelListener listener) {
+ cancelListener = listener;
+ }
+
+ public boolean isRunning() {
+ if(task == null) {
+ return false;
+ } else {
+ return task.isRunning();
+ }
+ }
@Override
public abstract void updateProgress(final String message);
@@ -172,8 +187,11 @@ public abstract class BackgroundTask<T> implements ProgressListener {
@Override
public void run() {
onDone(result);
+ taskStart.set(false);
}
});
+ } else {
+ taskStart.set(false);
}
} catch(InterruptedException interrupt) {
if(taskStart.get()) {
@@ -191,11 +209,14 @@ public abstract class BackgroundTask<T> implements ProgressListener {
public void run() {
try {
onError(t);
+ taskStart.set(false);
} catch(Exception e) {
// Don't care
}
}
});
+ } else {
+ taskStart.set(false);
}
}
}
@@ -212,6 +233,10 @@ public abstract class BackgroundTask<T> implements ProgressListener {
public void onError(Throwable t) {
error(t);
}
+
+ public boolean isRunning() {
+ return taskStart.get();
+ }
}
private class TaskRunnable implements Runnable {
@@ -238,4 +263,7 @@ public abstract class BackgroundTask<T> implements ProgressListener {
}
}
+ public static interface OnCancelListener {
+ void onCancel();
+ }
}
diff --git a/src/github/daneren2005/dsub/util/CancellableTask.java b/src/github/daneren2005/dsub/util/CancellableTask.java
deleted file mode 100644
index d519f276..00000000
--- a/src/github/daneren2005/dsub/util/CancellableTask.java
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- This file is part of Subsonic.
-
- Subsonic is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- Subsonic is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
-
- Copyright 2009 (C) Sindre Mehus
- */
-package github.daneren2005.dsub.util;
-
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicReference;
-
-import android.util.Log;
-
-/**
- * @author Sindre Mehus
- * @version $Id$
- */
-public abstract class CancellableTask {
-
- private static final String TAG = CancellableTask.class.getSimpleName();
-
- private final AtomicBoolean running = new AtomicBoolean(false);
- private final AtomicBoolean cancelled = new AtomicBoolean(false);
- private final AtomicReference<Thread> thread = new AtomicReference<Thread>();
- private final AtomicReference<OnCancelListener> cancelListener = new AtomicReference<OnCancelListener>();
-
- public void cancel() {
- Log.i(TAG, "Cancelling " + CancellableTask.this);
- cancelled.set(true);
-
- OnCancelListener listener = cancelListener.get();
- if (listener != null) {
- try {
- listener.onCancel();
- } catch (Throwable x) {
- Log.w(TAG, "Error when invoking OnCancelListener.", x);
- }
- }
- }
-
- public boolean isCancelled() {
- return cancelled.get();
- }
-
- public void setOnCancelListener(OnCancelListener listener) {
- cancelListener.set(listener);
- }
-
- public boolean isRunning() {
- return running.get();
- }
-
- public abstract void execute();
-
- public void start() {
- thread.set(new Thread("CancellableTask") {
- @Override
- public void run() {
- running.set(true);
- Log.i(TAG, "Starting thread for " + CancellableTask.this);
- try {
- execute();
- } finally {
- running.set(false);
- Log.i(TAG, "Stopping thread for " + CancellableTask.this);
- }
- }
- });
- thread.get().start();
- }
-
- public static interface OnCancelListener {
- void onCancel();
- }
-}
diff --git a/src/github/daneren2005/dsub/util/LoadingTask.java b/src/github/daneren2005/dsub/util/LoadingTask.java
index 17215abd..f774aa5b 100644
--- a/src/github/daneren2005/dsub/util/LoadingTask.java
+++ b/src/github/daneren2005/dsub/util/LoadingTask.java
@@ -51,7 +51,7 @@ public abstract class LoadingTask<T> extends BackgroundTask<T> {
}
@Override
- protected boolean isCancelled() {
+ public boolean isCancelled() {
return (tabActivity instanceof SubsonicActivity && ((SubsonicActivity) tabActivity).isDestroyedCompat()) || cancelled;
}
diff --git a/src/github/daneren2005/dsub/util/TabBackgroundTask.java b/src/github/daneren2005/dsub/util/TabBackgroundTask.java
index bd76a2d4..022bcaa7 100644
--- a/src/github/daneren2005/dsub/util/TabBackgroundTask.java
+++ b/src/github/daneren2005/dsub/util/TabBackgroundTask.java
@@ -35,7 +35,7 @@ public abstract class TabBackgroundTask<T> extends BackgroundTask<T> {
}
@Override
- protected boolean isCancelled() {
+ public boolean isCancelled() {
return !tabFragment.isAdded() || cancelled;
}