From 17301be2cae4742f6ff508f984730a4d44e0a66a Mon Sep 17 00:00:00 2001 From: danielnelz Date: Mon, 9 Mar 2015 20:09:05 +0100 Subject: Created enum MessageStatus in KeychainIntentServiceHandler and enum IOType in KeychainIntentService and replaced int constants with them. Fixed some typos. --- .../keychain/service/KeychainIntentService.java | 92 +++++++++++++--------- .../service/KeychainIntentServiceHandler.java | 34 +++++--- 2 files changed, 78 insertions(+), 48 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index d95701458..b0453474e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -60,6 +60,7 @@ import org.sufficientlysecure.keychain.pgp.SignEncryptParcel; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralMsgIdException; import org.sufficientlysecure.keychain.provider.ProviderHelper; +import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler.MessageStatus; import org.sufficientlysecure.keychain.util.FileHelper; import org.sufficientlysecure.keychain.util.InputData; import org.sufficientlysecure.keychain.util.Log; @@ -126,8 +127,20 @@ public class KeychainIntentService extends IntentService implements Progressable public static final String SOURCE = "source"; // possible targets: - public static final int IO_BYTES = 1; - public static final int IO_URI = 2; + public static enum IOType { + UNKNOWN, + BYTES, + URI; + + private static final IOType[] values = values(); + + public static IOType fromInt(int n) { + if(n >= values.length) + return UNKNOWN; + else + return values[n]; + } + } // encrypt public static final String ENCRYPT_DECRYPT_INPUT_URI = "input_uri"; @@ -244,7 +257,7 @@ public class KeychainIntentService extends IntentService implements Progressable CertifyResult result = op.certify(parcel, keyServerUri); // Result - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result); + sendMessageToHandler(MessageStatus.OKAY, result); break; } @@ -259,7 +272,7 @@ public class KeychainIntentService extends IntentService implements Progressable } // Result - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result); + sendMessageToHandler(MessageStatus.OKAY, result); break; } @@ -288,7 +301,7 @@ public class KeychainIntentService extends IntentService implements Progressable DecryptVerifyResult decryptVerifyResult = builder.build().execute(); - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, decryptVerifyResult); + sendMessageToHandler(MessageStatus.OKAY, decryptVerifyResult); } catch (Exception e) { sendErrorToHandler(e); } @@ -349,7 +362,7 @@ public class KeychainIntentService extends IntentService implements Progressable } // kind of awkward, but this whole class wants to pull bytes out of “data” - data.putInt(KeychainIntentService.TARGET, KeychainIntentService.IO_BYTES); + data.putInt(KeychainIntentService.TARGET, IOType.BYTES.ordinal()); data.putByteArray(KeychainIntentService.DECRYPT_CIPHERTEXT_BYTES, messageBytes); InputData inputData = createDecryptInputData(data); @@ -386,7 +399,7 @@ public class KeychainIntentService extends IntentService implements Progressable resultData.putString(KeychainIntentServiceHandler.KEYBASE_PROOF_URL, prover.getProofUrl()); resultData.putString(KeychainIntentServiceHandler.KEYBASE_PRESENCE_URL, prover.getPresenceUrl()); resultData.putString(KeychainIntentServiceHandler.KEYBASE_PRESENCE_LABEL, prover.getPresenceLabel()); - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, resultData); + sendMessageToHandler(MessageStatus.OKAY, resultData); } catch (Exception e) { sendErrorToHandler(e); } @@ -429,7 +442,7 @@ public class KeychainIntentService extends IntentService implements Progressable Log.logDebugBundle(resultData, "resultData"); - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, resultData); + sendMessageToHandler(MessageStatus.OKAY, resultData); } catch (Exception e) { sendErrorToHandler(e); } @@ -447,7 +460,7 @@ public class KeychainIntentService extends IntentService implements Progressable DeleteResult result = op.execute(masterKeyIds, isSecret); // Result - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result); + sendMessageToHandler(MessageStatus.OKAY, result); break; } @@ -462,7 +475,7 @@ public class KeychainIntentService extends IntentService implements Progressable EditKeyResult result = op.execute(saveParcel, passphrase); // Result - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result); + sendMessageToHandler(MessageStatus.OKAY, result); break; } @@ -476,7 +489,7 @@ public class KeychainIntentService extends IntentService implements Progressable PromoteKeyResult result = op.execute(keyRingId); // Result - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result); + sendMessageToHandler(MessageStatus.OKAY, result); break; } @@ -500,7 +513,7 @@ public class KeychainIntentService extends IntentService implements Progressable } // Result - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result); + sendMessageToHandler(MessageStatus.OKAY, result); break; } @@ -521,7 +534,7 @@ public class KeychainIntentService extends IntentService implements Progressable : importExportOperation.importKeyRings(cache, keyServer); // Result - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result); + sendMessageToHandler(MessageStatus.OKAY, result); break; @@ -537,7 +550,7 @@ public class KeychainIntentService extends IntentService implements Progressable SignEncryptResult result = op.execute(inputParcel); // Result - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result); + sendMessageToHandler(MessageStatus.OKAY, result); break; } @@ -560,7 +573,7 @@ public class KeychainIntentService extends IntentService implements Progressable throw new PgpGeneralException("Unable to export key to selected server"); } - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY); + sendMessageToHandler(MessageStatus.OKAY); } catch (Exception e) { sendErrorToHandler(e); } @@ -582,7 +595,7 @@ public class KeychainIntentService extends IntentService implements Progressable private void sendProofError(String msg) { Bundle bundle = new Bundle(); bundle.putString(KeychainIntentServiceHandler.DATA_ERROR, msg); - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, bundle); + sendMessageToHandler(MessageStatus.OKAY, bundle); } private void sendErrorToHandler(Exception e) { @@ -599,14 +612,14 @@ public class KeychainIntentService extends IntentService implements Progressable Bundle data = new Bundle(); data.putString(KeychainIntentServiceHandler.DATA_ERROR, message); - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_EXCEPTION, null, data); + sendMessageToHandler(MessageStatus.EXCEPTION, null, data); } - private void sendMessageToHandler(Integer arg1, Integer arg2, Bundle data) { + private void sendMessageToHandler(MessageStatus status, Integer arg2, Bundle data) { Message msg = Message.obtain(); assert msg != null; - msg.arg1 = arg1; + msg.arg1 = status.ordinal(); if (arg2 != null) { msg.arg2 = arg2; } @@ -623,18 +636,18 @@ public class KeychainIntentService extends IntentService implements Progressable } } - private void sendMessageToHandler(Integer arg1, OperationResult data) { + private void sendMessageToHandler(MessageStatus status, OperationResult data) { Bundle bundle = new Bundle(); bundle.putParcelable(OperationResult.EXTRA_RESULT, data); - sendMessageToHandler(arg1, null, bundle); + sendMessageToHandler(status, null, bundle); } - private void sendMessageToHandler(Integer arg1, Bundle data) { - sendMessageToHandler(arg1, null, data); + private void sendMessageToHandler(MessageStatus status, Bundle data) { + sendMessageToHandler(status, null, data); } - private void sendMessageToHandler(Integer arg1) { - sendMessageToHandler(arg1, null, null); + private void sendMessageToHandler(MessageStatus status) { + sendMessageToHandler(status, null, null); } /** @@ -651,7 +664,7 @@ public class KeychainIntentService extends IntentService implements Progressable data.putInt(KeychainIntentServiceHandler.DATA_PROGRESS, progress); data.putInt(KeychainIntentServiceHandler.DATA_PROGRESS_MAX, max); - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_UPDATE_PROGRESS, null, data); + sendMessageToHandler(MessageStatus.UPDATE_PROGRESS, null, data); } public void setProgress(int resourceId, int progress, int max) { @@ -664,7 +677,7 @@ public class KeychainIntentService extends IntentService implements Progressable @Override public void setPreventCancel() { - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_PREVENT_CANCEL); + sendMessageToHandler(MessageStatus.PREVENT_CANCEL); } private InputData createDecryptInputData(Bundle data) throws IOException, PgpGeneralException { @@ -673,35 +686,37 @@ public class KeychainIntentService extends IntentService implements Progressable private InputData createCryptInputData(Bundle data, String bytesName) throws PgpGeneralException, IOException { int source = data.get(SOURCE) != null ? data.getInt(SOURCE) : data.getInt(TARGET); - switch (source) { - case IO_BYTES: /* encrypting bytes directly */ + IOType type = IOType.fromInt(source); + switch (type) { + case BYTES: /* encrypting bytes directly */ byte[] bytes = data.getByteArray(bytesName); return new InputData(new ByteArrayInputStream(bytes), bytes.length); - case IO_URI: /* encrypting content uri */ + case URI: /* encrypting content uri */ Uri providerUri = data.getParcelable(ENCRYPT_DECRYPT_INPUT_URI); // InputStream return new InputData(getContentResolver().openInputStream(providerUri), FileHelper.getFileSize(this, providerUri, 0)); default: - throw new PgpGeneralException("No target choosen!"); + throw new PgpGeneralException("No target chosen!"); } } private OutputStream createCryptOutputStream(Bundle data) throws PgpGeneralException, FileNotFoundException { int target = data.getInt(TARGET); - switch (target) { - case IO_BYTES: + IOType type = IOType.fromInt(target); + switch (type) { + case BYTES: return new ByteArrayOutputStream(); - case IO_URI: + case URI: Uri providerUri = data.getParcelable(ENCRYPT_DECRYPT_OUTPUT_URI); return getContentResolver().openOutputStream(providerUri); default: - throw new PgpGeneralException("No target choosen!"); + throw new PgpGeneralException("No target chosen!"); } } @@ -711,12 +726,13 @@ public class KeychainIntentService extends IntentService implements Progressable private void finalizeCryptOutputStream(Bundle data, Bundle resultData, OutputStream outStream, String bytesName) { int target = data.getInt(TARGET); - switch (target) { - case IO_BYTES: + IOType type = IOType.fromInt(target); + switch (type) { + case BYTES: byte output[] = ((ByteArrayOutputStream) outStream).toByteArray(); resultData.putByteArray(bytesName, output); break; - case IO_URI: + case URI: // nothing, output was written, just send okay and verification bundle break; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java index 635fe86f1..fe0e7bb90 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java @@ -32,11 +32,24 @@ import org.sufficientlysecure.keychain.util.Log; public class KeychainIntentServiceHandler extends Handler { - // possible messages send from this service to handler on ui - public static final int MESSAGE_OKAY = 1; - public static final int MESSAGE_EXCEPTION = 2; - public static final int MESSAGE_UPDATE_PROGRESS = 3; - public static final int MESSAGE_PREVENT_CANCEL = 4; + // possible messages sent from this service to handler on ui + public static enum MessageStatus{ + UNKNOWN, + OKAY, + EXCEPTION, + UPDATE_PROGRESS, + PREVENT_CANCEL; + + private static final MessageStatus[] values = values(); + + public static MessageStatus fromInt(int n) + { + if(n >= values.length) + return UNKNOWN; + else + return values[n]; + } + } // possible data keys for messages public static final String DATA_ERROR = "error"; @@ -103,13 +116,14 @@ public class KeychainIntentServiceHandler extends Handler { return; } - switch (message.arg1) { - case MESSAGE_OKAY: + MessageStatus status = MessageStatus.fromInt(message.arg1); + switch (status) { + case OKAY: mProgressDialogFragment.dismissAllowingStateLoss(); break; - case MESSAGE_EXCEPTION: + case EXCEPTION: mProgressDialogFragment.dismissAllowingStateLoss(); // show error from service @@ -121,7 +135,7 @@ public class KeychainIntentServiceHandler extends Handler { break; - case MESSAGE_UPDATE_PROGRESS: + case UPDATE_PROGRESS: if (data.containsKey(DATA_PROGRESS) && data.containsKey(DATA_PROGRESS_MAX)) { // update progress from service @@ -139,7 +153,7 @@ public class KeychainIntentServiceHandler extends Handler { break; - case MESSAGE_PREVENT_CANCEL: + case PREVENT_CANCEL: mProgressDialogFragment.setPreventCancel(true); break; -- cgit v1.2.3 From 7d27faf00c42ce1ad6311c5fadc37c62ba999150 Mon Sep 17 00:00:00 2001 From: danielnelz Date: Mon, 9 Mar 2015 20:14:11 +0100 Subject: Added braces to if else. --- .../sufficientlysecure/keychain/service/KeychainIntentService.java | 5 +++-- .../keychain/service/KeychainIntentServiceHandler.java | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index b0453474e..9f769759e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -135,10 +135,11 @@ public class KeychainIntentService extends IntentService implements Progressable private static final IOType[] values = values(); public static IOType fromInt(int n) { - if(n >= values.length) + if(n >= values.length) { return UNKNOWN; - else + } else { return values[n]; + } } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java index fe0e7bb90..c102acc32 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java @@ -44,10 +44,11 @@ public class KeychainIntentServiceHandler extends Handler { public static MessageStatus fromInt(int n) { - if(n >= values.length) + if(n >= values.length) { return UNKNOWN; - else + } else { return values[n]; + } } } -- cgit v1.2.3 From 52200edfd738eb4019f3aac4fba659fe721e98d1 Mon Sep 17 00:00:00 2001 From: danielnelz Date: Wed, 11 Mar 2015 21:32:20 +0100 Subject: Fixed possible ArrayIndexOutOfBoundsException. --- .../org/sufficientlysecure/keychain/service/KeychainIntentService.java | 2 +- .../keychain/service/KeychainIntentServiceHandler.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index 9f769759e..bf6a62782 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -135,7 +135,7 @@ public class KeychainIntentService extends IntentService implements Progressable private static final IOType[] values = values(); public static IOType fromInt(int n) { - if(n >= values.length) { + if(n < 0 || n >= values.length) { return UNKNOWN; } else { return values[n]; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java index c102acc32..bd047518d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java @@ -44,7 +44,7 @@ public class KeychainIntentServiceHandler extends Handler { public static MessageStatus fromInt(int n) { - if(n >= values.length) { + if(n < 0 || n >= values.length) { return UNKNOWN; } else { return values[n]; -- cgit v1.2.3