mirror of
https://github.com/nextcloud/talk-android.git
synced 2024-11-26 15:05:44 +03:00
Spotbug: split message processing to reduce complexity
Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
parent
7b86b02c21
commit
c5067b7a60
1 changed files with 149 additions and 118 deletions
|
@ -54,6 +54,7 @@ import java.util.Map;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import autodagger.AutoInjector;
|
import autodagger.AutoInjector;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
|
@ -163,131 +164,39 @@ public class MagicWebSocketInstance extends WebSocketListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onMessage(WebSocket webSocket, String text) {
|
public void onMessage(@NonNull WebSocket webSocket, @NonNull String text) {
|
||||||
if (webSocket == internalWebSocket) {
|
if (webSocket == internalWebSocket) {
|
||||||
Log.d(TAG, "Receiving : " + webSocket + " " + text);
|
Log.d(TAG, "Receiving : " + webSocket + " " + text);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
BaseWebSocketMessage baseWebSocketMessage = LoganSquare.parse(text, BaseWebSocketMessage.class);
|
BaseWebSocketMessage baseWebSocketMessage = LoganSquare.parse(text, BaseWebSocketMessage.class);
|
||||||
String messageType = baseWebSocketMessage.getType();
|
String messageType = baseWebSocketMessage.getType();
|
||||||
switch (messageType) {
|
if (messageType != null) {
|
||||||
case "hello":
|
switch (messageType) {
|
||||||
connected = true;
|
case "hello":
|
||||||
reconnecting = false;
|
processHelloMessage(webSocket, text);
|
||||||
restartCount = 0;
|
break;
|
||||||
String oldResumeId = resumeId;
|
case "error":
|
||||||
HelloResponseOverallWebSocketMessage helloResponseWebSocketMessage = LoganSquare.parse(text, HelloResponseOverallWebSocketMessage.class);
|
processErrorMessage(webSocket, text);
|
||||||
resumeId = helloResponseWebSocketMessage.getHelloResponseWebSocketMessage().getResumeId();
|
break;
|
||||||
sessionId = helloResponseWebSocketMessage.getHelloResponseWebSocketMessage().getSessionId();
|
case "room":
|
||||||
hasMCU = helloResponseWebSocketMessage.getHelloResponseWebSocketMessage().serverHasMCUSupport();
|
processJoinedRoomMessage(text);
|
||||||
|
break;
|
||||||
for (int i = 0; i < messagesQueue.size(); i++) {
|
case "event":
|
||||||
webSocket.send(messagesQueue.get(i));
|
processEventMessage(text);
|
||||||
}
|
break;
|
||||||
|
case "message":
|
||||||
messagesQueue = new ArrayList<>();
|
processMessage(text);
|
||||||
HashMap<String, String> helloHasHap = new HashMap<>();
|
break;
|
||||||
if (!TextUtils.isEmpty(oldResumeId)) {
|
case "bye":
|
||||||
helloHasHap.put("oldResumeId", oldResumeId);
|
connected = false;
|
||||||
} else {
|
|
||||||
currentRoomToken = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!TextUtils.isEmpty(currentRoomToken)) {
|
|
||||||
helloHasHap.put(ROOM_TOKEN, currentRoomToken);
|
|
||||||
}
|
|
||||||
eventBus.post(new WebSocketCommunicationEvent("hello", helloHasHap));
|
|
||||||
break;
|
|
||||||
case "error":
|
|
||||||
Log.e(TAG, "Received error: " + text);
|
|
||||||
ErrorOverallWebSocketMessage errorOverallWebSocketMessage = LoganSquare.parse(text, ErrorOverallWebSocketMessage.class);
|
|
||||||
if (("no_such_session").equals(errorOverallWebSocketMessage.getErrorWebSocketMessage().getCode())) {
|
|
||||||
Log.d(TAG, "WebSocket " + webSocket.hashCode() + " resumeID " + resumeId + " expired");
|
|
||||||
resumeId = "";
|
resumeId = "";
|
||||||
currentRoomToken = "";
|
break;
|
||||||
restartWebSocket();
|
default:
|
||||||
} else if (("hello_expected").equals(errorOverallWebSocketMessage.getErrorWebSocketMessage().getCode())) {
|
break;
|
||||||
restartWebSocket();
|
}
|
||||||
}
|
} else {
|
||||||
|
Log.e(TAG, "Received message with type: null");
|
||||||
break;
|
|
||||||
case "room":
|
|
||||||
JoinedRoomOverallWebSocketMessage joinedRoomOverallWebSocketMessage = LoganSquare.parse(text, JoinedRoomOverallWebSocketMessage.class);
|
|
||||||
currentRoomToken = joinedRoomOverallWebSocketMessage.getRoomWebSocketMessage().getRoomId();
|
|
||||||
if (joinedRoomOverallWebSocketMessage.getRoomWebSocketMessage().getRoomPropertiesWebSocketMessage() != null && !TextUtils.isEmpty(currentRoomToken)) {
|
|
||||||
sendRoomJoinedEvent();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "event":
|
|
||||||
EventOverallWebSocketMessage eventOverallWebSocketMessage = LoganSquare.parse(text, EventOverallWebSocketMessage.class);
|
|
||||||
if (eventOverallWebSocketMessage.getEventMap() != null) {
|
|
||||||
String target = (String) eventOverallWebSocketMessage.getEventMap().get("target");
|
|
||||||
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.KEY_ROOM_TOKEN, (String) messageHashMap.get("roomid"));
|
|
||||||
refreshChatHashMap.put(BundleKeys.KEY_INTERNAL_USER_ID, Long.toString(conversationUser.getId()));
|
|
||||||
eventBus.post(new WebSocketCommunicationEvent("refreshChat", refreshChatHashMap));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (eventOverallWebSocketMessage.getEventMap().get("type").equals("join")) {
|
|
||||||
List<HashMap<String, Object>> joinEventList = (List<HashMap<String, Object>>) eventOverallWebSocketMessage.getEventMap().get("join");
|
|
||||||
HashMap<String, Object> internalHashMap;
|
|
||||||
Participant participant;
|
|
||||||
for (int i = 0; i < joinEventList.size(); i++) {
|
|
||||||
internalHashMap = joinEventList.get(i);
|
|
||||||
HashMap<String, Object> userMap = (HashMap<String, Object>) internalHashMap.get("user");
|
|
||||||
participant = new Participant();
|
|
||||||
String userId = (String) internalHashMap.get("userid");
|
|
||||||
if (userId != null) {
|
|
||||||
participant.setActorType(USERS);
|
|
||||||
participant.setActorId(userId);
|
|
||||||
} else {
|
|
||||||
participant.setActorType(GUESTS);
|
|
||||||
// FIXME seems to be not given by the HPB: participant.setActorId();
|
|
||||||
}
|
|
||||||
if (userMap != null) {
|
|
||||||
// There is no "user" attribute for guest participants.
|
|
||||||
participant.setDisplayName((String) userMap.get("displayname"));
|
|
||||||
}
|
|
||||||
usersHashMap.put((String) internalHashMap.get("sessionid"), participant);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case TARGET_PARTICIPANTS:
|
|
||||||
signalingMessageReceiver.process(eventOverallWebSocketMessage.getEventMap());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "message":
|
|
||||||
CallOverallWebSocketMessage callOverallWebSocketMessage = LoganSquare.parse(text, CallOverallWebSocketMessage.class);
|
|
||||||
NCSignalingMessage ncSignalingMessage = callOverallWebSocketMessage.getCallWebSocketMessage().getNcSignalingMessage();
|
|
||||||
if (TextUtils.isEmpty(ncSignalingMessage.getFrom()) && callOverallWebSocketMessage.getCallWebSocketMessage().getSenderWebSocketMessage() != null) {
|
|
||||||
ncSignalingMessage.setFrom(callOverallWebSocketMessage.getCallWebSocketMessage().getSenderWebSocketMessage().getSessionId());
|
|
||||||
}
|
|
||||||
|
|
||||||
signalingMessageReceiver.process(ncSignalingMessage);
|
|
||||||
break;
|
|
||||||
case "bye":
|
|
||||||
connected = false;
|
|
||||||
resumeId = "";
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, "Failed to recognize WebSocket message", e);
|
Log.e(TAG, "Failed to recognize WebSocket message", e);
|
||||||
|
@ -295,6 +204,128 @@ public class MagicWebSocketInstance extends WebSocketListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void processMessage(String text) throws IOException {
|
||||||
|
CallOverallWebSocketMessage callOverallWebSocketMessage = LoganSquare.parse(text, CallOverallWebSocketMessage.class);
|
||||||
|
NCSignalingMessage ncSignalingMessage = callOverallWebSocketMessage.getCallWebSocketMessage().getNcSignalingMessage();
|
||||||
|
if (TextUtils.isEmpty(ncSignalingMessage.getFrom()) && callOverallWebSocketMessage.getCallWebSocketMessage().getSenderWebSocketMessage() != null) {
|
||||||
|
ncSignalingMessage.setFrom(callOverallWebSocketMessage.getCallWebSocketMessage().getSenderWebSocketMessage().getSessionId());
|
||||||
|
}
|
||||||
|
|
||||||
|
signalingMessageReceiver.process(ncSignalingMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processEventMessage(String text) throws IOException {
|
||||||
|
EventOverallWebSocketMessage eventOverallWebSocketMessage = LoganSquare.parse(text, EventOverallWebSocketMessage.class);
|
||||||
|
if (eventOverallWebSocketMessage.getEventMap() != null) {
|
||||||
|
String target = (String) eventOverallWebSocketMessage.getEventMap().get("target");
|
||||||
|
switch (target) {
|
||||||
|
case "room":
|
||||||
|
if (eventOverallWebSocketMessage.getEventMap().get("type").equals("message")) {
|
||||||
|
processRoomMessageMessage(eventOverallWebSocketMessage);
|
||||||
|
} else if (eventOverallWebSocketMessage.getEventMap().get("type").equals("join")) {
|
||||||
|
processRoomJoinMessage(eventOverallWebSocketMessage);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case TARGET_PARTICIPANTS:
|
||||||
|
signalingMessageReceiver.process(eventOverallWebSocketMessage.getEventMap());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processRoomMessageMessage(EventOverallWebSocketMessage eventOverallWebSocketMessage) {
|
||||||
|
Map<String, Object> messageHashMap = (Map<String, Object>) eventOverallWebSocketMessage
|
||||||
|
.getEventMap()
|
||||||
|
.get("message");
|
||||||
|
if (messageHashMap != null && messageHashMap.containsKey("data")) {
|
||||||
|
Map<String, Object> dataHashMap = (Map<String, Object>) messageHashMap.get("data");
|
||||||
|
if (dataHashMap != null && dataHashMap.containsKey("chat")) {
|
||||||
|
Map<String, Object> chatMap = (Map<String, Object>) dataHashMap.get("chat");
|
||||||
|
if (chatMap != null && chatMap.containsKey("refresh") && (boolean) chatMap.get("refresh")) {
|
||||||
|
HashMap<String, String> refreshChatHashMap = new HashMap<>();
|
||||||
|
refreshChatHashMap.put(BundleKeys.KEY_ROOM_TOKEN, (String) messageHashMap.get("roomid"));
|
||||||
|
refreshChatHashMap.put(BundleKeys.KEY_INTERNAL_USER_ID, Long.toString(conversationUser.getId()));
|
||||||
|
eventBus.post(new WebSocketCommunicationEvent("refreshChat", refreshChatHashMap));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processRoomJoinMessage(EventOverallWebSocketMessage eventOverallWebSocketMessage) {
|
||||||
|
List<HashMap<String, Object>> joinEventList = (List<HashMap<String, Object>>) eventOverallWebSocketMessage
|
||||||
|
.getEventMap()
|
||||||
|
.get("join");
|
||||||
|
HashMap<String, Object> internalHashMap;
|
||||||
|
Participant participant;
|
||||||
|
for (int i = 0; i < joinEventList.size(); i++) {
|
||||||
|
internalHashMap = joinEventList.get(i);
|
||||||
|
HashMap<String, Object> userMap = (HashMap<String, Object>) internalHashMap.get("user");
|
||||||
|
participant = new Participant();
|
||||||
|
String userId = (String) internalHashMap.get("userid");
|
||||||
|
if (userId != null) {
|
||||||
|
participant.setActorType(USERS);
|
||||||
|
participant.setActorId(userId);
|
||||||
|
} else {
|
||||||
|
participant.setActorType(GUESTS);
|
||||||
|
// FIXME seems to be not given by the HPB: participant.setActorId();
|
||||||
|
}
|
||||||
|
if (userMap != null) {
|
||||||
|
// There is no "user" attribute for guest participants.
|
||||||
|
participant.setDisplayName((String) userMap.get("displayname"));
|
||||||
|
}
|
||||||
|
usersHashMap.put((String) internalHashMap.get("sessionid"), participant);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processJoinedRoomMessage(String text) throws IOException {
|
||||||
|
JoinedRoomOverallWebSocketMessage joinedRoomOverallWebSocketMessage = LoganSquare.parse(text, JoinedRoomOverallWebSocketMessage.class);
|
||||||
|
currentRoomToken = joinedRoomOverallWebSocketMessage.getRoomWebSocketMessage().getRoomId();
|
||||||
|
if (joinedRoomOverallWebSocketMessage.getRoomWebSocketMessage().getRoomPropertiesWebSocketMessage() != null && !TextUtils.isEmpty(currentRoomToken)) {
|
||||||
|
sendRoomJoinedEvent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processErrorMessage(WebSocket webSocket, String text) throws IOException {
|
||||||
|
Log.e(TAG, "Received error: " + text);
|
||||||
|
ErrorOverallWebSocketMessage errorOverallWebSocketMessage = LoganSquare.parse(text, ErrorOverallWebSocketMessage.class);
|
||||||
|
if ("no_such_session".equals(errorOverallWebSocketMessage.getErrorWebSocketMessage().getCode())) {
|
||||||
|
Log.d(TAG, "WebSocket " + webSocket.hashCode() + " resumeID " + resumeId + " expired");
|
||||||
|
resumeId = "";
|
||||||
|
currentRoomToken = "";
|
||||||
|
restartWebSocket();
|
||||||
|
} else if ("hello_expected".equals(errorOverallWebSocketMessage.getErrorWebSocketMessage().getCode())) {
|
||||||
|
restartWebSocket();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processHelloMessage(WebSocket webSocket, String text) throws IOException {
|
||||||
|
connected = true;
|
||||||
|
reconnecting = false;
|
||||||
|
restartCount = 0;
|
||||||
|
String oldResumeId = resumeId;
|
||||||
|
HelloResponseOverallWebSocketMessage helloResponseWebSocketMessage = LoganSquare.parse(text, HelloResponseOverallWebSocketMessage.class);
|
||||||
|
resumeId = helloResponseWebSocketMessage.getHelloResponseWebSocketMessage().getResumeId();
|
||||||
|
sessionId = helloResponseWebSocketMessage.getHelloResponseWebSocketMessage().getSessionId();
|
||||||
|
hasMCU = helloResponseWebSocketMessage.getHelloResponseWebSocketMessage().serverHasMCUSupport();
|
||||||
|
|
||||||
|
for (int i = 0; i < messagesQueue.size(); i++) {
|
||||||
|
webSocket.send(messagesQueue.get(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
messagesQueue = new ArrayList<>();
|
||||||
|
HashMap<String, String> helloHasHap = new HashMap<>();
|
||||||
|
if (!TextUtils.isEmpty(oldResumeId)) {
|
||||||
|
helloHasHap.put("oldResumeId", oldResumeId);
|
||||||
|
} else {
|
||||||
|
currentRoomToken = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!TextUtils.isEmpty(currentRoomToken)) {
|
||||||
|
helloHasHap.put(ROOM_TOKEN, currentRoomToken);
|
||||||
|
}
|
||||||
|
eventBus.post(new WebSocketCommunicationEvent("hello", helloHasHap));
|
||||||
|
}
|
||||||
|
|
||||||
private void sendRoomJoinedEvent() {
|
private void sendRoomJoinedEvent() {
|
||||||
HashMap<String, String> joinRoomHashMap = new HashMap<>();
|
HashMap<String, String> joinRoomHashMap = new HashMap<>();
|
||||||
joinRoomHashMap.put(ROOM_TOKEN, currentRoomToken);
|
joinRoomHashMap.put(ROOM_TOKEN, currentRoomToken);
|
||||||
|
|
Loading…
Reference in a new issue