Fixes issue #112 - Crash on too many songs in App Shortcut playlist
Instead of passing all songs in an intent extra, pass the Playlist which is then used to load the songs in MusicService
This commit is contained in:
parent
f504e83cbf
commit
54414e7d3e
4 changed files with 111 additions and 25 deletions
|
|
@ -8,15 +8,14 @@ import android.support.annotation.IntDef;
|
|||
import com.kabouzeid.gramophone.appshortcuts.shortcuttype.LastAddedShortcutType;
|
||||
import com.kabouzeid.gramophone.appshortcuts.shortcuttype.ShuffleAllShortcutType;
|
||||
import com.kabouzeid.gramophone.appshortcuts.shortcuttype.TopTracksShortcutType;
|
||||
import com.kabouzeid.gramophone.loader.LastAddedLoader;
|
||||
import com.kabouzeid.gramophone.loader.SongLoader;
|
||||
import com.kabouzeid.gramophone.loader.TopAndRecentlyPlayedTracksLoader;
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
import com.kabouzeid.gramophone.model.Playlist;
|
||||
import com.kabouzeid.gramophone.model.smartplaylist.LastAddedPlaylist;
|
||||
import com.kabouzeid.gramophone.model.smartplaylist.MyTopTracksPlaylist;
|
||||
import com.kabouzeid.gramophone.model.smartplaylist.ShuffleAllPlaylist;
|
||||
import com.kabouzeid.gramophone.service.MusicService;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* @author Adrian Campos
|
||||
|
|
@ -46,18 +45,18 @@ public class AppShortcutLauncherActivity extends Activity {
|
|||
|
||||
switch (shortcutType) {
|
||||
case SHORTCUT_TYPE_SHUFFLE_ALL:
|
||||
startServiceWithSongs(MusicService.SHUFFLE_MODE_SHUFFLE,
|
||||
SongLoader.getAllSongs(getApplicationContext()));
|
||||
startServiceWithPlaylist(MusicService.SHUFFLE_MODE_SHUFFLE,
|
||||
new ShuffleAllPlaylist(getApplicationContext()));
|
||||
DynamicShortcutManager.reportShortcutUsed(this, ShuffleAllShortcutType.getId());
|
||||
break;
|
||||
case SHORTCUT_TYPE_TOP_TRACKS:
|
||||
startServiceWithSongs(MusicService.SHUFFLE_MODE_NONE,
|
||||
TopAndRecentlyPlayedTracksLoader.getTopTracks(getApplicationContext()));
|
||||
startServiceWithPlaylist(MusicService.SHUFFLE_MODE_NONE,
|
||||
new MyTopTracksPlaylist(getApplicationContext()));
|
||||
DynamicShortcutManager.reportShortcutUsed(this, TopTracksShortcutType.getId());
|
||||
break;
|
||||
case SHORTCUT_TYPE_LAST_ADDED:
|
||||
startServiceWithSongs(MusicService.SHUFFLE_MODE_NONE,
|
||||
LastAddedLoader.getLastAddedSongs(getApplicationContext()));
|
||||
startServiceWithPlaylist(MusicService.SHUFFLE_MODE_NONE,
|
||||
new LastAddedPlaylist(getApplicationContext()));
|
||||
DynamicShortcutManager.reportShortcutUsed(this, LastAddedShortcutType.getId());
|
||||
break;
|
||||
}
|
||||
|
|
@ -65,12 +64,12 @@ public class AppShortcutLauncherActivity extends Activity {
|
|||
finish();
|
||||
}
|
||||
|
||||
private void startServiceWithSongs(int shuffleMode, ArrayList<Song> songs) {
|
||||
private void startServiceWithPlaylist(int shuffleMode, Playlist playlist) {
|
||||
Intent intent = new Intent(this, MusicService.class);
|
||||
intent.setAction(MusicService.ACTION_PLAY);
|
||||
intent.setAction(MusicService.ACTION_PLAY_PLAYLIST);
|
||||
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putParcelableArrayList(MusicService.INTENT_EXTRA_SONGS, songs);
|
||||
bundle.putParcelable(MusicService.INTENT_EXTRA_PLAYLIST, playlist);
|
||||
bundle.putInt(MusicService.INTENT_EXTRA_SHUFFLE_MODE, shuffleMode);
|
||||
|
||||
intent.putExtras(bundle);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
package com.kabouzeid.gramophone.model.smartplaylist;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Parcel;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.kabouzeid.gramophone.R;
|
||||
import com.kabouzeid.gramophone.loader.SongLoader;
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ShuffleAllPlaylist extends AbsSmartPlaylist {
|
||||
|
||||
public ShuffleAllPlaylist(@NonNull Context context) {
|
||||
super(context.getString(R.string.shuffle_all), R.drawable.ic_shuffle_white_24dp);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ArrayList<Song> getSongs(@NonNull Context context) {
|
||||
return SongLoader.getAllSongs(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear(@NonNull Context context) {
|
||||
//Can't clear all songs. Don't do anything here?
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
super.writeToParcel(dest, flags);
|
||||
}
|
||||
|
||||
protected ShuffleAllPlaylist(Parcel in) {
|
||||
super(in);
|
||||
}
|
||||
|
||||
public static final Creator<ShuffleAllPlaylist> CREATOR = new Creator<ShuffleAllPlaylist>() {
|
||||
public ShuffleAllPlaylist createFromParcel(Parcel source) {
|
||||
return new ShuffleAllPlaylist(source);
|
||||
}
|
||||
|
||||
public ShuffleAllPlaylist[] newArray(int size) {
|
||||
return new ShuffleAllPlaylist[size];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -46,7 +46,10 @@ import com.kabouzeid.gramophone.glide.BlurTransformation;
|
|||
import com.kabouzeid.gramophone.glide.SongGlideRequest;
|
||||
import com.kabouzeid.gramophone.helper.ShuffleHelper;
|
||||
import com.kabouzeid.gramophone.helper.StopWatch;
|
||||
import com.kabouzeid.gramophone.loader.PlaylistSongLoader;
|
||||
import com.kabouzeid.gramophone.model.Playlist;
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
import com.kabouzeid.gramophone.model.smartplaylist.AbsSmartPlaylist;
|
||||
import com.kabouzeid.gramophone.provider.HistoryStore;
|
||||
import com.kabouzeid.gramophone.provider.MusicPlaybackQueueStore;
|
||||
import com.kabouzeid.gramophone.provider.SongPlayCountStore;
|
||||
|
|
@ -74,12 +77,14 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
|||
|
||||
public static final String ACTION_TOGGLE_PAUSE = PHONOGRAPH_PACKAGE_NAME + ".togglepause";
|
||||
public static final String ACTION_PLAY = PHONOGRAPH_PACKAGE_NAME + ".play";
|
||||
public static final String ACTION_PLAY_PLAYLIST = PHONOGRAPH_PACKAGE_NAME + ".play.playlist";
|
||||
public static final String ACTION_PAUSE = PHONOGRAPH_PACKAGE_NAME + ".pause";
|
||||
public static final String ACTION_STOP = PHONOGRAPH_PACKAGE_NAME + ".stop";
|
||||
public static final String ACTION_SKIP = PHONOGRAPH_PACKAGE_NAME + ".skip";
|
||||
public static final String ACTION_REWIND = PHONOGRAPH_PACKAGE_NAME + ".rewind";
|
||||
public static final String ACTION_QUIT = PHONOGRAPH_PACKAGE_NAME + ".quitservice";
|
||||
public static final String INTENT_EXTRA_SONGS = PHONOGRAPH_PACKAGE_NAME + ".intentextra.songs";
|
||||
public static final String INTENT_EXTRA_PLAYLIST = PHONOGRAPH_PACKAGE_NAME + "intentextra.playlist";
|
||||
public static final String INTENT_EXTRA_SHUFFLE_MODE = PHONOGRAPH_PACKAGE_NAME + ".intentextra.shufflemode";
|
||||
|
||||
public static final String APP_WIDGET_UPDATE = PHONOGRAPH_PACKAGE_NAME + ".appwidgetupdate";
|
||||
|
|
@ -297,20 +302,29 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
|||
pause();
|
||||
break;
|
||||
case ACTION_PLAY:
|
||||
//Load songs from intent
|
||||
ArrayList<Song> songs = intent.getParcelableArrayListExtra(INTENT_EXTRA_SONGS);
|
||||
if (songs != null && !songs.isEmpty()) {
|
||||
int shuffleMode = intent.getIntExtra(INTENT_EXTRA_SHUFFLE_MODE, getShuffleMode());
|
||||
if (intent.hasExtra(INTENT_EXTRA_SHUFFLE_MODE) && intent.getIntExtra(INTENT_EXTRA_SHUFFLE_MODE, 0) == SHUFFLE_MODE_SHUFFLE) {
|
||||
int startPosition = 0;
|
||||
if (!songs.isEmpty()) {
|
||||
startPosition = new Random().nextInt(songs.size());
|
||||
}
|
||||
openQueue(songs, startPosition, false);
|
||||
setShuffleMode(shuffleMode);
|
||||
|
||||
//Play songs with the intent's shuffle mode, if it has one
|
||||
playSongs(songs,
|
||||
intent.getIntExtra(INTENT_EXTRA_SHUFFLE_MODE, getShuffleMode()));
|
||||
break;
|
||||
case ACTION_PLAY_PLAYLIST:
|
||||
//Load playlist from intent
|
||||
Playlist playlist = intent.getParcelableExtra(INTENT_EXTRA_PLAYLIST);
|
||||
if (playlist != null) {
|
||||
//Get songs from playlist
|
||||
ArrayList<Song> playlistSongs;
|
||||
if (playlist instanceof AbsSmartPlaylist) {
|
||||
playlistSongs = ((AbsSmartPlaylist) playlist).getSongs(getApplicationContext());
|
||||
} else {
|
||||
openQueue(songs, 0, false);
|
||||
//noinspection unchecked
|
||||
playlistSongs = (ArrayList<Song>) (List) PlaylistSongLoader.getPlaylistSongList(getApplicationContext(), playlist.id);
|
||||
}
|
||||
play();
|
||||
|
||||
//Play songs with the intent's shuffle mode, if it has one
|
||||
playSongs(playlistSongs,
|
||||
intent.getIntExtra(INTENT_EXTRA_SHUFFLE_MODE, getShuffleMode()));
|
||||
} else {
|
||||
Toast.makeText(getApplicationContext(), R.string.no_songs_in_playlist, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
|
@ -860,6 +874,24 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
|||
}
|
||||
}
|
||||
|
||||
public void playSongs(ArrayList<Song> songs, int shuffleMode) {
|
||||
if (songs != null && !songs.isEmpty()) {
|
||||
if (shuffleMode == SHUFFLE_MODE_SHUFFLE) {
|
||||
int startPosition = 0;
|
||||
if (!songs.isEmpty()) {
|
||||
startPosition = new Random().nextInt(songs.size());
|
||||
}
|
||||
openQueue(songs, startPosition, false);
|
||||
setShuffleMode(shuffleMode);
|
||||
} else {
|
||||
openQueue(songs, 0, false);
|
||||
}
|
||||
play();
|
||||
} else {
|
||||
Toast.makeText(getApplicationContext(), R.string.no_songs_in_playlist, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
public void playPreviousSong(boolean force) {
|
||||
playSongAt(getPreviousPosition(force));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -181,6 +181,7 @@
|
|||
<string name="favorites">Favorites</string>
|
||||
<string name="last_added">Last added</string>
|
||||
<string name="history">History</string>
|
||||
<string name="shuffle_all">Shuffle All</string>
|
||||
<string name="my_top_tracks">My top tracks</string>
|
||||
<string name="remove_cover">Remove cover</string>
|
||||
<string name="download_from_last_fm">Download from Last.fm</string>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue