Skip to content

Commit 7c1d33d

Browse files
committed
PlayerService: use lateinit for the player
This removes a bunch of unncessary checks. We forgo setting player to `null` in onDestroy, because shutting it down and when the service stops should be enough to free it for garbage collection.
1 parent 487f08c commit 7c1d33d

2 files changed

Lines changed: 26 additions & 35 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/Player.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,12 @@ private void destroyPlayer() {
580580
}
581581
}
582582

583-
public void destroy() {
583+
584+
/** Shut down this player.
585+
* Saves the stream progress, sets recovery.
586+
* Then destroys the player in all UIs and destroys the UIs as well.
587+
*/
588+
public void saveAndShutdown() {
584589
if (DEBUG) {
585590
Log.d(TAG, "destroy() called");
586591
}

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

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,11 @@ import java.util.function.Consumer
3838
* this allows us to keep playing even when switching between the different UIs.
3939
*/
4040
class PlayerService : Service() {
41-
private var player: Player? = null
41+
lateinit var player: Player
42+
private set
4243

4344
private val mBinder: IBinder = LocalBinder(this)
4445

45-
fun getPlayer(): Player? {
46-
return player
47-
}
48-
4946
/*//////////////////////////////////////////////////////////////////////////
5047
// Service's LifeCycle
5148
////////////////////////////////////////////////////////////////////////// */
@@ -63,7 +60,7 @@ class PlayerService : Service() {
6360
loading stream metadata) takes a lot of time, the app would crash on Android 8+ as the
6461
service would never be put in the foreground while we said to the system we would do so
6562
*/
66-
player!!.UIs().getOpt<NotificationPlayerUi>(NotificationPlayerUi::class.java)
63+
player.UIs().getOpt<NotificationPlayerUi>(NotificationPlayerUi::class.java)
6764
.ifPresent(Consumer { obj: NotificationPlayerUi? -> obj!!.createNotificationAndStartForeground() })
6865
}
6966

@@ -88,13 +85,11 @@ class PlayerService : Service() {
8885
If the service is already started in foreground, requesting it to be started shouldn't
8986
do anything
9087
*/
91-
if (player != null) {
92-
player!!.UIs().getOpt<NotificationPlayerUi>(NotificationPlayerUi::class.java)
93-
.ifPresent(Consumer { obj: NotificationPlayerUi? -> obj!!.createNotificationAndStartForeground() })
94-
}
88+
player.UIs().getOpt<NotificationPlayerUi>(NotificationPlayerUi::class.java)
89+
.ifPresent(Consumer { obj: NotificationPlayerUi? -> obj!!.createNotificationAndStartForeground() })
9590

9691
if (Intent.ACTION_MEDIA_BUTTON == intent.getAction() &&
97-
(player == null || player!!.getPlayQueue() == null)
92+
(player.getPlayQueue() == null)
9893
) {
9994
/*
10095
No need to process media button's actions if the player is not working, otherwise
@@ -106,17 +101,15 @@ class PlayerService : Service() {
106101
return START_NOT_STICKY
107102
}
108103

109-
if (player != null) {
110-
player!!.handleIntent(intent)
111-
player!!.UIs().getOpt<MediaSessionPlayerUi>(MediaSessionPlayerUi::class.java)
112-
.ifPresent(
113-
Consumer { ui: MediaSessionPlayerUi? ->
114-
ui!!.handleMediaButtonIntent(
115-
intent
116-
)
117-
}
118-
)
119-
}
104+
player.handleIntent(intent)
105+
player.UIs().getOpt<MediaSessionPlayerUi>(MediaSessionPlayerUi::class.java)
106+
.ifPresent(
107+
Consumer { ui: MediaSessionPlayerUi? ->
108+
ui!!.handleMediaButtonIntent(
109+
intent
110+
)
111+
}
112+
)
120113

121114
return START_NOT_STICKY
122115
}
@@ -126,17 +119,17 @@ class PlayerService : Service() {
126119
Log.d(TAG, "stopForImmediateReusing() called")
127120
}
128121

129-
if (player != null && !player!!.exoPlayerIsNull()) {
122+
if (!player.exoPlayerIsNull()) {
130123
// Releases wifi & cpu, disables keepScreenOn, etc.
131124
// We can't just pause the player here because it will make transition
132125
// from one stream to a new stream not smooth
133-
player!!.smoothStopForImmediateReusing()
126+
player.smoothStopForImmediateReusing()
134127
}
135128
}
136129

137130
override fun onTaskRemoved(rootIntent: Intent?) {
138131
super.onTaskRemoved(rootIntent)
139-
if (player != null && !player!!.videoPlayerSelected()) {
132+
if (!player.videoPlayerSelected()) {
140133
return
141134
}
142135
onDestroy()
@@ -148,18 +141,11 @@ class PlayerService : Service() {
148141
if (DEBUG) {
149142
Log.d(TAG, "destroy() called")
150143
}
151-
cleanup()
152-
}
153-
154-
private fun cleanup() {
155-
if (player != null) {
156-
player!!.destroy()
157-
player = null
158-
}
144+
player.saveAndShutdown()
159145
}
160146

161147
fun stopService() {
162-
cleanup()
148+
player.saveAndShutdown()
163149
stopSelf()
164150
}
165151

0 commit comments

Comments
 (0)