@@ -8,7 +8,6 @@ import android.os.Message
8
8
import android.os.RemoteException
9
9
import android.support.v4.media.MediaBrowserCompat.MediaItem
10
10
import android.support.v4.media.MediaBrowserServiceCompat
11
- import android.support.v4.media.MediaMetadataCompat
12
11
import android.support.v4.media.session.MediaButtonReceiver
13
12
import android.support.v4.media.session.MediaSessionCompat
14
13
import android.support.v4.media.session.PlaybackStateCompat
@@ -31,41 +30,27 @@ class MusicService : MediaBrowserServiceCompat(),
31
30
private var mMediaSession: MediaSessionCompat ? = null
32
31
private var mStateBuilder: PlaybackStateCompat .Builder ? = null
33
32
private lateinit var musicProvider: MusicProvider
34
- private lateinit var queueManager: QueueManager
35
33
private lateinit var playbackManager: PlaybackManager
36
34
private lateinit var playback: Playback
37
35
private var mMediaNotificationManager: MediaNotificationManager ? = null
38
36
private val mDelayedStopHandler = DelayedStopHandler (this )
37
+ private lateinit var mChainManager: ChainManager
39
38
40
39
override fun onCreate () {
41
40
super .onCreate()
41
+ try {
42
+ mMediaNotificationManager = MediaNotificationManager (this )
43
+ } catch (e: RemoteException ) {
44
+ throw IllegalStateException (" Could not create a MediaNotificationManager" , e)
45
+ }
42
46
musicProvider = MusicProvider (ObjectBoxMusicSource (application))
43
- musicProvider.retrieveMusic()
44
-
45
- queueManager = QueueManager (musicProvider, object : QueueManager .MetadataUpdateListener {
46
- override fun onMetadataChanged (metadata : MediaMetadataCompat ) {
47
- mMediaSession?.setMetadata(metadata)
48
- }
49
-
50
- override fun onMetadataRetrieveError () {
51
- // playbackManager.updatePlaybackState(
52
- // getString(R.string.error_no_metadata))
53
- }
54
-
55
- override fun onCurrentQueueIndexUpdated (queueIndex : Int ) {
56
- playbackManager.handlePlayRequest()
57
- }
58
-
59
- override fun onQueueUpdated (title : String ,
60
- newQueue : List <MediaSessionCompat .QueueItem >) {
61
- mMediaSession?.setQueue(newQueue)
62
- }
63
- })
64
-
47
+ mChainManager = ChainManagerImpl (musicProvider, application as App )
65
48
playback = LocalPlayback (applicationContext, musicProvider)
66
- playbackManager = PlaybackManager (LogManager (application), this , resources,
67
- musicProvider, queueManager, playback)
49
+ playbackManager = PlaybackManager (playback, mChainManager, this )
50
+ initMediaSession()
51
+ }
68
52
53
+ private fun initMediaSession () {
69
54
// Create a MediaSessionCompat
70
55
mMediaSession = MediaSessionCompat (applicationContext, LOG_TAG )
71
56
@@ -85,15 +70,9 @@ class MusicService : MediaBrowserServiceCompat(),
85
70
86
71
// MySessionCallback() has methods that handle callbacks from a media controller
87
72
mMediaSession!! .setCallback(playbackManager.mediaSessionCallback)
88
-
89
- try {
90
- mMediaNotificationManager = MediaNotificationManager (this )
91
- } catch (e: RemoteException ) {
92
- throw IllegalStateException (" Could not create a MediaNotificationManager" , e)
93
- }
94
-
95
73
}
96
74
75
+
97
76
override fun onGetRoot (clientPackageName : String , clientUid : Int , rootHints : Bundle ? )
98
77
: MediaBrowserServiceCompat .BrowserRoot ? {
99
78
// (Optional) Control the level of access for the specified package name.
@@ -110,6 +89,7 @@ class MusicService : MediaBrowserServiceCompat(),
110
89
}
111
90
112
91
private fun allowBrowsing (clientPackageName : String , clientUid : Int ): Boolean {
92
+ wtf { " client package name: $clientPackageName , client Uid: $clientUid " }
113
93
return true
114
94
}
115
95
@@ -143,17 +123,6 @@ class MusicService : MediaBrowserServiceCompat(),
143
123
}
144
124
145
125
146
- override fun onPlaybackStart () {
147
-
148
- mMediaSession?.isActive = true
149
- mDelayedStopHandler.removeCallbacksAndMessages(null )
150
-
151
- // The service needs to continue running even after the bound client (usually a
152
- // MediaController) disconnects, otherwise the music playback will stop.
153
- // Calling startService(Intent) will keep the service running until it is explicitly killed.
154
- startService(Intent (applicationContext, MusicService ::class .java))
155
- }
156
-
157
126
override fun onDestroy () {
158
127
super .onDestroy()
159
128
wtf { " onDestroy" }
@@ -174,6 +143,16 @@ class MusicService : MediaBrowserServiceCompat(),
174
143
.subscribe()
175
144
}
176
145
146
+ override fun onPlaybackStart () {
147
+
148
+ mMediaSession?.isActive = true
149
+ mDelayedStopHandler.removeCallbacksAndMessages(null )
150
+
151
+ // The service needs to continue running even after the bound client (usually a
152
+ // MediaController) disconnects, otherwise the music playback will stop.
153
+ // Calling startService(Intent) will keep the service running until it is explicitly killed.
154
+ startService(Intent (applicationContext, MusicService ::class .java))
155
+ }
177
156
178
157
override fun onPlaybackStop () {
179
158
mMediaSession?.isActive = false
@@ -216,7 +195,7 @@ class MusicService : MediaBrowserServiceCompat(),
216
195
private val LOG_TAG = " NextMusicService"
217
196
// The action of the incoming Intent indicating that it contains a command
218
197
// to be executed (see {@link #onStartCommand})
219
- val ACTION_CMD = " com.example.android.uamp .ACTION_CMD"
198
+ val ACTION_CMD = " com.junnanhao.next .ACTION_CMD"
220
199
// The key in the extras of the incoming Intent indicating the command that
221
200
// should be executed (see {@link #onStartCommand})
222
201
val CMD_NAME = " CMD_NAME"
@@ -227,9 +206,8 @@ class MusicService : MediaBrowserServiceCompat(),
227
206
// to local playback from cast playback.
228
207
val CMD_STOP_CASTING = " CMD_STOP_CASTING"
229
208
230
-
231
209
// Extra on MediaSession that contains the Cast device name currently connected to
232
- val EXTRA_CONNECTED_CAST = " com.example.android.uamp .CAST_NAME"
210
+ val EXTRA_CONNECTED_CAST = " com.junnanhao.next .CAST_NAME"
233
211
234
212
// Delay stopSelf by using a handler.
235
213
private val STOP_DELAY = 30000
0 commit comments