aboutsummaryrefslogtreecommitdiffstats
path: root/OpenPGP-Keychain-API
diff options
context:
space:
mode:
authorDominik Schürmann <dominik@dominikschuermann.de>2014-02-15 02:30:31 +0100
committerDominik Schürmann <dominik@dominikschuermann.de>2014-02-15 02:30:31 +0100
commite0111c2ec40b35db94f06d89c8ca50529fbbb401 (patch)
tree217ef9b5b075e3466959bc6d440d852dd05f5760 /OpenPGP-Keychain-API
parent5f39cb3ec0d1d3f77d48fdbb428a2147dfe11d03 (diff)
downloadopen-keychain-e0111c2ec40b35db94f06d89c8ca50529fbbb401.tar.gz
open-keychain-e0111c2ec40b35db94f06d89c8ca50529fbbb401.tar.bz2
open-keychain-e0111c2ec40b35db94f06d89c8ca50529fbbb401.zip
code simplifications
Diffstat (limited to 'OpenPGP-Keychain-API')
-rw-r--r--OpenPGP-Keychain-API/example-app/src/main/java/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java165
1 files changed, 53 insertions, 112 deletions
diff --git a/OpenPGP-Keychain-API/example-app/src/main/java/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java b/OpenPGP-Keychain-API/example-app/src/main/java/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java
index e578d0660..dee977657 100644
--- a/OpenPGP-Keychain-API/example-app/src/main/java/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java
+++ b/OpenPGP-Keychain-API/example-app/src/main/java/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java
@@ -44,6 +44,7 @@ import org.openintents.openpgp.util.OpenPgpServiceConnection;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
+import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
@@ -140,42 +141,63 @@ public class OpenPgpProviderActivity extends Activity {
return is;
}
+ private class MyCallback implements OpenPgpApi.IOpenPgpCallback {
+ boolean returnToCiphertextField;
+ ByteArrayOutputStream os;
+ int requestCode;
- public void sign(Bundle params) {
- params.putBoolean(OpenPgpConstants.PARAMS_REQUEST_ASCII_ARMOR, true);
-
- InputStream is = getInputstream(false);
- final ByteArrayOutputStream os = new ByteArrayOutputStream();
-
- OpenPgpApi api = new OpenPgpApi(mCryptoServiceConnection.getService());
- api.sign(params, is, os, new OpenPgpApi.IOpenPgpCallback() {
- @Override
- public void onReturn(Bundle result) {
- switch (result.getInt(OpenPgpConstants.RESULT_CODE)) {
- case OpenPgpConstants.RESULT_CODE_SUCCESS: {
- try {
- Log.d(OpenPgpConstants.TAG, "result: " + os.toByteArray().length
- + " str=" + os.toString("UTF-8"));
+ private MyCallback(boolean returnToCiphertextField, ByteArrayOutputStream os, int requestCode) {
+ this.returnToCiphertextField = returnToCiphertextField;
+ this.os = os;
+ this.requestCode = requestCode;
+ }
+ @Override
+ public void onReturn(Bundle result) {
+ switch (result.getInt(OpenPgpConstants.RESULT_CODE)) {
+ case OpenPgpConstants.RESULT_CODE_SUCCESS: {
+ try {
+ Log.d(OpenPgpConstants.TAG, "result: " + os.toByteArray().length
+ + " str=" + os.toString("UTF-8"));
+
+ if (returnToCiphertextField) {
mCiphertext.setText(os.toString("UTF-8"));
- } catch (UnsupportedEncodingException e) {
- Log.e(Constants.TAG, "UnsupportedEncodingException", e);
+ } else {
+ mMessage.setText(os.toString("UTF-8"));
}
- break;
+ } catch (UnsupportedEncodingException e) {
+ Log.e(Constants.TAG, "UnsupportedEncodingException", e);
}
- case OpenPgpConstants.RESULT_CODE_USER_INTERACTION_REQUIRED: {
- PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT);
- try {
- OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(),
- REQUEST_CODE_SIGN, null, 0, 0, 0);
- } catch (IntentSender.SendIntentException e) {
- Log.e(Constants.TAG, "SendIntentException", e);
- }
- break;
+ break;
+ }
+ case OpenPgpConstants.RESULT_CODE_USER_INTERACTION_REQUIRED: {
+ PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT);
+ try {
+ OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(),
+ requestCode, null, 0, 0, 0);
+ } catch (IntentSender.SendIntentException e) {
+ Log.e(Constants.TAG, "SendIntentException", e);
}
+ break;
+ }
+ case OpenPgpConstants.RESULT_CODE_ERROR: {
+ OpenPgpError error = result.getParcelable(OpenPgpConstants.RESULT_ERRORS);
+ handleError(error);
+ break;
}
}
- });
+ }
+ }
+
+
+ public void sign(Bundle params) {
+ params.putBoolean(OpenPgpConstants.PARAMS_REQUEST_ASCII_ARMOR, true);
+
+ InputStream is = getInputstream(false);
+ final ByteArrayOutputStream os = new ByteArrayOutputStream();
+
+ OpenPgpApi api = new OpenPgpApi(mCryptoServiceConnection.getService());
+ api.sign(params, is, os, new MyCallback(true, os, REQUEST_CODE_SIGN));
}
public void encrypt(Bundle params) {
@@ -186,34 +208,7 @@ public class OpenPgpProviderActivity extends Activity {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
OpenPgpApi api = new OpenPgpApi(mCryptoServiceConnection.getService());
- api.encrypt(params, is, os, new OpenPgpApi.IOpenPgpCallback() {
- @Override
- public void onReturn(Bundle result) {
- switch (result.getInt(OpenPgpConstants.RESULT_CODE)) {
- case OpenPgpConstants.RESULT_CODE_SUCCESS: {
- try {
- Log.d(OpenPgpConstants.TAG, "result: " + os.toByteArray().length
- + " str=" + os.toString("UTF-8"));
-
- mCiphertext.setText(os.toString("UTF-8"));
- } catch (UnsupportedEncodingException e) {
- Log.e(Constants.TAG, "UnsupportedEncodingException", e);
- }
- break;
- }
- case OpenPgpConstants.RESULT_CODE_USER_INTERACTION_REQUIRED: {
- PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT);
- try {
- OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(),
- REQUEST_CODE_ENCRYPT, null, 0, 0, 0);
- } catch (IntentSender.SendIntentException e) {
- Log.e(Constants.TAG, "SendIntentException", e);
- }
- break;
- }
- }
- }
- });
+ api.encrypt(params, is, os, new MyCallback(true, os, REQUEST_CODE_ENCRYPT));
}
public void signAndEncrypt(Bundle params) {
@@ -224,34 +219,7 @@ public class OpenPgpProviderActivity extends Activity {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
OpenPgpApi api = new OpenPgpApi(mCryptoServiceConnection.getService());
- api.signAndEncrypt(params, is, os, new OpenPgpApi.IOpenPgpCallback() {
- @Override
- public void onReturn(Bundle result) {
- switch (result.getInt(OpenPgpConstants.RESULT_CODE)) {
- case OpenPgpConstants.RESULT_CODE_SUCCESS: {
- try {
- Log.d(OpenPgpConstants.TAG, "result: " + os.toByteArray().length
- + " str=" + os.toString("UTF-8"));
-
- mCiphertext.setText(os.toString("UTF-8"));
- } catch (UnsupportedEncodingException e) {
- Log.e(Constants.TAG, "UnsupportedEncodingException", e);
- }
- break;
- }
- case OpenPgpConstants.RESULT_CODE_USER_INTERACTION_REQUIRED: {
- PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT);
- try {
- OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(),
- REQUEST_CODE_SIGN_AND_ENCRYPT, null, 0, 0, 0);
- } catch (IntentSender.SendIntentException e) {
- Log.e(Constants.TAG, "SendIntentException", e);
- }
- break;
- }
- }
- }
- });
+ api.signAndEncrypt(params, is, os, new MyCallback(true, os, REQUEST_CODE_SIGN_AND_ENCRYPT));
}
public void decryptAndVerify(Bundle params) {
@@ -261,34 +229,7 @@ public class OpenPgpProviderActivity extends Activity {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
OpenPgpApi api = new OpenPgpApi(mCryptoServiceConnection.getService());
- api.decryptAndVerify(params, is, os, new OpenPgpApi.IOpenPgpCallback() {
- @Override
- public void onReturn(Bundle result) {
- switch (result.getInt(OpenPgpConstants.RESULT_CODE)) {
- case OpenPgpConstants.RESULT_CODE_SUCCESS: {
- try {
- Log.d(OpenPgpConstants.TAG, "result: " + os.toByteArray().length
- + " str=" + os.toString("UTF-8"));
-
- mMessage.setText(os.toString("UTF-8"));
- } catch (UnsupportedEncodingException e) {
- Log.e(Constants.TAG, "UnsupportedEncodingException", e);
- }
- break;
- }
- case OpenPgpConstants.RESULT_CODE_USER_INTERACTION_REQUIRED: {
- PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT);
- try {
- OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(),
- REQUEST_CODE_DECRYPT_AND_VERIFY, null, 0, 0, 0);
- } catch (IntentSender.SendIntentException e) {
- Log.e(Constants.TAG, "SendIntentException", e);
- }
- break;
- }
- }
- }
- });
+ api.decryptAndVerify(params, is, os, new MyCallback(true, os, REQUEST_CODE_DECRYPT_AND_VERIFY));
}
@Override