Files
open-keychain/OpenPGP-Keychain-API-Demo/src/org/sufficientlysecure/keychain/demo/OpenPGPProviderActivity.java

280 lines
9.7 KiB
Java
Raw Normal View History

2013-05-28 15:10:36 +02:00
/*
* Copyright (C) 2013 Dominik Schürmann <dominik@dominikschuermann.de>
2013-05-28 15:10:36 +02:00
*
* 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.sufficientlysecure.keychain.demo;
import java.util.ArrayList;
import java.util.List;
2013-09-10 23:19:34 +02:00
import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.OpenPgpServiceConnection;
import org.openintents.openpgp.OpenPgpSignatureResult;
import org.openintents.openpgp.IOpenPgpCallback;
import org.openintents.openpgp.IOpenPgpService;
2013-05-28 15:10:36 +02:00
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
2013-05-28 15:10:36 +02:00
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
2013-05-28 15:10:36 +02:00
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
2013-06-17 19:51:41 +02:00
import android.widget.EditText;
import android.widget.ListAdapter;
2013-05-28 15:10:36 +02:00
import android.widget.TextView;
import android.widget.Toast;
2013-05-28 15:10:36 +02:00
2013-09-10 23:19:34 +02:00
public class OpenPGPProviderActivity extends Activity {
2013-05-28 15:10:36 +02:00
Activity mActivity;
2013-06-17 19:51:41 +02:00
EditText mMessage;
EditText mCiphertext;
EditText mEncryptUserIds;
2013-05-28 15:10:36 +02:00
2013-09-10 23:19:34 +02:00
private OpenPgpServiceConnection mCryptoServiceConnection;
2013-05-28 15:10:36 +02:00
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.crypto_provider_demo);
mActivity = this;
2013-06-17 19:51:41 +02:00
mMessage = (EditText) findViewById(R.id.crypto_provider_demo_message);
mCiphertext = (EditText) findViewById(R.id.crypto_provider_demo_ciphertext);
mEncryptUserIds = (EditText) findViewById(R.id.crypto_provider_demo_encrypt_user_id);
2013-05-28 15:10:36 +02:00
selectCryptoProvider();
2013-05-28 15:10:36 +02:00
}
/**
* Callback from remote crypto service
*/
2013-09-10 23:19:34 +02:00
final IOpenPgpCallback.Stub encryptCallback = new IOpenPgpCallback.Stub() {
@Override
2013-09-10 23:19:34 +02:00
public void onSuccess(final byte[] outputBytes, OpenPgpSignatureResult signatureResult)
2013-07-01 23:19:53 +02:00
throws RemoteException {
Log.d(Constants.TAG, "encryptCallback");
2013-06-17 19:51:41 +02:00
2013-09-06 13:48:27 +02:00
runOnUiThread(new Runnable() {
@Override
public void run() {
mCiphertext.setText(new String(outputBytes));
}
});
2013-05-28 15:10:36 +02:00
}
@Override
2013-09-10 23:19:34 +02:00
public void onError(OpenPgpError error) throws RemoteException {
handleError(error);
2013-07-01 23:19:53 +02:00
}
};
2013-09-06 11:55:08 +02:00
2013-09-10 23:19:34 +02:00
final IOpenPgpCallback.Stub decryptAndVerifyCallback = new IOpenPgpCallback.Stub() {
2013-07-01 23:19:53 +02:00
@Override
2013-09-10 23:19:34 +02:00
public void onSuccess(final byte[] outputBytes, final OpenPgpSignatureResult signatureResult)
throws RemoteException {
Log.d(Constants.TAG, "decryptAndVerifyCallback");
2013-09-06 13:48:27 +02:00
runOnUiThread(new Runnable() {
@Override
public void run() {
2013-09-09 22:38:09 +02:00
mMessage.setText(new String(outputBytes));
if (signatureResult != null) {
2013-09-10 23:19:34 +02:00
Toast.makeText(OpenPGPProviderActivity.this,
"signature result:\n" + signatureResult.toString(),
Toast.LENGTH_LONG).show();
}
2013-09-06 13:48:27 +02:00
}
});
2013-05-28 15:10:36 +02:00
}
@Override
2013-09-10 23:19:34 +02:00
public void onError(OpenPgpError error) throws RemoteException {
handleError(error);
}
2013-05-28 15:10:36 +02:00
};
2013-05-28 15:10:36 +02:00
2013-09-10 23:19:34 +02:00
private void handleError(final OpenPgpError error) {
2013-09-08 23:08:30 +02:00
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(mActivity,
"onError id:" + error.getErrorId() + "\n\n" + error.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(Constants.TAG, "onError getErrorId:" + error.getErrorId());
Log.e(Constants.TAG, "onError getMessage:" + error.getMessage());
}
});
}
2013-05-28 15:10:36 +02:00
public void encryptOnClick(View view) {
2013-06-17 19:51:41 +02:00
byte[] inputBytes = mMessage.getText().toString().getBytes();
2013-05-28 15:10:36 +02:00
try {
mCryptoServiceConnection.getService().encrypt(inputBytes,
mEncryptUserIds.getText().toString().split(","), true, encryptCallback);
2013-06-17 19:51:41 +02:00
} catch (RemoteException e) {
Log.e(Constants.TAG, "CryptoProviderDemo", e);
}
}
public void signOnClick(View view) {
byte[] inputBytes = mMessage.getText().toString().getBytes();
try {
mCryptoServiceConnection.getService().sign(inputBytes, true, encryptCallback);
2013-06-17 19:51:41 +02:00
} catch (RemoteException e) {
Log.e(Constants.TAG, "CryptoProviderDemo", e);
}
}
public void encryptAndSignOnClick(View view) {
byte[] inputBytes = mMessage.getText().toString().getBytes();
try {
mCryptoServiceConnection.getService().encryptAndSign(inputBytes,
mEncryptUserIds.getText().toString().split(","), true, encryptCallback);
2013-05-28 15:10:36 +02:00
} catch (RemoteException e) {
Log.e(Constants.TAG, "CryptoProviderDemo", e);
2013-05-28 15:10:36 +02:00
}
}
public void decryptAndVerifyOnClick(View view) {
2013-06-17 19:51:41 +02:00
byte[] inputBytes = mCiphertext.getText().toString().getBytes();
2013-05-28 15:10:36 +02:00
try {
mCryptoServiceConnection.getService().decryptAndVerify(inputBytes,
decryptAndVerifyCallback);
2013-05-28 15:10:36 +02:00
} catch (RemoteException e) {
Log.e(Constants.TAG, "CryptoProviderDemo", e);
2013-05-28 15:10:36 +02:00
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mCryptoServiceConnection != null) {
mCryptoServiceConnection.unbindFromService();
}
2013-05-28 15:10:36 +02:00
}
2013-09-10 23:19:34 +02:00
private static class OpenPGPProviderElement {
private String packageName;
private String simpleName;
private Drawable icon;
2013-05-28 15:10:36 +02:00
2013-09-10 23:19:34 +02:00
public OpenPGPProviderElement(String packageName, String simpleName, Drawable icon) {
this.packageName = packageName;
this.simpleName = simpleName;
this.icon = icon;
2013-05-28 15:10:36 +02:00
}
@Override
public String toString() {
return simpleName;
2013-05-28 15:10:36 +02:00
}
}
2013-05-28 15:10:36 +02:00
private void selectCryptoProvider() {
2013-09-10 23:19:34 +02:00
Intent intent = new Intent(IOpenPgpService.class.getName());
2013-05-28 15:10:36 +02:00
2013-09-10 23:19:34 +02:00
final ArrayList<OpenPGPProviderElement> providerList = new ArrayList<OpenPGPProviderElement>();
2013-05-28 15:10:36 +02:00
List<ResolveInfo> resInfo = getPackageManager().queryIntentServices(intent, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo resolveInfo : resInfo) {
if (resolveInfo.serviceInfo == null)
continue;
2013-05-28 15:10:36 +02:00
String packageName = resolveInfo.serviceInfo.packageName;
String simpleName = String.valueOf(resolveInfo.serviceInfo
.loadLabel(getPackageManager()));
Drawable icon = resolveInfo.serviceInfo.loadIcon(getPackageManager());
2013-09-10 23:19:34 +02:00
providerList.add(new OpenPGPProviderElement(packageName, simpleName, icon));
}
2013-05-28 15:10:36 +02:00
AlertDialog.Builder alert = new AlertDialog.Builder(this);
2013-09-10 23:19:34 +02:00
alert.setTitle("Select OpenPGP Provider!");
alert.setCancelable(false);
2013-05-28 15:10:36 +02:00
if (!providerList.isEmpty()) {
2013-05-28 15:10:36 +02:00
// Init ArrayAdapter with Crypto Providers
2013-09-10 23:19:34 +02:00
ListAdapter adapter = new ArrayAdapter<OpenPGPProviderElement>(this,
android.R.layout.select_dialog_item, android.R.id.text1, providerList) {
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);
2013-05-28 15:10:36 +02:00
// Put the image on the TextView
tv.setCompoundDrawablesWithIntrinsicBounds(providerList.get(position).icon,
null, null, null);
2013-05-28 15:10:36 +02:00
// Add margin between image and text (support various screen densities)
int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
tv.setCompoundDrawablePadding(dp5);
2013-05-28 15:10:36 +02:00
return v;
}
};
2013-05-28 15:10:36 +02:00
alert.setSingleChoiceItems(adapter, -1, new DialogInterface.OnClickListener() {
2013-05-28 15:10:36 +02:00
public void onClick(DialogInterface dialog, int position) {
String packageName = providerList.get(position).packageName;
2013-05-28 15:10:36 +02:00
// bind to service
2013-09-10 23:19:34 +02:00
mCryptoServiceConnection = new OpenPgpServiceConnection(
OpenPGPProviderActivity.this, packageName);
mCryptoServiceConnection.bindToService();
2013-05-28 15:10:36 +02:00
dialog.dismiss();
}
});
} else {
2013-09-10 23:19:34 +02:00
alert.setMessage("No OpenPGP Provider installed!");
}
alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
2013-05-28 15:10:36 +02:00
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
finish();
}
});
AlertDialog ad = alert.create();
ad.show();
}
}
2013-05-28 15:10:36 +02:00
}