Skip to content

Commit

Permalink
[android] fixes on playlist handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
gubatron committed Feb 9, 2016
1 parent 4bbacdd commit 5dd7c88
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public boolean onCreateOptionsMenu(final Menu menu) {
// Theme the search icon
mResources.setSearchIcon(menu);

final SearchView searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();
final SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Add voice search
final SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
final SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
Expand Down Expand Up @@ -238,9 +238,6 @@ protected void onOptionsItemNewPlaylistSelected() {
MusicUtils.refresh();
}

/**
* {@inheritDoc}
*/
@Override
protected void onResume() {
super.onResume();
Expand All @@ -251,9 +248,6 @@ protected void onResume() {
updateBottomActionBarInfo();
}

/**
* {@inheritDoc}
*/
@Override
protected void onStart() {
super.onStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public class ProfileActivity extends BaseActivity implements OnPageChangeListene
*/
private String mArtistName;

private long mArtistId;

/**
* The main profile title
*/
Expand Down Expand Up @@ -120,6 +122,9 @@ public void onCreate(final Bundle savedInstanceState) {
// Get the artist name
if (isArtist() || isAlbum()) {
mArtistName = mArguments.getString(Config.ARTIST_NAME);
if (isArtist()) {
mArtistId = mArguments.getLong(Config.ID);
}
}
// Initialize the pager adapter
mPagerAdapter = new PagerAdapter(this);
Expand Down Expand Up @@ -262,7 +267,8 @@ public boolean onPrepareOptionsMenu(final Menu menu) {
}

/**
* {@inheritDoc}
* This is the options menu that gets created when inside an artist/album
* on the action bar "..." button.
*/
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
Expand All @@ -282,13 +288,13 @@ public boolean onCreateOptionsMenu(final Menu menu) {
if (isArtist() || isAlbum()) {
// Add the album to a playlist
// If it's an artist, we should add all the songs for that artist.
if (isAlbum()) {
//if (isAlbum()) {
final SubMenu subMenu = menu.addSubMenu(
TabFragmentOrder.ALBUMS_POSITION,
FragmentMenuItems.ADD_TO_PLAYLIST,
Menu.NONE, R.string.add_to_playlist);
MusicUtils.makePlaylistMenu(this, TabFragmentOrder.ALBUMS_POSITION, subMenu, false);
}
//}
}

return super.onCreateOptionsMenu(menu);
Expand All @@ -308,47 +314,29 @@ public boolean onOptionsItemSelected(final MenuItem item) {
return true;

case FragmentMenuItems.PLAYLIST_SELECTED: {
// TODO: Possibly refactor this entire block to just use
// getIntent().getLongArrayExtra(Config.TRACKS)
// Not sure yet if it'll work for all cases.

// Add to existing playlist or to new playlist
if (isAlbum()) {
if (isAlbum() || isArtist()) {
long playlistId = -1;
// playlist id has been bundled with an intent extra along with the menu item.
if (item.getIntent()!=null && item.getIntent().hasExtra("playlist")) {
playlistId = item.getIntent().getLongExtra("playlist", -1);
}

// TODO: ReDo this by passing list of tracks to Activity on intent.
System.out.println("It is an album! Add it to playlist id.");
if (playlistId != -1) {
System.out.println("playlist we're adding to: " + playlistId);
System.out.println("item.getItemId() -> " + item.getItemId() + " == FragmentMenuItems.ADD_TO_PLAYLIST ("+FragmentMenuItems.ADD_TO_PLAYLIST+")");
final ProfileSongAdapter adapter = getAlbumSongFragment().getAdapter();
if (adapter != null && adapter.getCount() > 0) {
// for some reason they changed .getCount() to return n+1 if size > 0.
long songIds[] = new long[adapter.getCount()-1];
for (int i=0; i < adapter.getCount()-1; i++) {
final Song song = adapter.getItem(i);
System.out.println("Will add this song: (" + song.mSongId + ") " + song.mSongName);
songIds[i] = song.mSongId;
}

MusicUtils.addToPlaylist(this, songIds, playlistId);
long[] tracks = mArguments.getLongArray(Config.TRACKS);
if (playlistId != -1 && tracks != null && tracks.length > 0) {
MusicUtils.addToPlaylist(this, tracks, playlistId);
}
}
return true;

} else if (isArtist()){
// TODO:
}
return true;
}
case android.R.id.home:
// If an album profile, go up to the artist profile
if (isAlbum()) {
NavUtils.openArtistProfile(this, mArtistName);
final Long artistId = mArguments.getLong(Config.ID);
long[] tracks = MusicUtils.getSongListForArtist(this, artistId);
NavUtils.openArtistProfile(this, mArtistName, tracks);
finish();
} else {
// Otherwise just go back
Expand Down Expand Up @@ -628,7 +616,7 @@ public void selectNewPhoto() {
}

/**
* Fetchs for the artist or album art, other wise sets the default header
* Fetches for the artist or album art, other wise sets the default header
* image.
*/
public void selectOldPhoto() {
Expand Down Expand Up @@ -697,39 +685,39 @@ private void goBack() {
* @return True if the MIME type is vnd.android.cursor.dir/artists, false
* otherwise.
*/
private final boolean isArtist() {
private boolean isArtist() {
return mType.equals(MediaStore.Audio.Artists.CONTENT_TYPE);
}

/**
* @return True if the MIME type is vnd.android.cursor.dir/albums, false
* otherwise.
*/
private final boolean isAlbum() {
private boolean isAlbum() {
return mType.equals(MediaStore.Audio.Albums.CONTENT_TYPE);
}

/**
* @return True if the MIME type is vnd.android.cursor.dir/gere, false
* otherwise.
*/
private final boolean isGenre() {
private boolean isGenre() {
return mType.equals(MediaStore.Audio.Genres.CONTENT_TYPE);
}

/**
* @return True if the MIME type is vnd.android.cursor.dir/playlist, false
* otherwise.
*/
private final boolean isPlaylist() {
private boolean isPlaylist() {
return mType.equals(MediaStore.Audio.Playlists.CONTENT_TYPE);
}

/**
* @return True if the MIME type is one of the playlist types and the playlist is empty, false
* otherwise.
*/
private final boolean isEmptyPlaylist() {
private boolean isEmptyPlaylist() {
long[] list = null;
if (isPlaylist()) {
final long id = mArguments.getLong(Config.ID);
Expand All @@ -745,14 +733,14 @@ private final boolean isEmptyPlaylist() {
/**
* @return True if the MIME type is "Favorites", false otherwise.
*/
private final boolean isFavorites() {
private boolean isFavorites() {
return mType.equals(getString(R.string.playlist_favorites));
}

/**
* @return True if the MIME type is "LastAdded", false otherwise.
*/
private final boolean isLastAdded() {
private boolean isLastAdded() {
return mType.equals(getString(R.string.playlist_last_added));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public void onItemClick(final AdapterView<?> parent, final View view, final int
// If it's an artist, open the artist profile
if ("artist".equals(mimeType)) {
NavUtils.openArtistProfile(this,
cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST)));
cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST)),null);
} else if ("album".equals(mimeType)) {
// If it's an album, open the album profile
int albumId = cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums._ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ protected String getLayoutTypeName() {
public void onItemClick(final AdapterView<?> parent, final View view, final int position,
final long id) {
mItem = mAdapter.getItem(position);
NavUtils.openArtistProfile(getActivity(), mItem.mArtistName);
long[] tracks = MusicUtils.getSongListForArtist(getActivity(), mItem.mArtistId);
NavUtils.openArtistProfile(getActivity(), mItem.mArtistName, tracks);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.view.ContextMenu;
import android.view.*;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import com.andrew.apollo.Config;
Expand All @@ -32,6 +31,7 @@
import com.andrew.apollo.menu.FragmentMenuItems;
import com.andrew.apollo.menu.RenamePlaylist;
import com.andrew.apollo.model.Playlist;
import com.andrew.apollo.ui.activities.BaseActivity;
import com.andrew.apollo.ui.activities.ProfileActivity;
import com.andrew.apollo.ui.fragments.profile.ApolloFragment;
import com.andrew.apollo.utils.MusicUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ public void onPrepareOptionsMenu(final Menu menu) {
@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
if (isPlaylistPage()) {
inflater.inflate(R.menu.new_playlist, menu);
}

// Favorite action
inflater.inflate(R.menu.favorite, menu);
// Shuffle all
Expand Down Expand Up @@ -338,6 +342,10 @@ private boolean isRecentPage() {
return mViewPager.getCurrentItem() == TabFragmentOrder.RECENT_POSITION;
}

private boolean isPlaylistPage() {
return mViewPager.getCurrentItem() == TabFragmentOrder.PLAYLISTS_POSITION;
}

private RecentFragment getRecentFragment() {
return (RecentFragment)mPagerAdapter.getFragment(TabFragmentOrder.RECENT_POSITION);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import com.andrew.apollo.provider.RecentStore;
import com.andrew.apollo.recycler.RecycleHolder;
import com.andrew.apollo.ui.activities.BaseActivity;
import com.andrew.apollo.ui.fragments.Fragments;
import com.andrew.apollo.utils.ApolloUtils;
import com.andrew.apollo.utils.MusicUtils;
import com.andrew.apollo.utils.NavUtils;
Expand Down Expand Up @@ -271,6 +270,7 @@ public boolean onContextItemSelected(final android.view.MenuItem item) {
return true;
case FragmentMenuItems.NEW_PLAYLIST:
CreateNewPlaylist.getInstance(songList).show(getFragmentManager(), "CreatePlaylist");
refresh();
return true;
case FragmentMenuItems.PLAYLIST_SELECTED:
final long playlistId = item.getIntent().getLongExtra("playlist", 0);
Expand All @@ -283,7 +283,8 @@ public boolean onContextItemSelected(final android.view.MenuItem item) {
case FragmentMenuItems.DELETE:
return onDelete(songList);
case FragmentMenuItems.MORE_BY_ARTIST:
NavUtils.openArtistProfile(getActivity(), mArtistName);
long[] tracks = MusicUtils.getSongListForArtist(getActivity(), mSelectedId);
NavUtils.openArtistProfile(getActivity(), mArtistName, tracks);
return true;
case FragmentMenuItems.REMOVE_FROM_PLAYLIST:
return onRemoveFromPlaylist();
Expand Down
22 changes: 22 additions & 0 deletions android/apollo/src/com/andrew/apollo/utils/MusicUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,28 @@ public static long[] getSongListForArtist(final Context context, final long id)
return sEmptyList;
}

public static String getAlbumName(final Context context, final long id) {
String albumName = null;
final String[] projection = new String[] {
AudioColumns.ALBUM
};
final String selection = AudioColumns.ALBUM_ID + "=" + id + " AND " + AudioColumns.IS_MUSIC + "=1";
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
null,
null
);
if (cursor != null) {
cursor.moveToFirst();
albumName = cursor.getString(0);
cursor.close();
}
return albumName;
}

/**
* @param context The {@link Context} to use.
* @param id The ID of the album.
Expand Down
7 changes: 5 additions & 2 deletions android/apollo/src/com/andrew/apollo/utils/NavUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public final class NavUtils {
* @param context The {@link Activity} to use.
* @param artistName The name of the artist
*/
public static void openArtistProfile(final Activity context,
final String artistName) {
public static void openArtistProfile(final Activity context, final String artistName, final long[] songs) {
if (artistName == null || artistName.isEmpty()) {
return;
}
Expand All @@ -54,6 +53,10 @@ public static void openArtistProfile(final Activity context,
bundle.putString(Config.MIME_TYPE, MediaStore.Audio.Artists.CONTENT_TYPE);
bundle.putString(Config.ARTIST_NAME, artistName);

if (songs != null && songs.length > 0) {
bundle.putLongArray(Config.TRACKS, songs);
}

// Create the intent to launch the profile activity
final Intent intent = new Intent(context, ProfileActivity.class);
intent.putExtras(bundle);
Expand Down

0 comments on commit 5dd7c88

Please sign in to comment.