You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tailscale-android/libtailscale/notifier.go

49 lines
1.0 KiB
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package libtailscale
import (
"context"
"encoding/json"
"log"
"runtime/debug"
"tailscale.com/ipn"
)
func (app *App) WatchNotifications(mask int, cb NotificationCallback) NotificationManager {
app.ready.Wait()
ctx, cancel := context.WithCancel(context.Background())
go app.backend.WatchNotifications(ctx, ipn.NotifyWatchOpt(mask), func() {}, func(notify *ipn.Notify) bool {
defer func() {
if p := recover(); p != nil {
log.Printf("panic in WatchNotifications %s: %s", p, debug.Stack())
panic(p)
}
}()
b, err := json.Marshal(notify)
if err != nil {
log.Printf("error: WatchNotifications: marshal notify: %s", err)
return true
}
err = cb.OnNotify(b)
if err != nil {
log.Printf("error: WatchNotifications: OnNotify: %s", err)
return true
}
return true
})
return &notificationManager{cancel}
}
type notificationManager struct {
cancel func()
}
func (nm *notificationManager) Stop() {
nm.cancel()
}