package com.annimon.playlisteditor; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import com.annimon.playlisteditor.data.Playlist; import com.annimon.playlisteditor.data.Track; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; import android.text.TextUtils; public class PlaylistDatabase { public static void createPlaylist(Context context, String name, int[] trackIds) { if (TextUtils.isEmpty(name)) return; ContentValues cv = new ContentValues(); cv.put(MediaStore.Audio.Playlists.NAME, name); ContentResolver resolver = context.getContentResolver(); Uri uri = resolver.insert(MediaStore.Audio.Playlists.getContentUri("external"), cv); int size = trackIds.length; ContentValues[] values = new ContentValues[size]; for (int i = 0; i < size; i++) { values[i] = new ContentValues(); values[i].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, i); values[i].put(MediaStore.Audio.Playlists.Members.AUDIO_ID, trackIds[i]); } resolver.bulkInsert(uri, values); } public static Playlist[] getPlaylists(Context context) { String[] projection = { MediaStore.Audio.Playlists._ID, MediaStore.Audio.Playlists.NAME }; Cursor playlistSDCardCursor = query(context, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, projection, projection[1]); int size = playlistSDCardCursor.getCount(); Playlist[] playlists = new Playlist[size]; playlistSDCardCursor.moveToFirst(); for (int i = 0; i < size; i++) { int id = playlistSDCardCursor.getInt(0); String name = playlistSDCardCursor.getString(1); playlists[i] = new Playlist(id, name); playlistSDCardCursor.moveToNext(); } return playlists; } /** * Get tracks of playlist. If playlistId equals -1 - return all tracks in system. * @param context * @param playlistId id of playlist, or -1. * @return tracks array. */ public static Track[] getTracks(Context context, long playlistId) { String[] projection = { (playlistId == -1) ? MediaStore.Audio.Media._ID : MediaStore.Audio.Playlists.Members.AUDIO_ID, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE }; Cursor tracksCursor; if (playlistId == -1) { // Get all tracks. String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0 "; tracksCursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null, 0); } else { // Get tracks of playlist. tracksCursor = query(context, MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId), projection, null); } int size = tracksCursor.getCount(); Track[] tracks = new Track[size]; tracksCursor.moveToFirst(); for (int i = 0; i < size; i++) { int audioId = tracksCursor.getInt(0); String artist = tracksCursor.getString(1); String title = tracksCursor.getString(2); tracks[i] = new Track(audioId, artist, title); tracksCursor.moveToNext(); } return tracks; } private static Cursor query(Context context, Uri uri, String[] projection, String sortOrder) { return query(context, uri, projection, null, null, sortOrder, 0); } private static Cursor query(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, int limit) { try { ContentResolver resolver = context.getContentResolver(); if (resolver == null) { return null; } if (limit > 0) { uri = uri.buildUpon().appendQueryParameter("limit", String.valueOf(limit)).build(); } return resolver.query(uri, projection, selection, selectionArgs, sortOrder); } catch (UnsupportedOperationException ex) { ExceptionHandler.log(ex); return null; } } }