MediaPickerActivitypublic class MediaPickerActivity extends android.app.ListActivity implements MusicUtils.Defs
Fields Summary |
---|
private android.database.Cursor | mCursor | private String | mSortOrder | private String | mFirstYear | private String | mLastYear | private String | mWhereClause |
Constructors Summary |
---|
public MediaPickerActivity()
|
Methods Summary |
---|
private void | MakeCursor()
String[] audiocols = new String[] {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.MIME_TYPE,
MediaStore.Audio.Media.YEAR
};
String[] videocols = new String[] {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.MIME_TYPE
};
Cursor[] cs;
// Use ArrayList for the moment, since we don't know the size of
// Cursor[]. If the length of Corsor[] larger than really used,
// a NPE will come up when access the content of Corsor[].
ArrayList<Cursor> cList = new ArrayList<Cursor>();
Intent intent = getIntent();
String type = intent.getType();
if (mFirstYear != null) {
// If mFirstYear is not null, the picker only for audio because
// video has no year column.
if(type.equals("video/*")) {
mCursor = null;
return;
}
mWhereClause = MediaStore.Audio.Media.YEAR + ">=" + mFirstYear + " AND " +
MediaStore.Audio.Media.YEAR + "<=" + mLastYear;
}
// If use Cursor[] as before, the Cursor[i] could be null when there is
// no video/audio/sdcard. Then a NPE will come up when access the content of the
// Array.
Cursor c;
if (type.equals("video/*")) {
// Only video.
c = MusicUtils.query(this, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
videocols, null , null, mSortOrder);
if (c != null) {
cList.add(c);
}
} else {
c = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
audiocols, mWhereClause , null, mSortOrder);
if (c != null) {
cList.add(c);
}
if (mFirstYear == null && intent.getType().equals("media/*")) {
// video has no year column
c = MusicUtils.query(this, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
videocols, null , null, mSortOrder);
if (c != null) {
cList.add(c);
}
}
}
// Get the ArrayList size.
int size = cList.size();
if (0 == size) {
// If no video/audio/SDCard exist, return.
mCursor = null;
return;
}
// The size is known now, we're sure each item of Cursor[] is not null.
cs = new Cursor[size];
cs = cList.toArray(cs);
mCursor = new SortCursor(cs, MediaStore.Audio.Media.TITLE);
| public void | init()
setContentView(R.layout.media_picker_activity);
MakeCursor();
if (null == mCursor || 0 == mCursor.getCount()) {
return;
}
PickListAdapter adapter = new PickListAdapter(
this,
R.layout.track_list_item,
mCursor,
new String[] {},
new int[] {});
setListAdapter(adapter);
| public void | onCreate(android.os.Bundle icicle)Called when the activity is first created.
super.onCreate(icicle);
mFirstYear = getIntent().getStringExtra("firstyear");
mLastYear = getIntent().getStringExtra("lastyear");
if (mFirstYear == null) {
setTitle(R.string.all_title);
} else if (mFirstYear.equals(mLastYear)) {
setTitle(mFirstYear);
} else {
setTitle(mFirstYear + "-" + mLastYear);
}
MusicUtils.bindToService(this);
init();
| public void | onDestroy()
MusicUtils.unbindFromService(this);
super.onDestroy();
if (mCursor != null) {
mCursor.close();
}
| protected void | onListItemClick(android.widget.ListView l, android.view.View v, int position, long id)
mCursor.moveToPosition(position);
String type = mCursor.getString(mCursor.getColumnIndexOrThrow(
MediaStore.Audio.Media.MIME_TYPE));
String action = getIntent().getAction();
if (Intent.ACTION_GET_CONTENT.equals(action)) {
Uri uri;
long mediaId;
if (type.startsWith("video")) {
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
mediaId = mCursor.getLong(mCursor.getColumnIndexOrThrow(
MediaStore.Video.Media._ID));
} else {
uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
mediaId = mCursor.getLong(mCursor.getColumnIndexOrThrow(
MediaStore.Audio.Media._ID));
}
setResult(RESULT_OK, new Intent().setData(ContentUris.withAppendedId(uri, mediaId)));
finish();
return;
}
// Need to stop the playbackservice, in case it is busy playing audio
// and the user selected a video.
if (MusicUtils.sService != null) {
try {
MusicUtils.sService.stop();
} catch (RemoteException ex) {
}
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id), type);
startActivity(intent);
|
|