Skip to content

Commit

Permalink
Implement counter app
Browse files Browse the repository at this point in the history
  • Loading branch information
desttinghim authored and ikskuh committed Jan 10, 2023
1 parent b09c072 commit 9324dee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
25 changes: 22 additions & 3 deletions examples/invocationhandler/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,33 @@ comptime {
_ = @import("root").log;
}

pub fn timerInvoke(_: ?*anyopaque, jni: android.jni.JNI, method: android.jobject, args: android.jobjectArray) android.jobject {
const ButtonData = struct {
count: usize = 0,
};

pub fn timerInvoke(data: ?*anyopaque, jni: android.jni.JNI, method: android.jobject, args: android.jobjectArray) android.jobject {
var btn_data = @ptrCast(*ButtonData, @alignCast(@alignOf(*ButtonData), data));
btn_data.count += 1;
std.log.info("Running invoke!", .{});
_ = method;
const method_name = android.jni.JNI.String.init(jni, jni.callObjectMethod(method, "getName", "()Ljava/lang/String;", .{}));
defer method_name.deinit(jni);
std.log.info("Method {}", .{std.unicode.fmtUtf16le(method_name.slice)});

const length = jni.invokeJni(.GetArrayLength, .{args});
var i: i32 = 0;
while (i < length) : (i += 1) {
const object = jni.invokeJni(.GetObjectArrayElement, .{ args, i });
const string = android.jni.JNI.String.init(jni, jni.callObjectMethod(object, "toString", "()Ljava/lang/String;", .{}));
defer string.deinit(jni);
std.log.info("Arg {}: {}", .{ i, std.unicode.fmtUtf16le(string.slice) });

if (i == 0) {
const Button = jni.findClass("android/widget/Button");
const setText = jni.invokeJni(.GetMethodID, .{ Button, "setText", "(Ljava/lang/CharSequence;)V" });
var buf: [256:0]u8 = undefined;
const str = std.fmt.bufPrintZ(&buf, "Pressed {} times!", .{btn_data.count}) catch "formatting bug";
jni.invokeJni(.CallVoidMethod, .{ object, setText, jni.newString(str) });
}
}

return null;
Expand Down Expand Up @@ -53,6 +70,8 @@ pub const AndroidApp = struct {
uiThreadLooper: *android.ALooper = undefined,
uiThreadId: std.Thread.Id = undefined,

btn_data: ButtonData = .{},

pub fn init(allocator: std.mem.Allocator, activity: *android.ANativeActivity, stored_state: ?[]const u8) !AndroidApp {
_ = stored_state;

Expand Down Expand Up @@ -241,7 +260,7 @@ pub const AndroidApp = struct {
self.invocation_handler = NativeInvocationHandler.init(jni, class);

// Create a TimerTask invoker
const invocation_handler = try self.invocation_handler.createAlloc(jni, self.allocator, null, &timerInvoke);
const invocation_handler = try self.invocation_handler.createAlloc(jni, self.allocator, &self.btn_data, &timerInvoke);

std.log.info("Creating Interface array", .{});
const interface_array = jni.invokeJni(.NewObjectArray, .{
Expand Down
5 changes: 0 additions & 5 deletions src/native-invocation-handler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ class: android.jobject,
initFn: android.jmethodID,

pub fn init(jni: android.jni.JNI, class: android.jobject) Self {
// const native_invocation_handler_buffer = @embedFile("NativeInvocationHandler.dex");
// TODO: define class is not implemented on android
// const NativeInvocationHandler = jni.invokeJni(.DefineClass, .{ "NativeInvocationHandler", class_loader, @ptrCast([*]const i8, &native_invocation_handler_buffer), native_invocation_handler_buffer.len });
// const NativeInvocationHandler = jni.findClass("NativeInvocationHandler");
// const NativeInvocationHandler = jni.findClass("net/random_projects/zig_android_template/NativeInvocationHandler");
const methods = [_]android.JNINativeMethod{
.{
.name = "invoke0",
Expand Down

0 comments on commit 9324dee

Please sign in to comment.