@Override public void run() { lockSendRequestsToService.lock(); try { final StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("[SpiceManager : "); stringBuilder.append("Requests to be launched : \n"); dumpMap(stringBuilder, mapRequestToLaunchToRequestListener); stringBuilder.append("Pending requests : \n"); dumpMap(stringBuilder, mapPendingRequestToRequestListener); stringBuilder.append(']'); waitForServiceToBeBound(); if (spiceService == null) { return; } spiceService.dumpState(); } catch (final InterruptedException e) { Ln.e(e, "Interrupted while waiting for acquiring service."); } finally { lockSendRequestsToService.unlock(); } } });
/** * Remove all listeners of a request that may have already been passed to * the {@link SpiceService}. If the request has already been passed to the * {@link SpiceService}, the method will bind to the service and ask it to * remove listeners. * @param request * the request for which listeners must be unregistered. */ private void removeListenersOfPendingCachedRequest(final SpiceRequest<?> request) throws InterruptedException { synchronized (mapPendingRequestToRequestListener) { for (final CachedSpiceRequest<?> cachedSpiceRequest : mapPendingRequestToRequestListener.keySet()) { if (match(cachedSpiceRequest, request)) { waitForServiceToBeBound(); if (spiceService == null) { return; } final Set<RequestListener<?>> setRequestListeners = mapPendingRequestToRequestListener.get(cachedSpiceRequest); spiceService.dontNotifyRequestListenersForRequest(cachedSpiceRequest, setRequestListeners); mapPendingRequestToRequestListener.remove(cachedSpiceRequest); break; } } } }
@Override public T call() { try { spiceManager.waitForServiceToBeBound(); if (spiceManager.spiceService == null) { return null; } spiceManager.lockSendRequestsToService.lock(); try { if (spiceManager.spiceService == null || spiceManager.isStopped) { return null; } T result = executeWhenBound(spiceManager.spiceService); successFull = true; return result; } catch (Exception e) { Ln.e(e); this.exception = e; return null; } finally { spiceManager.lockSendRequestsToService.unlock(); } } catch (InterruptedException e) { Ln.e(e, "Spice command %s couldn't bind to service.", getClass().getName()); return null; } }
@Override public void run() { if (!tryToStartService()) { Ln.d("Service was not started as Activity died prematurely"); isStopped = true; return; } bindToService(); try { waitForServiceToBeBound(); if (spiceService == null) { Ln.d("No spice service bound."); return; } while (!requestQueue.isEmpty() || !isStopped && !Thread.interrupted()) { try { sendRequestToService(requestQueue.take()); } catch (final InterruptedException ex) { Ln.d("Interrupted while waiting for new request."); // we receive an interrupted exception while waiting // see java spec : http://stackoverflow.com/a/6699006/693752 break; } } Ln.d("SpiceManager request runner terminated. Requests count: %d, stopped %b, interrupted %b", requestQueue.size(), isStopped, Thread.interrupted()); } catch (final InterruptedException e) { Ln.d(e, "Interrupted while waiting for acquiring service."); } }