fix(android): fix bugs related Android PIP listeners (#4441)
* fix(android): fixed bug where listeners were not removed properly * fix(android): fixes a bug that causes PIP to work when player is null * refactor(android): call pipUnsubscriber without handler * chore(android): apply lint
This commit is contained in:
@@ -17,6 +17,7 @@ import androidx.annotation.ChecksSdkIntAtLeast
|
|||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.core.app.AppOpsManagerCompat
|
import androidx.core.app.AppOpsManagerCompat
|
||||||
import androidx.core.app.PictureInPictureModeChangedInfo
|
import androidx.core.app.PictureInPictureModeChangedInfo
|
||||||
|
import androidx.core.util.Consumer
|
||||||
import androidx.lifecycle.Lifecycle
|
import androidx.lifecycle.Lifecycle
|
||||||
import androidx.media3.exoplayer.ExoPlayer
|
import androidx.media3.exoplayer.ExoPlayer
|
||||||
import com.brentvatne.common.toolbox.DebugLog
|
import com.brentvatne.common.toolbox.DebugLog
|
||||||
@@ -40,7 +41,7 @@ object PictureInPictureUtil {
|
|||||||
fun addLifecycleEventListener(context: ThemedReactContext, view: ReactExoplayerView): Runnable {
|
fun addLifecycleEventListener(context: ThemedReactContext, view: ReactExoplayerView): Runnable {
|
||||||
val activity = context.findActivity()
|
val activity = context.findActivity()
|
||||||
|
|
||||||
val onPictureInPictureModeChanged: (info: PictureInPictureModeChangedInfo) -> Unit = { info: PictureInPictureModeChangedInfo ->
|
val onPictureInPictureModeChanged = Consumer<PictureInPictureModeChangedInfo> { info ->
|
||||||
view.setIsInPictureInPicture(info.isInPictureInPictureMode)
|
view.setIsInPictureInPicture(info.isInPictureInPictureMode)
|
||||||
if (!info.isInPictureInPictureMode && activity.lifecycle.currentState == Lifecycle.State.CREATED) {
|
if (!info.isInPictureInPictureMode && activity.lifecycle.currentState == Lifecycle.State.CREATED) {
|
||||||
// when user click close button of PIP
|
// when user click close button of PIP
|
||||||
@@ -48,7 +49,7 @@ object PictureInPictureUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val onUserLeaveHintCallback = {
|
val onUserLeaveHintCallback = Runnable {
|
||||||
if (view.enterPictureInPictureOnLeave) {
|
if (view.enterPictureInPictureOnLeave) {
|
||||||
view.enterPictureInPictureMode()
|
view.enterPictureInPictureMode()
|
||||||
}
|
}
|
||||||
@@ -61,10 +62,10 @@ object PictureInPictureUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// @TODO convert to lambda when ReactExoplayerView migrated
|
// @TODO convert to lambda when ReactExoplayerView migrated
|
||||||
return object : Runnable {
|
return Runnable {
|
||||||
override fun run() {
|
with(activity) {
|
||||||
context.findActivity().removeOnPictureInPictureModeChangedListener(onPictureInPictureModeChanged)
|
removeOnPictureInPictureModeChangedListener(onPictureInPictureModeChanged)
|
||||||
context.findActivity().removeOnUserLeaveHintListener(onUserLeaveHintCallback)
|
removeOnUserLeaveHintListener(onUserLeaveHintCallback)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -332,7 +332,6 @@ public class ReactExoplayerView extends FrameLayout implements
|
|||||||
|
|
||||||
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
||||||
themedReactContext.addLifecycleEventListener(this);
|
themedReactContext.addLifecycleEventListener(this);
|
||||||
pipListenerUnsubscribe = PictureInPictureUtil.addLifecycleEventListener(context, this);
|
|
||||||
audioBecomingNoisyReceiver = new AudioBecomingNoisyReceiver(themedReactContext);
|
audioBecomingNoisyReceiver = new AudioBecomingNoisyReceiver(themedReactContext);
|
||||||
audioFocusChangeListener = new OnAudioFocusChangedListener(this, themedReactContext);
|
audioFocusChangeListener = new OnAudioFocusChangedListener(this, themedReactContext);
|
||||||
pictureInPictureReceiver = new PictureInPictureReceiver(this, themedReactContext);
|
pictureInPictureReceiver = new PictureInPictureReceiver(this, themedReactContext);
|
||||||
@@ -767,6 +766,8 @@ public class ReactExoplayerView extends FrameLayout implements
|
|||||||
if (player == null) {
|
if (player == null) {
|
||||||
// Initialize core configuration and listeners
|
// Initialize core configuration and listeners
|
||||||
initializePlayerCore(self);
|
initializePlayerCore(self);
|
||||||
|
pipListenerUnsubscribe = PictureInPictureUtil.addLifecycleEventListener(themedReactContext, this);
|
||||||
|
PictureInPictureUtil.applyAutoEnterEnabled(themedReactContext, pictureInPictureParamsBuilder, this.enterPictureInPictureOnLeave);
|
||||||
}
|
}
|
||||||
if (!source.isLocalAssetFile() && !source.isAsset() && source.getBufferConfig().getCacheSize() > 0) {
|
if (!source.isLocalAssetFile() && !source.isAsset() && source.getBufferConfig().getCacheSize() > 0) {
|
||||||
RNVSimpleCache.INSTANCE.setSimpleCache(
|
RNVSimpleCache.INSTANCE.setSimpleCache(
|
||||||
@@ -1298,7 +1299,7 @@ public class ReactExoplayerView extends FrameLayout implements
|
|||||||
player.removeListener(this);
|
player.removeListener(this);
|
||||||
PictureInPictureUtil.applyAutoEnterEnabled(themedReactContext, pictureInPictureParamsBuilder, false);
|
PictureInPictureUtil.applyAutoEnterEnabled(themedReactContext, pictureInPictureParamsBuilder, false);
|
||||||
if (pipListenerUnsubscribe != null) {
|
if (pipListenerUnsubscribe != null) {
|
||||||
new Handler().post(pipListenerUnsubscribe);
|
pipListenerUnsubscribe.run();
|
||||||
}
|
}
|
||||||
trackSelector = null;
|
trackSelector = null;
|
||||||
|
|
||||||
@@ -2245,8 +2246,10 @@ public class ReactExoplayerView extends FrameLayout implements
|
|||||||
|
|
||||||
public void setEnterPictureInPictureOnLeave(boolean enterPictureInPictureOnLeave) {
|
public void setEnterPictureInPictureOnLeave(boolean enterPictureInPictureOnLeave) {
|
||||||
this.enterPictureInPictureOnLeave = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && enterPictureInPictureOnLeave;
|
this.enterPictureInPictureOnLeave = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && enterPictureInPictureOnLeave;
|
||||||
|
if (player != null) {
|
||||||
PictureInPictureUtil.applyAutoEnterEnabled(themedReactContext, pictureInPictureParamsBuilder, this.enterPictureInPictureOnLeave);
|
PictureInPictureUtil.applyAutoEnterEnabled(themedReactContext, pictureInPictureParamsBuilder, this.enterPictureInPictureOnLeave);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected void setIsInPictureInPicture(boolean isInPictureInPicture) {
|
protected void setIsInPictureInPicture(boolean isInPictureInPicture) {
|
||||||
eventEmitter.onPictureInPictureStatusChanged.invoke(isInPictureInPicture);
|
eventEmitter.onPictureInPictureStatusChanged.invoke(isInPictureInPicture);
|
||||||
|
|||||||
Reference in New Issue
Block a user