Optimizing

This commit is contained in:
Karim Abou Zeid 2015-12-22 20:47:57 +01:00
commit 4b42ef6b9d
3 changed files with 74 additions and 24 deletions

View file

@ -101,30 +101,51 @@ public class MusicPlayerRemote {
} }
} }
/**
* Async
*/
public static void playSongAt(final int position) { public static void playSongAt(final int position) {
if (musicService != null) { if (musicService != null) {
musicService.playSongAt(position); musicService.playSongAt(position);
} }
} }
/**
* Async
*/
public static void setPosition(final int position) {
if (musicService != null) {
musicService.setPosition(position);
}
}
public static void pauseSong() { public static void pauseSong() {
if (musicService != null) { if (musicService != null) {
musicService.pause(); musicService.pause();
} }
} }
/**
* Async
*/
public static void playNextSong() { public static void playNextSong() {
if (musicService != null) { if (musicService != null) {
musicService.playNextSong(true); musicService.playNextSong(true);
} }
} }
/**
* Async
*/
public static void playPreviousSong() { public static void playPreviousSong() {
if (musicService != null) { if (musicService != null) {
musicService.playPreviousSong(true); musicService.playPreviousSong(true);
} }
} }
/**
* Async
*/
public static void back() { public static void back() {
if (musicService != null) { if (musicService != null) {
musicService.back(true); musicService.back(true);
@ -141,12 +162,42 @@ public class MusicPlayerRemote {
} }
} }
public static void openQueue(final ArrayList<Song> playingQueue, final int startPosition, final boolean startPlaying) { /**
if (musicService != null) { * Async
musicService.openAndPlayQueue(playingQueue, startPosition, startPlaying); */
public static void openQueue(final ArrayList<Song> queue, final int startPosition, final boolean startPlaying) {
if (!tryToHandleOpenPlayingQueue(queue, startPosition, startPlaying) && musicService != null) {
musicService.openAndPlayQueue(queue, startPosition, startPlaying);
} }
} }
/**
* Async
*/
public static void openAndShuffleQueue(final ArrayList<Song> queue, boolean startPlaying) {
int startPosition = 0;
if (!queue.isEmpty()) {
startPosition = new Random().nextInt(queue.size());
}
if (!tryToHandleOpenPlayingQueue(queue, startPosition, startPlaying) && musicService != null) {
openQueue(queue, startPosition, startPlaying);
setShuffleMode(MusicService.SHUFFLE_MODE_SHUFFLE);
}
}
private static boolean tryToHandleOpenPlayingQueue(final ArrayList<Song> queue, final int startPosition, final boolean startPlaying) {
if (getPlayingQueue() == queue) {
if (startPlaying) {
playSongAt(startPosition);
} else {
setPosition(startPosition);
}
return true;
}
return false;
}
public static Song getCurrentSong() { public static Song getCurrentSong() {
if (musicService != null) { if (musicService != null) {
return musicService.getCurrentSong(); return musicService.getCurrentSong();
@ -227,17 +278,6 @@ public class MusicPlayerRemote {
return false; return false;
} }
public static boolean openAndShuffleQueue(@NonNull final ArrayList<Song> songs, boolean startPlaying) {
if (musicService != null) {
if (!songs.isEmpty()) {
MusicPlayerRemote.openQueue(songs, new Random().nextInt(songs.size()), startPlaying);
setShuffleMode(MusicService.SHUFFLE_MODE_SHUFFLE);
}
return true;
}
return false;
}
public static boolean playNext(Song song) { public static boolean playNext(Song song) {
if (musicService != null) { if (musicService != null) {
musicService.addSong(getPosition() + 1, song); musicService.addSong(getPosition() + 1, song);

View file

@ -91,6 +91,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
public static final int PLAY_SONG = 13; public static final int PLAY_SONG = 13;
public static final int SAVE_QUEUES = 14; public static final int SAVE_QUEUES = 14;
public static final int PREPARE_NEXT = 15; public static final int PREPARE_NEXT = 15;
public static final int SET_POSITION = 16;
public static final int SHUFFLE_MODE_NONE = 0; public static final int SHUFFLE_MODE_NONE = 0;
public static final int SHUFFLE_MODE_SHUFFLE = 1; public static final int SHUFFLE_MODE_SHUFFLE = 1;
public static final int REPEAT_MODE_NONE = 0; public static final int REPEAT_MODE_NONE = 0;
@ -370,7 +371,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
return position; return position;
} }
private void setPosition(int position) { private void setPositionInternal(int position) {
this.position = position; this.position = position;
} }
@ -380,7 +381,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
private boolean openTrackAndPrepareNextAt(int position) { private boolean openTrackAndPrepareNextAt(int position) {
synchronized (this) { synchronized (this) {
setPosition(position); setPositionInternal(position);
boolean prepared = openCurrent(); boolean prepared = openCurrent();
if (prepared) prepareNextImpl(); if (prepared) prepareNextImpl();
notifyChange(META_CHANGED); notifyChange(META_CHANGED);
@ -576,7 +577,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
this.originalPlayingQueue = restoredOriginalQueue; this.originalPlayingQueue = restoredOriginalQueue;
this.playingQueue = restoredQueue; this.playingQueue = restoredQueue;
setPosition(restoredPosition); setPositionInternal(restoredPosition);
openCurrent(); openCurrent();
prepareNext(); prepareNext();
@ -637,11 +638,11 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
originalPlayingQueue.add(to, tmpSong); originalPlayingQueue.add(to, tmpSong);
} }
if (from > currentPosition && to <= currentPosition) { if (from > currentPosition && to <= currentPosition) {
setPosition(getPosition() + 1); setPositionInternal(getPosition() + 1);
} else if (from < currentPosition && to >= currentPosition) { } else if (from < currentPosition && to >= currentPosition) {
setPosition(getPosition() - 1); setPositionInternal(getPosition() - 1);
} else if (from == currentPosition) { } else if (from == currentPosition) {
setPosition(to); setPositionInternal(to);
} }
notifyChange(QUEUE_CHANGED); notifyChange(QUEUE_CHANGED);
} }
@ -652,6 +653,12 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
playerHandler.obtainMessage(PLAY_SONG, position, 0).sendToTarget(); playerHandler.obtainMessage(PLAY_SONG, position, 0).sendToTarget();
} }
public void setPosition(final int position) {
// handle this on the handlers thread to avoid blocking the ui thread
playerHandler.removeMessages(SET_POSITION);
playerHandler.obtainMessage(SET_POSITION, position, 0).sendToTarget();
}
private void playSongAtImpl(int position) { private void playSongAtImpl(int position) {
if (openTrackAndPrepareNextAt(position)) { if (openTrackAndPrepareNextAt(position)) {
play(); play();
@ -777,7 +784,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
case SHUFFLE_MODE_SHUFFLE: case SHUFFLE_MODE_SHUFFLE:
this.shuffleMode = shuffleMode; this.shuffleMode = shuffleMode;
ShuffleHelper.makeShuffleList(this.getPlayingQueue(), getPosition()); ShuffleHelper.makeShuffleList(this.getPlayingQueue(), getPosition());
setPosition(0); setPositionInternal(0);
break; break;
case SHUFFLE_MODE_NONE: case SHUFFLE_MODE_NONE:
this.shuffleMode = shuffleMode; this.shuffleMode = shuffleMode;
@ -789,7 +796,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
newPosition = getPlayingQueue().indexOf(song); newPosition = getPlayingQueue().indexOf(song);
} }
} }
setPosition(newPosition); setPositionInternal(newPosition);
break; break;
} }
notifyChange(SHUFFLE_MODE_CHANGED); notifyChange(SHUFFLE_MODE_CHANGED);
@ -950,7 +957,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
service.pause(); service.pause();
service.seek(0); service.seek(0);
} else { } else {
service.setPosition(service.nextPosition); service.setPositionInternal(service.nextPosition);
service.prepareNextImpl(); service.prepareNextImpl();
service.notifyChange(META_CHANGED); service.notifyChange(META_CHANGED);
} }
@ -973,6 +980,10 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
service.playSongAtImpl(msg.arg1); service.playSongAtImpl(msg.arg1);
break; break;
case SET_POSITION:
service.openTrackAndPrepareNextAt(msg.arg1);
break;
case PREPARE_NEXT: case PREPARE_NEXT:
service.prepareNextImpl(); service.prepareNextImpl();
break; break;

View file

@ -23,7 +23,6 @@ public class PlayerAlbumCoverFragment extends AbsMusicServiceFragment implements
@Bind(R.id.player_album_cover_viewpager) @Bind(R.id.player_album_cover_viewpager)
ViewPager viewPager; ViewPager viewPager;
private AlbumCoverPagerAdapter pagerAdapter;
private OnColorChangedListener onColorChangedListener; private OnColorChangedListener onColorChangedListener;
@Override @Override