diff options
author | Scott Jackson <daneren2005@gmail.com> | 2015-07-13 19:36:58 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2015-07-13 19:37:09 -0700 |
commit | a12581051e8bce57c085a50082c47edbe5848036 (patch) | |
tree | 14ddeb684cbea13550ae3c46d0b2c0215d172946 /app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java | |
parent | ad11c8479efb9ebce977edee15cdeb2838deba63 (diff) | |
download | dsub-a12581051e8bce57c085a50082c47edbe5848036.tar.gz dsub-a12581051e8bce57c085a50082c47edbe5848036.tar.bz2 dsub-a12581051e8bce57c085a50082c47edbe5848036.zip |
#527 Change from polling to using DownloadService events
Diffstat (limited to 'app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java')
-rw-r--r-- | app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java | 152 |
1 files changed, 70 insertions, 82 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java b/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java index f0685a5f..e5144f6f 100644 --- a/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java +++ b/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java @@ -45,6 +45,7 @@ import com.sothree.slidinguppanel.SlidingUpPanelLayout; import java.io.File; import java.util.Date; +import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -82,13 +83,12 @@ import github.daneren2005.dsub.view.ChangeLog; /** * Created by Scott on 10/14/13. */ -public class SubsonicFragmentActivity extends SubsonicActivity { +public class SubsonicFragmentActivity extends SubsonicActivity implements DownloadService.OnSongChangedListener { private static String TAG = SubsonicFragmentActivity.class.getSimpleName(); private static boolean infoDialogDisplayed; private static boolean sessionInitialized = false; private static long ALLOWED_SKEW = 30000L; - private Handler handler = new Handler(); private SlidingUpPanelLayout slideUpPanel; private SlidingUpPanelLayout.PanelSlideListener panelSlideListener; private NowPlayingFragment nowPlayingFragment; @@ -96,7 +96,6 @@ public class SubsonicFragmentActivity extends SubsonicActivity { private Toolbar mainToolbar; private Toolbar nowPlayingToolbar; - private ScheduledExecutorService executorService; private View bottomBar; private ImageView coverArtView; private TextView trackView; @@ -292,11 +291,6 @@ public class SubsonicFragmentActivity extends SubsonicActivity { getDownloadService().previous(); return null; } - - @Override - protected void done(Void result) { - update(); - } }.execute(); } }); @@ -317,11 +311,6 @@ public class SubsonicFragmentActivity extends SubsonicActivity { return null; } - - @Override - protected void done(Void result) { - update(); - } }.execute(); } }); @@ -340,11 +329,6 @@ public class SubsonicFragmentActivity extends SubsonicActivity { getDownloadService().next(); return null; } - - @Override - protected void done(Void result) { - update(); - } }.execute(); } }); @@ -406,18 +390,6 @@ public class SubsonicFragmentActivity extends SubsonicActivity { public void onResume() { super.onResume(); - Runnable runnable = new Runnable() { - @Override - public void run() { - handler.post(new Runnable() { - @Override - public void run() { - update(); - } - }); - } - }; - if(getIntent().hasExtra(Constants.INTENT_EXTRA_VIEW_ALBUM)) { SubsonicFragment fragment = new SelectDirectoryFragment(); Bundle args = new Bundle(); @@ -437,15 +409,21 @@ public class SubsonicFragmentActivity extends SubsonicActivity { } createAccount(); - - executorService = Executors.newSingleThreadScheduledExecutor(); - executorService.scheduleWithFixedDelay(runnable, 0L, 1000L, TimeUnit.MILLISECONDS); + runWhenServiceAvailable(new Runnable() { + @Override + public void run() { + getDownloadService().addOnSongChangedListener(SubsonicFragmentActivity.this, true); + } + }); } @Override public void onPause() { super.onPause(); - executorService.shutdown(); + DownloadService downloadService = getDownloadService(); + if(downloadService != null) { + downloadService.removeOnSongChangeListener(this); + } } @Override @@ -638,54 +616,6 @@ public class SubsonicFragmentActivity extends SubsonicActivity { } } - private void update() { - DownloadService downloadService = getDownloadService(); - if (downloadService == null) { - return; - } - - DownloadFile current = downloadService.getCurrentPlaying(); - PlayerState state = downloadService.getPlayerState(); - if(current == currentPlaying && state == currentState) { - if(current == null && slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.COLLAPSED) { - slideUpPanel.setPanelState(SlidingUpPanelLayout.PanelState.HIDDEN); - } - return; - } else { - currentPlaying = current; - currentState = state; - } - - MusicDirectory.Entry song = null; - if (current != null) { - song = current.getSong(); - trackView.setText(song.getTitle()); - artistView.setText(song.getArtist()); - - if(slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.HIDDEN) { - slideUpPanel.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED); - } - } else if(slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.COLLAPSED) { - slideUpPanel.setPanelState(SlidingUpPanelLayout.PanelState.HIDDEN); - } - - if (coverArtView != null) { - int height = coverArtView.getHeight(); - if (height <= 0) { - int[] attrs = new int[]{R.attr.actionBarSize}; - TypedArray typedArray = this.obtainStyledAttributes(attrs); - height = typedArray.getDimensionPixelSize(0, 0); - typedArray.recycle(); - } - getImageLoader().loadImage(coverArtView, song, false, height, false); - } - - int[] attrs = new int[]{(state == PlayerState.STARTED) ? R.attr.actionbar_pause : R.attr.actionbar_start}; - TypedArray typedArray = this.obtainStyledAttributes(attrs); - startButton.setImageResource(typedArray.getResourceId(0, 0)); - typedArray.recycle(); - } - public void checkUpdates() { try { String version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName; @@ -892,4 +822,62 @@ public class SubsonicFragmentActivity extends SubsonicActivity { public Toolbar getActiveToolbar() { return slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED ? nowPlayingToolbar : mainToolbar; } + + @Override + public void onSongChanged(DownloadFile currentPlaying, int currentPlayingIndex) { + DownloadService downloadService = getDownloadService(); + PlayerState state = downloadService.getPlayerState(); + if(currentPlaying == this.currentPlaying && state == currentState) { + if(currentPlaying == null && slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.COLLAPSED) { + slideUpPanel.setPanelState(SlidingUpPanelLayout.PanelState.HIDDEN); + } + return; + } else { + this.currentPlaying = currentPlaying; + } + + MusicDirectory.Entry song = null; + if (currentPlaying != null) { + song = currentPlaying.getSong(); + trackView.setText(song.getTitle()); + artistView.setText(song.getArtist()); + + if(slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.HIDDEN) { + slideUpPanel.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED); + } + } else if(slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.COLLAPSED) { + slideUpPanel.setPanelState(SlidingUpPanelLayout.PanelState.HIDDEN); + } + + if (coverArtView != null) { + int height = coverArtView.getHeight(); + if (height <= 0) { + int[] attrs = new int[]{R.attr.actionBarSize}; + TypedArray typedArray = this.obtainStyledAttributes(attrs); + height = typedArray.getDimensionPixelSize(0, 0); + typedArray.recycle(); + } + getImageLoader().loadImage(coverArtView, song, false, height, false); + } + } + + @Override + public void onSongsChanged(List<DownloadFile> songs, DownloadFile currentPlaying, int currentPlayingIndex) { + if(this.currentPlaying != currentPlaying) { + onSongChanged(currentPlaying, currentPlayingIndex); + } + } + + @Override + public void onSongProgress(DownloadFile currentPlaying, int millisPlayed, Integer duration, boolean isSeekable) { + + } + + @Override + public void onStateUpdate(DownloadFile downloadFile, PlayerState playerState) { + int[] attrs = new int[]{(playerState == PlayerState.STARTED) ? R.attr.actionbar_pause : R.attr.actionbar_start}; + TypedArray typedArray = this.obtainStyledAttributes(attrs); + startButton.setImageResource(typedArray.getResourceId(0, 0)); + typedArray.recycle(); + } } |