From f93a9a0f22b2eb9f7edcdf6ff47ab0ade38ee92a Mon Sep 17 00:00:00 2001 From: Nicholas Tinsley Date: Fri, 22 Dec 2023 15:27:12 -0500 Subject: [PATCH] Sort call participants by raised hand. --- .../webrtc/GroupConnectedActionProcessor.java | 12 ++++------ .../collections/ParticipantCollection.java | 23 +++++++++++++++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/service/webrtc/GroupConnectedActionProcessor.java b/app/src/main/java/org/thoughtcrime/securesms/service/webrtc/GroupConnectedActionProcessor.java index a00f06908f..19e075ec6a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/service/webrtc/GroupConnectedActionProcessor.java +++ b/app/src/main/java/org/thoughtcrime/securesms/service/webrtc/GroupConnectedActionProcessor.java @@ -206,9 +206,7 @@ public class GroupConnectedActionProcessor extends GroupActionProcessor { protected @NonNull WebRtcServiceState handleSelfRaiseHand(@NonNull WebRtcServiceState currentState, boolean raised) { Log.i(tag, "handleSelfRaiseHand():"); try { - final CallInfoState callInfoState = currentState.getCallInfoState(); - - callInfoState.requireGroupCall().raiseHand(raised); + currentState.getCallInfoState().requireGroupCall().raiseHand(raised); return currentState; } catch (CallException e) { @@ -268,11 +266,11 @@ public class GroupConnectedActionProcessor extends GroupActionProcessor { List participants = currentState.getCallInfoState().getRemoteCallParticipants(); for (CallParticipant updatedParticipant : participants) { - boolean isHandCurrentlyRaised = raisedHands.contains(updatedParticipant.getCallParticipantId().getDemuxId()); + int raisedHandIndex = raisedHands.indexOf(updatedParticipant.getCallParticipantId().getDemuxId()); boolean wasHandAlreadyRaised = updatedParticipant.isHandRaised(); - if (isHandCurrentlyRaised && !wasHandAlreadyRaised) { - builder.putParticipant(updatedParticipant.getCallParticipantId(), updatedParticipant.withHandRaisedTimestamp(now)); - } else if (!isHandCurrentlyRaised && wasHandAlreadyRaised) { + if (raisedHandIndex >= 0 && !wasHandAlreadyRaised) { + builder.putParticipant(updatedParticipant.getCallParticipantId(), updatedParticipant.withHandRaisedTimestamp(now + raisedHandIndex)); + } else if (raisedHandIndex < 0 && wasHandAlreadyRaised) { builder.putParticipant(updatedParticipant.getCallParticipantId(), updatedParticipant.withHandRaisedTimestamp(CallParticipant.HAND_LOWERED)); } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/service/webrtc/collections/ParticipantCollection.java b/app/src/main/java/org/thoughtcrime/securesms/service/webrtc/collections/ParticipantCollection.java index 578eb2c09c..4c11f364d6 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/service/webrtc/collections/ParticipantCollection.java +++ b/app/src/main/java/org/thoughtcrime/securesms/service/webrtc/collections/ParticipantCollection.java @@ -21,9 +21,22 @@ import java.util.stream.Collectors; */ public class ParticipantCollection { - private static final Comparator LEAST_RECENTLY_ADDED = (a, b) -> Long.compare(a.getAddedToCallTime(), b.getAddedToCallTime()); - private static final Comparator MOST_RECENTLY_SPOKEN = (a, b) -> Long.compare(b.getLastSpoke(), a.getLastSpoke()); - private static final Comparator MOST_RECENTLY_SPOKEN_THEN_LEAST_RECENTLY_ADDED = ComparatorCompat.chain(MOST_RECENTLY_SPOKEN).thenComparing(LEAST_RECENTLY_ADDED); + private static final Comparator LEAST_RECENTLY_ADDED = (a, b) -> Long.compare(a.getAddedToCallTime(), b.getAddedToCallTime()); + private static final Comparator MOST_RECENTLY_SPOKEN = (a, b) -> Long.compare(b.getLastSpoke(), a.getLastSpoke()); + private static final Comparator HAND_RAISED = (a, b) -> { + if (a.isHandRaised() && b.isHandRaised()) { + return Long.compare(a.getHandRaisedTimestamp(), b.getHandRaisedTimestamp()); + } else if (a.isHandRaised()) { + return -1; + } else if (b.isHandRaised()) { + return 1; + } else { + return 0; + } + }; + private static final Comparator COMPLEX_COMPARATOR_CHAIN = ComparatorCompat.chain(HAND_RAISED) + .thenComparing(MOST_RECENTLY_SPOKEN) + .thenComparing(LEAST_RECENTLY_ADDED); private final int maxGridCellCount; private final List participants; @@ -43,12 +56,12 @@ public class ParticipantCollection { return new ParticipantCollection(maxGridCellCount); } else if (this.participants.isEmpty()) { List newParticipants = new ArrayList<>(participants); - Collections.sort(newParticipants, participants.size() <= maxGridCellCount ? LEAST_RECENTLY_ADDED : MOST_RECENTLY_SPOKEN_THEN_LEAST_RECENTLY_ADDED); + Collections.sort(newParticipants, participants.size() <= maxGridCellCount ? LEAST_RECENTLY_ADDED : COMPLEX_COMPARATOR_CHAIN); return new ParticipantCollection(maxGridCellCount, newParticipants); } else { List newParticipants = new ArrayList<>(participants); - Collections.sort(newParticipants, MOST_RECENTLY_SPOKEN_THEN_LEAST_RECENTLY_ADDED); + Collections.sort(newParticipants, COMPLEX_COMPARATOR_CHAIN); List oldGridParticipantIds = Stream.of(getGridParticipants()) .map(CallParticipant::getCallParticipantId)