-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.patch
More file actions
796 lines (747 loc) · 25.6 KB
/
service.patch
File metadata and controls
796 lines (747 loc) · 25.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index b9d892a..4936d30 100755
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -63,6 +63,10 @@
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
+
+ <service
+ android:name="com.androidplayer.MusicPlayer"
+ android:process=":remote" />
</application>
</manifest>
\ No newline at end of file
diff --git a/src/com/androidplayer/MusicPlayer.java b/src/com/androidplayer/MusicPlayer.java
index d52fd00..46316fc 100755
--- a/src/com/androidplayer/MusicPlayer.java
+++ b/src/com/androidplayer/MusicPlayer.java
@@ -9,13 +9,13 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
-import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.util.URIUtil;
import shuffle.SongFactory;
import tags.Song;
import tags.Tag;
import android.app.PendingIntent;
+import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
@@ -30,80 +30,35 @@ import android.media.MediaPlayer.OnCompletionListener;
import android.media.RemoteControlClient;
import android.media.audiofx.AudioEffect;
import android.net.Uri;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.IBinder;
+import android.os.Message;
+import android.os.Messenger;
+import android.os.RemoteException;
import android.provider.MediaStore;
-import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
-public class MusicPlayer {
+public class MusicPlayer extends Service implements OnAudioFocusChangeListener {
private static final String TAG = "MusicPlayer";
- private static MusicPlayer musicPlayer = null;
- private Context context;
-
- private final String GENRES_META_FILE_NAME = "GENRES_META_FILE.txt";
+ private static final MediaPlayer player = new MediaPlayer();
+ private static final SongFactory songFactory = new SongFactory();
private final List<Song> songs = new ArrayList<Song>();
private final List<String> artists = new ArrayList<String>();
private final List<String> genres = new ArrayList<String>();
- public static final SongFactory songFactory = new SongFactory();
- public static final MediaPlayer player = new MediaPlayer();
-
- public static final String CURRENT_SONG = "com.androidplayer.CURRENT_SONG";
- public static final String SONG_CHANGED = "com.androidplayer.SONG_CHANGED";
-
+ // For lock screen audio remote control
private ComponentName mediaButtonReceiverComponent;
private RemoteControlClientCompat remoteControlClientCompat;
+ private final String GENRES_META_FILE_NAME = "GENRES_META_FILE.txt";
private static boolean wasPlaying = false;
- private final OnAudioFocusChangeListener audioFocusListener = new OnAudioFocusChangeListener() {
-
- public void onAudioFocusChange(int focusChange) {
- AudioManager am = (AudioManager) context
- .getSystemService(Context.AUDIO_SERVICE);
-
- if(focusChange != AudioManager.AUDIOFOCUS_GAIN)
- wasPlaying = isPlaying();
-
- switch (focusChange) {
- case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK):
- player.setVolume(0.2f, 0.2f);
- break;
- case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT):
- pausePlayback();
- break;
-
- case (AudioManager.AUDIOFOCUS_LOSS):
- pausePlayback();
- ComponentName component = new ComponentName(context,
- RemoteControlBroadcastReceiver.class);
- am.unregisterMediaButtonEventReceiver(component);
- break;
-
- case (AudioManager.AUDIOFOCUS_GAIN):
- player.setVolume(1f, 1f);
- if (wasPlaying)
- startPlayback();
- break;
-
- default:
- break;
- }
- }
- };
-
- public static synchronized MusicPlayer getInstance(Context context) {
- if (musicPlayer == null) {
- musicPlayer = new MusicPlayer();
- musicPlayer.initialize(context);
-
- return musicPlayer;
- }
- // send a song change request each time the music player is initialized
- musicPlayer.sendSongChangedRequest();
- return musicPlayer;
+ // Private constructor! Cannot make objects of this class directly
+ private MusicPlayer() {
}
public List<Song> getSongs() {
@@ -118,20 +73,17 @@ public class MusicPlayer {
double duration = player.getCurrentPosition() / 1000;
double maxDuration = player.getDuration() / 1000;
songFactory.setCurrent(duration, maxDuration, song);
- sendSongChangedRequest();
}
public Song getPrev() {
Song ret = songFactory.prev(player.getCurrentPosition() / 1000,
player.getDuration() / 1000);
- sendSongChangedRequest();
return ret;
}
public Song getNext() {
Song ret = songFactory.next(player.getCurrentPosition() / 1000,
player.getDuration() / 1000);
- sendSongChangedRequest();
return ret;
}
@@ -159,41 +111,26 @@ public class MusicPlayer {
player.seekTo(msec);
}
- public void playSong(Song song, boolean start)
- throws IllegalArgumentException, SecurityException,
- IllegalStateException, IOException {
+ public MediaPlayer getMediaPlayer() {
+ return player;
+ }
+ private void playSong(Song song) {
if (player.isPlaying()) {
player.stop();
}
+
player.reset();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
- player.setDataSource(getURLFileName(song.getFileName()));
- player.prepare();
-
- if (start) {
- if (getAudioFocus()) {
- remoteControlClientCompat
- .setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
- }
- player.start();
- }
- }
-
- public MediaPlayer getMediaPlayer() {
- return player;
- }
-
- private MusicPlayer() {
- }
-
- private String getURLFileName(String filename) {
try {
- return URIUtil.encodeQuery("file:///" + filename);
- } catch (URIException e) {
+ player.setDataSource(URIUtil.encodeQuery("file:///"
+ + song.getFileName()));
+ player.prepare();
+ } catch (Exception e) {
e.printStackTrace();
}
- return null;
+
+ startPlayback();
}
private void readGenresFromFile() throws IOException {
@@ -220,7 +157,7 @@ public class MusicPlayer {
String[] genresProjection = { MediaStore.Audio.Genres.NAME,
MediaStore.Audio.Genres._ID };
- mediaCursor = context.getContentResolver().query(
+ mediaCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, mediaProjection,
MediaStore.Audio.Media.IS_MUSIC + " != 0", null, null);
@@ -244,7 +181,7 @@ public class MusicPlayer {
Uri uri = MediaStore.Audio.Genres.getContentUriForAudioId(
"external", musicId);
- genresCursor = context.getContentResolver().query(uri,
+ genresCursor = getContentResolver().query(uri,
genresProjection, null, null, null);
if (genresCursor.moveToFirst()) {
@@ -292,8 +229,7 @@ public class MusicPlayer {
}
}
- private void initialize(Context context) {
- this.context = context;
+ private void initialize() {
try {
constructLists();
} catch (IOException e1) {
@@ -304,30 +240,22 @@ public class MusicPlayer {
@Override
public void onCompletion(MediaPlayer mp) {
try {
- playSong(getNext(), true);
+ playSong(getNext());
} catch (Exception e) {
e.printStackTrace();
}
}
});
- try {
- playSong(getCurrentSong(), false);
- } catch (Exception e) {
- e.printStackTrace();
- }
registerEquilizer();
- // registerRemoteClient();
registerRemoteClient();
registerAudioJackListener();
}
- private void sendSongChangedRequest() {
- Intent intent = new Intent(SONG_CHANGED);
- intent.putExtra(CURRENT_SONG, getCurrentSong());
- LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
- // update remote client
- updateRemoteClientMetaData();
+ public void finalize() {
+ unregisterEquilizer();
+ unregisterRemoteClient();
+ unregisterAudioJackListener();
}
private void registerEquilizer() {
@@ -336,29 +264,28 @@ public class MusicPlayer {
audioEffectsIntent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION,
player.getAudioSessionId());
audioEffectsIntent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME,
- context.getPackageName());
- context.sendBroadcast(audioEffectsIntent);
+ getPackageName());
+ sendBroadcast(audioEffectsIntent);
}
- private void unRegisterEquilizer() {
+ private void unregisterEquilizer() {
final Intent audioEffectsIntent = new Intent(
AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION);
audioEffectsIntent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION,
player.getAudioSessionId());
audioEffectsIntent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME,
- context.getPackageName());
- context.sendBroadcast(audioEffectsIntent);
+ getPackageName());
+ sendBroadcast(audioEffectsIntent);
}
private void registerRemoteClient() {
- AudioManager am = (AudioManager) context
- .getSystemService(Context.AUDIO_SERVICE);
+ AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
- mediaButtonReceiverComponent = new ComponentName(context,
+ mediaButtonReceiverComponent = new ComponentName(this,
RemoteControlBroadcastReceiver.class);
mediaButtonIntent.setComponent(mediaButtonReceiverComponent);
remoteControlClientCompat = new RemoteControlClientCompat(
- PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0));
+ PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0));
RemoteControlHelper.registerRemoteControlClient(am,
remoteControlClientCompat);
final int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
@@ -370,30 +297,15 @@ public class MusicPlayer {
remoteControlClientCompat.setTransportControlFlags(flags);
}
- // private void registerRemoteClient() {
- // AudioManager audioManager = (AudioManager) context
- // .getSystemService(Context.AUDIO_SERVICE);
- //
- // Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
- //
- // ComponentName remoteComponentName = new ComponentName(context,
- // RemoteControlBroadcastReceiver.class);
- // mediaButtonIntent.setComponent(remoteComponentName);
- //
- // PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context,
- // 0, mediaButtonIntent, 0);
- //
- // remoteControlClient = new RemoteControlClient(mediaPendingIntent);
- // audioManager.registerRemoteControlClient(remoteControlClient);
- //
- // remoteControlClient
- // .setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY
- // | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
- // | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
- // | RemoteControlClient.FLAG_KEY_MEDIA_NEXT);
- //
- // audioManager.registerMediaButtonEventReceiver(mediaPendingIntent);
- // }
+ private void unregisterRemoteClient() {
+ try {
+ AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
+ RemoteControlHelper.unregisterRemoteControlClient(audioManager,
+ remoteControlClientCompat);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
private void updateRemoteClientMetaData() {
remoteControlClientCompat
@@ -407,10 +319,9 @@ public class MusicPlayer {
}
private boolean getAudioFocus() {
- AudioManager am = (AudioManager) context
- .getSystemService(Context.AUDIO_SERVICE);
- int result = am.requestAudioFocus(audioFocusListener,
- AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
+ AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
+ int result = am.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
+ AudioManager.AUDIOFOCUS_GAIN);
if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
Log.i(TAG, "Audio focus not granted");
return false;
@@ -419,21 +330,7 @@ public class MusicPlayer {
return true;
}
- private void unregisterRemoteClient() {
- try {
- AudioManager audioManager = (AudioManager) context
- .getSystemService(Context.AUDIO_SERVICE);
- RemoteControlHelper.unregisterRemoteControlClient(audioManager,
- remoteControlClientCompat);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- /**
- * Pausing output when the headset is disconnected
- */
- private class NoisyAudioStreamReceiver extends BroadcastReceiver {
+ private BroadcastReceiver noisyAudioReciever = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent
@@ -441,11 +338,154 @@ public class MusicPlayer {
pausePlayback();
}
}
- }
+ };
private void registerAudioJackListener() {
IntentFilter noiseFilter = new IntentFilter(
AudioManager.ACTION_AUDIO_BECOMING_NOISY);
- context.registerReceiver(new NoisyAudioStreamReceiver(), noiseFilter);
+ registerReceiver(noisyAudioReciever, noiseFilter);
+ }
+
+ private void unregisterAudioJackListener() {
+ unregisterReceiver(noisyAudioReciever);
+ }
+
+ @Override
+ public void onAudioFocusChange(int focusChange) {
+ AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
+
+ if (focusChange != AudioManager.AUDIOFOCUS_GAIN)
+ wasPlaying = isPlaying();
+
+ switch (focusChange) {
+ case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK):
+ player.setVolume(0.2f, 0.2f);
+ break;
+
+ case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT):
+ pausePlayback();
+ break;
+
+ case (AudioManager.AUDIOFOCUS_LOSS):
+ pausePlayback();
+ ComponentName component = new ComponentName(this,
+ RemoteControlBroadcastReceiver.class);
+ am.unregisterMediaButtonEventReceiver(component);
+ break;
+
+ case (AudioManager.AUDIOFOCUS_GAIN):
+ player.setVolume(1f, 1f);
+ if (wasPlaying)
+ startPlayback();
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ private final Messenger mMessenger = new Messenger(new IncomingHandler());
+ private ArrayList<Messenger> mClients = new ArrayList<Messenger>();
+
+ // Messages
+ public static final int MSG_REGISTER_CLIENT = 10;
+ public static final int MSG_UNREGISTER_CLIENT = 20;
+ public static final int MSG_PLAY = 30;
+ public static final int MSG_PAUSE = 40;
+ public static final int MSG_NEXT = 50;
+ public static final int MSG_PREV = 60;
+ public static final int MSG_SET_CURRENT = 70;
+ public static final int MSG_GET_CURRENT = 80;
+ public static final int ACK_MSG_GET_CURRENT = 90;
+ public static final int MSG_IS_PLAYING = 100;
+ public static final int MSG_SONG_CHANGED = 110;
+
+ public static final String SONG_PAYLOAD = "com.androidplayer.musicplayer.songPayload";
+
+ class IncomingHandler extends Handler {
+ @Override
+ public void handleMessage(Message msg) {
+ Song song = null;
+ switch (msg.what) {
+ case MSG_REGISTER_CLIENT:
+ mClients.add(msg.replyTo);
+ break;
+ case MSG_UNREGISTER_CLIENT:
+ mClients.remove(msg.replyTo);
+ break;
+ case MSG_PLAY:
+ song = (Song) msg.getData().getSerializable(SONG_PAYLOAD);
+ if (song == null && getCurrentSong() != null) {
+ startPlayback();
+ } else {
+ sendSongChangedMessage(song);
+ }
+ break;
+ case MSG_PAUSE:
+ pausePlayback();
+ break;
+ case MSG_NEXT:
+ song = getNext();
+ if (isPlaying()) {
+ playSong(song);
+ } else {
+ setCurrent(song);
+ }
+ sendSongChangedMessage(song);
+ break;
+ case MSG_PREV:
+ song = getPrev();
+ if (isPlaying()) {
+ playSong(song);
+ } else {
+ setCurrent(song);
+ }
+ sendSongChangedMessage(song);
+ break;
+ case MSG_IS_PLAYING:
+ for (int i = mClients.size() - 1; i >= 0; i--) {
+ try {
+ Message out = Message.obtain(null, MSG_IS_PLAYING, 0, 0);
+ Bundle bundle = new Bundle();
+ bundle.putBoolean(SONG_PAYLOAD, isPlaying());
+ out.setData(bundle);
+ mClients.get(i).send(out);
+ } catch (RemoteException e) {
+ mClients.remove(i);
+ }
+ }
+ default:
+ super.handleMessage(msg);
+ }
+ }
+ }
+
+ private void sendSongChangedMessage(Song song) {
+ for (int i = mClients.size() - 1; i >= 0; i--) {
+ try {
+ Message out = Message.obtain(null, MSG_SONG_CHANGED, 0, 0);
+ Bundle bundle = new Bundle();
+ bundle.putSerializable(SONG_PAYLOAD, song);
+ out.setData(bundle);
+ mClients.get(i).send(out);
+ } catch (RemoteException e) {
+ mClients.remove(i);
+ }
+ }
+ }
+
+ @Override
+ public void onCreate() {
+ initialize();
+ }
+
+ @Override
+ public void onDestroy() {
+ finalize();
+ }
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ return mMessenger.getBinder();
}
}
\ No newline at end of file
diff --git a/src/com/androidplayer/RemoteControlBroadcastReceiver.java b/src/com/androidplayer/RemoteControlBroadcastReceiver.java
index 2a7687d..50bee8b 100755
--- a/src/com/androidplayer/RemoteControlBroadcastReceiver.java
+++ b/src/com/androidplayer/RemoteControlBroadcastReceiver.java
@@ -17,7 +17,7 @@ public class RemoteControlBroadcastReceiver extends BroadcastReceiver {
if (key.getAction() != KeyEvent.ACTION_DOWN)
return;
- MusicPlayer musicPlayer = MusicPlayer.getInstance(context);
+// MusicPlayer musicPlayer = MusicPlayer.getInstance(context);
switch (key.getKeyCode()) {
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
diff --git a/src/com/androidplayer/fragments/NowPlayingFragment.java b/src/com/androidplayer/fragments/NowPlayingFragment.java
index 0db2565..b1d387f 100755
--- a/src/com/androidplayer/fragments/NowPlayingFragment.java
+++ b/src/com/androidplayer/fragments/NowPlayingFragment.java
@@ -1,12 +1,17 @@
package com.androidplayer.fragments;
import tags.Song;
-import android.content.BroadcastReceiver;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
+import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
+import android.os.IBinder;
+import android.os.Message;
+import android.os.Messenger;
+import android.os.RemoteException;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
@@ -35,17 +40,100 @@ public class NowPlayingFragment extends Fragment implements FragmentInterface {
private static Handler seekHandler = new Handler();
private static Runnable run;
- private static MusicPlayer musicPlayer = null;
-
private View rootView;
- private BroadcastReceiver broadCastReveiver = new BroadcastReceiver() {
+
+ /** Messenger for communicating with service. */
+ Messenger mService = null;
+ /** Flag indicating whether we have called bind on the service. */
+ boolean mIsBound;
+
+ /**
+ * Handler of incoming messages from service.
+ */
+ class IncomingHandler extends Handler {
@Override
- public void onReceive(Context context, Intent intent) {
- setSong((Song) intent
- .getSerializableExtra(MusicPlayer.CURRENT_SONG));
+ public void handleMessage(Message msg) {
+ switch (msg.what) {
+ case MusicPlayer.MSG_SONG_CHANGED:
+ setSong((Song) msg.getData().getSerializable(
+ MusicPlayer.SONG_PAYLOAD));
+ break;
+ default:
+ super.handleMessage(msg);
+ }
+ }
+ }
+
+ /**
+ * Target we publish for clients to send messages to IncomingHandler.
+ */
+ final Messenger mMessenger = new Messenger(new IncomingHandler());
+
+ /**
+ * Class for interacting with the main interface of the service.
+ */
+ private ServiceConnection mConnection = new ServiceConnection() {
+ public void onServiceConnected(ComponentName className, IBinder service) {
+ // This is called when the connection with the service has been
+ // established, giving us the service object we can use to
+ // interact with the service. We are communicating with our
+ // service through an IDL interface, so get a client-side
+ // representation of that from the raw service object.
+ mService = new Messenger(service);
+
+ // We want to monitor the service for as long as we are
+ // connected to it.
+ try {
+ Message msg = Message.obtain(null,
+ MusicPlayer.MSG_REGISTER_CLIENT);
+ msg.replyTo = mMessenger;
+ mService.send(msg);
+
+ } catch (RemoteException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void onServiceDisconnected(ComponentName className) {
+ // This is called when the connection with the service has been
+ // unexpectedly disconnected -- that is, its process crashed.
+ mService = null;
}
};
+ void doBindService() {
+ // Establish a connection with the service. We use an explicit
+ // class name because there is no reason to be able to let other
+ // applications replace our component.
+ getActivity().getApplicationContext().bindService(
+ new Intent(getActivity().getApplicationContext(),
+ MusicPlayer.class), mConnection,
+ Context.BIND_AUTO_CREATE);
+ mIsBound = true;
+ }
+
+ void doUnbindService() {
+ if (mIsBound) {
+ // If we have received the service, and hence registered with
+ // it, then now is the time to unregister.
+ if (mService != null) {
+ try {
+ Message msg = Message.obtain(null,
+ MusicPlayer.MSG_UNREGISTER_CLIENT);
+ msg.replyTo = mMessenger;
+ mService.send(msg);
+ } catch (RemoteException e) {
+ // There is nothing special we need to do if the service
+ // has crashed.
+ }
+ }
+
+ // Detach our existing connection.
+ getActivity().getApplicationContext().unbindService(mConnection);
+ mIsBound = false;
+ }
+ }
+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.rootView = inflater.inflate(R.layout.now_playing_fragment,
@@ -56,20 +144,10 @@ public class NowPlayingFragment extends Fragment implements FragmentInterface {
@Override
public void onDestroy() {
- LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).unregisterReceiver(broadCastReveiver);
super.onDestroy();
}
public void createView() {
- // receiver for when song changes
- LocalBroadcastManager
- .getInstance(getActivity().getApplicationContext())
- .registerReceiver(broadCastReveiver,
- new IntentFilter(MusicPlayer.SONG_CHANGED));
-
- musicPlayer = MusicPlayer.getInstance(getActivity()
- .getApplicationContext());
-
constructControls();
registerListeners();
@@ -88,14 +166,14 @@ public class NowPlayingFragment extends Fragment implements FragmentInterface {
currentArtist.setText("{" + song.getTag().artist + "}");
currentGenre.setText("[[" + song.getTag().genre + "]]");
- seekBar.setProgress(musicPlayer.getMediaPlayer().getCurrentPosition());
- seekBar.setMax(musicPlayer.getMediaPlayer().getDuration());
+// seekBar.setProgress(musicPlayer.getMediaPlayer().getCurrentPosition());
+// seekBar.setMax(musicPlayer.getMediaPlayer().getDuration());
- if (!musicPlayer.isPlaying()) {
- play.setImageResource(R.drawable.ic_action_play);
- } else {
+// if (!musicPlayer.isPlaying()) {
+// play.setImageResource(R.drawable.ic_action_play);
+// } else {
play.setImageResource(R.drawable.ic_action_pause);
- }
+// }
}
private void constructControls() {
@@ -116,8 +194,7 @@ public class NowPlayingFragment extends Fragment implements FragmentInterface {
@Override
public void onClick(View view) {
try {
- musicPlayer.playSong(musicPlayer.getNext(),
- musicPlayer.isPlaying());
+ mService.send(Message.obtain(null, MusicPlayer.MSG_NEXT));
} catch (Exception e) {
e.printStackTrace();
}
@@ -128,9 +205,8 @@ public class NowPlayingFragment extends Fragment implements FragmentInterface {
@Override
public void onClick(View view) {
- Song prevSong = musicPlayer.getPrev();
try {
- musicPlayer.playSong(prevSong, musicPlayer.isPlaying());
+ mService.send(Message.obtain(null, MusicPlayer.MSG_PREV));
} catch (Exception e) {
e.printStackTrace();
}
@@ -141,12 +217,17 @@ public class NowPlayingFragment extends Fragment implements FragmentInterface {
@Override
public void onClick(View v) {
- if (musicPlayer.isPlaying()) {
- musicPlayer.pausePlayback();
- play.setImageResource(R.drawable.ic_action_play);
- } else {
- musicPlayer.startPlayback();
- play.setImageResource(R.drawable.ic_action_pause);
+// if (musicPlayer.isPlaying()) {
+// musicPlayer.pausePlayback();
+// play.setImageResource(R.drawable.ic_action_play);
+// } else {
+// musicPlayer.startPlayback();
+// play.setImageResource(R.drawable.ic_action_pause);
+// }
+ try {
+ mService.send(Message.obtain(null, MusicPlayer.MSG_PLAY));
+ } catch (RemoteException e) {
+ e.printStackTrace();
}
}
});
@@ -155,7 +236,7 @@ public class NowPlayingFragment extends Fragment implements FragmentInterface {
@Override
public void onStopTrackingTouch(SeekBar sb) {
- musicPlayer.seek(sb.getProgress());
+// musicPlayer.seek(sb.getProgress());
}
@Override
@@ -166,14 +247,14 @@ public class NowPlayingFragment extends Fragment implements FragmentInterface {
public void onProgressChanged(SeekBar sb, int progress,
boolean fromUser) {
if (fromUser) {
- musicPlayer.seek(sb.getProgress());
+// musicPlayer.seek(sb.getProgress());
}
}
});
}
private void seekUpdation() {
- seekBar.setProgress(musicPlayer.getMediaPlayer().getCurrentPosition());
+// seekBar.setProgress(musicPlayer.getMediaPlayer().getCurrentPosition());
seekHandler.postDelayed(run, 500);
}