Browse Source

Add null checks and extract constants

Signed-off-by: Tim Krüger <t@timkrueger.me>
Tim Krüger 3 years ago
parent
commit
e52b2d8b7f

+ 21 - 6
app/src/main/java/com/nextcloud/talk/activities/CallActivity.java

@@ -157,6 +157,12 @@ import me.zhanghai.android.effortlesspermissions.OpenAppDetailsDialogFragment;
 import okhttp3.Cache;
 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)
 public class CallActivity extends CallBaseActivity {
 
@@ -1479,24 +1485,33 @@ public class CallActivity extends CallBaseActivity {
                     performCall();
                 }
                 break;
-            case "participantsUpdate":
+            case PARTICIPANTS_UPDATE:
                 Log.d(TAG, "onMessageEvent 'participantsUpdate'");
 
                 // See MagicWebSocketInstance#onMessage in case "participants" how the 'updateParameters' are created
                 Map<String, String> updateParameters = webSocketCommunicationEvent.getHashMap();
 
-                if (roomToken.equals(updateParameters.get("roomToken"))) {
-                    if (updateParameters.containsKey("all") && Boolean.parseBoolean(updateParameters.get("all"))) {
-                        if (updateParameters.containsKey("incall") && "0".equals(updateParameters.get("incall"))) {
+                if (updateParameters == null) {
+                    break;
+                }
+
+                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.");
                             hangup(true);
                         }
-                    } else if (updateParameters.containsKey("jobId")) {
+                    } else if (jobId != null) {
                         // In that case a list of users for the room is passed.
                         processUsersInRoom(
                             (List<HashMap<String, Object>>) webSocketClient
                                 .getJobWithId(
-                                    Integer.valueOf(updateParameters.get("jobId"))));
+                                    Integer.valueOf(jobId)));
                     }
 
                 }

+ 18 - 0
app/src/main/java/com/nextcloud/talk/webrtc/Globals.java

@@ -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";
+}

+ 61 - 31
app/src/main/java/com/nextcloud/talk/webrtc/MagicWebSocketInstance.java

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