niotify/android/notify_android.go

47 lines
1.3 KiB
Go
Raw Normal View History

2020-06-19 17:40:18 -04:00
package android
import (
"fmt"
"runtime"
2020-06-19 17:40:18 -04:00
"gioui.org/app"
"github.com/tailscale/tailscale-android/jni"
)
type NotificationChannel struct {
javaObj jni.Object
2020-06-19 17:40:18 -04:00
}
func NewChannel(name, description string) (*NotificationChannel, error) {
var channel jni.Object
if err := jni.Do(jni.JVMFor(app.JavaVM()), func(env jni.Env) error {
appCtx := jni.Object(app.AppContext())
classLoader := jni.ClassLoaderFor(env, appCtx)
2020-06-19 17:40:18 -04:00
notifyClass, err := jni.LoadClass(env, classLoader, "ht/sr/git/whereswaldon/niotify/NotificationHelper")
if err != nil {
return err
}
newChannelMethod := jni.GetStaticMethodID(env, notifyClass, "newChannel", "(Landroid/content/Context;)Landroid/app/NotificationChannel;")
// jname := jni.Value(jni.JavaString(env, name))
// jdescription := jni.Value(jni.JavaString(env, description))
channel, err = jni.CallStaticObjectMethod(env, notifyClass, newChannelMethod, jni.Value(app.AppContext()))
if err != nil {
return err
}
channel = jni.NewGlobalRef(env, channel)
return nil
2020-06-19 17:40:18 -04:00
}); err != nil {
return nil, fmt.Errorf("failed creating notification channel: %w", err)
}
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
2020-06-19 17:40:18 -04:00
}