From 5142f978cf02c9532c9842389deebcfb206ce6ef Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 23 Feb 2016 23:47:49 -0500 Subject: [PATCH] Fixed polling resuming in the background in some cases --- app/src/main/java/com/limelight/AppView.java | 6 +++- app/src/main/java/com/limelight/PcView.java | 30 +++++++++----------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/limelight/AppView.java b/app/src/main/java/com/limelight/AppView.java index ec372401..934353a4 100644 --- a/app/src/main/java/com/limelight/AppView.java +++ b/app/src/main/java/com/limelight/AppView.java @@ -50,6 +50,7 @@ public class AppView extends Activity implements AdapterFragmentCallbacks { private String lastRawApplist; private int lastRunningAppId; private boolean suspendGridUpdates; + private boolean inForeground; private final static int START_OR_RESUME_ID = 1; private final static int QUIT_ID = 2; @@ -108,7 +109,8 @@ public class AppView extends Activity implements AdapterFragmentCallbacks { }; private void startComputerUpdates() { - if (managerBinder == null) { + // Don't start polling if we're not bound or in the foreground + if (managerBinder == null || !inForeground) { return; } @@ -252,6 +254,7 @@ public class AppView extends Activity implements AdapterFragmentCallbacks { protected void onResume() { super.onResume(); + inForeground = true; startComputerUpdates(); } @@ -259,6 +262,7 @@ public class AppView extends Activity implements AdapterFragmentCallbacks { protected void onPause() { super.onPause(); + inForeground = false; stopComputerUpdates(); } diff --git a/app/src/main/java/com/limelight/PcView.java b/app/src/main/java/com/limelight/PcView.java index 07e5f4dc..848a75ee 100644 --- a/app/src/main/java/com/limelight/PcView.java +++ b/app/src/main/java/com/limelight/PcView.java @@ -53,7 +53,7 @@ public class PcView extends Activity implements AdapterFragmentCallbacks { private RelativeLayout noPcFoundLayout; private PcGridAdapter pcGridAdapter; private ComputerManagerService.ComputerManagerBinder managerBinder; - private boolean freezeUpdates, runningPolling, hasResumed; + private boolean freezeUpdates, runningPolling, inForeground; private final ServiceConnection serviceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder binder) { final ComputerManagerService.ComputerManagerBinder localBinder = @@ -161,11 +161,9 @@ public class PcView extends Activity implements AdapterFragmentCallbacks { } private void startComputerUpdates() { - if (managerBinder != null) { - if (runningPolling) { - return; - } - + // Only allow polling to start if we're bound to CMS, polling is not already running, + // and our activity is in the foreground. + if (managerBinder != null && !runningPolling && inForeground) { freezeUpdates = false; managerBinder.startPolling(new ComputerManagerListener() { @Override @@ -215,7 +213,7 @@ public class PcView extends Activity implements AdapterFragmentCallbacks { protected void onResume() { super.onResume(); - hasResumed = true; + inForeground = true; startComputerUpdates(); } @@ -223,7 +221,7 @@ public class PcView extends Activity implements AdapterFragmentCallbacks { protected void onPause() { super.onPause(); - hasResumed = false; + inForeground = false; stopComputerUpdates(false); } @@ -271,10 +269,9 @@ public class PcView extends Activity implements AdapterFragmentCallbacks { @Override public void onContextMenuClosed(Menu menu) { // For some reason, this gets called again _after_ onPause() is called on this activity. - // We don't want to start computer updates again, so we need to keep track of whether we're paused. - if (hasResumed) { - startComputerUpdates(); - } + // startComputerUpdates() manages this and won't actual start polling until the activity + // returns to the foreground. + startComputerUpdates(); } private void doPair(final ComputerDetails computer) { @@ -368,14 +365,15 @@ public class PcView extends Activity implements AdapterFragmentCallbacks { } if (toastSuccess) { - // Open the app list after a successful pairing attemp + // Open the app list after a successful pairing attempt doAppList(computer); } + else { + // Start polling again if we're still in the foreground + startComputerUpdates(); + } } }); - - // Start polling again - startComputerUpdates(); } }).start(); }