2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.music;
19 import android.app.Activity;
20 import android.app.SearchManager;
21 import android.content.BroadcastReceiver;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.content.ServiceConnection;
27 import android.graphics.drawable.Drawable;
28 import android.media.AudioManager;
29 import android.media.MediaFile;
30 import android.net.Uri;
31 import android.os.Bundle;
32 import android.os.RemoteException;
33 import android.os.IBinder;
34 import android.provider.MediaStore;
35 import android.view.Menu;
36 import android.view.MenuItem;
37 import android.view.MotionEvent;
38 import android.view.View;
39 import android.view.Window;
40 import android.widget.ImageButton;
41 import android.widget.TextView;
43 public class MusicBrowserActivity extends Activity
44 implements MusicUtils.Defs, View.OnClickListener {
45 private View mNowPlayingView;
46 private TextView mTitle;
47 private TextView mArtist;
48 private boolean mAutoShuffle = false;
49 private static final int SEARCH_MUSIC = CHILD_MENU_BASE;
51 public MusicBrowserActivity() {
55 * Called when the activity is first created.
58 public void onCreate(Bundle icicle) {
59 super.onCreate(icicle);
60 setVolumeControlStream(AudioManager.STREAM_MUSIC);
61 String shuf = getIntent().getStringExtra("autoshuffle");
62 if ("true".equals(shuf)) {
65 MusicUtils.bindToService(this, new ServiceConnection() {
66 public void onServiceConnected(ComponentName classname, IBinder obj) {
70 public void onServiceDisconnected(ComponentName classname) {
75 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
80 public void onDestroy() {
81 MusicUtils.unbindFromService(this);
86 requestWindowFeature(Window.FEATURE_NO_TITLE);
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);
92 ImageButton b = (ImageButton) findViewById(R.id.browse_button);
93 b.setOnClickListener(this);
94 b.setOnTouchListener(mMarqueeEnablerTouch);
95 b.setOnFocusChangeListener(mMarqueeEnablerFocus);
97 b = (ImageButton) findViewById(R.id.albums_button);
98 b.setOnClickListener(this);
99 b.setOnTouchListener(mMarqueeEnablerTouch);
100 b.setOnFocusChangeListener(mMarqueeEnablerFocus);
102 b = (ImageButton) findViewById(R.id.tracks_button);
103 b.setOnClickListener(this);
104 b.setOnTouchListener(mMarqueeEnablerTouch);
105 b.setOnFocusChangeListener(mMarqueeEnablerFocus);
107 b = (ImageButton) findViewById(R.id.playlists_button);
108 b.setOnClickListener(this);
109 b.setOnTouchListener(mMarqueeEnablerTouch);
110 b.setOnFocusChangeListener(mMarqueeEnablerFocus);
113 private View.OnTouchListener mMarqueeEnablerTouch = new View.OnTouchListener() {
115 public boolean onTouch(View v, MotionEvent event) {
116 switch (event.getAction()) {
117 case MotionEvent.ACTION_DOWN:
120 case MotionEvent.ACTION_CANCEL:
121 case MotionEvent.ACTION_UP:
129 private View.OnFocusChangeListener mMarqueeEnablerFocus = new View.OnFocusChangeListener() {
131 public void onFocusChange(View v, boolean hasFocus) {
132 doMarquee(v, hasFocus);
136 private void doMarquee(View v, boolean hasFocus) {
138 case R.id.browse_button:
139 findViewById(R.id.artists_label).setSelected(hasFocus);
141 case R.id.tracks_button:
142 findViewById(R.id.tracks_label).setSelected(hasFocus);
144 case R.id.albums_button:
145 findViewById(R.id.albums_label).setSelected(hasFocus);
147 case R.id.playlists_button:
148 findViewById(R.id.playlists_label).setSelected(hasFocus);
153 private void updateMenu() {
155 if (MusicUtils.sService != null && MusicUtils.sService.getAudioId() != -1) {
156 makeNowPlayingView();
157 mNowPlayingView.setVisibility(View.VISIBLE);
160 } catch (RemoteException ex) {
162 mNowPlayingView.setVisibility(View.INVISIBLE);
166 public void onResume() {
168 IntentFilter f = new IntentFilter();
169 f.addAction(MediaPlaybackService.META_CHANGED);
170 registerReceiver(mStatusListener, new IntentFilter(f));
173 mAutoShuffle = false;
179 public void onPause() {
181 unregisterReceiver(mStatusListener);
185 public boolean onCreateOptionsMenu(Menu menu) {
186 super.onCreateOptionsMenu(menu);
187 menu.add(0, PARTY_SHUFFLE, 0, R.string.party_shuffle); // icon will be set in onPrepareOptionsMenu()
188 menu.add(0, SEARCH_MUSIC, 0, R.string.search_title).setIcon(android.R.drawable.ic_menu_search);
193 public boolean onPrepareOptionsMenu(Menu menu) {
194 MenuItem item = menu.findItem(PARTY_SHUFFLE);
196 int shuffle = MusicUtils.getCurrentShuffleMode();
197 if (shuffle == MediaPlaybackService.SHUFFLE_AUTO) {
198 item.setIcon(R.drawable.ic_menu_party_shuffle);
199 item.setTitle(R.string.party_shuffle_off);
201 item.setIcon(R.drawable.ic_menu_party_shuffle);
202 item.setTitle(R.string.party_shuffle);
209 public boolean onOptionsItemSelected(MenuItem item) {
212 switch (item.getItemId()) {
214 int shuffle = MusicUtils.sService.getShuffleMode();
215 if (shuffle == MediaPlaybackService.SHUFFLE_AUTO) {
216 MusicUtils.sService.setShuffleMode(MediaPlaybackService.SHUFFLE_NONE);
218 MusicUtils.sService.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
223 intent = new Intent(Intent.ACTION_PICK);
224 intent.setClass(this, QueryBrowserActivity.class);
225 intent.putExtra(SearchManager.QUERY, "");
226 startActivity(intent);
230 } catch (RemoteException ex) {
232 return super.onOptionsItemSelected(item);
236 // TODO: Activities are requested to call onSearchRequested, and to override
237 // that function in order to insert custom fields (e.g. the search bundle). startSearch
238 // was not intended to be overridden.
240 public void startSearch(String initialQuery, boolean selectQuery, Bundle appSearchData,
241 boolean globalSearch) {
242 Intent intent = new Intent(Intent.ACTION_PICK);
243 intent.setClass(this, QueryBrowserActivity.class);
244 intent.putExtra(SearchManager.QUERY, initialQuery);
245 startActivity(intent);
248 public void onClick(View v) {
251 case R.id.browse_button:
252 intent = new Intent(Intent.ACTION_PICK);
253 intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/artistalbum");
254 startActivity(intent);
256 case R.id.albums_button:
257 intent = new Intent(Intent.ACTION_PICK);
258 intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/album");
259 startActivity(intent);
261 case R.id.tracks_button:
262 intent = new Intent(Intent.ACTION_PICK);
263 intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track");
264 startActivity(intent);
266 case R.id.playlists_button:
267 intent = new Intent(Intent.ACTION_PICK);
268 intent.setDataAndType(Uri.EMPTY, MediaStore.Audio.Playlists.CONTENT_TYPE);
269 startActivity(intent);
271 case R.id.nowplaying:
272 intent = new Intent("com.android.music.PLAYBACK_VIEWER");
273 startActivity(intent);
278 private void doAutoShuffle() {
279 bindService((new Intent()).setClass(this, MediaPlaybackService.class), autoshuffle, 0);
282 private ServiceConnection autoshuffle = new ServiceConnection() {
283 public void onServiceConnected(ComponentName classname, IBinder obj) {
284 // we need to be able to bind again, so unbind
286 IMediaPlaybackService serv = IMediaPlaybackService.Stub.asInterface(obj);
289 serv.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
291 } catch (RemoteException ex) {
296 public void onServiceDisconnected(ComponentName classname) {
300 private void makeNowPlayingView() {
302 mTitle.setText(MusicUtils.sService.getTrackName());
303 String artistName = MusicUtils.sService.getArtistName();
304 if (MediaFile.UNKNOWN_STRING.equals(artistName)) {
305 artistName = getString(R.string.unknown_artist_name);
307 mArtist.setText(artistName);
308 mNowPlayingView.setOnFocusChangeListener(mFocuser);
309 mNowPlayingView.setOnClickListener(this);
310 } catch (RemoteException ex) {
315 View.OnFocusChangeListener mFocuser = new View.OnFocusChangeListener() {
318 public void onFocusChange(View v, boolean hasFocus) {
321 mBack = mNowPlayingView.getBackground();
323 Drawable dr = getResources().getDrawable(android.R.drawable.menuitem_background);
324 dr.setState(new int[] { android.R.attr.state_focused});
325 mNowPlayingView.setBackgroundDrawable(dr);
327 mNowPlayingView.setBackgroundDrawable(mBack);
332 private BroadcastReceiver mStatusListener = new BroadcastReceiver() {
334 public void onReceive(Context context, Intent intent) {
335 // this receiver is only used for META_CHANGED events