Refactor code to get smaller methods

This commit is contained in:
cketti 2015-02-20 18:31:42 +01:00
parent d8448c3510
commit c6abb50d10
1 changed files with 76 additions and 65 deletions

View File

@ -16,7 +16,6 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentSender.SendIntentException; import android.content.IntentSender.SendIntentException;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.fsck.k9.Account; import com.fsck.k9.Account;
@ -191,7 +190,7 @@ public class MessageCryptoHelper {
Log.e(K9.LOG_TAG, "MessagingException", e); Log.e(K9.LOG_TAG, "MessagingException", e);
} }
onCryptoConverge(decryptedPart); onCryptoOperationReturned(decryptedPart);
} }
}); });
} }
@ -220,7 +219,7 @@ public class MessageCryptoHelper {
@Override @Override
public void onReturn(Intent result) { public void onReturn(Intent result) {
currentCryptoResult = result; currentCryptoResult = result;
onCryptoConverge(null); onCryptoOperationReturned(null);
} }
}); });
} }
@ -309,19 +308,26 @@ public class MessageCryptoHelper {
@Override @Override
protected void onPostExecute(MimeBodyPart decryptedPart) { protected void onPostExecute(MimeBodyPart decryptedPart) {
onCryptoConverge(decryptedPart); onCryptoOperationReturned(decryptedPart);
} }
}.execute(); }.execute();
return decryptedOutputStream; return decryptedOutputStream;
} }
private void onCryptoConverge(MimeBodyPart outputPart) { private void onCryptoOperationReturned(MimeBodyPart outputPart) {
try {
if (currentCryptoResult == null) { if (currentCryptoResult == null) {
Log.e(K9.LOG_TAG, "Internal error: we should have a result here!"); Log.e(K9.LOG_TAG, "Internal error: we should have a result here!");
return; return;
} }
try {
handleCryptoOperationResult(outputPart);
} finally {
currentCryptoResult = null;
}
}
private void handleCryptoOperationResult(MimeBodyPart outputPart) {
int resultCode = currentCryptoResult.getIntExtra(OpenPgpApi.RESULT_CODE, INVALID_OPENPGP_RESULT_CODE); int resultCode = currentCryptoResult.getIntExtra(OpenPgpApi.RESULT_CODE, INVALID_OPENPGP_RESULT_CODE);
if (K9.DEBUG) { if (K9.DEBUG) {
Log.d(K9.LOG_TAG, "OpenPGP API decryptVerify result code: " + resultCode); Log.d(K9.LOG_TAG, "OpenPGP API decryptVerify result code: " + resultCode);
@ -333,30 +339,43 @@ public class MessageCryptoHelper {
break; break;
} }
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: { case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: {
handleUserInteractionRequest();
break;
}
case OpenPgpApi.RESULT_CODE_ERROR: {
handleCryptoOperationError();
break;
}
case OpenPgpApi.RESULT_CODE_SUCCESS: {
handleCryptoOperationSuccess(outputPart);
break;
}
}
}
private void handleUserInteractionRequest() {
PendingIntent pendingIntent = currentCryptoResult.getParcelableExtra(OpenPgpApi.RESULT_INTENT); PendingIntent pendingIntent = currentCryptoResult.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
if (pendingIntent == null) { if (pendingIntent == null) {
throw new AssertionError("Expecting PendingIntent on USER_INTERACTION_REQUIRED!"); throw new AssertionError("Expecting PendingIntent on USER_INTERACTION_REQUIRED!");
} }
try { try {
activity.startIntentSenderForResult(pendingIntent.getIntentSender(), activity.startIntentSenderForResult(pendingIntent.getIntentSender(), REQUEST_CODE_CRYPTO, null, 0, 0, 0);
REQUEST_CODE_CRYPTO, null, 0, 0, 0);
} catch (SendIntentException e) { } catch (SendIntentException e) {
Log.e(K9.LOG_TAG, "Internal error on starting pendingintent!", e); Log.e(K9.LOG_TAG, "Internal error on starting pendingintent!", e);
} }
break;
} }
case OpenPgpApi.RESULT_CODE_ERROR: {
OpenPgpError error = currentCryptoResult.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
private void handleCryptoOperationError() {
OpenPgpError error = currentCryptoResult.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
if (K9.DEBUG) { if (K9.DEBUG) {
Log.w(K9.LOG_TAG, "OpenPGP API error: " + error.getMessage()); Log.w(K9.LOG_TAG, "OpenPGP API error: " + error.getMessage());
} }
onCryptoFailed(error); onCryptoFailed(error);
break;
} }
case OpenPgpApi.RESULT_CODE_SUCCESS: {
private void handleCryptoOperationSuccess(MimeBodyPart outputPart) {
OpenPgpResultAnnotation resultAnnotation = new OpenPgpResultAnnotation(); OpenPgpResultAnnotation resultAnnotation = new OpenPgpResultAnnotation();
resultAnnotation.setOutputData(outputPart); resultAnnotation.setOutputData(outputPart);
@ -365,21 +384,13 @@ public class MessageCryptoHelper {
// this is not easy to determine for inline data though // this is not easy to determine for inline data though
resultAnnotation.setWasEncrypted(false); resultAnnotation.setWasEncrypted(false);
OpenPgpSignatureResult signatureResult = OpenPgpSignatureResult signatureResult = currentCryptoResult.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE);
currentCryptoResult.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE);
resultAnnotation.setSignatureResult(signatureResult); resultAnnotation.setSignatureResult(signatureResult);
PendingIntent pendingIntent = PendingIntent pendingIntent = currentCryptoResult.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
currentCryptoResult.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
resultAnnotation.setPendingIntent(pendingIntent); resultAnnotation.setPendingIntent(pendingIntent);
onCryptoSuccess(resultAnnotation); onCryptoSuccess(resultAnnotation);
break;
}
}
} finally {
currentCryptoResult = null;
}
} }
public void handleCryptoResult(int requestCode, int resultCode, Intent data) { public void handleCryptoResult(int requestCode, int resultCode, Intent data) {