Skip to content

Commit 545c4f0

Browse files
committed
PlayerUIList: restrict superclasses a little
1 parent f41b34c commit 545c4f0

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,15 +492,15 @@ private void initUIsForCurrentPlayerType() {
492492

493493
switch (playerType) {
494494
case MAIN:
495-
UIs.destroyAll(PopupPlayerUi.class);
495+
UIs.destroyAllOfType(PopupPlayerUi.class);
496496
UIs.addAndPrepare(new MainPlayerUi(this, binding));
497497
break;
498498
case POPUP:
499-
UIs.destroyAll(MainPlayerUi.class);
499+
UIs.destroyAllOfType(MainPlayerUi.class);
500500
UIs.addAndPrepare(new PopupPlayerUi(this, binding));
501501
break;
502502
case AUDIO:
503-
UIs.destroyAll(VideoPlayerUi.class);
503+
UIs.destroyAllOfType(VideoPlayerUi.class);
504504
break;
505505
}
506506
}
@@ -606,7 +606,7 @@ public void destroy() {
606606
databaseUpdateDisposable.clear();
607607
progressUpdateDisposable.set(null);
608608

609-
UIs.destroyAll(Object.class); // destroy every UI: obviously every UI extends Object
609+
UIs.destroyAllOfType(null);
610610
}
611611

612612
public void setRecovery() {
@@ -1995,6 +1995,10 @@ public void setFragmentListener(final PlayerServiceEventListener listener) {
19951995
triggerProgressUpdate();
19961996
}
19971997

1998+
/**
1999+
* Remove the listener, if it was set.
2000+
* @param listener listener to remove
2001+
* */
19982002
public void removeFragmentListener(final PlayerServiceEventListener listener) {
19992003
if (fragmentListener == listener) {
20002004
fragmentListener = null;
@@ -2009,6 +2013,10 @@ void setActivityListener(final PlayerEventListener listener) {
20092013
triggerProgressUpdate();
20102014
}
20112015

2016+
/**
2017+
* Remove the listener, if it was set.
2018+
* @param listener listener to remove
2019+
* */
20122020
void removeActivityListener(final PlayerEventListener listener) {
20132021
if (activityListener == listener) {
20142022
activityListener = null;

app/src/main/java/org/schabi/newpipe/player/ui/PlayerUiList.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ class PlayerUiList(vararg initialPlayerUis: PlayerUi) {
5050

5151
/**
5252
* Destroys all matching player UIs and removes them from the list.
53-
* @param playerUiType the class of the player UI to destroy;
54-
* the [Class.isInstance] method will be used, so even subclasses will be
53+
* @param playerUiType the class of the player UI to destroy, everything if `null`.
54+
* The [Class.isInstance] method will be used, so even subclasses will be
5555
* destroyed and removed
5656
* @param T the class type parameter </T>
5757
* */
58-
fun <T> destroyAll(playerUiType: Class<T?>) {
58+
fun <T : PlayerUi> destroyAllOfType(playerUiType: Class<T>? = null) {
5959
val toDestroy = mutableListOf<PlayerUi>()
6060

6161
// short blocking removal from class to prevent interfering from other threads
6262
playerUis.runWithLockSync {
6363
val new = mutableListOf<PlayerUi>()
6464
for (ui in lockData) {
65-
if (playerUiType.isInstance(ui)) {
65+
if (playerUiType == null || playerUiType.isInstance(ui)) {
6666
toDestroy.add(ui)
6767
} else {
6868
new.add(ui)
@@ -83,7 +83,7 @@ class PlayerUiList(vararg initialPlayerUis: PlayerUi) {
8383
* @param T the class type parameter
8484
* @return the first player UI of the required type found in the list, or null
8585
</T> */
86-
fun <T> get(playerUiType: Class<T>): T? =
86+
fun <T : PlayerUi> get(playerUiType: Class<T>): T? =
8787
playerUis.runWithLockSync {
8888
for (ui in lockData) {
8989
if (playerUiType.isInstance(ui)) {
@@ -105,7 +105,7 @@ class PlayerUiList(vararg initialPlayerUis: PlayerUi) {
105105
* [Optional] otherwise
106106
</T> */
107107
@Deprecated("use get", ReplaceWith("get(playerUiType)"))
108-
fun <T> getOpt(playerUiType: Class<T>): Optional<T & Any> =
108+
fun <T : PlayerUi> getOpt(playerUiType: Class<T>): Optional<T> =
109109
Optional.ofNullable(get(playerUiType))
110110

111111
/**

0 commit comments

Comments
 (0)