From 916f7ebbb9657928f8fc55774cf45db23ac479b9 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Fri, 7 Mar 2014 14:41:29 -0800 Subject: #167 Finish conversion to SilentBackgroundTask for DownloadFile --- .../dsub/service/CachedMusicService.java | 4 +- .../daneren2005/dsub/service/DownloadFile.java | 17 +++-- .../daneren2005/dsub/service/MusicService.java | 4 +- .../daneren2005/dsub/service/RESTMusicService.java | 25 +++---- .../daneren2005/dsub/util/BackgroundTask.java | 30 +++++++- .../daneren2005/dsub/util/CancellableTask.java | 87 ---------------------- src/github/daneren2005/dsub/util/LoadingTask.java | 2 +- .../daneren2005/dsub/util/TabBackgroundTask.java | 2 +- 8 files changed, 55 insertions(+), 116 deletions(-) delete mode 100644 src/github/daneren2005/dsub/util/CancellableTask.java (limited to 'src') 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 parameterNames, List parameterValues, - List
headers, ProgressListener progressListener, CancellableTask task) throws Exception { + List
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 parameterNames, List parameterValues, - List
headers, ProgressListener progressListener, CancellableTask task) throws IOException { + List
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 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 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 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 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 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 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 . - - 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 = new AtomicReference(); - private final AtomicReference cancelListener = new AtomicReference(); - - 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 extends BackgroundTask { } @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 extends BackgroundTask { } @Override - protected boolean isCancelled() { + public boolean isCancelled() { return !tabFragment.isAdded() || cancelled; } -- cgit v1.2.3