From ddefc94516d121eb6b6681237a73f7eebdc7250d Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sat, 28 Jun 2014 12:20:52 -0700 Subject: Fix for switching now playing not stopping old download --- .../daneren2005/dsub/service/DownloadFile.java | 8 +++++--- .../daneren2005/dsub/service/RESTMusicService.java | 10 +++++---- .../daneren2005/dsub/util/BackgroundTask.java | 24 +++++++++------------- src/github/daneren2005/dsub/util/ImageLoader.java | 4 ++-- src/github/daneren2005/dsub/util/LoadingTask.java | 4 ++-- .../daneren2005/dsub/util/TabBackgroundTask.java | 2 +- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/github/daneren2005/dsub/service/DownloadFile.java b/src/github/daneren2005/dsub/service/DownloadFile.java index c82607f5..4fb6bb42 100644 --- a/src/github/daneren2005/dsub/service/DownloadFile.java +++ b/src/github/daneren2005/dsub/service/DownloadFile.java @@ -237,7 +237,7 @@ public class DownloadFile implements BufferFile { @Override public synchronized void onResume() { - if(!isFailedMax() && !isDownloading()) { + if(!isFailedMax() && !isDownloading() && !isDownloadCancelled()) { download(); } } @@ -477,8 +477,10 @@ public class DownloadFile implements BufferFile { } // Only run these if not interrupted, ie: cancelled - new CacheCleaner(context, DownloadService.getInstance()).cleanSpace(); - checkDownloads(); + if(!isCancelled()) { + new CacheCleaner(context, DownloadService.getInstance()).cleanSpace(); + checkDownloads(); + } return null; } diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java index 3299ec4b..43d443d2 100644 --- a/src/github/daneren2005/dsub/service/RESTMusicService.java +++ b/src/github/daneren2005/dsub/service/RESTMusicService.java @@ -92,6 +92,7 @@ import github.daneren2005.dsub.service.parser.UserParser; 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.BackgroundTask; import github.daneren2005.dsub.util.SilentBackgroundTask; import github.daneren2005.dsub.util.Constants; import github.daneren2005.dsub.util.FileUtil; @@ -1526,7 +1527,7 @@ public class RESTMusicService implements MusicService { HttpConnectionParams.setSoTimeout(newParams, networkTimeout); httpClient.setParams(newParams); - final AtomicReference cancelled = new AtomicReference(false); + final AtomicReference isCancelled = new AtomicReference(false); int attempts = 0; while (true) { attempts++; @@ -1535,10 +1536,11 @@ public class RESTMusicService implements MusicService { if (task != null) { // Attempt to abort the HTTP request if the task is cancelled. - task.setOnCancelListener(new SilentBackgroundTask.OnCancelListener() { + task.setOnCancelListener(new BackgroundTask.OnCancelListener() { @Override public void onCancel() { try { + isCancelled.set(true); request.abort(); } catch(Exception e) { Log.e(TAG, "Failed to stop http task"); @@ -1579,14 +1581,14 @@ public class RESTMusicService implements MusicService { return response; } catch (IOException x) { request.abort(); - if (attempts >= HTTP_REQUEST_MAX_ATTEMPTS || cancelled.get()) { + if (attempts >= HTTP_REQUEST_MAX_ATTEMPTS || isCancelled.get()) { throw x; } if (progressListener != null) { String msg = context.getResources().getString(R.string.music_service_retry, attempts, HTTP_REQUEST_MAX_ATTEMPTS - 1); progressListener.updateProgress(msg); } - Log.w(TAG, "Got IOException (" + attempts + "), will retry", x); + Log.w(TAG, "Got IOException " + x + " (" + attempts + "), will retry"); increaseTimeouts(requestParams); Thread.sleep(2000L); } diff --git a/src/github/daneren2005/dsub/util/BackgroundTask.java b/src/github/daneren2005/dsub/util/BackgroundTask.java index ad9d89c0..f64acbff 100644 --- a/src/github/daneren2005/dsub/util/BackgroundTask.java +++ b/src/github/daneren2005/dsub/util/BackgroundTask.java @@ -44,7 +44,7 @@ public abstract class BackgroundTask implements ProgressListener { private static final String TAG = BackgroundTask.class.getSimpleName(); private final Context context; - protected boolean cancelled = false; + protected AtomicBoolean cancelled = new AtomicBoolean(false); protected OnCancelListener cancelListener; protected Task task; @@ -135,24 +135,20 @@ public abstract class BackgroundTask implements ProgressListener { } public void cancel() { - if(cancelled) { - // Already cancelled, don't do anything - return; - } + if(cancelled.compareAndSet(false, true)) { + if(task != null && task.isRunning()) { + if(cancelListener != null) { + cancelListener.onCancel(); + } else { + task.cancel(); + } - cancelled = true; - if(task != null && task.isRunning()) { - if(cancelListener != null) { - cancelListener.onCancel(); - } else { - task.cancel(); + task = null; } - - task = null; } } public boolean isCancelled() { - return cancelled; + return cancelled.get(); } public void setOnCancelListener(OnCancelListener listener) { cancelListener = listener; diff --git a/src/github/daneren2005/dsub/util/ImageLoader.java b/src/github/daneren2005/dsub/util/ImageLoader.java index f9c5fed5..dc10ebba 100644 --- a/src/github/daneren2005/dsub/util/ImageLoader.java +++ b/src/github/daneren2005/dsub/util/ImageLoader.java @@ -292,7 +292,7 @@ public class ImageLoader { mDrawable = Util.createDrawableFromBitmap(mContext, bitmap); } catch (Throwable x) { Log.e(TAG, "Failed to download album art.", x); - cancelled = true; + cancelled.set(true); } return null; @@ -358,7 +358,7 @@ public class ImageLoader { } } catch (Throwable x) { Log.e(TAG, "Failed to download album art.", x); - cancelled = true; + cancelled.set(true); } return null; diff --git a/src/github/daneren2005/dsub/util/LoadingTask.java b/src/github/daneren2005/dsub/util/LoadingTask.java index f774aa5b..6c3385d2 100644 --- a/src/github/daneren2005/dsub/util/LoadingTask.java +++ b/src/github/daneren2005/dsub/util/LoadingTask.java @@ -52,12 +52,12 @@ public abstract class LoadingTask extends BackgroundTask { @Override public boolean isCancelled() { - return (tabActivity instanceof SubsonicActivity && ((SubsonicActivity) tabActivity).isDestroyedCompat()) || cancelled; + return (tabActivity instanceof SubsonicActivity && ((SubsonicActivity) tabActivity).isDestroyedCompat()) || cancelled.get(); } @Override public void updateProgress(final String message) { - if(!cancelled) { + if(!cancelled.get()) { getHandler().post(new Runnable() { @Override public void run() { diff --git a/src/github/daneren2005/dsub/util/TabBackgroundTask.java b/src/github/daneren2005/dsub/util/TabBackgroundTask.java index 022bcaa7..b0a24b28 100644 --- a/src/github/daneren2005/dsub/util/TabBackgroundTask.java +++ b/src/github/daneren2005/dsub/util/TabBackgroundTask.java @@ -36,7 +36,7 @@ public abstract class TabBackgroundTask extends BackgroundTask { @Override public boolean isCancelled() { - return !tabFragment.isAdded() || cancelled; + return !tabFragment.isAdded() || cancelled.get(); } @Override -- cgit v1.2.3