Skip to content

Commit b5dd49e

Browse files
committed
PlayerService: simplify nullable calls, getters
1 parent 945fbd8 commit b5dd49e

1 file changed

Lines changed: 15 additions & 20 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/PlayerService.kt

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class PlayerService : MediaBrowserServiceCompat() {
9191

9292
// see https://developer.android.com/training/cars/media#browser_workflow
9393
mediaSession = MediaSessionCompat(this, "MediaSessionPlayerServ")
94-
setSessionToken(mediaSession!!.getSessionToken())
94+
setSessionToken(mediaSession!!.sessionToken)
9595
sessionConnector = MediaSessionConnector(mediaSession!!)
9696
sessionConnector!!.setMetadataDeduplicationEnabled(true)
9797

@@ -127,7 +127,7 @@ class PlayerService : MediaBrowserServiceCompat() {
127127
TAG,
128128
(
129129
"onStartCommand() called with: intent = [" + intent +
130-
"], extras = [" + intent.getExtras().toDebugString() +
130+
"], extras = [" + intent.extras.toDebugString() +
131131
"], flags = [" + flags + "], startId = [" + startId + "]"
132132
)
133133
)
@@ -150,8 +150,8 @@ class PlayerService : MediaBrowserServiceCompat() {
150150
// no one already and starting the service in foreground should not create any issues.
151151
// If the service is already started in foreground, requesting it to be started
152152
// shouldn't do anything.
153-
player!!.UIs().getOpt<NotificationPlayerUi>(NotificationPlayerUi::class.java)
154-
.ifPresent(Consumer { obj: NotificationPlayerUi? -> obj!!.createNotificationAndStartForeground() })
153+
player!!.UIs().get(NotificationPlayerUi::class.java)
154+
?.createNotificationAndStartForeground()
155155

156156
if (playerWasNull && onPlayerStartedOrStopped != null) {
157157
// notify that a new player was created (but do it after creating the foreground
@@ -161,8 +161,8 @@ class PlayerService : MediaBrowserServiceCompat() {
161161
}
162162
}
163163

164-
if (Intent.ACTION_MEDIA_BUTTON == intent.getAction() &&
165-
(player == null || player!!.getPlayQueue() == null)
164+
if (Intent.ACTION_MEDIA_BUTTON == intent.action &&
165+
(player == null || player!!.playQueue == null)
166166
) {
167167
/*
168168
No need to process media button's actions if the player is not working, otherwise
@@ -174,16 +174,11 @@ class PlayerService : MediaBrowserServiceCompat() {
174174
return START_NOT_STICKY
175175
}
176176

177-
if (player != null) {
178-
player!!.handleIntent(intent)
179-
player!!.UIs().getOpt<MediaSessionPlayerUi>(MediaSessionPlayerUi::class.java)
180-
.ifPresent(
181-
Consumer { ui: MediaSessionPlayerUi? ->
182-
ui!!.handleMediaButtonIntent(
183-
intent
184-
)
185-
}
186-
)
177+
val p = player
178+
if (p != null) {
179+
p.handleIntent(intent)
180+
p.UIs().get(MediaSessionPlayerUi::class.java)
181+
?.handleMediaButtonIntent(intent)
187182
}
188183

189184
return START_NOT_STICKY
@@ -278,16 +273,16 @@ class PlayerService : MediaBrowserServiceCompat() {
278273
TAG,
279274
(
280275
"onBind() called with: intent = [" + intent +
281-
"], extras = [" + intent.getExtras().toDebugString() + "]"
276+
"], extras = [" + intent.extras.toDebugString() + "]"
282277
)
283278
)
284279
}
285280

286-
if (BIND_PLAYER_HOLDER_ACTION == intent.getAction()) {
281+
if (BIND_PLAYER_HOLDER_ACTION == intent.action) {
287282
// Note that this binder might be reused multiple times while the service is alive, even
288283
// after unbind() has been called: https://stackoverflow.com/a/8794930 .
289284
return mBinder
290-
} else if (SERVICE_INTERFACE == intent.getAction()) {
285+
} else if (SERVICE_INTERFACE == intent.action) {
291286
// MediaBrowserService also uses its own binder, so for actions related to the media
292287
// browser service, pass the onBind to the superclass.
293288
return super.onBind(intent)
@@ -297,7 +292,7 @@ class PlayerService : MediaBrowserServiceCompat() {
297292
}
298293
}
299294

300-
class LocalBinder internal constructor(playerService: PlayerService?) : Binder() {
295+
class LocalBinder internal constructor(playerService: PlayerService) : Binder() {
301296
private val playerService: WeakReference<PlayerService?>
302297

303298
init {

0 commit comments

Comments
 (0)