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

MusicBrowserActivity

public class MusicBrowserActivity extends android.app.Activity implements View.OnClickListener, MusicUtils.Defs

Fields Summary
private android.view.View
mNowPlayingView
private android.widget.TextView
mTitle
private android.widget.TextView
mArtist
private boolean
mAutoShuffle
private static final int
SEARCH_MUSIC
private android.content.ServiceConnection
autoshuffle
View.OnFocusChangeListener
mFocuser
private android.content.BroadcastReceiver
mStatusListener
Constructors Summary
public MusicBrowserActivity()


      
    
Methods Summary
private voiddoAutoShuffle()

        bindService((new Intent()).setClass(this, MediaPlaybackService.class), autoshuffle, 0);
    
public voidinit()

        setContentView(R.layout.music_library);
        mNowPlayingView = findViewById(R.id.nowplaying);
        mTitle = (TextView) mNowPlayingView.findViewById(R.id.title);
        mArtist = (TextView) mNowPlayingView.findViewById(R.id.artist);
        
        View b = (View) findViewById(R.id.browse_button); 
        b.setOnClickListener(this);
        
        b = (View) findViewById(R.id.albums_button);
        b.setOnClickListener(this);

        b = (View) findViewById(R.id.tracks_button);
        b.setOnClickListener(this);

        b = (View) findViewById(R.id.playlists_button);
        b.setOnClickListener(this);
    
private voidmakeNowPlayingView()


       
        try {
            mTitle.setText(MusicUtils.sService.getTrackName());
            String artistName = MusicUtils.sService.getArtistName();
            if (MediaFile.UNKNOWN_STRING.equals(artistName)) {
                artistName = getString(R.string.unknown_artist_name);
            }
            mArtist.setText(artistName);
            mNowPlayingView.setOnFocusChangeListener(mFocuser);
            mNowPlayingView.setOnClickListener(this);
        } catch (RemoteException ex) {

        }
    
public voidonClick(android.view.View v)

        Intent intent;
        switch (v.getId()) {
            case R.id.browse_button:
                intent = new Intent(Intent.ACTION_PICK);
                intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/artistalbum");
                startActivity(intent);
                break;
            case R.id.albums_button:
                intent = new Intent(Intent.ACTION_PICK);
                intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/album");
                startActivity(intent);
                break;
            case R.id.tracks_button:
                intent = new Intent(Intent.ACTION_PICK);
                intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track");
                startActivity(intent);
                break;
            case R.id.playlists_button:
                intent = new Intent(Intent.ACTION_PICK);
                intent.setDataAndType(Uri.EMPTY, MediaStore.Audio.Playlists.CONTENT_TYPE);
                startActivity(intent);
                break;
            case R.id.nowplaying:
                intent = new Intent("com.android.music.PLAYBACK_VIEWER");
                startActivity(intent);
                break;
        }
    
public voidonCreate(android.os.Bundle icicle)
Called when the activity is first created.

        super.onCreate(icicle);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        String shuf = getIntent().getStringExtra("autoshuffle");
        if ("true".equals(shuf)) {
            mAutoShuffle = true;
        }
        MusicUtils.bindToService(this, new ServiceConnection() {
            public void onServiceConnected(ComponentName classname, IBinder obj) {
                updateMenu();
            }

            public void onServiceDisconnected(ComponentName classname) {
                updateMenu();
            }
        
        });
        setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
        init();
    
public booleanonCreateOptionsMenu(android.view.Menu menu)

        super.onCreateOptionsMenu(menu);
         menu.add(0, PARTY_SHUFFLE, 0, R.string.party_shuffle); // icon will be set in onPrepareOptionsMenu()
         menu.add(0, SEARCH_MUSIC, 0, R.string.search_title).setIcon(android.R.drawable.ic_menu_search);
        return true;
    
public voidonDestroy()

        MusicUtils.unbindFromService(this);
        super.onDestroy();
    
public booleanonOptionsItemSelected(android.view.MenuItem item)

        Intent intent;
        try {
            switch (item.getItemId()) {
                case PARTY_SHUFFLE:
                    int shuffle = MusicUtils.sService.getShuffleMode();
                    if (shuffle == MediaPlaybackService.SHUFFLE_AUTO) {
                        MusicUtils.sService.setShuffleMode(MediaPlaybackService.SHUFFLE_NONE);
                    } else {
                        MusicUtils.sService.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
                    }
                    break;
                    
                case SEARCH_MUSIC: {
                    startSearch("", false, null, false);
                    return true;
                }
            }
        } catch (RemoteException ex) {
        }
        return super.onOptionsItemSelected(item);
    
public voidonPause()

        super.onPause();
        unregisterReceiver(mStatusListener);
    
public booleanonPrepareOptionsMenu(android.view.Menu menu)

        MenuItem item = menu.findItem(PARTY_SHUFFLE);
        if (item != null) {
            int shuffle = MusicUtils.getCurrentShuffleMode();
            if (shuffle == MediaPlaybackService.SHUFFLE_AUTO) {
                item.setIcon(R.drawable.ic_menu_party_shuffle);
                item.setTitle(R.string.party_shuffle_off);
            } else {
                item.setIcon(R.drawable.ic_menu_party_shuffle);
                item.setTitle(R.string.party_shuffle);
            }
        }
        return true;
    
public voidonResume()

        super.onResume();
        IntentFilter f = new IntentFilter();
        f.addAction(MediaPlaybackService.META_CHANGED);
        registerReceiver(mStatusListener, new IntentFilter(f));
        updateMenu();
        if (mAutoShuffle) {
            mAutoShuffle = false;
            doAutoShuffle();
        }
    
private voidupdateMenu()

        try {
            if (MusicUtils.sService != null && MusicUtils.sService.getAudioId() != -1) {
                makeNowPlayingView();
                mNowPlayingView.setVisibility(View.VISIBLE);
                return;
            }
        } catch (RemoteException ex) {
        }
        mNowPlayingView.setVisibility(View.INVISIBLE);