Working version which returns data via Go channels after waiting for

all user interactions to complete successfully. Streamline code and
eliminating PgpFragment (make PgpConnect itself a Fragment).
This commit is contained in:
Greg 2019-11-15 20:17:43 -05:00
parent ed998c3d24
commit f5a3da4398
6 changed files with 149 additions and 225 deletions

View File

@ -26,12 +26,11 @@ import android.content.IntentSender.OnFinished;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
public class PgpConnect {
//OpenPgpServiceConnection mServiceConnection;
public class PgpConnect extends Fragment {
Activity act;
Context ctx;
Handler handler;
PgpFragment frag;
OpenPgpServiceConnection mServiceConnection;
PgpConnect(Activity act) {
act = act;
@ -41,21 +40,156 @@ public class PgpConnect {
//this.mServiceConnection.bindToService();
final FragmentManager fm = act.getFragmentManager();
frag = new PgpFragment();
final Fragment frag = this;
handler.post(new Runnable() {
public void run() {
FragmentTransaction ft = fm.beginTransaction();
ft.add(frag, "PgpFragment");
ft.add(frag, "PgpConnect");
ft.commitNow();
}
});
}
public String Decrypt(byte[] dat, int chint) {
return frag.Decrypt(dat, chint);
@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();
}
public String Encrypt(byte[] id, byte[] dat, int chint) {
return frag.Encrypt(id, dat, chint);
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("gio", "onActivityResult(" + requestCode + "): " + resultCode);
super.onActivityResult(requestCode, resultCode, data);
//activityResult(requestCode, resultCode);
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 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);
}

View File

@ -1,170 +0,0 @@
package st.wow.git.pgp;
import android.util.Log;
import android.content.Context;
import android.app.Fragment;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
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.Intent;
import android.app.PendingIntent;
import android.content.IntentSender;
import android.content.IntentSender.SendIntentException;
import android.os.Handler;
public class PgpFragment extends Fragment {
OpenPgpServiceConnection mServiceConnection;
Context ctx;
Handler handler;
@Override public void onAttach(Context ctx) {
super.onAttach(ctx);
this.ctx = ctx;
this.handler = new Handler(ctx.getMainLooper());
Log.d("PgpFragment", "onAttach()");
mServiceConnection = new OpenPgpServiceConnection(ctx, "org.sufficientlysecure.keychain");
mServiceConnection.bindToService();
}
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("PgpFragment", "onActivityResult(" + requestCode + "): " + resultCode);
super.onActivityResult(requestCode, resultCode, data);
activityResult(requestCode, resultCode);
}
public String Decrypt(byte []dat, int chint) {
if (handler == null) {
return "";
}
final BlockingQueue<String> q = new LinkedBlockingQueue<String>();
handler.post(new Runnable() {
public void run() {
try {
q.put(_decrypt(dat, chint));
} catch (InterruptedException e) {
Log.e("gio", "InterruptedException", e);
}
}
});
try {
return q.take();
} catch (InterruptedException e) {
Log.e("gio", "InterruptedException", e);
}
return "";
}
private String _decrypt(byte []dat, int chint) {
String ret = "";
Intent data = new Intent();
data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
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 {
ret = os.toString("UTF-8");
Log.d(OpenPgpApi.TAG, "output: " + ret);
} catch (UnsupportedEncodingException e) {
ret = "";
Log.e("gio", "UnsupportedEncodingException", e);
}
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);
}
break;
}
case OpenPgpApi.RESULT_CODE_ERROR: {
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
break;
}
}
return ret;
}
public String Encrypt(byte[] id, byte[] dat, int chint) {
if (handler == null) {
return "";
}
final BlockingQueue<String> q = new LinkedBlockingQueue<String>();
handler.post(new Runnable() {
public void run() {
try {
q.put(_encrypt(id, dat, chint));
} catch (InterruptedException e) {
Log.e("gio", "InterruptedException", e);
}
}
});
try {
return q.take();
} catch (InterruptedException e) {
Log.e("gio", "InterruptedException", e);
}
return "";
}
private String _encrypt(byte[] id, byte[] dat, int chint) {
String ret = "";
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);
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 {
ret = os.toString("UTF-8");
Log.d(OpenPgpApi.TAG, "output: " + ret);
} catch (UnsupportedEncodingException e) {
Log.e("gio", "UnsupportedEncodingException", e);
}
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);
}
break;
}
case OpenPgpApi.RESULT_CODE_ERROR: {
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
break;
}
}
return ret;
}
static private native void activityResult(int requestCode, int resultCode);
static private native void stringResult(int requestCode, String result);
}

View File

