aboutsummaryrefslogtreecommitdiffstats
path: root/libs/openpgp-api-lib/src
diff options
context:
space:
mode:
authorSam Whited <sam@samwhited.com>2014-10-28 12:16:35 -0400
committerSam Whited <sam@samwhited.com>2014-10-28 12:16:35 -0400
commite3f4742ed81264b1bd40211ac7056909a8ce25ca (patch)
tree15796ee0711518467fe2d975f8b4d9a860a17bac /libs/openpgp-api-lib/src
parent4a1bae3a69a76cae0341b821e3b880e5b8b8f5a3 (diff)
parent0be263d5d3effd2df5f976fa4a127017268749cc (diff)
Subtree merged in openpgp api lib
Diffstat (limited to '')
-rw-r--r--libs/openpgp-api-lib/src/org/openintents/openpgp/IOpenPgpService.aidl24
-rw-r--r--libs/openpgp-api-lib/src/org/openintents/openpgp/OpenPgpError.java118
-rw-r--r--libs/openpgp-api-lib/src/org/openintents/openpgp/OpenPgpMetadata.java132
-rw-r--r--libs/openpgp-api-lib/src/org/openintents/openpgp/OpenPgpSignatureResult.java183
-rw-r--r--libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpApi.java306
-rw-r--r--libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpListPreference.java257
-rw-r--r--libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpServiceConnection.java124
-rw-r--r--libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpUtils.java76
-rw-r--r--libs/openpgp-api-lib/src/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java106
9 files changed, 1326 insertions, 0 deletions
diff --git a/libs/openpgp-api-lib/src/org/openintents/openpgp/IOpenPgpService.aidl b/libs/openpgp-api-lib/src/org/openintents/openpgp/IOpenPgpService.aidl
new file mode 100644
index 00000000..7ee79d6a
--- /dev/null
+++ b/libs/openpgp-api-lib/src/org/openintents/openpgp/IOpenPgpService.aidl
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
+ *
+ * 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/libs/openpgp-api-lib/src/org/openintents/openpgp/OpenPgpError.java b/libs/openpgp-api-lib/src/org/openintents/openpgp/OpenPgpError.java
new file mode 100644
index 00000000..b894a460
--- /dev/null
+++ b/libs/openpgp-api-lib/src/org/openintents/openpgp/OpenPgpError.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
+ *
+ * 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<OpenPgpError> CREATOR = new Creator<OpenPgpError>() {
+ 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/libs/openpgp-api-lib/src/org/openintents/openpgp/OpenPgpMetadata.java b/libs/openpgp-api-lib/src/org/openintents/openpgp/OpenPgpMetadata.java
new file mode 100644
index 00000000..2a99e406
--- /dev/null
+++ b/libs/openpgp-api-lib/src/org/openintents/openpgp/OpenPgpMetadata.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
+ *
+ * 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<OpenPgpMetadata> CREATOR = new Creator<OpenPgpMetadata>() {
+ 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/libs/openpgp-api-lib/src/org/openintents/openpgp/OpenPgpSignatureResult.java b/libs/openpgp-api-lib/src/org/openintents/openpgp/OpenPgpSignatureResult.java
new file mode 100644
index 00000000..dbcd74b6
--- /dev/null
+++ b/libs/openpgp-api-lib/src/org/openintents/openpgp/OpenPgpSignatureResult.java
@@ -0,0 +1,183 @@
+/*
+ * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
+ *
+ * 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<String> 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<String> getUserIds() {
+ return userIds;
+ }
+
+ public void setUserIds(ArrayList<String> 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<String> 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<OpenPgpSignatureResult> CREATOR = new Creator<OpenPgpSignatureResult>() {
+ 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<String>();
+ 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/libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpApi.java b/libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpApi.java
new file mode 100644
index 00000000..3e18ab0c
--- /dev/null
+++ b/libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpApi.java
@@ -0,0 +1,306 @@
+/*
+ * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
+ *
+ * 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.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
+ * ---------------
+ * <p/>
+ * 3:
+ * - first public stable version
+ * <p/>
+ * 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<String> userIds
+ */
+ public static final int API_VERSION = 5;
+
+ /**
+ * 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)
+ */
+
+ /**
+ * Sign only
+ * <p/>
+ * optional extras:
+ * boolean EXTRA_REQUEST_ASCII_ARMOR (request ascii armor for output)
+ * String EXTRA_PASSPHRASE (key passphrase)
+ */
+ public static final String ACTION_SIGN = "org.openintents.openpgp.action.SIGN";
+
+ /**
+ * Encrypt
+ * <p/>
+ * 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
+ * <p/>
+ * 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
+ * <p/>
+ * 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
+ * <p/>
+ * 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.
+ * <p/>
+ * If OpenPgpSignatureResult.getStatus() == OpenPgpSignatureResult.SIGNATURE_KEY_MISSING
+ * in addition a PendingIntent is returned via RESULT_INTENT to download missing keys.
+ * <p/>
+ * optional extras:
+ * boolean EXTRA_REQUEST_ASCII_ARMOR (request ascii armor for output)
+ * <p/>
+ * returned extras:
+ * OpenPgpSignatureResult RESULT_SIGNATURE
+ * OpenPgpDecryptMetadata RESULT_METADATA
+ */
+ 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.
+ * <p/>
+ * This does not decrypt the actual content of the file.
+ * <p/>
+ * returned extras:
+ * OpenPgpDecryptMetadata RESULT_METADATA
+ */
+ public static final String ACTION_DECRYPT_METADATA = "org.openintents.openpgp.action.DECRYPT_METADATA";
+
+ /**
+ * Get key ids based on given user ids (=emails)
+ * <p/>
+ * required extras:
+ * String[] EXTRA_USER_IDS
+ * <p/>
+ * 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.
+ * <p/>
+ * 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.
+ * <p/>
+ * 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";
+
+ // 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";
+
+ // 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 RESULT_SIGNATURE = "signature";
+ public static final String RESULT_METADATA = "metadata";
+
+ 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<Void, Integer, Intent> {
+ 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);
+ }
+ }
+
+ public Intent executeApi(Intent data, InputStream is, OutputStream os) {
+ try {
+ data.putExtra(EXTRA_API_VERSION, OpenPgpApi.API_VERSION);
+
+ Intent result;
+
+ // pipe the input and output
+ ParcelFileDescriptor input = null;
+ if (is != null) {
+ input = ParcelFileDescriptorUtil.pipeFrom(is,
+ new ParcelFileDescriptorUtil.IThreadListener() {
+
+ @Override
+ public void onThreadFinished(Thread thread) {
+ //Log.d(OpenPgpApi.TAG, "Copy to service finished");
+ }
+ }
+ );
+ }
+ ParcelFileDescriptor output = null;
+ 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);
+ // close() is required to halt the TransferThread
+ if (output != null) {
+ output.close();
+ }
+ // TODO: close input?
+
+ // 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;
+ }
+ }
+
+}
diff --git a/libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpListPreference.java b/libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpListPreference.java
new file mode 100644
index 00000000..cf586462
--- /dev/null
+++ b/libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpListPreference.java
@@ -0,0 +1,257 @@
+/*
+ * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
+ *
+ * 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 ArrayList<OpenPgpProviderEntry> mLegacyList = new ArrayList<OpenPgpProviderEntry>();
+ private ArrayList<OpenPgpProviderEntry> mList = new ArrayList<OpenPgpProviderEntry>();
+
+ 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<OpenPgpProviderEntry> providerList = new ArrayList<OpenPgpProviderEntry>();
+ Intent intent = new Intent(OpenPgpApi.SERVICE_INTENT);
+ List<ResolveInfo> 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());
+
+ 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<OpenPgpProviderEntry>(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/libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpServiceConnection.java b/libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpServiceConnection.java
new file mode 100644
index 00000000..15096d9e
--- /dev/null
+++ b/libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpServiceConnection.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
+ *
+ * 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/libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpUtils.java b/libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpUtils.java
new file mode 100644
index 00000000..416b2841
--- /dev/null
+++ b/libs/openpgp-api-lib/src/org/openintents/openpgp/util/OpenPgpUtils.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
+ *
+ * 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;
+
+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<ResolveInfo> resInfo = context.getPackageManager().queryIntentServices(intent, 0);
+ if (!resInfo.isEmpty()) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ 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;
+ }
+}
diff --git a/libs/openpgp-api-lib/src/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java b/libs/openpgp-api-lib/src/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java
new file mode 100644
index 00000000..4fd4b39a
--- /dev/null
+++ b/libs/openpgp-api-lib/src/org/openintents/openpgp/util/ParcelFileDescriptorUtil.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
+ * 2013 Florian Schmaus <flo@geekplace.eu>
+ *
+ * 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 <a href="http://stackoverflow.com/questions/18212152/">Stackoverflow: Transfer InputStream to another Service (across process boundaries)</a>
+ **/
+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);
+ }
+ }
+ }
+}