Fix ANR when switching from the PC view to the app view

This commit is contained in:
Cameron Gutman 2014-08-05 22:30:11 -07:00
parent 829532c572
commit f892db6ee8

View File

@ -12,6 +12,7 @@ import java.util.TimerTask;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import com.limelight.LimeLog; import com.limelight.LimeLog;
import com.limelight.binding.PlatformBinding; import com.limelight.binding.PlatformBinding;
@ -39,6 +40,8 @@ public class ComputerManagerService extends Service {
private ComputerManagerBinder binder = new ComputerManagerBinder(); private ComputerManagerBinder binder = new ComputerManagerBinder();
private ComputerDatabaseManager dbManager; private ComputerDatabaseManager dbManager;
private AtomicInteger dbRefCount = new AtomicInteger(0);
private IdentityManager idManager; private IdentityManager idManager;
private ThreadPoolExecutor pollingPool; private ThreadPoolExecutor pollingPool;
private Timer pollingTimer; private Timer pollingTimer;
@ -173,15 +176,41 @@ public class ComputerManagerService extends Service {
} }
public void removeComputer(String name) { public void removeComputer(String name) {
if (!getLocalDatabaseReference()) {
return;
}
// Remove it from the database // Remove it from the database
dbManager.deleteComputer(name); dbManager.deleteComputer(name);
releaseLocalDatabaseReference();
}
private boolean getLocalDatabaseReference() {
if (dbRefCount.get() == 0) {
return false;
}
dbRefCount.incrementAndGet();
return true;
}
private void releaseLocalDatabaseReference() {
if (dbRefCount.decrementAndGet() == 0) {
dbManager.close();
}
} }
private TimerTask getTimerTask() { private TimerTask getTimerTask() {
return new TimerTask() { return new TimerTask() {
@Override @Override
public void run() { public void run() {
if (!getLocalDatabaseReference()) {
return;
}
List<ComputerDetails> computerList = dbManager.getAllComputers(); List<ComputerDetails> computerList = dbManager.getAllComputers();
releaseLocalDatabaseReference();
for (ComputerDetails computer : computerList) { for (ComputerDetails computer : computerList) {
pollingPool.execute(getPollingRunnable(computer)); pollingPool.execute(getPollingRunnable(computer));
} }
@ -356,6 +385,10 @@ public class ComputerManagerService extends Service {
public void run() { public void run() {
boolean newPc = (details.name == null); boolean newPc = (details.name == null);
if (!getLocalDatabaseReference()) {
return;
}
// Poll the machine // Poll the machine
if (!doPollMachine(details)) { if (!doPollMachine(details)) {
details.state = ComputerDetails.State.OFFLINE; details.state = ComputerDetails.State.OFFLINE;
@ -369,6 +402,7 @@ public class ComputerManagerService extends Service {
// removed after this was issued // removed after this was issued
if (dbManager.getComputerByName(details.name) == null) { if (dbManager.getComputerByName(details.name) == null) {
// It's gone // It's gone
releaseLocalDatabaseReference();
return; return;
} }
} }
@ -380,6 +414,8 @@ public class ComputerManagerService extends Service {
if (listener != null) { if (listener != null) {
listener.notifyComputerUpdated(details); listener.notifyComputerUpdated(details);
} }
releaseLocalDatabaseReference();
} }
}; };
} }
@ -400,6 +436,7 @@ public class ComputerManagerService extends Service {
// Initialize the DB // Initialize the DB
dbManager = new ComputerDatabaseManager(this); dbManager = new ComputerDatabaseManager(this);
dbRefCount.set(1);
} }
@Override @Override
@ -411,12 +448,11 @@ public class ComputerManagerService extends Service {
// Stop the thread pool // Stop the thread pool
pollingPool.shutdownNow(); pollingPool.shutdownNow();
try {
pollingPool.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
} catch (InterruptedException e) {}
// Close the DB // FIXME: Should await termination here but we have timeout issues in HttpURLConnection
dbManager.close();
// Remove the initial DB reference
releaseLocalDatabaseReference();
} }
@Override @Override