Skip to content

Commit

Permalink
Fixes issue kabouzeid#112 - Crash on too many songs in App Shortcut p…
Browse files Browse the repository at this point in the history
…laylist

Instead of passing all songs in an intent extra, pass the Playlist which is then used to load the songs in MusicService
  • Loading branch information
Adrian Campos authored and oshmoun committed Apr 23, 2017
1 parent dcf988d commit 401c791
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
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
Expand Down Expand Up @@ -46,31 +45,31 @@ public void onCreate(Bundle savedInstanceState) {

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;
}

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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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];
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
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;
Expand Down Expand Up @@ -75,12 +78,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";
Expand Down Expand Up @@ -298,20 +303,29 @@ public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
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();
}
Expand Down Expand Up @@ -861,6 +875,24 @@ public void play() {
}
}

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));
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 401c791

Please sign in to comment.