Skip to content

Commit 8c267d1

Browse files
committed
do not unsubscribe from topic in onPause()
1 parent d569c50 commit 8c267d1

File tree

5 files changed

+81
-49
lines changed

5 files changed

+81
-49
lines changed

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

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import co.tinode.tindroid.db.BaseDb;
1010
import co.tinode.tindroid.media.VxCard;
11+
import co.tinode.tinodesdk.ComTopic;
1112
import co.tinode.tinodesdk.FndTopic;
1213
import co.tinode.tinodesdk.MeTopic;
1314
import co.tinode.tinodesdk.PromisedReply;
@@ -21,60 +22,87 @@
2122
public class Cache {
2223
private static final String API_KEY = "AQEAAAABAAD_rAp4DJh05a1HAwFT3A6K";
2324

24-
private static Tinode sTinode = null;
25+
private static final Cache sInstance = new Cache();
2526

26-
// Current video call.
27-
private static CallInProgress sCallInProgress = null;
27+
private Tinode mTinode = null;
2828

29-
public static CallInProgress getCallInProgress() {
30-
return sCallInProgress;
31-
}
29+
// Currently active topic.
30+
private String mTopicSelected = null;
3231

33-
public static void registerCallInProgress(String topic, int seq) {
34-
sCallInProgress = new CallInProgress(topic, seq);
35-
}
36-
37-
public static void unregisterCallInProgress() {
38-
sCallInProgress = null;
39-
}
32+
// Current video call.
33+
private CallInProgress mCallInProgress = null;
4034

41-
public static Tinode getTinode() {
42-
if (sTinode == null) {
43-
sTinode = new Tinode("Tindroid/" + TindroidApp.getAppVersion(), API_KEY,
35+
public static synchronized Tinode getTinode() {
36+
if (sInstance.mTinode == null) {
37+
sInstance.mTinode = new Tinode("Tindroid/" + TindroidApp.getAppVersion(), API_KEY,
4438
BaseDb.getInstance().getStore(), null);
45-
sTinode.setOsString(Build.VERSION.RELEASE);
39+
sInstance.mTinode.setOsString(Build.VERSION.RELEASE);
4640

4741
// Default types for parsing Public, Private fields of messages
48-
sTinode.setDefaultTypeOfMetaPacket(VxCard.class, PrivateType.class);
49-
sTinode.setMeTypeOfMetaPacket(VxCard.class);
50-
sTinode.setFndTypeOfMetaPacket(VxCard.class);
42+
sInstance.mTinode.setDefaultTypeOfMetaPacket(VxCard.class, PrivateType.class);
43+
sInstance.mTinode.setMeTypeOfMetaPacket(VxCard.class);
44+
sInstance.mTinode.setFndTypeOfMetaPacket(VxCard.class);
5145

5246
// Set device language
53-
sTinode.setLanguage(Locale.getDefault().toString());
47+
sInstance.mTinode.setLanguage(Locale.getDefault().toString());
5448

5549
// Keep in app to prevent garbage collection.
56-
TindroidApp.retainTinodeCache(sTinode);
50+
TindroidApp.retainCache(sInstance);
5751
}
5852

5953
FirebaseMessaging fbId = FirebaseMessaging.getInstance();
6054
//noinspection ConstantConditions: Google lies about getInstance not returning null.
6155
if (fbId != null) {
6256
fbId.getToken().addOnSuccessListener(token -> {
63-
if (sTinode != null) {
64-
sTinode.setDeviceToken(token);
57+
if (sInstance.mTinode != null) {
58+
sInstance.mTinode.setDeviceToken(token);
6559
}
6660
});
6761
}
68-
return sTinode;
62+
return sInstance.mTinode;
6963
}
7064

7165
// Invalidate existing cache.
7266
static void invalidate() {
73-
if (sTinode != null) {
74-
sTinode.logout();
75-
sTinode = null;
76-
FirebaseMessaging.getInstance().deleteToken();
67+
unregisterCallInProgress();
68+
setSelectedTopicName(null);
69+
if (sInstance.mTinode != null) {
70+
sInstance.mTinode.logout();
71+
sInstance.mTinode = null;
7772
}
73+
FirebaseMessaging.getInstance().deleteToken();
74+
}
75+
76+
public static CallInProgress getCallInProgress() {
77+
return sInstance.mCallInProgress;
78+
}
79+
80+
public static void registerCallInProgress(String topic, int seq) {
81+
sInstance.mCallInProgress = new CallInProgress(topic, seq);
82+
}
83+
84+
public static void unregisterCallInProgress() {
85+
sInstance.mCallInProgress = null;
86+
}
87+
88+
public static String getSelectedTopicName() {
89+
return sInstance.mTopicSelected;
90+
}
91+
92+
// Save the new topic name. If the old topic is not null, unsubscribe.
93+
public static void setSelectedTopicName(String topicName) {
94+
String oldTopic = sInstance.mTopicSelected;
95+
sInstance.mTopicSelected = topicName;
96+
if (sInstance.mTinode != null && oldTopic != null && !oldTopic.equals(topicName)) {
97+
ComTopic topic = (ComTopic) sInstance.mTinode.getTopic(oldTopic);
98+
if (topic != null) {
99+
topic.leave();
100+
}
101+
}
102+
}
103+
104+
public static void setServer(String serverHost, boolean useTLS) {
105+
getTinode().setServer(serverHost, useTLS);
78106
}
79107

80108
// Connect to 'me' topic.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ public void onResume() {
8989
mTinodeListener = new ContactsEventListener(tinode.isConnected());
9090
tinode.addListener(mTinodeListener);
9191

92+
Cache.setSelectedTopicName(null);
93+
9294
UiUtils.setupToolbar(this, null, null, false, null, false);
9395

9496
if (!mMeTopic.isAttached()) {

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public void onResume() {
220220

221221
// Get the topic name from intent, internal or external.
222222
if (!changeTopic(readTopicNameFromIntent(intent), false)) {
223+
Cache.setSelectedTopicName(null);
223224
finish();
224225
return;
225226
}
@@ -259,6 +260,7 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
259260
}
260261

261262
// Topic has changed. Update all the views with the new data.
263+
// Returns 'true' if topic was successfully changed, false otherwise.
262264
boolean changeTopic(String topicName, boolean forceReset) {
263265
if (TextUtils.isEmpty(topicName)) {
264266
Log.w(TAG, "Failed to switch topics: empty topic name");
@@ -284,7 +286,9 @@ boolean changeTopic(String topicName, boolean forceReset) {
284286
mTopic = topic;
285287
boolean changed = false;
286288
if (mTopicName == null || !mTopicName.equals(topicName)) {
289+
Cache.setSelectedTopicName(topicName);
287290
mTopicName = topicName;
291+
288292
changed = true;
289293

290294
if (mTopic == null) {
@@ -437,7 +441,7 @@ private void topicAttach(boolean interactive) {
437441
.thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
438442
@Override
439443
public PromisedReply<ServerMessage> onSuccess(ServerMessage result) {
440-
if (result.ctrl.code == 303) {
444+
if (result.ctrl != null && result.ctrl.code == 303) {
441445
// Redirect.
442446
changeTopic(result.ctrl.getStringParam("topic", null), false);
443447
return null;
@@ -487,11 +491,6 @@ private void topicDetach() {
487491

488492
if (mTopic != null) {
489493
mTopic.setListener(null);
490-
491-
// Deactivate current topic
492-
if (mTopic.isAttached()) {
493-
mTopic.leave();
494-
}
495494
}
496495
UiUtils.setVisibleTopic(null);
497496
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class TindroidApp extends Application implements DefaultLifecycleObserver
7272
private static ContentObserver sContactsObserver = null;
7373

7474
// The Tinode cache is linked from here so it's never garbage collected.
75-
private static Tinode sTinodeCache;
75+
private static Cache sCache;
7676

7777
private static String sAppVersion = null;
7878
private static int sAppBuild = 0;
@@ -106,9 +106,9 @@ public static boolean getDefaultTLS() {
106106
return !isEmulator();
107107
}
108108

109-
public static void retainTinodeCache(Tinode tinode) {
110-
sTinodeCache = tinode;
111-
sTinodeCache.setServer(sServerHost, sUseTLS);
109+
public static void retainCache(Cache cache) {
110+
sCache = cache;
111+
Cache.setServer(sServerHost, sUseTLS);
112112
}
113113

114114
// Detect if the code is running in an emulator.
@@ -177,7 +177,7 @@ public void onCreate() {
177177
public void onReceive(Context context, Intent intent) {
178178
String token = intent.getStringExtra("token");
179179
if (token != null && !token.equals("")) {
180-
sTinodeCache.setDeviceToken(token);
180+
Cache.getTinode().setDeviceToken(token);
181181
}
182182
}
183183
};
@@ -205,14 +205,14 @@ public void onReceive(Context context, Intent intent) {
205205
Cache.getTinode().addListener(new Tinode.EventListener() {
206206
@Override
207207
public void onDataMessage(MsgServerData data) {
208-
if (sTinodeCache.isMe(data.from)) {
208+
if (Cache.getTinode().isMe(data.from)) {
209209
return;
210210
}
211211
String webrtc = data.getStringHeader("webrtc");
212212
if (MsgServerData.parseWebRTC(webrtc) != MsgServerData.WebRTC.STARTED) {
213213
return;
214214
}
215-
ComTopic topic = (ComTopic) sTinodeCache.getTopic(data.topic);
215+
ComTopic topic = (ComTopic) Cache.getTinode().getTopic(data.topic);
216216
if (topic == null) {
217217
return;
218218
}
@@ -253,7 +253,7 @@ public void onInfoMessage(MsgServerInfo info) {
253253
}
254254

255255
CallInProgress call = Cache.getCallInProgress();
256-
if (Tinode.TOPIC_ME.equals(info.topic) && sTinodeCache.isMe(info.from) &&
256+
if (Tinode.TOPIC_ME.equals(info.topic) && Cache.getTinode().isMe(info.from) &&
257257
call != null && call.equals(info.src, info.seq)) {
258258
// Another client has accepted the call. Dismiss call notification.
259259
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(TindroidApp.this);
@@ -274,8 +274,8 @@ public void onInfoMessage(MsgServerInfo info) {
274274
.addInterceptor(chain -> {
275275
Request picassoReq = chain.request();
276276
Map<String, String> headers;
277-
if (sTinodeCache.isTrustedURL(picassoReq.url().url()) &&
278-
(headers = sTinodeCache.getRequestHeaders()) != null) {
277+
if (Cache.getTinode().isTrustedURL(picassoReq.url().url()) &&
278+
(headers = Cache.getTinode().getRequestHeaders()) != null) {
279279
Request.Builder builder = picassoReq.newBuilder();
280280
for (Map.Entry<String, String> el : headers.entrySet()) {
281281
builder = builder.addHeader(el.getKey(), el.getValue());
@@ -301,7 +301,7 @@ public void onInfoMessage(MsgServerInfo info) {
301301
@Override
302302
public void onAvailable(@NonNull Network network) {
303303
super.onAvailable(network);
304-
sTinodeCache.reconnectNow(true, false, false);
304+
Cache.getTinode().reconnectNow(true, false, false);
305305
}
306306
});
307307
}
@@ -328,8 +328,8 @@ public void onStart(@NonNull LifecycleOwner owner) {
328328
@Override
329329
public void onStop(@NonNull LifecycleOwner owner) {
330330
// Disconnect now, so the connection does not wait for the timeout.
331-
if (sTinodeCache != null) {
332-
sTinodeCache.disconnect(false);
331+
if (Cache.getTinode() != null) {
332+
Cache.getTinode().disconnect(false);
333333
}
334334
}
335335

app/src/main/java/co/tinode/tindroid/services/FBaseMessagingService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import co.tinode.tindroid.ChatsActivity;
3030
import co.tinode.tindroid.MessageActivity;
3131
import co.tinode.tindroid.R;
32+
import co.tinode.tindroid.TindroidApp;
3233
import co.tinode.tindroid.UiUtils;
3334
import co.tinode.tindroid.account.Utils;
3435
import co.tinode.tindroid.format.FontFormatter;
@@ -127,7 +128,9 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
127128

128129
// Update data state, maybe fetch missing data.
129130
String token = Utils.getLoginToken(getApplicationContext());
130-
tinode.oobNotification(data, token, "started".equals(webrtc));
131+
String selectedTopic = Cache.getSelectedTopicName();
132+
tinode.oobNotification(data, token, "started".equals(webrtc) ||
133+
topicName.equals(selectedTopic));
131134

132135
if (Boolean.parseBoolean(data.get("silent"))) {
133136
// TODO: cancel some notifications.

0 commit comments

Comments
 (0)