Fixed polling resuming in the background in some cases

This commit is contained in:
Cameron Gutman
2016-02-23 23:47:49 -05:00
parent 667ffd4dfd
commit 5142f978cf
2 changed files with 19 additions and 17 deletions

View File

@@ -50,6 +50,7 @@ public class AppView extends Activity implements AdapterFragmentCallbacks {
private String lastRawApplist; private String lastRawApplist;
private int lastRunningAppId; private int lastRunningAppId;
private boolean suspendGridUpdates; private boolean suspendGridUpdates;
private boolean inForeground;
private final static int START_OR_RESUME_ID = 1; private final static int START_OR_RESUME_ID = 1;
private final static int QUIT_ID = 2; private final static int QUIT_ID = 2;
@@ -108,7 +109,8 @@ public class AppView extends Activity implements AdapterFragmentCallbacks {
}; };
private void startComputerUpdates() { private void startComputerUpdates() {
if (managerBinder == null) { // Don't start polling if we're not bound or in the foreground
if (managerBinder == null || !inForeground) {
return; return;
} }
@@ -252,6 +254,7 @@ public class AppView extends Activity implements AdapterFragmentCallbacks {
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
inForeground = true;
startComputerUpdates(); startComputerUpdates();
} }
@@ -259,6 +262,7 @@ public class AppView extends Activity implements AdapterFragmentCallbacks {
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
inForeground = false;
stopComputerUpdates(); stopComputerUpdates();
} }

View File

@@ -53,7 +53,7 @@ public class PcView extends Activity implements AdapterFragmentCallbacks {
private RelativeLayout noPcFoundLayout; private RelativeLayout noPcFoundLayout;
private PcGridAdapter pcGridAdapter; private PcGridAdapter pcGridAdapter;
private ComputerManagerService.ComputerManagerBinder managerBinder; private ComputerManagerService.ComputerManagerBinder managerBinder;
private boolean freezeUpdates, runningPolling, hasResumed; private boolean freezeUpdates, runningPolling, inForeground;
private final ServiceConnection serviceConnection = new ServiceConnection() { private final ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) { public void onServiceConnected(ComponentName className, IBinder binder) {
final ComputerManagerService.ComputerManagerBinder localBinder = final ComputerManagerService.ComputerManagerBinder localBinder =
@@ -161,11 +161,9 @@ public class PcView extends Activity implements AdapterFragmentCallbacks {
} }
private void startComputerUpdates() { private void startComputerUpdates() {
if (managerBinder != null) { // Only allow polling to start if we're bound to CMS, polling is not already running,
if (runningPolling) { // and our activity is in the foreground.
return; if (managerBinder != null && !runningPolling && inForeground) {
}
freezeUpdates = false; freezeUpdates = false;
managerBinder.startPolling(new ComputerManagerListener() { managerBinder.startPolling(new ComputerManagerListener() {
@Override @Override
@@ -215,7 +213,7 @@ public class PcView extends Activity implements AdapterFragmentCallbacks {
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
hasResumed = true; inForeground = true;
startComputerUpdates(); startComputerUpdates();
} }
@@ -223,7 +221,7 @@ public class PcView extends Activity implements AdapterFragmentCallbacks {
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
hasResumed = false; inForeground = false;
stopComputerUpdates(false); stopComputerUpdates(false);
} }
@@ -271,10 +269,9 @@ public class PcView extends Activity implements AdapterFragmentCallbacks {
@Override @Override
public void onContextMenuClosed(Menu menu) { public void onContextMenuClosed(Menu menu) {
// For some reason, this gets called again _after_ onPause() is called on this activity. // 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. // startComputerUpdates() manages this and won't actual start polling until the activity
if (hasResumed) { // returns to the foreground.
startComputerUpdates(); startComputerUpdates();
}
} }
private void doPair(final ComputerDetails computer) { private void doPair(final ComputerDetails computer) {
@@ -368,14 +365,15 @@ public class PcView extends Activity implements AdapterFragmentCallbacks {
} }
if (toastSuccess) { if (toastSuccess) {
// Open the app list after a successful pairing attemp // Open the app list after a successful pairing attempt
doAppList(computer); doAppList(computer);
} }
else {
// Start polling again if we're still in the foreground
startComputerUpdates();
}
} }
}); });
// Start polling again
startComputerUpdates();
} }
}).start(); }).start();
} }