Skip to content

Commit 8b46410

Browse files
author
Chris Bellew
committed
When resuming app, if subscribed to a plex client and it's been more than 60 seconds since the last heartbeat was received, resubscribe to the client.
1 parent c6d3438 commit 8b46410

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

mobile/src/main/java/com/atomjack/vcfp/PlexSubscription.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.net.ServerSocket;
3030
import java.net.Socket;
3131
import java.net.SocketException;
32+
import java.util.Calendar;
3233
import java.util.Date;
3334
import java.util.HashMap;
3435
import java.util.List;
@@ -59,6 +60,8 @@ public class PlexSubscription {
5960
private int failedHeartbeats = 0;
6061
private final int failedHeartbeatMax = 5;
6162

63+
private Calendar lastHeartbeatResponded;
64+
6265
private Handler mHandler;
6366

6467
private PlayerState currentState = PlayerState.STOPPED;
@@ -244,6 +247,11 @@ public void run() {
244247
serverThread.start();
245248
}
246249

250+
public void resubscribe() {
251+
if(mClient != null)
252+
subscribe(mClient);
253+
}
254+
247255
public void subscribe(final PlexClient client) {
248256
Logger.d("PlexSubscription subscribe: %s, handler is null: %s", client, updateConversationHandler == null);
249257
if(updateConversationHandler == null)
@@ -284,6 +292,8 @@ public void onSuccess(PlexResponse response) {
284292
mHandler.removeCallbacks(subscriptionHeartbeat);
285293
mHandler.postDelayed(subscriptionHeartbeat, SUBSCRIBE_INTERVAL);
286294
onSubscribed();
295+
} else {
296+
lastHeartbeatResponded = Calendar.getInstance();
287297
}
288298
}
289299
}
@@ -537,4 +547,7 @@ public void subtitlesOff() {
537547
nowPlayingMedia.setActiveStream(nowPlayingMedia.getStreams(Stream.SUBTITLE).get(0));
538548
}
539549

550+
public Calendar getLastHeartbeatResponded() {
551+
return lastHeartbeatResponded;
552+
}
540553
}

mobile/src/main/java/com/atomjack/vcfp/activities/MainActivity.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@
117117
import java.io.OutputStreamWriter;
118118
import java.io.Writer;
119119
import java.net.SocketTimeoutException;
120+
import java.text.SimpleDateFormat;
120121
import java.util.ArrayList;
122+
import java.util.Calendar;
121123
import java.util.Collections;
122124
import java.util.Date;
123125
import java.util.HashMap;
@@ -540,6 +542,17 @@ protected void onResume() {
540542

541543
plexSubscription.setListener(plexSubscriptionListener);
542544
castPlayerManager.setListener(plexSubscriptionListener);
545+
546+
if(plexSubscription.isSubscribed() && plexSubscription.getLastHeartbeatResponded() != null) {
547+
Calendar cal = Calendar.getInstance();
548+
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy 'at' h:mm:ss a");
549+
cal.add(Calendar.SECOND, -60);
550+
if(plexSubscription.getLastHeartbeatResponded().before(cal)) {
551+
Logger.d("It's been more than 60 seconds since last heartbeat responded. now: %s, last heartbeat responded: %s", format.format(Calendar.getInstance().getTime()), format.format(plexSubscription.getLastHeartbeatResponded().getTime()));
552+
plexSubscription.resubscribe();
553+
}
554+
}
555+
543556
if(!doingFirstTimeSetup) {
544557
mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
545558
} else

0 commit comments

Comments
 (0)