/** * @param context * @param ids * @param playlistid */ public static void addToPlaylist(Context context, long[] ids, long playlistid) { if (ids == null) { } else { int size = ids.length; ContentResolver resolver = context.getContentResolver(); // need to determine the number of items currently in the playlist, // so the play_order field can be maintained. String[] cols = new String[] {"count(*)"}; Uri uri = Playlists.Members.getContentUri(EXTERNAL, playlistid); Cursor cur = resolver.query(uri, cols, null, null, null); cur.moveToFirst(); int base = cur.getInt(0); cur.close(); int numinserted = 0; for (int i = 0; i < size; i += 1000) { makeInsertItems(ids, i, 1000, base); numinserted += resolver.bulkInsert(uri, sContentValuesCache); } String message = context .getResources() .getQuantityString(R.plurals.NNNtrackstoplaylist, numinserted, numinserted); Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); } }
/** * @param context * @param id * @return */ public static long[] getSongListForPlaylist(Context context, long id) { final String[] projection = new String[] {Playlists.Members.AUDIO_ID}; String sortOrder = Playlists.Members.DEFAULT_SORT_ORDER; Uri uri = Playlists.Members.getContentUri(EXTERNAL, id); Cursor cursor = query(context, uri, projection, null, null, sortOrder); if (cursor != null) { long[] list = getSongListForCursor(cursor); cursor.close(); return list; } return sEmptyList; }
/** @param which */ private void removePlaylistItem(int which) { mCursor.moveToPosition(which); long id = mCursor.getLong(mMediaIdIndex); if (mPlaylistId >= 0) { Uri uri = Playlists.Members.getContentUri(EXTERNAL, mPlaylistId); getActivity().getContentResolver().delete(uri, Playlists.Members.AUDIO_ID + "=" + id, null); } else if (mPlaylistId == PLAYLIST_QUEUE) { MusicUtils.removeTrack(id); reloadQueueCursor(); } else if (mPlaylistId == PLAYLIST_FAVORITES) { MusicUtils.removeFromFavorites(getActivity(), id); } mListView.invalidateViews(); }
/** * @param context * @param id */ public static void removeFromFavorites(Context context, long id) { long favorites_id; if (id < 0) { } else { ContentResolver resolver = context.getContentResolver(); String favorites_where = PlaylistsColumns.NAME + "='" + PLAYLIST_NAME_FAVORITES + "'"; String[] favorites_cols = new String[] {BaseColumns._ID}; Uri favorites_uri = Playlists.EXTERNAL_CONTENT_URI; Cursor cursor = resolver.query(favorites_uri, favorites_cols, favorites_where, null, null); if (cursor.getCount() <= 0) { favorites_id = createPlaylist(context, PLAYLIST_NAME_FAVORITES); } else { cursor.moveToFirst(); favorites_id = cursor.getLong(0); cursor.close(); } Uri uri = Playlists.Members.getContentUri(EXTERNAL, favorites_id); resolver.delete(uri, Playlists.Members.AUDIO_ID + "=" + id, null); } }
/** * @param context * @param id */ public static void addToFavorites(Context context, long id) { long favorites_id; if (id < 0) { } else { ContentResolver resolver = context.getContentResolver(); String favorites_where = PlaylistsColumns.NAME + "='" + PLAYLIST_NAME_FAVORITES + "'"; String[] favorites_cols = new String[] {BaseColumns._ID}; Uri favorites_uri = Playlists.EXTERNAL_CONTENT_URI; Cursor cursor = resolver.query(favorites_uri, favorites_cols, favorites_where, null, null); if (cursor.getCount() <= 0) { favorites_id = createPlaylist(context, PLAYLIST_NAME_FAVORITES); } else { cursor.moveToFirst(); favorites_id = cursor.getLong(0); cursor.close(); } String[] cols = new String[] {Playlists.Members.AUDIO_ID}; Uri uri = Playlists.Members.getContentUri(EXTERNAL, favorites_id); Cursor cur = resolver.query(uri, cols, null, null, null); int base = cur.getCount(); cur.moveToFirst(); while (!cur.isAfterLast()) { if (cur.getLong(0) == id) return; cur.moveToNext(); } cur.close(); ContentValues values = new ContentValues(); values.put(Playlists.Members.AUDIO_ID, id); values.put(Playlists.Members.PLAY_ORDER, base + 1); resolver.insert(uri, values); } }
/** * @param context * @param id * @return */ public static boolean isFavorite(Context context, long id) { long favorites_id; if (id < 0) { } else { ContentResolver resolver = context.getContentResolver(); String favorites_where = PlaylistsColumns.NAME + "='" + PLAYLIST_NAME_FAVORITES + "'"; String[] favorites_cols = new String[] {BaseColumns._ID}; Uri favorites_uri = Playlists.EXTERNAL_CONTENT_URI; Cursor cursor = resolver.query(favorites_uri, favorites_cols, favorites_where, null, null); if (cursor.getCount() <= 0) { favorites_id = createPlaylist(context, PLAYLIST_NAME_FAVORITES); } else { cursor.moveToFirst(); favorites_id = cursor.getLong(0); cursor.close(); } String[] cols = new String[] {Playlists.Members.AUDIO_ID}; Uri uri = Playlists.Members.getContentUri(EXTERNAL, favorites_id); Cursor cur = resolver.query(uri, cols, null, null, null); cur.moveToFirst(); while (!cur.isAfterLast()) { if (cur.getLong(0) == id) { cur.close(); return true; } cur.moveToNext(); } cur.close(); return false; } return false; }
/** * @param context * @param plid */ public static void clearPlaylist(Context context, int plid) { Uri uri = Playlists.Members.getContentUri(EXTERNAL, plid); context.getContentResolver().delete(uri, null, null); return; }
@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); }