Pass ContentResolver instead of Context in some cases

This commit is contained in:
Christopher Eby 2011-09-25 03:45:24 -05:00
parent 870bc77e8b
commit 6cdaf3787d
6 changed files with 41 additions and 47 deletions

View File

@ -541,7 +541,7 @@ public class LibraryActivity extends PlaybackActivity implements AdapterView.OnI
private void addToPlaylist(long playlistId, Intent intent)
{
QueryTask query = buildQueryFromIntent(intent, true);
int count = Playlist.addToPlaylist(this, playlistId, query);
int count = Playlist.addToPlaylist(getContentResolver(), playlistId, query);
String message = getResources().getQuantityString(R.plurals.added_to_playlist, count, count, intent.getStringExtra("playlistName"));
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
@ -559,8 +559,9 @@ public class LibraryActivity extends PlaybackActivity implements AdapterView.OnI
int type = intent.getIntExtra("type", 1);
long id = intent.getLongExtra("id", -1);
ContentResolver resolver = getContentResolver();
if (type == MediaUtils.TYPE_PLAYLIST) {
Playlist.deletePlaylist(this, id);
Playlist.deletePlaylist(resolver, id);
String message = getResources().getString(R.string.playlist_deleted, intent.getStringExtra("title"));
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
} else {
@ -603,7 +604,7 @@ public class LibraryActivity extends PlaybackActivity implements AdapterView.OnI
case MENU_ADD_TO_PLAYLIST: {
SubMenu playlistMenu = item.getSubMenu();
playlistMenu.add(0, MENU_NEW_PLAYLIST, 0, R.string.new_playlist).setIntent(intent);
Cursor cursor = Playlist.queryPlaylists(this);
Cursor cursor = Playlist.queryPlaylists(getContentResolver());
if (cursor != null) {
for (int i = 0, count = cursor.getCount(); i != count; ++i) {
cursor.moveToPosition(i);
@ -717,7 +718,7 @@ public class LibraryActivity extends PlaybackActivity implements AdapterView.OnI
NewPlaylistDialog dialog = (NewPlaylistDialog)message.obj;
if (dialog.isAccepted()) {
String name = dialog.getText();
long playlistId = Playlist.createPlaylist(this, name);
long playlistId = Playlist.createPlaylist(getContentResolver(), name);
Intent intent = dialog.getIntent();
intent.putExtra("playlistName", name);
addToPlaylist(playlistId, intent);
@ -731,7 +732,7 @@ public class LibraryActivity extends PlaybackActivity implements AdapterView.OnI
NewPlaylistDialog dialog = (NewPlaylistDialog)message.obj;
if (dialog.isAccepted()) {
long playlistId = dialog.getIntent().getLongExtra("id", -1);
Playlist.renamePlaylist(this, playlistId, dialog.getText());
Playlist.renamePlaylist(getContentResolver(), playlistId, dialog.getText());
}
break;
}

View File

@ -229,15 +229,16 @@ public class MediaUtils {
/**
* Query the MediaStore to determine the id of the genre the song belongs
* to.
*
* @param resolver A ContentResolver to use.
* @param id The id of the song to query the genre for.
*/
public static long queryGenreForSong(Context context, long id)
public static long queryGenreForSong(ContentResolver resolver, long id)
{
// This is terribly inefficient, but it seems to be the only way to do
// this. Honeycomb introduced an API to query the genre of the song.
// We should look into it when ICS is released.
ContentResolver resolver = context.getContentResolver();
// query ids of all the genres
Uri uri = MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
String[] projection = { "_id" };
@ -380,14 +381,13 @@ public class MediaUtils {
/**
* Determine if any songs are available from the library.
*
* @param context A context to use.
* @param resolver A ContentResolver to use.
* @return True if it's possible to retrieve any songs, false otherwise. For
* example, false could be returned if there are no songs in the library.
*/
public static boolean isSongAvailable(Context context)
public static boolean isSongAvailable(ContentResolver resolver)
{
if (sSongCount == -1) {
ContentResolver resolver = context.getContentResolver();
Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
Cursor cursor = resolver.query(media, new String[]{"count(_id)"}, selection, null, null);
@ -407,14 +407,13 @@ public class MediaUtils {
* Returns a shuffled array contaning the ids of all the songs on the
* device's library.
*
* @param context A context to use.
* @param resolver A ContentResolver to use.
*/
public static long[] loadAllSongs(Context context)
public static long[] queryAllSongs(ContentResolver resolver)
{
sAllSongsIdx = 0;
sRandomCacheEnd = -1;
ContentResolver resolver = context.getContentResolver();
Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
Cursor cursor = resolver.query(media, Song.EMPTY_PROJECTION, selection, null, null);
@ -433,7 +432,7 @@ public class MediaUtils {
sSongCount = count;
cursor.close();
MediaUtils.shuffle(ids);
shuffle(ids);
return ids;
}
@ -448,14 +447,14 @@ public class MediaUtils {
* Returns a song randomly selected from all the songs in the Android
* MediaStore.
*
* @param context A context to use.
* @param resolver A ContentResolver to use.
*/
public static Song randomSong(Context context)
public static Song randomSong(ContentResolver resolver)
{
long[] songs = sAllSongs;
if (songs == null) {
songs = loadAllSongs(context);
songs = queryAllSongs(resolver);
if (songs == null)
return null;
sAllSongs = songs;
@ -466,7 +465,6 @@ public class MediaUtils {
}
if (sAllSongsIdx >= sRandomCacheEnd) {
ContentResolver resolver = context.getContentResolver();
Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
StringBuilder selection = new StringBuilder("_ID IN (");

View File

@ -23,6 +23,7 @@
package org.kreed.vanilla;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
@ -140,7 +141,8 @@ public class NewPlaylistDialog extends Dialog implements TextWatcher, View.OnCli
mPositiveButton.setEnabled(true);
// Update the action button based on whether there is an
// existing playlist with the given name.
int res = Playlist.getPlaylist(getContext(), string) == -1 ? mActionRes : R.string.overwrite;
ContentResolver resolver = getContext().getContentResolver();
int res = Playlist.getPlaylist(resolver, string) == -1 ? mActionRes : R.string.overwrite;
mPositiveButton.setText(res);
}
}

View File

@ -623,7 +623,7 @@ public final class PlaybackService extends Service implements Handler.Callback,
Song song = mTimeline.shiftCurrentSong(delta);
mCurrentSong = song;
if (song == null || song.id == -1 || song.path == null) {
if (MediaUtils.isSongAvailable(this)) {
if (MediaUtils.isSongAvailable(getContentResolver())) {
int flag = (mState & FLAG_RANDOM) == 0 ? FLAG_EMPTY_QUEUE : FLAG_ERROR;
synchronized (mStateLock) {
updateState((mState | flag) & ~FLAG_NO_MEDIA);
@ -780,7 +780,7 @@ public final class PlaybackService extends Service implements Handler.Callback,
public void onMediaChange()
{
if (MediaUtils.isSongAvailable(this)) {
if (MediaUtils.isSongAvailable(getContentResolver())) {
if ((mState & FLAG_NO_MEDIA) != 0)
setCurrentSong(0);
} else {
@ -1067,7 +1067,7 @@ public final class PlaybackService extends Service implements Handler.Callback,
id = current.albumId;
break;
case MediaUtils.TYPE_GENRE:
id = MediaUtils.queryGenreForSong(this, current.id);
id = MediaUtils.queryGenreForSong(getContentResolver(), current.id);
break;
default:
throw new IllegalArgumentException("Unsupported media type: " + type);

View File

@ -25,7 +25,6 @@ package org.kreed.vanilla;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
@ -37,12 +36,11 @@ public class Playlist {
/**
* Queries all the playlists known to the MediaStore.
*
* @param context A context to use.
* @param resolver A ContentResolver to use.
* @return The queried cursor.
*/
public static Cursor queryPlaylists(Context context)
public static Cursor queryPlaylists(ContentResolver resolver)
{
ContentResolver resolver = context.getContentResolver();
Uri media = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Audio.Playlists._ID, MediaStore.Audio.Playlists.NAME };
String sort = MediaStore.Audio.Playlists.NAME;
@ -52,16 +50,15 @@ public class Playlist {
/**
* Retrieves the id for a playlist with the given name.
*
* @param context A context to use.
* @param resolver A ContentResolver to use.
* @param name The name of the playlist.
* @return The id of the playlist, or -1 if there is no playlist with the
* given name.
*/
public static long getPlaylist(Context context, String name)
public static long getPlaylist(ContentResolver resolver, String name)
{
long id = -1;
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Playlists._ID },
MediaStore.Audio.Playlists.NAME + "=?",
@ -80,14 +77,13 @@ public class Playlist {
* Create a new playlist with the given name. If a playlist with the given
* name already exists, it will be overwritten.
*
* @param context A context to use.
* @param resolver A ContentResolver to use.
* @param name The name of the playlist.
* @return The id of the new playlist.
*/
public static long createPlaylist(Context context, String name)
public static long createPlaylist(ContentResolver resolver, String name)
{
ContentResolver resolver = context.getContentResolver();
long id = getPlaylist(context, name);
long id = getPlaylist(resolver, name);
if (id == -1) {
// We need to create a new playlist.
@ -108,19 +104,17 @@ public class Playlist {
* Run the given query and add the results to the given playlist. Should be
* run on a background thread.
*
* @param context A context to use.
* @param resolver A ContentResolver to use.
* @param playlistId The MediaStore.Audio.Playlist id of the playlist to
* modify.
* @param query The query to run. The audio id should be the first column.
* @return The number of songs that were added to the playlist.
*/
public static int addToPlaylist(Context context, long playlistId, QueryTask query)
public static int addToPlaylist(ContentResolver resolver, long playlistId, QueryTask query)
{
if (playlistId == -1)
return 0;
ContentResolver resolver = context.getContentResolver();
// Find the greatest PLAY_ORDER in the playlist
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
String[] projection = new String[] { MediaStore.Audio.Playlists.Members.PLAY_ORDER };
@ -154,33 +148,32 @@ public class Playlist {
/**
* Delete the playlist with the given id.
*
* @param context A context to use.
* @param resolver A ContentResolver to use.
* @param id The Media.Audio.Playlists id of the playlist.
*/
public static void deletePlaylist(Context context, long id)
public static void deletePlaylist(ContentResolver resolver, long id)
{
Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, id);
context.getContentResolver().delete(uri, null, null);
resolver.delete(uri, null, null);
}
/**
* Rename the playlist with the given id.
*
* @param context A context to use.
* @param resolver A ContentResolver to use.
* @param id The Media.Audio.Playlists id of the playlist.
* @param newName The new name for the playlist.
*/
public static void renamePlaylist(Context context, long id, String newName)
public static void renamePlaylist(ContentResolver resolver, long id, String newName)
{
long existingId = getPlaylist(context, newName);
long existingId = getPlaylist(resolver, newName);
// We are already called the requested name; nothing to do.
if (existingId == id)
return;
// There is already a playlist with this name. Kill it.
if (existingId != -1)
deletePlaylist(context, existingId);
deletePlaylist(resolver, existingId);
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues(1);
values.put(MediaStore.Audio.Playlists.NAME, newName);
resolver.update(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values, MediaStore.Audio.Playlists._ID + "=" + id, null);

View File

@ -405,7 +405,7 @@ public final class SongTimeline {
song = timeline.get(0);
break;
case FINISH_RANDOM:
song = MediaUtils.randomSong(mContext);
song = MediaUtils.randomSong(mContext.getContentResolver());
if (song == null)
return null;
timeline.add(song);