// Run all queued methods
  private static void flushQueuedNativeCallsLocked(final State state) {
    int lastSkipped = -1;
    for (int i = 0; i < QUEUED_CALLS.size(); i++) {
      final QueuedCall call = QUEUED_CALLS.get(i);
      if (call == null) {
        // We already handled the call.
        continue;
      }
      if (!state.isAtLeast(call.state)) {
        // The call is not ready yet; skip it.
        lastSkipped = i;
        continue;
      }
      // Mark as handled.
      QUEUED_CALLS.set(i, null);

      if (call.method == null) {
        final GeckoEvent e = (GeckoEvent) call.target;
        GeckoAppShell.notifyGeckoOfEvent(e);
        e.recycle();
        continue;
      }
      invokeMethod(call.method, call.target, call.args);
    }
    if (lastSkipped < 0) {
      // We're done here; release the memory
      QUEUED_CALLS.clear();
      QUEUED_CALLS.trimToSize();
    } else if (lastSkipped < QUEUED_CALLS.size() - 1) {
      // We skipped some; free up null entries at the end,
      // but keep all the previous entries for later.
      QUEUED_CALLS.subList(lastSkipped + 1, QUEUED_CALLS.size()).clear();
    }
  }
 public static void addPendingEvent(final GeckoEvent e) {
   synchronized (QUEUED_CALLS) {
     if (isRunning()) {
       // We may just have switched to running state.
       GeckoAppShell.notifyGeckoOfEvent(e);
       e.recycle();
     } else {
       QUEUED_CALLS.add(new QueuedCall(null, e, null, State.RUNNING));
     }
   }
 }