|
1 | 1 | package co.tinode.tindroid; |
2 | 2 |
|
| 3 | +import android.content.Intent; |
3 | 4 | import android.os.Build; |
4 | 5 |
|
5 | 6 | import com.google.firebase.messaging.FirebaseMessaging; |
|
8 | 9 |
|
9 | 10 | import androidx.annotation.NonNull; |
10 | 11 | import androidx.annotation.Nullable; |
| 12 | +import androidx.localbroadcastmanager.content.LocalBroadcastManager; |
| 13 | + |
11 | 14 | import co.tinode.tindroid.db.BaseDb; |
12 | 15 | import co.tinode.tindroid.media.VxCard; |
13 | 16 | import co.tinode.tindroid.services.CallConnection; |
14 | 17 | import co.tinode.tinodesdk.ComTopic; |
15 | 18 | import co.tinode.tinodesdk.FndTopic; |
16 | 19 | import co.tinode.tinodesdk.MeTopic; |
17 | 20 | import co.tinode.tinodesdk.PromisedReply; |
| 21 | +import co.tinode.tinodesdk.Storage; |
18 | 22 | import co.tinode.tinodesdk.Tinode; |
| 23 | +import co.tinode.tinodesdk.model.MsgServerData; |
| 24 | +import co.tinode.tinodesdk.model.MsgServerInfo; |
19 | 25 | import co.tinode.tinodesdk.model.PrivateType; |
20 | 26 | import co.tinode.tinodesdk.model.ServerMessage; |
21 | 27 |
|
@@ -49,6 +55,69 @@ public static synchronized Tinode getTinode() { |
49 | 55 | // Set device language |
50 | 56 | sInstance.mTinode.setLanguage(Locale.getDefault().toString()); |
51 | 57 |
|
| 58 | + // Event handlers for video calls. |
| 59 | + sInstance.mTinode.addListener(new Tinode.EventListener() { |
| 60 | + @Override |
| 61 | + public void onDataMessage(MsgServerData data) { |
| 62 | + if (Cache.getTinode().isMe(data.from)) { |
| 63 | + return; |
| 64 | + } |
| 65 | + String webrtc = data.getStringHeader("webrtc"); |
| 66 | + if (MsgServerData.parseWebRTC(webrtc) != MsgServerData.WebRTC.STARTED) { |
| 67 | + return; |
| 68 | + } |
| 69 | + ComTopic topic = (ComTopic) Cache.getTinode().getTopic(data.topic); |
| 70 | + if (topic == null) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // Check if we have a later version of the message (which means the call |
| 75 | + // has been not yet either accepted or finished). |
| 76 | + Storage.Message msg = topic.getMessage(data.seq); |
| 77 | + if (msg != null) { |
| 78 | + webrtc = msg.getStringHeader("webrtc"); |
| 79 | + if (webrtc != null && MsgServerData.parseWebRTC(webrtc) != MsgServerData.WebRTC.STARTED) { |
| 80 | + return; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + CallInProgress call = Cache.getCallInProgress(); |
| 85 | + if (call == null) { |
| 86 | + CallManager.acceptIncomingCall(TindroidApp.getAppContext(), |
| 87 | + data.topic, data.seq, data.getBooleanHeader("aonly")); |
| 88 | + } else if (!call.equals(data.topic, data.seq)) { |
| 89 | + // Another incoming call. Decline. |
| 90 | + topic.videoCallHangUp(data.seq); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onInfoMessage(MsgServerInfo info) { |
| 96 | + if (MsgServerInfo.parseWhat(info.what) != MsgServerInfo.What.CALL) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + CallInProgress call = Cache.getCallInProgress(); |
| 101 | + if (call == null || !call.equals(info.src, info.seq) || !Tinode.TOPIC_ME.equals(info.topic)) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + // Dismiss call notification. |
| 106 | + // Hang-up event received or current user accepted the call from another device. |
| 107 | + if (MsgServerInfo.parseEvent(info.event) == MsgServerInfo.Event.HANG_UP || |
| 108 | + (Cache.getTinode().isMe(info.from) && |
| 109 | + MsgServerInfo.parseEvent(info.event) == MsgServerInfo.Event.ACCEPT)) { |
| 110 | + LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(TindroidApp.getAppContext()); |
| 111 | + final Intent intent = new Intent(TindroidApp.getAppContext(), |
| 112 | + HangUpBroadcastReceiver.class); |
| 113 | + intent.setAction(Const.INTENT_ACTION_CALL_CLOSE); |
| 114 | + intent.putExtra(Const.INTENT_EXTRA_TOPIC, info.src); |
| 115 | + intent.putExtra(Const.INTENT_EXTRA_SEQ, info.seq); |
| 116 | + lbm.sendBroadcast(intent); |
| 117 | + } |
| 118 | + } |
| 119 | + }); |
| 120 | + |
52 | 121 | // Keep in app to prevent garbage collection. |
53 | 122 | TindroidApp.retainCache(sInstance); |
54 | 123 | } |
|
0 commit comments