@Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { String[] projection = new String[] { BaseColumns._ID, MediaColumns.TITLE, AudioColumns.ALBUM, AudioColumns.ARTIST }; StringBuilder where = new StringBuilder(); String sortOrder = Audio.Media.DEFAULT_SORT_ORDER; where.append(AudioColumns.IS_MUSIC + "=1").append(" AND " + MediaColumns.TITLE + " != ''"); Uri uri = Audio.Media.EXTERNAL_CONTENT_URI; if (getArguments() != null) { mPlaylistId = getArguments().getLong(BaseColumns._ID); String mimeType = getArguments().getString(MIME_TYPE); if (Audio.Playlists.CONTENT_TYPE.equals(mimeType)) { where = new StringBuilder(); where.append(AudioColumns.IS_MUSIC + "=1"); where.append(" AND " + MediaColumns.TITLE + " != ''"); switch ((int)mPlaylistId) { case (int)PLAYLIST_QUEUE: uri = Audio.Media.EXTERNAL_CONTENT_URI; long[] mNowPlaying = MusicUtils.getQueue(); if (mNowPlaying.length == 0) return null; where = new StringBuilder(); where.append(BaseColumns._ID + " IN ("); if (mNowPlaying == null || mNowPlaying.length <= 0) return null; for (long queue_id : mNowPlaying) { where.append(queue_id + ","); } where.deleteCharAt(where.length() - 1); where.append(")"); break; case (int)PLAYLIST_FAVORITES: long favorites_id = MusicUtils.getFavoritesId(getActivity()); projection = new String[] { Playlists.Members._ID, Playlists.Members.AUDIO_ID, MediaColumns.TITLE, AudioColumns.ALBUM, AudioColumns.ARTIST }; uri = Playlists.Members.getContentUri(EXTERNAL, favorites_id); sortOrder = Playlists.Members.DEFAULT_SORT_ORDER; break; default: if (id < 0) return null; projection = new String[] { Playlists.Members._ID, Playlists.Members.AUDIO_ID, MediaColumns.TITLE, AudioColumns.ALBUM, AudioColumns.ARTIST, AudioColumns.DURATION }; uri = Playlists.Members.getContentUri(EXTERNAL, mPlaylistId); sortOrder = Playlists.Members.DEFAULT_SORT_ORDER; break; } } else if (Audio.Genres.CONTENT_TYPE.equals(mimeType)) { long genreId = getArguments().getLong(BaseColumns._ID); uri = Genres.Members.getContentUri(EXTERNAL, genreId); projection = new String[] { BaseColumns._ID, MediaColumns.TITLE, AudioColumns.ALBUM, AudioColumns.ARTIST }; where = new StringBuilder(); where.append(AudioColumns.IS_MUSIC + "=1").append( " AND " + MediaColumns.TITLE + " != ''"); sortOrder = Genres.Members.DEFAULT_SORT_ORDER; } else { if (Audio.Albums.CONTENT_TYPE.equals(mimeType)) { long albumId = getArguments().getLong(BaseColumns._ID); where.append(" AND " + AudioColumns.ALBUM_ID + "=" + albumId); sortOrder = Audio.Media.TRACK + ", " + sortOrder; } else if (Audio.Artists.CONTENT_TYPE.equals(mimeType)) { sortOrder = MediaColumns.TITLE; long artist_id = getArguments().getLong(BaseColumns._ID); where.append(" AND " + AudioColumns.ARTIST_ID + "=" + artist_id); } } } return new CursorLoader(getActivity(), uri, projection, where.toString(), null, sortOrder); }
@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { // Check for database errors if (data == null) { return; } if (getArguments() != null && Playlists.CONTENT_TYPE.equals(getArguments().getString(MIME_TYPE)) && (getArguments().getLong(BaseColumns._ID) >= 0 || getArguments().getLong( BaseColumns._ID) == PLAYLIST_FAVORITES)) { mMediaIdIndex = data.getColumnIndexOrThrow(Playlists.Members.AUDIO_ID); mTitleIndex = data.getColumnIndexOrThrow(MediaColumns.TITLE); mAlbumIndex = data.getColumnIndexOrThrow(AudioColumns.ALBUM); // FIXME // mArtistIndex = // data.getColumnIndexOrThrow(Playlists.Members.ARTIST); } else if (getArguments() != null && Genres.CONTENT_TYPE.equals(getArguments().getString(MIME_TYPE))) { mMediaIdIndex = data.getColumnIndexOrThrow(BaseColumns._ID); mTitleIndex = data.getColumnIndexOrThrow(MediaColumns.TITLE); mArtistIndex = data.getColumnIndexOrThrow(AudioColumns.ARTIST); mAlbumIndex = data.getColumnIndexOrThrow(AudioColumns.ALBUM); } else if (getArguments() != null && Artists.CONTENT_TYPE.equals(getArguments().getString(MIME_TYPE))) { mTitleIndex = data.getColumnIndexOrThrow(MediaColumns.TITLE); // mArtistIndex is "line2" of the ListView mArtistIndex = data.getColumnIndexOrThrow(AudioColumns.ALBUM); } else if (getArguments() != null && Albums.CONTENT_TYPE.equals(getArguments().getString(MIME_TYPE))) { mMediaIdIndex = data.getColumnIndexOrThrow(BaseColumns._ID); mTitleIndex = data.getColumnIndexOrThrow(MediaColumns.TITLE); mArtistIndex = data.getColumnIndexOrThrow(AudioColumns.ARTIST); } else { mMediaIdIndex = data.getColumnIndexOrThrow(BaseColumns._ID); mTitleIndex = data.getColumnIndexOrThrow(MediaColumns.TITLE); mArtistIndex = data.getColumnIndexOrThrow(AudioColumns.ARTIST); mAlbumIndex = data.getColumnIndexOrThrow(AudioColumns.ALBUM); } mTrackAdapter.changeCursor(data); mListView.invalidateViews(); mCursor = data; }