Program: 21883
Project: 21883
Buggy source:
@Override protected void onFocusChanged(boolean focused,int direction,Rect previouslyFocusedRect){
  if (mTemporaryDetach) {
    super.onFocusChanged(focused,direction,previouslyFocusedRect);
    return;
  }
  mShowCursor=SystemClock.uptimeMillis();
  ensureEndedBatchEdit();
  if (focused) {
    int selStart=getSelectionStart();
    int selEnd=getSelectionEnd();
    if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) {
      if (mLastTouchOffset >= 0) {
        mLastTouchOffset=Math.min(mLastTouchOffset,mText.length());
        Selection.setSelection((Spannable)mText,mLastTouchOffset);
      }
      if (mMovement != null) {
        mMovement.onTakeFocus(this,(Spannable)mText,direction);
      }
      if (mSelectAllOnFocus) {
        Selection.setSelection((Spannable)mText,0,mText.length());
      }
      if (((this instanceof ExtractEditText) || mSelectionMoved) && selStart >= 0 && selEnd >= 0) {
        Selection.setSelection((Spannable)mText,selStart,selEnd);
      }
      mTouchFocusSelected=true;
    }
    mFrozenWithFocus=false;
    mSelectionMoved=false;
    if (mText instanceof Spannable) {
      Spannable sp=(Spannable)mText;
      MetaKeyKeyListener.resetMetaState(sp);
    }
    makeBlink();
    if (mError != null) {
      showError();
    }
  }
 else {
    if (mError != null) {
      hideError();
    }
    onEndBatchEdit();
    hideInsertionPointCursorController();
    if (this instanceof ExtractEditText) {
      mIsInTextSelectionMode=false;
    }
 else {
      terminateTextSelectionMode();
    }
  }
  startStopMarquee(focused);
  if (mTransformation != null) {
    mTransformation.onFocusChanged(this,mText,focused,direction,previouslyFocusedRect);
  }
  super.onFocusChanged(focused,direction,previouslyFocusedRect);
}

Fixed version (ground truth):
@Override protected void onFocusChanged(boolean focused,int direction,Rect previouslyFocusedRect){
  if (mTemporaryDetach) {
    super.onFocusChanged(focused,direction,previouslyFocusedRect);
    return;
  }
  mShowCursor=SystemClock.uptimeMillis();
  ensureEndedBatchEdit();
  if (focused) {
    int selStart=getSelectionStart();
    int selEnd=getSelectionEnd();
    if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) {
      if (mLastTouchOffset >= 0) {
        mLastTouchOffset=Math.min(mLastTouchOffset,mText.length());
        Selection.setSelection((Spannable)mText,mLastTouchOffset);
      }
      if (mMovement != null) {
        mMovement.onTakeFocus(this,(Spannable)mText,direction);
      }
      if (mSelectAllOnFocus) {
        Selection.setSelection((Spannable)mText,0,mText.length());
      }
      if (((this instanceof ExtractEditText) || mSelectionMoved) && selStart >= 0 && selEnd >= 0) {
        Selection.setSelection((Spannable)mText,selStart,selEnd);
      }
      mTouchFocusSelected=true;
    }
    mFrozenWithFocus=false;
    mSelectionMoved=false;
    if (mText instanceof Spannable) {
      Spannable sp=(Spannable)mText;
      MetaKeyKeyListener.resetMetaState(sp);
    }
    makeBlink();
    if (mError != null) {
      showError();
    }
  }
 else {
    if (mError != null) {
      hideError();
    }
    onEndBatchEdit();
    hideInsertionPointCursorController();
    if (this instanceof ExtractEditText) {
      mIsInTextSelectionMode=false;
    }
 else {
      terminateTextSelectionMode();
    }
    mLastTouchOffset=-1;
  }
  startStopMarquee(focused);
  if (mTransformation != null) {
    mTransformation.onFocusChanged(this,mText,focused,direction,previouslyFocusedRect);
  }
  super.onFocusChanged(focused,direction,previouslyFocusedRect);
}