FileDocCategorySizeDatePackage
QueryBrowserActivity.javaAPI DocAndroid 1.5 API16205Wed May 06 22:42:46 BST 2009com.android.music

QueryBrowserActivity

public class QueryBrowserActivity extends android.app.ListActivity implements MusicUtils.Defs

Fields Summary
private static final int
PLAY_NOW
private static final int
ADD_TO_QUEUE
private static final int
PLAY_NEXT
private static final int
PLAY_ARTIST
private static final int
EXPLORE_ARTIST
private static final int
PLAY_ALBUM
private static final int
EXPLORE_ALBUM
private static final int
REQUERY
private QueryListAdapter
mAdapter
private boolean
mAdapterSent
private String
mFilterString
private android.content.BroadcastReceiver
mScanListener
private android.os.Handler
mReScanHandler
private android.widget.ListView
mTrackList
private android.database.Cursor
mQueryCursor
Constructors Summary
public QueryBrowserActivity()


     
    
    
Methods Summary
private android.database.CursorgetQueryCursor(android.content.AsyncQueryHandler async, java.lang.String filter)

        if (filter == null) {
            filter = "";
        }
        String[] ccols = new String[] {
                "_id",   // this will be the artist, album or track ID
                MediaStore.Audio.Media.MIME_TYPE, // mimetype of audio file, or "artist" or "album"
                SearchManager.SUGGEST_COLUMN_TEXT_1,
                "data1",
                "data2"
        };

        Uri search = Uri.parse("content://media/external/audio/" + 
                SearchManager.SUGGEST_URI_PATH_QUERY + "/" + Uri.encode(filter));
        
        Cursor ret = null;
        if (async != null) {
            async.startQuery(0, null, search, ccols, null, null, null);
        } else {
            ret = MusicUtils.query(this, search, ccols, null, null, null);
        }
        return ret;
    
public voidinit(android.database.Cursor c)

        
        mAdapter.changeCursor(c);

        if (mQueryCursor == null) {
            MusicUtils.displayDatabaseError(this);
            setListAdapter(null);
            mReScanHandler.sendEmptyMessageDelayed(0, 1000);
            return;
        }
        MusicUtils.hideDatabaseError(this);
    
protected voidonActivityResult(int requestCode, int resultCode, android.content.Intent intent)


    
            
        switch (requestCode) {
            case SCAN_DONE:
                if (resultCode == RESULT_CANCELED) {
                    finish();
                } else {
                    getQueryCursor(mAdapter.getQueryHandler(), null);
                }
                break;
        }
    
public voidonCreate(android.os.Bundle icicle)
Called when the activity is first created.

        super.onCreate(icicle);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        MusicUtils.bindToService(this);
        IntentFilter f = new IntentFilter();
        f.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
        f.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
        f.addDataScheme("file");
        registerReceiver(mScanListener, f);
        
        if (icicle == null) {
            Intent intent = getIntent();
            
            if (intent.getAction().equals(Intent.ACTION_VIEW)) {
                // this is something we got from the search bar
                Uri uri = intent.getData();
                String path = uri.toString();
                if (path.startsWith("content://media/external/audio/media/")) {
                    // This is a specific file
                    String id = uri.getLastPathSegment();
                    int [] list = new int[] { Integer.valueOf(id) };
                    MusicUtils.playAll(this, list, 0);
                    finish();
                    return;
                } else if (path.startsWith("content://media/external/audio/albums/")) {
                    // This is an album, show the songs on it
                    Intent i = new Intent(Intent.ACTION_PICK);
                    i.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track");
                    i.putExtra("album", uri.getLastPathSegment());
                    startActivity(i);
                    finish();
                    return;
                } else if (path.startsWith("content://media/external/audio/artists/")) {
                    // This is an artist, show the albums for that artist
                    Intent i = new Intent(Intent.ACTION_PICK);
                    i.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/album");
                    i.putExtra("artist", uri.getLastPathSegment());
                    startActivity(i);
                    finish();
                    return;
                }
            }
            mFilterString = intent.getStringExtra(SearchManager.QUERY);
        }

        setContentView(R.layout.query_activity);
        mTrackList = getListView();
        mTrackList.setTextFilterEnabled(true);
        mAdapter = (QueryListAdapter) getLastNonConfigurationInstance();
        if (mAdapter == null) {
            mAdapter = new QueryListAdapter(
                    getApplication(),
                    this,
                    R.layout.track_list_item,
                    null, // cursor
                    new String[] {},
                    new int[] {});
            setListAdapter(mAdapter);
            if (TextUtils.isEmpty(mFilterString)) {
                getQueryCursor(mAdapter.getQueryHandler(), null);
            } else {
                mTrackList.setFilterText(mFilterString);
                mFilterString = null;
            }
        } else {
            mAdapter.setActivity(this);
            setListAdapter(mAdapter);
            mQueryCursor = mAdapter.getCursor();
            if (mQueryCursor != null) {
                init(mQueryCursor);
            } else {
                getQueryCursor(mAdapter.getQueryHandler(), mFilterString);
            }
        }
    
public voidonDestroy()

        MusicUtils.unbindFromService(this);
        unregisterReceiver(mScanListener);
        super.onDestroy();
        if (!mAdapterSent && mAdapter != null) {
            Cursor c = mAdapter.getCursor();
            if (c != null) {
                c.close();
            }
        }
    
protected voidonListItemClick(android.widget.ListView l, android.view.View v, int position, long id)

        // Dialog doesn't allow us to wait for a result, so we need to store
        // the info we need for when the dialog posts its result
        mQueryCursor.moveToPosition(position);
        if (mQueryCursor.isBeforeFirst() || mQueryCursor.isAfterLast()) {
            return;
        }
        String selectedType = mQueryCursor.getString(mQueryCursor.getColumnIndexOrThrow(
                MediaStore.Audio.Media.MIME_TYPE));
        
        if ("artist".equals(selectedType)) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/album");
            intent.putExtra("artist", Long.valueOf(id).toString());
            startActivity(intent);
        } else if ("album".equals(selectedType)) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track");
            intent.putExtra("album", Long.valueOf(id).toString());
            startActivity(intent);
        } else if (position >= 0 && id >= 0){
            int [] list = new int[] { (int) id };
            MusicUtils.playAll(this, list, 0);
        } else {
            Log.e("QueryBrowser", "invalid position/id: " + position + "/" + id);
        }
    
public booleanonOptionsItemSelected(android.view.MenuItem item)

        switch (item.getItemId()) {
            case USE_AS_RINGTONE: {
                // Set the system setting to make this the current ringtone
                MusicUtils.setRingtone(this, mTrackList.getSelectedItemId());
                return true;
            }

        }
        return super.onOptionsItemSelected(item);
    
public voidonPause()

        mReScanHandler.removeCallbacksAndMessages(null);
        super.onPause();
    
public java.lang.ObjectonRetainNonConfigurationInstance()

        mAdapterSent = true;
        return mAdapter;