Program: 43469
Project: 43469
Buggy source:
/** 
 * Wait for the last event dispatch to complete, then find the next target that should receive the given event and wait for that one to be ready to receive it.
 */
Object waitForNextEventTarget(KeyEvent nextKey,QueuedEvent qev,MotionEvent nextMotion,boolean isPointerEvent,boolean failIfTimeout){
  long startTime=SystemClock.uptimeMillis();
  long keyDispatchingTimeout=5 * 1000;
  long waitedFor=0;
  while (true) {
    WindowState targetWin=mLastWin;
    boolean targetIsNew=targetWin == null;
    if (DEBUG_INPUT)     Log.v(TAG,"String_Node_Str" + mFinished + "String_Node_Str"+ mLastWin);
    if (targetIsNew) {
      Object target=findTargetWindow(nextKey,qev,nextMotion,isPointerEvent);
      if (target == SKIP_TARGET_TOKEN) {
        if (DEBUG_INPUT)         Log.v(TAG,"String_Node_Str" + nextKey + "String_Node_Str"+ nextMotion);
        return null;
      }
      if (target == CONSUMED_EVENT_TOKEN) {
        if (DEBUG_INPUT)         Log.v(TAG,"String_Node_Str" + nextKey + "String_Node_Str"+ nextMotion);
        return target;
      }
      targetWin=(WindowState)target;
    }
    AppWindowToken targetApp=null;
synchronized (this) {
      if (!targetIsNew && mLastWin == null) {
        continue;
      }
      if (mFinished && !mDisplayFrozen) {
        if (!mEventDispatching) {
          if (DEBUG_INPUT)           Log.v(TAG,"String_Node_Str" + nextKey + "String_Node_Str"+ nextMotion);
          return null;
        }
        if (targetWin != null) {
          if (targetIsNew && !targetWin.mToken.paused) {
            return targetWin;
          }
        }
 else         if (mFocusedApp == null) {
          if (DEBUG_INPUT)           Log.v(TAG,"String_Node_Str" + nextKey + "String_Node_Str"+ nextMotion);
          return null;
        }
      }
      if (DEBUG_INPUT)       Log.v(TAG,"String_Node_Str" + mLastBinder + "String_Node_Str"+ targetWin+ "String_Node_Str"+ mFinished+ "String_Node_Str"+ mDisplayFrozen+ "String_Node_Str"+ targetIsNew+ "String_Node_Str"+ (targetWin != null ? targetWin.mToken.paused : false)+ "String_Node_Str"+ mFocusedApp+ "String_Node_Str"+ mCurrentFocus);
      targetApp=targetWin != null ? targetWin.mAppToken : mFocusedApp;
      long curTimeout=keyDispatchingTimeout;
      if (mTimeToSwitch != 0) {
        long now=SystemClock.uptimeMillis();
        if (mTimeToSwitch <= now) {
          doFinishedKeyLocked(true);
          continue;
        }
        long switchTimeout=mTimeToSwitch - now;
        if (curTimeout > switchTimeout) {
          curTimeout=switchTimeout;
        }
      }
      try {
        if (DEBUG_INPUT)         Log.v(TAG,"String_Node_Str" + curTimeout);
        wait(curTimeout);
        if (DEBUG_INPUT)         Log.v(TAG,"String_Node_Str" + SystemClock.uptimeMillis() + "String_Node_Str"+ startTime+ "String_Node_Str"+ mTimeToSwitch+ "String_Node_Str"+ targetWin+ "String_Node_Str"+ mLastWin+ "String_Node_Str"+ mLastBinder+ "String_Node_Str"+ mFinished+ "String_Node_Str"+ mCurrentFocus);
      }
 catch (      InterruptedException e) {
      }
    }
    if (mWasFrozen) {
      waitedFor=0;
      mWasFrozen=false;
    }
 else {
      waitedFor=SystemClock.uptimeMillis() - startTime;
    }
    if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
      IApplicationToken at=null;
synchronized (this) {
        Log.w(TAG,"String_Node_Str" + (targetWin != null ? targetWin.mAttrs.getTitle() : "String_Node_Str"));
        Log.w(TAG,"String_Node_Str" + mDispatchState);
        Log.w(TAG,"String_Node_Str" + new DispatchState(nextKey,targetWin));
        if (targetWin != null) {
          at=targetWin.getAppToken();
        }
 else         if (targetApp != null) {
          at=targetApp.appToken;
        }
      }
      boolean abort=true;
      if (at != null) {
        try {
          long timeout=at.getKeyDispatchingTimeout();
          if (timeout > waitedFor) {
            keyDispatchingTimeout=timeout - waitedFor;
            continue;
          }
 else {
            abort=at.keyDispatchingTimedOut();
          }
        }
 catch (        RemoteException ex) {
        }
      }
synchronized (this) {
        if (abort && (mLastWin == targetWin || targetWin == null)) {
          mFinished=true;
          if (mLastWin != null) {
            if (DEBUG_INPUT)             Log.v(TAG,"String_Node_Str" + mLastWin + "String_Node_Str");
            if (mLastWin.mToken.paused) {
              Log.w(TAG,"String_Node_Str");
              mLastWin.mToken.paused=false;
            }
          }
          if (mMotionTarget == targetWin) {
            mMotionTarget=null;
          }
          mLastWin=null;
          mLastBinder=null;
          if (failIfTimeout || targetWin == null) {
            return null;
          }
        }
 else {
          Log.w(TAG,"String_Node_Str");
          startTime=SystemClock.uptimeMillis();
        }
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Wait for the last event dispatch to complete, then find the next target that should receive the given event and wait for that one to be ready to receive it.
 */
Object waitForNextEventTarget(KeyEvent nextKey,QueuedEvent qev,MotionEvent nextMotion,boolean isPointerEvent,boolean failIfTimeout,int callingPid,int callingUid){
  long startTime=SystemClock.uptimeMillis();
  long keyDispatchingTimeout=5 * 1000;
  long waitedFor=0;
  while (true) {
    WindowState targetWin=mLastWin;
    boolean targetIsNew=targetWin == null;
    if (DEBUG_INPUT)     Log.v(TAG,"String_Node_Str" + mFinished + "String_Node_Str"+ mLastWin);
    if (targetIsNew) {
      Object target=findTargetWindow(nextKey,qev,nextMotion,isPointerEvent,callingPid,callingUid);
      if (target == SKIP_TARGET_TOKEN) {
        if (DEBUG_INPUT)         Log.v(TAG,"String_Node_Str" + nextKey + "String_Node_Str"+ nextMotion);
        return null;
      }
      if (target == CONSUMED_EVENT_TOKEN) {
        if (DEBUG_INPUT)         Log.v(TAG,"String_Node_Str" + nextKey + "String_Node_Str"+ nextMotion);
        return target;
      }
      targetWin=(WindowState)target;
    }
    AppWindowToken targetApp=null;
synchronized (this) {
      if (!targetIsNew && mLastWin == null) {
        continue;
      }
      if (mFinished && !mDisplayFrozen) {
        if (!mEventDispatching) {
          if (DEBUG_INPUT)           Log.v(TAG,"String_Node_Str" + nextKey + "String_Node_Str"+ nextMotion);
          return null;
        }
        if (targetWin != null) {
          if (targetIsNew && !targetWin.mToken.paused) {
            return targetWin;
          }
        }
 else         if (mFocusedApp == null) {
          if (DEBUG_INPUT)           Log.v(TAG,"String_Node_Str" + nextKey + "String_Node_Str"+ nextMotion);
          return null;
        }
      }
      if (DEBUG_INPUT)       Log.v(TAG,"String_Node_Str" + mLastBinder + "String_Node_Str"+ targetWin+ "String_Node_Str"+ mFinished+ "String_Node_Str"+ mDisplayFrozen+ "String_Node_Str"+ targetIsNew+ "String_Node_Str"+ (targetWin != null ? targetWin.mToken.paused : false)+ "String_Node_Str"+ mFocusedApp+ "String_Node_Str"+ mCurrentFocus);
      targetApp=targetWin != null ? targetWin.mAppToken : mFocusedApp;
      long curTimeout=keyDispatchingTimeout;
      if (mTimeToSwitch != 0) {
        long now=SystemClock.uptimeMillis();
        if (mTimeToSwitch <= now) {
          doFinishedKeyLocked(true);
          continue;
        }
        long switchTimeout=mTimeToSwitch - now;
        if (curTimeout > switchTimeout) {
          curTimeout=switchTimeout;
        }
      }
      try {
        if (DEBUG_INPUT)         Log.v(TAG,"String_Node_Str" + curTimeout);
        wait(curTimeout);
        if (DEBUG_INPUT)         Log.v(TAG,"String_Node_Str" + SystemClock.uptimeMillis() + "String_Node_Str"+ startTime+ "String_Node_Str"+ mTimeToSwitch+ "String_Node_Str"+ targetWin+ "String_Node_Str"+ mLastWin+ "String_Node_Str"+ mLastBinder+ "String_Node_Str"+ mFinished+ "String_Node_Str"+ mCurrentFocus);
      }
 catch (      InterruptedException e) {
      }
    }
    if (mWasFrozen) {
      waitedFor=0;
      mWasFrozen=false;
    }
 else {
      waitedFor=SystemClock.uptimeMillis() - startTime;
    }
    if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
      IApplicationToken at=null;
synchronized (this) {
        Log.w(TAG,"String_Node_Str" + (targetWin != null ? targetWin.mAttrs.getTitle() : "String_Node_Str"));
        Log.w(TAG,"String_Node_Str" + mDispatchState);
        Log.w(TAG,"String_Node_Str" + new DispatchState(nextKey,targetWin));
        if (targetWin != null) {
          at=targetWin.getAppToken();
        }
 else         if (targetApp != null) {
          at=targetApp.appToken;
        }
      }
      boolean abort=true;
      if (at != null) {
        try {
          long timeout=at.getKeyDispatchingTimeout();
          if (timeout > waitedFor) {
            keyDispatchingTimeout=timeout - waitedFor;
            continue;
          }
 else {
            abort=at.keyDispatchingTimedOut();
          }
        }
 catch (        RemoteException ex) {
        }
      }
synchronized (this) {
        if (abort && (mLastWin == targetWin || targetWin == null)) {
          mFinished=true;
          if (mLastWin != null) {
            if (DEBUG_INPUT)             Log.v(TAG,"String_Node_Str" + mLastWin + "String_Node_Str");
            if (mLastWin.mToken.paused) {
              Log.w(TAG,"String_Node_Str");
              mLastWin.mToken.paused=false;
            }
          }
          if (mMotionTarget == targetWin) {
            mMotionTarget=null;
          }
          mLastWin=null;
          mLastBinder=null;
          if (failIfTimeout || targetWin == null) {
            return null;
          }
        }
 else {
          Log.w(TAG,"String_Node_Str");
          startTime=SystemClock.uptimeMillis();
        }
      }
    }
  }
}