OSDN Git Service

auto import from //branches/cupcake/...@125939
[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.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;
42
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;
50
51     public MusicBrowserActivity() {
52     }
53
54     /**
55      * Called when the activity is first created.
56      */
57     @Override
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)) {
63             mAutoShuffle = true;
64         }
65         MusicUtils.bindToService(this, new ServiceConnection() {
66             public void onServiceConnected(ComponentName classname, IBinder obj) {
67                 updateMenu();
68             }
69
70             public void onServiceDisconnected(ComponentName classname) {
71                 updateMenu();
72             }
73         
74         });
75         setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
76         init();
77     }
78
79     @Override
80     public void onDestroy() {
81         MusicUtils.unbindFromService(this);
82         super.onDestroy();
83     }
84
85     public void init() {
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);
91         
92         ImageButton b = (ImageButton) findViewById(R.id.browse_button); 
93         b.setOnClickListener(this);
94         b.setOnTouchListener(mMarqueeEnablerTouch);
95         b.setOnFocusChangeListener(mMarqueeEnablerFocus);
96         
97         b = (ImageButton) findViewById(R.id.albums_button);
98         b.setOnClickListener(this);
99         b.setOnTouchListener(mMarqueeEnablerTouch);
100         b.setOnFocusChangeListener(mMarqueeEnablerFocus);
101
102         b = (ImageButton) findViewById(R.id.tracks_button);
103         b.setOnClickListener(this);
104         b.setOnTouchListener(mMarqueeEnablerTouch);
105         b.setOnFocusChangeListener(mMarqueeEnablerFocus);
106
107         b = (ImageButton) findViewById(R.id.playlists_button);
108         b.setOnClickListener(this);
109         b.setOnTouchListener(mMarqueeEnablerTouch);
110         b.setOnFocusChangeListener(mMarqueeEnablerFocus);
111     }
112     
113     private View.OnTouchListener mMarqueeEnablerTouch = new View.OnTouchListener() {
114
115         public boolean onTouch(View v, MotionEvent event) {
116             switch (event.getAction()) {
117                 case MotionEvent.ACTION_DOWN:
118                     doMarquee(v, true);
119                     break;
120                 case MotionEvent.ACTION_CANCEL:
121                 case MotionEvent.ACTION_UP:
122                     doMarquee(v, false);
123                     break;
124             }
125             return false;
126         }
127     };
128     
129     private View.OnFocusChangeListener mMarqueeEnablerFocus = new View.OnFocusChangeListener() {
130
131         public void onFocusChange(View v, boolean hasFocus) {
132             doMarquee(v, hasFocus);
133         }
134     };
135     
136     private void doMarquee(View v, boolean hasFocus) {
137         switch (v.getId()) {
138             case R.id.browse_button:
139                 findViewById(R.id.artists_label).setSelected(hasFocus);
140                 break;
141             case R.id.tracks_button:
142                 findViewById(R.id.tracks_label).setSelected(hasFocus);
143                 break;
144             case R.id.albums_button:
145                 findViewById(R.id.albums_label).setSelected(hasFocus);
146                 break;
147             case R.id.playlists_button:
148                 findViewById(R.id.playlists_label).setSelected(hasFocus);
149                 break;
150           }
151     }
152
153     private void updateMenu() {
154         try {
155             if (MusicUtils.sService != null && MusicUtils.sService.getAudioId() != -1) {
156                 makeNowPlayingView();
157                 mNowPlayingView.setVisibility(View.VISIBLE);
158                 return;
159             }
160         } catch (RemoteException ex) {
161         }
162         mNowPlayingView.setVisibility(View.INVISIBLE);
163     }
164
165     @Override
166     public void onResume() {
167         super.onResume();
168         IntentFilter f = new IntentFilter();
169         f.addAction(MediaPlaybackService.META_CHANGED);
170         registerReceiver(mStatusListener, new IntentFilter(f));
171         updateMenu();
172         if (mAutoShuffle) {
173             mAutoShuffle = false;
174             doAutoShuffle();
175         }
176     }
177
178     @Override
179     public void onPause() {
180         super.onPause();
181         unregisterReceiver(mStatusListener);
182     }
183
184     @Override
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);
189         return true;
190     }
191
192     @Override
193     public boolean onPrepareOptionsMenu(Menu menu) {
194         MenuItem item = menu.findItem(PARTY_SHUFFLE);
195         if (item != null) {
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);
200             } else {
201                 item.setIcon(R.drawable.ic_menu_party_shuffle);
202                 item.setTitle(R.string.party_shuffle);
203             }
204         }
205         return true;
206     }
207
208     @Override
209     public boolean onOptionsItemSelected(MenuItem item) {
210         Intent intent;
211         try {
212             switch (item.getItemId()) {
213                 case PARTY_SHUFFLE:
214                     int shuffle = MusicUtils.sService.getShuffleMode();
215                     if (shuffle == MediaPlaybackService.SHUFFLE_AUTO) {
216                         MusicUtils.sService.setShuffleMode(MediaPlaybackService.SHUFFLE_NONE);
217                     } else {
218                         MusicUtils.sService.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
219                     }
220                     break;
221                     
222                 case SEARCH_MUSIC: {
223                     intent = new Intent(Intent.ACTION_PICK);
224                     intent.setClass(this, QueryBrowserActivity.class);
225                     intent.putExtra(SearchManager.QUERY, "");
226                     startActivity(intent);
227                     return true;
228                 }
229             }
230         } catch (RemoteException ex) {
231         }
232         return super.onOptionsItemSelected(item);
233     }
234     
235     
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.
239     @Override
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);
246     }
247
248     public void onClick(View v) {
249         Intent intent;
250         switch (v.getId()) {
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);
255                 break;
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);
260                 break;
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);
265                 break;
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);
270                 break;
271             case R.id.nowplaying:
272                 intent = new Intent("com.android.music.PLAYBACK_VIEWER");
273                 startActivity(intent);
274                 break;
275         }
276     }
277
278     private void doAutoShuffle() {
279         bindService((new Intent()).setClass(this, MediaPlaybackService.class), autoshuffle, 0);
280     }
281
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
285             unbindService(this);
286             IMediaPlaybackService serv = IMediaPlaybackService.Stub.asInterface(obj);
287             if (serv != null) {
288                 try {
289                     serv.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
290                     updateMenu();
291                 } catch (RemoteException ex) {
292                 }
293             }
294         }
295
296         public void onServiceDisconnected(ComponentName classname) {
297         }
298     };
299
300     private void makeNowPlayingView() {
301         try {
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);
306             }
307             mArtist.setText(artistName);
308             mNowPlayingView.setOnFocusChangeListener(mFocuser);
309             mNowPlayingView.setOnClickListener(this);
310         } catch (RemoteException ex) {
311
312         }
313     }
314
315     View.OnFocusChangeListener mFocuser = new View.OnFocusChangeListener() {
316         Drawable mBack;
317
318         public void onFocusChange(View v, boolean hasFocus) {
319             if (hasFocus) {
320                 if (mBack == null) {
321                     mBack = mNowPlayingView.getBackground();
322                 }
323                 Drawable dr = getResources().getDrawable(android.R.drawable.menuitem_background);
324                 dr.setState(new int[] { android.R.attr.state_focused});
325                 mNowPlayingView.setBackgroundDrawable(dr);
326             } else {
327                 mNowPlayingView.setBackgroundDrawable(mBack);
328             }
329         }
330     };
331
332     private BroadcastReceiver mStatusListener = new BroadcastReceiver() {
333         @Override
334         public void onReceive(Context context, Intent intent) {
335             // this receiver is only used for META_CHANGED events
336             updateMenu();
337         }
338     };
339 }
340