@ -30,44 +30,13 @@ CreateObject(JNIEnv* env, jclass cls) {
return (*env)->NewObject(env, cls, init);
}
void
AddFragment(JNIEnv* env, jobject act, jobject frag) {
jclass cls = (*env)->GetObjectClass(env, act);
jmethodID mid = (*env)->GetMethodID(env, cls, "getFragmentManager", "()Landroid/app/FragmentManager;");
jobject fm = (*env)->CallObjectMethod(env, act, mid);
cls = (*env)->GetObjectClass(env, fm);
mid = (*env)->GetMethodID(env, cls, "beginTransaction", "()Landroid/app/FragmentTransaction;");
jobject ft = (*env)->CallObjectMethod(env, fm, mid);
cls = (*env)->GetObjectClass(env, ft);
mid = (*env)->GetMethodID(env, cls, "add", "(Landroid/app/Fragment;)V");
(*env)->CallVoidMethod(env, ft, mid, frag);
mid = (*env)->GetMethodID(env, cls, "commitNow", "()V");
(*env)->CallVoidMethod(env, ft, mid);
cls = (*env)->GetObjectClass(env, frag);
static const JNINativeMethod fragMethods[] = {
{
.name = "activityResult",
.signature = "(II)V",
.fnPtr = activityResult
},
{
.name = "stringResult",
.signature = "(ILjava/lang/String;)V",
.fnPtr = stringResult
},
};
(*env)->RegisterNatives(env, cls, fragMethods, sizeof(fragMethods)/sizeof(fragMethods[0]));
}
jobject
NewPgpConnect(JNIEnv* env, jobject act) {
jclass cls = FindClass(env, "st/wow/git/pgp/PgpConnect");
jmethodID init = (*env)->GetMethodID(env, cls, "<init>", "(Landroid/app/Activity;)V");
jobject ret = (*env)->NewObject(env, cls, init, act);
//cls = (*env)->GetObjectClass(env, ret);
cls = FindClass(env, "st/wow/git/pgp/PgpFragment");
static const JNINativeMethod actMethods[] = {
{
.name = "activityResult",
@ -91,7 +60,7 @@ Decrypt(JNIEnv* env, jobject p, char* cdata, int datalen, int chint) {
jbyteArray data = (*env)->NewByteArray(env, datalen);
(*env)->SetByteArrayRegion(env, data, 0, datalen, cdata);
jclass cls = (*env)->GetObjectClass(env, p);
jmethodID mid = (*env)->GetMethodID(env, cls, "Decrypt", "([BI)Ljava/lang/String;");
jmethodID mid = (*env)->GetMethodID(env, cls, "Decrypt", "([BI)V");
jstring ret = (*env)->CallObjectMethod(env, p, mid, data, chint);
return (*env)->GetStringUTFChars(env, ret, NULL);
}
@ -108,7 +77,7 @@ Encrypt(JNIEnv* env, jobject p, char* cid, int idlen, char* cdata, int datalen,
jbyteArray data = (*env)->NewByteArray(env, datalen);
(*env)->SetByteArrayRegion(env, data, 0, datalen, cdata);
jclass cls = (*env)->GetObjectClass(env, p);
jmethodID mid = (*env)->GetMethodID(env, cls, "Encrypt", "([B[BI)Ljava/lang/String;");
jmethodID mid = (*env)->GetMethodID(env, cls, "Encrypt", "([B[BI)V");
jstring ret = (*env)->CallObjectMethod(env, p, mid, id, data, chint);
return (*env)->GetStringUTFChars(env, ret, NULL);
}

View File

@ -34,10 +34,6 @@ func FindClass(env *C.JNIEnv, name string) C.jclass {
return C.FindClass((*C.JNIEnv)(env), cname)
}
func AddFragment(env *C.JNIEnv, act C.jobject, frag C.jobject) {
C.AddFragment(env, act, frag);
}
func JniCallVoidMethod(env *C.JNIEnv, obj C.jobject, methodID C.jmethodID) {
C.CallVoidMethod(env, obj, methodID)
}
@ -87,13 +83,11 @@ func (p PGP) Decrypt(data string) string {
cdata := C.CString(data)
defer C.free(unsafe.Pointer(cdata))
var ret string
RunInJVM(func(env *JNIEnv) {
retc := C.Decrypt(env, (C.jobject)(p), (*C.char)(unsafe.Pointer(cdata)), (C.int)(len(data)), C.int(curint))
defer C.free(unsafe.Pointer(retc))
ret = C.GoString(retc)
})
return ret
return <-ch
}
func (p PGP) Encrypt(id, data string) string {
@ -111,13 +105,11 @@ func (p PGP) Encrypt(id, data string) string {
defer C.free(unsafe.Pointer(cid))
cdata := C.CString(data)
defer C.free(unsafe.Pointer(cdata))
var ret string
RunInJVM(func(env *JNIEnv) {
retc := C.Encrypt(env, (C.jobject)(p), (*C.char)(unsafe.Pointer(cid)), (C.int)(len(id)), (*C.char)(unsafe.Pointer(cdata)), (C.int)(len(data)), C.int(curint))
defer C.free(unsafe.Pointer(retc))
ret = C.GoString(retc)
})
return ret
return <-ch
}
func RunInJVM(f func(env *C.JNIEnv)) {

View File

@ -3,7 +3,6 @@
void SetLoader(JNIEnv* env, jobject context);
jclass FindClass(JNIEnv* env, char* name);
jobject CreateObject(JNIEnv* env, jclass cls);
void AddFragment(JNIEnv* env, jobject act, jobject frag);
jobject NewPgpConnect(JNIEnv* env, jobject act);
const char* Decrypt(JNIEnv* env, jobject p, char* cdata, int datalen, int chint);
const char* Encrypt(JNIEnv* env, jobject p, char* cid, int idlen, char* cdata, int datalen, int chint);

View File

@ -1,5 +1,5 @@
//go:generate mkdir -p classes
//go:generate javac -bootclasspath $ANDROID_HOME/platforms/android-29/android.jar -classpath openpgp-api.jar -d classes PgpConnect.java PgpFragment.java
//go:generate javac -bootclasspath $ANDROID_HOME/platforms/android-29/android.jar -classpath openpgp-api.jar -d classes PgpConnect.java
//go:generate jar cf PgpConnect.jar -C classes .
//go:generate rm -rf classes