Skip to content

Commit 5b40c52

Browse files
aforgeor-else
authored andcommitted
Always add video call listener to Tindroid.
1 parent 206bea7 commit 5b40c52

2 files changed

Lines changed: 69 additions & 66 deletions

File tree

app/src/main/java/co/tinode/tindroid/Cache.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package co.tinode.tindroid;
22

3+
import android.content.Intent;
34
import android.os.Build;
45

56
import com.google.firebase.messaging.FirebaseMessaging;
@@ -8,14 +9,19 @@
89

910
import androidx.annotation.NonNull;
1011
import androidx.annotation.Nullable;
12+
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
13+
1114
import co.tinode.tindroid.db.BaseDb;
1215
import co.tinode.tindroid.media.VxCard;
1316
import co.tinode.tindroid.services.CallConnection;
1417
import co.tinode.tinodesdk.ComTopic;
1518
import co.tinode.tinodesdk.FndTopic;
1619
import co.tinode.tinodesdk.MeTopic;
1720
import co.tinode.tinodesdk.PromisedReply;
21+
import co.tinode.tinodesdk.Storage;
1822
import co.tinode.tinodesdk.Tinode;
23+
import co.tinode.tinodesdk.model.MsgServerData;
24+
import co.tinode.tinodesdk.model.MsgServerInfo;
1925
import co.tinode.tinodesdk.model.PrivateType;
2026
import co.tinode.tinodesdk.model.ServerMessage;
2127

@@ -49,6 +55,69 @@ public static synchronized Tinode getTinode() {
4955
// Set device language
5056
sInstance.mTinode.setLanguage(Locale.getDefault().toString());
5157

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+
52121
// Keep in app to prevent garbage collection.
53122
TindroidApp.retainCache(sInstance);
54123
}

app/src/main/java/co/tinode/tindroid/TindroidApp.java

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,8 @@
5757
import co.tinode.tindroid.account.ContactsObserver;
5858
import co.tinode.tindroid.account.Utils;
5959
import co.tinode.tindroid.db.BaseDb;
60-
import co.tinode.tinodesdk.ComTopic;
6160
import co.tinode.tinodesdk.ServerResponseException;
62-
import co.tinode.tinodesdk.Storage;
6361
import co.tinode.tinodesdk.Tinode;
64-
import co.tinode.tinodesdk.model.MsgServerData;
65-
import co.tinode.tinodesdk.model.MsgServerInfo;
6662

6763
import okhttp3.OkHttpClient;
6864
import okhttp3.Request;
@@ -207,68 +203,6 @@ public void onReceive(Context context, Intent intent) {
207203
editor.putBoolean(Utils.PREFS_USE_TLS, getDefaultTLS());
208204
editor.apply();
209205
}
210-
// Event handlers for video calls.
211-
Cache.getTinode().addListener(new Tinode.EventListener() {
212-
@Override
213-
public void onDataMessage(MsgServerData data) {
214-
if (Cache.getTinode().isMe(data.from)) {
215-
return;
216-
}
217-
String webrtc = data.getStringHeader("webrtc");
218-
if (MsgServerData.parseWebRTC(webrtc) != MsgServerData.WebRTC.STARTED) {
219-
return;
220-
}
221-
ComTopic topic = (ComTopic) Cache.getTinode().getTopic(data.topic);
222-
if (topic == null) {
223-
return;
224-
}
225-
226-
// Check if we have a later version of the message (which means the call
227-
// has been not yet either accepted or finished).
228-
Storage.Message msg = topic.getMessage(data.seq);
229-
if (msg != null) {
230-
webrtc = msg.getStringHeader("webrtc");
231-
if (webrtc != null && MsgServerData.parseWebRTC(webrtc) != MsgServerData.WebRTC.STARTED) {
232-
return;
233-
}
234-
}
235-
236-
CallInProgress call = Cache.getCallInProgress();
237-
if (call == null) {
238-
CallManager.acceptIncomingCall(TindroidApp.this,
239-
data.topic, data.seq, data.getBooleanHeader("aonly"));
240-
} else if (!call.equals(data.topic, data.seq)) {
241-
// Another incoming call. Decline.
242-
topic.videoCallHangUp(data.seq);
243-
}
244-
}
245-
246-
@Override
247-
public void onInfoMessage(MsgServerInfo info) {
248-
if (MsgServerInfo.parseWhat(info.what) != MsgServerInfo.What.CALL) {
249-
return;
250-
}
251-
252-
CallInProgress call = Cache.getCallInProgress();
253-
if (call == null || !call.equals(info.src, info.seq) || !Tinode.TOPIC_ME.equals(info.topic)) {
254-
return;
255-
}
256-
257-
// Dismiss call notification.
258-
// Hang-up event received or current user accepted the call from another device.
259-
if (MsgServerInfo.parseEvent(info.event) == MsgServerInfo.Event.HANG_UP ||
260-
(Cache.getTinode().isMe(info.from) &&
261-
MsgServerInfo.parseEvent(info.event) == MsgServerInfo.Event.ACCEPT)) {
262-
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(TindroidApp.this);
263-
final Intent intent = new Intent(TindroidApp.this,
264-
HangUpBroadcastReceiver.class);
265-
intent.setAction(Const.INTENT_ACTION_CALL_CLOSE);
266-
intent.putExtra(Const.INTENT_EXTRA_TOPIC, info.src);
267-
intent.putExtra(Const.INTENT_EXTRA_SEQ, info.seq);
268-
lbm.sendBroadcast(intent);
269-
}
270-
}
271-
});
272206

273207
// Clear completed/failed upload tasks.
274208
WorkManager.getInstance(this).pruneWork();

0 commit comments

Comments
 (0)