2020-06-19 17:40:18 -04:00
|
|
|
package android
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-06-19 18:28:01 -04:00
|
|
|
"runtime"
|
2020-06-19 17:40:18 -04:00
|
|
|
|
|
|
|
"gioui.org/app"
|
|
|
|
"github.com/tailscale/tailscale-android/jni"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NotificationChannel struct {
|
2020-06-19 18:28:01 -04:00
|
|
|
javaObj jni.Object
|
2020-06-19 17:40:18 -04:00
|
|
|
}
|
|
|
|
|
2020-06-19 18:28:01 -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
|
|
|
|
}
|
2020-06-19 18:28:01 -04:00
|
|
|
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)
|
|
|
|
}
|
2020-06-19 18:28:01 -04:00
|
|
|
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
|
|
|
}
|