Program: 14946
Project: 14946
Buggy source:
/** 
 * Rich Text Editor
 */
@Override public boolean onTouch(View v,MotionEvent event){
  float pos=event.getY();
  if (event.getAction() == 0)   mLastYPos=pos;
  if (event.getAction() > 1) {
    int scrollThreshold=DisplayUtils.dpToPx(getActivity(),2);
    if (((mLastYPos - pos) > scrollThreshold) || ((pos - mLastYPos) > scrollThreshold))     mScrollDetected=true;
  }
  mLastYPos=pos;
  if (event.getAction() == MotionEvent.ACTION_UP) {
    if (mActivity != null && mActivity.getSupportActionBar().isShowing()) {
      setContentEditingModeVisible(true);
      return false;
    }
  }
  if (event.getAction() == MotionEvent.ACTION_UP && !mScrollDetected) {
    Layout layout=((TextView)v).getLayout();
    int x=(int)event.getX();
    int y=(int)event.getY();
    x+=v.getScrollX();
    y+=v.getScrollY();
    if (layout != null) {
      int line=layout.getLineForVertical(y);
      int charPosition=layout.getOffsetForHorizontal(line,x);
      final Spannable s=mContentEditText.getText();
      if (s == null)       return false;
      WPImageSpan[] image_spans=s.getSpans(charPosition,charPosition,WPImageSpan.class);
      if (image_spans.length != 0) {
        final WPImageSpan span=image_spans[0];
        MediaFile mediaFile=span.getMediaFile();
        if (mediaFile == null)         return false;
        if (!mediaFile.isVideo()) {
          LayoutInflater factory=LayoutInflater.from(getActivity());
          final View alertView=factory.inflate(R.layout.alert_image_options,null);
          if (alertView == null)           return false;
          final EditText imageWidthText=(EditText)alertView.findViewById(R.id.imageWidthText);
          final EditText titleText=(EditText)alertView.findViewById(R.id.title);
          final EditText caption=(EditText)alertView.findViewById(R.id.caption);
          final CheckBox featuredCheckBox=(CheckBox)alertView.findViewById(R.id.featuredImage);
          final CheckBox featuredInPostCheckBox=(CheckBox)alertView.findViewById(R.id.featuredInPost);
          if (WordPress.getCurrentBlog().isFeaturedImageCapable()) {
            featuredCheckBox.setVisibility(View.VISIBLE);
            featuredInPostCheckBox.setVisibility(View.VISIBLE);
          }
          featuredCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override public void onCheckedChanged(            CompoundButton buttonView,            boolean isChecked){
              if (isChecked) {
                featuredInPostCheckBox.setVisibility(View.VISIBLE);
              }
 else {
                featuredInPostCheckBox.setVisibility(View.GONE);
              }
            }
          }
);
          final SeekBar seekBar=(SeekBar)alertView.findViewById(R.id.imageWidth);
          final Spinner alignmentSpinner=(Spinner)alertView.findViewById(R.id.alignment_spinner);
          ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(getActivity(),R.array.alignment_array,android.R.layout.simple_spinner_item);
          adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          alignmentSpinner.setAdapter(adapter);
          imageWidthText.setText(String.valueOf(mediaFile.getWidth()) + "String_Node_Str");
          seekBar.setProgress(mediaFile.getWidth());
          titleText.setText(mediaFile.getTitle());
          caption.setText(mediaFile.getCaption());
          featuredCheckBox.setChecked(mediaFile.isFeatured());
          if (mediaFile.isFeatured())           featuredInPostCheckBox.setVisibility(View.VISIBLE);
 else           featuredInPostCheckBox.setVisibility(View.GONE);
          featuredInPostCheckBox.setChecked(mediaFile.isFeaturedInPost());
          alignmentSpinner.setSelection(mediaFile.getHorizontalAlignment(),true);
          final int maxWidth=MediaUtils.getMinimumImageWitdh(getActivity(),span.getImageSource());
          seekBar.setMax(maxWidth / 10);
          if (mediaFile.getWidth() != 0)           seekBar.setProgress(mediaFile.getWidth() / 10);
          seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
            @Override public void onStopTrackingTouch(            SeekBar seekBar){
            }
            @Override public void onStartTrackingTouch(            SeekBar seekBar){
            }
            @Override public void onProgressChanged(            SeekBar seekBar,            int progress,            boolean fromUser){
              if (progress == 0)               progress=1;
              imageWidthText.setText(progress * 10 + "String_Node_Str");
            }
          }
);
          imageWidthText.setOnFocusChangeListener(new View.OnFocusChangeListener(){
            @Override public void onFocusChange(            View v,            boolean hasFocus){
              if (hasFocus) {
                imageWidthText.setText("String_Node_Str");
              }
            }
          }
);
          imageWidthText.setOnEditorActionListener(new TextView.OnEditorActionListener(){
            @Override public boolean onEditorAction(            TextView v,            int actionId,            KeyEvent event){
              int width=getEditTextIntegerClamped(imageWidthText,10,maxWidth);
              seekBar.setProgress(width / 10);
              imageWidthText.setSelection((String.valueOf(width).length()));
              InputMethodManager imm=(InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(imageWidthText.getWindowToken(),InputMethodManager.RESULT_UNCHANGED_SHOWN);
              return true;
            }
          }
);
          AlertDialog ad=new AlertDialog.Builder(getActivity()).setTitle(getString(R.string.image_settings)).setView(alertView).setPositiveButton(getString(R.string.ok),new DialogInterface.OnClickListener(){
            public void onClick(            DialogInterface dialog,            int whichButton){
              String title=(titleText.getText() != null) ? titleText.getText().toString() : "String_Node_Str";
              MediaFile mediaFile=span.getMediaFile();
              if (mediaFile == null)               return;
              mediaFile.setTitle(title);
              mediaFile.setHorizontalAlignment(alignmentSpinner.getSelectedItemPosition());
              mediaFile.setWidth(getEditTextIntegerClamped(imageWidthText,10,maxWidth));
              String captionText=(caption.getText() != null) ? caption.getText().toString() : "String_Node_Str";
              mediaFile.setCaption(captionText);
              mediaFile.setFeatured(featuredCheckBox.isChecked());
              if (featuredCheckBox.isChecked()) {
                WPImageSpan[] click_spans=s.getSpans(0,s.length(),WPImageSpan.class);
                if (click_spans.length > 1) {
                  for (                  WPImageSpan verifySpan : click_spans) {
                    if (verifySpan != span) {
                      MediaFile verifySpanMediaFile=verifySpan.getMediaFile();
                      verifySpanMediaFile.setFeatured(false);
                      verifySpanMediaFile.setFeaturedInPost(false);
                    }
                  }
                }
              }
              mediaFile.setFeaturedInPost(featuredInPostCheckBox.isChecked());
            }
          }
).setNegativeButton(getString(R.string.cancel),new DialogInterface.OnClickListener(){
            public void onClick(            DialogInterface dialog,            int whichButton){
              dialog.dismiss();
            }
          }
).create();
          ad.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
          ad.show();
          mScrollDetected=false;
          return true;
        }
      }
 else {
        mContentEditText.setMovementMethod(ArrowKeyMovementMethod.getInstance());
        int selectionStart=mContentEditText.getSelectionStart();
        if (selectionStart >= 0 && mContentEditText.getSelectionEnd() >= selectionStart)         mContentEditText.setSelection(selectionStart,mContentEditText.getSelectionEnd());
      }
      MediaGalleryImageSpan[] gallerySpans=s.getSpans(charPosition,charPosition,MediaGalleryImageSpan.class);
      if (gallerySpans.length > 0) {
        final MediaGalleryImageSpan gallerySpan=gallerySpans[0];
        startMediaGalleryActivity(gallerySpan.getMediaGallery());
      }
    }
  }
 else   if (event.getAction() == 1) {
    mScrollDetected=false;
  }
  return false;
}

