Allow the MediaAdapter to be used without an activity

This commit is contained in:
Adrian Ulrich 2015-09-17 18:59:06 +02:00
parent 435bab2000
commit 1d9cc03105
2 changed files with 25 additions and 11 deletions

View File

@ -313,26 +313,26 @@ public class LibraryPagerAdapter
switch (type) {
case MediaUtils.TYPE_ARTIST:
adapter = mArtistAdapter = new MediaAdapter(activity, MediaUtils.TYPE_ARTIST, null, looper);
adapter = mArtistAdapter = new MediaAdapter(activity, MediaUtils.TYPE_ARTIST, null, activity, looper);
mArtistAdapter.setExpandable(mSongsPosition != -1 || mAlbumsPosition != -1);
mArtistHeader = header = (LinearLayout)inflater.inflate(R.layout.library_row_expandable, null);
break;
case MediaUtils.TYPE_ALBUM:
adapter = mAlbumAdapter = new MediaAdapter(activity, MediaUtils.TYPE_ALBUM, mPendingAlbumLimiter, looper);
adapter = mAlbumAdapter = new MediaAdapter(activity, MediaUtils.TYPE_ALBUM, mPendingAlbumLimiter, activity, looper);
mAlbumAdapter.setExpandable(mSongsPosition != -1);
mPendingAlbumLimiter = null;
mAlbumHeader = header = (LinearLayout)inflater.inflate(R.layout.library_row_expandable, null);
break;
case MediaUtils.TYPE_SONG:
adapter = mSongAdapter = new MediaAdapter(activity, MediaUtils.TYPE_SONG, mPendingSongLimiter, looper);
adapter = mSongAdapter = new MediaAdapter(activity, MediaUtils.TYPE_SONG, mPendingSongLimiter, activity, looper);
mPendingSongLimiter = null;
mSongHeader = header = (LinearLayout)inflater.inflate(R.layout.library_row_expandable, null);
break;
case MediaUtils.TYPE_PLAYLIST:
adapter = mPlaylistAdapter = new MediaAdapter(activity, MediaUtils.TYPE_PLAYLIST, null, looper);
adapter = mPlaylistAdapter = new MediaAdapter(activity, MediaUtils.TYPE_PLAYLIST, null, activity, looper);
break;
case MediaUtils.TYPE_GENRE:
adapter = mGenreAdapter = new MediaAdapter(activity, MediaUtils.TYPE_GENRE, null, looper);
adapter = mGenreAdapter = new MediaAdapter(activity, MediaUtils.TYPE_GENRE, null, activity, looper);
mGenreAdapter.setExpandable(mSongsPosition != -1);
break;
case MediaUtils.TYPE_FILE:

View File

@ -68,6 +68,10 @@ public class MediaAdapter
/**
* A context to use.
*/
private final Context mContext;
/**
* The library activity to use.
*/
private final LibraryActivity mActivity;
/**
* A LayoutInflater to use.
@ -146,20 +150,30 @@ public class MediaAdapter
* Construct a MediaAdapter representing the given <code>type</code> of
* media.
*
* @param activity The LibraryActivity that will contain this adapter.
* @param context The Context used to access the content model.
* @param type The type of media to represent. Must be one of the
* Song.TYPE_* constants. This determines which content provider to query
* and what fields to display in the views.
* @param limiter An initial limiter to use
* @param activity The LibraryActivity that will contain this adapter - may be null
* @param looper The looper to use for image processing - may be null
*
*/
public MediaAdapter(LibraryActivity activity, int type, Limiter limiter, Looper looper)
public MediaAdapter(Context context, int type, Limiter limiter, LibraryActivity activity, Looper looper)
{
mContext = context;
mActivity = activity;
mType = type;
mLimiter = limiter;
mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mLooper = looper;
if (mActivity != null) {
mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} else {
mInflater = null; // not running inside an activity
}
mCoverCacheType = MediaUtils.TYPE_INVALID;
String coverCacheKey = "0"; // SQL dummy entry
@ -267,7 +281,7 @@ public class MediaAdapter
// Magic sort mode: sort by playcount
if (sortStringRaw == SORT_MAGIC_PLAYCOUNT) {
ArrayList<Long> topSongs = (new PlayCountsHelper(mActivity)).getTopSongs(4096);
ArrayList<Long> topSongs = (new PlayCountsHelper(mContext)).getTopSongs(4096);
int sortWeight = -1 * topSongs.size(); // Sort mode is actually reversed (default: mostplayed -> leastplayed)
StringBuilder sb = new StringBuilder("CASE WHEN _id=0 THEN 0"); // include dummy statement in initial string -> topSongs may be empty
@ -340,9 +354,9 @@ public class MediaAdapter
}
@Override
public Object query()
public Cursor query()
{
return buildQuery(mProjection, false).runQuery(mActivity.getContentResolver());
return buildQuery(mProjection, false).runQuery(mContext.getContentResolver());
}
@Override