mirror of
https://github.com/moparisthebest/open-keychain
synced 2024-12-25 00:18:51 -05:00
API update
This commit is contained in:
parent
a4ae976284
commit
bf7fb08bca
@ -51,7 +51,7 @@ interface IOpenPgpService {
|
|||||||
oneway void sign(in byte[] inputBytes, in boolean asciiArmor, in IOpenPgpCallback callback);
|
oneway void sign(in byte[] inputBytes, in boolean asciiArmor, in IOpenPgpCallback callback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypt and sign
|
* Sign then encrypt
|
||||||
*
|
*
|
||||||
* @param inputBytes
|
* @param inputBytes
|
||||||
* Byte array you want to encrypt
|
* Byte array you want to encrypt
|
||||||
@ -64,7 +64,7 @@ interface IOpenPgpService {
|
|||||||
* @param callback
|
* @param callback
|
||||||
* Callback where to return results
|
* 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
|
* Decrypts and verifies given input bytes. If no signature is present this method
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -43,6 +43,10 @@ public class OpenPgpServiceConnection {
|
|||||||
return mService;
|
return mService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isBound() {
|
||||||
|
return bound;
|
||||||
|
}
|
||||||
|
|
||||||
private ServiceConnection mCryptoServiceConnection = new ServiceConnection() {
|
private ServiceConnection mCryptoServiceConnection = new ServiceConnection() {
|
||||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||||
mService = IOpenPgpService.Stub.asInterface(service);
|
mService = IOpenPgpService.Stub.asInterface(service);
|
||||||
|
@ -20,26 +20,40 @@ import android.os.Parcel;
|
|||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
|
||||||
public class OpenPgpSignatureResult implements 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;
|
String signatureUserId;
|
||||||
boolean signatureSuccess;
|
boolean signatureOnly;
|
||||||
boolean signatureUnknown;
|
|
||||||
|
public int getSignatureStatus() {
|
||||||
|
return signatureStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSignatureUserId() {
|
||||||
|
return signatureUserId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSignatureOnly() {
|
||||||
|
return signatureOnly;
|
||||||
|
}
|
||||||
|
|
||||||
public OpenPgpSignatureResult() {
|
public OpenPgpSignatureResult() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public OpenPgpSignatureResult(String signatureUserId, boolean signatureSuccess,
|
public OpenPgpSignatureResult(int signatureStatus, String signatureUserId, boolean signatureOnly) {
|
||||||
boolean signatureUnknown) {
|
this.signatureStatus = signatureStatus;
|
||||||
this.signatureUserId = signatureUserId;
|
this.signatureUserId = signatureUserId;
|
||||||
this.signatureSuccess = signatureSuccess;
|
this.signatureOnly = signatureOnly;
|
||||||
this.signatureUnknown = signatureUnknown;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public OpenPgpSignatureResult(OpenPgpSignatureResult b) {
|
public OpenPgpSignatureResult(OpenPgpSignatureResult b) {
|
||||||
|
this.signatureStatus = b.signatureStatus;
|
||||||
this.signatureUserId = b.signatureUserId;
|
this.signatureUserId = b.signatureUserId;
|
||||||
this.signatureSuccess = b.signatureSuccess;
|
this.signatureOnly = b.signatureOnly;
|
||||||
this.signatureUnknown = b.signatureUnknown;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int describeContents() {
|
public int describeContents() {
|
||||||
@ -47,18 +61,17 @@ public class OpenPgpSignatureResult implements Parcelable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void writeToParcel(Parcel dest, int flags) {
|
public void writeToParcel(Parcel dest, int flags) {
|
||||||
|
dest.writeInt(signatureStatus);
|
||||||
dest.writeString(signatureUserId);
|
dest.writeString(signatureUserId);
|
||||||
|
dest.writeByte((byte) (signatureOnly ? 1 : 0));
|
||||||
dest.writeByte((byte) (signatureSuccess ? 1 : 0));
|
|
||||||
dest.writeByte((byte) (signatureUnknown ? 1 : 0));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final Creator<OpenPgpSignatureResult> CREATOR = new Creator<OpenPgpSignatureResult>() {
|
public static final Creator<OpenPgpSignatureResult> CREATOR = new Creator<OpenPgpSignatureResult>() {
|
||||||
public OpenPgpSignatureResult createFromParcel(final Parcel source) {
|
public OpenPgpSignatureResult createFromParcel(final Parcel source) {
|
||||||
OpenPgpSignatureResult vr = new OpenPgpSignatureResult();
|
OpenPgpSignatureResult vr = new OpenPgpSignatureResult();
|
||||||
|
vr.signatureStatus = source.readInt();
|
||||||
vr.signatureUserId = source.readString();
|
vr.signatureUserId = source.readString();
|
||||||
vr.signatureSuccess = source.readByte() == 1;
|
vr.signatureOnly = source.readByte() == 1;
|
||||||
vr.signatureUnknown = source.readByte() == 1;
|
|
||||||
return vr;
|
return vr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,9 +83,9 @@ public class OpenPgpSignatureResult implements Parcelable {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String out = new String();
|
String out = new String();
|
||||||
|
out += "\nsignatureStatus: " + signatureStatus;
|
||||||
out += "signatureUserId: " + signatureUserId;
|
out += "signatureUserId: " + signatureUserId;
|
||||||
out += "\nsignatureSuccess: " + signatureSuccess;
|
out += "\nsignatureOnly: " + signatureOnly;
|
||||||
out += "\nsignatureUnknown: " + signatureUnknown;
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,7 +159,7 @@ public class OpenPgpProviderActivity extends Activity {
|
|||||||
byte[] inputBytes = mMessage.getText().toString().getBytes();
|
byte[] inputBytes = mMessage.getText().toString().getBytes();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
mCryptoServiceConnection.getService().encryptAndSign(inputBytes,
|
mCryptoServiceConnection.getService().signAndEncrypt(inputBytes,
|
||||||
mEncryptUserIds.getText().toString().split(","), true, encryptCallback);
|
mEncryptUserIds.getText().toString().split(","), true, encryptCallback);
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
Log.e(Constants.TAG, "CryptoProviderDemo", e);
|
Log.e(Constants.TAG, "CryptoProviderDemo", e);
|
||||||
|
@ -51,7 +51,7 @@ interface IOpenPgpService {
|
|||||||
oneway void sign(in byte[] inputBytes, in boolean asciiArmor, in IOpenPgpCallback callback);
|
oneway void sign(in byte[] inputBytes, in boolean asciiArmor, in IOpenPgpCallback callback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypt and sign
|
* Sign then encrypt
|
||||||
*
|
*
|
||||||
* @param inputBytes
|
* @param inputBytes
|
||||||
* Byte array you want to encrypt
|
* Byte array you want to encrypt
|
||||||
@ -64,7 +64,7 @@ interface IOpenPgpService {
|
|||||||
* @param callback
|
* @param callback
|
||||||
* Callback where to return results
|
* 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
|
* Decrypts and verifies given input bytes. If no signature is present this method
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -43,6 +43,10 @@ public class OpenPgpServiceConnection {
|
|||||||
return mService;
|
return mService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isBound() {
|
||||||
|
return bound;
|
||||||
|
}
|
||||||
|
|
||||||
private ServiceConnection mCryptoServiceConnection = new ServiceConnection() {
|
private ServiceConnection mCryptoServiceConnection = new ServiceConnection() {
|
||||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||||
mService = IOpenPgpService.Stub.asInterface(service);
|
mService = IOpenPgpService.Stub.asInterface(service);
|
||||||
|
@ -20,26 +20,40 @@ import android.os.Parcel;
|
|||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
|
||||||
public class OpenPgpSignatureResult implements 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;
|
String signatureUserId;
|
||||||
boolean signatureSuccess;
|
boolean signatureOnly;
|
||||||
boolean signatureUnknown;
|
|
||||||
|
public int getSignatureStatus() {
|
||||||
|
return signatureStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSignatureUserId() {
|
||||||
|
return signatureUserId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSignatureOnly() {
|
||||||
|
return signatureOnly;
|
||||||
|
}
|
||||||
|
|
||||||
public OpenPgpSignatureResult() {
|
public OpenPgpSignatureResult() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public OpenPgpSignatureResult(String signatureUserId, boolean signatureSuccess,
|
public OpenPgpSignatureResult(int signatureStatus, String signatureUserId, boolean signatureOnly) {
|
||||||
boolean signatureUnknown) {
|
this.signatureStatus = signatureStatus;
|
||||||
this.signatureUserId = signatureUserId;
|
this.signatureUserId = signatureUserId;
|
||||||
this.signatureSuccess = signatureSuccess;
|
this.signatureOnly = signatureOnly;
|
||||||
this.signatureUnknown = signatureUnknown;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public OpenPgpSignatureResult(OpenPgpSignatureResult b) {
|
public OpenPgpSignatureResult(OpenPgpSignatureResult b) {
|
||||||
|
this.signatureStatus = b.signatureStatus;
|
||||||
this.signatureUserId = b.signatureUserId;
|
this.signatureUserId = b.signatureUserId;
|
||||||
this.signatureSuccess = b.signatureSuccess;
|
this.signatureOnly = b.signatureOnly;
|
||||||
this.signatureUnknown = b.signatureUnknown;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int describeContents() {
|
public int describeContents() {
|
||||||
@ -47,18 +61,17 @@ public class OpenPgpSignatureResult implements Parcelable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void writeToParcel(Parcel dest, int flags) {
|
public void writeToParcel(Parcel dest, int flags) {
|
||||||
|
dest.writeInt(signatureStatus);
|
||||||
dest.writeString(signatureUserId);
|
dest.writeString(signatureUserId);
|
||||||
|
dest.writeByte((byte) (signatureOnly ? 1 : 0));
|
||||||
dest.writeByte((byte) (signatureSuccess ? 1 : 0));
|
|
||||||
dest.writeByte((byte) (signatureUnknown ? 1 : 0));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final Creator<OpenPgpSignatureResult> CREATOR = new Creator<OpenPgpSignatureResult>() {
|
public static final Creator<OpenPgpSignatureResult> CREATOR = new Creator<OpenPgpSignatureResult>() {
|
||||||
public OpenPgpSignatureResult createFromParcel(final Parcel source) {
|
public OpenPgpSignatureResult createFromParcel(final Parcel source) {
|
||||||
OpenPgpSignatureResult vr = new OpenPgpSignatureResult();
|
OpenPgpSignatureResult vr = new OpenPgpSignatureResult();
|
||||||
|
vr.signatureStatus = source.readInt();
|
||||||
vr.signatureUserId = source.readString();
|
vr.signatureUserId = source.readString();
|
||||||
vr.signatureSuccess = source.readByte() == 1;
|
vr.signatureOnly = source.readByte() == 1;
|
||||||
vr.signatureUnknown = source.readByte() == 1;
|
|
||||||
return vr;
|
return vr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,9 +83,9 @@ public class OpenPgpSignatureResult implements Parcelable {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String out = new String();
|
String out = new String();
|
||||||
|
out += "\nsignatureStatus: " + signatureStatus;
|
||||||
out += "signatureUserId: " + signatureUserId;
|
out += "signatureUserId: " + signatureUserId;
|
||||||
out += "\nsignatureSuccess: " + signatureSuccess;
|
out += "\nsignatureOnly: " + signatureOnly;
|
||||||
out += "\nsignatureUnknown: " + signatureUnknown;
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -461,19 +461,26 @@ public class OpenPgpService extends Service {
|
|||||||
|
|
||||||
// get signature informations from bundle
|
// get signature informations from bundle
|
||||||
boolean signature = outputBundle.getBoolean(KeychainIntentService.RESULT_SIGNATURE);
|
boolean signature = outputBundle.getBoolean(KeychainIntentService.RESULT_SIGNATURE);
|
||||||
long signatureKeyId = outputBundle
|
|
||||||
.getLong(KeychainIntentService.RESULT_SIGNATURE_KEY_ID);
|
|
||||||
String signatureUserId = outputBundle
|
|
||||||
.getString(KeychainIntentService.RESULT_SIGNATURE_USER_ID);
|
|
||||||
boolean signatureSuccess = outputBundle
|
|
||||||
.getBoolean(KeychainIntentService.RESULT_SIGNATURE_SUCCESS);
|
|
||||||
boolean signatureUnknown = outputBundle
|
|
||||||
.getBoolean(KeychainIntentService.RESULT_SIGNATURE_UNKNOWN);
|
|
||||||
|
|
||||||
OpenPgpSignatureResult sigResult = null;
|
OpenPgpSignatureResult sigResult = null;
|
||||||
if (signature) {
|
if (signature) {
|
||||||
sigResult = new OpenPgpSignatureResult(signatureUserId, signatureSuccess,
|
long signatureKeyId = outputBundle
|
||||||
signatureUnknown);
|
.getLong(KeychainIntentService.RESULT_SIGNATURE_KEY_ID);
|
||||||
|
String signatureUserId = outputBundle
|
||||||
|
.getString(KeychainIntentService.RESULT_SIGNATURE_USER_ID);
|
||||||
|
boolean signatureSuccess = outputBundle
|
||||||
|
.getBoolean(KeychainIntentService.RESULT_SIGNATURE_SUCCESS);
|
||||||
|
boolean signatureUnknown = outputBundle
|
||||||
|
.getBoolean(KeychainIntentService.RESULT_SIGNATURE_UNKNOWN);
|
||||||
|
|
||||||
|
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
|
// return over handler on client side
|
||||||
@ -514,7 +521,7 @@ public class OpenPgpService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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 boolean asciiArmor, final IOpenPgpCallback callback) throws RemoteException {
|
||||||
|
|
||||||
final AppSettings settings = getAppSettings();
|
final AppSettings settings = getAppSettings();
|
||||||
|
Loading…
Reference in New Issue
Block a user