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) {
if (musicService != null) {
musicService.playSongAt(position);
}
}
/**
* Async
*/
public static void setPosition(final int position) {
if (musicService != null) {
musicService.setPosition(position);
}
}
public static void pauseSong() {
if (musicService != null) {
musicService.pause();
}
}
/**
* Async
*/
public static void playNextSong() {
if (musicService != null) {
musicService.playNextSong(true);
}
}
/**
* Async
*/
public static void playPreviousSong() {
if (musicService != null) {
musicService.playPreviousSong(true);
}
}
/**
* Async
*/
public static void back() {
if (musicService != null) {
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) {
musicService.openAndPlayQueue(playingQueue, startPosition, startPlaying);
/**
* Async
*/
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() {
if (musicService != null) {
return musicService.getCurrentSong();
@ -227,17 +278,6 @@ public class MusicPlayerRemote {
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) {
if (musicService != null) {
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 SAVE_QUEUES = 14;
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_SHUFFLE = 1;
public static final int REPEAT_MODE_NONE = 0;
@ -370,7 +371,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
return position;
}
private void setPosition(int position) {
private void setPositionInternal(int position) {
this.position = position;
}
@ -380,7 +381,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
private boolean openTrackAndPrepareNextAt(int position) {
synchronized (this) {
setPosition(position);
setPositionInternal(position);
boolean prepared = openCurrent();
if (prepared) prepareNextImpl();
notifyChange(META_CHANGED);
@ -576,7 +577,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
this.originalPlayingQueue = restoredOriginalQueue;
this.playingQueue = restoredQueue;
setPosition(restoredPosition);
setPositionInternal(restoredPosition);
openCurrent();
prepareNext();
@ -637,11 +638,11 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
originalPlayingQueue.add(to, tmpSong);
}
if (from > currentPosition && to <= currentPosition) {
setPosition(getPosition() + 1);
setPositionInternal(getPosition() + 1);
} else if (from < currentPosition && to >= currentPosition) {
setPosition(getPosition() - 1);
setPositionInternal(getPosition() - 1);
} else if (from == currentPosition) {
setPosition(to);
setPositionInternal(to);
}
notifyChange(QUEUE_CHANGED);
}
@ -652,6 +653,12 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
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) {
if (openTrackAndPrepareNextAt(position)) {
play();
@ -777,7 +784,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
case SHUFFLE_MODE_SHUFFLE:
this.shuffleMode = shuffleMode;
ShuffleHelper.makeShuffleList(this.getPlayingQueue(), getPosition());
setPosition(0);
setPositionInternal(0);
break;
case SHUFFLE_MODE_NONE:
this.shuffleMode = shuffleMode;
@ -789,7 +796,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
newPosition = getPlayingQueue().indexOf(song);
}
}
setPosition(newPosition);
setPositionInternal(newPosition);
break;
}
notifyChange(SHUFFLE_MODE_CHANGED);
@ -950,7 +957,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
service.pause();
service.seek(0);
} else {
service.setPosition(service.nextPosition);
service.setPositionInternal(service.nextPosition);
service.prepareNextImpl();
service.notifyChange(META_CHANGED);
}
@ -973,6 +980,10 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
service.playSongAtImpl(msg.arg1);
break;
case SET_POSITION:
service.openTrackAndPrepareNextAt(msg.arg1);
break;
case PREPARE_NEXT:
service.prepareNextImpl();
break;

View file

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