Skip to content

Commit 86b27cf

Browse files
ProfpatschStypox
authored andcommitted
PlayerHolder: kotlinify optional calls
1 parent 4fd3ddf commit 86b27cf

1 file changed

Lines changed: 22 additions & 53 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/helper/PlayerHolder.kt

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ import org.schabi.newpipe.player.event.PlayerServiceEventListener
2020
import org.schabi.newpipe.player.event.PlayerServiceExtendedEventListener
2121
import org.schabi.newpipe.player.playqueue.PlayQueue
2222
import org.schabi.newpipe.util.NavigationHelper
23-
import java.util.Optional
2423
import java.util.function.Consumer
25-
import java.util.function.Function
2624

2725
class PlayerHolder private constructor() {
2826
private var listener: PlayerServiceExtendedEventListener? = null
@@ -120,9 +118,7 @@ class PlayerHolder private constructor() {
120118
if (DEBUG) {
121119
Log.d(TAG, "stopService() called")
122120
}
123-
if (playerService != null) {
124-
playerService!!.destroyPlayerAndStopService()
125-
}
121+
playerService?.destroyPlayerAndStopService()
126122
val context = this.commonContext
127123
unbind(context)
128124
// destroyPlayerAndStopService() already runs the next line of code, but run it again just
@@ -218,26 +214,20 @@ class PlayerHolder private constructor() {
218214
this.isBound = false
219215
stopPlayerListener()
220216
playerService = null
221-
if (listener != null) {
222-
listener!!.onPlayerDisconnected()
223-
listener!!.onServiceDisconnected()
224-
}
217+
listener?.onPlayerDisconnected()
218+
listener?.onServiceDisconnected()
225219
}
226220
}
227221

228222
private fun startPlayerListener() {
229-
if (playerService != null) {
230-
// setting the player listener will take care of calling relevant callbacks if the
231-
// player in the service is (not) already active, also see playerStateListener below
232-
playerService!!.setPlayerListener(playerStateListener)
233-
}
223+
// setting the player listener will take care of calling relevant callbacks if the
224+
// player in the service is (not) already active, also see playerStateListener below
225+
playerService?.setPlayerListener(playerStateListener)
234226
this.player?.setFragmentListener(internalListener)
235227
}
236228

237229
private fun stopPlayerListener() {
238-
if (playerService != null) {
239-
playerService!!.setPlayerListener(null)
240-
}
230+
playerService?.setPlayerListener(null)
241231
this.player?.removeFragmentListener(internalListener)
242232
}
243233

@@ -246,48 +236,34 @@ class PlayerHolder private constructor() {
246236
*/
247237
private val internalListener: PlayerServiceEventListener = object : PlayerServiceEventListener {
248238
override fun onViewCreated() {
249-
if (listener != null) {
250-
listener!!.onViewCreated()
251-
}
239+
listener?.onViewCreated()
252240
}
253241

254242
override fun onFullscreenStateChanged(fullscreen: Boolean) {
255-
if (listener != null) {
256-
listener!!.onFullscreenStateChanged(fullscreen)
257-
}
243+
listener?.onFullscreenStateChanged(fullscreen)
258244
}
259245

260246
override fun onScreenRotationButtonClicked() {
261-
if (listener != null) {
262-
listener!!.onScreenRotationButtonClicked()
263-
}
247+
listener?.onScreenRotationButtonClicked()
264248
}
265249

266250
override fun onMoreOptionsLongClicked() {
267-
if (listener != null) {
268-
listener!!.onMoreOptionsLongClicked()
269-
}
251+
listener?.onMoreOptionsLongClicked()
270252
}
271253

272254
override fun onPlayerError(
273255
error: PlaybackException?,
274256
isCatchableException: Boolean
275257
) {
276-
if (listener != null) {
277-
listener!!.onPlayerError(error, isCatchableException)
278-
}
258+
listener?.onPlayerError(error, isCatchableException)
279259
}
280260

281261
override fun hideSystemUiIfNeeded() {
282-
if (listener != null) {
283-
listener!!.hideSystemUiIfNeeded()
284-
}
262+
listener?.hideSystemUiIfNeeded()
285263
}
286264

287265
override fun onQueueUpdate(queue: PlayQueue?) {
288-
if (listener != null) {
289-
listener!!.onQueueUpdate(queue)
290-
}
266+
listener?.onQueueUpdate(queue)
291267
}
292268

293269
override fun onPlaybackUpdate(
@@ -296,31 +272,23 @@ class PlayerHolder private constructor() {
296272
shuffled: Boolean,
297273
parameters: PlaybackParameters?
298274
) {
299-
if (listener != null) {
300-
listener!!.onPlaybackUpdate(state, repeatMode, shuffled, parameters)
301-
}
275+
listener?.onPlaybackUpdate(state, repeatMode, shuffled, parameters)
302276
}
303277

304278
override fun onProgressUpdate(
305279
currentProgress: Int,
306280
duration: Int,
307281
bufferPercent: Int
308282
) {
309-
if (listener != null) {
310-
listener!!.onProgressUpdate(currentProgress, duration, bufferPercent)
311-
}
283+
listener?.onProgressUpdate(currentProgress, duration, bufferPercent)
312284
}
313285

314286
override fun onMetadataUpdate(info: StreamInfo?, queue: PlayQueue?) {
315-
if (listener != null) {
316-
listener!!.onMetadataUpdate(info, queue)
317-
}
287+
listener?.onMetadataUpdate(info, queue)
318288
}
319289

320290
override fun onServiceStopped() {
321-
if (listener != null) {
322-
listener!!.onServiceStopped()
323-
}
291+
listener?.onServiceStopped()
324292
unbind(this@PlayerHolder.commonContext)
325293
}
326294
}
@@ -331,14 +299,15 @@ class PlayerHolder private constructor() {
331299
* Auto media browser queries.
332300
*/
333301
private val playerStateListener = Consumer { player: Player? ->
334-
if (listener != null) {
302+
val l = listener
303+
if (l != null) {
335304
if (player == null) {
336305
// player.fragmentListener=null is already done by player.stopActivityBinding(),
337306
// which is called by player.destroy(), which is in turn called by PlayerService
338307
// before setting its player to null
339-
listener!!.onPlayerDisconnected()
308+
l.onPlayerDisconnected()
340309
} else {
341-
listener!!.onPlayerConnected(player, serviceConnection.playAfterConnect)
310+
l.onPlayerConnected(player, serviceConnection.playAfterConnect)
342311
// reset the value of playAfterConnect: if it was true before, it is now "consumed"
343312
serviceConnection.playAfterConnect = false;
344313
player.setFragmentListener(internalListener)

0 commit comments

Comments
 (0)