feat: simplify implementation by avoiding returning channel

This commit is contained in:
Chris Waldon 2020-06-19 18:35:55 -04:00
parent 52786735c7
commit 420149d9e9
No known key found for this signature in database
GPG Key ID: 35BBC7C073F5A5D3
3 changed files with 12 additions and 24 deletions

View File

@ -9,20 +9,16 @@ import android.graphics.Bitmap;
import android.graphics.drawable.Icon; import android.graphics.drawable.Icon;
public class NotificationHelper { public class NotificationHelper {
public static NotificationChannel newChannel(Context ctx) { private final static String tag = "NotificationHelper";
String tag = "NotificationHelper"; public static void newChannel(Context ctx, String channelID, String name, String description) {
Log.w(tag,String.format("newChannel invoked")); 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; int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); NotificationChannel channel = new NotificationChannel(channelID, name, importance);
Log.e(tag,String.format("channel: %s",channel)); Log.e(tag,String.format("channel: %s",channel));
channel.setDescription(description); channel.setDescription(description);
NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class); NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class);
Log.e(tag,String.format("manager: %s",notificationManager)); Log.e(tag,String.format("manager: %s",notificationManager));
notificationManager.createNotificationChannel(channel); notificationManager.createNotificationChannel(channel);
return channel;
} }
} }

View File

@ -2,18 +2,16 @@ 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 id string
} }
func NewChannel(name, description string) (*NotificationChannel, error) { func NewChannel(id, name, description string) (*NotificationChannel, error) {
var channel jni.Object
if err := jni.Do(jni.JVMFor(app.JavaVM()), func(env jni.Env) error { if err := jni.Do(jni.JVMFor(app.JavaVM()), func(env jni.Env) error {
appCtx := jni.Object(app.AppContext()) appCtx := jni.Object(app.AppContext())
classLoader := jni.ClassLoaderFor(env, appCtx) classLoader := jni.ClassLoaderFor(env, appCtx)
@ -21,26 +19,20 @@ func NewChannel(name, description string) (*NotificationChannel, error) {
if err != nil { if err != nil {
return err return err
} }
newChannelMethod := jni.GetStaticMethodID(env, notifyClass, "newChannel", "(Landroid/content/Context;)Landroid/app/NotificationChannel;") newChannelMethod := jni.GetStaticMethodID(env, notifyClass, "newChannel", "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")
// 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))
channel, err = jni.CallStaticObjectMethod(env, notifyClass, newChannelMethod, jni.Value(app.AppContext())) jID := jni.Value(jni.JavaString(env, id))
err = jni.CallStaticVoidMethod(env, notifyClass, newChannelMethod, jni.Value(app.AppContext()), jID, jname, jdescription)
if err != nil { if err != nil {
return err return err
} }
channel = jni.NewGlobalRef(env, channel)
return nil 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)
} }
nc := &NotificationChannel{ nc := &NotificationChannel{
javaObj: channel, id: id,
} }
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 return nc, nil
} }

View File

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