-Fixed incorrect indexing due to item removed after shuffle.

-Fixed activity binding not unbound after service shutdown.
This commit is contained in:
John Zhen M 2017-10-10 12:25:48 -07:00 committed by John Zhen Mo
parent 94f7baf299
commit 770dcc1832
3 changed files with 55 additions and 26 deletions

View File

@ -469,8 +469,8 @@ public final class BackgroundPlayer extends Service {
@Override @Override
public void shutdown() { public void shutdown() {
super.shutdown(); super.shutdown();
stopSelf();
stopActivityBinding(); stopActivityBinding();
stopSelf();
} }
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////

View File

@ -53,7 +53,7 @@ public class BackgroundPlayerActivity extends AppCompatActivity
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
private static final int RECYCLER_ITEM_POPUP_MENU_GROUP_ID = 47; private static final int RECYCLER_ITEM_POPUP_MENU_GROUP_ID = 47;
private static final int PLAYBACK_SPEED_POPUP_MENU_GROUP_ID = 61; private static final int PLAYBACK_SPEED_POPUP_MENU_GROUP_ID = 61;
private static final int PLAYBACK_PITCH_POPUP_MENU_GROUP_ID = 97; private static final int PLAYBACK_PITCH_POPUP_MENU_GROUP_ID = 97;
private View rootView; private View rootView;
@ -92,8 +92,10 @@ public class BackgroundPlayerActivity extends AppCompatActivity
final Toolbar toolbar = rootView.findViewById(R.id.toolbar); final Toolbar toolbar = rootView.findViewById(R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(R.string.title_activity_background_player); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(R.string.title_activity_background_player);
}
serviceConnection = backgroundPlayerConnection(); serviceConnection = backgroundPlayerConnection();
} }
@ -101,9 +103,7 @@ public class BackgroundPlayerActivity extends AppCompatActivity
@Override @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
final Intent mIntent = new Intent(this, BackgroundPlayer.class); bind();
final boolean success = bindService(mIntent, serviceConnection, BIND_AUTO_CREATE);
if (!success) unbindService(serviceConnection);
} }
@Override @Override
@ -123,24 +123,36 @@ public class BackgroundPlayerActivity extends AppCompatActivity
@Override @Override
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
if(serviceBound) { unbind();
unbindService(serviceConnection);
serviceBound = false;
}
} }
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Service Connection // Service Connection
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
private void bind() {
final Intent mIntent = new Intent(this, BackgroundPlayer.class);
final boolean success = bindService(mIntent, serviceConnection, BIND_AUTO_CREATE);
if (!success) {
unbindService(serviceConnection);
}
serviceBound = success;
}
private void unbind() {
if(serviceBound) {
unbindService(serviceConnection);
serviceBound = false;
player = null;
finish();
}
}
private ServiceConnection backgroundPlayerConnection() { private ServiceConnection backgroundPlayerConnection() {
return new ServiceConnection() { return new ServiceConnection() {
@Override @Override
public void onServiceDisconnected(ComponentName name) { public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "Background player service is disconnected"); Log.d(TAG, "Background player service is disconnected");
serviceBound = false;
player = null;
finish();
} }
@Override @Override
@ -149,12 +161,9 @@ public class BackgroundPlayerActivity extends AppCompatActivity
final BackgroundPlayer.LocalBinder mLocalBinder = (BackgroundPlayer.LocalBinder) service; final BackgroundPlayer.LocalBinder mLocalBinder = (BackgroundPlayer.LocalBinder) service;
player = mLocalBinder.getBackgroundPlayerInstance(); player = mLocalBinder.getBackgroundPlayerInstance();
if (player == null) { if (player == null) {
finish(); unbind();
} else { } else {
serviceBound = true;
buildComponents(); buildComponents();
player.setActivityListener(BackgroundPlayerActivity.this);
} }
} }
}; };
@ -169,6 +178,7 @@ public class BackgroundPlayerActivity extends AppCompatActivity
buildMetadata(); buildMetadata();
buildSeekBar(); buildSeekBar();
buildControls(); buildControls();
buildListeners();
} }
private void buildQueue() { private void buildQueue() {
@ -220,6 +230,10 @@ public class BackgroundPlayerActivity extends AppCompatActivity
buildPlaybackPitchMenu(); buildPlaybackPitchMenu();
} }
private void buildListeners() {
player.setActivityListener(this);
}
private void buildPlaybackSpeedMenu() { private void buildPlaybackSpeedMenu() {
if (playbackSpeedPopupMenu == null) return; if (playbackSpeedPopupMenu == null) return;
@ -241,11 +255,11 @@ public class BackgroundPlayerActivity extends AppCompatActivity
private void buildPlaybackPitchMenu() { private void buildPlaybackPitchMenu() {
if (playbackPitchPopupMenu == null) return; if (playbackPitchPopupMenu == null) return;
playbackPitchPopupMenu.getMenu().removeGroup(PLAYBACK_SPEED_POPUP_MENU_GROUP_ID); playbackPitchPopupMenu.getMenu().removeGroup(PLAYBACK_PITCH_POPUP_MENU_GROUP_ID);
for (int i = 0; i < BasePlayer.PLAYBACK_PITCHES.length; i++) { for (int i = 0; i < BasePlayer.PLAYBACK_PITCHES.length; i++) {
final float playbackPitch = BasePlayer.PLAYBACK_PITCHES[i]; final float playbackPitch = BasePlayer.PLAYBACK_PITCHES[i];
final String formattedPitch = player.formatPitch(playbackPitch); final String formattedPitch = player.formatPitch(playbackPitch);
final MenuItem item = playbackPitchPopupMenu.getMenu().add(PLAYBACK_SPEED_POPUP_MENU_GROUP_ID, i, Menu.NONE, formattedPitch); final MenuItem item = playbackPitchPopupMenu.getMenu().add(PLAYBACK_PITCH_POPUP_MENU_GROUP_ID, i, Menu.NONE, formattedPitch);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override @Override
public boolean onMenuItemClick(MenuItem menuItem) { public boolean onMenuItemClick(MenuItem menuItem) {
@ -488,6 +502,6 @@ public class BackgroundPlayerActivity extends AppCompatActivity
@Override @Override
public void onServiceStopped() { public void onServiceStopped() {
finish(); unbind();
} }
} }

View File

@ -274,11 +274,11 @@ public abstract class PlayQueue implements Serializable {
queueIndex.set(0); queueIndex.set(0);
} }
streams.remove(index);
if (backup != null) { if (backup != null) {
final int backupIndex = backup.indexOf(getItem(index)); final int backupIndex = backup.indexOf(getItem(index));
backup.remove(backupIndex); backup.remove(backupIndex);
} }
streams.remove(index);
} }
public synchronized void move(final int source, final int target) { public synchronized void move(final int source, final int target) {
@ -303,7 +303,8 @@ public abstract class PlayQueue implements Serializable {
* *
* This method first backs up the existing play queue and item being played. * This method first backs up the existing play queue and item being played.
* Then a newly shuffled play queue will be generated along with the index of * Then a newly shuffled play queue will be generated along with the index of
* the previously playing item. * the previously playing item if it is found in the shuffled play queue. If
* not found, the current index will reset to 0.
* *
* Will emit a {@link ReorderEvent} in any context. * Will emit a {@link ReorderEvent} in any context.
* */ * */
@ -313,7 +314,13 @@ public abstract class PlayQueue implements Serializable {
} }
final PlayQueueItem current = getItem(); final PlayQueueItem current = getItem();
Collections.shuffle(streams); Collections.shuffle(streams);
queueIndex.set(streams.indexOf(current));
final int newIndex = streams.indexOf(current);
if (newIndex != -1) {
queueIndex.set(newIndex);
} else {
queueIndex.set(0);
}
broadcast(new ReorderEvent()); broadcast(new ReorderEvent());
} }
@ -321,17 +328,25 @@ public abstract class PlayQueue implements Serializable {
/** /**
* Unshuffles the current play queue if a backup play queue exists. * Unshuffles the current play queue if a backup play queue exists.
* *
* This method undoes shuffling and index will be set to the previously playing item. * This method undoes shuffling and index will be set to the previously playing item if found,
* otherwise, the index will reset to 0.
* *
* Will emit a {@link ReorderEvent} if a backup exists. * Will emit a {@link ReorderEvent} if a backup exists.
* */ * */
public synchronized void unshuffle() { public synchronized void unshuffle() {
if (backup == null) return; if (backup == null) return;
final PlayQueueItem current = getItem(); final PlayQueueItem current = getItem();
streams.clear(); streams.clear();
streams = backup; streams = backup;
backup = null; backup = null;
queueIndex.set(streams.indexOf(current));
final int newIndex = streams.indexOf(current);
if (newIndex != -1) {
queueIndex.set(newIndex);
} else {
queueIndex.set(0);
}
broadcast(new ReorderEvent()); broadcast(new ReorderEvent());
} }