Beispiel #1
0
 /**
  * @param context
  * @param id
  * @return
  */
 public static long[] getSongListForGenre(Context context, long id) {
   String[] projection = new String[] {BaseColumns._ID};
   StringBuilder selection = new StringBuilder();
   selection.append(AudioColumns.IS_MUSIC + "=1");
   selection.append(" AND " + MediaColumns.TITLE + "!=''");
   Uri uri = Genres.Members.getContentUri(EXTERNAL, id);
   Cursor cursor =
       context.getContentResolver().query(uri, projection, selection.toString(), null, null);
   if (cursor != null) {
     long[] list = getSongListForCursor(cursor);
     cursor.close();
     return list;
   }
   return sEmptyList;
 }
  @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);
  }