android-go/examples/pgp/PgpConnect.java
2020-06-11 10:42:43 -04:00

209 lines
6.5 KiB
Java

package st.wow.git.pgp;
import java.lang.Runnable;
import java.lang.String;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import android.os.Handler;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.OpenPgpSignatureResult;
import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection;
import android.content.Context;
import android.content.ClipboardManager;
import android.content.ClipData;
import android.content.Intent;
import android.util.Log;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.PendingIntent;
import android.content.IntentSender;
import android.content.IntentSender.OnFinished;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
public class PgpConnect extends Fragment {
Activity act;
Context ctx;
Handler handler;
OpenPgpServiceConnection mServiceConnection;
ClipboardManager cb;
PgpConnect(Activity act) {
act = act;
ctx = act.getApplicationContext();
this.handler = new Handler(ctx.getMainLooper());
final FragmentManager fm = act.getFragmentManager();
final Fragment frag = this;
handler.post(new Runnable() {
public void run() {
FragmentTransaction ft = fm.beginTransaction();
ft.add(frag, "PgpConnect");
ft.commitNow();
}
});
}
@Override public void onAttach(Context ctx) {
super.onAttach(ctx);
Log.d("gio", "onAttach()");
this.ctx = ctx;
this.handler = new Handler(ctx.getMainLooper());
mServiceConnection = new OpenPgpServiceConnection(ctx, "org.sufficientlysecure.keychain");
mServiceConnection.bindToService();
cb = (ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE);
}
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("gio", "onActivityResult(" + requestCode + "): " + resultCode);
super.onActivityResult(requestCode, resultCode, data);
//activityResult(requestCode, resultCode);
if (resultCode != Activity.RESULT_OK) {
Log.d("gio", "onActivityResult: not OK");
stringResult(requestCode, null);
return;
}
switch (data.getAction()) {
case OpenPgpApi.ACTION_DECRYPT_VERIFY: {
Log.d("gio", "action decrypt");
_decrypt(data, requestCode);
break;
}
case OpenPgpApi.ACTION_ENCRYPT: {
Log.d("gio", "action encrypt");
_encrypt(data, requestCode);
break;
}
default: {
Log.d("gio", "some other action");
}
}
}
public void Clip(byte []dat) {
ClipData clip = ClipData.newPlainText("PgpConnect", new String(dat));
cb.setPrimaryClip(clip);
}
public void Decrypt(byte []dat, int chint) {
if (handler == null) {
return;
}
handler.post(new Runnable() {
public void run() {
Intent data = new Intent();
data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
data.putExtra("DATA", dat);
_decrypt(data, chint);
}
});
}
private void _decrypt(Intent data, int chint) {
Log.d("gio","_decrypt");
byte []dat = data.getByteArrayExtra("DATA");
InputStream is = new ByteArrayInputStream(dat);
ByteArrayOutputStream os = new ByteArrayOutputStream();
OpenPgpApi api = new OpenPgpApi(this.ctx, mServiceConnection.getService());
Intent result = api.executeApi(data, is, os);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS: {
try {
String ret = os.toString("UTF-8");
Log.d(OpenPgpApi.TAG, "output: " + ret);
stringResult(chint, ret);
} catch (UnsupportedEncodingException e) {
Log.e("gio", "UnsupportedEncodingException", e);
stringResult(chint, null);
}
break;
}
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: {
PendingIntent pi = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
try {
startIntentSenderForResult(pi.getIntentSender(), chint, null, 0, 0, 0, null);
} catch (IntentSender.SendIntentException e) {
Log.e("gio", "SendIntentException", e);
stringResult(chint, null);
}
break;
}
case OpenPgpApi.RESULT_CODE_ERROR: {
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
stringResult(chint, null);
}
}
}
public void Encrypt(byte[] id, byte[] dat, int chint) {
if (handler == null) {
return;
}
handler.post(new Runnable() {
public void run() {
Intent data = new Intent();
data.setAction(OpenPgpApi.ACTION_ENCRYPT);
data.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[]{new String(id)});
data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
data.putExtra("DATA", dat);
_encrypt(data, chint);
}
});
}
private void _encrypt(Intent data, int chint) {
Log.d("gio","_encrypt");
byte []dat = data.getByteArrayExtra("DATA");
InputStream is = new ByteArrayInputStream(dat);
ByteArrayOutputStream os = new ByteArrayOutputStream();
OpenPgpApi api = new OpenPgpApi(this.ctx, mServiceConnection.getService());
Intent result = api.executeApi(data, is, os);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS: {
try {
String ret = os.toString("UTF-8");
Log.d(OpenPgpApi.TAG, "output: " + ret);
stringResult(chint, ret);
} catch (UnsupportedEncodingException e) {
Log.e("gio", "UnsupportedEncodingException", e);
stringResult(chint, null);
}
if (result.hasExtra(OpenPgpApi.RESULT_SIGNATURE)) {
OpenPgpSignatureResult sigResult
= result.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE);
}
break;
}
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: {
PendingIntent pi = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
try {
IntentSender sender = pi.getIntentSender();
Log.d("PgpConnect", "IntentSender:" + sender.toString());
startIntentSenderForResult(sender, chint, null, 0, 0, 0, null);
} catch (IntentSender.SendIntentException e) {
Log.e("gio", "SendIntentException", e);
stringResult(chint, null);
}
break;
}
case OpenPgpApi.RESULT_CODE_ERROR: {
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
stringResult(chint, null);
break;
}
}
}
static private native void activityResult(int requestCode, int resultCode);
static private native void stringResult(int requestCode, String result);
}