Skip to content

Commit

Permalink
android 8+ fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LikhanovD committed Aug 11, 2019
1 parent df915ab commit 3404acd
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 22 deletions.
7 changes: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" package="ru.vvdev.radiolibrary">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
79 changes: 79 additions & 0 deletions app/src/main/java/ru/vvdev/radiolibrary/TestService.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,93 @@
package ru.vvdev.radiolibrary

import android.os.Handler
import android.os.Looper
import com.google.gson.Gson
import com.likhanov.radioplayer.model.NotificationData
import com.likhanov.radioplayer.radio.RadioService
import io.socket.client.IO
import io.socket.client.Socket
import org.json.JSONArray
import org.json.JSONObject
import java.io.Serializable

class TestService : RadioService() {

private var socket: Socket? = null
private var gson = Gson()
private val mainLooperHandler = Handler(Looper.getMainLooper())

override fun onCreate() {
super.onCreate()
init(this, TestService::class.java)
updateUrl("http://icecast-studio21.cdnvideo.ru/S21_1")
setSessionActivity(MainActivity::class.java)
setActivityForNotificationIntent(MainActivity::class.java)
initSockets()
}

override fun onDestroy() {
mainLooperHandler.removeCallbacksAndMessages(null)
socket?.disconnect()
super.onDestroy()
}

private fun initSockets() {
if (socket == null) {
socket = IO.socket(SocketEvent.SOCKET_URL)
socket?.on(SocketEvent.PLAYLIST) {
val obj = it[0] as JSONArray
if (obj.length() > 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<Artist>
) : 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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -114,16 +113,13 @@ open class RadioService : MediaBrowserServiceCompat(), PlaybackManager.PlaybackS
} else MediaButtonReceiver.handleIntent(session, startIntent)
}

delayedStopHandler.removeCallbacksAndMessages(null)
delayedStopHandler.sendEmptyMessageDelayed(0, STOP_DELAY.toLong())
return START_STICKY
}

override fun onDestroy() {
super.onDestroy()
unregisterAudioNoisyReceiver()
radioNotificationManager.stopNotification()
delayedStopHandler.removeCallbacksAndMessages(null)
session.release()
disposable.dispose()
}
Expand All @@ -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)
Expand All @@ -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()
}
Expand Down Expand Up @@ -209,14 +208,6 @@ open class RadioService : MediaBrowserServiceCompat(), PlaybackManager.PlaybackS
stopSelf()
}

private class DelayedStopHandler(service: Service) : Handler() {
private val weakReference: WeakReference<Service> = WeakReference<Service>(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) {
Expand Down

0 comments on commit 3404acd

Please sign in to comment.