Skip to content

Commit

Permalink
[android] more player code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
gubatron committed Feb 2, 2016
1 parent b9ff717 commit 9dd4997
Show file tree
Hide file tree
Showing 29 changed files with 126 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public abstract class ApolloFragmentAdapter<I> extends ArrayAdapter<I> {
/**
* Used to set the size of the data in the adapter
*/
protected List<I> mCount = Lists.newArrayList();
protected List<I> mDataList = Lists.newArrayList();

/**
* Image cache and image fetcher
Expand All @@ -77,7 +77,7 @@ public ApolloFragmentAdapter(Context context, int mLayoutId) {
*/
public void unload() {
mData = null;
mCount.clear();
mDataList.clear();
clear();
}

Expand All @@ -90,8 +90,8 @@ public void flush() {
/**
* @param data The {@link List} used to return the count for the adapter.
*/
public void setCount(final List<I> data) {
mCount = data;
public void setDataList(final List<I> data) {
mDataList = data;
}

public abstract long getItemId(int position);
Expand All @@ -109,7 +109,7 @@ protected void playAlbum(final ImageView album, final int position) {
public void onClick(final View v) {
final long id = getItemId(position);
final long[] list = MusicUtils.getSongListForAlbum(getContext(), id);
MusicUtils.playAll(getContext(), list, 0, false);
MusicUtils.playAll(list, 0, false);
}
});
}
Expand Down Expand Up @@ -159,7 +159,7 @@ public boolean hasStableIds() {
*/
@Override
public int getCount() {
final int size = mCount.size();
final int size = mDataList.size();
return size == 0 ? 0 : size + 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private void playArtist(final ImageView artist, final int position) {
public void onClick(final View v) {
final long id = getItem(position).mArtistId;
final long[] list = MusicUtils.getSongListForArtist(getContext(), id);
MusicUtils.playAll(getContext(), list, 0, false);
MusicUtils.playAll(list, 0, false);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
import com.andrew.apollo.model.Album;
import com.andrew.apollo.ui.MusicHolder;
import com.andrew.apollo.ui.fragments.profile.ArtistAlbumFragment;
import com.andrew.apollo.utils.Lists;
import com.andrew.apollo.utils.MusicUtils;
import com.frostwire.android.R;
import com.frostwire.util.Ref;

import java.util.List;

/**
* This {@link ArrayAdapter} is used to display the albums for a particular
* artist for {@link ArtistAlbumFragment} .
Expand Down Expand Up @@ -62,11 +59,6 @@ public class ArtistAlbumAdapter extends ApolloFragmentAdapter<Album> {
*/
private final View mHeader;

/**
* Used to set the size of the data in the adapter
*/
private List<Album> mCount = Lists.newArrayList();

/**
* Constructor of <code>ArtistAlbumAdapter</code>
*
Expand All @@ -77,6 +69,7 @@ public ArtistAlbumAdapter(final Activity context, final int layoutId) {
super(context, layoutId);
// Used to create the custom layout
mInflater = LayoutInflater.from(context);

// Cache the header
mHeader = mInflater.inflate(R.layout.faux_carousel, null);
}
Expand Down Expand Up @@ -153,7 +146,15 @@ public long getItemId(final int position) {
if (position == 0) {
return -1;
}
return position - 1;

int realPosition = position-1;
if (mData != null && realPosition < mData.length) {
return mData[realPosition].mItemId;
} else if (!mDataList.isEmpty() && position < mDataList.size()) {
return mDataList.get(realPosition).mAlbumId;
}

return - 1;
}

/**
Expand All @@ -170,7 +171,7 @@ protected void playAlbum(final ImageView album, final int position) {
public void onClick(final View v) {
final long id = getItem(position - 1).mAlbumId;
final long[] list = MusicUtils.getSongListForAlbum(getContext(), id);
MusicUtils.playAll(getContext(), list, 0, false);
MusicUtils.playAll(list, 0, false);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.andrew.apollo.ui.fragments.profile.PlaylistSongFragment;
import com.andrew.apollo.utils.Lists;
import com.andrew.apollo.utils.MusicUtils;
import com.frostwire.logging.Logger;

import java.util.List;

Expand All @@ -44,6 +45,8 @@
*/
public class ProfileSongAdapter extends ApolloFragmentAdapter<Song> {

public static final Logger LOGGER = Logger.getLogger(ProfileSongAdapter.class);

/**
* Default display setting: title/album
*/
Expand Down Expand Up @@ -160,6 +163,12 @@ public View getView(final int position, View convertView, final ViewGroup parent
// Retrieve the album
final Song song = getItem(position - 1);

if (song == null) {
LOGGER.info("getItem("+position+" - 1) == null");
} else {
LOGGER.info("getItem("+position+" - 1) == " + song.mSongId);
}

// Set each track name (line one)
holder.mLineOne.get().setText(song.mSongName);
// Set the line two
Expand Down Expand Up @@ -205,15 +214,6 @@ public boolean hasStableIds() {
return true;
}

/**
* {@inheritDoc}
*/
@Override
public int getCount() {
final int size = mCount.size();
return size == 0 ? 0 : size + 1;
}

/**
* {@inheritDoc}
*/
Expand Down
19 changes: 10 additions & 9 deletions android/apollo/src/com/andrew/apollo/loaders/AlbumLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,13 @@
import java.util.List;

/**
* Used to query {@link MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI} and return
* Used to query MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI and return
* the albums on a user's device.
*
* @author Andrew Neal (andrewdneal@gmail.com)
* @author Angel Leon (gubatron@gmail.com)
*/
public class AlbumLoader extends WrappedAsyncTaskLoader<List<Album>> {

/**
* The result
*/
private final ArrayList<Album> mAlbumsList = Lists.newArrayList();

/**
* Constructor of <code>AlbumLoader</code>
*
Expand All @@ -51,10 +46,12 @@ public AlbumLoader(final Context context) {
*/
@Override
public List<Album> loadInBackground() {
final ArrayList<Album> mAlbumsList = Lists.newArrayList();

// Create the Cursor
Cursor mCursor;
try {
mCursor = makeAlbumCursor(getContext());
mCursor = makeCursor(getContext());
} catch (Throwable e) {
return Collections.EMPTY_LIST;
}
Expand Down Expand Up @@ -97,7 +94,7 @@ public List<Album> loadInBackground() {
* @param context The {@link Context} to use.
* @return The {@link Cursor} used to run the album query.
*/
public static final Cursor makeAlbumCursor(final Context context) {
private Cursor makeAlbumCursor(final Context context) {
return context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[] {
/* 0 */
Expand All @@ -112,4 +109,8 @@ public static final Cursor makeAlbumCursor(final Context context) {
AlbumColumns.FIRST_YEAR
}, null, null, PreferenceUtils.getInstance(context).getAlbumSortOrder());
}

public Cursor makeCursor(final Context context) {
return makeAlbumCursor(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,16 @@
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.AlbumColumns;

import com.frostwire.android.R;
import com.andrew.apollo.model.Album;
import com.andrew.apollo.utils.Lists;
import com.andrew.apollo.utils.PreferenceUtils;

import java.util.ArrayList;
import java.util.List;

/**
* Used to query {@link MediaStore.Audio.Artists.Albums} and return the albums
* for a particular artist.
*
* @author Andrew Neal (andrewdneal@gmail.com)
* @author Angel Leon (gubatron@gmail.com)
*/
public class ArtistAlbumLoader extends WrappedAsyncTaskLoader<List<Album>> {

/**
* The result
*/
private final ArrayList<Album> mAlbumsList = Lists.newArrayList();

/**
* The {@link Cursor} used to run the query.
*/
private Cursor mCursor;
public class ArtistAlbumLoader extends AlbumLoader {

/**
* The Id of the artist the albums belong to.
Expand All @@ -59,51 +43,16 @@ public ArtistAlbumLoader(final Context context, final Long artistId) {
mArtistID = artistId;
}

/**
* {@inheritDoc}
*/
@Override
public List<Album> loadInBackground() {
// Create the Cursor
mCursor = makeArtistAlbumCursor(getContext(), mArtistID);
// Gather the dataS
if (mCursor != null && mCursor.moveToFirst()) {
do {
// Copy the album id
final long id = mCursor.getLong(0);

// Copy the album name
final String albumName = mCursor.getString(1);

// Copy the artist name
final String artist = mCursor.getString(2);

// Copy the number of songs
final int songCount = mCursor.getInt(3);

// Copy the release year
final String year = mCursor.getString(4);

// Create a new album
final Album album = new Album(id, albumName, artist, songCount, year);

// Add everything up
mAlbumsList.add(album);
} while (mCursor.moveToNext());
}
// Close the cursor
if (mCursor != null) {
mCursor.close();
mCursor = null;
}
return mAlbumsList;
public Cursor makeCursor(Context context) {
return makeArtistAlbumCursor(context, mArtistID);
}

/**
* @param context The {@link Context} to use.
* @param artistId The Id of the artist the albums belong to.
*/
private static final Cursor makeArtistAlbumCursor(final Context context, final Long artistId) {
private static Cursor makeArtistAlbumCursor(final Context context, final Long artistId) {
if (artistId == -1) {
// fix an error reported in Play console
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
import java.util.List;

/**
* Used to query {@link MediaStore.Audio.Media.EXTERNAL_CONTENT_URI} and return
* Used to query {MediaStore.Audio.Media.EXTERNAL_CONTENT_URI} and return
* the songs for a particular artist.
*
* @author Andrew Neal (andrewdneal@gmail.com)
* @author Angel Leon (gubatron@gmail.com)
*/
public class ArtistSongLoader extends WrappedAsyncTaskLoader<List<Song>> {

Expand Down
20 changes: 5 additions & 15 deletions android/apollo/src/com/andrew/apollo/loaders/GenreLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,21 @@
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.GenresColumns;

import com.andrew.apollo.model.Genre;
import com.andrew.apollo.utils.Lists;

import java.util.ArrayList;
import java.util.List;

/**
* Used to query {@link MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI} and return
* Used to query MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI and return
* the genres on a user's device.
*
* @author Andrew Neal (andrewdneal@gmail.com)
* @author Angel Leon (gubatron@gmail.com)
*/
public class GenreLoader extends WrappedAsyncTaskLoader<List<Genre>> {

/**
* The result
*/
private final ArrayList<Genre> mGenreList = Lists.newArrayList();

/**
* Constructor of <code>GenreLoader</code>
*
Expand All @@ -50,10 +45,7 @@ public GenreLoader(final Context context) {
*/
@Override
public List<Genre> loadInBackground() {

if (!mGenreList.isEmpty()) {
mGenreList.clear();
}
ArrayList<Genre> mGenreList = Lists.newArrayList();

// Create the Cursor
Cursor mCursor = makeGenreCursor(getContext());
Expand Down Expand Up @@ -86,15 +78,13 @@ public List<Genre> loadInBackground() {
* @param context The {@link Context} to use.
* @return The {@link Cursor} used to run the genre query.
*/
public static final Cursor makeGenreCursor(final Context context) {
final StringBuilder selection = new StringBuilder();
selection.append(MediaStore.Audio.Genres.NAME + " != ''");
public static Cursor makeGenreCursor(final Context context) {
return context.getContentResolver().query(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI,
new String[] {
/* 0 */
BaseColumns._ID,
/* 1 */
GenresColumns.NAME
}, selection.toString(), null, MediaStore.Audio.Genres.DEFAULT_SORT_ORDER);
}, (MediaStore.Audio.Genres.NAME + " != ''"), null, MediaStore.Audio.Genres.DEFAULT_SORT_ORDER);
}
}
6 changes: 5 additions & 1 deletion android/apollo/src/com/andrew/apollo/loaders/SongLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,17 @@ public List<Song> loadInBackground() {
return mSongList;
}

public static Cursor makeCursor(final Context context) {
return makeSongCursor(context);
}

/**
* Creates the {@link Cursor} used to run the query.
*
* @param context The {@link Context} to use.
* @return The {@link Cursor} used to run the song query.
*/
public static final Cursor makeSongCursor(final Context context) {
private static Cursor makeSongCursor(final Context context) {
final StringBuilder mSelection = new StringBuilder();
mSelection.append(AudioColumns.IS_MUSIC + "=1");
mSelection.append(" AND " + AudioColumns.TITLE + " != ''"); //$NON-NLS-2$
Expand Down

0 comments on commit 9dd4997

Please sign in to comment.