Program: 14399
Project: 14399
Buggy source:
@Override public void onActivityResult(int requestCode,int resultCode,Intent data){
  super.onActivityResult(requestCode,resultCode,data);
  if (data != null || ((requestCode == MediaUtils.RequestCode.ACTIVITY_REQUEST_CODE_TAKE_PHOTO || requestCode == MediaUtils.RequestCode.ACTIVITY_REQUEST_CODE_TAKE_VIDEO))) {
    Bundle extras;
switch (requestCode) {
case MediaPickerActivity.ACTIVITY_REQUEST_CODE_MEDIA_SELECTION:
      if (resultCode == MediaPickerActivity.ACTIVITY_RESULT_CODE_MEDIA_SELECTED) {
        handleMediaSelectionResult(data);
      }
 else       if (resultCode == MediaPickerActivity.ACTIVITY_RESULT_CODE_GALLERY_CREATED) {
        mGalleryIds=new ArrayList<>();
        mUploadingMedia=new ArrayList<>();
        mMediaIdToPath=new HashMap<>();
        handleGalleryResult(data);
      }
    break;
case MediaGalleryActivity.REQUEST_CODE:
  if (resultCode == Activity.RESULT_OK) {
    handleMediaGalleryResult(data);
  }
break;
case MediaUtils.RequestCode.ACTIVITY_REQUEST_CODE_PICTURE_LIBRARY:
Uri imageUri=data.getData();
fetchMedia(imageUri);
AnalyticsTracker.track(Stat.EDITOR_ADDED_PHOTO_VIA_LOCAL_LIBRARY);
break;
case MediaUtils.RequestCode.ACTIVITY_REQUEST_CODE_VIDEO_LIBRARY:
Uri videoUri=data.getData();
fetchMedia(videoUri);
break;
case ACTIVITY_REQUEST_CODE_CREATE_LINK:
try {
extras=data.getExtras();
if (extras == null) return;
String linkURL=extras.getString("String_Node_Str");
if (linkURL != null && !linkURL.equals("String_Node_Str") && !linkURL.equals("String_Node_Str")) {
if (mSelectionStart > mSelectionEnd) {
int temp=mSelectionEnd;
mSelectionEnd=mSelectionStart;
mSelectionStart=temp;
}
Editable str=mContentEditText.getText();
if (str == null) return;
if (mActivity.getPost().isLocalDraft()) {
if (extras.getString("String_Node_Str") == null) {
if (mSelectionStart < mSelectionEnd) str.delete(mSelectionStart,mSelectionEnd);
str.insert(mSelectionStart,linkURL);
str.setSpan(new URLSpan(linkURL),mSelectionStart,mSelectionStart + linkURL.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mContentEditText.setSelection(mSelectionStart + linkURL.length());
}
 else {
String linkText=extras.getString("String_Node_Str");
if (linkText == null) return;
if (mSelectionStart < mSelectionEnd) str.delete(mSelectionStart,mSelectionEnd);
str.insert(mSelectionStart,linkText);
str.setSpan(new URLSpan(linkURL),mSelectionStart,mSelectionStart + linkText.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mContentEditText.setSelection(mSelectionStart + linkText.length());
}
}
 else {
if (extras.getString("String_Node_Str") == null) {
if (mSelectionStart < mSelectionEnd) str.delete(mSelectionStart,mSelectionEnd);
String urlHTML="String_Node_Str" + linkURL + "String_Node_Str"+ linkURL+ "String_Node_Str";
str.insert(mSelectionStart,urlHTML);
mContentEditText.setSelection(mSelectionStart + urlHTML.length());
}
 else {
String linkText=extras.getString("String_Node_Str");
if (mSelectionStart < mSelectionEnd) str.delete(mSelectionStart,mSelectionEnd);
String urlHTML="String_Node_Str" + linkURL + "String_Node_Str"+ linkText+ "String_Node_Str";
str.insert(mSelectionStart,urlHTML);
mContentEditText.setSelection(mSelectionStart + urlHTML.length());
}
}
}
}
 catch (RuntimeException e) {
AppLog.e(T.POSTS,e);
}
break;
}
}
}

Fixed version (ground truth):
@Override public void onActivityResult(int requestCode,int resultCode,Intent data){
  super.onActivityResult(requestCode,resultCode,data);
  if (data != null || ((requestCode == MediaUtils.RequestCode.ACTIVITY_REQUEST_CODE_TAKE_PHOTO || requestCode == MediaUtils.RequestCode.ACTIVITY_REQUEST_CODE_TAKE_VIDEO))) {
    Bundle extras;
switch (requestCode) {
case MediaPickerActivity.ACTIVITY_REQUEST_CODE_MEDIA_SELECTION:
      if (resultCode == MediaPickerActivity.ACTIVITY_RESULT_CODE_MEDIA_SELECTED) {
        handleMediaSelectionResult(data);
      }
 else       if (resultCode == MediaPickerActivity.ACTIVITY_RESULT_CODE_GALLERY_CREATED) {
        mGalleryIds=new ArrayList<>();
        mUploadingMedia=new ArrayList<>();
        mMediaIdToPath=new HashMap<>();
        handleGalleryResult(data);
      }
    break;
case MediaGalleryActivity.REQUEST_CODE:
  if (resultCode == Activity.RESULT_OK) {
    handleMediaGalleryResult(data);
  }
break;
case MediaUtils.RequestCode.ACTIVITY_REQUEST_CODE_PICTURE_LIBRARY:
Uri imageUri=data.getData();
fetchMedia(imageUri);
AnalyticsTracker.track(Stat.EDITOR_ADDED_PHOTO_VIA_LOCAL_LIBRARY);
break;
case MediaUtils.RequestCode.ACTIVITY_REQUEST_CODE_VIDEO_LIBRARY:
Uri videoUri=data.getData();
fetchMedia(videoUri);
break;
case MediaUtils.RequestCode.ACTIVITY_REQUEST_CODE_TAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
try {
File f=new File(mMediaCapturePath);
Uri capturedImageUri=Uri.fromFile(f);
if (!addMedia(capturedImageUri,null,getActivity())) Toast.makeText(getActivity(),getResources().getText(R.string.gallery_error),Toast.LENGTH_SHORT).show();
getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("String_Node_Str" + Environment.getExternalStorageDirectory())));
AnalyticsTracker.track(Stat.EDITOR_ADDED_PHOTO_VIA_LOCAL_LIBRARY);
}
 catch (RuntimeException e) {
AppLog.e(T.POSTS,e);
}
catch (OutOfMemoryError e) {
AppLog.e(T.POSTS,e);
}
}
 else if (mActivity != null && mQuickMediaType > -1 && TextUtils.isEmpty(mContentEditText.getText())) {
WordPress.wpDB.deletePost(mActivity.getPost());
mActivity.finish();
}
break;
case ACTIVITY_REQUEST_CODE_CREATE_LINK:
try {
extras=data.getExtras();
if (extras == null) return;
String linkURL=extras.getString("String_Node_Str");
if (linkURL != null && !linkURL.equals("String_Node_Str") && !linkURL.equals("String_Node_Str")) {
if (mSelectionStart > mSelectionEnd) {
int temp=mSelectionEnd;
mSelectionEnd=mSelectionStart;
mSelectionStart=temp;
}
Editable str=mContentEditText.getText();
if (str == null) return;
if (mActivity.getPost().isLocalDraft()) {
if (extras.getString("String_Node_Str") == null) {
if (mSelectionStart < mSelectionEnd) str.delete(mSelectionStart,mSelectionEnd);
str.insert(mSelectionStart,linkURL);
str.setSpan(new URLSpan(linkURL),mSelectionStart,mSelectionStart + linkURL.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mContentEditText.setSelection(mSelectionStart + linkURL.length());
}
 else {
String linkText=extras.getString("String_Node_Str");
if (linkText == null) return;
if (mSelectionStart < mSelectionEnd) str.delete(mSelectionStart,mSelectionEnd);
str.insert(mSelectionStart,linkText);
str.setSpan(new URLSpan(linkURL),mSelectionStart,mSelectionStart + linkText.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mContentEditText.setSelection(mSelectionStart + linkText.length());
}
}
 else {
if (extras.getString("String_Node_Str") == null) {
if (mSelectionStart < mSelectionEnd) str.delete(mSelectionStart,mSelectionEnd);
String urlHTML="String_Node_Str" + linkURL + "String_Node_Str"+ linkURL+ "String_Node_Str";
str.insert(mSelectionStart,urlHTML);
mContentEditText.setSelection(mSelectionStart + urlHTML.length());
}
 else {
String linkText=extras.getString("String_Node_Str");
if (mSelectionStart < mSelectionEnd) str.delete(mSelectionStart,mSelectionEnd);
String urlHTML="String_Node_Str" + linkURL + "String_Node_Str"+ linkText+ "String_Node_Str";
str.insert(mSelectionStart,urlHTML);
mContentEditText.setSelection(mSelectionStart + urlHTML.length());
}
}
}
}
 catch (RuntimeException e) {
AppLog.e(T.POSTS,e);
}
break;
}
}
}