OSDN Git Service

auto import from //branches/cupcake/...@132276
[android-x86/packages-apps-Music.git] / src / com / android / music / MusicBrowserActivity.java
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
17 package com.android.music;
18
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;
43
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;
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             unbindService(this);
222             IMediaPlaybackService serv = IMediaPlaybackService.Stub.asInterface(obj);
223             if (serv != null) {
224                 try {
225                     serv.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
226                     updateMenu();
227                 } catch (RemoteException ex) {
228                 }
229             }
230         }
231
232         public void onServiceDisconnected(ComponentName classname) {
233         }
234     };
235
236     private void makeNowPlayingView() {
237         try {
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);
242             }
243             mArtist.setText(artistName);
244             mNowPlayingView.setOnFocusChangeListener(mFocuser);
245             mNowPlayingView.setOnClickListener(this);
246         } catch (RemoteException ex) {
247
248         }
249     }
250
251     View.OnFocusChangeListener mFocuser = new View.OnFocusChangeListener() {
252         Drawable mBack;
253
254         public void onFocusChange(View v, boolean hasFocus) {
255             if (hasFocus) {
256                 if (mBack == null) {
257                     mBack = mNowPlayingView.getBackground();
258                 }
259                 Drawable dr = getResources().getDrawable(android.R.drawable.menuitem_background);
260                 dr.setState(new int[] { android.R.attr.state_focused});
261                 mNowPlayingView.setBackgroundDrawable(dr);
262             } else {
263                 mNowPlayingView.setBackgroundDrawable(mBack);
264             }
265         }
266     };
267
268     private BroadcastReceiver mStatusListener = new BroadcastReceiver() {
269         @Override
270         public void onReceive(Context context, Intent intent) {
271             // this receiver is only used for META_CHANGED events
272             updateMenu();
273         }
274     };
275 }
276