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.AlertDialog;
21 import android.app.SearchManager;
22 import android.content.BroadcastReceiver;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.DialogInterface;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.content.ServiceConnection;
29 import android.graphics.Bitmap;
30 import android.media.AudioManager;
31 import android.media.MediaFile;
32 import android.net.Uri;
33 import android.os.Bundle;
34 import android.os.RemoteException;
35 import android.os.Handler;
36 import android.os.IBinder;
37 import android.os.Looper;
38 import android.os.Message;
39 import android.os.SystemClock;
40 import android.provider.MediaStore;
41 import android.text.Layout;
42 import android.text.TextUtils.TruncateAt;
43 import android.util.Log;
44 import android.view.KeyEvent;
45 import android.view.Menu;
46 import android.view.MenuItem;
47 import android.view.MotionEvent;
48 import android.view.SubMenu;
49 import android.view.View;
50 import android.view.ViewConfiguration;
51 import android.view.Window;
52 import android.widget.ImageButton;
53 import android.widget.ImageView;
54 import android.widget.ProgressBar;
55 import android.widget.SeekBar;
56 import android.widget.TextView;
57 import android.widget.Toast;
58 import android.widget.SeekBar.OnSeekBarChangeListener;
61 public class MediaPlaybackActivity extends Activity implements MusicUtils.Defs,
62 View.OnTouchListener, View.OnLongClickListener
64 private static final int USE_AS_RINGTONE = CHILD_MENU_BASE;
66 private boolean mOneShot = false;
67 private boolean mSeeking = false;
68 private boolean mTrackball;
69 private long mStartSeekPos = 0;
70 private long mLastSeekEventTime;
71 private IMediaPlaybackService mService = null;
72 private RepeatingImageButton mPrevButton;
73 private ImageButton mPauseButton;
74 private RepeatingImageButton mNextButton;
75 private ImageButton mRepeatButton;
76 private ImageButton mShuffleButton;
77 private ImageButton mQueueButton;
78 private Worker mAlbumArtWorker;
79 private AlbumArtHandler mAlbumArtHandler;
81 private boolean mRelaunchAfterConfigChange;
82 private int mTouchSlop;
84 public MediaPlaybackActivity()
88 /** Called when the activity is first created. */
90 public void onCreate(Bundle icicle)
92 super.onCreate(icicle);
93 setVolumeControlStream(AudioManager.STREAM_MUSIC);
95 mAlbumArtWorker = new Worker("album art worker");
96 mAlbumArtHandler = new AlbumArtHandler(mAlbumArtWorker.getLooper());
98 requestWindowFeature(Window.FEATURE_NO_TITLE);
99 setContentView(R.layout.audio_player);
101 mCurrentTime = (TextView) findViewById(R.id.currenttime);
102 mTotalTime = (TextView) findViewById(R.id.totaltime);
103 mProgress = (ProgressBar) findViewById(android.R.id.progress);
104 mAlbum = (ImageView) findViewById(R.id.album);
105 mArtistName = (TextView) findViewById(R.id.artistname);
106 mAlbumName = (TextView) findViewById(R.id.albumname);
107 mTrackName = (TextView) findViewById(R.id.trackname);
109 View v = (View)mArtistName.getParent();
110 v.setOnTouchListener(this);
111 v.setOnLongClickListener(this);
113 v = (View)mAlbumName.getParent();
114 v.setOnTouchListener(this);
115 v.setOnLongClickListener(this);
117 v = (View)mTrackName.getParent();
118 v.setOnTouchListener(this);
119 v.setOnLongClickListener(this);
121 mPrevButton = (RepeatingImageButton) findViewById(R.id.prev);
122 mPrevButton.setOnClickListener(mPrevListener);
123 mPrevButton.setRepeatListener(mRewListener, 260);
124 mPauseButton = (ImageButton) findViewById(R.id.pause);
125 mPauseButton.requestFocus();
126 mPauseButton.setOnClickListener(mPauseListener);
127 mNextButton = (RepeatingImageButton) findViewById(R.id.next);
128 mNextButton.setOnClickListener(mNextListener);
129 mNextButton.setRepeatListener(mFfwdListener, 260);
132 mTrackball = true; /* (See bug 1044348) (getResources().getConfiguration().navigation ==
133 Resources.Configuration.NAVIGATION_TRACKBALL);*/
135 mQueueButton = (ImageButton) findViewById(R.id.curplaylist);
136 mQueueButton.setOnClickListener(mQueueListener);
137 mShuffleButton = ((ImageButton) findViewById(R.id.shuffle));
138 mShuffleButton.setOnClickListener(mShuffleListener);
139 mRepeatButton = ((ImageButton) findViewById(R.id.repeat));
140 mRepeatButton.setOnClickListener(mRepeatListener);
142 if (mProgress instanceof SeekBar) {
143 SeekBar seeker = (SeekBar) mProgress;
144 seeker.setOnSeekBarChangeListener(mSeekListener);
146 mProgress.setMax(1000);
148 if (icicle != null) {
149 mRelaunchAfterConfigChange = icicle.getBoolean("configchange");
150 mOneShot = icicle.getBoolean("oneshot");
152 mOneShot = getIntent().getBooleanExtra("oneshot", false);
155 mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();
162 boolean mDraggingLabel = false;
164 TextView textViewForContainer(View v) {
165 View vv = v.findViewById(R.id.artistname);
166 if (vv != null) return (TextView) vv;
167 vv = v.findViewById(R.id.albumname);
168 if (vv != null) return (TextView) vv;
169 vv = v.findViewById(R.id.trackname);
170 if (vv != null) return (TextView) vv;
174 public boolean onTouch(View v, MotionEvent event) {
175 int action = event.getAction();
176 TextView tv = textViewForContainer(v);
180 if (action == MotionEvent.ACTION_DOWN) {
181 v.setBackgroundColor(0xff606060);
182 mInitialX = mLastX = (int) event.getX();
183 mDraggingLabel = false;
184 } else if (action == MotionEvent.ACTION_UP ||
185 action == MotionEvent.ACTION_CANCEL) {
186 v.setBackgroundColor(0);
187 if (mDraggingLabel) {
188 Message msg = mLabelScroller.obtainMessage(0, tv);
189 mLabelScroller.sendMessageDelayed(msg, 1000);
191 } else if (action == MotionEvent.ACTION_MOVE) {
192 if (mDraggingLabel) {
193 int scrollx = tv.getScrollX();
194 int x = (int) event.getX();
195 int delta = mLastX - x;
199 if (scrollx > mTextWidth) {
200 // scrolled the text completely off the view to the left
201 scrollx -= mTextWidth;
202 scrollx -= mViewWidth;
204 if (scrollx < -mViewWidth) {
205 // scrolled the text completely off the view to the right
206 scrollx += mViewWidth;
207 scrollx += mTextWidth;
209 tv.scrollTo(scrollx, 0);
213 int delta = mInitialX - (int) event.getX();
214 if (Math.abs(delta) > mTouchSlop) {
216 mLabelScroller.removeMessages(0, tv);
218 // Only turn ellipsizing off when it's not already off, because it
219 // causes the scroll position to be reset to 0.
220 if (tv.getEllipsize() != null) {
221 tv.setEllipsize(null);
223 Layout ll = tv.getLayout();
224 // layout might be null if the text just changed, or ellipsizing
225 // was just turned off
229 // get the non-ellipsized line width, to determine whether scrolling
230 // should even be allowed
231 mTextWidth = (int) tv.getLayout().getLineWidth(0);
232 mViewWidth = tv.getWidth();
233 if (mViewWidth > mTextWidth) {
234 tv.setEllipsize(TruncateAt.END);
238 mDraggingLabel = true;
239 tv.setHorizontalFadingEdgeEnabled(true);
247 Handler mLabelScroller = new Handler() {
249 public void handleMessage(Message msg) {
250 TextView tv = (TextView) msg.obj;
251 int x = tv.getScrollX();
255 tv.setEllipsize(TruncateAt.END);
257 Message newmsg = obtainMessage(0, tv);
258 mLabelScroller.sendMessageDelayed(newmsg, 15);
263 public boolean onLongClick(View view) {
265 CharSequence title = null;
273 artist = mService.getArtistName();
274 album = mService.getAlbumName();
275 song = mService.getTrackName();
276 } catch (RemoteException ex) {
280 boolean knownartist =
281 (artist != null) && !MediaFile.UNKNOWN_STRING.equals(artist);
284 (album != null) && !MediaFile.UNKNOWN_STRING.equals(album);
286 if (knownartist && view.equals(mArtistName.getParent())) {
288 query = artist.toString();
289 mime = MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE;
290 } else if (knownalbum && view.equals(mAlbumName.getParent())) {
293 query = artist + " " + album;
297 mime = MediaStore.Audio.Albums.ENTRY_CONTENT_TYPE;
298 } else if (view.equals(mTrackName.getParent()) || !knownartist || !knownalbum) {
299 if ((song == null) || MediaFile.UNKNOWN_STRING.equals(song)) {
300 // A popup of the form "Search for null/'' using ..." is pretty
301 // unhelpful, plus, we won't find any way to buy it anyway.
307 query = artist + " " + song;
311 mime = "audio/*"; // the specific type doesn't matter, so don't bother retrieving it
313 throw new RuntimeException("shouldn't be here");
315 title = getString(R.string.mediasearch, title);
317 Intent i = new Intent();
318 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
319 i.setAction(MediaStore.INTENT_ACTION_MEDIA_SEARCH);
320 i.putExtra(SearchManager.QUERY, query);
322 i.putExtra(MediaStore.EXTRA_MEDIA_ARTIST, artist);
325 i.putExtra(MediaStore.EXTRA_MEDIA_ALBUM, album);
327 i.putExtra(MediaStore.EXTRA_MEDIA_TITLE, song);
328 i.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, mime);
330 startActivity(Intent.createChooser(i, title));
334 private OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
335 public void onStartTrackingTouch(SeekBar bar) {
336 mLastSeekEventTime = 0;
339 public void onProgressChanged(SeekBar bar, int progress, boolean fromuser) {
340 if (!fromuser || (mService == null)) return;
341 long now = SystemClock.elapsedRealtime();
342 if ((now - mLastSeekEventTime) > 250) {
343 mLastSeekEventTime = now;
344 mPosOverride = mDuration * progress / 1000;
346 mService.seek(mPosOverride);
347 } catch (RemoteException ex) {
350 // trackball event, allow progress updates
357 public void onStopTrackingTouch(SeekBar bar) {
363 private View.OnClickListener mQueueListener = new View.OnClickListener() {
364 public void onClick(View v) {
366 new Intent(Intent.ACTION_EDIT)
367 .setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track")
368 .putExtra("playlist", "nowplaying")
373 private View.OnClickListener mShuffleListener = new View.OnClickListener() {
374 public void onClick(View v) {
379 private View.OnClickListener mRepeatListener = new View.OnClickListener() {
380 public void onClick(View v) {
385 private View.OnClickListener mPauseListener = new View.OnClickListener() {
386 public void onClick(View v) {
391 private View.OnClickListener mPrevListener = new View.OnClickListener() {
392 public void onClick(View v) {
393 if (mService == null) return;
395 if (mService.position() < 2000) {
401 } catch (RemoteException ex) {
406 private View.OnClickListener mNextListener = new View.OnClickListener() {
407 public void onClick(View v) {
408 if (mService == null) return;
411 } catch (RemoteException ex) {
416 private RepeatingImageButton.RepeatListener mRewListener =
417 new RepeatingImageButton.RepeatListener() {
418 public void onRepeat(View v, long howlong, int repcnt) {
419 scanBackward(repcnt, howlong);
423 private RepeatingImageButton.RepeatListener mFfwdListener =
424 new RepeatingImageButton.RepeatListener() {
425 public void onRepeat(View v, long howlong, int repcnt) {
426 scanForward(repcnt, howlong);
431 public void onStop() {
433 if (mService != null && mOneShot && getChangingConfigurations() == 0) {
436 } catch (RemoteException ex) {
439 mHandler.removeMessages(REFRESH);
440 unregisterReceiver(mStatusListener);
441 MusicUtils.unbindFromService(this);
446 public void onSaveInstanceState(Bundle outState) {
447 outState.putBoolean("configchange", getChangingConfigurations() != 0);
448 outState.putBoolean("oneshot", mOneShot);
449 super.onSaveInstanceState(outState);
453 public void onStart() {
457 if (false == MusicUtils.bindToService(this, osc)) {
458 // something went wrong
459 mHandler.sendEmptyMessage(QUIT);
462 IntentFilter f = new IntentFilter();
463 f.addAction(MediaPlaybackService.PLAYSTATE_CHANGED);
464 f.addAction(MediaPlaybackService.META_CHANGED);
465 f.addAction(MediaPlaybackService.PLAYBACK_COMPLETE);
466 registerReceiver(mStatusListener, new IntentFilter(f));
468 long next = refreshNow();
469 queueNextRefresh(next);
473 public void onNewIntent(Intent intent) {
475 mOneShot = intent.getBooleanExtra("oneshot", false);
479 public void onResume() {
482 setPauseButtonImage();
486 public void onDestroy()
488 mAlbumArtWorker.quit();
490 //System.out.println("***************** playback activity onDestroy\n");
494 public boolean onCreateOptionsMenu(Menu menu) {
495 super.onCreateOptionsMenu(menu);
496 // Don't show the menu items if we got launched by path/filedescriptor, since
497 // those tend to not be in the media database.
498 if (MusicUtils.getCurrentAudioId() >= 0) {
500 menu.add(0, GOTO_START, 0, R.string.goto_start).setIcon(R.drawable.ic_menu_music_library);
501 menu.add(0, PARTY_SHUFFLE, 0, R.string.party_shuffle); // icon will be set in onPrepareOptionsMenu()
503 SubMenu sub = menu.addSubMenu(0, ADD_TO_PLAYLIST, 0,
504 R.string.add_to_playlist).setIcon(android.R.drawable.ic_menu_add);
505 MusicUtils.makePlaylistMenu(this, sub);
506 menu.add(0, USE_AS_RINGTONE, 0, R.string.ringtone_menu_short).setIcon(R.drawable.ic_menu_set_as_ringtone);
507 menu.add(0, DELETE_ITEM, 0, R.string.delete_item).setIcon(R.drawable.ic_menu_delete);
513 public boolean onPrepareOptionsMenu(Menu menu) {
514 MenuItem item = menu.findItem(PARTY_SHUFFLE);
516 int shuffle = MusicUtils.getCurrentShuffleMode();
517 if (shuffle == MediaPlaybackService.SHUFFLE_AUTO) {
518 item.setIcon(R.drawable.ic_menu_party_shuffle);
519 item.setTitle(R.string.party_shuffle_off);
521 item.setIcon(R.drawable.ic_menu_party_shuffle);
522 item.setTitle(R.string.party_shuffle);
529 public boolean onOptionsItemSelected(MenuItem item) {
532 switch (item.getItemId()) {
534 intent = new Intent();
535 intent.setClass(this, MusicBrowserActivity.class);
536 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
537 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
538 startActivity(intent);
540 case USE_AS_RINGTONE: {
541 // Set the system setting to make this the current ringtone
542 if (mService != null) {
543 MusicUtils.setRingtone(this, mService.getAudioId());
548 if (mService != null) {
549 int shuffle = mService.getShuffleMode();
550 if (shuffle == MediaPlaybackService.SHUFFLE_AUTO) {
551 mService.setShuffleMode(MediaPlaybackService.SHUFFLE_NONE);
553 mService.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
556 setShuffleButtonImage();
560 intent = new Intent();
561 intent.setClass(this, CreatePlaylist.class);
562 startActivityForResult(intent, NEW_PLAYLIST);
566 case PLAYLIST_SELECTED: {
567 int [] list = new int[1];
568 list[0] = MusicUtils.getCurrentAudioId();
569 int playlist = item.getIntent().getIntExtra("playlist", 0);
570 MusicUtils.addToPlaylist(this, list, playlist);
575 if (mService != null) {
576 int [] list = new int[1];
577 list[0] = MusicUtils.getCurrentAudioId();
578 Bundle b = new Bundle();
579 b.putString("description", getString(R.string.delete_song_desc,
580 mService.getTrackName()));
581 b.putIntArray("items", list);
582 intent = new Intent();
583 intent.setClass(this, DeleteItems.class);
585 startActivityForResult(intent, -1);
590 } catch (RemoteException ex) {
592 return super.onOptionsItemSelected(item);
596 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
597 if (resultCode != RESULT_OK) {
600 switch (requestCode) {
602 Uri uri = intent.getData();
604 int [] list = new int[1];
605 list[0] = MusicUtils.getCurrentAudioId();
606 int playlist = Integer.parseInt(uri.getLastPathSegment());
607 MusicUtils.addToPlaylist(this, list, playlist);
612 private final int keyboard[][] = {
635 KeyEvent.KEYCODE_DEL,
645 KeyEvent.KEYCODE_COMMA,
646 KeyEvent.KEYCODE_PERIOD,
647 KeyEvent.KEYCODE_ENTER
655 private boolean seekMethod1(int keyCode)
657 for(int x=0;x<10;x++) {
658 for(int y=0;y<3;y++) {
659 if(keyboard[y][x] == keyCode) {
662 if(x == lastX && y == lastY) dir = 0;
663 else if (y == 0 && lastY == 0 && x > lastX) dir = 1;
664 else if (y == 0 && lastY == 0 && x < lastX) dir = -1;
666 else if (y == 2 && lastY == 2 && x > lastX) dir = -1;
667 else if (y == 2 && lastY == 2 && x < lastX) dir = 1;
669 else if (y < lastY && x <= 4) dir = 1;
670 else if (y < lastY && x >= 5) dir = -1;
672 else if (y > lastY && x <= 4) dir = -1;
673 else if (y > lastY && x >= 5) dir = 1;
677 mService.seek(mService.position() + dir * 5);
678 } catch (RemoteException ex) {
690 private boolean seekMethod2(int keyCode)
692 if (mService == null) return false;
693 for(int i=0;i<10;i++) {
694 if(keyboard[0][i] == keyCode) {
695 int seekpercentage = 100*i/10;
697 mService.seek(mService.duration() * seekpercentage / 100);
698 } catch (RemoteException ex) {
708 public boolean onKeyUp(int keyCode, KeyEvent event) {
712 case KeyEvent.KEYCODE_DPAD_LEFT:
716 if (mService != null) {
717 if (!mSeeking && mStartSeekPos >= 0) {
718 mPauseButton.requestFocus();
719 if (mStartSeekPos < 1000) {
725 scanBackward(-1, event.getEventTime() - event.getDownTime());
726 mPauseButton.requestFocus();
733 case KeyEvent.KEYCODE_DPAD_RIGHT:
737 if (mService != null) {
738 if (!mSeeking && mStartSeekPos >= 0) {
739 mPauseButton.requestFocus();
742 scanForward(-1, event.getEventTime() - event.getDownTime());
743 mPauseButton.requestFocus();
751 } catch (RemoteException ex) {
753 return super.onKeyUp(keyCode, event);
757 public boolean onKeyDown(int keyCode, KeyEvent event)
760 int repcnt = event.getRepeatCount();
762 if((seekmethod==0)?seekMethod1(keyCode):seekMethod2(keyCode))
769 case KeyEvent.KEYCODE_Q: av.adjustParams(-0.05, 0.0, 0.0, 0.0, 0.0,-1.0); break;
770 case KeyEvent.KEYCODE_E: av.adjustParams( 0.05, 0.0, 0.0, 0.0, 0.0, 1.0); break;
772 case KeyEvent.KEYCODE_W: av.adjustParams( 0.0, 0.0,-1.0, 0.0, 0.0, 0.0); break;
773 case KeyEvent.KEYCODE_X: av.adjustParams( 0.0, 0.0, 1.0, 0.0, 0.0, 0.0); break;
774 case KeyEvent.KEYCODE_A: av.adjustParams( 0.0,-1.0, 0.0, 0.0, 0.0, 0.0); break;
775 case KeyEvent.KEYCODE_D: av.adjustParams( 0.0, 1.0, 0.0, 0.0, 0.0, 0.0); break;
777 case KeyEvent.KEYCODE_R: av.adjustParams( 0.0, 0.0, 0.0, 0.0, 0.0,-1.0); break;
778 case KeyEvent.KEYCODE_U: av.adjustParams( 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); break;
780 case KeyEvent.KEYCODE_Y: av.adjustParams( 0.0, 0.0, 0.0, 0.0,-1.0, 0.0); break;
781 case KeyEvent.KEYCODE_N: av.adjustParams( 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); break;
782 case KeyEvent.KEYCODE_G: av.adjustParams( 0.0, 0.0, 0.0,-1.0, 0.0, 0.0); break;
783 case KeyEvent.KEYCODE_J: av.adjustParams( 0.0, 0.0, 0.0, 1.0, 0.0, 0.0); break;
787 case KeyEvent.KEYCODE_SLASH:
788 seekmethod = 1 - seekmethod;
791 case KeyEvent.KEYCODE_DPAD_LEFT:
795 if (!mPrevButton.hasFocus()) {
796 mPrevButton.requestFocus();
798 scanBackward(repcnt, event.getEventTime() - event.getDownTime());
800 case KeyEvent.KEYCODE_DPAD_RIGHT:
804 if (!mNextButton.hasFocus()) {
805 mNextButton.requestFocus();
807 scanForward(repcnt, event.getEventTime() - event.getDownTime());
810 case KeyEvent.KEYCODE_S:
814 case KeyEvent.KEYCODE_DPAD_CENTER:
815 case KeyEvent.KEYCODE_SPACE:
819 return super.onKeyDown(keyCode, event);
822 private void scanBackward(int repcnt, long delta) {
823 if(mService == null) return;
826 mStartSeekPos = mService.position();
827 mLastSeekEventTime = 0;
832 // seek at 10x speed for the first 5 seconds
835 // seek at 40x after that
836 delta = 50000 + (delta - 5000) * 40;
838 long newpos = mStartSeekPos - delta;
840 // move to previous track
842 long duration = mService.duration();
843 mStartSeekPos += duration;
846 if (((delta - mLastSeekEventTime) > 250) || repcnt < 0){
847 mService.seek(newpos);
848 mLastSeekEventTime = delta;
851 mPosOverride = newpos;
857 } catch (RemoteException ex) {
861 private void scanForward(int repcnt, long delta) {
862 if(mService == null) return;
865 mStartSeekPos = mService.position();
866 mLastSeekEventTime = 0;
871 // seek at 10x speed for the first 5 seconds
874 // seek at 40x after that
875 delta = 50000 + (delta - 5000) * 40;
877 long newpos = mStartSeekPos + delta;
878 long duration = mService.duration();
879 if (newpos >= duration) {
880 // move to next track
882 mStartSeekPos -= duration; // is OK to go negative
885 if (((delta - mLastSeekEventTime) > 250) || repcnt < 0){
886 mService.seek(newpos);
887 mLastSeekEventTime = delta;
890 mPosOverride = newpos;
896 } catch (RemoteException ex) {
900 private void doPauseResume() {
902 if(mService != null) {
903 if (mService.isPlaying()) {
909 setPauseButtonImage();
911 } catch (RemoteException ex) {
915 private void toggleShuffle() {
916 if (mService == null) {
920 int shuffle = mService.getShuffleMode();
921 if (shuffle == MediaPlaybackService.SHUFFLE_NONE) {
922 mService.setShuffleMode(MediaPlaybackService.SHUFFLE_NORMAL);
923 if (mService.getRepeatMode() == MediaPlaybackService.REPEAT_CURRENT) {
924 mService.setRepeatMode(MediaPlaybackService.REPEAT_ALL);
925 setRepeatButtonImage();
927 showToast(R.string.shuffle_on_notif);
928 } else if (shuffle == MediaPlaybackService.SHUFFLE_NORMAL ||
929 shuffle == MediaPlaybackService.SHUFFLE_AUTO) {
930 mService.setShuffleMode(MediaPlaybackService.SHUFFLE_NONE);
931 showToast(R.string.shuffle_off_notif);
933 Log.e("MediaPlaybackActivity", "Invalid shuffle mode: " + shuffle);
935 setShuffleButtonImage();
936 } catch (RemoteException ex) {
940 private void cycleRepeat() {
941 if (mService == null) {
945 int mode = mService.getRepeatMode();
946 if (mode == MediaPlaybackService.REPEAT_NONE) {
947 mService.setRepeatMode(MediaPlaybackService.REPEAT_ALL);
948 showToast(R.string.repeat_all_notif);
949 } else if (mode == MediaPlaybackService.REPEAT_ALL) {
950 mService.setRepeatMode(MediaPlaybackService.REPEAT_CURRENT);
951 if (mService.getShuffleMode() != MediaPlaybackService.SHUFFLE_NONE) {
952 mService.setShuffleMode(MediaPlaybackService.SHUFFLE_NONE);
953 setShuffleButtonImage();
955 showToast(R.string.repeat_current_notif);
957 mService.setRepeatMode(MediaPlaybackService.REPEAT_NONE);
958 showToast(R.string.repeat_off_notif);
960 setRepeatButtonImage();
961 } catch (RemoteException ex) {
966 private void showToast(int resid) {
967 if (mToast == null) {
968 mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
970 mToast.setText(resid);
974 private void startPlayback() {
978 Intent intent = getIntent();
979 String filename = "";
980 Uri uri = intent.getData();
981 if (uri != null && uri.toString().length() > 0) {
982 // If this is a file:// URI, just use the path directly instead
983 // of going through the open-from-filedescriptor codepath.
984 String scheme = uri.getScheme();
985 if ("file".equals(scheme)) {
986 filename = uri.getPath();
988 filename = uri.toString();
992 if (! mRelaunchAfterConfigChange) {
994 mService.openfile(filename);
997 } catch (Exception ex) {
998 Log.d("MediaPlaybackActivity", "couldn't start playback: " + ex);
1003 long next = refreshNow();
1004 queueNextRefresh(next);
1007 private ServiceConnection osc = new ServiceConnection() {
1008 public void onServiceConnected(ComponentName classname, IBinder obj) {
1009 mService = IMediaPlaybackService.Stub.asInterface(obj);
1010 if (MusicUtils.sService == null) {
1011 MusicUtils.sService = mService;
1015 // Assume something is playing when the service says it is,
1016 // but also if the audio ID is valid but the service is paused.
1017 if (mService.getAudioId() >= 0 || mService.isPlaying() ||
1018 mService.getPath() != null) {
1019 // something is playing now, we're done
1020 if (mOneShot || mService.getAudioId() < 0) {
1021 mRepeatButton.setVisibility(View.INVISIBLE);
1022 mShuffleButton.setVisibility(View.INVISIBLE);
1023 mQueueButton.setVisibility(View.INVISIBLE);
1025 mRepeatButton.setVisibility(View.VISIBLE);
1026 mShuffleButton.setVisibility(View.VISIBLE);
1027 mQueueButton.setVisibility(View.VISIBLE);
1028 setRepeatButtonImage();
1029 setShuffleButtonImage();
1031 setPauseButtonImage();
1034 } catch (RemoteException ex) {
1036 // Service is dead or not playing anything. If we got here as part
1037 // of a "play this file" Intent, exit. Otherwise go to the Music
1038 // app start screen.
1039 if (getIntent().getData() == null) {
1040 Intent intent = new Intent(Intent.ACTION_MAIN);
1041 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1042 intent.setClass(MediaPlaybackActivity.this, MusicBrowserActivity.class);
1043 startActivity(intent);
1047 public void onServiceDisconnected(ComponentName classname) {
1051 private void setRepeatButtonImage() {
1053 switch (mService.getRepeatMode()) {
1054 case MediaPlaybackService.REPEAT_ALL:
1055 mRepeatButton.setImageResource(R.drawable.ic_mp_repeat_all_btn);
1057 case MediaPlaybackService.REPEAT_CURRENT:
1058 mRepeatButton.setImageResource(R.drawable.ic_mp_repeat_once_btn);
1061 mRepeatButton.setImageResource(R.drawable.ic_mp_repeat_off_btn);
1064 } catch (RemoteException ex) {
1068 private void setShuffleButtonImage() {
1070 switch (mService.getShuffleMode()) {
1071 case MediaPlaybackService.SHUFFLE_NONE:
1072 mShuffleButton.setImageResource(R.drawable.ic_mp_shuffle_off_btn);
1074 case MediaPlaybackService.SHUFFLE_AUTO:
1075 mShuffleButton.setImageResource(R.drawable.ic_mp_partyshuffle_on_btn);
1078 mShuffleButton.setImageResource(R.drawable.ic_mp_shuffle_on_btn);
1081 } catch (RemoteException ex) {
1085 private void setPauseButtonImage() {
1087 if (mService != null && mService.isPlaying()) {
1088 mPauseButton.setImageResource(android.R.drawable.ic_media_pause);
1090 mPauseButton.setImageResource(android.R.drawable.ic_media_play);
1092 } catch (RemoteException ex) {
1096 private ImageView mAlbum;
1097 private TextView mCurrentTime;
1098 private TextView mTotalTime;
1099 private TextView mArtistName;
1100 private TextView mAlbumName;
1101 private TextView mTrackName;
1102 private ProgressBar mProgress;
1103 private long mPosOverride = -1;
1104 private boolean mFromTouch = false;
1105 private long mDuration;
1106 private int seekmethod;
1107 private boolean paused;
1109 private static final int REFRESH = 1;
1110 private static final int QUIT = 2;
1111 private static final int GET_ALBUM_ART = 3;
1112 private static final int ALBUM_ART_DECODED = 4;
1114 private void queueNextRefresh(long delay) {
1116 Message msg = mHandler.obtainMessage(REFRESH);
1117 mHandler.removeMessages(REFRESH);
1118 mHandler.sendMessageDelayed(msg, delay);
1122 private long refreshNow() {
1123 if(mService == null)
1126 long pos = mPosOverride < 0 ? mService.position() : mPosOverride;
1127 long remaining = 1000 - (pos % 1000);
1128 if ((pos >= 0) && (mDuration > 0)) {
1129 mCurrentTime.setText(MusicUtils.makeTimeString(this, pos / 1000));
1131 if (mService.isPlaying()) {
1132 mCurrentTime.setVisibility(View.VISIBLE);
1134 // blink the counter
1135 int vis = mCurrentTime.getVisibility();
1136 mCurrentTime.setVisibility(vis == View.INVISIBLE ? View.VISIBLE : View.INVISIBLE);
1140 mProgress.setProgress((int) (1000 * pos / mDuration));
1142 mCurrentTime.setText("--:--");
1143 mProgress.setProgress(1000);
1145 // return the number of milliseconds until the next full second, so
1146 // the counter can be updated at just the right time
1148 } catch (RemoteException ex) {
1153 private final Handler mHandler = new Handler() {
1155 public void handleMessage(Message msg) {
1157 case ALBUM_ART_DECODED:
1158 mAlbum.setImageBitmap((Bitmap)msg.obj);
1159 mAlbum.getDrawable().setDither(true);
1163 long next = refreshNow();
1164 queueNextRefresh(next);
1168 // This can be moved back to onCreate once the bug that prevents
1169 // Dialogs from being started from onCreate/onResume is fixed.
1170 new AlertDialog.Builder(MediaPlaybackActivity.this)
1171 .setTitle(R.string.service_start_error_title)
1172 .setMessage(R.string.service_start_error_msg)
1173 .setPositiveButton(R.string.service_start_error_button,
1174 new DialogInterface.OnClickListener() {
1175 public void onClick(DialogInterface dialog, int whichButton) {
1179 .setCancelable(false)
1189 private BroadcastReceiver mStatusListener = new BroadcastReceiver() {
1191 public void onReceive(Context context, Intent intent) {
1192 String action = intent.getAction();
1193 if (action.equals(MediaPlaybackService.META_CHANGED)) {
1194 // redraw the artist/title info and
1195 // set new max for progress bar
1197 setPauseButtonImage();
1198 queueNextRefresh(1);
1199 } else if (action.equals(MediaPlaybackService.PLAYBACK_COMPLETE)) {
1203 setPauseButtonImage();
1205 } else if (action.equals(MediaPlaybackService.PLAYSTATE_CHANGED)) {
1206 setPauseButtonImage();
1211 private void updateTrackInfo() {
1212 if (mService == null) {
1216 String path = mService.getPath();
1222 if (mService.getAudioId() < 0 && path.toLowerCase().startsWith("http://")) {
1223 ((View) mArtistName.getParent()).setVisibility(View.INVISIBLE);
1224 ((View) mAlbumName.getParent()).setVisibility(View.INVISIBLE);
1225 mAlbum.setVisibility(View.GONE);
1226 mTrackName.setText(path);
1227 mAlbumArtHandler.removeMessages(GET_ALBUM_ART);
1228 mAlbumArtHandler.obtainMessage(GET_ALBUM_ART, -1, 0).sendToTarget();
1230 ((View) mArtistName.getParent()).setVisibility(View.VISIBLE);
1231 ((View) mAlbumName.getParent()).setVisibility(View.VISIBLE);
1232 String artistName = mService.getArtistName();
1233 if (MediaFile.UNKNOWN_STRING.equals(artistName)) {
1234 artistName = getString(R.string.unknown_artist_name);
1236 mArtistName.setText(artistName);
1237 String albumName = mService.getAlbumName();
1238 int albumid = mService.getAlbumId();
1239 if (MediaFile.UNKNOWN_STRING.equals(albumName)) {
1240 albumName = getString(R.string.unknown_album_name);
1243 mAlbumName.setText(albumName);
1244 mTrackName.setText(mService.getTrackName());
1245 mAlbumArtHandler.removeMessages(GET_ALBUM_ART);
1246 mAlbumArtHandler.obtainMessage(GET_ALBUM_ART, albumid, 0).sendToTarget();
1247 mAlbum.setVisibility(View.VISIBLE);
1249 mDuration = mService.duration();
1250 mTotalTime.setText(MusicUtils.makeTimeString(this, mDuration / 1000));
1251 } catch (RemoteException ex) {
1256 public class AlbumArtHandler extends Handler {
1257 private int mAlbumId = -1;
1259 public AlbumArtHandler(Looper looper) {
1262 public void handleMessage(Message msg)
1264 int albumid = msg.arg1;
1265 if (msg.what == GET_ALBUM_ART && (mAlbumId != albumid || albumid < 0)) {
1266 // while decoding the new image, show the default album art
1267 Message numsg = mHandler.obtainMessage(ALBUM_ART_DECODED, null);
1268 mHandler.removeMessages(ALBUM_ART_DECODED);
1269 mHandler.sendMessageDelayed(numsg, 300);
1270 Bitmap bm = MusicUtils.getArtwork(MediaPlaybackActivity.this, albumid);
1272 bm = MusicUtils.getArtwork(MediaPlaybackActivity.this, -1);
1276 numsg = mHandler.obtainMessage(ALBUM_ART_DECODED, bm);
1277 mHandler.removeMessages(ALBUM_ART_DECODED);
1278 mHandler.sendMessage(numsg);
1285 private class Worker implements Runnable {
1286 private final Object mLock = new Object();
1287 private Looper mLooper;
1290 * Creates a worker thread with the given name. The thread
1291 * then runs a {@link android.os.Looper}.
1292 * @param name A name for the new thread
1294 Worker(String name) {
1295 Thread t = new Thread(null, this, name);
1296 t.setPriority(Thread.MIN_PRIORITY);
1298 synchronized (mLock) {
1299 while (mLooper == null) {
1302 } catch (InterruptedException ex) {
1308 public Looper getLooper() {
1313 synchronized (mLock) {
1315 mLooper = Looper.myLooper();
1321 public void quit() {