Skip to content

Commit 209659c

Browse files
author
Chris Bellew
committed
Fixed crash caused by watching media on a client when already subscribed to another client, caused by switching to the main fragment on resume when we're not subscribed (needed behavior when unsubscribing via the notification). The fix was to not switch back to the main fragment if we're in the process of subscribing, since the app gets launched before subscribing to the client is finished.
1 parent cf87d21 commit 209659c

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public static final class RECEIVER_EVENTS {
119119
// private CastListener listener;
120120

121121
private boolean subscribed = false;
122+
private boolean subscribing = false;
122123

123124
public PlexClient mClient;
124125

@@ -137,9 +138,8 @@ public void setContext(Context context) {
137138
mContext = context;
138139
}
139140

140-
141-
142141
public void subscribe(final PlexClient _client, boolean showFeedback) {
142+
subscribing = true;
143143
subscribe(_client, null, showFeedback);
144144
}
145145

@@ -160,6 +160,7 @@ public void subscribe(final PlexClient _client, final Runnable onFinished, final
160160
@Override
161161
public void run() {
162162
mClient = _client;
163+
163164
// currentState = castManager.getPlaybackStatus();
164165
Logger.d("castConsumer connected to %s", mClient.name);
165166
getPlaybackState();
@@ -188,7 +189,7 @@ public void run() {
188189
} catch (Exception ex) {}
189190

190191

191-
192+
subscribing = false;
192193
subscribed = true;
193194
if(listener != null)
194195
listener.onSubscribed(_client, showFeedback);
@@ -206,6 +207,14 @@ public boolean isSubscribed() {
206207
return subscribed && mClient != null;
207208
}
208209

210+
public boolean isSubscribing() {
211+
return subscribing;
212+
}
213+
214+
public PlexClient getClient() {
215+
return mClient;
216+
}
217+
209218
public void unsubscribe() {
210219
try {
211220
Logger.d("is connected: %s", castManager.isConnected());

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class PlexSubscription {
5555
private int commandId = 1;
5656
private int subscriptionPort = 59409;
5757
private boolean subscribed = false;
58+
private boolean subscribing = false;
5859
private ServerSocket serverSocket;
5960
Thread serverThread = null;
6061
Handler updateConversationHandler;
@@ -105,6 +106,10 @@ public boolean isSubscribed() {
105106
return subscribed && mClient != null;
106107
}
107108

109+
public boolean isSubscribing() {
110+
return subscribing;
111+
}
112+
108113
class ServerThread implements Runnable {
109114
Runnable onReady;
110115

@@ -260,6 +265,7 @@ public void resubscribe() {
260265

261266
public void subscribe(final PlexClient client, boolean showFeedback) {
262267
Logger.d("PlexSubscription subscribe: %s, handler is null: %s", client, updateConversationHandler == null);
268+
subscribing = true;
263269
if(updateConversationHandler == null)
264270
startSubscription(client, showFeedback);
265271
else
@@ -274,6 +280,7 @@ public void subscribe(PlexClient client, final boolean isHeartbeat, final boolea
274280
PlexHttpClient.subscribe(client, subscriptionPort, commandId, VoiceControlForPlexApplication.getInstance().getUUID(), VoiceControlForPlexApplication.getInstance().getString(R.string.app_name), new PlexHttpResponseHandler() {
275281
@Override
276282
public void onSuccess(PlexResponse response) {
283+
subscribing = false;
277284
failedHeartbeats = 0;
278285
if(!isHeartbeat)
279286
Logger.d("PlexSubscription: Subscribed: %s, Code: %d", response != null ? response.status : "", response.code);
@@ -307,6 +314,7 @@ public void onSuccess(PlexResponse response) {
307314
@Override
308315
public void onFailure(final Throwable error) {
309316
error.printStackTrace();
317+
subscribing = false;
310318
if(isHeartbeat) {
311319
failedHeartbeats++;
312320
Logger.d("%d failed heartbeats", failedHeartbeats);

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ public void onPlayStarted(PlexMedia media, ArrayList<? extends PlexMedia> playli
399399
logger.d("onPlayStarted: %s", media.getTitle());
400400
handler.removeCallbacks(autoDisconnectPlayerTimer);
401401
int layout = getLayoutForMedia(media, state);
402-
logger.d("layout: %d", layout);
403402
if(layout != -1) {
404403
playerFragment.init(layout, client, media, playlist, false);
405404
switchToPlayerFragment();
@@ -416,6 +415,10 @@ public void onStateChanged(PlexMedia media, PlayerState state) {
416415
logger.d("onStopped");
417416
VoiceControlForPlexApplication.getInstance().cancelNotification();
418417
switchToMainFragment();
418+
if(plexSubscription.isSubscribed() && plexSubscription.getClient() != gsonRead.fromJson(prefs.get(Preferences.CLIENT, ""), PlexClient.class))
419+
plexSubscription.unsubscribe();
420+
if(castPlayerManager.isSubscribed() && castPlayerManager.getClient() != gsonRead.fromJson(prefs.get(Preferences.CLIENT, ""), PlexClient.class))
421+
castPlayerManager.unsubscribe();
419422
} else {
420423
playerFragment.setState(state);
421424
}
@@ -607,6 +610,8 @@ protected void onPause() {
607610
super.onPause();
608611
logger.d("onPause");
609612
handler.removeCallbacks(autoDisconnectPlayerTimer);
613+
plexSubscription.setListener(null);
614+
castPlayerManager.setListener(null);
610615
VoiceControlForPlexApplication.applicationPaused();
611616
if (isFinishing() && mMediaRouter != null) {
612617
mMediaRouter.removeCallback(mMediaRouterCallback);
@@ -725,8 +730,10 @@ protected void onResume() {
725730
}
726731

727732
// If we get unsubscribed from the notification, and the app isn't visible, the next time we show up the app will think
728-
// it's still subscribed, so we have to set the UI to be unsubbed
729-
if(!isSubscribed() && !doingFirstTimeSetup) {
733+
// it's still subscribed, so we have to set the UI to be unsubbed. Also, need to make sure we're not in the middle of subscribing,
734+
// as that will happen when a voice search is done to play something - this activity will be launched before the subscribe
735+
// process is done. If this isn't checked, we end up switching to the main fragment when we should stay with the player fragment, and crash.
736+
if(!isSubscribed() && !isSubscribing() && !doingFirstTimeSetup) {
730737
switchToMainFragment();
731738
setCastIconInactive();
732739
prefs.remove(Preferences.SUBSCRIBED_CLIENT);
@@ -1685,6 +1692,10 @@ private boolean isSubscribed() {
16851692
return plexSubscription.isSubscribed() || castPlayerManager.isSubscribed() || VoiceControlForPlexApplication.getInstance().localClientSubscription.subscribed;
16861693
}
16871694

1695+
private boolean isSubscribing() {
1696+
return plexSubscription.isSubscribing() || castPlayerManager.isSubscribing();
1697+
}
1698+
16881699
private Runnable onClientRefreshFinished = null;
16891700
private Runnable onServerRefreshFinished = null;
16901701

mobile/src/main/java/com/atomjack/vcfp/fragments/PlayerFragment.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ public void init(int layout, PlexClient client, PlexMedia media, ArrayList<? ext
173173
this.client = client;
174174
nowPlayingMedia = media;
175175
nowPlayingPlaylist = playlist == null ? new ArrayList<PlexMedia>() : playlist;
176-
logger.d("Got %d items in playlist", playlist.size());
177176
this.fromWear = fromWear;
178177
currentMediaIndex = 0;
179178
}
@@ -306,12 +305,12 @@ public void onGlobalLayout() {
306305

307306
@Override
308307
public void onResume() {
308+
super.onResume();
309309
logger.d("onResume");
310310
if(doingMic) {
311311
doPlay();
312312
doingMic = false;
313313
}
314-
super.onResume();
315314
}
316315

317316
private void setThumb(final byte[] bytes) {
@@ -651,27 +650,17 @@ public void onStartTrackingTouch(SeekBar seekBar) {
651650
}
652651

653652
public void setState(PlayerState newState) {
654-
if(currentState != newState) {
655-
currentState = newState;
656-
if (playPauseSpinner != null && playButton != null && pauseButton != null) {
657-
playPauseSpinner.setVisibility(View.INVISIBLE);
658-
playButton.setVisibility(View.INVISIBLE);
659-
pauseButton.setVisibility(View.INVISIBLE);
660-
if (currentState == PlayerState.PAUSED) {
661-
playButton.setVisibility(View.VISIBLE);
662-
} else if (currentState == PlayerState.PLAYING) {
663-
pauseButton.setVisibility(View.VISIBLE);
664-
} else {
665-
playPauseSpinner.setVisibility(View.VISIBLE);
666-
}
667-
}
653+
// logger.d("setState: %s, current state: %s", newState, currentState);
654+
currentState = newState;
655+
if (playPauseSpinner != null && playButton != null && pauseButton != null) {
656+
playPauseSpinner.setVisibility(currentState == PlayerState.BUFFERING ? View.VISIBLE : View.INVISIBLE);
657+
playButton.setVisibility(currentState == PlayerState.PAUSED ? View.VISIBLE : View.INVISIBLE);
658+
pauseButton.setVisibility(currentState == PlayerState.PLAYING ? View.VISIBLE : View.INVISIBLE);
668659
}
669660
}
670661

671662
public void setPosition(int position) {
672663
if(!isSeeking) {
673-
// if(position != this.position)
674-
// logger.d("setting position to %d", position);
675664
this.position = position;
676665
if (seekBar != null)
677666
seekBar.setProgress(position);

0 commit comments

Comments
 (0)