Skip to content

Commit 016c20c

Browse files
committed
try to fix foreground mode on android 8+
1 parent 3404acd commit 016c20c

File tree

3 files changed

+157
-33
lines changed

3 files changed

+157
-33
lines changed

.idea/codeStyles/Project.xml

Lines changed: 109 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

radioplayer/src/main/java/com/likhanov/radioplayer/radio/RadioNotificationManager.kt

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import com.likhanov.radioplayer.model.NotificationData
3131
import com.likhanov.radioplayer.util.Store
3232
import java.util.*
3333

34-
class RadioNotificationManager(val service: MediaBrowserServiceCompat, val context: Context?) : BroadcastReceiver() {
34+
class RadioNotificationManager(val service: MediaBrowserServiceCompat, val context: Context?) :
35+
BroadcastReceiver() {
3536

3637
private val TAG = "RadioNotification"
3738

@@ -61,7 +62,8 @@ class RadioNotificationManager(val service: MediaBrowserServiceCompat, val conte
6162
var started = false
6263
private val notificationBuilder = NotificationCompat.Builder(service, CHANNEL_ID)
6364
private var art = BitmapFactory.decodeResource(service.resources, R.drawable.ic_song_image)
64-
private var lastBitmap = BitmapFactory.decodeResource(service.resources, R.drawable.ic_song_image)
65+
private var lastBitmap =
66+
BitmapFactory.decodeResource(service.resources, R.drawable.ic_song_image)
6567
private var activity: Class<*>? = null
6668
private var notificationImage: Int? = null
6769

@@ -97,28 +99,25 @@ class RadioNotificationManager(val service: MediaBrowserServiceCompat, val conte
9799

98100
fun startNotification() {
99101
Log.d("NotifTag", "startNotifi")
100-
if (!started) {
101-
metadata = controller?.metadata
102-
playbackState = controller?.playbackState
103-
104-
// The notification must be updated after setting started to true
105-
val notification = createNotification(lastData, null, false)
106-
if (notification != null) {
107-
val filter = IntentFilter()
108-
filter.addAction(ACTION_PAUSE)
109-
filter.addAction(ACTION_PLAY)
110-
filter.addAction(ACTION_STOP)
111-
try {
112-
service.registerReceiver(this, filter)
113-
} catch (e: Exception) {
114-
115-
}
116-
117-
Log.d("NotifTag", "startForeground")
118-
service.startForeground(NOTIFICATION_ID, notification)
119-
started = true
120-
}
102+
103+
metadata = controller?.metadata
104+
playbackState = controller?.playbackState
105+
106+
// The notification must be updated after setting started to true
107+
val notification = createNotification(lastData, null, false)
108+
val filter = IntentFilter()
109+
filter.addAction(ACTION_PAUSE)
110+
filter.addAction(ACTION_PLAY)
111+
filter.addAction(ACTION_STOP)
112+
try {
113+
service.registerReceiver(this, filter)
114+
} catch (e: Exception) {
115+
121116
}
117+
118+
Log.d("NotifTag", "startForeground")
119+
service.startForeground(NOTIFICATION_ID, notification)
120+
started = true
122121
}
123122

124123
fun stopNotification() {
@@ -152,7 +151,12 @@ class RadioNotificationManager(val service: MediaBrowserServiceCompat, val conte
152151
val openUI = Intent(service, activity)
153152
openUI.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
154153

155-
return PendingIntent.getActivity(service, REQUEST_CODE, openUI, PendingIntent.FLAG_CANCEL_CURRENT)
154+
return PendingIntent.getActivity(
155+
service,
156+
REQUEST_CODE,
157+
openUI,
158+
PendingIntent.FLAG_CANCEL_CURRENT
159+
)
156160
} ?: return null
157161
}
158162

@@ -168,11 +172,11 @@ class RadioNotificationManager(val service: MediaBrowserServiceCompat, val conte
168172
}
169173
}
170174

171-
private fun createNotification(data: NotificationData?, bitmap: Bitmap?, forPause: Boolean?): Notification? {
172-
if (playbackState == null) {
173-
return null
174-
}
175-
175+
private fun createNotification(
176+
data: NotificationData?,
177+
bitmap: Bitmap?,
178+
forPause: Boolean?
179+
): Notification {
176180
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
177181
createNotificationChannel()
178182
}
@@ -199,7 +203,8 @@ class RadioNotificationManager(val service: MediaBrowserServiceCompat, val conte
199203
}
200204

201205
private fun createOnDismissedIntent(): PendingIntent {
202-
val intent = Intent(context, RadioService.Companion.NotificationDismissedReceiver::class.java)
206+
val intent =
207+
Intent(context, RadioService.Companion.NotificationDismissedReceiver::class.java)
203208

204209
val pendingIntent = PendingIntent.getBroadcast(context, NOTIFICATION_ID, intent, 0)
205210
return pendingIntent
@@ -256,12 +261,21 @@ class RadioNotificationManager(val service: MediaBrowserServiceCompat, val conte
256261
override fun onLoadFailed(errorDrawable: Drawable?) {
257262
super.onLoadFailed(errorDrawable)
258263
lastBitmap = null
259-
notificationManager.notify(NOTIFICATION_ID, createNotification(null, null, null))
264+
notificationManager.notify(
265+
NOTIFICATION_ID,
266+
createNotification(null, null, null)
267+
)
260268
}
261269

262-
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
270+
override fun onResourceReady(
271+
resource: Bitmap,
272+
transition: Transition<in Bitmap>?
273+
) {
263274
lastBitmap = resource
264-
notificationManager.notify(NOTIFICATION_ID, createNotification(null, resource, null))
275+
notificationManager.notify(
276+
NOTIFICATION_ID,
277+
createNotification(null, resource, null)
278+
)
265279
}
266280
})
267281
}

radioplayer/src/main/java/com/likhanov/radioplayer/radio/RadioService.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ open class RadioService : MediaBrowserServiceCompat(), PlaybackManager.PlaybackS
163163
serviceClass?.let {
164164
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
165165
ContextCompat.startForegroundService(applicationContext, Intent(applicationContext, it))
166+
radioNotificationManager.startNotification()
166167
} else startService(Intent(applicationContext, it))
167168
}
168169
}

0 commit comments

Comments
 (0)