Program: 142342
Project: 142342
Buggy source:
@Override protected void onActivityResult(int requestCode,int resultCode,Intent intent){
  try {
switch (requestCode) {
case INIT_APP:
      if (resultCode == RESULT_CANCELED) {
        this.finish();
        return;
      }
 else       if (resultCode == RESULT_OK) {
        return;
      }
    break;
case UPGRADE_APP:
  if (resultCode == RESULT_CANCELED) {
    return;
  }
 else   if (resultCode == RESULT_OK) {
    if (intent.getBooleanExtra(CommCareSetupActivity.KEY_REQUIRE_REFRESH,true)) {
      Toast.makeText(this,Localization.get("String_Node_Str"),Toast.LENGTH_LONG).show();
      CommCareApplication._().getSession().logout();
    }
    return;
  }
break;
case MISSING_MEDIA_ACTIVITY:
if (resultCode == RESULT_CANCELED) {
this.finish();
return;
}
 else if (resultCode == RESULT_OK) {
return;
}
case REPORT_PROBLEM_ACTIVITY:
if (resultCode == RESULT_CANCELED) {
return;
}
 else if (resultCode == RESULT_OK) {
CommCareApplication._().notifyLogsPending();
refreshView();
return;
}
case LOGIN_USER:
if (resultCode == RESULT_CANCELED) {
this.finish();
return;
}
 else if (resultCode == RESULT_OK) {
if (intent.getBooleanExtra(LoginActivity.ALREADY_LOGGED_IN,false)) {
}
 else {
refreshView();
checkAndStartUnsentTask(this);
}
return;
}
break;
case GET_INCOMPLETE_FORM:
AndroidSessionWrapper currentState=CommCareApplication._().getCurrentSessionWrapper();
if (resultCode == RESULT_CANCELED) {
refreshView();
return;
}
 else if (resultCode == RESULT_OK) {
int record=intent.getIntExtra(FormRecord.STORAGE_KEY,-1);
if (record == -1) {
break;
}
FormRecord r=CommCareApplication._().getStorage(FormRecord.STORAGE_KEY,FormRecord.class).read(record);
SqlIndexedStorageUtility<SessionStateDescriptor> ssdStorage=CommCareApplication._().getStorage(SessionStateDescriptor.STORAGE_KEY,SessionStateDescriptor.class);
Vector<Integer> ssds=ssdStorage.getIDsForValue(SessionStateDescriptor.META_FORM_RECORD_ID,r.getID());
if (ssds.size() == 1) {
currentState.loadFromStateDescription(ssdStorage.read(ssds.firstElement()));
}
 else {
currentState.setFormRecordId(r.getID());
}
formEntry(platform.getFormContentUri(r.getFormNamespace()),r);
return;
}
break;
case GET_COMMAND:
currentState=CommCareApplication._().getCurrentSessionWrapper();
if (resultCode == RESULT_CANCELED) {
if (currentState.getSession().getCommand() == null) {
currentState.reset();
refreshView();
return;
}
 else {
currentState.getSession().stepBack();
break;
}
}
 else if (resultCode == RESULT_OK) {
String command=intent.getStringExtra(CommCareSession.STATE_COMMAND_ID);
currentState.getSession().setCommand(command);
break;
}
break;
case GET_CASE:
currentState=CommCareApplication._().getCurrentSessionWrapper();
if (resultCode == RESULT_CANCELED) {
currentState.getSession().stepBack();
break;
}
 else if (resultCode == RESULT_OK) {
currentState.getSession().setDatum(currentState.getSession().getNeededDatum().getDataId(),intent.getStringExtra(CommCareSession.STATE_DATUM_VAL));
if (intent.hasExtra(CallOutActivity.CALL_DURATION)) {
platform.setCallDuration(intent.getLongExtra(CallOutActivity.CALL_DURATION,0));
}
break;
}
case MODEL_RESULT:
currentState=CommCareApplication._().getCurrentSessionWrapper();
if (resultCode == 1) {
if (intent.hasExtra("String_Node_Str")) {
Throwable ex=(Throwable)intent.getSerializableExtra("String_Node_Str");
ExceptionReportTask task=new ExceptionReportTask();
task.execute(ex);
}
 else {
RuntimeException ex=new RuntimeException("String_Node_Str");
ExceptionReportTask task=new ExceptionReportTask();
task.execute(ex);
}
Toast.makeText(this,Localization.get("String_Node_Str"),Toast.LENGTH_LONG);
currentState.reset();
refreshView();
break;
}
 else if (resultCode == RESULT_OK) {
FormRecord current=currentState.getFormRecord();
if (current.getStatus() == FormRecord.STATUS_COMPLETE || current.getStatus() == FormRecord.STATUS_SAVED) {
currentState.reset();
if (wasExternal) {
this.finish();
}
refreshView();
return;
}
Uri resultInstanceURI=intent.getData();
if (resultInstanceURI == null) {
Logger.log(AndroidLogger.TYPE_ERROR_WORKFLOW,"String_Node_Str");
CommCareApplication._().reportNotificationMessage(NotificationMessageFactory.message(StockMessages.FormEntry_Unretrievable));
Toast.makeText(this,"String_Node_Str",Toast.LENGTH_LONG);
currentState.reset();
if (wasExternal) {
this.finish();
}
refreshView();
return;
}
Cursor c=managedQuery(resultInstanceURI,null,null,null,null);
boolean complete=false;
try {
complete=currentState.beginRecordTransaction(resultInstanceURI,c);
}
 catch (IllegalArgumentException iae) {
iae.printStackTrace();
CommCareApplication._().reportNotificationMessage(NotificationMessageFactory.message(StockMessages.FormEntry_Unretrievable));
Toast.makeText(this,"String_Node_Str",Toast.LENGTH_LONG);
Logger.log(AndroidLogger.TYPE_ERROR_WORKFLOW,"String_Node_Str" + iae.getMessage());
currentState.reset();
if (wasExternal) {
this.finish();
}
refreshView();
return;
}
 finally {
c.close();
}
try {
current=currentState.commitRecordTransaction();
}
 catch (Exception e) {
CommCareApplication._().reportNotificationMessage(NotificationMessageFactory.message(StockMessages.FormEntry_Unretrievable));
Toast.makeText(this,"String_Node_Str" + e.getMessage() + "String_Node_Str",Toast.LENGTH_LONG);
new FormRecordCleanupTask(this,platform).wipeRecord(currentState);
ExceptionReportTask ert=new ExceptionReportTask();
ert.execute(e);
currentState.reset();
if (wasExternal) {
this.finish();
}
refreshView();
return;
}
Logger.log(AndroidLogger.TYPE_FORM_ENTRY,"String_Node_Str");
if (complete) {
SharedPreferences settings=PreferenceManager.getDefaultSharedPreferences(CommCareApplication._());
mProcess=new ProcessAndSendTask(this,platform,settings.getString("String_Node_Str",this.getString(R.string.PostURL)));
mProcess.setListeners(this,CommCareApplication._().getSession().startDataSubmissionListener());
refreshView();
showDialog(DIALOG_PROCESS);
mProcess.execute(current);
if (wasExternal) {
this.finish();
}
currentState.reset();
return;
}
 else {
currentState.reset();
if (wasExternal) {
this.finish();
}
refreshView();
return;
}
}
 else {
Logger.log(AndroidLogger.TYPE_FORM_ENTRY,"String_Node_Str");
FormRecord current=currentState.getFormRecord();
if (current.getStatus() == FormRecord.STATUS_UNSTARTED) {
new FormRecordCleanupTask(this,platform).wipeRecord(currentState);
}
currentState.reset();
if (wasExternal) {
this.finish();
}
refreshView();
return;
}
}
startNextFetch();
super.onActivityResult(requestCode,resultCode,intent);
}
 catch (SessionUnavailableException sue) {
returnToLogin();
}
}

