From 5394aaa44cc5f7f526255e6338963e91f202e251 Mon Sep 17 00:00:00 2001 From: Alex Hart Date: Fri, 27 Sep 2024 10:27:08 -0300 Subject: [PATCH] Set pip auto-enter based off live view-model value. --- .../securesms/WebRtcCallActivity.java | 26 +++++++++++++------ .../webrtc/WebRtcCallViewModel.java | 7 ++--- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/WebRtcCallActivity.java b/app/src/main/java/org/thoughtcrime/securesms/WebRtcCallActivity.java index aa72ba0804..b5d63b0b35 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/WebRtcCallActivity.java +++ b/app/src/main/java/org/thoughtcrime/securesms/WebRtcCallActivity.java @@ -370,7 +370,7 @@ public class WebRtcCallActivity extends BaseActivity implements SafetyNumberChan private boolean enterPipModeIfPossible() { if (isSystemPipEnabledAndAvailable()) { - if (viewModel.canEnterPipMode()) { + if (Boolean.TRUE.equals(viewModel.canEnterPipMode().getValue())) { try { enterPictureInPictureMode(pipBuilderParams.build()); } catch (Exception e) { @@ -380,6 +380,7 @@ public class WebRtcCallActivity extends BaseActivity implements SafetyNumberChan return true; } + if (Build.VERSION.SDK_INT >= 31) { pipBuilderParams.setAutoEnterEnabled(false); } @@ -573,15 +574,24 @@ public class WebRtcCallActivity extends BaseActivity implements SafetyNumberChan pipBuilderParams = new PictureInPictureParams.Builder(); pipBuilderParams.setAspectRatio(aspectRatio); + if (Build.VERSION.SDK_INT >= 31) { - pipBuilderParams.setAutoEnterEnabled(true); + viewModel.canEnterPipMode().observe(this, canEnterPipMode -> { + pipBuilderParams.setAutoEnterEnabled(canEnterPipMode); + tryToSetPictureInPictureParams(); + }); + } else { + tryToSetPictureInPictureParams(); } - if (Build.VERSION.SDK_INT >= 26) { - try { - setPictureInPictureParams(pipBuilderParams.build()); - } catch (Exception e) { - Log.w(TAG, "System lied about having PiP available.", e); - } + } + } + + private void tryToSetPictureInPictureParams() { + if (Build.VERSION.SDK_INT >= 26) { + try { + setPictureInPictureParams(pipBuilderParams.build()); + } catch (Exception e) { + Log.w(TAG, "System lied about having PiP available.", e); } } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/webrtc/WebRtcCallViewModel.java b/app/src/main/java/org/thoughtcrime/securesms/components/webrtc/WebRtcCallViewModel.java index 03988cab52..4d15cd6f73 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/webrtc/WebRtcCallViewModel.java +++ b/app/src/main/java/org/thoughtcrime/securesms/components/webrtc/WebRtcCallViewModel.java @@ -17,6 +17,7 @@ import androidx.lifecycle.ViewModel; import com.annimon.stream.Stream; import org.signal.core.util.ThreadUtil; +import org.signal.core.util.logging.Log; import org.thoughtcrime.securesms.components.webrtc.v2.CallControlsState; import org.thoughtcrime.securesms.components.webrtc.v2.CallEvent; import org.thoughtcrime.securesms.database.GroupTable; @@ -74,6 +75,7 @@ public class WebRtcCallViewModel extends ViewModel { private final LiveData groupMemberCount = Transformations.map(groupMembers, List::size); private final Observable shouldShowSpeakerHint = participantsState.map(this::shouldShowSpeakerHint); private final MutableLiveData isLandscapeEnabled = new MutableLiveData<>(); + private final MutableLiveData canEnterPipMode = new MutableLiveData<>(false); private final Observer> groupMemberStateUpdater = m -> participantsState.onNext(CallParticipantsState.update(participantsState.getValue(), m)); private final MutableLiveData ephemeralState = new MutableLiveData<>(); private final BehaviorProcessor recipientId = BehaviorProcessor.createDefault(RecipientId.UNKNOWN); @@ -91,7 +93,6 @@ public class WebRtcCallViewModel extends ViewModel { private boolean wasInOutgoingRingingMode = false; private long callConnectedTime = -1; private boolean answerWithVideoAvailable = false; - private boolean canEnterPipMode = false; private List previousParticipantsList = Collections.emptyList(); private boolean callStarting = false; private boolean switchOnFirstScreenShare = true; @@ -209,7 +210,7 @@ public class WebRtcCallViewModel extends ViewModel { return ephemeralState; } - public boolean canEnterPipMode() { + public LiveData canEnterPipMode() { return canEnterPipMode; } @@ -280,7 +281,7 @@ public class WebRtcCallViewModel extends ViewModel { @MainThread public void updateFromWebRtcViewModel(@NonNull WebRtcViewModel webRtcViewModel, boolean enableVideo) { - canEnterPipMode = !webRtcViewModel.getState().isPreJoinOrNetworkUnavailable(); + canEnterPipMode.setValue(!webRtcViewModel.getState().isPreJoinOrNetworkUnavailable()); if (callStarting && webRtcViewModel.getState().isPassedPreJoin()) { callStarting = false; }