feat: instantiate channel and return to go

This commit is contained in:
Chris Waldon 2020-06-19 18:28:01 -04:00
parent e9b23d155d
commit 52786735c7
No known key found for this signature in database
GPG Key ID: 35BBC7C073F5A5D3
3 changed files with 43 additions and 11 deletions

View File

@ -1,5 +1,6 @@
package ht.sr.git.whereswaldon.niotify; package ht.sr.git.whereswaldon.niotify;
import android.content.Context;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
@ -8,7 +9,20 @@ import android.graphics.Bitmap;
import android.graphics.drawable.Icon; import android.graphics.drawable.Icon;
public class NotificationHelper { public class NotificationHelper {
public static void newChannel() { public static NotificationChannel newChannel(Context ctx) {
Log.w("NotificationHelper",String.format("newChannel invoked")); String tag = "NotificationHelper";
Log.w(tag,String.format("newChannel invoked"));
String CHANNEL_ID = "CHANNEL_ID";
CharSequence name = "notification_channel_name";
String description = "notification_channel_description";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
Log.e(tag,String.format("channel: %s",channel));
channel.setDescription(description);
NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class);
Log.e(tag,String.format("manager: %s",notificationManager));
notificationManager.createNotificationChannel(channel);
return channel;
} }
} }

View File

@ -2,27 +2,45 @@ package android
import ( import (
"fmt" "fmt"
"runtime"
"gioui.org/app" "gioui.org/app"
"github.com/tailscale/tailscale-android/jni" "github.com/tailscale/tailscale-android/jni"
) )
type NotificationChannel struct { type NotificationChannel struct {
javaObj jni.Object
} }
func NewChannel(jvm uintptr, name, description string) (*NotificationChannel, error) { func NewChannel(name, description string) (*NotificationChannel, error) {
if err := jni.Do(jni.JVMFor(jvm), func(env jni.Env) error { var channel jni.Object
classLoader := jni.ClassLoaderFor(env, jni.Object(app.AppContext())) if err := jni.Do(jni.JVMFor(app.JavaVM()), func(env jni.Env) error {
appCtx := jni.Object(app.AppContext())
classLoader := jni.ClassLoaderFor(env, appCtx)
notifyClass, err := jni.LoadClass(env, classLoader, "ht/sr/git/whereswaldon/niotify/NotificationHelper") notifyClass, err := jni.LoadClass(env, classLoader, "ht/sr/git/whereswaldon/niotify/NotificationHelper")
if err != nil { if err != nil {
return err return err
} }
newChannelMethod := jni.GetStaticMethodID(env, notifyClass, "newChannel", "()V") newChannelMethod := jni.GetStaticMethodID(env, notifyClass, "newChannel", "(Landroid/content/Context;)Landroid/app/NotificationChannel;")
jname := jni.Value(jni.JavaString(env, name)) // jname := jni.Value(jni.JavaString(env, name))
jdescription := jni.Value(jni.JavaString(env, description)) // jdescription := jni.Value(jni.JavaString(env, description))
return jni.CallStaticVoidMethod(env, notifyClass, newChannelMethod, jname, jdescription) channel, err = jni.CallStaticObjectMethod(env, notifyClass, newChannelMethod, jni.Value(app.AppContext()))
if err != nil {
return err
}
channel = jni.NewGlobalRef(env, channel)
return nil
}); err != nil { }); err != nil {
return nil, fmt.Errorf("failed creating notification channel: %w", err) return nil, fmt.Errorf("failed creating notification channel: %w", err)
} }
return &NotificationChannel{}, nil nc := &NotificationChannel{
javaObj: channel,
}
runtime.SetFinalizer(nc, func(obj *NotificationChannel) {
_ = jni.Do(jni.JVMFor(app.JavaVM()), func(env jni.Env) error {
jni.DeleteGlobalRef(env, obj.javaObj)
return nil
})
})
return nc, nil
} }

View File

@ -31,7 +31,7 @@ func main() {
} }
}() }()
go func() { go func() {
channel, err := android.NewChannel(app.JavaVM(), "hello", "description") channel, err := android.NewChannel("hello", "description")
if err != nil { if err != nil {
log.Printf("channel creation failed: %v", err) log.Printf("channel creation failed: %v", err)
} }