Program: 2142
Project: 2142
Buggy source:
/** 
 * Executes a command and waits for a specific accessibility event up to a given wait timeout. To detect a sequence of events one can implement a filter that keeps track of seen events of the expected sequence and returns true after the last event of that sequence is received. <p> <strong>Note:</strong> It is caller's responsibility to recycle the returned event. </p>
 * @param command The command to execute.
 * @param filter Filter that recognizes the expected event.
 * @param timeoutMillis The wait timeout in milliseconds.
 * @throws TimeoutException If the expected event is not received within the timeout.
 */
public AccessibilityEvent executeAndWaitForEvent(Runnable command,AccessibilityEventFilter filter,long timeoutMillis) throws TimeoutException {
synchronized (mLock) {
    throwIfNotConnectedLocked();
    mEventQueue.clear();
    mWaitingForEventDelivery=true;
    final long executionStartTimeMillis=SystemClock.uptimeMillis();
    command.run();
    try {
      final long startTimeMillis=SystemClock.uptimeMillis();
      while (true) {
        while (!mEventQueue.isEmpty()) {
          AccessibilityEvent event=mEventQueue.remove(0);
          if (event.getEventTime() <= executionStartTimeMillis) {
            continue;
          }
          if (filter.accept(event)) {
            return event;
          }
          event.recycle();
        }
        final long elapsedTimeMillis=SystemClock.uptimeMillis() - startTimeMillis;
        final long remainingTimeMillis=timeoutMillis - elapsedTimeMillis;
        if (remainingTimeMillis <= 0) {
          throw new TimeoutException("String_Node_Str" + timeoutMillis + "String_Node_Str");
        }
        try {
          mLock.wait(remainingTimeMillis);
        }
 catch (        InterruptedException ie) {
        }
      }
    }
  finally {
      mWaitingForEventDelivery=false;
      mEventQueue.clear();
      mLock.notifyAll();
    }
  }
}

Fixed version (ground truth):
/** 
 * Executes a command and waits for a specific accessibility event up to a given wait timeout. To detect a sequence of events one can implement a filter that keeps track of seen events of the expected sequence and returns true after the last event of that sequence is received. <p> <strong>Note:</strong> It is caller's responsibility to recycle the returned event. </p>
 * @param command The command to execute.
 * @param filter Filter that recognizes the expected event.
 * @param timeoutMillis The wait timeout in milliseconds.
 * @throws TimeoutException If the expected event is not received within the timeout.
 */
public AccessibilityEvent executeAndWaitForEvent(Runnable command,AccessibilityEventFilter filter,long timeoutMillis) throws TimeoutException {
synchronized (mLock) {
    throwIfNotConnectedLocked();
    mEventQueue.clear();
    mWaitingForEventDelivery=true;
  }
  final long executionStartTimeMillis=SystemClock.uptimeMillis();
  command.run();
synchronized (mLock) {
    try {
      final long startTimeMillis=SystemClock.uptimeMillis();
      while (true) {
        while (!mEventQueue.isEmpty()) {
          AccessibilityEvent event=mEventQueue.remove(0);
          if (event.getEventTime() < executionStartTimeMillis) {
            continue;
          }
          if (filter.accept(event)) {
            return event;
          }
          event.recycle();
        }
        final long elapsedTimeMillis=SystemClock.uptimeMillis() - startTimeMillis;
        final long remainingTimeMillis=timeoutMillis - elapsedTimeMillis;
        if (remainingTimeMillis <= 0) {
          throw new TimeoutException("String_Node_Str" + timeoutMillis + "String_Node_Str");
        }
        try {
          mLock.wait(remainingTimeMillis);
        }
 catch (        InterruptedException ie) {
        }
      }
    }
  finally {
      mWaitingForEventDelivery=false;
      mEventQueue.clear();
      mLock.notifyAll();
    }
  }
}