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.util.Log;
36 import android.view.Menu;
37 import android.view.MenuItem;
38 import android.view.MotionEvent;
39 import android.view.View;
40 import android.view.Window;
41 import android.widget.ImageButton;
42 import android.widget.TextView;
44 public 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;
52 public MusicBrowserActivity() {
56 * Called when the activity is first created.
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)) {
66 MusicUtils.bindToService(this, new ServiceConnection() {
67 public void onServiceConnected(ComponentName classname, IBinder obj) {
71 public void onServiceDisconnected(ComponentName classname) {
76 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
81 public void onDestroy() {
82 MusicUtils.unbindFromService(this);
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 View b = (View) findViewById(R.id.browse_button);
93 b.setOnClickListener(this);
95 b = (View) findViewById(R.id.albums_button);
96 b.setOnClickListener(this);
98 b = (View) findViewById(R.id.tracks_button);
99 b.setOnClickListener(this);
101 b = (View) findViewById(R.id.playlists_button);
102 b.setOnClickListener(this);
105 private void updateMenu() {
107 if (MusicUtils.sService != null && MusicUtils.sService.getAudioId() != -1) {
108 makeNowPlayingView();
109 mNowPlayingView.setVisibility(View.VISIBLE);
112 } catch (RemoteException ex) {
114 mNowPlayingView.setVisibility(View.INVISIBLE);
118 public void onResume() {
120 IntentFilter f = new IntentFilter();
121 f.addAction(MediaPlaybackService.META_CHANGED);
122 registerReceiver(mStatusListener, new IntentFilter(f));
125 mAutoShuffle = false;
131 public void onPause() {
133 unregisterReceiver(mStatusListener);
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);
145 public boolean onPrepareOptionsMenu(Menu menu) {
146 MenuItem item = menu.findItem(PARTY_SHUFFLE);
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);
153 item.setIcon(R.drawable.ic_menu_party_shuffle);
154 item.setTitle(R.string.party_shuffle);
161 public boolean onOptionsItemSelected(MenuItem item) {
164 switch (item.getItemId()) {
166 int shuffle = MusicUtils.sService.getShuffleMode();
167 if (shuffle == MediaPlaybackService.SHUFFLE_AUTO) {
168 MusicUtils.sService.setShuffleMode(MediaPlaybackService.SHUFFLE_NONE);
170 MusicUtils.sService.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
175 startSearch("", false, null, false);
179 } catch (RemoteException ex) {
181 return super.onOptionsItemSelected(item);
184 public void onClick(View v) {
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);
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);
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);
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);
207 case R.id.nowplaying:
208 intent = new Intent("com.android.music.PLAYBACK_VIEWER");
209 startActivity(intent);
214 private void doAutoShuffle() {
215 bindService((new Intent()).setClass(this, MediaPlaybackService.class), autoshuffle, 0);
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
222 IMediaPlaybackService serv = IMediaPlaybackService.Stub.asInterface(obj);
225 serv.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
227 } catch (RemoteException ex) {
232 public void onServiceDisconnected(ComponentName classname) {
236 private void makeNowPlayingView() {
238 mTitle.setText(MusicUtils.sService.getTrackName());
239 String artistName = MusicUtils.sService.getArtistName();
240 if (MediaFile.UNKNOWN_STRING.equals(artistName)) {
241 artistName = getString(R.string.unknown_artist_name);
243 mArtist.setText(artistName);
244 mNowPlayingView.setOnFocusChangeListener(mFocuser);
245 mNowPlayingView.setOnClickListener(this);
246 } catch (RemoteException ex) {
251 View.OnFocusChangeListener mFocuser = new View.OnFocusChangeListener() {
254 public void onFocusChange(View v, boolean hasFocus) {
257 mBack = mNowPlayingView.getBackground();
259 Drawable dr = getResources().getDrawable(android.R.drawable.menuitem_background);
260 dr.setState(new int[] { android.R.attr.state_focused});
261 mNowPlayingView.setBackgroundDrawable(dr);
263 mNowPlayingView.setBackgroundDrawable(mBack);
268 private BroadcastReceiver mStatusListener = new BroadcastReceiver() {
270 public void onReceive(Context context, Intent intent) {
271 // this receiver is only used for META_CHANGED events