Fixed version (ground truth):
@Override protected void onActivityResult(int requestCode,int resultCode,Intent intent){
  try {
switch (requestCode) {
case INIT_APP:
      if (resultCode == RESULT_CANCELED) {
        this.finish();
        return;
      }
 else       if (resultCode == RESULT_OK) {
        return;
      }
    break;
case UPGRADE_APP:
  if (resultCode == RESULT_CANCELED) {
    return;
  }
 else   if (resultCode == RESULT_OK) {
    if (intent.getBooleanExtra(CommCareSetupActivity.KEY_REQUIRE_REFRESH,true)) {
      Toast.makeText(this,Localization.get("String_Node_Str"),Toast.LENGTH_LONG).show();
      CommCareApplication._().getSession().logout();
    }
    return;
  }
break;
case MISSING_MEDIA_ACTIVITY:
if (resultCode == RESULT_CANCELED) {
this.finish();
return;
}
 else if (resultCode == RESULT_OK) {
return;
}
case REPORT_PROBLEM_ACTIVITY:
if (resultCode == RESULT_CANCELED) {
return;
}
 else if (resultCode == RESULT_OK) {
CommCareApplication._().notifyLogsPending();
refreshView();
return;
}
case LOGIN_USER:
if (resultCode == RESULT_CANCELED) {
this.finish();
return;
}
 else if (resultCode == RESULT_OK) {
if (intent.getBooleanExtra(LoginActivity.ALREADY_LOGGED_IN,false)) {
}
 else {
refreshView();
checkAndStartUnsentTask(this);
}
return;
}
break;
case GET_INCOMPLETE_FORM:
AndroidSessionWrapper currentState=CommCareApplication._().getCurrentSessionWrapper();
if (resultCode == RESULT_CANCELED) {
refreshView();
return;
}
 else if (resultCode == RESULT_OK) {
int record=intent.getIntExtra(FormRecord.STORAGE_KEY,-1);
if (record == -1) {
break;
}
FormRecord r=CommCareApplication._().getStorage(FormRecord.STORAGE_KEY,FormRecord.class).read(record);
SqlIndexedStorageUtility<SessionStateDescriptor> ssdStorage=CommCareApplication._().getStorage(SessionStateDescriptor.STORAGE_KEY,SessionStateDescriptor.class);
Vector<Integer> ssds=ssdStorage.getIDsForValue(SessionStateDescriptor.META_FORM_RECORD_ID,r.getID());
if (ssds.size() == 1) {
currentState.loadFromStateDescription(ssdStorage.read(ssds.firstElement()));
}
 else {
currentState.setFormRecordId(r.getID());
}
formEntry(platform.getFormContentUri(r.getFormNamespace()),r);
return;
}
break;
case GET_COMMAND:
currentState=CommCareApplication._().getCurrentSessionWrapper();
if (resultCode == RESULT_CANCELED) {
if (currentState.getSession().getCommand() == null) {
currentState.reset();
refreshView();
return;
}
 else {
currentState.getSession().stepBack();
break;
}
}
 else if (resultCode == RESULT_OK) {
String command=intent.getStringExtra(CommCareSession.STATE_COMMAND_ID);
currentState.getSession().setCommand(command);
break;
}
break;
case GET_CASE:
currentState=CommCareApplication._().getCurrentSessionWrapper();
if (resultCode == RESULT_CANCELED) {
currentState.getSession().stepBack();
break;
}
 else if (resultCode == RESULT_OK) {
currentState.getSession().setDatum(currentState.getSession().getNeededDatum().getDataId(),intent.getStringExtra(CommCareSession.STATE_DATUM_VAL));
if (intent.hasExtra(CallOutActivity.CALL_DURATION)) {
platform.setCallDuration(intent.getLongExtra(CallOutActivity.CALL_DURATION,0));
}
break;
}
case MODEL_RESULT:
currentState=CommCareApplication._().getCurrentSessionWrapper();
if (resultCode == 1) {
if (intent.hasExtra("String_Node_Str")) {
Throwable ex=(Throwable)intent.getSerializableExtra("String_Node_Str");
ExceptionReportTask task=new ExceptionReportTask();
task.execute(ex);
}
 else {
RuntimeException ex=new RuntimeException("String_Node_Str");
ExceptionReportTask task=new ExceptionReportTask();
task.execute(ex);
}
Toast.makeText(this,Localization.get("String_Node_Str"),Toast.LENGTH_LONG);
currentState.reset();
refreshView();
break;
}
 else if (resultCode == RESULT_OK) {
FormRecord current=currentState.getFormRecord();
if (current.getStatus() == FormRecord.STATUS_COMPLETE || current.getStatus() == FormRecord.STATUS_SAVED) {
currentState.reset();
if (wasExternal) {
this.finish();
}
refreshView();
return;
}
Uri resultInstanceURI=intent.getData();
if (resultInstanceURI == null) {
Logger.log(AndroidLogger.TYPE_ERROR_WORKFLOW,"String_Node_Str");
CommCareApplication._().reportNotificationMessage(NotificationMessageFactory.message(StockMessages.FormEntry_Unretrievable));
Toast.makeText(this,"String_Node_Str",Toast.LENGTH_LONG);
currentState.reset();
if (wasExternal) {
this.finish();
}
refreshView();
return;
}
Cursor c=managedQuery(resultInstanceURI,null,null,null,null);
boolean complete=false;
try {
complete=currentState.beginRecordTransaction(resultInstanceURI,c);
}
 catch (IllegalArgumentException iae) {
iae.printStackTrace();
CommCareApplication._().reportNotificationMessage(NotificationMessageFactory.message(StockMessages.FormEntry_Unretrievable));
Toast.makeText(this,"String_Node_Str",Toast.LENGTH_LONG);
Logger.log(AndroidLogger.TYPE_ERROR_WORKFLOW,"String_Node_Str" + iae.getMessage());
currentState.reset();
if (wasExternal) {
this.finish();
}
refreshView();
return;
}
 finally {
c.close();
}
try {
current=currentState.commitRecordTransaction();
}
 catch (Exception e) {
CommCareApplication._().reportNotificationMessage(NotificationMessageFactory.message(StockMessages.FormEntry_Unretrievable));
Toast.makeText(this,"String_Node_Str" + e.getMessage() + "String_Node_Str",Toast.LENGTH_LONG);
new FormRecordCleanupTask(this,platform).wipeRecord(currentState);
ExceptionReportTask ert=new ExceptionReportTask();
ert.execute(e);
currentState.reset();
if (wasExternal) {
this.finish();
}
refreshView();
return;
}
Logger.log(AndroidLogger.TYPE_FORM_ENTRY,"String_Node_Str");
if (complete) {
SharedPreferences settings=PreferenceManager.getDefaultSharedPreferences(CommCareApplication._());
mProcess=new ProcessAndSendTask(this,platform,settings.getString("String_Node_Str",this.getString(R.string.PostURL)));
mProcess.setListeners(this,CommCareApplication._().getSession().startDataSubmissionListener());
refreshView();
showDialog(DIALOG_PROCESS);
mProcess.execute(current);
if (wasExternal) {
this.finish();
}
currentState.reset();
return;
}
 else {
currentState.reset();
if (wasExternal) {
this.finish();
}
refreshView();
return;
}
}
 else {
Logger.log(AndroidLogger.TYPE_FORM_ENTRY,"String_Node_Str");
FormRecord current=currentState.getFormRecord();
if (current.getStatus() == FormRecord.STATUS_UNSTARTED) {
new FormRecordCleanupTask(this,platform).wipeRecord(currentState);
}
if (wasExternal) {
this.finish();
currentState.reset();
refreshView();
return;
}
 else {
currentState.getSession().stepBack();
currentState.setFormRecordId(-1);
}
}
}
startNextFetch();
super.onActivityResult(requestCode,resultCode,intent);
}
 catch (SessionUnavailableException sue) {
returnToLogin();
}
}