From bf7fb08bca2abe85c9ed16592b1dc20fc51772eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 14 Sep 2013 02:08:06 +0200 Subject: API update --- .../org/openintents/openpgp/IOpenPgpService.aidl | 4 +- .../src/org/openintents/openpgp/OpenPgpHelper.java | 52 ++++++ .../openintents/openpgp/OpenPgpListPreference.java | 182 +++++++++++++++++++++ .../openpgp/OpenPgpServiceConnection.java | 4 + .../openpgp/OpenPgpSignatureResult.java | 43 +++-- .../keychain/demo/OpenPgpProviderActivity.java | 2 +- 6 files changed, 269 insertions(+), 18 deletions(-) create mode 100644 OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpHelper.java create mode 100644 OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpListPreference.java (limited to 'OpenPGP-Keychain-API-Demo') diff --git a/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/IOpenPgpService.aidl b/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/IOpenPgpService.aidl index 1d54ac77f..bc5220985 100644 --- a/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/IOpenPgpService.aidl +++ b/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/IOpenPgpService.aidl @@ -51,7 +51,7 @@ interface IOpenPgpService { oneway void sign(in byte[] inputBytes, in boolean asciiArmor, in IOpenPgpCallback callback); /** - * Encrypt and sign + * Sign then encrypt * * @param inputBytes * Byte array you want to encrypt @@ -64,7 +64,7 @@ interface IOpenPgpService { * @param callback * Callback where to return results */ - oneway void encryptAndSign(in byte[] inputBytes, in String[] encryptionUserIds, in boolean asciiArmor, in IOpenPgpCallback callback); + oneway void signAndEncrypt(in byte[] inputBytes, in String[] encryptionUserIds, in boolean asciiArmor, in IOpenPgpCallback callback); /** * Decrypts and verifies given input bytes. If no signature is present this method diff --git a/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpHelper.java b/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpHelper.java new file mode 100644 index 000000000..56c4a4dca --- /dev/null +++ b/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpHelper.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2013 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 java.util.List; +import java.util.regex.Pattern; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ResolveInfo; + +public class OpenPgpHelper { + private Context context; + + public static Pattern PGP_MESSAGE = Pattern.compile( + ".*?(-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----).*", Pattern.DOTALL); + + public static Pattern PGP_SIGNED_MESSAGE = Pattern + .compile( + ".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*", + Pattern.DOTALL); + + public OpenPgpHelper(Context context) { + super(); + this.context = context; + } + + public boolean isAvailable() { + Intent intent = new Intent(IOpenPgpService.class.getName()); + List resInfo = context.getPackageManager().queryIntentServices(intent, 0); + if (!resInfo.isEmpty()) { + return true; + } else { + return false; + } + } + +} diff --git a/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpListPreference.java b/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpListPreference.java new file mode 100644 index 000000000..4d40616dd --- /dev/null +++ b/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpListPreference.java @@ -0,0 +1,182 @@ +/* + * Copyright (C) 2013 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 java.util.ArrayList; +import java.util.List; + +import android.app.AlertDialog.Builder; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.pm.ResolveInfo; +import android.graphics.drawable.Drawable; +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; + +public class OpenPgpListPreference extends DialogPreference { + static final Intent intent = new Intent(IOpenPgpService.class.getName()); + + ArrayList mProviderList = new ArrayList(); + private String mSelectedPackage; + + public OpenPgpListPreference(Context context, AttributeSet attrs) { + super(context, attrs); + + List resInfo = context.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(context.getPackageManager())); + Drawable icon = resolveInfo.serviceInfo.loadIcon(context.getPackageManager()); + mProviderList.add(new OpenPgpProviderEntry(packageName, simpleName, icon)); + } + } + } + + public OpenPgpListPreference(Context context) { + this(context, null); + } + + /** + * Can be used to add "no selection" + * + * @param packageName + * @param simpleName + * @param icon + */ + public void addProvider(int position, String packageName, String simpleName, Drawable icon) { + mProviderList.add(position, new OpenPgpProviderEntry(packageName, simpleName, icon)); + } + + @Override + protected void onPrepareDialogBuilder(Builder builder) { + // Init ArrayAdapter with OpenPGP Providers + ListAdapter adapter = new ArrayAdapter(getContext(), + android.R.layout.select_dialog_singlechoice, android.R.id.text1, mProviderList) { + 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(mProviderList.get(position).icon, + null, null, null); + + // Add margin between image and text (support various screen + // densities) + int dp5 = (int) (5 * getContext().getResources().getDisplayMetrics().density + 0.5f); + tv.setCompoundDrawablePadding(dp5); + + return v; + } + }; + + builder.setSingleChoiceItems(adapter, getIndexOfProviderList(getValue()), + new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + mSelectedPackage = mProviderList.get(which).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 : mProviderList) { + if (app.packageName.equals(packageName)) { + return mProviderList.indexOf(app); + } + } + + return -1; + } + + public void setValue(String packageName) { + mSelectedPackage = packageName; + persistString(packageName); + } + + public String getValue() { + return mSelectedPackage; + } + + public String getEntry() { + return getEntryByValue(mSelectedPackage); + } + + public String getEntryByValue(String packageName) { + for (OpenPgpProviderEntry app : mProviderList) { + if (app.packageName.equals(packageName)) { + return app.simpleName; + } + } + + return null; + } + + private static class OpenPgpProviderEntry { + private String packageName; + private String simpleName; + private Drawable icon; + + public OpenPgpProviderEntry(String packageName, String simpleName, Drawable icon) { + this.packageName = packageName; + this.simpleName = simpleName; + this.icon = icon; + } + + @Override + public String toString() { + return simpleName; + } + } +} diff --git a/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpServiceConnection.java b/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpServiceConnection.java index 9f4fec470..56a922d73 100644 --- a/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpServiceConnection.java +++ b/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpServiceConnection.java @@ -43,6 +43,10 @@ public class OpenPgpServiceConnection { return mService; } + public boolean isBound() { + return bound; + } + private ServiceConnection mCryptoServiceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName name, IBinder service) { mService = IOpenPgpService.Stub.asInterface(service); diff --git a/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpSignatureResult.java b/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpSignatureResult.java index d0712df1c..f0528572d 100644 --- a/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpSignatureResult.java +++ b/OpenPGP-Keychain-API-Demo/src/org/openintents/openpgp/OpenPgpSignatureResult.java @@ -20,26 +20,40 @@ import android.os.Parcel; import android.os.Parcelable; public class OpenPgpSignatureResult implements Parcelable { + public static final int SIGNATURE_ERROR = 0; + public static final int SIGNATURE_SUCCESS = 1; + public static final int SIGNATURE_UNKNOWN = 2; + int signatureStatus; String signatureUserId; - boolean signatureSuccess; - boolean signatureUnknown; + boolean signatureOnly; + + public int getSignatureStatus() { + return signatureStatus; + } + + public String getSignatureUserId() { + return signatureUserId; + } + + public boolean isSignatureOnly() { + return signatureOnly; + } public OpenPgpSignatureResult() { } - public OpenPgpSignatureResult(String signatureUserId, boolean signatureSuccess, - boolean signatureUnknown) { + public OpenPgpSignatureResult(int signatureStatus, String signatureUserId, boolean signatureOnly) { + this.signatureStatus = signatureStatus; this.signatureUserId = signatureUserId; - this.signatureSuccess = signatureSuccess; - this.signatureUnknown = signatureUnknown; + this.signatureOnly = signatureOnly; } public OpenPgpSignatureResult(OpenPgpSignatureResult b) { + this.signatureStatus = b.signatureStatus; this.signatureUserId = b.signatureUserId; - this.signatureSuccess = b.signatureSuccess; - this.signatureUnknown = b.signatureUnknown; + this.signatureOnly = b.signatureOnly; } public int describeContents() { @@ -47,18 +61,17 @@ public class OpenPgpSignatureResult implements Parcelable { } public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(signatureStatus); dest.writeString(signatureUserId); - - dest.writeByte((byte) (signatureSuccess ? 1 : 0)); - dest.writeByte((byte) (signatureUnknown ? 1 : 0)); + dest.writeByte((byte) (signatureOnly ? 1 : 0)); } public static final Creator CREATOR = new Creator() { public OpenPgpSignatureResult createFromParcel(final Parcel source) { OpenPgpSignatureResult vr = new OpenPgpSignatureResult(); + vr.signatureStatus = source.readInt(); vr.signatureUserId = source.readString(); - vr.signatureSuccess = source.readByte() == 1; - vr.signatureUnknown = source.readByte() == 1; + vr.signatureOnly = source.readByte() == 1; return vr; } @@ -70,9 +83,9 @@ public class OpenPgpSignatureResult implements Parcelable { @Override public String toString() { String out = new String(); + out += "\nsignatureStatus: " + signatureStatus; out += "signatureUserId: " + signatureUserId; - out += "\nsignatureSuccess: " + signatureSuccess; - out += "\nsignatureUnknown: " + signatureUnknown; + out += "\nsignatureOnly: " + signatureOnly; return out; } diff --git a/OpenPGP-Keychain-API-Demo/src/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java b/OpenPGP-Keychain-API-Demo/src/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java index 2c2b86835..a5f1072bc 100644 --- a/OpenPGP-Keychain-API-Demo/src/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java +++ b/OpenPGP-Keychain-API-Demo/src/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java @@ -159,7 +159,7 @@ public class OpenPgpProviderActivity extends Activity { byte[] inputBytes = mMessage.getText().toString().getBytes(); try { - mCryptoServiceConnection.getService().encryptAndSign(inputBytes, + mCryptoServiceConnection.getService().signAndEncrypt(inputBytes, mEncryptUserIds.getText().toString().split(","), true, encryptCallback); } catch (RemoteException e) { Log.e(Constants.TAG, "CryptoProviderDemo", e); -- cgit v1.2.3