Add null checks and extract constants

Signed-off-by: Tim Krüger <t@timkrueger.me>
This commit is contained in:
Tim Krüger 2022-05-05 13:06:15 +02:00
parent 4f5a344a20
commit e52b2d8b7f
No known key found for this signature in database
GPG Key ID: FECE3A7222C52A4E
3 changed files with 100 additions and 37 deletions

View File

@ -157,6 +157,12 @@ import me.zhanghai.android.effortlesspermissions.OpenAppDetailsDialogFragment;
import okhttp3.Cache; import okhttp3.Cache;
import pub.devrel.easypermissions.AfterPermissionGranted; import pub.devrel.easypermissions.AfterPermissionGranted;
import static com.nextcloud.talk.webrtc.Globals.JOB_ID;
import static com.nextcloud.talk.webrtc.Globals.PARTICIPANTS_UPDATE;
import static com.nextcloud.talk.webrtc.Globals.ROOM_TOKEN;
import static com.nextcloud.talk.webrtc.Globals.UPDATE_ALL;
import static com.nextcloud.talk.webrtc.Globals.UPDATE_IN_CALL;
@AutoInjector(NextcloudTalkApplication.class) @AutoInjector(NextcloudTalkApplication.class)
public class CallActivity extends CallBaseActivity { public class CallActivity extends CallBaseActivity {
@ -1479,24 +1485,33 @@ public class CallActivity extends CallBaseActivity {
performCall(); performCall();
} }
break; break;
case "participantsUpdate": case PARTICIPANTS_UPDATE:
Log.d(TAG, "onMessageEvent 'participantsUpdate'"); Log.d(TAG, "onMessageEvent 'participantsUpdate'");
// See MagicWebSocketInstance#onMessage in case "participants" how the 'updateParameters' are created // See MagicWebSocketInstance#onMessage in case "participants" how the 'updateParameters' are created
Map<String, String> updateParameters = webSocketCommunicationEvent.getHashMap(); Map<String, String> updateParameters = webSocketCommunicationEvent.getHashMap();
if (roomToken.equals(updateParameters.get("roomToken"))) { if (updateParameters == null) {
if (updateParameters.containsKey("all") && Boolean.parseBoolean(updateParameters.get("all"))) { break;
if (updateParameters.containsKey("incall") && "0".equals(updateParameters.get("incall"))) { }
String updateRoomToken = updateParameters.get(ROOM_TOKEN);
String updateAll = updateParameters.get(UPDATE_ALL);
String updateInCall = updateParameters.get(UPDATE_IN_CALL);
String jobId = updateParameters.get(JOB_ID);
if (roomToken.equals(updateRoomToken)) {
if (updateAll != null && Boolean.parseBoolean(updateAll)) {
if ("0".equals(updateInCall)) {
Log.d(TAG, "Most probably a moderator ended the call for all."); Log.d(TAG, "Most probably a moderator ended the call for all.");
hangup(true); hangup(true);
} }
} else if (updateParameters.containsKey("jobId")) { } else if (jobId != null) {
// In that case a list of users for the room is passed. // In that case a list of users for the room is passed.
processUsersInRoom( processUsersInRoom(
(List<HashMap<String, Object>>) webSocketClient (List<HashMap<String, Object>>) webSocketClient
.getJobWithId( .getJobWithId(
Integer.valueOf(updateParameters.get("jobId")))); Integer.valueOf(jobId)));
} }
} }

View File

@ -0,0 +1,18 @@
package com.nextcloud.talk.webrtc;
public class Globals {
public static final String ROOM_TOKEN = "roomToken";
public static final String JOB_ID = "jobId";
public static final String PARTICIPANTS_UPDATE = "participantsUpdate";
public static final String TARGET_PARTICIPANTS = "participants";
public static final String EVENT_TYPE = "type";
public static final String EVENT_TYPE_UPDATE = "update";
public static final String UPDATE_ALL = "all";
public static final String UPDATE_IN_CALL = "incall";
public static final String UPDATE_ROOM_ID = "roomid";
public static final String UPDATE_USERS = "users";
}

View File

@ -65,11 +65,22 @@ import okio.ByteString;
import static com.nextcloud.talk.models.json.participants.Participant.ActorType.GUESTS; import static com.nextcloud.talk.models.json.participants.Participant.ActorType.GUESTS;
import static com.nextcloud.talk.models.json.participants.Participant.ActorType.USERS; import static com.nextcloud.talk.models.json.participants.Participant.ActorType.USERS;
import static com.nextcloud.talk.webrtc.Globals.EVENT_TYPE;
import static com.nextcloud.talk.webrtc.Globals.EVENT_TYPE_UPDATE;
import static com.nextcloud.talk.webrtc.Globals.JOB_ID;
import static com.nextcloud.talk.webrtc.Globals.PARTICIPANTS_UPDATE;
import static com.nextcloud.talk.webrtc.Globals.ROOM_TOKEN;
import static com.nextcloud.talk.webrtc.Globals.TARGET_PARTICIPANTS;
import static com.nextcloud.talk.webrtc.Globals.UPDATE_ALL;
import static com.nextcloud.talk.webrtc.Globals.UPDATE_IN_CALL;
import static com.nextcloud.talk.webrtc.Globals.UPDATE_ROOM_ID;
import static com.nextcloud.talk.webrtc.Globals.UPDATE_USERS;
@AutoInjector(NextcloudTalkApplication.class) @AutoInjector(NextcloudTalkApplication.class)
public class MagicWebSocketInstance extends WebSocketListener { public class MagicWebSocketInstance extends WebSocketListener {
private static final String TAG = "MagicWebSocketInstance"; private static final String TAG = "MagicWebSocketInstance";
@Inject @Inject
OkHttpClient okHttpClient; OkHttpClient okHttpClient;
@ -190,7 +201,7 @@ public class MagicWebSocketInstance extends WebSocketListener {
} }
if (!TextUtils.isEmpty(currentRoomToken)) { if (!TextUtils.isEmpty(currentRoomToken)) {
helloHasHap.put("roomToken", currentRoomToken); helloHasHap.put(ROOM_TOKEN, currentRoomToken);
} }
eventBus.post(new WebSocketCommunicationEvent("hello", helloHasHap)); eventBus.post(new WebSocketCommunicationEvent("hello", helloHasHap));
break; break;
@ -221,23 +232,23 @@ public class MagicWebSocketInstance extends WebSocketListener {
switch (target) { switch (target) {
case "room": case "room":
if (eventOverallWebSocketMessage.getEventMap().get("type").equals("message")) { if (eventOverallWebSocketMessage.getEventMap().get("type").equals("message")) {
Map<String, Object> messageHashMap = Map<String, Object> messageHashMap =
(Map<String, Object>) eventOverallWebSocketMessage.getEventMap().get("message"); (Map<String, Object>) eventOverallWebSocketMessage.getEventMap().get("message");
if (messageHashMap.containsKey("data")) { if (messageHashMap.containsKey("data")) {
Map<String, Object> dataHashMap = (Map<String, Object>) messageHashMap.get( Map<String, Object> dataHashMap = (Map<String, Object>) messageHashMap.get(
"data"); "data");
if (dataHashMap.containsKey("chat")) { if (dataHashMap.containsKey("chat")) {
boolean shouldRefreshChat; boolean shouldRefreshChat;
Map<String, Object> chatMap = (Map<String, Object>) dataHashMap.get("chat"); Map<String, Object> chatMap = (Map<String, Object>) dataHashMap.get("chat");
if (chatMap.containsKey("refresh")) { if (chatMap.containsKey("refresh")) {
shouldRefreshChat = (boolean) chatMap.get("refresh"); shouldRefreshChat = (boolean) chatMap.get("refresh");
if (shouldRefreshChat) { if (shouldRefreshChat) {
HashMap<String, String> refreshChatHashMap = new HashMap<>(); HashMap<String, String> refreshChatHashMap = new HashMap<>();
refreshChatHashMap.put(BundleKeys.INSTANCE.getKEY_ROOM_TOKEN(), (String) messageHashMap.get("roomid")); refreshChatHashMap.put(BundleKeys.INSTANCE.getKEY_ROOM_TOKEN(), (String) messageHashMap.get("roomid"));
refreshChatHashMap.put(BundleKeys.INSTANCE.getKEY_INTERNAL_USER_ID(), Long.toString(conversationUser.getId())); refreshChatHashMap.put(BundleKeys.INSTANCE.getKEY_INTERNAL_USER_ID(), Long.toString(conversationUser.getId()));
eventBus.post(new WebSocketCommunicationEvent("refreshChat", refreshChatHashMap)); eventBus.post(new WebSocketCommunicationEvent("refreshChat", refreshChatHashMap));
}
} }
}
} }
} }
} else if (eventOverallWebSocketMessage.getEventMap().get("type").equals("join")) { } else if (eventOverallWebSocketMessage.getEventMap().get("type").equals("join")) {
@ -264,26 +275,45 @@ public class MagicWebSocketInstance extends WebSocketListener {
} }
} }
break; break;
case "participants": case TARGET_PARTICIPANTS:
if (eventOverallWebSocketMessage.getEventMap().get("type").equals("update")) { if (EVENT_TYPE_UPDATE.equals(eventOverallWebSocketMessage.getEventMap().get(EVENT_TYPE))) {
HashMap<String, String> refreshChatHashMap = new HashMap<>(); HashMap<String, String> refreshChatHashMap = new HashMap<>();
HashMap<String, Object> updateEventMap = (HashMap<String, Object>) eventOverallWebSocketMessage.getEventMap().get("update"); HashMap<String, Object> updateEventMap = (HashMap<String, Object>) eventOverallWebSocketMessage.getEventMap().get(EVENT_TYPE_UPDATE);
refreshChatHashMap.put("roomToken", (String) updateEventMap.get("roomid"));
if (updateEventMap.containsKey("users")) { if (updateEventMap == null) {
refreshChatHashMap.put("jobId", Integer.toString(magicMap.add(updateEventMap.get("users")))); break;
} }
if (updateEventMap.containsKey("incall")) { if (updateEventMap.containsKey(UPDATE_ROOM_ID)) {
refreshChatHashMap.put("incall", Object updateRoomId = updateEventMap.get(UPDATE_ROOM_ID);
Long.toString((Long)updateEventMap.get("incall"))); if (updateRoomId != null) {
refreshChatHashMap.put(ROOM_TOKEN,
(String) updateEventMap.get(UPDATE_ROOM_ID));
}
} }
if (updateEventMap.containsKey("all")) { if (updateEventMap.containsKey(UPDATE_USERS)) {
refreshChatHashMap.put("all", Boolean.toString((Boolean) updateEventMap.get("all"))); Object updateUsers = updateEventMap.get(UPDATE_USERS);
if (updateUsers != null) {
refreshChatHashMap.put(JOB_ID, Integer.toString(magicMap.add(updateUsers)));
}
} }
eventBus.post(new WebSocketCommunicationEvent("participantsUpdate", refreshChatHashMap)); if (updateEventMap.containsKey(UPDATE_IN_CALL)) {
Object inCall = updateEventMap.get(UPDATE_IN_CALL);
if (inCall != null) {
refreshChatHashMap.put(UPDATE_IN_CALL, Long.toString((Long) inCall));
}
}
if (updateEventMap.containsKey(UPDATE_ALL)) {
Object updateAll = updateEventMap.get(UPDATE_ALL);
if (updateAll != null) {
refreshChatHashMap.put(UPDATE_ALL, Boolean.toString((Boolean) updateAll));
}
}
eventBus.post(new WebSocketCommunicationEvent(PARTICIPANTS_UPDATE, refreshChatHashMap));
} }
break; break;
} }
@ -298,7 +328,7 @@ public class MagicWebSocketInstance extends WebSocketListener {
if (!TextUtils.isEmpty(ncSignalingMessage.getFrom())) { if (!TextUtils.isEmpty(ncSignalingMessage.getFrom())) {
HashMap<String, String> messageHashMap = new HashMap<>(); HashMap<String, String> messageHashMap = new HashMap<>();
messageHashMap.put("jobId", Integer.toString(magicMap.add(ncSignalingMessage))); messageHashMap.put(JOB_ID, Integer.toString(magicMap.add(ncSignalingMessage)));
eventBus.post(new WebSocketCommunicationEvent("signalingMessage", messageHashMap)); eventBus.post(new WebSocketCommunicationEvent("signalingMessage", messageHashMap));
} }
break; break;
@ -316,7 +346,7 @@ public class MagicWebSocketInstance extends WebSocketListener {
private void sendRoomJoinedEvent() { private void sendRoomJoinedEvent() {
HashMap<String, String> joinRoomHashMap = new HashMap<>(); HashMap<String, String> joinRoomHashMap = new HashMap<>();
joinRoomHashMap.put("roomToken", currentRoomToken); joinRoomHashMap.put(ROOM_TOKEN, currentRoomToken);
eventBus.post(new WebSocketCommunicationEvent("roomJoined", joinRoomHashMap)); eventBus.post(new WebSocketCommunicationEvent("roomJoined", joinRoomHashMap));
} }