PlaylistBrowserActivitypublic class PlaylistBrowserActivity extends android.app.ListActivity implements View.OnCreateContextMenuListener, MusicUtils.Defs
Fields Summary |
---|
private static final String | TAG | private static final int | DELETE_PLAYLIST | private static final int | EDIT_PLAYLIST | private static final int | RENAME_PLAYLIST | private static final int | CHANGE_WEEKS | private static final long | RECENTLY_ADDED_PLAYLIST | private static final long | ALL_SONGS_PLAYLIST | private static final long | PODCASTS_PLAYLIST | private PlaylistListAdapter | mAdapter | boolean | mAdapterSent | private boolean | mCreateShortcut | private android.content.BroadcastReceiver | mScanListener | private android.os.Handler | mReScanHandler | String[] | mCols | private android.database.Cursor | mPlaylistCursor |
Constructors Summary |
---|
public PlaylistBrowserActivity()
|
Methods Summary |
---|
private android.database.Cursor | getPlaylistCursor(android.content.AsyncQueryHandler async, java.lang.String filterstring)
StringBuilder where = new StringBuilder();
where.append(MediaStore.Audio.Playlists.NAME + " != ''");
// Add in the filtering constraints
String [] keywords = null;
if (filterstring != null) {
String [] searchWords = filterstring.split(" ");
keywords = new String[searchWords.length];
Collator col = Collator.getInstance();
col.setStrength(Collator.PRIMARY);
for (int i = 0; i < searchWords.length; i++) {
keywords[i] = '%" + searchWords[i] + '%";
}
for (int i = 0; i < searchWords.length; i++) {
where.append(" AND ");
where.append(MediaStore.Audio.Playlists.NAME + " LIKE ?");
}
}
String whereclause = where.toString();
if (async != null) {
async.startQuery(0, null, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
mCols, whereclause, keywords, MediaStore.Audio.Playlists.NAME);
return null;
}
Cursor c = null;
c = MusicUtils.query(this, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
mCols, whereclause, keywords, MediaStore.Audio.Playlists.NAME);
return mergedCursor(c);
| public void | init(android.database.Cursor cursor)
mAdapter.changeCursor(cursor);
if (mPlaylistCursor == null) {
MusicUtils.displayDatabaseError(this);
closeContextMenu();
mReScanHandler.sendEmptyMessageDelayed(0, 1000);
return;
}
MusicUtils.hideDatabaseError(this);
setTitle();
| private android.database.Cursor | mergedCursor(android.database.Cursor c)
if (c == null) {
return null;
}
if (c instanceof MergeCursor) {
// this shouldn't happen, but fail gracefully
Log.d("PlaylistBrowserActivity", "Already wrapped");
return c;
}
ArrayList<ArrayList> autoplaylists = new ArrayList<ArrayList>();
if (mCreateShortcut) {
ArrayList<Object> all = new ArrayList<Object>(2);
all.add(ALL_SONGS_PLAYLIST);
all.add(getString(R.string.play_all));
autoplaylists.add(all);
}
ArrayList<Object> recent = new ArrayList<Object>(2);
recent.add(RECENTLY_ADDED_PLAYLIST);
recent.add(getString(R.string.recentlyadded));
autoplaylists.add(recent);
// check if there are any podcasts
Cursor counter = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] {"count(*)"}, "is_podcast=1", null, null);
if (counter != null) {
counter.moveToFirst();
int numpodcasts = counter.getInt(0);
counter.close();
if (numpodcasts > 0) {
ArrayList<Object> podcasts = new ArrayList<Object>(2);
podcasts.add(PODCASTS_PLAYLIST);
podcasts.add(getString(R.string.podcasts_listitem));
autoplaylists.add(podcasts);
}
}
ArrayListCursor autoplaylistscursor = new ArrayListCursor(mCols, autoplaylists);
Cursor cc = new MergeCursor(new Cursor [] {autoplaylistscursor, c});
return cc;
| protected void | onActivityResult(int requestCode, int resultCode, android.content.Intent intent)
switch (requestCode) {
case SCAN_DONE:
if (resultCode == RESULT_CANCELED) {
finish();
} else {
getPlaylistCursor(mAdapter.getQueryHandler(), null);
}
break;
}
| public boolean | onContextItemSelected(android.view.MenuItem item)
AdapterContextMenuInfo mi = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case PLAY_SELECTION:
if (mi.id == RECENTLY_ADDED_PLAYLIST) {
playRecentlyAdded();
} else if (mi.id == PODCASTS_PLAYLIST) {
playPodcasts();
} else {
MusicUtils.playPlaylist(this, mi.id);
}
break;
case DELETE_PLAYLIST:
Uri uri = ContentUris.withAppendedId(
MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, mi.id);
getContentResolver().delete(uri, null, null);
Toast.makeText(this, R.string.playlist_deleted_message, Toast.LENGTH_SHORT).show();
if (mPlaylistCursor.getCount() == 0) {
setTitle(R.string.no_playlists_title);
}
break;
case EDIT_PLAYLIST:
if (mi.id == RECENTLY_ADDED_PLAYLIST) {
Intent intent = new Intent();
intent.setClass(this, WeekSelector.class);
startActivityForResult(intent, CHANGE_WEEKS);
return true;
} else {
Log.e(TAG, "should not be here");
}
break;
case RENAME_PLAYLIST:
Intent intent = new Intent();
intent.setClass(this, RenamePlaylist.class);
intent.putExtra("rename", mi.id);
startActivityForResult(intent, RENAME_PLAYLIST);
break;
}
return true;
| public void | onCreate(android.os.Bundle icicle)Called when the activity is first created.
super.onCreate(icicle);
final Intent intent = getIntent();
final String action = intent.getAction();
if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
mCreateShortcut = true;
}
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
MusicUtils.bindToService(this, new ServiceConnection() {
public void onServiceConnected(ComponentName classname, IBinder obj) {
if (Intent.ACTION_VIEW.equals(action)) {
long id = Long.parseLong(intent.getExtras().getString("playlist"));
if (id == RECENTLY_ADDED_PLAYLIST) {
playRecentlyAdded();
} else if (id == PODCASTS_PLAYLIST) {
playPodcasts();
} else if (id == ALL_SONGS_PLAYLIST) {
int [] list = MusicUtils.getAllSongs(PlaylistBrowserActivity.this);
if (list != null) {
MusicUtils.playAll(PlaylistBrowserActivity.this, list, 0);
}
} else {
MusicUtils.playPlaylist(PlaylistBrowserActivity.this, id);
}
finish();
}
}
public void onServiceDisconnected(ComponentName classname) {
}
});
IntentFilter f = new IntentFilter();
f.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
f.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
f.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
f.addDataScheme("file");
registerReceiver(mScanListener, f);
setContentView(R.layout.media_picker_activity);
ListView lv = getListView();
lv.setOnCreateContextMenuListener(this);
lv.setTextFilterEnabled(true);
mAdapter = (PlaylistListAdapter) getLastNonConfigurationInstance();
if (mAdapter == null) {
//Log.i("@@@", "starting query");
mAdapter = new PlaylistListAdapter(
getApplication(),
this,
R.layout.track_list_item,
mPlaylistCursor,
new String[] { MediaStore.Audio.Playlists.NAME},
new int[] { android.R.id.text1 });
setListAdapter(mAdapter);
setTitle(R.string.working_playlists);
getPlaylistCursor(mAdapter.getQueryHandler(), null);
} else {
mAdapter.setActivity(this);
setListAdapter(mAdapter);
mPlaylistCursor = mAdapter.getCursor();
// If mPlaylistCursor is null, this can be because it doesn't have
// a cursor yet (because the initial query that sets its cursor
// is still in progress), or because the query failed.
// In order to not flash the error dialog at the user for the
// first case, simply retry the query when the cursor is null.
// Worst case, we end up doing the same query twice.
if (mPlaylistCursor != null) {
init(mPlaylistCursor);
} else {
setTitle(R.string.working_playlists);
getPlaylistCursor(mAdapter.getQueryHandler(), null);
}
}
| public void | onCreateContextMenu(android.view.ContextMenu menu, android.view.View view, android.view.ContextMenu.ContextMenuInfo menuInfoIn)
if (mCreateShortcut) {
return;
}
AdapterContextMenuInfo mi = (AdapterContextMenuInfo) menuInfoIn;
menu.add(0, PLAY_SELECTION, 0, R.string.play_selection);
if (mi.id >= 0 /*|| mi.id == PODCASTS_PLAYLIST*/) {
menu.add(0, DELETE_PLAYLIST, 0, R.string.delete_playlist_menu);
}
if (mi.id == RECENTLY_ADDED_PLAYLIST) {
menu.add(0, EDIT_PLAYLIST, 0, R.string.edit_playlist_menu);
}
if (mi.id >= 0) {
menu.add(0, RENAME_PLAYLIST, 0, R.string.rename_playlist_menu);
}
mPlaylistCursor.moveToPosition(mi.position);
menu.setHeaderTitle(mPlaylistCursor.getString(mPlaylistCursor.getColumnIndexOrThrow(
MediaStore.Audio.Playlists.NAME)));
| public boolean | onCreateOptionsMenu(android.view.Menu menu)
if (!mCreateShortcut) {
menu.add(0, GOTO_START, 0, R.string.goto_start).setIcon(
R.drawable.ic_menu_music_library);
menu.add(0, GOTO_PLAYBACK, 0, R.string.goto_playback).setIcon(
R.drawable.ic_menu_playback).setVisible(MusicUtils.isMusicLoaded());
}
return super.onCreateOptionsMenu(menu);
| public void | onDestroy()
MusicUtils.unbindFromService(this);
if (!mAdapterSent) {
Cursor c = mAdapter.getCursor();
if (c != null) {
c.close();
}
}
unregisterReceiver(mScanListener);
super.onDestroy();
| protected void | onListItemClick(android.widget.ListView l, android.view.View v, int position, long id)
if (mCreateShortcut) {
final Intent shortcut = new Intent();
shortcut.setAction(Intent.ACTION_VIEW);
shortcut.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/playlist");
shortcut.putExtra("playlist", String.valueOf(id));
final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, ((TextView) v.findViewById(R.id.line1)).getText());
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(
this, R.drawable.ic_launcher_shortcut_music_playlist));
setResult(RESULT_OK, intent);
finish();
return;
}
if (id == RECENTLY_ADDED_PLAYLIST) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track");
intent.putExtra("playlist", "recentlyadded");
startActivity(intent);
} else if (id == PODCASTS_PLAYLIST) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track");
intent.putExtra("playlist", "podcasts");
startActivity(intent);
} else {
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track");
intent.putExtra("playlist", Long.valueOf(id).toString());
startActivity(intent);
}
| public boolean | onOptionsItemSelected(android.view.MenuItem item)
Intent intent;
switch (item.getItemId()) {
case GOTO_START:
intent = new Intent();
intent.setClass(this, MusicBrowserActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case GOTO_PLAYBACK:
intent = new Intent("com.android.music.PLAYBACK_VIEWER");
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
| public void | onPause()
mReScanHandler.removeCallbacksAndMessages(null);
super.onPause();
| public void | onResume()
super.onResume();
MusicUtils.setSpinnerState(this);
| public java.lang.Object | onRetainNonConfigurationInstance()
PlaylistListAdapter a = mAdapter;
mAdapterSent = true;
return a;
| private void | playPodcasts()
// do a query for all files that are podcasts
final String[] ccols = new String[] { MediaStore.Audio.Media._ID};
Cursor cursor = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
ccols, MediaStore.Audio.Media.IS_PODCAST + "=1",
null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
if (cursor == null) {
// Todo: show a message
return;
}
try {
int len = cursor.getCount();
int [] list = new int[len];
for (int i = 0; i < len; i++) {
cursor.moveToNext();
list[i] = cursor.getInt(0);
}
MusicUtils.playAll(this, list, 0);
} catch (SQLiteException ex) {
} finally {
cursor.close();
}
| private void | playRecentlyAdded()
// do a query for all songs added in the last X weeks
int X = MusicUtils.getIntPref(this, "numweeks", 2) * (3600 * 24 * 7);
final String[] ccols = new String[] { MediaStore.Audio.Media._ID};
String where = MediaStore.MediaColumns.DATE_ADDED + ">" + (System.currentTimeMillis() / 1000 - X);
Cursor cursor = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
ccols, where, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
if (cursor == null) {
// Todo: show a message
return;
}
try {
int len = cursor.getCount();
int [] list = new int[len];
for (int i = 0; i < len; i++) {
cursor.moveToNext();
list[i] = cursor.getInt(0);
}
MusicUtils.playAll(this, list, 0);
} catch (SQLiteException ex) {
} finally {
cursor.close();
}
| private void | setTitle()
setTitle(R.string.playlists_title);
|
|