1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-06-27 07:32:54 +00:00

PlayerUIList: restrict superclasses a little

This commit is contained in:
Profpatsch 2025-05-13 15:12:47 +02:00
parent a8da9946d1
commit 731efc2124
2 changed files with 18 additions and 10 deletions

View File

@ -492,15 +492,15 @@ public final class Player implements PlaybackListener, Listener {
switch (playerType) { switch (playerType) {
case MAIN: case MAIN:
UIs.destroyAll(PopupPlayerUi.class); UIs.destroyAllOfType(PopupPlayerUi.class);
UIs.addAndPrepare(new MainPlayerUi(this, binding)); UIs.addAndPrepare(new MainPlayerUi(this, binding));
break; break;
case POPUP: case POPUP:
UIs.destroyAll(MainPlayerUi.class); UIs.destroyAllOfType(MainPlayerUi.class);
UIs.addAndPrepare(new PopupPlayerUi(this, binding)); UIs.addAndPrepare(new PopupPlayerUi(this, binding));
break; break;
case AUDIO: case AUDIO:
UIs.destroyAll(VideoPlayerUi.class); UIs.destroyAllOfType(VideoPlayerUi.class);
break; break;
} }
} }
@ -606,7 +606,7 @@ public final class Player implements PlaybackListener, Listener {
databaseUpdateDisposable.clear(); databaseUpdateDisposable.clear();
progressUpdateDisposable.set(null); progressUpdateDisposable.set(null);
UIs.destroyAll(Object.class); // destroy every UI: obviously every UI extends Object UIs.destroyAllOfType(null);
} }
public void setRecovery() { public void setRecovery() {
@ -1995,6 +1995,10 @@ public final class Player implements PlaybackListener, Listener {
triggerProgressUpdate(); triggerProgressUpdate();
} }
/**
* Remove the listener, if it was set.
* @param listener listener to remove
* */
public void removeFragmentListener(final PlayerServiceEventListener listener) { public void removeFragmentListener(final PlayerServiceEventListener listener) {
if (fragmentListener == listener) { if (fragmentListener == listener) {
fragmentListener = null; fragmentListener = null;
@ -2009,6 +2013,10 @@ public final class Player implements PlaybackListener, Listener {
triggerProgressUpdate(); triggerProgressUpdate();
} }
/**
* Remove the listener, if it was set.
* @param listener listener to remove
* */
void removeActivityListener(final PlayerEventListener listener) { void removeActivityListener(final PlayerEventListener listener) {
if (activityListener == listener) { if (activityListener == listener) {
activityListener = null; activityListener = null;

View File

@ -50,19 +50,19 @@ class PlayerUiList(vararg initialPlayerUis: PlayerUi) {
/** /**
* Destroys all matching player UIs and removes them from the list. * Destroys all matching player UIs and removes them from the list.
* @param playerUiType the class of the player UI to destroy; * @param playerUiType the class of the player UI to destroy, everything if `null`.
* the [Class.isInstance] method will be used, so even subclasses will be * The [Class.isInstance] method will be used, so even subclasses will be
* destroyed and removed * destroyed and removed
* @param T the class type parameter </T> * @param T the class type parameter </T>
* */ * */
fun <T> destroyAll(playerUiType: Class<T?>) { fun <T : PlayerUi> destroyAllOfType(playerUiType: Class<T>? = null) {
val toDestroy = mutableListOf<PlayerUi>() val toDestroy = mutableListOf<PlayerUi>()
// short blocking removal from class to prevent interfering from other threads // short blocking removal from class to prevent interfering from other threads
playerUis.runWithLockSync { playerUis.runWithLockSync {
val new = mutableListOf<PlayerUi>() val new = mutableListOf<PlayerUi>()
for (ui in lockData) { for (ui in lockData) {
if (playerUiType.isInstance(ui)) { if (playerUiType == null || playerUiType.isInstance(ui)) {
toDestroy.add(ui) toDestroy.add(ui)
} else { } else {
new.add(ui) new.add(ui)
@ -83,7 +83,7 @@ class PlayerUiList(vararg initialPlayerUis: PlayerUi) {
* @param T the class type parameter * @param T the class type parameter
* @return the first player UI of the required type found in the list, or null * @return the first player UI of the required type found in the list, or null
</T> */ </T> */
fun <T> get(playerUiType: Class<T>): T? = fun <T : PlayerUi> get(playerUiType: Class<T>): T? =
playerUis.runWithLockSync { playerUis.runWithLockSync {
for (ui in lockData) { for (ui in lockData) {
if (playerUiType.isInstance(ui)) { if (playerUiType.isInstance(ui)) {
@ -105,7 +105,7 @@ class PlayerUiList(vararg initialPlayerUis: PlayerUi) {
* [Optional] otherwise * [Optional] otherwise
</T> */ </T> */
@Deprecated("use get", ReplaceWith("get(playerUiType)")) @Deprecated("use get", ReplaceWith("get(playerUiType)"))
fun <T> getOpt(playerUiType: Class<T>): Optional<T & Any> = fun <T : PlayerUi> getOpt(playerUiType: Class<T>): Optional<T> =
Optional.ofNullable(get(playerUiType)) Optional.ofNullable(get(playerUiType))
/** /**