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;
public class NotificationHelper {
public static NotificationChannel newChannel(Context ctx) {
String tag = "NotificationHelper";
private final static String tag = "NotificationHelper";
public static void newChannel(Context ctx, String channelID, String name, String description) {
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);
NotificationChannel channel = new NotificationChannel(channelID, 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,18 +2,16 @@ package android
import (
"fmt"
"runtime"
"gioui.org/app"
"github.com/tailscale/tailscale-android/jni"
)
type NotificationChannel struct {
javaObj jni.Object
id string
}
func NewChannel(name, description string) (*NotificationChannel, error) {
var channel jni.Object
func NewChannel(id, name, description string) (*NotificationChannel, error) {
if err := jni.Do(jni.JVMFor(app.JavaVM()), func(env jni.Env) error {
appCtx := jni.Object(app.AppContext())
classLoader := jni.ClassLoaderFor(env, appCtx)
@ -21,26 +19,20 @@ func NewChannel(name, description string) (*NotificationChannel, error) {
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()))
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))
jdescription := jni.Value(jni.JavaString(env, description))
jID := jni.Value(jni.JavaString(env, id))
err = jni.CallStaticVoidMethod(env, notifyClass, newChannelMethod, jni.Value(app.AppContext()), jID, jname, jdescription)
if err != nil {
return err
}
channel = jni.NewGlobalRef(env, channel)
return nil
}); err != nil {
return nil, fmt.Errorf("failed creating notification channel: %w", err)
}
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
}

View File

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