diff --git a/app/build.gradle b/app/build.gradle index c3f704e..4f4604e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -48,4 +48,11 @@ dependencies { implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' implementation 'io.reactivex.rxjava2:rxjava:2.2.8' compile project(path: ':radioplayer') + + //SOCKET IO + implementation('io.socket:socket.io-client:1.0.0') { + exclude group: 'org.json', module: 'json' + } + + implementation "com.google.code.gson:gson:2.8.0" } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f665adb..caa113a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + 0) { + val song = gson.fromJson(obj[0].toString(), Song::class.java) + song?.let { + mainLooperHandler.post { + updateNotification( + NotificationData( + it.artist, it.song, it.info?.image ?: "" + ) + ) + } + } + } + }?.on(SocketEvent.SONG_BEGIN) { + val obj = it[0] as JSONObject + val song = gson.fromJson(obj.toString(), Song::class.java) + song?.let { + mainLooperHandler.post { + updateNotification( + NotificationData( + it.artist, it.song, it.info?.image ?: "" + ) + ) + } + } + } + + socket?.let { if (!it.connected()) it.connect() } + } } +} + +data class Song( + val song: String, val artist: String, val info: Info?, + val startTime: String, val startTs: Long +) : Serializable + +data class Info( + val song: String, + val artistName: String, + val image: String, + val audio: String, + val artists: MutableList +) : Serializable + +data class Artist(val id: String, val name: String) : Serializable + +object SocketEvent { + val SOCKET_URL = "https://mplb.emg.fm/nr" + + val PLAYLIST = "playlist" + val SONG_BEGIN = "song began" + val SONG_END = "song ended" + val GET_PLAYLIST = "get playlist" } \ No newline at end of file diff --git a/radioplayer/src/main/java/com/likhanov/radioplayer/radio/RadioNotificationManager.kt b/radioplayer/src/main/java/com/likhanov/radioplayer/radio/RadioNotificationManager.kt index eaf09ba..1830a8a 100644 --- a/radioplayer/src/main/java/com/likhanov/radioplayer/radio/RadioNotificationManager.kt +++ b/radioplayer/src/main/java/com/likhanov/radioplayer/radio/RadioNotificationManager.kt @@ -13,7 +13,6 @@ import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.drawable.Drawable import android.os.Build -import android.os.Handler import android.os.RemoteException import android.support.annotation.RequiresApi import android.support.v4.app.NotificationCompat @@ -109,7 +108,11 @@ class RadioNotificationManager(val service: MediaBrowserServiceCompat, val conte filter.addAction(ACTION_PAUSE) filter.addAction(ACTION_PLAY) filter.addAction(ACTION_STOP) - service.registerReceiver(this, filter) + try { + service.registerReceiver(this, filter) + } catch (e: Exception) { + + } Log.d("NotifTag", "startForeground") service.startForeground(NOTIFICATION_ID, notification) diff --git a/radioplayer/src/main/java/com/likhanov/radioplayer/radio/RadioService.kt b/radioplayer/src/main/java/com/likhanov/radioplayer/radio/RadioService.kt index 60084d7..98f30c8 100644 --- a/radioplayer/src/main/java/com/likhanov/radioplayer/radio/RadioService.kt +++ b/radioplayer/src/main/java/com/likhanov/radioplayer/radio/RadioService.kt @@ -13,6 +13,7 @@ import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Message +import android.support.v4.content.ContextCompat import android.support.v4.media.MediaBrowserCompat import android.support.v4.media.MediaBrowserServiceCompat import android.support.v4.media.MediaMetadataCompat @@ -45,7 +46,6 @@ open class RadioService : MediaBrowserServiceCompat(), PlaybackManager.PlaybackS private lateinit var radioNotificationManager: RadioNotificationManager private lateinit var mediaRouter: MediaRouter private val radioStateController = RadioStateController() - private lateinit var delayedStopHandler: DelayedStopHandler private lateinit var audioManager: AudioManager private var focusRequest: AudioFocusRequest? = null private val myNoisyAudioStreamReceiver = NoisyAudioStreamReceiver() @@ -76,7 +76,6 @@ open class RadioService : MediaBrowserServiceCompat(), PlaybackManager.PlaybackS this.service = service this.serviceClass = serviceClass - delayedStopHandler = DelayedStopHandler(service) playback = RadioPlayback("") playbackManager = PlaybackManager(playback, this) @@ -114,8 +113,6 @@ open class RadioService : MediaBrowserServiceCompat(), PlaybackManager.PlaybackS } else MediaButtonReceiver.handleIntent(session, startIntent) } - delayedStopHandler.removeCallbacksAndMessages(null) - delayedStopHandler.sendEmptyMessageDelayed(0, STOP_DELAY.toLong()) return START_STICKY } @@ -123,7 +120,6 @@ open class RadioService : MediaBrowserServiceCompat(), PlaybackManager.PlaybackS super.onDestroy() unregisterAudioNoisyReceiver() radioNotificationManager.stopNotification() - delayedStopHandler.removeCallbacksAndMessages(null) session.release() disposable.dispose() } @@ -145,7 +141,7 @@ open class RadioService : MediaBrowserServiceCompat(), PlaybackManager.PlaybackS } override fun updateNotification(data: NotificationData?) { - Log.d(TAG, "updateNotification") + Log.d(TAG, "updateNotification, $needUpdateNotification") data?.let { lastData = data if (needUpdateNotification) radioNotificationManager.updateNotification(data, null) @@ -161,27 +157,30 @@ open class RadioService : MediaBrowserServiceCompat(), PlaybackManager.PlaybackS radioNotificationManager.setNotificationDrawable(drawableRes) override fun onPlaybackStart() { + Log.d(TAG, "onPlaybackStart") session.isActive = true - delayedStopHandler.removeCallbacksAndMessages(null) - serviceClass?.let { startService(Intent(applicationContext, it)) } + serviceClass?.let { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + ContextCompat.startForegroundService(applicationContext, Intent(applicationContext, it)) + } else startService(Intent(applicationContext, it)) + } } override fun onNotificationRequired() { + Log.d(TAG, "onNotificationRequired") needUpdateNotification = true radioNotificationManager.startNotification() + radioNotificationManager.updateNotification(lastData, null) } override fun onPlaybackStop() { Log.d(TAG, "onPlaybackStop") session.isActive = false - delayedStopHandler.removeCallbacksAndMessages(null) - delayedStopHandler.sendEmptyMessageDelayed(0, STOP_DELAY.toLong()) - stopForeground(false) radioNotificationManager.started = false - if(needUpdateNotification) radioNotificationManager.updateNotification(lastData, true) + if (needUpdateNotification) radioNotificationManager.updateNotification(lastData, true) needUpdateNotification = false unregisterAudioNoisyReceiver() } @@ -209,14 +208,6 @@ open class RadioService : MediaBrowserServiceCompat(), PlaybackManager.PlaybackS stopSelf() } - private class DelayedStopHandler(service: Service) : Handler() { - private val weakReference: WeakReference = WeakReference(service) - - override fun handleMessage(msg: Message) { - } - } - - private fun initAudioManager() { audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {