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

268 lines
9.3 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;
import org.openintents.crypto.CryptoError;
import org.openintents.crypto.CryptoServiceConnection;
import org.openintents.crypto.CryptoSignatureResult;
import org.openintents.crypto.ICryptoCallback;
2013-06-17 16:17:59 +02:00
import org.openintents.crypto.ICryptoService;
2013-05-28 15:10:36 +02:00
import org.sufficientlysecure.keychain.demo.R;
import org.sufficientlysecure.keychain.integration.Constants;
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;
public class CryptoProviderDemoActivity extends Activity {
Activity mActivity;
2013-06-17 19:51:41 +02:00
EditText mMessage;
EditText mCiphertext;
EditText mEncryptUserId;
EditText mSignUserId;
2013-05-28 15:10:36 +02:00
private CryptoServiceConnection 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);
mEncryptUserId = (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-07-01 23:19:53 +02:00
final ICryptoCallback.Stub encryptCallback = new ICryptoCallback.Stub() {
@Override
2013-09-06 13:48:27 +02:00
public void onSuccess(final byte[] outputBytes, CryptoSignatureResult signatureResult)
2013-07-01 23:19:53 +02:00
throws RemoteException {
2013-06-17 19:51:41 +02:00
Log.d(Constants.TAG, "onEncryptSignSuccess");
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-07-01 23:19:53 +02:00
public void onError(CryptoError error) throws RemoteException {
Log.e(Constants.TAG, "onError getErrorId:" + error.getErrorId());
Log.e(Constants.TAG, "onError getMessage:" + error.getMessage());
}
};
2013-09-06 11:55:08 +02:00
2013-07-01 23:19:53 +02:00
final ICryptoCallback.Stub decryptCallback = new ICryptoCallback.Stub() {
@Override
2013-09-06 13:48:27 +02:00
public void onSuccess(final byte[] outputBytes, final CryptoSignatureResult signatureResult)
throws RemoteException {
Log.d(Constants.TAG, "onDecryptVerifySuccess");
2013-09-06 13:48:27 +02:00
runOnUiThread(new Runnable() {
@Override
public void run() {
mMessage.setText(new String(outputBytes) + "\n\n" + signatureResult.toString());
}
});
2013-05-28 15:10:36 +02:00
}
@Override
public void onError(CryptoError error) throws RemoteException {
Log.e(Constants.TAG, "onError getErrorId:" + error.getErrorId());
2013-06-17 19:51:41 +02:00
Log.e(Constants.TAG, "onError getMessage:" + error.getMessage());
}
2013-05-28 15:10:36 +02:00
};
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,
2013-07-01 23:19:53 +02:00
new String[] { mEncryptUserId.getText().toString() }, 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,
2013-07-01 23:19:53 +02:00
mSignUserId.getText().toString(), 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,
new String[] { mEncryptUserId.getText().toString() },
2013-07-01 23:19:53 +02:00
mSignUserId.getText().toString(), 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 decryptOnClick(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 {
2013-07-01 23:19:53 +02:00
mCryptoServiceConnection.getService().decryptAndVerify(inputBytes, decryptCallback);
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
}
private static class CryptoProviderElement {
private String packageName;
private String simpleName;
private Drawable icon;
2013-05-28 15:10:36 +02:00
public CryptoProviderElement(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-06-17 16:17:59 +02:00
Intent intent = new Intent(ICryptoService.class.getName());
2013-05-28 15:10:36 +02:00
final ArrayList<CryptoProviderElement> providerList = new ArrayList<CryptoProviderElement>();
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());
providerList.add(new CryptoProviderElement(packageName, simpleName, icon));
}
2013-05-28 15:10:36 +02:00
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Select Crypto 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
ListAdapter adapter = new ArrayAdapter<CryptoProviderElement>(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
mCryptoServiceConnection = new CryptoServiceConnection(
CryptoProviderDemoActivity.this, packageName);
mCryptoServiceConnection.bindToService();
2013-05-28 15:10:36 +02:00
dialog.dismiss();
}
});
} else {
alert.setMessage("No Crypto 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
}