From f4886ea804b31975f46d914e9502075b8981536e Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sun, 15 Mar 2015 16:34:15 -0700 Subject: #428 Add restore logic with confirmation --- .../dsub/activity/SubsonicFragmentActivity.java | 47 ++++++++++++++++++++++ src/github/daneren2005/dsub/domain/ServerInfo.java | 4 ++ .../daneren2005/dsub/service/DownloadService.java | 5 +++ .../service/DownloadServiceLifecycleSupport.java | 13 +++++- 4 files changed, 67 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java b/src/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java index 672d6711..68666b84 100644 --- a/src/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java +++ b/src/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java @@ -23,6 +23,7 @@ import android.accounts.AccountManager; import android.app.Dialog; import android.content.ContentResolver; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.TypedArray; @@ -37,12 +38,15 @@ import android.widget.ImageButton; import android.widget.TextView; import java.io.File; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.MusicDirectory; +import github.daneren2005.dsub.domain.PlayerQueue; import github.daneren2005.dsub.domain.PlayerState; import github.daneren2005.dsub.domain.ServerInfo; import github.daneren2005.dsub.fragments.AdminFragment; @@ -478,6 +482,10 @@ public class SubsonicFragmentActivity extends SubsonicActivity { if(!Util.isOffline(this) && ServerInfo.canBookmark(this)) { loadBookmarks(); } + // If we are on Subsonic 5.2+, save play queue + if(ServerInfo.canSavePlayQueue(this) && !Util.isOffline(this)) { + loadRemotePlayQueue(); + } sessionInitialized = true; } @@ -536,6 +544,45 @@ public class SubsonicFragmentActivity extends SubsonicActivity { } }.execute(); } + private void loadRemotePlayQueue() { + final Context context = this; + new SilentBackgroundTask(this) { + @Override + protected Void doInBackground() throws Throwable { + try { + MusicService musicService = MusicServiceFactory.getMusicService(context); + PlayerQueue remoteState = musicService.getPlayQueue(context, null); + + // Make sure we wait until download service is ready + DownloadService downloadService = getDownloadService(); + while(downloadService == null) { + Util.sleepQuietly(100L); + downloadService = getDownloadService(); + } + + // If we had a remote state and it's changed is more recent than our existing state + if(remoteState != null) { + Date localChange = downloadService.getLastStateChanged(); + if(localChange == null || localChange.after(remoteState.changed)) { + promptRestoreFromRemoteQueue(remoteState); + } + } + } catch (Exception e) { + Log.e(TAG, "Failed to get playing queue to server", e); + } + + return null; + } + }.execute(); + } + private void promptRestoreFromRemoteQueue(final PlayerQueue remoteState) { + Util.confirmDialog(this, R.string.download_restore_play_queue, new SimpleDateFormat("MMM dd hh:mm").format(remoteState.changed), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + getDownloadService().restore(remoteState.songs, null, remoteState.currentPlayingIndex, remoteState.currentPlayingPosition); + } + }); + } private void createAccount() { final Context context = this; diff --git a/src/github/daneren2005/dsub/domain/ServerInfo.java b/src/github/daneren2005/dsub/domain/ServerInfo.java index a0ce23e1..2394a233 100644 --- a/src/github/daneren2005/dsub/domain/ServerInfo.java +++ b/src/github/daneren2005/dsub/domain/ServerInfo.java @@ -202,4 +202,8 @@ public class ServerInfo implements Serializable { public static boolean canBookmark(Context context) { return checkServerVersion(context, "1.9"); } + + public static boolean canSavePlayQueue(Context context) { + return ServerInfo.checkServerVersion(context, "1.12") && !ServerInfo.isMadsonic(context); + } } diff --git a/src/github/daneren2005/dsub/service/DownloadService.java b/src/github/daneren2005/dsub/service/DownloadService.java index a6b66d5a..5efa48a0 100644 --- a/src/github/daneren2005/dsub/service/DownloadService.java +++ b/src/github/daneren2005/dsub/service/DownloadService.java @@ -58,6 +58,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; +import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Timer; @@ -463,6 +464,10 @@ public class DownloadService extends Service { suggestedPlaylistId = prefs.getString(Constants.PREFERENCES_KEY_PLAYLIST_ID, null); } + public synchronized Date getLastStateChanged() { + return lifecycleSupport.getLastChange(); + } + public synchronized void setRemovePlayed(boolean enabled) { removePlayed = enabled; if(removePlayed) { diff --git a/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java b/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java index 8af16a76..31844210 100644 --- a/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java +++ b/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java @@ -67,6 +67,7 @@ public class DownloadServiceLifecycleSupport { private final AtomicBoolean setup = new AtomicBoolean(false); private long lastPressTime = 0; private SilentBackgroundTask currentSavePlayQueueTask = null; + private Date lastChange = null; /** * This receiver manages the intent that could come from other applications. @@ -292,13 +293,13 @@ public class DownloadServiceLifecycleSupport { if(currentPlaying != null) { state.renameCurrent = currentPlaying.isWorkDone() && !currentPlaying.isCompleteFileAvailable(); } - state.changed = new Date(); + state.changed = lastChange = new Date(); Log.i(TAG, "Serialized currentPlayingIndex: " + state.currentPlayingIndex + ", currentPlayingPosition: " + state.currentPlayingPosition); FileUtil.serialize(downloadService, state, FILENAME_DOWNLOADS_SER); // If we are on Subsonic 5.2+, save play queue - if(ServerInfo.checkServerVersion(downloadService, "1.12") && !ServerInfo.isMadsonic(downloadService) && !Util.isOffline(downloadService) && state.songs.size() > 0) { + if(ServerInfo.canSavePlayQueue(downloadService) && !Util.isOffline(downloadService) && state.songs.size() > 0) { // Cancel any currently running tasks if(currentSavePlayQueueTask != null) { currentSavePlayQueueTask.cancel(); @@ -347,6 +348,14 @@ public class DownloadServiceLifecycleSupport { } downloadService.restore(state.songs, state.toDelete, state.currentPlayingIndex, state.currentPlayingPosition); + + if(state != null) { + lastChange = state.changed; + } + } + + public Date getLastChange() { + return lastChange; } private void handleKeyEvent(KeyEvent event) { -- cgit v1.2.3