From 2dcaad3d3b6f8757f01e4abd2295431f24cdfed5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 12 Dec 2012 19:14:09 +0100 Subject: API changes --- .../android/apg/integration/ApgData.java | 48 ++++++------ .../android/apg/integration/ApgIntentHelper.java | 85 +++++++++++++++------- .../android/apg/integration/ApgUtil.java | 72 ++++++++++++++++++ .../thialfihar/android/apg/integration/Util.java | 51 ------------- 4 files changed, 154 insertions(+), 102 deletions(-) create mode 100644 APG-API-Lib/src/org/thialfihar/android/apg/integration/ApgUtil.java delete mode 100644 APG-API-Lib/src/org/thialfihar/android/apg/integration/Util.java (limited to 'APG-API-Lib/src') diff --git a/APG-API-Lib/src/org/thialfihar/android/apg/integration/ApgData.java b/APG-API-Lib/src/org/thialfihar/android/apg/integration/ApgData.java index 5ef20bfbb..538e591df 100644 --- a/APG-API-Lib/src/org/thialfihar/android/apg/integration/ApgData.java +++ b/APG-API-Lib/src/org/thialfihar/android/apg/integration/ApgData.java @@ -22,36 +22,36 @@ import java.util.Arrays; public class ApgData implements Serializable { private static final long serialVersionUID = 6314045536270848410L; - protected long mEncryptionKeyIds[] = null; - protected long mSignatureKeyId = 0; - protected String mSignatureUserId = null; + protected long mPublicKeyIds[] = null; + protected long mSecretKeyId = 0; + protected String mSecretKeyUserId = null; protected boolean mSignatureSuccess = false; protected boolean mSignatureUnknown = false; protected String mDecryptedData = null; protected String mEncryptedData = null; - public void setSignatureKeyId(long keyId) { - mSignatureKeyId = keyId; + public void setSecretKeyId(long keyId) { + mSecretKeyId = keyId; } - public long getSignatureKeyId() { - return mSignatureKeyId; + public long getSecretKeyId() { + return mSecretKeyId; } - public void setEncryptionKeys(long keyIds[]) { - mEncryptionKeyIds = keyIds; + public void setPublicKeys(long keyIds[]) { + mPublicKeyIds = keyIds; } - public long[] getEncryptionKeys() { - return mEncryptionKeyIds; + public long[] getPublicKeys() { + return mPublicKeyIds; } - public boolean hasSignatureKey() { - return mSignatureKeyId != 0; + public boolean hasSecretKey() { + return mSecretKeyId != 0; } - public boolean hasEncryptionKeys() { - return (mEncryptionKeyIds != null) && (mEncryptionKeyIds.length > 0); + public boolean hasPublicKeys() { + return (mPublicKeyIds != null) && (mPublicKeyIds.length > 0); } public String getEncryptedData() { @@ -70,12 +70,12 @@ public class ApgData implements Serializable { mDecryptedData = data; } - public void setSignatureUserId(String userId) { - mSignatureUserId = userId; + public void setSecretKeyUserId(String userId) { + mSecretKeyUserId = userId; } - public String getSignatureUserId() { - return mSignatureUserId; + public String getSecretKeyUserId() { + return mSecretKeyUserId; } public boolean getSignatureSuccess() { @@ -96,11 +96,11 @@ public class ApgData implements Serializable { @Override public String toString() { - String output = "mEncryptionKeyIds: " + Arrays.toString(mEncryptionKeyIds) - + "\nmSignatureKeyId: " + mSignatureKeyId + "\nmSignatureUserId: " - + mSignatureUserId + "\nmSignatureSuccess: " + mSignatureSuccess - + "\nmSignatureUnknown: " + mSignatureUnknown + "\nmDecryptedData: " - + mDecryptedData + "\nmEncryptedData: " + mEncryptedData; + String output = "mPublicKeyIds: " + Arrays.toString(mPublicKeyIds) + "\nmSecretKeyId: " + + mSecretKeyId + "\nmSecretKeyUserId: " + mSecretKeyUserId + + "\nmSignatureSuccess: " + mSignatureSuccess + "\nmSignatureUnknown: " + + mSignatureUnknown + "\nmDecryptedData: " + mDecryptedData + "\nmEncryptedData: " + + mEncryptedData; return output; } diff --git a/APG-API-Lib/src/org/thialfihar/android/apg/integration/ApgIntentHelper.java b/APG-API-Lib/src/org/thialfihar/android/apg/integration/ApgIntentHelper.java index 09a544f26..cbd438f2f 100644 --- a/APG-API-Lib/src/org/thialfihar/android/apg/integration/ApgIntentHelper.java +++ b/APG-API-Lib/src/org/thialfihar/android/apg/integration/ApgIntentHelper.java @@ -27,12 +27,32 @@ public class ApgIntentHelper { public static final String APG_INTENT_PREFIX = "org.thialfihar.android.apg.intent."; // Intents - public static final String ACTION_DECRYPT = APG_INTENT_PREFIX + "DECRYPT"; + /** + * Encrypt + */ + // without permission public static final String ACTION_ENCRYPT = APG_INTENT_PREFIX + "ENCRYPT"; - public static final String ACTION_DECRYPT_FILE = APG_INTENT_PREFIX + "DECRYPT_FILE"; public static final String ACTION_ENCRYPT_FILE = APG_INTENT_PREFIX + "ENCRYPT_FILE"; - public static final String ACTION_DECRYPT_AND_RETURN = APG_INTENT_PREFIX + "DECRYPT_AND_RETURN"; + + // with permission public static final String ACTION_ENCRYPT_AND_RETURN = APG_INTENT_PREFIX + "ENCRYPT_AND_RETURN"; + public static final String ACTION_GENERATE_SIGNATURE_AND_RETURN = APG_INTENT_PREFIX + + "GENERATE_SIGNATURE_AND_RETURN"; + public static final String ACTION_ENCRYPT_STREAM_AND_RETURN = APG_INTENT_PREFIX + + "ENCRYPT_STREAM_AND_RETURN"; + + /** + * Decrypt + */ + // without permission + public static final String ACTION_DECRYPT = APG_INTENT_PREFIX + "DECRYPT"; + public static final String ACTION_DECRYPT_FILE = APG_INTENT_PREFIX + "DECRYPT_FILE"; + + // with permission + public static final String ACTION_DECRYPT_AND_RETURN = APG_INTENT_PREFIX + "DECRYPT_AND_RETURN"; + public static final String ACTION_DECRYPT_STREAM_AND_RETURN = APG_INTENT_PREFIX + + "DECRYPT_STREAM_AND_RETURN"; + public static final String ACTION_SELECT_PUBLIC_KEYS = APG_INTENT_PREFIX + "SELECT_PUBLIC_KEYS"; public static final String ACTION_SELECT_SECRET_KEY = APG_INTENT_PREFIX + "SELECT_SECRET_KEY"; public static final String ACTION_CREATE_KEY = APG_INTENT_PREFIX + "CREATE_KEY"; @@ -60,15 +80,19 @@ public class ApgIntentHelper { public static final String RESULT_EXTRA_MASTER_KEY_IDS = "masterKeyIds"; public static final String RESULT_EXTRA_USER_IDS = "userIds"; + + // result from EditKey + public static final String RESULT_EXTRA_MASTER_KEY_ID = "masterKeyId"; + public static final String RESULT_EXTRA_USER_ID = "userId"; public static final String INTENT_VERSION = "1"; - public static final int DECRYPT_MESSAGE = 0x21070001; - public static final int ENCRYPT_MESSAGE = 0x21070002; - public static final int SELECT_PUBLIC_KEYS = 0x21070003; - public static final int SELECT_SECRET_KEY = 0x21070004; - public static final int CREATE_KEY = 0x21070005; - public static final int EDIT_KEY = 0x21070006; + public static final int DECRYPT_MESSAGE = 0x00007121; + public static final int ENCRYPT_MESSAGE = 0x00007122; + public static final int SELECT_PUBLIC_KEYS = 0x00007123; + public static final int SELECT_SECRET_KEY = 0x00007124; + public static final int CREATE_KEY = 0x00007125; + public static final int EDIT_KEY = 0x00007126; private Activity activity; @@ -221,44 +245,53 @@ public class ApgIntentHelper { public boolean onActivityResult(int requestCode, int resultCode, Intent data, ApgData apgData) { switch (requestCode) { - case SELECT_SECRET_KEY: + case CREATE_KEY: if (resultCode != Activity.RESULT_OK || data == null) { // user canceled! break; } - apgData.setSignatureKeyId(data.getLongExtra(EXTRA_KEY_ID, 0)); - apgData.setSignatureUserId(data.getStringExtra(EXTRA_USER_ID)); + apgData.setSecretKeyId(data.getLongExtra(RESULT_EXTRA_MASTER_KEY_ID, 0)); + apgData.setSecretKeyUserId(data.getStringExtra(RESULT_EXTRA_USER_ID)); + break; + case SELECT_SECRET_KEY: + if (resultCode != Activity.RESULT_OK || data == null) { + // user canceled! + break; + } + apgData.setSecretKeyId(data.getLongExtra(EXTRA_KEY_ID, 0)); + apgData.setSecretKeyUserId(data.getStringExtra(EXTRA_USER_ID)); + break; case SELECT_PUBLIC_KEYS: if (resultCode != Activity.RESULT_OK || data == null) { - apgData.setEncryptionKeys(null); + apgData.setPublicKeys(null); break; } - apgData.setEncryptionKeys(data.getLongArrayExtra(RESULT_EXTRA_MASTER_KEY_IDS)); - break; + apgData.setPublicKeys(data.getLongArrayExtra(RESULT_EXTRA_MASTER_KEY_IDS)); + break; case ENCRYPT_MESSAGE: if (resultCode != Activity.RESULT_OK || data == null) { - apgData.setEncryptionKeys(null); + apgData.setPublicKeys(null); break; } apgData.setEncryptedData(data.getStringExtra(EXTRA_ENCRYPTED_MESSAGE)); - break; + break; case DECRYPT_MESSAGE: if (resultCode != Activity.RESULT_OK || data == null) { break; } - apgData.setSignatureUserId(data.getStringExtra(EXTRA_SIGNATURE_USER_ID)); - apgData.setSignatureKeyId(data.getLongExtra(EXTRA_SIGNATURE_KEY_ID, 0)); + apgData.setSecretKeyUserId(data.getStringExtra(EXTRA_SIGNATURE_USER_ID)); + apgData.setSecretKeyId(data.getLongExtra(EXTRA_SIGNATURE_KEY_ID, 0)); apgData.setSignatureSuccess(data.getBooleanExtra(EXTRA_SIGNATURE_SUCCESS, false)); apgData.setSignatureUnknown(data.getBooleanExtra(EXTRA_SIGNATURE_UNKNOWN, false)); apgData.setDecryptedData(data.getStringExtra(EXTRA_DECRYPTED_MESSAGE)); - break; + break; default: return false; } @@ -273,8 +306,8 @@ public class ApgIntentHelper { * The emails that should be used for preselection. * @return true when activity was found and executed successfully */ - public boolean selectEncryptionKeys(String emails) { - return selectEncryptionKeys(emails, null); + public boolean selectPublicKeys(String emails) { + return selectPublicKeys(emails, null); } /** @@ -286,19 +319,17 @@ public class ApgIntentHelper { * ApgData with encryption keys and signature keys preselected * @return true when activity was found and executed successfully */ - public boolean selectEncryptionKeys(String emails, ApgData apgData) { + public boolean selectPublicKeys(String emails, ApgData apgData) { Intent intent = new Intent(ACTION_SELECT_PUBLIC_KEYS); intent.putExtra(EXTRA_INTENT_VERSION, INTENT_VERSION); long[] initialKeyIds = null; - if (apgData == null || !apgData.hasEncryptionKeys()) { - + if (apgData == null || !apgData.hasPublicKeys()) { ApgContentProviderHelper cPHelper = new ApgContentProviderHelper(activity); initialKeyIds = cPHelper.getPublicKeyIdsFromEmail(emails); - } else { - initialKeyIds = apgData.getEncryptionKeys(); + initialKeyIds = apgData.getPublicKeys(); } intent.putExtra(EXTRA_SELECTION, initialKeyIds); diff --git a/APG-API-Lib/src/org/thialfihar/android/apg/integration/ApgUtil.java b/APG-API-Lib/src/org/thialfihar/android/apg/integration/ApgUtil.java new file mode 100644 index 000000000..a8a01f913 --- /dev/null +++ b/APG-API-Lib/src/org/thialfihar/android/apg/integration/ApgUtil.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2012 Dominik Schürmann + * Copyright (C) 2010-2011 K-9 Mail Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.thialfihar.android.apg.integration; + +import android.content.Context; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager.NameNotFoundException; + +import android.widget.Toast; + +public class ApgUtil { + + /** + * Check whether APG is installed and at a high enough version. + * + * @param context + * @return whether a suitable version of APG was found + */ + public boolean isApgAvailable(Context context) { + try { + PackageInfo pi = context.getPackageManager().getPackageInfo(Constants.APG_PACKAGE_NAME, + 0); + if (pi.versionCode >= Constants.MIN_REQUIRED_VERSION) { + return true; + } else { + Toast.makeText(context, + "This APG version is not supported! Please update to a newer one!", + Toast.LENGTH_LONG).show(); + } + } catch (NameNotFoundException e) { + // not found + } + + return false; + } + + /** + * Splits userId string into naming part and email part + * + * @param userId + * @return array with naming (0) and email (1) + */ + public static String[] splitUserId(String userId) { + String[] output = new String[2]; + + String chunks[] = userId.split(" <", 2); + userId = chunks[0]; + if (chunks.length > 1) { + output[1] = "<" + chunks[1]; + output[1] = output[1].replaceAll("<", ""); + output[1] = output[1].replaceAll(">", ""); + } + output[0] = userId; + + return output; + } +} diff --git a/APG-API-Lib/src/org/thialfihar/android/apg/integration/Util.java b/APG-API-Lib/src/org/thialfihar/android/apg/integration/Util.java deleted file mode 100644 index 01c6bb699..000000000 --- a/APG-API-Lib/src/org/thialfihar/android/apg/integration/Util.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2012 Dominik Schürmann - * Copyright (C) 2010-2011 K-9 Mail Contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.thialfihar.android.apg.integration; - -import android.content.Context; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager.NameNotFoundException; - -import android.widget.Toast; - -public class Util { - - /** - * Check whether APG is installed and at a high enough version. - * - * @param context - * @return whether a suitable version of APG was found - */ - public boolean isApgAvailable(Context context) { - try { - PackageInfo pi = context.getPackageManager().getPackageInfo(Constants.APG_PACKAGE_NAME, - 0); - if (pi.versionCode >= Constants.MIN_REQUIRED_VERSION) { - return true; - } else { - Toast.makeText(context, - "This APG version is not supported! Please update to a newer one!", - Toast.LENGTH_LONG).show(); - } - } catch (NameNotFoundException e) { - // not found - } - - return false; - } -} -- cgit v1.2.3