use of com.kabouzeid.gramophone.model.Song in project Phonograph by kabouzeid.
the class MainActivity method handlePlaybackIntent.
private void handlePlaybackIntent(@Nullable Intent intent) {
if (intent == null) {
return;
}
Uri uri = intent.getData();
String mimeType = intent.getType();
boolean handled = false;
if (intent.getAction() != null && intent.getAction().equals(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH)) {
final List<Song> songs = SearchQueryHelper.getSongs(this, intent.getExtras());
if (MusicPlayerRemote.getShuffleMode() == MusicService.SHUFFLE_MODE_SHUFFLE) {
MusicPlayerRemote.openAndShuffleQueue(songs, true);
} else {
MusicPlayerRemote.openQueue(songs, 0, true);
}
handled = true;
}
if (uri != null && uri.toString().length() > 0) {
MusicPlayerRemote.playFromUri(uri);
handled = true;
} else if (MediaStore.Audio.Playlists.CONTENT_TYPE.equals(mimeType)) {
final long id = parseIdFromIntent(intent, "playlistId", "playlist");
if (id >= 0) {
int position = intent.getIntExtra("position", 0);
List<Song> songs = new ArrayList<>(PlaylistSongLoader.getPlaylistSongList(this, id));
MusicPlayerRemote.openQueue(songs, position, true);
handled = true;
}
} else if (MediaStore.Audio.Albums.CONTENT_TYPE.equals(mimeType)) {
final long id = parseIdFromIntent(intent, "albumId", "album");
if (id >= 0) {
int position = intent.getIntExtra("position", 0);
MusicPlayerRemote.openQueue(AlbumLoader.getAlbum(this, id).songs, position, true);
handled = true;
}
} else if (MediaStore.Audio.Artists.CONTENT_TYPE.equals(mimeType)) {
final long id = parseIdFromIntent(intent, "artistId", "artist");
if (id >= 0) {
int position = intent.getIntExtra("position", 0);
MusicPlayerRemote.openQueue(ArtistLoader.getArtist(this, id).getSongs(), position, true);
handled = true;
}
}
if (handled) {
setIntent(new Intent());
}
}
use of com.kabouzeid.gramophone.model.Song in project Phonograph by kabouzeid.
the class MusicService method setShuffleMode.
public void setShuffleMode(final int shuffleMode) {
PreferenceManager.getDefaultSharedPreferences(this).edit().putInt(SAVED_SHUFFLE_MODE, shuffleMode).apply();
switch(shuffleMode) {
case SHUFFLE_MODE_SHUFFLE:
this.shuffleMode = shuffleMode;
ShuffleHelper.makeShuffleList(this.getPlayingQueue(), getPosition());
position = 0;
break;
case SHUFFLE_MODE_NONE:
this.shuffleMode = shuffleMode;
long currentSongId = getCurrentSong().id;
playingQueue = new ArrayList<>(originalPlayingQueue);
int newPosition = 0;
for (Song song : getPlayingQueue()) {
if (song.id == currentSongId) {
newPosition = getPlayingQueue().indexOf(song);
}
}
position = newPosition;
break;
}
handleAndSendChangeInternal(SHUFFLE_MODE_CHANGED);
notifyChange(QUEUE_CHANGED);
}
use of com.kabouzeid.gramophone.model.Song in project Phonograph by kabouzeid.
the class MusicService method onStartCommand.
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
if (intent != null) {
if (intent.getAction() != null) {
restoreQueuesAndPositionIfNecessary();
String action = intent.getAction();
switch(action) {
case ACTION_TOGGLE_PAUSE:
if (isPlaying()) {
pause();
} else {
play();
}
break;
case ACTION_PAUSE:
pause();
break;
case ACTION_PLAY:
play();
break;
case ACTION_PLAY_PLAYLIST:
Playlist playlist = intent.getParcelableExtra(INTENT_EXTRA_PLAYLIST);
int shuffleMode = intent.getIntExtra(INTENT_EXTRA_SHUFFLE_MODE, getShuffleMode());
if (playlist != null) {
List<Song> playlistSongs;
if (playlist instanceof AbsCustomPlaylist) {
playlistSongs = ((AbsCustomPlaylist) playlist).getSongs(getApplicationContext());
} else {
// noinspection unchecked
playlistSongs = (List) PlaylistSongLoader.getPlaylistSongList(getApplicationContext(), playlist.id);
}
if (!playlistSongs.isEmpty()) {
if (shuffleMode == SHUFFLE_MODE_SHUFFLE) {
int startPosition = 0;
if (!playlistSongs.isEmpty()) {
startPosition = new Random().nextInt(playlistSongs.size());
}
openQueue(playlistSongs, startPosition, true);
setShuffleMode(shuffleMode);
} else {
openQueue(playlistSongs, 0, true);
}
} else {
Toast.makeText(getApplicationContext(), R.string.playlist_is_empty, Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), R.string.playlist_is_empty, Toast.LENGTH_LONG).show();
}
break;
case ACTION_REWIND:
back(true);
break;
case ACTION_SKIP:
playNextSong(true);
break;
case ACTION_STOP:
case ACTION_QUIT:
pendingQuit = false;
quit();
break;
case ACTION_PENDING_QUIT:
pendingQuit = true;
break;
}
}
}
return START_NOT_STICKY;
}
use of com.kabouzeid.gramophone.model.Song in project Phonograph by kabouzeid.
the class MusicService method sendPublicIntent.
// to let other apps know whats playing. i.E. last.fm (scrobbling) or musixmatch
private void sendPublicIntent(@NonNull final String what) {
final Intent intent = new Intent(what.replace(PHONOGRAPH_PACKAGE_NAME, MUSIC_PACKAGE_NAME));
final Song song = getCurrentSong();
intent.putExtra("id", song.id);
intent.putExtra("artist", song.artistName);
intent.putExtra("album", song.albumName);
intent.putExtra("track", song.title);
intent.putExtra("duration", song.duration);
intent.putExtra("position", (long) getSongProgressMillis());
intent.putExtra("playing", isPlaying());
intent.putExtra("scrobbling_source", PHONOGRAPH_PACKAGE_NAME);
sendStickyBroadcast(intent);
}
use of com.kabouzeid.gramophone.model.Song in project Phonograph by kabouzeid.
the class CardPlayerFragment method updateLyrics.
private void updateLyrics() {
if (updateLyricsAsyncTask != null)
updateLyricsAsyncTask.cancel(false);
final Song song = MusicPlayerRemote.getCurrentSong();
updateLyricsAsyncTask = new AsyncTask<Void, Void, Lyrics>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
lyrics = null;
playerAlbumCoverFragment.setLyrics(null);
toolbar.getMenu().removeItem(R.id.action_show_lyrics);
}
@Override
protected Lyrics doInBackground(Void... params) {
String data = MusicUtil.getLyrics(song);
if (TextUtils.isEmpty(data)) {
return null;
}
return Lyrics.parse(song, data);
}
@Override
protected void onPostExecute(Lyrics l) {
lyrics = l;
playerAlbumCoverFragment.setLyrics(lyrics);
if (lyrics == null) {
if (toolbar != null) {
toolbar.getMenu().removeItem(R.id.action_show_lyrics);
}
} else {
Activity activity = getActivity();
if (toolbar != null && activity != null)
if (toolbar.getMenu().findItem(R.id.action_show_lyrics) == null) {
int color = ToolbarContentTintHelper.toolbarContentColor(activity, Color.TRANSPARENT);
Drawable drawable = ImageUtil.getTintedVectorDrawable(activity, R.drawable.ic_comment_text_outline_white_24dp, color);
toolbar.getMenu().add(Menu.NONE, R.id.action_show_lyrics, Menu.NONE, R.string.action_show_lyrics).setIcon(drawable).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
}
@Override
protected void onCancelled(Lyrics s) {
onPostExecute(null);
}
}.execute();
}
Aggregations