Remove playlist instances

This commit is contained in:
Christopher Eby 2011-09-21 13:34:21 -05:00
parent f524be7199
commit eb7930543c
2 changed files with 14 additions and 47 deletions

View File

@ -31,59 +31,22 @@ import android.net.Uri;
import android.provider.MediaStore; import android.provider.MediaStore;
/** /**
* Instances of this class simply provide a basic representation of a playlist * Provides various playlist-related utility functions.
* (currently only the id and name). The class also provides various playlist-
* related static utility functions.
*/ */
public class Playlist { public class Playlist {
/**
* Create a Playlist with the given id and name.
*/
public Playlist(long id, String name)
{
this.id = id;
this.name = name;
}
/**
* The MediaStore.Audio.Playlists id of the playlist.
*/
public long id;
/**
* The name of the playlist.
*/
public String name;
/** /**
* Queries all the playlists known to the MediaStore. * Queries all the playlists known to the MediaStore.
* *
* @param context A context to use. * @param context A context to use.
* @return An array of Playlists * @return The queried cursor.
*/ */
public static Playlist[] getPlaylists(Context context) public static Cursor queryPlaylists(Context context)
{ {
ContentResolver resolver = context.getContentResolver(); ContentResolver resolver = context.getContentResolver();
Uri media = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI; Uri media = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Audio.Playlists._ID, MediaStore.Audio.Playlists.NAME }; String[] projection = { MediaStore.Audio.Playlists._ID, MediaStore.Audio.Playlists.NAME };
String sort = MediaStore.Audio.Playlists.NAME; String sort = MediaStore.Audio.Playlists.NAME;
Cursor cursor = resolver.query(media, projection, null, null, sort); return resolver.query(media, projection, null, null, sort);
if (cursor == null)
return null;
int count = cursor.getCount();
if (count == 0)
return null;
Playlist[] playlists = new Playlist[count];
for (int i = 0; i != count; ++i) {
if (!cursor.moveToNext())
return null;
playlists[i] = new Playlist(cursor.getLong(0), cursor.getString(1));
}
cursor.close();
return playlists;
} }
/** /**

View File

@ -604,14 +604,18 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
case MENU_ADD_TO_PLAYLIST: { case MENU_ADD_TO_PLAYLIST: {
SubMenu playlistMenu = item.getSubMenu(); SubMenu playlistMenu = item.getSubMenu();
playlistMenu.add(0, MENU_NEW_PLAYLIST, 0, R.string.new_playlist).setIntent(intent); playlistMenu.add(0, MENU_NEW_PLAYLIST, 0, R.string.new_playlist).setIntent(intent);
Playlist[] playlists = Playlist.getPlaylists(this); Cursor cursor = Playlist.queryPlaylists(this);
if (playlists != null) { if (cursor != null) {
for (int i = 0; i != playlists.length; ++i) { for (int i = 0, count = cursor.getCount(); i != count; ++i) {
cursor.moveToPosition(i);
long id = cursor.getLong(0);
String name = cursor.getString(1);
Intent copy = new Intent(intent); Intent copy = new Intent(intent);
copy.putExtra("playlist", playlists[i].id); copy.putExtra("playlist", id);
copy.putExtra("playlistName", playlists[i].name); copy.putExtra("playlistName", name);
playlistMenu.add(0, MENU_SELECT_PLAYLIST, 0, playlists[i].name).setIntent(copy); playlistMenu.add(0, MENU_SELECT_PLAYLIST, 0, name).setIntent(copy);
} }
cursor.close();
} }
break; break;
} }