Fixed version (ground truth):
/** 
 * Rich Text Editor
 */
@Override public boolean onTouch(View v,MotionEvent event){
  float pos=event.getY();
  if (event.getAction() == 0)   mLastYPos=pos;
  if (event.getAction() > 1) {
    int scrollThreshold=DisplayUtils.dpToPx(getActivity(),2);
    if (((mLastYPos - pos) > scrollThreshold) || ((pos - mLastYPos) > scrollThreshold))     mScrollDetected=true;
  }
  mLastYPos=pos;
  if (event.getAction() == MotionEvent.ACTION_UP) {
    if (mActivity != null && mActivity.getSupportActionBar().isShowing()) {
      setContentEditingModeVisible(true);
      return false;
    }
  }
  if (event.getAction() == MotionEvent.ACTION_UP && !mScrollDetected) {
    Layout layout=((TextView)v).getLayout();
    int x=(int)event.getX();
    int y=(int)event.getY();
    x+=v.getScrollX();
    y+=v.getScrollY();
    if (layout != null) {
      int line=layout.getLineForVertical(y);
      int charPosition=layout.getOffsetForHorizontal(line,x);
      Spannable s=mContentEditText.getText();
      if (s == null)       return false;
      WPImageSpan[] image_spans=s.getSpans(charPosition,charPosition,WPImageSpan.class);
      if (image_spans.length != 0) {
        final WPImageSpan span=image_spans[0];
        MediaFile mediaFile=span.getMediaFile();
        if (mediaFile == null)         return false;
        if (!mediaFile.isVideo()) {
          LayoutInflater factory=LayoutInflater.from(getActivity());
          final View alertView=factory.inflate(R.layout.alert_image_options,null);
          if (alertView == null)           return false;
          final EditText imageWidthText=(EditText)alertView.findViewById(R.id.imageWidthText);
          final EditText titleText=(EditText)alertView.findViewById(R.id.title);
          final EditText caption=(EditText)alertView.findViewById(R.id.caption);
          final CheckBox featuredCheckBox=(CheckBox)alertView.findViewById(R.id.featuredImage);
          final CheckBox featuredInPostCheckBox=(CheckBox)alertView.findViewById(R.id.featuredInPost);
          if (WordPress.getCurrentBlog().isFeaturedImageCapable()) {
            featuredCheckBox.setVisibility(View.VISIBLE);
            featuredInPostCheckBox.setVisibility(View.VISIBLE);
          }
          featuredCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override public void onCheckedChanged(            CompoundButton buttonView,            boolean isChecked){
              if (isChecked) {
                featuredInPostCheckBox.setVisibility(View.VISIBLE);
              }
 else {
                featuredInPostCheckBox.setVisibility(View.GONE);
              }
            }
          }
);
          final SeekBar seekBar=(SeekBar)alertView.findViewById(R.id.imageWidth);
          final Spinner alignmentSpinner=(Spinner)alertView.findViewById(R.id.alignment_spinner);
          ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(getActivity(),R.array.alignment_array,android.R.layout.simple_spinner_item);
          adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          alignmentSpinner.setAdapter(adapter);
          imageWidthText.setText(String.valueOf(mediaFile.getWidth()) + "String_Node_Str");
          seekBar.setProgress(mediaFile.getWidth());
          titleText.setText(mediaFile.getTitle());
          caption.setText(mediaFile.getCaption());
          featuredCheckBox.setChecked(mediaFile.isFeatured());
          if (mediaFile.isFeatured())           featuredInPostCheckBox.setVisibility(View.VISIBLE);
 else           featuredInPostCheckBox.setVisibility(View.GONE);
          featuredInPostCheckBox.setChecked(mediaFile.isFeaturedInPost());
          alignmentSpinner.setSelection(mediaFile.getHorizontalAlignment(),true);
          final int maxWidth=MediaUtils.getMinimumImageWitdh(getActivity(),span.getImageSource());
          seekBar.setMax(maxWidth / 10);
          if (mediaFile.getWidth() != 0)           seekBar.setProgress(mediaFile.getWidth() / 10);
          seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
            @Override public void onStopTrackingTouch(            SeekBar seekBar){
            }
            @Override public void onStartTrackingTouch(            SeekBar seekBar){
            }
            @Override public void onProgressChanged(            SeekBar seekBar,            int progress,            boolean fromUser){
              if (progress == 0)               progress=1;
              imageWidthText.setText(progress * 10 + "String_Node_Str");
            }
          }
);
          imageWidthText.setOnFocusChangeListener(new View.OnFocusChangeListener(){
            @Override public void onFocusChange(            View v,            boolean hasFocus){
              if (hasFocus) {
                imageWidthText.setText("String_Node_Str");
              }
            }
          }
);
          imageWidthText.setOnEditorActionListener(new TextView.OnEditorActionListener(){
            @Override public boolean onEditorAction(            TextView v,            int actionId,            KeyEvent event){
              int width=getEditTextIntegerClamped(imageWidthText,10,maxWidth);
              seekBar.setProgress(width / 10);
              imageWidthText.setSelection((String.valueOf(width).length()));
              InputMethodManager imm=(InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(imageWidthText.getWindowToken(),InputMethodManager.RESULT_UNCHANGED_SHOWN);
              return true;
            }
          }
);
          AlertDialog ad=new AlertDialog.Builder(getActivity()).setTitle(getString(R.string.image_settings)).setView(alertView).setPositiveButton(getString(R.string.ok),new DialogInterface.OnClickListener(){
            public void onClick(            DialogInterface dialog,            int whichButton){
              String title=(titleText.getText() != null) ? titleText.getText().toString() : "String_Node_Str";
              MediaFile mediaFile=span.getMediaFile();
              if (mediaFile == null)               return;
              mediaFile.setTitle(title);
              mediaFile.setHorizontalAlignment(alignmentSpinner.getSelectedItemPosition());
              mediaFile.setWidth(getEditTextIntegerClamped(imageWidthText,10,maxWidth));
              String captionText=(caption.getText() != null) ? caption.getText().toString() : "String_Node_Str";
              mediaFile.setCaption(captionText);
              mediaFile.setFeatured(featuredCheckBox.isChecked());
              if (featuredCheckBox.isChecked()) {
                Spannable contentSpannable=mContentEditText.getText();
                WPImageSpan[] postImageSpans=contentSpannable.getSpans(0,contentSpannable.length(),WPImageSpan.class);
                if (postImageSpans.length > 1) {
                  for (                  WPImageSpan postImageSpan : postImageSpans) {
                    if (postImageSpan != span) {
                      MediaFile postMediaFile=postImageSpan.getMediaFile();
                      postMediaFile.setFeatured(false);
                      postMediaFile.setFeaturedInPost(false);
                      postMediaFile.save();
                    }
                  }
                }
              }
              mediaFile.setFeaturedInPost(featuredInPostCheckBox.isChecked());
              mediaFile.save();
            }
          }
).setNegativeButton(getString(R.string.cancel),new DialogInterface.OnClickListener(){
            public void onClick(            DialogInterface dialog,            int whichButton){
              dialog.dismiss();
            }
          }
).create();
          ad.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
          ad.show();
          mScrollDetected=false;
          return true;
        }
      }
 else {
        mContentEditText.setMovementMethod(ArrowKeyMovementMethod.getInstance());
        int selectionStart=mContentEditText.getSelectionStart();
        if (selectionStart >= 0 && mContentEditText.getSelectionEnd() >= selectionStart)         mContentEditText.setSelection(selectionStart,mContentEditText.getSelectionEnd());
      }
      MediaGalleryImageSpan[] gallerySpans=s.getSpans(charPosition,charPosition,MediaGalleryImageSpan.class);
      if (gallerySpans.length > 0) {
        final MediaGalleryImageSpan gallerySpan=gallerySpans[0];
        startMediaGalleryActivity(gallerySpan.getMediaGallery());
      }
    }
  }
 else   if (event.getAction() == 1) {
    mScrollDetected=false;
  }
  return false;
}