@@ -31,7 +31,8 @@ import com.likhanov.radioplayer.model.NotificationData
31
31
import com.likhanov.radioplayer.util.Store
32
32
import java.util.*
33
33
34
- class RadioNotificationManager (val service : MediaBrowserServiceCompat , val context : Context ? ) : BroadcastReceiver() {
34
+ class RadioNotificationManager (val service : MediaBrowserServiceCompat , val context : Context ? ) :
35
+ BroadcastReceiver () {
35
36
36
37
private val TAG = " RadioNotification"
37
38
@@ -61,7 +62,8 @@ class RadioNotificationManager(val service: MediaBrowserServiceCompat, val conte
61
62
var started = false
62
63
private val notificationBuilder = NotificationCompat .Builder (service, CHANNEL_ID )
63
64
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)
65
67
private var activity: Class <* >? = null
66
68
private var notificationImage: Int? = null
67
69
@@ -97,28 +99,25 @@ class RadioNotificationManager(val service: MediaBrowserServiceCompat, val conte
97
99
98
100
fun startNotification () {
99
101
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
+
121
116
}
117
+
118
+ Log .d(" NotifTag" , " startForeground" )
119
+ service.startForeground(NOTIFICATION_ID , notification)
120
+ started = true
122
121
}
123
122
124
123
fun stopNotification () {
@@ -152,7 +151,12 @@ class RadioNotificationManager(val service: MediaBrowserServiceCompat, val conte
152
151
val openUI = Intent (service, activity)
153
152
openUI.flags = Intent .FLAG_ACTIVITY_SINGLE_TOP
154
153
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
+ )
156
160
} ? : return null
157
161
}
158
162
@@ -168,11 +172,11 @@ class RadioNotificationManager(val service: MediaBrowserServiceCompat, val conte
168
172
}
169
173
}
170
174
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 {
176
180
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
177
181
createNotificationChannel()
178
182
}
@@ -199,7 +203,8 @@ class RadioNotificationManager(val service: MediaBrowserServiceCompat, val conte
199
203
}
200
204
201
205
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)
203
208
204
209
val pendingIntent = PendingIntent .getBroadcast(context, NOTIFICATION_ID , intent, 0 )
205
210
return pendingIntent
@@ -256,12 +261,21 @@ class RadioNotificationManager(val service: MediaBrowserServiceCompat, val conte
256
261
override fun onLoadFailed (errorDrawable : Drawable ? ) {
257
262
super .onLoadFailed(errorDrawable)
258
263
lastBitmap = null
259
- notificationManager.notify(NOTIFICATION_ID , createNotification(null , null , null ))
264
+ notificationManager.notify(
265
+ NOTIFICATION_ID ,
266
+ createNotification(null , null , null )
267
+ )
260
268
}
261
269
262
- override fun onResourceReady (resource : Bitmap , transition : Transition <in Bitmap >? ) {
270
+ override fun onResourceReady (
271
+ resource : Bitmap ,
272
+ transition : Transition <in Bitmap >?
273
+ ) {
263
274
lastBitmap = resource
264
- notificationManager.notify(NOTIFICATION_ID , createNotification(null , resource, null ))
275
+ notificationManager.notify(
276
+ NOTIFICATION_ID ,
277
+ createNotification(null , resource, null )
278
+ )
265
279
}
266
280
})
267
281
}
0 commit comments