Change how unfilled songs are handled

* Remove code checks for -1 and path == null, it's the job of the
creator of a Song object to not inject unfilled songs into the timeline
This commit is contained in:
Adrian Ulrich 2015-10-07 21:10:02 +02:00
parent c7653c7051
commit 305a7c1823
7 changed files with 25 additions and 19 deletions

View File

@ -51,7 +51,7 @@ public class AudioPickerActivity extends PlaybackActivity {
}
mSong = getSongForUri(uri);
if (mSong.isEmpty()) {
if (mSong == null) {
// unsupported intent or song not found
finish();
return;
@ -113,7 +113,7 @@ public class AudioPickerActivity extends PlaybackActivity {
* Attempts to resolve given uri to a song object
*
* @param uri The uri to resolve
* @return A song object, id will be -1 on failure
* @return A song object, null on failure
*/
private Song getSongForUri(Uri uri) {
Song song = new Song(-1);
@ -130,7 +130,7 @@ public class AudioPickerActivity extends PlaybackActivity {
}
cursor.close();
}
return song;
return song.isFilled() ? song : null;
}
}

View File

@ -594,10 +594,8 @@ public class FullPlaybackActivity extends PlaybackActivity
}
mFormat = sb.toString();
if(song.path != null) { /* ICS bug? */
float[] rg = PlaybackService.get(this).getReplayGainValues(song.path);
mReplayGain = "track="+rg[0]+"dB, album="+rg[1]+"dB";
}
float[] rg = PlaybackService.get(this).getReplayGainValues(song.path);
mReplayGain = "track="+rg[0]+"dB, album="+rg[1]+"dB";
data.release();
}

View File

@ -412,7 +412,7 @@ public class MediaUtils {
}
cursor.close();
}
return song.isEmpty() ? null : song;
return song.isFilled() ? song : null;
}
/**

View File

@ -738,7 +738,6 @@ public final class PlaybackService extends Service
Song nextSong = getSong(1);
if( nextSong != null
&& nextSong.path != null
&& fa != SongTimeline.FINISH_REPEAT_CURRENT
&& fa != SongTimeline.FINISH_STOP_CURRENT
&& !mTimeline.isEndOfQueue() ) {
@ -1279,7 +1278,7 @@ public final class PlaybackService extends Service
else
song = mTimeline.shiftCurrentSong(delta);
mCurrentSong = song;
if (song == null || song.isEmpty()) {
if (song == null) {
if (MediaUtils.isSongAvailable(getContentResolver())) {
int flag = finishAction(mState) == SongTimeline.FINISH_RANDOM ? FLAG_ERROR : FLAG_EMPTY_QUEUE;
synchronized (mStateLock) {
@ -1325,7 +1324,7 @@ public final class PlaybackService extends Service
mPreparedMediaPlayer = tmpPlayer; // this was mMediaPlayer and is in reset() state
Log.v("VanillaMusic", "Swapped media players");
}
else if(song.path != null) {
else {
prepareMediaPlayer(mMediaPlayer, song.path);
}

View File

@ -87,7 +87,7 @@ public class RemoteControl {
remote.setPlaybackState(isPlaying ? RemoteControlClient.PLAYSTATE_PLAYING : RemoteControlClient.PLAYSTATE_PAUSED);
RemoteControlClient.MetadataEditor editor = remote.editMetadata(true);
if (song != null && song.isEmpty() == false) {
if (song != null) {
editor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, song.artist);
editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, song.album);
editor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, song.title);

View File

@ -157,11 +157,11 @@ public class Song implements Comparable<Song> {
}
/**
* Returns true if the song is empty
* Returns true if the song is filled
*/
public boolean isEmpty()
public boolean isFilled()
{
return id == -1;
return (id != -1 && path != null);
}
/**

View File

@ -37,6 +37,7 @@ import java.util.Comparator;
import java.util.Iterator;
import java.util.ListIterator;
import junit.framework.Assert;
import android.util.Log;
/**
* Contains the list of currently playing songs, implements repeat and shuffle
@ -363,11 +364,11 @@ public final class SongTimeline {
// The query may have returned zero results or we might
// have failed to populate some songs: Get rid of all
// uninitialized items (where path == null)
// uninitialized items
Iterator<Song> it = songs.iterator();
while (it.hasNext()) {
Song e = it.next();
if (e.path == null) {
if (e.isFilled() == false) {
it.remove();
}
}
@ -683,7 +684,8 @@ public final class SongTimeline {
int type = query.type;
long data = query.data;
int count = cursor.getCount();
int count = cursor.getCount(); // Items found by query
int added = 0; // Items actually added to the queue
if (count == 0 && type == MediaUtils.TYPE_FILE && query.selectionArgs.length == 1) {
String pathQuery = query.selectionArgs[0];
@ -743,9 +745,16 @@ public final class SongTimeline {
for (int j = 0; j != count; ++j) {
cursor.moveToPosition(j);
Song song = new Song(-1);
song.populate(cursor);
if (song.isFilled() == false) {
// Song vanished from device for some reason: we are silently skipping it.
continue;
}
timeline.add(addAtPos++, song);
added++;
if (jumpSong == null) {
if ((mode == MODE_PLAY_POS_FIRST || mode == MODE_ENQUEUE_POS_FIRST) && j == data) {
@ -790,7 +799,7 @@ public final class SongTimeline {
changed();
return count;
return added;
}
/**