API update

This commit is contained in:
Dominik Schürmann 2013-09-14 02:08:06 +02:00
parent a4ae976284
commit bf7fb08bca
12 changed files with 555 additions and 46 deletions

View File

@ -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

View File

@ -0,0 +1,52 @@
/*
* Copyright (C) 2013 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 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<ResolveInfo> resInfo = context.getPackageManager().queryIntentServices(intent, 0);
if (!resInfo.isEmpty()) {
return true;
} else {
return false;
}
}
}

View File

@ -0,0 +1,182 @@
/*
* Copyright (C) 2013 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 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<OpenPgpProviderEntry> mProviderList = new ArrayList<OpenPgpProviderEntry>();
private String mSelectedPackage;
public OpenPgpListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
List<ResolveInfo> 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<OpenPgpProviderEntry>(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;
}
}
}

View File

@ -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);

View File

@ -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<OpenPgpSignatureResult> CREATOR = new Creator<OpenPgpSignatureResult>() {
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;
}

View File

@ -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);

View File

@ -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

View File

@ -0,0 +1,52 @@
/*
* Copyright (C) 2013 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 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<ResolveInfo> resInfo = context.getPackageManager().queryIntentServices(intent, 0);
if (!resInfo.isEmpty()) {
return true;
} else {
return false;
}
}
}

View File

@ -0,0 +1,182 @@
/*
* Copyright (C) 2013 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 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<OpenPgpProviderEntry> mProviderList = new ArrayList<OpenPgpProviderEntry>();
private String mSelectedPackage;
public OpenPgpListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
List<ResolveInfo> 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<OpenPgpProviderEntry>(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;
}
}
}

View File

@ -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);

View File

@ -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<OpenPgpSignatureResult> CREATOR = new Creator<OpenPgpSignatureResult>() {
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;
}

View File

@ -461,6 +461,9 @@ public class OpenPgpService extends Service {
// get signature informations from bundle
boolean signature = outputBundle.getBoolean(KeychainIntentService.RESULT_SIGNATURE);
OpenPgpSignatureResult sigResult = null;
if (signature) {
long signatureKeyId = outputBundle
.getLong(KeychainIntentService.RESULT_SIGNATURE_KEY_ID);
String signatureUserId = outputBundle
@ -470,10 +473,14 @@ public class OpenPgpService extends Service {
boolean signatureUnknown = outputBundle
.getBoolean(KeychainIntentService.RESULT_SIGNATURE_UNKNOWN);
OpenPgpSignatureResult sigResult = null;
if (signature) {
sigResult = new OpenPgpSignatureResult(signatureUserId, signatureSuccess,
signatureUnknown);
int signatureStatus = OpenPgpSignatureResult.SIGNATURE_ERROR;
if (signatureSuccess) {
signatureStatus = OpenPgpSignatureResult.SIGNATURE_SUCCESS;
} else if (signatureUnknown) {
signatureStatus = OpenPgpSignatureResult.SIGNATURE_UNKNOWN;
}
sigResult = new OpenPgpSignatureResult(signatureStatus, signatureUserId, signedOnly);
}
// return over handler on client side
@ -514,7 +521,7 @@ public class OpenPgpService extends Service {
}
@Override
public void encryptAndSign(final byte[] inputBytes, final String[] encryptionUserIds,
public void signAndEncrypt(final byte[] inputBytes, final String[] encryptionUserIds,
final boolean asciiArmor, final IOpenPgpCallback callback) throws RemoteException {
final AppSettings settings = getAppSettings();