Compare commits
No commits in common. "0b6929c5223648e5d0789e8c13db6806f153d5fc" and "2675f322afbd9ed8ecf43ef08ad1467d156fcf79" have entirely different histories.
0b6929c522
...
2675f322af
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -1,11 +1,9 @@
|
|||
*.ast
|
||||
*.apk
|
||||
*.jar
|
||||
*.dex
|
||||
*.class
|
||||
examples/jni/AClass.jar
|
||||
examples/pgp/PgpConnect.jar
|
||||
examples/sensors/sensors
|
||||
examples/jni/jni
|
||||
examples/dex/dex
|
||||
examples/pgp/pgp
|
||||
examples/permissions/permissions
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
package android27
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -x objective-c -fno-objc-arc
|
||||
#cgo LDFLAGS: -landroid -llog -lnativewindow -lm
|
||||
|
||||
#import <android/looper.h>
|
||||
#import <android/sensor.h>
|
||||
|
||||
typedef struct {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
int8_t status;
|
||||
} ASensorVector2;
|
||||
|
||||
ASensorVector2
|
||||
ASensorAcceleration(ASensorEvent* event) {
|
||||
ASensorVector2 ret;
|
||||
ret.x = event->acceleration.x;
|
||||
ret.y = event->acceleration.y;
|
||||
ret.z = event->acceleration.z;
|
||||
ret.status = event->acceleration.status;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ASensorEventQueue*
|
||||
ASensorManager_createEventQueue2(ASensorManager* manager, ALooper* looper, int ident) {
|
||||
return ASensorManager_createEventQueue(manager, looper, ident, 0, 0);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
type ASensorVector C.ASensorVector2
|
||||
|
||||
func (x ASensorVector) X() Float { return (Float)(x.x) }
|
||||
func (x ASensorVector) Y() Float { return (Float)(x.y) }
|
||||
func (x ASensorVector) Z() Float { return (Float)(x.z) }
|
||||
|
||||
func (x *ASensorEvent) Acceleration() ASensorVector {
|
||||
return (ASensorVector)(C.ASensorAcceleration(x))
|
||||
}
|
||||
|
||||
func ASensorManagerCreateEventQueue(manager *ASensorManager, looper *ALooper, ident int) *ASensorEventQueue {
|
||||
ret := (*ASensorEventQueue)(C.ASensorManager_createEventQueue2((*C.ASensorManager)(manager), (*C.ALooper)(looper), (C.int)(ident)))
|
||||
return ret
|
||||
}
|
||||
|
1618
android27/main.go
1618
android27/main.go
File diff suppressed because it is too large
Load Diff
|
@ -1,48 +0,0 @@
|
|||
package android28
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -x objective-c -fno-objc-arc
|
||||
#cgo LDFLAGS: -landroid -llog -lnativewindow -lm
|
||||
|
||||
#import <android/looper.h>
|
||||
#import <android/sensor.h>
|
||||
|
||||
typedef struct {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
int8_t status;
|
||||
} ASensorVector2;
|
||||
|
||||
ASensorVector2
|
||||
ASensorAcceleration(ASensorEvent* event) {
|
||||
ASensorVector2 ret;
|
||||
ret.x = event->acceleration.x;
|
||||
ret.y = event->acceleration.y;
|
||||
ret.z = event->acceleration.z;
|
||||
ret.status = event->acceleration.status;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ASensorEventQueue*
|
||||
ASensorManager_createEventQueue2(ASensorManager* manager, ALooper* looper, int ident) {
|
||||
return ASensorManager_createEventQueue(manager, looper, ident, 0, 0);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
type ASensorVector C.ASensorVector2
|
||||
|
||||
func (x ASensorVector) X() Float { return (Float)(x.x) }
|
||||
func (x ASensorVector) Y() Float { return (Float)(x.y) }
|
||||
func (x ASensorVector) Z() Float { return (Float)(x.z) }
|
||||
|
||||
func (x *ASensorEvent) Acceleration() ASensorVector {
|
||||
return (ASensorVector)(C.ASensorAcceleration(x))
|
||||
}
|
||||
|
||||
func ASensorManagerCreateEventQueue(manager *ASensorManager, looper *ALooper, ident int) *ASensorEventQueue {
|
||||
ret := (*ASensorEventQueue)(C.ASensorManager_createEventQueue2((*C.ASensorManager)(manager), (*C.ALooper)(looper), (C.int)(ident)))
|
||||
return ret
|
||||
}
|
||||
|
1900
android28/main.go
1900
android28/main.go
File diff suppressed because it is too large
Load Diff
|
@ -1,48 +0,0 @@
|
|||
package android29
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -x objective-c -fno-objc-arc
|
||||
#cgo LDFLAGS: -landroid -llog -lnativewindow -lm
|
||||
|
||||
#import <android/looper.h>
|
||||
#import <android/sensor.h>
|
||||
|
||||
typedef struct {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
int8_t status;
|
||||
} ASensorVector2;
|
||||
|
||||
ASensorVector2
|
||||
ASensorAcceleration(ASensorEvent* event) {
|
||||
ASensorVector2 ret;
|
||||
ret.x = event->acceleration.x;
|
||||
ret.y = event->acceleration.y;
|
||||
ret.z = event->acceleration.z;
|
||||
ret.status = event->acceleration.status;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ASensorEventQueue*
|
||||
ASensorManager_createEventQueue2(ASensorManager* manager, ALooper* looper, int ident) {
|
||||
return ASensorManager_createEventQueue(manager, looper, ident, 0, 0);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
type ASensorVector C.ASensorVector2
|
||||
|
||||
func (x ASensorVector) X() Float { return (Float)(x.x) }
|
||||
func (x ASensorVector) Y() Float { return (Float)(x.y) }
|
||||
func (x ASensorVector) Z() Float { return (Float)(x.z) }
|
||||
|
||||
func (x *ASensorEvent) Acceleration() ASensorVector {
|
||||
return (ASensorVector)(C.ASensorAcceleration(x))
|
||||
}
|
||||
|
||||
func ASensorManagerCreateEventQueue(manager *ASensorManager, looper *ALooper, ident int) *ASensorEventQueue {
|
||||
ret := (*ASensorEventQueue)(C.ASensorManager_createEventQueue2((*C.ASensorManager)(manager), (*C.ALooper)(looper), (C.int)(ident)))
|
||||
return ret
|
||||
}
|
||||
|
2020
android29/main.go
2020
android29/main.go
File diff suppressed because it is too large
Load Diff
|
@ -5,9 +5,8 @@ package main
|
|||
|
||||
import (
|
||||
"gioui.org/app"
|
||||
"gioui.org/io/system"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
"gioui.org/io/system"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget/material"
|
||||
|
||||
|
@ -18,11 +17,6 @@ var (
|
|||
labchan chan string
|
||||
)
|
||||
|
||||
type (
|
||||
D = layout.Dimensions
|
||||
C = layout.Context
|
||||
)
|
||||
|
||||
func main() {
|
||||
labchan = make(chan string)
|
||||
go eventloop()
|
||||
|
@ -30,13 +24,14 @@ func main() {
|
|||
}
|
||||
|
||||
func eventloop() {
|
||||
gofont.Register()
|
||||
w := app.NewWindow(app.Title("Dex"))
|
||||
th := material.NewTheme(gofont.Collection())
|
||||
var ops op.Ops
|
||||
th := material.NewTheme()
|
||||
gtx := &layout.Context{Queue: w.Queue()}
|
||||
|
||||
sysinset := &layout.Inset{}
|
||||
margin := layout.UniformInset(unit.Dp(10))
|
||||
labels := []material.LabelStyle{}
|
||||
labels := []material.Label{}
|
||||
list := &layout.List{Axis: layout.Vertical}
|
||||
|
||||
go func() {
|
||||
|
@ -54,18 +49,18 @@ func eventloop() {
|
|||
|
||||
for { select {
|
||||
case x := <-labchan:
|
||||
labels = append(labels, material.Body1(th, x))
|
||||
labels = append(labels, th.Body1(x))
|
||||
case e := <-w.Events():
|
||||
switch e := e.(type) {
|
||||
case system.DestroyEvent:
|
||||
return
|
||||
case system.FrameEvent:
|
||||
gtx := layout.NewContext(&ops, e)
|
||||
gtx.Reset(e.Config, e.Size)
|
||||
resetSysinset(e.Insets)
|
||||
sysinset.Layout(gtx, func(gtx C) D {
|
||||
return margin.Layout(gtx, func(gtx C) D {
|
||||
return list.Layout(gtx, len(labels), func(gtx C, i int) D {
|
||||
return labels[i].Layout(gtx)
|
||||
sysinset.Layout(gtx, func() {
|
||||
margin.Layout(gtx, func() {
|
||||
list.Layout(gtx, len(labels), func(i int) {
|
||||
labels[i].Layout(gtx)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
"gioui.org/app"
|
||||
"gioui.org/io/system"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget/material"
|
||||
|
||||
|
@ -19,11 +18,6 @@ var (
|
|||
labchan chan string
|
||||
)
|
||||
|
||||
type (
|
||||
D = layout.Dimensions
|
||||
C = layout.Context
|
||||
)
|
||||
|
||||
func main() {
|
||||
labchan = make(chan string)
|
||||
log.Print("Staring event loop")
|
||||
|
@ -33,15 +27,16 @@ func main() {
|
|||
}
|
||||
|
||||
func eventloop() {
|
||||
gofont.Register()
|
||||
w := app.NewWindow(
|
||||
app.Size(unit.Dp(400), unit.Dp(400)),
|
||||
app.Title("Hello"))
|
||||
th := material.NewTheme(gofont.Collection())
|
||||
var ops op.Ops
|
||||
th := material.NewTheme()
|
||||
gtx := &layout.Context{Queue: w.Queue()}
|
||||
|
||||
sysinset := &layout.Inset{}
|
||||
margin := layout.UniformInset(unit.Dp(10))
|
||||
labels := []material.LabelStyle{}
|
||||
labels := []material.Label{}
|
||||
list := &layout.List{Axis: layout.Vertical}
|
||||
|
||||
resetSysinset := func(x system.Insets) {
|
||||
|
@ -57,18 +52,18 @@ func eventloop() {
|
|||
for {
|
||||
select {
|
||||
case x := <-labchan:
|
||||
labels = append(labels, material.Body1(th, x))
|
||||
labels = append(labels, th.Body1(x))
|
||||
case e := <-w.Events():
|
||||
switch e := e.(type) {
|
||||
case system.DestroyEvent:
|
||||
return
|
||||
case system.FrameEvent:
|
||||
gtx := layout.NewContext(&ops, e)
|
||||
gtx.Reset(e.Config, e.Size)
|
||||
resetSysinset(e.Insets)
|
||||
sysinset.Layout(gtx, func(gtx C) D {
|
||||
return margin.Layout(gtx, func(gtx C) D {
|
||||
return list.Layout(gtx, len(labels), func(gtx C, i int) D {
|
||||
return labels[i].Layout(gtx)
|
||||
sysinset.Layout(gtx, func() {
|
||||
margin.Layout(gtx, func() {
|
||||
list.Layout(gtx, len(labels), func(i int) {
|
||||
labels[i].Layout(gtx)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -3,6 +3,5 @@
|
|||
package main
|
||||
|
||||
func callJni() {
|
||||
labchan <- "nothing to do"
|
||||
}
|
||||
|
||||
|
|
|
@ -1,208 +0,0 @@
|
|||
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);
|
||||
}
|
|
@ -1,116 +0,0 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
#include <jni.h>
|
||||
#include <android/log.h>
|
||||
#include "jni_android.h"
|
||||
#include "_cgo_export.h"
|
||||
|
||||
static jobject gClassLoader;
|
||||
static jmethodID gFindClassMethod;
|
||||
|
||||
void
|
||||
SetLoader(JNIEnv* env, jobject context) {
|
||||
jclass cclass = (*env)->GetObjectClass(env, context);
|
||||
jmethodID gcl_id = (*env)->GetMethodID(env, cclass, "getClassLoader", "()Ljava/lang/ClassLoader;");
|
||||
jobject loader = (*env)->CallObjectMethod(env, context, gcl_id);
|
||||
gClassLoader = (*env)->NewGlobalRef(env, loader);
|
||||
jclass lclass = (*env)->GetObjectClass(env, loader);
|
||||
gFindClassMethod = (*env)->GetMethodID(env, lclass, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;");
|
||||
}
|
||||
|
||||
jclass
|
||||
FindClass(JNIEnv* env, char* name) {
|
||||
jstring strClassName = (*env)->NewStringUTF(env, name);
|
||||
return (*env)->CallObjectMethod(env, gClassLoader, gFindClassMethod, strClassName);
|
||||
}
|
||||
|
||||
jobject
|
||||
CreateObject(JNIEnv* env, jclass cls) {
|
||||
jmethodID init = (*env)->GetMethodID(env, cls, "<init>", "()V");
|
||||
return (*env)->NewObject(env, cls, init);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
static const JNINativeMethod actMethods[] = {
|
||||
{
|
||||
.name = "activityResult",
|
||||
.signature = "(II)V",
|
||||
.fnPtr = activityResult
|
||||
},
|
||||
{
|
||||
.name = "stringResult",
|
||||
.signature = "(ILjava/lang/String;)V",
|
||||
.fnPtr = stringResult
|
||||
},
|
||||
};
|
||||
|
||||
(*env)->RegisterNatives(env, cls, actMethods, sizeof(actMethods)/sizeof(actMethods[0]));
|
||||
|
||||
return (*env)->NewGlobalRef(env, ret);
|
||||
}
|
||||
|
||||
const char*
|
||||
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)V");
|
||||
jstring ret = (*env)->CallObjectMethod(env, p, mid, data, chint);
|
||||
return (*env)->GetStringUTFChars(env, ret, NULL);
|
||||
}
|
||||
|
||||
void stringResult(JNIEnv* env, jclass cls, jint requestCode, jobject response) {
|
||||
if (response == 0) {
|
||||
goStringResult(env, cls, requestCode, 0);
|
||||
} else {
|
||||
char* str = (*env)->GetStringUTFChars(env, response, NULL);
|
||||
goStringResult(env, cls, requestCode, str);
|
||||
}
|
||||
}
|
||||
|
||||
const char*
|
||||
Encrypt(JNIEnv* env, jobject p, char* cid, int idlen, char* cdata, int datalen, int chint) {
|
||||
jbyteArray id = (*env)->NewByteArray(env, idlen);
|
||||
(*env)->SetByteArrayRegion(env, id, 0, idlen, cid);
|
||||
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)V");
|
||||
jstring ret = (*env)->CallObjectMethod(env, p, mid, id, data, chint);
|
||||
return (*env)->GetStringUTFChars(env, ret, NULL);
|
||||
}
|
||||
|
||||
void CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID) {
|
||||
(*env)->CallVoidMethod(env, obj, methodID);
|
||||
}
|
||||
|
||||
void CallVoidMethod1(JNIEnv *env, jobject obj, jmethodID methodID, jobject arg) {
|
||||
(*env)->CallVoidMethod(env, obj, methodID, arg);
|
||||
}
|
||||
|
||||
jint CallIntMethod(JNIEnv *env, jobject obj, jmethodID methodID) {
|
||||
return (*env)->CallIntMethod(env, obj, methodID);
|
||||
}
|
||||
|
||||
jmethodID GetMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig) {
|
||||
return (*env)->GetMethodID(env, clazz, name, sig);
|
||||
}
|
||||
|
||||
jint GetEnv(JavaVM *vm, JNIEnv **env, jint version) {
|
||||
return (*vm)->GetEnv(vm, (void **)env, version);
|
||||
}
|
||||
|
||||
jint AttachCurrentThread(JavaVM *vm, JNIEnv **p_env, void *thr_args) {
|
||||
return (*vm)->AttachCurrentThread(vm, p_env, thr_args);
|
||||
}
|
||||
|
||||
jint DetachCurrentThread(JavaVM *vm) {
|
||||
return (*vm)->DetachCurrentThread(vm);
|
||||
}
|
||||
|
|
@ -1,160 +0,0 @@
|
|||
package main
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -landroid
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "jni_android.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var theJVM *C.JavaVM
|
||||
type JNIEnv = C.JNIEnv
|
||||
|
||||
func SetJVM(jvm, context uintptr) {
|
||||
log.Print("set theJVM")
|
||||
theJVM = (*C.JavaVM)(unsafe.Pointer(jvm))
|
||||
RunInJVM(func(env *C.JNIEnv) {
|
||||
C.SetLoader(env, (C.jobject)(context))
|
||||
})
|
||||
}
|
||||
|
||||
func FindClass(env *C.JNIEnv, name string) C.jclass {
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.FindClass((*C.JNIEnv)(env), cname)
|
||||
}
|
||||
|
||||
func JniCallVoidMethod(env *C.JNIEnv, obj C.jobject, methodID C.jmethodID) {
|
||||
C.CallVoidMethod(env, obj, methodID)
|
||||
}
|
||||
|
||||
func JniCallVoidMethod1(env *C.JNIEnv, obj C.jobject, methodID C.jmethodID, arg uintptr) {
|
||||
C.CallVoidMethod1(env, obj, methodID, C.jobject(unsafe.Pointer(arg)))
|
||||
}
|
||||
|
||||
func JniCallIntMethod(env *C.JNIEnv, obj C.jobject, methodID C.jmethodID) int {
|
||||
return (int)(C.CallIntMethod(env, obj, methodID))
|
||||
}
|
||||
|
||||
func JniGetMethodID(env *C.JNIEnv, cls C.jclass, name, sig string) C.jmethodID {
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
csig := C.CString(sig)
|
||||
defer C.free(unsafe.Pointer(csig))
|
||||
return C.GetMethodID(env, cls, cname, csig)
|
||||
}
|
||||
|
||||
func CreateObject(env *C.JNIEnv, cls C.jclass) C.jobject {
|
||||
return C.CreateObject(env, (C.jclass)(cls))
|
||||
}
|
||||
|
||||
type PGP C.jobject
|
||||
|
||||
func NewPgpConnect(env uintptr, act uintptr) PGP {
|
||||
return (PGP)(C.NewPgpConnect((*C.JNIEnv)(unsafe.Pointer(env)), (C.jobject)(unsafe.Pointer(act))))
|
||||
}
|
||||
|
||||
var (
|
||||
fragChans map[int]chan string
|
||||
chmux sync.Mutex
|
||||
chint int
|
||||
)
|
||||
|
||||
func (p PGP) Decrypt(data string) (string, error) {
|
||||
ch := make(chan string)
|
||||
chmux.Lock()
|
||||
if fragChans == nil {
|
||||
fragChans = make(map[int]chan string)
|
||||
}
|
||||
fragChans[chint] = ch
|
||||
curint := chint
|
||||
chint++
|
||||
chmux.Unlock()
|
||||
|
||||
cdata := C.CString(data)
|
||||
defer C.free(unsafe.Pointer(cdata))
|
||||
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))
|
||||
})
|
||||
switch x := <-ch; {
|
||||
case x == "":
|
||||
return "", fmt.Errorf("Decryption failed")
|
||||
default:
|
||||
return x, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p PGP) Encrypt(id, data string) (string, error) {
|
||||
ch := make(chan string)
|
||||
chmux.Lock()
|
||||
if fragChans == nil {
|
||||
fragChans = make(map[int]chan string)
|
||||
}
|
||||
fragChans[chint] = ch
|
||||
curint := chint
|
||||
chint++
|
||||
chmux.Unlock()
|
||||
|
||||
cid := C.CString(id)
|
||||
defer C.free(unsafe.Pointer(cid))
|
||||
cdata := C.CString(data)
|
||||
defer C.free(unsafe.Pointer(cdata))
|
||||
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))
|
||||
})
|
||||
switch x := <-ch; {
|
||||
case x == "":
|
||||
return "", fmt.Errorf("Encryption failed")
|
||||
default:
|
||||
return x, nil
|
||||
}
|
||||
}
|
||||
|
||||
func RunInJVM(f func(env *C.JNIEnv)) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
var env *C.JNIEnv
|
||||
var detach bool
|
||||
if res := C.GetEnv(theJVM, &env, C.JNI_VERSION_1_6); res != C.JNI_OK {
|
||||
if res != C.JNI_EDETACHED {
|
||||
panic(fmt.Errorf("JNI GetEnv failed with error %d", res))
|
||||
}
|
||||
if C.AttachCurrentThread(theJVM, &env, nil) != C.JNI_OK {
|
||||
panic(errors.New("runInJVM: AttachCurrentThread failed"))
|
||||
}
|
||||
detach = true
|
||||
}
|
||||
|
||||
if detach {
|
||||
defer func() {
|
||||
C.DetachCurrentThread(theJVM)
|
||||
}()
|
||||
}
|
||||
f(env)
|
||||
}
|
||||
|
||||
//export activityResult
|
||||
func activityResult(env *C.JNIEnv, class C.jclass, request, result C.jint) {
|
||||
log.Printf("activityResult (%d): %d", request, result)
|
||||
}
|
||||
|
||||
//export goStringResult
|
||||
func goStringResult(env *C.JNIEnv, class C.jclass, request C.jint, str *C.char) {
|
||||
fragChans[int(request)] <- C.GoString(str)
|
||||
C.free(unsafe.Pointer(str))
|
||||
chmux.Lock()
|
||||
delete(fragChans, int(request))
|
||||
chmux.Unlock()
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
#include <jni.h>
|
||||
|
||||
void SetLoader(JNIEnv* env, jobject context);
|
||||
jclass FindClass(JNIEnv* env, char* name);
|
||||
jobject CreateObject(JNIEnv* env, jclass cls);
|
||||
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);
|
||||
void stringResult(JNIEnv* env, jclass cls, jint requestCode, jobject response);
|
||||
void CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID);
|
||||
void CallVoidMethod1(JNIEnv *env, jobject obj, jmethodID methodID, jobject arg);
|
||||
jint CallIntMethod(JNIEnv *env, jobject obj, jmethodID methodID);
|
||||
jmethodID GetMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig);
|
||||
jint GetEnv(JavaVM *vm, JNIEnv **env, jint version);
|
||||
jint AttachCurrentThread(JavaVM *vm, JNIEnv **p_env, void *thr_args);
|
||||
jint DetachCurrentThread(JavaVM *vm);
|
|
@ -1,87 +0,0 @@
|
|||
// +build darwin linux
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"gioui.org/app"
|
||||
"gioui.org/io/system"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget/material"
|
||||
|
||||
"gioui.org/font/gofont"
|
||||
)
|
||||
|
||||
var (
|
||||
labchan chan string
|
||||
)
|
||||
|
||||
type (
|
||||
D = layout.Dimensions
|
||||
C = layout.Context
|
||||
)
|
||||
|
||||
func main() {
|
||||
labchan = make(chan string)
|
||||
log.Print("Staring event loop")
|
||||
go eventloop()
|
||||
app.Main()
|
||||
log.Print("app.Main() returned")
|
||||
}
|
||||
|
||||
func eventloop() {
|
||||
w := app.NewWindow(
|
||||
app.Size(unit.Dp(400), unit.Dp(400)),
|
||||
app.Title("Hello"))
|
||||
th := material.NewTheme(gofont.Collection())
|
||||
var ops op.Ops
|
||||
|
||||
sysinset := &layout.Inset{}
|
||||
margin := layout.UniformInset(unit.Dp(10))
|
||||
labels := []material.LabelStyle{}
|
||||
list := &layout.List{Axis: layout.Vertical}
|
||||
|
||||
resetSysinset := func(x system.Insets) {
|
||||
sysinset.Top = x.Top
|
||||
sysinset.Bottom = x.Bottom
|
||||
sysinset.Left = x.Left
|
||||
sysinset.Right = x.Right
|
||||
}
|
||||
go func() {
|
||||
labchan <- "Starting"
|
||||
callJni(w)
|
||||
}()
|
||||
for {
|
||||
select {
|
||||
case x := <-labchan:
|
||||
labels = append(labels, material.Body1(th, x))
|
||||
w.Invalidate()
|
||||
case e := <-w.Events():
|
||||
switch e := e.(type) {
|
||||
case system.DestroyEvent:
|
||||
return
|
||||
case system.FrameEvent:
|
||||
gtx := layout.NewContext(&ops, e)
|
||||
resetSysinset(e.Insets)
|
||||
sysinset.Layout(gtx, func(gtx C) D {
|
||||
return margin.Layout(gtx, func(gtx C) D {
|
||||
return list.Layout(gtx, len(labels), func(gtx C, i int) D {
|
||||
return labels[i].Layout(gtx)
|
||||
})
|
||||
})
|
||||
})
|
||||
e.Frame(gtx.Ops)
|
||||
case system.StageEvent:
|
||||
log.Printf("stage event -- %s", e.Stage)
|
||||
if e.Stage == system.StageRunning {
|
||||
initPgp(w)
|
||||
}
|
||||
case *system.CommandEvent:
|
||||
log.Print("command event")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -1,83 +0,0 @@
|
|||
//go:generate mkdir -p classes
|
||||
//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
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"gioui.org/app"
|
||||
)
|
||||
|
||||
var (
|
||||
h *app.Handle
|
||||
connected bool
|
||||
ready bool
|
||||
wch chan struct{}
|
||||
w *app.Window
|
||||
pgp PGP
|
||||
)
|
||||
|
||||
func init() {
|
||||
wch = make(chan struct{})
|
||||
}
|
||||
|
||||
func initPgp(x *app.Window) {
|
||||
w = x
|
||||
close(wch)
|
||||
}
|
||||
|
||||
func connect(w *app.Window) bool {
|
||||
<-wch
|
||||
if !connected {
|
||||
log.Print("PlatformHandle()")
|
||||
h = app.PlatformHandle()
|
||||
log.Print("SetJVM()")
|
||||
SetJVM(h.JVM, h.Context)
|
||||
log.Print("SetJVM() returned")
|
||||
log.Print("calling window.Run()()")
|
||||
w.Run(func(env app.JNIEnv, act app.Activity) {
|
||||
log.Print("in window.Run() -- NewPgpConnect()")
|
||||
pgp = NewPgpConnect(env.JNIEnv, act.Activity)
|
||||
})
|
||||
connected = true
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func checkPermission(w *app.Window) bool {
|
||||
if !connect(w) {
|
||||
return false
|
||||
}
|
||||
//return pgp.CheckPermission(env)
|
||||
return false
|
||||
}
|
||||
|
||||
func callJni(w *app.Window) {
|
||||
if !connect(w) {
|
||||
return
|
||||
}
|
||||
labchan <- fmt.Sprintf("JVM = %d", h.JVM)
|
||||
labchan <- fmt.Sprintf("Context = %d", h.Context)
|
||||
time.Sleep(time.Second / 5)
|
||||
|
||||
val, err := pgp.Encrypt("greg_pomerantz@yahoo.com", "hi there")
|
||||
if err != nil {
|
||||
fmt.Printf("Encryption error: %s", err)
|
||||
labchan <- err.Error()
|
||||
return
|
||||
}
|
||||
log.Print("val = ", val)
|
||||
val2, err := pgp.Decrypt(val)
|
||||
if err != nil {
|
||||
fmt.Printf("Decryption error: %s", err)
|
||||
labchan <- err.Error()
|
||||
return
|
||||
}
|
||||
labchan <- "result = " + val2
|
||||
}
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
// +build !android
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"gioui.org/app"
|
||||
)
|
||||
|
||||
func callJni(w *app.Window) {
|
||||
}
|
||||
|
||||
func initPgp(w *app.Window) {
|
||||
}
|
|
@ -3,41 +3,23 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image/color"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"gioui.org/app"
|
||||
"gioui.org/f32"
|
||||
"gioui.org/io/system"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
"gioui.org/op/clip"
|
||||
"gioui.org/op/paint"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget/material"
|
||||
|
||||
"gioui.org/font/gofont"
|
||||
)
|
||||
|
||||
type vector struct {
|
||||
x, y, z float64
|
||||
}
|
||||
|
||||
var (
|
||||
labchan chan string
|
||||
senschan chan vector
|
||||
)
|
||||
|
||||
type (
|
||||
D = layout.Dimensions
|
||||
C = layout.Context
|
||||
)
|
||||
|
||||
func main() {
|
||||
labchan = make(chan string)
|
||||
senschan = make(chan vector)
|
||||
log.Print("Staring event loop")
|
||||
go eventloop()
|
||||
app.Main()
|
||||
|
@ -52,16 +34,16 @@ func diffInsets(x, y system.Insets) bool {
|
|||
}
|
||||
|
||||
func eventloop() {
|
||||
gofont.Register()
|
||||
w := app.NewWindow(
|
||||
app.Size(unit.Dp(400), unit.Dp(400)),
|
||||
app.Title("Hello"))
|
||||
|
||||
th := material.NewTheme(gofont.Collection())
|
||||
var ops op.Ops
|
||||
th := material.NewTheme()
|
||||
gtx := &layout.Context{Queue: w.Queue()}
|
||||
|
||||
sysinset := &layout.Inset{}
|
||||
margin := layout.UniformInset(unit.Dp(10))
|
||||
labels := []material.LabelStyle{}
|
||||
labels := []material.Label{}
|
||||
list := &layout.List{Axis: layout.Vertical}
|
||||
|
||||
resetSysinset := func(x system.Insets) {
|
||||
|
@ -74,98 +56,23 @@ func eventloop() {
|
|||
go func() {
|
||||
labchan <- "Starting"
|
||||
}()
|
||||
|
||||
var accel vector
|
||||
accel.y = 1
|
||||
|
||||
lablist := func(gtx C) D {
|
||||
return list.Layout(gtx, len(labels), func(gtx C, i int) D {
|
||||
return labels[i].Layout(gtx)
|
||||
})
|
||||
}
|
||||
|
||||
vecview := func(gtx C) D {
|
||||
f := layout.Flex{Axis: layout.Horizontal, Spacing: layout.SpaceEvenly}
|
||||
return f.Layout(gtx,
|
||||
layout.Rigid(func(gtx C) D {
|
||||
return material.Body1(th, fmt.Sprintf("x = %f", accel.x)).Layout(gtx)
|
||||
}),
|
||||
layout.Rigid(func(gtx C) D {
|
||||
return material.Body1(th, fmt.Sprintf("y = %f", accel.y)).Layout(gtx)
|
||||
}),
|
||||
layout.Rigid(func(gtx C) D {
|
||||
return material.Body1(th, fmt.Sprintf("z = %f", accel.z)).Layout(gtx)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
var cx, cy, cvx, cvy, cs float32 // circle position
|
||||
cx = 200
|
||||
cy = 200
|
||||
cs = 50 // circle size (radius)
|
||||
|
||||
circle := func(gtx C, width, height float32) D {
|
||||
// clip circle position and bounce off of the edges
|
||||
if (cx < cs) { cx = cs; cvx = (-0.5) * cvx }
|
||||
if (cy < cs) { cy = cs; cvy = (-0.5) * cvy }
|
||||
if (cx > width - cs) { cx = width - cs; cvx = (-0.5) * cvx }
|
||||
if (cy > height - cs) { cy = height - cs; cvy = (-0.5) * cvy }
|
||||
|
||||
blue := color.RGBA{0x3f, 0x51, 0xb5, 0x80}
|
||||
r1 := f32.Rectangle{f32.Point{cx - cs, cy - cs}, f32.Point{cx + cs, cy + cs}}
|
||||
clip.Rect{ Rect: r1, NE: cs, NW: cs, SE: cs, SW: cs}.Op(gtx.Ops).Add(gtx.Ops)
|
||||
paint.ColorOp{Color: blue}.Add(gtx.Ops)
|
||||
paint.PaintOp{Rect: r1}.Add(gtx.Ops)
|
||||
|
||||
var ret D
|
||||
ret.Size.X = int(cs * 2)
|
||||
ret.Size.Y = int(cs * 2)
|
||||
return ret
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(time.Second / 60)
|
||||
|
||||
var t, told int64
|
||||
t = time.Now().UnixNano()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <- ticker.C:
|
||||
told = t
|
||||
t = time.Now().UnixNano()
|
||||
elapsed := float32((t - told) / 1000000)
|
||||
|
||||
cvx = cvx - float32(accel.x/1000) * elapsed
|
||||
cvy = cvy + float32(accel.y/1000) * elapsed
|
||||
|
||||
cx = cx + cvx * elapsed
|
||||
cy = cy + cvy * elapsed
|
||||
w.Invalidate()
|
||||
|
||||
case x := <-labchan:
|
||||
labels = append(labels, material.Body1(th, x))
|
||||
|
||||
case x := <-senschan:
|
||||
accel = x
|
||||
|
||||
labels = append(labels, th.Body1(x))
|
||||
case e := <-w.Events():
|
||||
switch e := e.(type) {
|
||||
case system.DestroyEvent:
|
||||
return
|
||||
case system.FrameEvent:
|
||||
gtx := layout.NewContext(&ops, e)
|
||||
gtx.Reset(e.Config, e.Size)
|
||||
resetSysinset(e.Insets)
|
||||
sysinset.Layout(gtx, func(gtx C) D {
|
||||
ret := margin.Layout(gtx, func(gtx C) D {
|
||||
f1 := layout.Flex{Axis: layout.Vertical}
|
||||
ret := f1.Layout(gtx,
|
||||
layout.Rigid(vecview),
|
||||
layout.Flexed(1, lablist),
|
||||
)
|
||||
circle(gtx, float32(ret.Size.X), float32(ret.Size.Y))
|
||||
return ret
|
||||
sysinset.Layout(gtx, func() {
|
||||
margin.Layout(gtx, func() {
|
||||
list.Layout(gtx, len(labels), func(i int) {
|
||||
labels[i].Layout(gtx)
|
||||
})
|
||||
})
|
||||
return ret
|
||||
})
|
||||
e.Frame(gtx.Ops)
|
||||
}
|
||||
|
|
|
@ -3,9 +3,9 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
"os"
|
||||
|
||||
"gioui.org/app"
|
||||
ndk "git.wow.st/gmp/android-go/android27"
|
||||
)
|
||||
|
||||
|
@ -13,74 +13,55 @@ var (
|
|||
tag *ndk.Char
|
||||
)
|
||||
|
||||
func mkHomeDir() {
|
||||
msg := ndk.CharWithGoString("getting datadir")
|
||||
ndk.AndroidLogWrite(ndk.ANDROID_LOG_WARN, tag, msg)
|
||||
msg.Free()
|
||||
|
||||
godir, _ := app.DataDir()
|
||||
labchan <- fmt.Sprintf("DataDir is %s", godir)
|
||||
|
||||
msg = ndk.CharWithGoString("ok. calling mkdir")
|
||||
ndk.AndroidLogWrite(ndk.ANDROID_LOG_WARN, tag, msg)
|
||||
msg.Free()
|
||||
|
||||
if _, err := os.Stat(godir); !os.IsNotExist(err) {
|
||||
labchan <- "already exists"
|
||||
return
|
||||
}
|
||||
if err := os.MkdirAll(godir, 0700); err != nil {
|
||||
fmt.Sprintf("Error creating directory: %s", err)
|
||||
labchan <- fmt.Sprintf("Error creating directory: %s", err)
|
||||
} else {
|
||||
labchan <- "created"
|
||||
}
|
||||
}
|
||||
|
||||
func apiLevel() {
|
||||
msg := ndk.CharWithGoString("hello log world")
|
||||
ndk.AndroidLogWrite(ndk.ANDROID_LOG_WARN, tag, msg)
|
||||
msg.Free()
|
||||
|
||||
apilevel := ndk.AndroidGetDeviceApiLevel()
|
||||
message := fmt.Sprintf("API level is %d", apilevel)
|
||||
labchan <- message
|
||||
|
||||
msg = ndk.CharWithGoString(message)
|
||||
ndk.AndroidLogWrite(ndk.ANDROID_LOG_WARN, tag, msg)
|
||||
msg.Free()
|
||||
}
|
||||
|
||||
func sensorLoop() {
|
||||
// the Android Looper is thread-local
|
||||
runtime.LockOSThread()
|
||||
|
||||
pkg := ndk.CharWithGoString("st.wow.git.sensors")
|
||||
manager := ndk.ASensorManagerGetInstanceForPackage(pkg)
|
||||
func getSensors() {
|
||||
pkg := ndk.CharWithGoString("st.wow.git.jni")
|
||||
sm := ndk.ASensorManagerGetInstanceForPackage(pkg)
|
||||
pkg.Free()
|
||||
|
||||
sens := ndk.ASensorManagerGetDefaultSensor(manager, ndk.ASENSOR_TYPE_ACCELEROMETER)
|
||||
sens := ndk.ASensorManagerGetDefaultSensor(sm, ndk.ASENSOR_TYPE_ACCELEROMETER)
|
||||
stp := ndk.ASensorGetStringType(sens)
|
||||
labchan <- "sensor" + stp.String()
|
||||
|
||||
var looper *ndk.ALooper
|
||||
var queue *ndk.ASensorEventQueue
|
||||
looper_id := 1
|
||||
var rate ndk.Int32_t = 60
|
||||
|
||||
setup := func() {
|
||||
looper = ndk.ALooperForThread()
|
||||
if (looper == nil) {
|
||||
labchan <- "no looper for thread"
|
||||
looper = ndk.ALooperPrepare(ndk.ALOOPER_PREPARE_ALLOW_NON_CALLBACKS)
|
||||
}
|
||||
|
||||
queue = ndk.ASensorManagerCreateEventQueue(manager, looper, looper_id)
|
||||
|
||||
ndk.ASensorEventQueueEnableSensor(queue, sens)
|
||||
ndk.ASensorEventQueueSetEventRate(queue, sens, 1000000/rate) // microseconds
|
||||
}
|
||||
|
||||
setup()
|
||||
labchan <- "polling accelerometer"
|
||||
for {
|
||||
var zero ndk.Int
|
||||
id := (int)(ndk.ALooperPollOnce(-1, &zero, &zero, (*unsafe.Pointer)(unsafe.Pointer(nil)))) // poll forever
|
||||
if (id == ndk.ALOOPER_POLL_ERROR) { // set up a new looper
|
||||
labchan <- "getting a new looper"
|
||||
setup()
|
||||
continue
|
||||
}
|
||||
if (id == looper_id) {
|
||||
var event ndk.ASensorEvent
|
||||
if (ndk.ASensorEventQueueGetEvents(queue, &event, 1) != 1) {
|
||||
continue
|
||||
}
|
||||
accel := (&event).Acceleration()
|
||||
senschan <- vector{float64(accel.X()), float64(accel.Y()), float64(accel.Z())}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
tag = ndk.CharWithGoString("gio")
|
||||
log.Print("Android starting")
|
||||
go apiLevel()
|
||||
go sensorLoop()
|
||||
go mkHomeDir()
|
||||
go getSensors()
|
||||
}
|
||||
|
|
4
go.mod
4
go.mod
|
@ -1,5 +1,5 @@
|
|||
module git.wow.st/gmp/android-go
|
||||
|
||||
go 1.14
|
||||
go 1.13
|
||||
|
||||
require gioui.org v0.0.0-20200611133933-6d5fbcba3f21
|
||||
require gioui.org v0.0.0-20191031200634-4e71f195ab51
|
||||
|
|
5
go.sum
5
go.sum
|
@ -1,10 +1,6 @@
|
|||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
|
||||
gioui.org v0.0.0-20191031200634-4e71f195ab51 h1:Epxo/e6oPvbDMMB8rrpmUlBe6ELeDpRuLzFUGfT6kJM=
|
||||
gioui.org v0.0.0-20191031200634-4e71f195ab51/go.mod h1:KqFFi2Dq5gYA3FJ0sDOt8OBXoMsuxMtE8v2f0JExXAY=
|
||||
gioui.org v0.0.0-20191112235626-1bf2c7ef293c h1:jhUehzf6xTCgEewbJyjd+WE2Kq5pvPIxq2MahDph0mg=
|
||||
gioui.org v0.0.0-20191112235626-1bf2c7ef293c/go.mod h1:KqFFi2Dq5gYA3FJ0sDOt8OBXoMsuxMtE8v2f0JExXAY=
|
||||
gioui.org v0.0.0-20200611133933-6d5fbcba3f21 h1:VFB+0QxUPABMgEarfyJeHQvUNc++9TASnDVXcFgxLcA=
|
||||
gioui.org v0.0.0-20200611133933-6d5fbcba3f21/go.mod h1:AHI9rFr6AEEHCb8EPVtb/p5M+NMJRKH58IOp8O3Je04=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
|
@ -24,7 +20,6 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
|
|||
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/tools v0.0.0-20190927191325-030b2cf1153e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
|
|
Loading…
Reference in New Issue
Block a user