MusicBrowserActivity.java revision 37ac4438fa1693597cc3b00fb5e24335bdcfab92
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.music; 18 19import android.app.Activity; 20import android.app.SearchManager; 21import android.content.BroadcastReceiver; 22import android.content.ComponentName; 23import android.content.Context; 24import android.content.Intent; 25import android.content.IntentFilter; 26import android.content.ServiceConnection; 27import android.graphics.drawable.Drawable; 28import android.media.AudioManager; 29import android.media.MediaFile; 30import android.net.Uri; 31import android.os.Bundle; 32import android.os.RemoteException; 33import android.os.IBinder; 34import android.provider.MediaStore; 35import android.util.Log; 36import android.view.Menu; 37import android.view.MenuItem; 38import android.view.MotionEvent; 39import android.view.View; 40import android.view.Window; 41import android.widget.ImageButton; 42import android.widget.TextView; 43 44public class MusicBrowserActivity extends Activity 45 implements MusicUtils.Defs, View.OnClickListener { 46 private View mNowPlayingView; 47 private TextView mTitle; 48 private TextView mArtist; 49 private boolean mAutoShuffle = false; 50 private static final int SEARCH_MUSIC = CHILD_MENU_BASE; 51 52 public MusicBrowserActivity() { 53 } 54 55 /** 56 * Called when the activity is first created. 57 */ 58 @Override 59 public void onCreate(Bundle icicle) { 60 super.onCreate(icicle); 61 setVolumeControlStream(AudioManager.STREAM_MUSIC); 62 String shuf = getIntent().getStringExtra("autoshuffle"); 63 if ("true".equals(shuf)) { 64 mAutoShuffle = true; 65 } 66 MusicUtils.bindToService(this, new ServiceConnection() { 67 public void onServiceConnected(ComponentName classname, IBinder obj) { 68 updateMenu(); 69 } 70 71 public void onServiceDisconnected(ComponentName classname) { 72 updateMenu(); 73 } 74 75 }); 76 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); 77 init(); 78 } 79 80 @Override 81 public void onDestroy() { 82 MusicUtils.unbindFromService(this); 83 super.onDestroy(); 84 } 85 86 public void init() { 87 setContentView(R.layout.music_library); 88 mNowPlayingView = findViewById(R.id.nowplaying); 89 mTitle = (TextView) mNowPlayingView.findViewById(R.id.title); 90 mArtist = (TextView) mNowPlayingView.findViewById(R.id.artist); 91 92 View b = (View) findViewById(R.id.browse_button); 93 b.setOnClickListener(this); 94 95 b = (View) findViewById(R.id.albums_button); 96 b.setOnClickListener(this); 97 98 b = (View) findViewById(R.id.tracks_button); 99 b.setOnClickListener(this); 100 101 b = (View) findViewById(R.id.playlists_button); 102 b.setOnClickListener(this); 103 } 104 105 private void updateMenu() { 106 try { 107 if (MusicUtils.sService != null && MusicUtils.sService.getAudioId() != -1) { 108 makeNowPlayingView(); 109 mNowPlayingView.setVisibility(View.VISIBLE); 110 return; 111 } 112 } catch (RemoteException ex) { 113 } 114 mNowPlayingView.setVisibility(View.INVISIBLE); 115 } 116 117 @Override 118 public void onResume() { 119 super.onResume(); 120 IntentFilter f = new IntentFilter(); 121 f.addAction(MediaPlaybackService.META_CHANGED); 122 registerReceiver(mStatusListener, new IntentFilter(f)); 123 updateMenu(); 124 if (mAutoShuffle) { 125 mAutoShuffle = false; 126 doAutoShuffle(); 127 } 128 } 129 130 @Override 131 public void onPause() { 132 super.onPause(); 133 unregisterReceiver(mStatusListener); 134 } 135 136 @Override 137 public boolean onCreateOptionsMenu(Menu menu) { 138 super.onCreateOptionsMenu(menu); 139 menu.add(0, PARTY_SHUFFLE, 0, R.string.party_shuffle); // icon will be set in onPrepareOptionsMenu() 140 menu.add(0, SEARCH_MUSIC, 0, R.string.search_title).setIcon(android.R.drawable.ic_menu_search); 141 return true; 142 } 143 144 @Override 145 public boolean onPrepareOptionsMenu(Menu menu) { 146 MenuItem item = menu.findItem(PARTY_SHUFFLE); 147 if (item != null) { 148 int shuffle = MusicUtils.getCurrentShuffleMode(); 149 if (shuffle == MediaPlaybackService.SHUFFLE_AUTO) { 150 item.setIcon(R.drawable.ic_menu_party_shuffle); 151 item.setTitle(R.string.party_shuffle_off); 152 } else { 153 item.setIcon(R.drawable.ic_menu_party_shuffle); 154 item.setTitle(R.string.party_shuffle); 155 } 156 } 157 return true; 158 } 159 160 @Override 161 public boolean onOptionsItemSelected(MenuItem item) { 162 Intent intent; 163 try { 164 switch (item.getItemId()) { 165 case PARTY_SHUFFLE: 166 int shuffle = MusicUtils.sService.getShuffleMode(); 167 if (shuffle == MediaPlaybackService.SHUFFLE_AUTO) { 168 MusicUtils.sService.setShuffleMode(MediaPlaybackService.SHUFFLE_NONE); 169 } else { 170 MusicUtils.sService.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO); 171 } 172 break; 173 174 case SEARCH_MUSIC: { 175 startSearch("", false, null, false); 176 return true; 177 } 178 } 179 } catch (RemoteException ex) { 180 } 181 return super.onOptionsItemSelected(item); 182 } 183 184 public void onClick(View v) { 185 Intent intent; 186 switch (v.getId()) { 187 case R.id.browse_button: 188 intent = new Intent(Intent.ACTION_PICK); 189 intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/artistalbum"); 190 startActivity(intent); 191 break; 192 case R.id.albums_button: 193 intent = new Intent(Intent.ACTION_PICK); 194 intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/album"); 195 startActivity(intent); 196 break; 197 case R.id.tracks_button: 198 intent = new Intent(Intent.ACTION_PICK); 199 intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track"); 200 startActivity(intent); 201 break; 202 case R.id.playlists_button: 203 intent = new Intent(Intent.ACTION_PICK); 204 intent.setDataAndType(Uri.EMPTY, MediaStore.Audio.Playlists.CONTENT_TYPE); 205 startActivity(intent); 206 break; 207 case R.id.nowplaying: 208 intent = new Intent("com.android.music.PLAYBACK_VIEWER"); 209 startActivity(intent); 210 break; 211 } 212 } 213 214 private void doAutoShuffle() { 215 bindService((new Intent()).setClass(this, MediaPlaybackService.class), autoshuffle, 0); 216 } 217 218 private ServiceConnection autoshuffle = new ServiceConnection() { 219 public void onServiceConnected(ComponentName classname, IBinder obj) { 220 // we need to be able to bind again, so unbind 221 try { 222 unbindService(this); 223 } catch (IllegalArgumentException e) { 224 } 225 IMediaPlaybackService serv = IMediaPlaybackService.Stub.asInterface(obj); 226 if (serv != null) { 227 try { 228 serv.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO); 229 updateMenu(); 230 } catch (RemoteException ex) { 231 } 232 } 233 } 234 235 public void onServiceDisconnected(ComponentName classname) { 236 } 237 }; 238 239 private void makeNowPlayingView() { 240 try { 241 mTitle.setText(MusicUtils.sService.getTrackName()); 242 String artistName = MusicUtils.sService.getArtistName(); 243 if (MediaFile.UNKNOWN_STRING.equals(artistName)) { 244 artistName = getString(R.string.unknown_artist_name); 245 } 246 mArtist.setText(artistName); 247 mNowPlayingView.setOnFocusChangeListener(mFocuser); 248 mNowPlayingView.setOnClickListener(this); 249 } catch (RemoteException ex) { 250 251 } 252 } 253 254 View.OnFocusChangeListener mFocuser = new View.OnFocusChangeListener() { 255 Drawable mBack; 256 257 public void onFocusChange(View v, boolean hasFocus) { 258 if (hasFocus) { 259 if (mBack == null) { 260 mBack = mNowPlayingView.getBackground(); 261 } 262 Drawable dr = getResources().getDrawable(android.R.drawable.menuitem_background); 263 dr.setState(new int[] { android.R.attr.state_focused}); 264 mNowPlayingView.setBackgroundDrawable(dr); 265 mNowPlayingView.setSelected(true); 266 } else { 267 mNowPlayingView.setBackgroundDrawable(mBack); 268 mNowPlayingView.setSelected(false); 269 } 270 } 271 }; 272 273 private BroadcastReceiver mStatusListener = new BroadcastReceiver() { 274 @Override 275 public void onReceive(Context context, Intent intent) { 276 // this receiver is only used for META_CHANGED events 277 updateMenu(); 278 } 279 }; 280} 281 282