From dccef45dc1646d623c01dcde7a28711b2ac815e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 29 Jan 2015 20:43:35 +0100 Subject: Change to gradle file structure, include changes by ligi --- src/main/AndroidManifest.xml | 13 + .../org/openintents/openpgp/IOpenPgpService.aidl | 24 ++ .../java/org/openintents/openpgp/OpenPgpError.java | 118 +++++++ .../org/openintents/openpgp/OpenPgpMetadata.java | 132 ++++++++ .../openpgp/OpenPgpSignatureResult.java | 183 ++++++++++ .../org/openintents/openpgp/util/OpenPgpApi.java | 370 +++++++++++++++++++++ .../openpgp/util/OpenPgpListPreference.java | 266 +++++++++++++++ .../openpgp/util/OpenPgpServiceConnection.java | 124 +++++++ .../org/openintents/openpgp/util/OpenPgpUtils.java | 106 ++++++ .../openpgp/util/ParcelFileDescriptorUtil.java | 106 ++++++ .../ic_action_cancel_launchersize.png | Bin 0 -> 1520 bytes .../ic_action_cancel_launchersize_light.png | Bin 0 -> 1940 bytes .../ic_action_cancel_launchersize.png | Bin 0 -> 1032 bytes .../ic_action_cancel_launchersize_light.png | Bin 0 -> 1098 bytes .../ic_action_cancel_launchersize.png | Bin 0 -> 1570 bytes .../ic_action_cancel_launchersize_light.png | Bin 0 -> 2039 bytes .../ic_action_cancel_launchersize.png | Bin 0 -> 2345 bytes .../ic_action_cancel_launchersize_light.png | Bin 0 -> 2404 bytes src/main/res/values-cs/strings.xml | 5 + src/main/res/values-de/strings.xml | 5 + src/main/res/values-es/strings.xml | 5 + src/main/res/values-et/strings.xml | 2 + src/main/res/values-fi/strings.xml | 2 + src/main/res/values-fr/strings.xml | 5 + src/main/res/values-is/strings.xml | 2 + src/main/res/values-it/strings.xml | 5 + src/main/res/values-ja/strings.xml | 5 + src/main/res/values-nl/strings.xml | 2 + src/main/res/values-pl/strings.xml | 2 + src/main/res/values-pt/strings.xml | 2 + src/main/res/values-ru/strings.xml | 5 + src/main/res/values-sl/strings.xml | 5 + src/main/res/values-tr/strings.xml | 2 + src/main/res/values-uk/strings.xml | 5 + src/main/res/values-zh/strings.xml | 2 + src/main/res/values/strings.xml | 7 + .../org/openintents/openpgp/OpenPgpUtilsTest.java | 50 +++ 37 files changed, 1560 insertions(+) create mode 100644 src/main/AndroidManifest.xml create mode 100644 src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl create mode 100644 src/main/java/org/openintents/openpgp/OpenPgpError.java create mode 100644 src/main/java/org/openintents/openpgp/OpenPgpMetadata.java create mode 100644 src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java create mode 100644 src/main/java/org/openintents/openpgp/util/OpenPgpApi.java create mode 100644 src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java create mode 100644 src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java create mode 100644 src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java create mode 100644 src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java create mode 100644 src/main/res/drawable-hdpi/ic_action_cancel_launchersize.png create mode 100644 src/main/res/drawable-hdpi/ic_action_cancel_launchersize_light.png create mode 100644 src/main/res/drawable-mdpi/ic_action_cancel_launchersize.png create mode 100644 src/main/res/drawable-mdpi/ic_action_cancel_launchersize_light.png create mode 100644 src/main/res/drawable-xhdpi/ic_action_cancel_launchersize.png create mode 100644 src/main/res/drawable-xhdpi/ic_action_cancel_launchersize_light.png create mode 100644 src/main/res/drawable-xxhdpi/ic_action_cancel_launchersize.png create mode 100644 src/main/res/drawable-xxhdpi/ic_action_cancel_launchersize_light.png create mode 100644 src/main/res/values-cs/strings.xml create mode 100644 src/main/res/values-de/strings.xml create mode 100644 src/main/res/values-es/strings.xml create mode 100644 src/main/res/values-et/strings.xml create mode 100644 src/main/res/values-fi/strings.xml create mode 100644 src/main/res/values-fr/strings.xml create mode 100644 src/main/res/values-is/strings.xml create mode 100644 src/main/res/values-it/strings.xml create mode 100644 src/main/res/values-ja/strings.xml create mode 100644 src/main/res/values-nl/strings.xml create mode 100644 src/main/res/values-pl/strings.xml create mode 100644 src/main/res/values-pt/strings.xml create mode 100644 src/main/res/values-ru/strings.xml create mode 100644 src/main/res/values-sl/strings.xml create mode 100644 src/main/res/values-tr/strings.xml create mode 100644 src/main/res/values-uk/strings.xml create mode 100644 src/main/res/values-zh/strings.xml create mode 100644 src/main/res/values/strings.xml create mode 100644 src/main/test/org/openintents/openpgp/OpenPgpUtilsTest.java (limited to 'src/main') diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml new file mode 100644 index 0000000..98cb89f --- /dev/null +++ b/src/main/AndroidManifest.xml @@ -0,0 +1,13 @@ + + + + + + + + \ No newline at end of file diff --git a/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl b/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl new file mode 100644 index 0000000..2451207 --- /dev/null +++ b/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2014-2015 Dominik Schürmann + * + * 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.openintents.openpgp; + +interface IOpenPgpService { + + // see OpenPgpApi for documentation + Intent execute(in Intent data, in ParcelFileDescriptor input, in ParcelFileDescriptor output); + +} \ No newline at end of file diff --git a/src/main/java/org/openintents/openpgp/OpenPgpError.java b/src/main/java/org/openintents/openpgp/OpenPgpError.java new file mode 100644 index 0000000..ce8f21f --- /dev/null +++ b/src/main/java/org/openintents/openpgp/OpenPgpError.java @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2014-2015 Dominik Schürmann + * + * 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.openintents.openpgp; + +import android.os.Parcel; +import android.os.Parcelable; + +/** + * Parcelable versioning has been copied from Dashclock Widget + * https://code.google.com/p/dashclock/source/browse/api/src/main/java/com/google/android/apps/dashclock/api/ExtensionData.java + */ +public class OpenPgpError implements Parcelable { + /** + * Since there might be a case where new versions of the client using the library getting + * old versions of the protocol (and thus old versions of this class), we need a versioning + * system for the parcels sent between the clients and the providers. + */ + public static final int PARCELABLE_VERSION = 1; + + // possible values for errorId + public static final int CLIENT_SIDE_ERROR = -1; + public static final int GENERIC_ERROR = 0; + public static final int INCOMPATIBLE_API_VERSIONS = 1; + public static final int NO_OR_WRONG_PASSPHRASE = 2; + public static final int NO_USER_IDS = 3; + + int errorId; + String message; + + public OpenPgpError() { + } + + public OpenPgpError(int errorId, String message) { + this.errorId = errorId; + this.message = message; + } + + public OpenPgpError(OpenPgpError b) { + this.errorId = b.errorId; + this.message = b.message; + } + + public int getErrorId() { + return errorId; + } + + public void setErrorId(int errorId) { + this.errorId = errorId; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel dest, int flags) { + /** + * NOTE: When adding fields in the process of updating this API, make sure to bump + * {@link #PARCELABLE_VERSION}. + */ + dest.writeInt(PARCELABLE_VERSION); + // Inject a placeholder that will store the parcel size from this point on + // (not including the size itself). + int sizePosition = dest.dataPosition(); + dest.writeInt(0); + int startPosition = dest.dataPosition(); + // version 1 + dest.writeInt(errorId); + dest.writeString(message); + // Go back and write the size + int parcelableSize = dest.dataPosition() - startPosition; + dest.setDataPosition(sizePosition); + dest.writeInt(parcelableSize); + dest.setDataPosition(startPosition + parcelableSize); + } + + public static final Creator CREATOR = new Creator() { + public OpenPgpError createFromParcel(final Parcel source) { + int parcelableVersion = source.readInt(); + int parcelableSize = source.readInt(); + int startPosition = source.dataPosition(); + + OpenPgpError error = new OpenPgpError(); + error.errorId = source.readInt(); + error.message = source.readString(); + + // skip over all fields added in future versions of this parcel + source.setDataPosition(startPosition + parcelableSize); + + return error; + } + + public OpenPgpError[] newArray(final int size) { + return new OpenPgpError[size]; + } + }; +} diff --git a/src/main/java/org/openintents/openpgp/OpenPgpMetadata.java b/src/main/java/org/openintents/openpgp/OpenPgpMetadata.java new file mode 100644 index 0000000..d620a57 --- /dev/null +++ b/src/main/java/org/openintents/openpgp/OpenPgpMetadata.java @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2014-2015 Dominik Schürmann + * + * 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.openintents.openpgp; + +import android.os.Parcel; +import android.os.Parcelable; + +/** + * Parcelable versioning has been copied from Dashclock Widget + * https://code.google.com/p/dashclock/source/browse/api/src/main/java/com/google/android/apps/dashclock/api/ExtensionData.java + */ +public class OpenPgpMetadata implements Parcelable { + /** + * Since there might be a case where new versions of the client using the library getting + * old versions of the protocol (and thus old versions of this class), we need a versioning + * system for the parcels sent between the clients and the providers. + */ + public static final int PARCELABLE_VERSION = 1; + + String filename; + String mimeType; + long modificationTime; + long originalSize; + + public String getFilename() { + return filename; + } + + public String getMimeType() { + return mimeType; + } + + public long getModificationTime() { + return modificationTime; + } + + public long getOriginalSize() { + return originalSize; + } + + public OpenPgpMetadata() { + } + + public OpenPgpMetadata(String filename, String mimeType, long modificationTime, + long originalSize) { + this.filename = filename; + this.mimeType = mimeType; + this.modificationTime = modificationTime; + this.originalSize = originalSize; + } + + public OpenPgpMetadata(OpenPgpMetadata b) { + this.filename = b.filename; + this.mimeType = b.mimeType; + this.modificationTime = b.modificationTime; + this.originalSize = b.originalSize; + } + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel dest, int flags) { + /** + * NOTE: When adding fields in the process of updating this API, make sure to bump + * {@link #PARCELABLE_VERSION}. + */ + dest.writeInt(PARCELABLE_VERSION); + // Inject a placeholder that will store the parcel size from this point on + // (not including the size itself). + int sizePosition = dest.dataPosition(); + dest.writeInt(0); + int startPosition = dest.dataPosition(); + // version 1 + dest.writeString(filename); + dest.writeString(mimeType); + dest.writeLong(modificationTime); + dest.writeLong(originalSize); + // Go back and write the size + int parcelableSize = dest.dataPosition() - startPosition; + dest.setDataPosition(sizePosition); + dest.writeInt(parcelableSize); + dest.setDataPosition(startPosition + parcelableSize); + } + + public static final Creator CREATOR = new Creator() { + public OpenPgpMetadata createFromParcel(final Parcel source) { + int parcelableVersion = source.readInt(); + int parcelableSize = source.readInt(); + int startPosition = source.dataPosition(); + + OpenPgpMetadata vr = new OpenPgpMetadata(); + vr.filename = source.readString(); + vr.mimeType = source.readString(); + vr.modificationTime = source.readLong(); + vr.originalSize = source.readLong(); + + // skip over all fields added in future versions of this parcel + source.setDataPosition(startPosition + parcelableSize); + + return vr; + } + + public OpenPgpMetadata[] newArray(final int size) { + return new OpenPgpMetadata[size]; + } + }; + + @Override + public String toString() { + String out = "\nfilename: " + filename; + out += "\nmimeType: " + mimeType; + out += "\nmodificationTime: " + modificationTime; + out += "\noriginalSize: " + originalSize; + return out; + } + +} diff --git a/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java b/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java new file mode 100644 index 0000000..7eb06d9 --- /dev/null +++ b/src/main/java/org/openintents/openpgp/OpenPgpSignatureResult.java @@ -0,0 +1,183 @@ +/* + * Copyright (C) 2014-2015 Dominik Schürmann + * + * 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.openintents.openpgp; + +import android.os.Parcel; +import android.os.Parcelable; + +import org.openintents.openpgp.util.OpenPgpUtils; + +import java.util.ArrayList; +import java.util.Locale; + +/** + * Parcelable versioning has been copied from Dashclock Widget + * https://code.google.com/p/dashclock/source/browse/api/src/main/java/com/google/android/apps/dashclock/api/ExtensionData.java + */ +public class OpenPgpSignatureResult implements Parcelable { + /** + * Since there might be a case where new versions of the client using the library getting + * old versions of the protocol (and thus old versions of this class), we need a versioning + * system for the parcels sent between the clients and the providers. + */ + public static final int PARCELABLE_VERSION = 2; + + // generic error on signature verification + public static final int SIGNATURE_ERROR = 0; + // successfully verified signature, with certified key + public static final int SIGNATURE_SUCCESS_CERTIFIED = 1; + // no key was found for this signature verification + public static final int SIGNATURE_KEY_MISSING = 2; + // successfully verified signature, but with uncertified key + public static final int SIGNATURE_SUCCESS_UNCERTIFIED = 3; + // key has been revoked + public static final int SIGNATURE_KEY_REVOKED = 4; + // key is expired + public static final int SIGNATURE_KEY_EXPIRED = 5; + + int status; + boolean signatureOnly; + String primaryUserId; + ArrayList userIds; + long keyId; + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } + + public boolean isSignatureOnly() { + return signatureOnly; + } + + public void setSignatureOnly(boolean signatureOnly) { + this.signatureOnly = signatureOnly; + } + + public String getPrimaryUserId() { + return primaryUserId; + } + + public void setPrimaryUserId(String primaryUserId) { + this.primaryUserId = primaryUserId; + } + + public ArrayList getUserIds() { + return userIds; + } + + public void setUserIds(ArrayList userIds) { + this.userIds = userIds; + } + + public long getKeyId() { + return keyId; + } + + public void setKeyId(long keyId) { + this.keyId = keyId; + } + + public OpenPgpSignatureResult() { + + } + + public OpenPgpSignatureResult(int signatureStatus, String signatureUserId, + boolean signatureOnly, long keyId, ArrayList userIds) { + this.status = signatureStatus; + this.signatureOnly = signatureOnly; + this.primaryUserId = signatureUserId; + this.keyId = keyId; + this.userIds = userIds; + } + + public OpenPgpSignatureResult(OpenPgpSignatureResult b) { + this.status = b.status; + this.primaryUserId = b.primaryUserId; + this.signatureOnly = b.signatureOnly; + this.keyId = b.keyId; + this.userIds = b.userIds; + } + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel dest, int flags) { + /** + * NOTE: When adding fields in the process of updating this API, make sure to bump + * {@link #PARCELABLE_VERSION}. + */ + dest.writeInt(PARCELABLE_VERSION); + // Inject a placeholder that will store the parcel size from this point on + // (not including the size itself). + int sizePosition = dest.dataPosition(); + dest.writeInt(0); + int startPosition = dest.dataPosition(); + // version 1 + dest.writeInt(status); + dest.writeByte((byte) (signatureOnly ? 1 : 0)); + dest.writeString(primaryUserId); + dest.writeLong(keyId); + // version 2 + dest.writeStringList(userIds); + // Go back and write the size + int parcelableSize = dest.dataPosition() - startPosition; + dest.setDataPosition(sizePosition); + dest.writeInt(parcelableSize); + dest.setDataPosition(startPosition + parcelableSize); + } + + public static final Creator CREATOR = new Creator() { + public OpenPgpSignatureResult createFromParcel(final Parcel source) { + int parcelableVersion = source.readInt(); + int parcelableSize = source.readInt(); + int startPosition = source.dataPosition(); + + OpenPgpSignatureResult vr = new OpenPgpSignatureResult(); + vr.status = source.readInt(); + vr.signatureOnly = source.readByte() == 1; + vr.primaryUserId = source.readString(); + vr.keyId = source.readLong(); + vr.userIds = new ArrayList(); + source.readStringList(vr.userIds); + + // skip over all fields added in future versions of this parcel + source.setDataPosition(startPosition + parcelableSize); + + return vr; + } + + public OpenPgpSignatureResult[] newArray(final int size) { + return new OpenPgpSignatureResult[size]; + } + }; + + @Override + public String toString() { + String out = "\nstatus: " + status; + out += "\nprimaryUserId: " + primaryUserId; + out += "\nuserIds: " + userIds; + out += "\nsignatureOnly: " + signatureOnly; + out += "\nkeyId: " + OpenPgpUtils.convertKeyIdToHex(keyId); + return out; + } + +} diff --git a/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java b/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java new file mode 100644 index 0000000..dbfb797 --- /dev/null +++ b/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java @@ -0,0 +1,370 @@ +/* + * Copyright (C) 2014-2015 Dominik Schürmann + * + * 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.openintents.openpgp.util; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.Intent; +import android.os.AsyncTask; +import android.os.Build; +import android.os.ParcelFileDescriptor; +import android.util.Log; + +import org.openintents.openpgp.IOpenPgpService; +import org.openintents.openpgp.OpenPgpError; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class OpenPgpApi { + + public static final String TAG = "OpenPgp API"; + + public static final String SERVICE_INTENT = "org.openintents.openpgp.IOpenPgpService"; + + /** + * Version history + * --------------- + *

+ * 3: + * - First public stable version + *

+ * 4: + * - No changes to existing methods -> backward compatible + * - Introduction of ACTION_DECRYPT_METADATA, RESULT_METADATA, EXTRA_ORIGINAL_FILENAME, and OpenPgpMetadata parcel + * - Introduction of internal NFC extras: EXTRA_NFC_SIGNED_HASH, EXTRA_NFC_SIG_CREATION_TIMESTAMP + * 5: + * - OpenPgpSignatureResult: new consts SIGNATURE_KEY_REVOKED and SIGNATURE_KEY_EXPIRED + * - OpenPgpSignatureResult: ArrayList userIds + * 6: + * - Deprecate ACTION_SIGN + * - Introduce ACTION_CLEARTEXT_SIGN and ACTION_DETACHED_SIGN + * - New extra for ACTION_DETACHED_SIGN: EXTRA_DETACHED_SIGNATURE + * - New result for ACTION_DECRYPT_VERIFY: RESULT_DETACHED_SIGNATURE + * - New result for ACTION_DECRYPT_VERIFY: RESULT_CHARSET + */ + public static final int API_VERSION = 6; + + /** + * General extras + * -------------- + * + * required extras: + * int EXTRA_API_VERSION (always required) + * + * returned extras: + * int RESULT_CODE (RESULT_CODE_ERROR, RESULT_CODE_SUCCESS or RESULT_CODE_USER_INTERACTION_REQUIRED) + * OpenPgpError RESULT_ERROR (if RESULT_CODE == RESULT_CODE_ERROR) + * PendingIntent RESULT_INTENT (if RESULT_CODE == RESULT_CODE_USER_INTERACTION_REQUIRED) + */ + + /** + * DEPRECATED + * Same as ACTION_CLEARTEXT_SIGN + *

+ * optional extras: + * boolean EXTRA_REQUEST_ASCII_ARMOR (DEPRECATED: this makes no sense here) + * String EXTRA_PASSPHRASE (key passphrase) + */ + public static final String ACTION_SIGN = "org.openintents.openpgp.action.SIGN"; + + /** + * Sign text resulting in a cleartext signature + * Some magic pre-processing of the text is done to convert it to a format usable for + * cleartext signatures per RFC 4880 before the text is actually signed: + * - end cleartext with newline + * - remove whitespaces on line endings + *

+ * optional extras: + * String EXTRA_PASSPHRASE (key passphrase) + */ + public static final String ACTION_CLEARTEXT_SIGN = "org.openintents.openpgp.action.CLEARTEXT_SIGN"; + + /** + * Sign text or binary data resulting in a detached signature. + * No OutputStream necessary for ACTION_DETACHED_SIGN (No magic pre-processing like in ACTION_CLEARTEXT_SIGN)! + * The detached signature is returned separately in RESULT_DETACHED_SIGNATURE. + *

+ * optional extras: + * boolean EXTRA_REQUEST_ASCII_ARMOR (request ascii armor for detached signature) + * String EXTRA_PASSPHRASE (key passphrase) + *

+ * returned extras: + * byte[] RESULT_DETACHED_SIGNATURE + */ + public static final String ACTION_DETACHED_SIGN = "org.openintents.openpgp.action.DETACHED_SIGN"; + + /** + * Encrypt + *

+ * required extras: + * String[] EXTRA_USER_IDS (=emails of recipients, if more than one key has a user_id, a PendingIntent is returned via RESULT_INTENT) + * or + * long[] EXTRA_KEY_IDS + *

+ * optional extras: + * boolean EXTRA_REQUEST_ASCII_ARMOR (request ascii armor for output) + * String EXTRA_PASSPHRASE (key passphrase) + * String EXTRA_ORIGINAL_FILENAME (original filename to be encrypted as metadata) + */ + public static final String ACTION_ENCRYPT = "org.openintents.openpgp.action.ENCRYPT"; + + /** + * Sign and encrypt + *

+ * required extras: + * String[] EXTRA_USER_IDS (=emails of recipients, if more than one key has a user_id, a PendingIntent is returned via RESULT_INTENT) + * or + * long[] EXTRA_KEY_IDS + *

+ * optional extras: + * boolean EXTRA_REQUEST_ASCII_ARMOR (request ascii armor for output) + * String EXTRA_PASSPHRASE (key passphrase) + * String EXTRA_ORIGINAL_FILENAME (original filename to be encrypted as metadata) + */ + public static final String ACTION_SIGN_AND_ENCRYPT = "org.openintents.openpgp.action.SIGN_AND_ENCRYPT"; + + /** + * Decrypts and verifies given input stream. This methods handles encrypted-only, signed-and-encrypted, + * and also signed-only input. + * OutputStream is optional, e.g., for verifying detached signatures! + *

+ * If OpenPgpSignatureResult.getStatus() == OpenPgpSignatureResult.SIGNATURE_KEY_MISSING + * in addition a PendingIntent is returned via RESULT_INTENT to download missing keys. + *

+ * optional extras: + * byte[] EXTRA_DETACHED_SIGNATURE (detached signature) + *

+ * returned extras: + * OpenPgpSignatureResult RESULT_SIGNATURE + * OpenPgpDecryptMetadata RESULT_METADATA + * String RESULT_CHARSET (charset which was specified in the headers of ascii armored input, if any) + */ + public static final String ACTION_DECRYPT_VERIFY = "org.openintents.openpgp.action.DECRYPT_VERIFY"; + + /** + * Decrypts the header of an encrypted file to retrieve metadata such as original filename. + *

+ * This does not decrypt the actual content of the file. + *

+ * returned extras: + * OpenPgpDecryptMetadata RESULT_METADATA + * String RESULT_CHARSET (charset which was specified in the headers of ascii armored input, if any) + */ + public static final String ACTION_DECRYPT_METADATA = "org.openintents.openpgp.action.DECRYPT_METADATA"; + + /** + * Get key ids based on given user ids (=emails) + *

+ * required extras: + * String[] EXTRA_USER_IDS + *

+ * returned extras: + * long[] RESULT_KEY_IDS + */ + public static final String ACTION_GET_KEY_IDS = "org.openintents.openpgp.action.GET_KEY_IDS"; + + /** + * This action returns RESULT_CODE_SUCCESS if the OpenPGP Provider already has the key + * corresponding to the given key id in its database. + *

+ * It returns RESULT_CODE_USER_INTERACTION_REQUIRED if the Provider does not have the key. + * The PendingIntent from RESULT_INTENT can be used to retrieve those from a keyserver. + *

+ * required extras: + * long EXTRA_KEY_ID + */ + public static final String ACTION_GET_KEY = "org.openintents.openpgp.action.GET_KEY"; + + /* Intent extras */ + public static final String EXTRA_API_VERSION = "api_version"; + + public static final String EXTRA_ACCOUNT_NAME = "account_name"; + + // ACTION_DETACHED_SIGN, ENCRYPT, SIGN_AND_ENCRYPT, DECRYPT_VERIFY + // request ASCII Armor for output + // OpenPGP Radix-64, 33 percent overhead compared to binary, see http://tools.ietf.org/html/rfc4880#page-53) + public static final String EXTRA_REQUEST_ASCII_ARMOR = "ascii_armor"; + + // ACTION_DETACHED_SIGN + public static final String RESULT_DETACHED_SIGNATURE = "detached_signature"; + + // ENCRYPT, SIGN_AND_ENCRYPT + public static final String EXTRA_USER_IDS = "user_ids"; + public static final String EXTRA_KEY_IDS = "key_ids"; + // optional extras: + public static final String EXTRA_PASSPHRASE = "passphrase"; + public static final String EXTRA_ORIGINAL_FILENAME = "original_filename"; + + // internal NFC states + public static final String EXTRA_NFC_SIGNED_HASH = "nfc_signed_hash"; + public static final String EXTRA_NFC_SIG_CREATION_TIMESTAMP = "nfc_sig_creation_timestamp"; + public static final String EXTRA_NFC_DECRYPTED_SESSION_KEY = "nfc_decrypted_session_key"; + + // GET_KEY + public static final String EXTRA_KEY_ID = "key_id"; + public static final String RESULT_KEY_IDS = "key_ids"; + + /* Service Intent returns */ + public static final String RESULT_CODE = "result_code"; + + // get actual error object from RESULT_ERROR + public static final int RESULT_CODE_ERROR = 0; + // success! + public static final int RESULT_CODE_SUCCESS = 1; + // get PendingIntent from RESULT_INTENT, start PendingIntent with startIntentSenderForResult, + // and execute service method again in onActivityResult + public static final int RESULT_CODE_USER_INTERACTION_REQUIRED = 2; + + public static final String RESULT_ERROR = "error"; + public static final String RESULT_INTENT = "intent"; + + // DECRYPT_VERIFY + public static final String EXTRA_DETACHED_SIGNATURE = "detached_signature"; + + public static final String RESULT_SIGNATURE = "signature"; + public static final String RESULT_METADATA = "metadata"; + // This will be the charset which was specified in the headers of ascii armored input, if any + public static final String RESULT_CHARSET = "charset"; + + IOpenPgpService mService; + Context mContext; + + public OpenPgpApi(Context context, IOpenPgpService service) { + this.mContext = context; + this.mService = service; + } + + public interface IOpenPgpCallback { + void onReturn(final Intent result); + } + + private class OpenPgpAsyncTask extends AsyncTask { + Intent data; + InputStream is; + OutputStream os; + IOpenPgpCallback callback; + + private OpenPgpAsyncTask(Intent data, InputStream is, OutputStream os, IOpenPgpCallback callback) { + this.data = data; + this.is = is; + this.os = os; + this.callback = callback; + } + + @Override + protected Intent doInBackground(Void... unused) { + return executeApi(data, is, os); + } + + protected void onPostExecute(Intent result) { + callback.onReturn(result); + } + + } + + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public void executeApiAsync(Intent data, InputStream is, OutputStream os, IOpenPgpCallback callback) { + OpenPgpAsyncTask task = new OpenPgpAsyncTask(data, is, os, callback); + + // don't serialize async tasks! + // http://commonsware.com/blog/2012/04/20/asynctask-threading-regression-confirmed.html + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null); + } else { + task.execute((Void[]) null); + } + } + + /** + * InputStream and OutputStreams are always closed after operating on them! + * + * @param data + * @param is + * @param os + * @return + */ + public Intent executeApi(Intent data, InputStream is, OutputStream os) { + ParcelFileDescriptor input = null; + ParcelFileDescriptor output = null; + try { + // always send version from client + data.putExtra(EXTRA_API_VERSION, OpenPgpApi.API_VERSION); + + Intent result; + + // pipe the input and output + if (is != null) { + input = ParcelFileDescriptorUtil.pipeFrom(is, + new ParcelFileDescriptorUtil.IThreadListener() { + + @Override + public void onThreadFinished(Thread thread) { + //Log.d(OpenPgpApi.TAG, "Copy to service finished"); + } + } + ); + } + if (os != null) { + output = ParcelFileDescriptorUtil.pipeTo(os, + new ParcelFileDescriptorUtil.IThreadListener() { + + @Override + public void onThreadFinished(Thread thread) { + //Log.d(OpenPgpApi.TAG, "Service finished writing!"); + } + } + ); + } + + // blocks until result is ready + result = mService.execute(data, input, output); + + // set class loader to current context to allow unparcelling + // of OpenPgpError and OpenPgpSignatureResult + // http://stackoverflow.com/a/3806769 + result.setExtrasClassLoader(mContext.getClassLoader()); + + return result; + } catch (Exception e) { + Log.e(OpenPgpApi.TAG, "Exception in executeApi call", e); + Intent result = new Intent(); + result.putExtra(RESULT_CODE, RESULT_CODE_ERROR); + result.putExtra(RESULT_ERROR, + new OpenPgpError(OpenPgpError.CLIENT_SIDE_ERROR, e.getMessage())); + return result; + } finally { + // close() is required to halt the TransferThread + if (output != null) { + try { + output.close(); + } catch (IOException e) { + Log.e(OpenPgpApi.TAG, "IOException when closing ParcelFileDescriptor!", e); + } + } + if (input != null) { + try { + input.close(); + } catch (IOException e) { + Log.e(OpenPgpApi.TAG, "IOException when closing ParcelFileDescriptor!", e); + } + } + } + } + +} diff --git a/src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java b/src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java new file mode 100644 index 0000000..31ba97c --- /dev/null +++ b/src/main/java/org/openintents/openpgp/util/OpenPgpListPreference.java @@ -0,0 +1,266 @@ +/* + * Copyright (C) 2014-2015 Dominik Schürmann + * + * 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.openintents.openpgp.util; + +import android.app.AlertDialog.Builder; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.pm.ResolveInfo; +import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.preference.DialogPreference; +import android.util.AttributeSet; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ListAdapter; +import android.widget.TextView; +import org.openintents.openpgp.R; + +import java.util.ArrayList; +import java.util.List; + +/** + * Does not extend ListPreference, but is very similar to it! + * http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/preference/ListPreference.java/?v=source + */ +public class OpenPgpListPreference extends DialogPreference { + private static final String OPENKEYCHAIN_PACKAGE = "org.sufficientlysecure.keychain"; + private static final String MARKET_INTENT_URI_BASE = "market://details?id=%s"; + private static final Intent MARKET_INTENT = new Intent(Intent.ACTION_VIEW, Uri.parse( + String.format(MARKET_INTENT_URI_BASE, OPENKEYCHAIN_PACKAGE))); + + private static final ArrayList PROVIDER_BLACKLIST = new ArrayList(); + + static { + // Unfortunately, the current released version of APG includes a broken version of the API + PROVIDER_BLACKLIST.add("org.thialfihar.android.apg"); + } + + private ArrayList mLegacyList = new ArrayList(); + private ArrayList mList = new ArrayList(); + + private String mSelectedPackage; + + public OpenPgpListPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public OpenPgpListPreference(Context context) { + this(context, null); + } + + /** + * Public method to add new entries for legacy applications + * + * @param packageName + * @param simpleName + * @param icon + */ + public void addLegacyProvider(int position, String packageName, String simpleName, Drawable icon) { + mLegacyList.add(position, new OpenPgpProviderEntry(packageName, simpleName, icon)); + } + + @Override + protected void onPrepareDialogBuilder(Builder builder) { + mList.clear(); + + // add "none"-entry + mList.add(0, new OpenPgpProviderEntry("", + getContext().getString(R.string.openpgp_list_preference_none), + getContext().getResources().getDrawable(R.drawable.ic_action_cancel_launchersize))); + + // add all additional (legacy) providers + mList.addAll(mLegacyList); + + // search for OpenPGP providers... + ArrayList providerList = new ArrayList(); + Intent intent = new Intent(OpenPgpApi.SERVICE_INTENT); + List resInfo = getContext().getPackageManager().queryIntentServices(intent, 0); + if (!resInfo.isEmpty()) { + for (ResolveInfo resolveInfo : resInfo) { + if (resolveInfo.serviceInfo == null) + continue; + + String packageName = resolveInfo.serviceInfo.packageName; + String simpleName = String.valueOf(resolveInfo.serviceInfo.loadLabel(getContext() + .getPackageManager())); + Drawable icon = resolveInfo.serviceInfo.loadIcon(getContext().getPackageManager()); + + if (!PROVIDER_BLACKLIST.contains(packageName)) { + providerList.add(new OpenPgpProviderEntry(packageName, simpleName, icon)); + } + } + } + + if (providerList.isEmpty()) { + // add install links if provider list is empty + resInfo = getContext().getPackageManager().queryIntentActivities + (MARKET_INTENT, 0); + for (ResolveInfo resolveInfo : resInfo) { + Intent marketIntent = new Intent(MARKET_INTENT); + marketIntent.setPackage(resolveInfo.activityInfo.packageName); + Drawable icon = resolveInfo.activityInfo.loadIcon(getContext().getPackageManager()); + String marketName = String.valueOf(resolveInfo.activityInfo.applicationInfo + .loadLabel(getContext().getPackageManager())); + String simpleName = String.format(getContext().getString(R.string + .openpgp_install_openkeychain_via), marketName); + mList.add(new OpenPgpProviderEntry(OPENKEYCHAIN_PACKAGE, simpleName, + icon, marketIntent)); + } + } else { + // add provider + mList.addAll(providerList); + } + + // Init ArrayAdapter with OpenPGP Providers + ListAdapter adapter = new ArrayAdapter(getContext(), + android.R.layout.select_dialog_singlechoice, android.R.id.text1, mList) { + public View getView(int position, View convertView, ViewGroup parent) { + // User super class to create the View + View v = super.getView(position, convertView, parent); + TextView tv = (TextView) v.findViewById(android.R.id.text1); + + // Put the image on the TextView + tv.setCompoundDrawablesWithIntrinsicBounds(mList.get(position).icon, null, + null, null); + + // Add margin between image and text (support various screen densities) + int dp10 = (int) (10 * getContext().getResources().getDisplayMetrics().density + 0.5f); + tv.setCompoundDrawablePadding(dp10); + + return v; + } + }; + + builder.setSingleChoiceItems(adapter, getIndexOfProviderList(getValue()), + new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + OpenPgpProviderEntry entry = mList.get(which); + + if (entry.intent != null) { + /* + * Intents are called as activity + * + * Current approach is to assume the user installed the app. + * If he does not, the selected package is not valid. + * + * However applications should always consider this could happen, + * as the user might remove the currently used OpenPGP app. + */ + getContext().startActivity(entry.intent); + } + + mSelectedPackage = entry.packageName; + + /* + * Clicking on an item simulates the positive button click, and dismisses + * the dialog. + */ + OpenPgpListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE); + dialog.dismiss(); + } + }); + + /* + * The typical interaction for list-based dialogs is to have click-on-an-item dismiss the + * dialog instead of the user having to press 'Ok'. + */ + builder.setPositiveButton(null, null); + } + + @Override + protected void onDialogClosed(boolean positiveResult) { + super.onDialogClosed(positiveResult); + + if (positiveResult && (mSelectedPackage != null)) { + if (callChangeListener(mSelectedPackage)) { + setValue(mSelectedPackage); + } + } + } + + private int getIndexOfProviderList(String packageName) { + for (OpenPgpProviderEntry app : mList) { + if (app.packageName.equals(packageName)) { + return mList.indexOf(app); + } + } + + return -1; + } + + public void setValue(String packageName) { + mSelectedPackage = packageName; + persistString(packageName); + } + + public String getValue() { + return mSelectedPackage; + } + + public String getEntry() { + return getEntryByValue(mSelectedPackage); + } + + @Override + protected Object onGetDefaultValue(TypedArray a, int index) { + return a.getString(index); + } + + @Override + protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { + setValue(restoreValue ? getPersistedString(mSelectedPackage) : (String) defaultValue); + } + + public String getEntryByValue(String packageName) { + for (OpenPgpProviderEntry app : mList) { + if (app.packageName.equals(packageName)) { + return app.simpleName; + } + } + + return null; + } + + private static class OpenPgpProviderEntry { + private String packageName; + private String simpleName; + private Drawable icon; + private Intent intent; + + public OpenPgpProviderEntry(String packageName, String simpleName, Drawable icon) { + this.packageName = packageName; + this.simpleName = simpleName; + this.icon = icon; + } + + public OpenPgpProviderEntry(String packageName, String simpleName, Drawable icon, Intent intent) { + this(packageName, simpleName, icon); + this.intent = intent; + } + + @Override + public String toString() { + return simpleName; + } + } +} diff --git a/src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java b/src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java new file mode 100644 index 0000000..bbc8645 --- /dev/null +++ b/src/main/java/org/openintents/openpgp/util/OpenPgpServiceConnection.java @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2014-2015 Dominik Schürmann + * + * 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.openintents.openpgp.util; + +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.IBinder; + +import org.openintents.openpgp.IOpenPgpService; + +public class OpenPgpServiceConnection { + + // callback interface + public interface OnBound { + public void onBound(IOpenPgpService service); + + public void onError(Exception e); + } + + private Context mApplicationContext; + + private IOpenPgpService mService; + private String mProviderPackageName; + + private OnBound mOnBoundListener; + + /** + * Create new connection + * + * @param context + * @param providerPackageName specify package name of OpenPGP provider, + * e.g., "org.sufficientlysecure.keychain" + */ + public OpenPgpServiceConnection(Context context, String providerPackageName) { + this.mApplicationContext = context.getApplicationContext(); + this.mProviderPackageName = providerPackageName; + } + + /** + * Create new connection with callback + * + * @param context + * @param providerPackageName specify package name of OpenPGP provider, + * e.g., "org.sufficientlysecure.keychain" + * @param onBoundListener callback, executed when connection to service has been established + */ + public OpenPgpServiceConnection(Context context, String providerPackageName, + OnBound onBoundListener) { + this(context, providerPackageName); + this.mOnBoundListener = onBoundListener; + } + + public IOpenPgpService getService() { + return mService; + } + + public boolean isBound() { + return (mService != null); + } + + private ServiceConnection mServiceConnection = new ServiceConnection() { + public void onServiceConnected(ComponentName name, IBinder service) { + mService = IOpenPgpService.Stub.asInterface(service); + if (mOnBoundListener != null) { + mOnBoundListener.onBound(mService); + } + } + + public void onServiceDisconnected(ComponentName name) { + mService = null; + } + }; + + /** + * If not already bound, bind to service! + * + * @return + */ + public void bindToService() { + // if not already bound... + if (mService == null) { + try { + Intent serviceIntent = new Intent(OpenPgpApi.SERVICE_INTENT); + // NOTE: setPackage is very important to restrict the intent to this provider only! + serviceIntent.setPackage(mProviderPackageName); + boolean connect = mApplicationContext.bindService(serviceIntent, mServiceConnection, + Context.BIND_AUTO_CREATE); + if (!connect) { + throw new Exception("bindService() returned false!"); + } + } catch (Exception e) { + if (mOnBoundListener != null) { + mOnBoundListener.onError(e); + } + } + } else { + // already bound, but also inform client about it with callback + if (mOnBoundListener != null) { + mOnBoundListener.onBound(mService); + } + } + } + + public void unbindFromService() { + mApplicationContext.unbindService(mServiceConnection); + } + +} diff --git a/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java b/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java new file mode 100644 index 0000000..ef0a88c --- /dev/null +++ b/src/main/java/org/openintents/openpgp/util/OpenPgpUtils.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2014-2015 Dominik Schürmann + * + * 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.openintents.openpgp.util; + +import java.util.List; +import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ResolveInfo; +import android.text.TextUtils; + +public class OpenPgpUtils { + + public static final Pattern PGP_MESSAGE = Pattern.compile( + ".*?(-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----).*", + Pattern.DOTALL); + + public static final Pattern PGP_SIGNED_MESSAGE = Pattern.compile( + ".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*", + Pattern.DOTALL); + + public static final int PARSE_RESULT_NO_PGP = -1; + public static final int PARSE_RESULT_MESSAGE = 0; + public static final int PARSE_RESULT_SIGNED_MESSAGE = 1; + + public static int parseMessage(String message) { + Matcher matcherSigned = PGP_SIGNED_MESSAGE.matcher(message); + Matcher matcherMessage = PGP_MESSAGE.matcher(message); + + if (matcherMessage.matches()) { + return PARSE_RESULT_MESSAGE; + } else if (matcherSigned.matches()) { + return PARSE_RESULT_SIGNED_MESSAGE; + } else { + return PARSE_RESULT_NO_PGP; + } + } + + public static boolean isAvailable(Context context) { + Intent intent = new Intent(OpenPgpApi.SERVICE_INTENT); + List resInfo = context.getPackageManager().queryIntentServices(intent, 0); + return !resInfo.isEmpty(); + } + + public static String convertKeyIdToHex(long keyId) { + return "0x" + convertKeyIdToHex32bit(keyId >> 32) + convertKeyIdToHex32bit(keyId); + } + + private static String convertKeyIdToHex32bit(long keyId) { + String hexString = Long.toHexString(keyId & 0xffffffffL).toLowerCase(Locale.ENGLISH); + while (hexString.length() < 8) { + hexString = "0" + hexString; + } + return hexString; + } + + private static final Pattern USER_ID_PATTERN = Pattern.compile("^(.*?)(?: (\\[.*\\]))?(?: \\((.*)\\))?(?: <(.*)>)?$"); + + /** + * Splits userId string into naming part, email part, and comment part + *

+ * User ID matching: + * http://fiddle.re/t4p6f + * + * @param userId + * @return theParsedUserInfo + */ + public static UserInfo splitUserId(final String userId) { + if (!TextUtils.isEmpty(userId)) { + final Matcher matcher = USER_ID_PATTERN.matcher(userId); + if (matcher.matches()) { + return new UserInfo(matcher.group(1), matcher.group(4), matcher.group(3)); + } + } + return new UserInfo(null, null, null); + } + + public static class UserInfo { + public final String name; + public final String email; + public final String comment; + + public UserInfo(String name, String email, String comment) { + this.name = name; + this.email = email; + this.comment = comment; + } + } +} diff --git a/src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java b/src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java new file mode 100644 index 0000000..b9492f9 --- /dev/null +++ b/src/main/java/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2014-2015 Dominik Schürmann + * 2013 Florian Schmaus + * + * 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.openintents.openpgp.util; + +import android.os.ParcelFileDescriptor; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Partially based on Stackoverflow: Transfer InputStream to another Service (across process boundaries) + **/ +public class ParcelFileDescriptorUtil { + + public interface IThreadListener { + void onThreadFinished(final Thread thread); + } + + public static ParcelFileDescriptor pipeFrom(InputStream inputStream, IThreadListener listener) + throws IOException { + ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe(); + ParcelFileDescriptor readSide = pipe[0]; + ParcelFileDescriptor writeSide = pipe[1]; + + // start the transfer thread + new TransferThread(inputStream, new ParcelFileDescriptor.AutoCloseOutputStream(writeSide), + listener) + .start(); + + return readSide; + } + + public static ParcelFileDescriptor pipeTo(OutputStream outputStream, IThreadListener listener) + throws IOException { + ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe(); + ParcelFileDescriptor readSide = pipe[0]; + ParcelFileDescriptor writeSide = pipe[1]; + + // start the transfer thread + new TransferThread(new ParcelFileDescriptor.AutoCloseInputStream(readSide), outputStream, + listener) + .start(); + + return writeSide; + } + + static class TransferThread extends Thread { + final InputStream mIn; + final OutputStream mOut; + final IThreadListener mListener; + + TransferThread(InputStream in, OutputStream out, IThreadListener listener) { + super("ParcelFileDescriptor Transfer Thread"); + mIn = in; + mOut = out; + mListener = listener; + setDaemon(true); + } + + @Override + public void run() { + byte[] buf = new byte[1024]; + int len; + + try { + while ((len = mIn.read(buf)) > 0) { + mOut.write(buf, 0, len); + } + mOut.flush(); // just to be safe + } catch (IOException e) { + //Log.e(OpenPgpApi.TAG, "TransferThread" + getId() + ": writing failed", e); + } finally { + try { + mIn.close(); + } catch (IOException e) { + //Log.e(OpenPgpApi.TAG, "TransferThread" + getId(), e); + } + try { + mOut.close(); + } catch (IOException e) { + //Log.e(OpenPgpApi.TAG, "TransferThread" + getId(), e); + } + } + if (mListener != null) { + //Log.d(OpenPgpApi.TAG, "TransferThread " + getId() + " finished!"); + mListener.onThreadFinished(this); + } + } + } +} diff --git a/src/main/res/drawable-hdpi/ic_action_cancel_launchersize.png b/src/main/res/drawable-hdpi/ic_action_cancel_launchersize.png new file mode 100644 index 0000000..71b9118 Binary files /dev/null and b/src/main/res/drawable-hdpi/ic_action_cancel_launchersize.png differ diff --git a/src/main/res/drawable-hdpi/ic_action_cancel_launchersize_light.png b/src/main/res/drawable-hdpi/ic_action_cancel_launchersize_light.png new file mode 100644 index 0000000..73b1d08 Binary files /dev/null and b/src/main/res/drawable-hdpi/ic_action_cancel_launchersize_light.png differ diff --git a/src/main/res/drawable-mdpi/ic_action_cancel_launchersize.png b/src/main/res/drawable-mdpi/ic_action_cancel_launchersize.png new file mode 100644 index 0000000..270abf4 Binary files /dev/null and b/src/main/res/drawable-mdpi/ic_action_cancel_launchersize.png differ diff --git a/src/main/res/drawable-mdpi/ic_action_cancel_launchersize_light.png b/src/main/res/drawable-mdpi/ic_action_cancel_launchersize_light.png new file mode 100644 index 0000000..d841821 Binary files /dev/null and b/src/main/res/drawable-mdpi/ic_action_cancel_launchersize_light.png differ diff --git a/src/main/res/drawable-xhdpi/ic_action_cancel_launchersize.png b/src/main/res/drawable-xhdpi/ic_action_cancel_launchersize.png new file mode 100644 index 0000000..1e3571f Binary files /dev/null and b/src/main/res/drawable-xhdpi/ic_action_cancel_launchersize.png differ diff --git a/src/main/res/drawable-xhdpi/ic_action_cancel_launchersize_light.png b/src/main/res/drawable-xhdpi/ic_action_cancel_launchersize_light.png new file mode 100644 index 0000000..d505046 Binary files /dev/null and b/src/main/res/drawable-xhdpi/ic_action_cancel_launchersize_light.png differ diff --git a/src/main/res/drawable-xxhdpi/ic_action_cancel_launchersize.png b/src/main/res/drawable-xxhdpi/ic_action_cancel_launchersize.png new file mode 100644 index 0000000..5204460 Binary files /dev/null and b/src/main/res/drawable-xxhdpi/ic_action_cancel_launchersize.png differ diff --git a/src/main/res/drawable-xxhdpi/ic_action_cancel_launchersize_light.png b/src/main/res/drawable-xxhdpi/ic_action_cancel_launchersize_light.png new file mode 100644 index 0000000..d6fb86b Binary files /dev/null and b/src/main/res/drawable-xxhdpi/ic_action_cancel_launchersize_light.png differ diff --git a/src/main/res/values-cs/strings.xml b/src/main/res/values-cs/strings.xml new file mode 100644 index 0000000..c9fe1fa --- /dev/null +++ b/src/main/res/values-cs/strings.xml @@ -0,0 +1,5 @@ + + + Žádný + Instalovat OpenKeychain pomocí %s + diff --git a/src/main/res/values-de/strings.xml b/src/main/res/values-de/strings.xml new file mode 100644 index 0000000..91e800a --- /dev/null +++ b/src/main/res/values-de/strings.xml @@ -0,0 +1,5 @@ + + + Keine Auswahl + Installiere OpenKeychain mit %s + diff --git a/src/main/res/values-es/strings.xml b/src/main/res/values-es/strings.xml new file mode 100644 index 0000000..da8979b --- /dev/null +++ b/src/main/res/values-es/strings.xml @@ -0,0 +1,5 @@ + + + Ninguno + Instalar OpenKeychain mediante %s + diff --git a/src/main/res/values-et/strings.xml b/src/main/res/values-et/strings.xml new file mode 100644 index 0000000..c757504 --- /dev/null +++ b/src/main/res/values-et/strings.xml @@ -0,0 +1,2 @@ + + diff --git a/src/main/res/values-fi/strings.xml b/src/main/res/values-fi/strings.xml new file mode 100644 index 0000000..c757504 --- /dev/null +++ b/src/main/res/values-fi/strings.xml @@ -0,0 +1,2 @@ + + diff --git a/src/main/res/values-fr/strings.xml b/src/main/res/values-fr/strings.xml new file mode 100644 index 0000000..9b36df2 --- /dev/null +++ b/src/main/res/values-fr/strings.xml @@ -0,0 +1,5 @@ + + + Aucun + Installer OpenKeychain par %s + diff --git a/src/main/res/values-is/strings.xml b/src/main/res/values-is/strings.xml new file mode 100644 index 0000000..c757504 --- /dev/null +++ b/src/main/res/values-is/strings.xml @@ -0,0 +1,2 @@ + + diff --git a/src/main/res/values-it/strings.xml b/src/main/res/values-it/strings.xml new file mode 100644 index 0000000..23e8e80 --- /dev/null +++ b/src/main/res/values-it/strings.xml @@ -0,0 +1,5 @@ + + + Nessuno + Installa OpenKeychain via %s + diff --git a/src/main/res/values-ja/strings.xml b/src/main/res/values-ja/strings.xml new file mode 100644 index 0000000..5e337f5 --- /dev/null +++ b/src/main/res/values-ja/strings.xml @@ -0,0 +1,5 @@ + + + 無し + %s 経由でOpenKeychainをインストール + diff --git a/src/main/res/values-nl/strings.xml b/src/main/res/values-nl/strings.xml new file mode 100644 index 0000000..c757504 --- /dev/null +++ b/src/main/res/values-nl/strings.xml @@ -0,0 +1,2 @@ + + diff --git a/src/main/res/values-pl/strings.xml b/src/main/res/values-pl/strings.xml new file mode 100644 index 0000000..c757504 --- /dev/null +++ b/src/main/res/values-pl/strings.xml @@ -0,0 +1,2 @@ + + diff --git a/src/main/res/values-pt/strings.xml b/src/main/res/values-pt/strings.xml new file mode 100644 index 0000000..c757504 --- /dev/null +++ b/src/main/res/values-pt/strings.xml @@ -0,0 +1,2 @@ + + diff --git a/src/main/res/values-ru/strings.xml b/src/main/res/values-ru/strings.xml new file mode 100644 index 0000000..e8fd1dd --- /dev/null +++ b/src/main/res/values-ru/strings.xml @@ -0,0 +1,5 @@ + + + Нет + Установить OpenKeychain через %s + diff --git a/src/main/res/values-sl/strings.xml b/src/main/res/values-sl/strings.xml new file mode 100644 index 0000000..20bf70b --- /dev/null +++ b/src/main/res/values-sl/strings.xml @@ -0,0 +1,5 @@ + + + Brez + Namesti OpenKeychain prek %s + diff --git a/src/main/res/values-tr/strings.xml b/src/main/res/values-tr/strings.xml new file mode 100644 index 0000000..c757504 --- /dev/null +++ b/src/main/res/values-tr/strings.xml @@ -0,0 +1,2 @@ + + diff --git a/src/main/res/values-uk/strings.xml b/src/main/res/values-uk/strings.xml new file mode 100644 index 0000000..baf600a --- /dev/null +++ b/src/main/res/values-uk/strings.xml @@ -0,0 +1,5 @@ + + + Жоден + Встановити OpenKeychain через %s + diff --git a/src/main/res/values-zh/strings.xml b/src/main/res/values-zh/strings.xml new file mode 100644 index 0000000..c757504 --- /dev/null +++ b/src/main/res/values-zh/strings.xml @@ -0,0 +1,2 @@ + + diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml new file mode 100644 index 0000000..0119831 --- /dev/null +++ b/src/main/res/values/strings.xml @@ -0,0 +1,7 @@ + + + + None + Install OpenKeychain via %s + + \ No newline at end of file diff --git a/src/main/test/org/openintents/openpgp/OpenPgpUtilsTest.java b/src/main/test/org/openintents/openpgp/OpenPgpUtilsTest.java new file mode 100644 index 0000000..f115398 --- /dev/null +++ b/src/main/test/org/openintents/openpgp/OpenPgpUtilsTest.java @@ -0,0 +1,50 @@ +package test.org.openintents.openpgp; + +import org.openintents.openpgp.util.OpenPgpUtils; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; + +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openintents.openpgp.util.OpenPgpUtils; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; + +@RunWith(AndroidJUnit4.class) +public class OpenPgpUtilsTest { + @Test + public void splitCompleteUserIdShouldReturnAll3Components() throws Exception { + OpenPgpUtils.UserInfo info = OpenPgpUtils.splitUserId("Max Mustermann (this is a comment) "); + assertEquals("Max Mustermann", info.name); + assertEquals("this is a comment", info.comment); + assertEquals("max@example.com", info.email); + } + + @Test + public void splitUserIdWithAllButCommentShouldReturnNameAndEmail() throws Exception { + OpenPgpUtils.UserInfo info = OpenPgpUtils.splitUserId("Max Mustermann "); + assertEquals("Max Mustermann", info.name); + assertNull(info.comment); + assertEquals("max@example.com", info.email); + } + + @Test + public void splitUserIdWithAllButEmailShouldReturnNameAndComment() throws Exception { + OpenPgpUtils.UserInfo info = OpenPgpUtils.splitUserId("Max Mustermann (this is a comment)"); + assertEquals(info.name, "Max Mustermann"); + assertEquals(info.comment, "this is a comment"); + assertNull(info.email); + } + + @Test + public void splitUserIdWithOnlyNameShouldReturnNameOnly() throws Exception { + OpenPgpUtils.UserInfo info = OpenPgpUtils.splitUserId("Max Mustermann [this is a nothing]"); + assertEquals("Max Mustermann", info.name); + assertNull(info.comment); + assertNull(info.email); + } +} \ No newline at end of file -- cgit v1.2.3