From e6885e9967e831fbcc72f1a6febb1be7fcef6a78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 8 Feb 2024 22:14:09 +0800 Subject: [PATCH] platform: Ignore momentary pause on iOS --- experimental/libbox/service.go | 12 ++------ experimental/libbox/service_pause.go | 45 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 experimental/libbox/service_pause.go diff --git a/experimental/libbox/service.go b/experimental/libbox/service.go index a484c64b..b30ca22f 100644 --- a/experimental/libbox/service.go +++ b/experimental/libbox/service.go @@ -32,6 +32,8 @@ type BoxService struct { instance *box.Box pauseManager pause.Manager urlTestHistoryStorage *urltest.HistoryStorage + + servicePauseFields } func NewService(configContent string, platformInterface PlatformInterface) (*BoxService, error) { @@ -75,16 +77,6 @@ func (s *BoxService) Close() error { return s.instance.Close() } -func (s *BoxService) Sleep() { - s.pauseManager.DevicePause() - _ = s.instance.Router().ResetNetwork() -} - -func (s *BoxService) Wake() { - s.pauseManager.DeviceWake() - _ = s.instance.Router().ResetNetwork() -} - var ( _ platform.Interface = (*platformInterfaceWrapper)(nil) _ log.PlatformWriter = (*platformInterfaceWrapper)(nil) diff --git a/experimental/libbox/service_pause.go b/experimental/libbox/service_pause.go new file mode 100644 index 00000000..4a58fb50 --- /dev/null +++ b/experimental/libbox/service_pause.go @@ -0,0 +1,45 @@ +package libbox + +import ( + "sync" + "time" +) + +type servicePauseFields struct { + pauseAccess sync.Mutex + pauseTimer *time.Timer +} + +func (s *BoxService) Pause() { + s.pauseAccess.Lock() + defer s.pauseAccess.Unlock() + + if s.pauseTimer != nil { + s.pauseTimer.Stop() + } + + s.pauseTimer = time.AfterFunc(time.Minute, s.pause) +} + +func (s *BoxService) pause() { + s.pauseAccess.Lock() + defer s.pauseAccess.Unlock() + + s.pauseManager.DevicePause() + _ = s.instance.Router().ResetNetwork() + s.pauseTimer = nil +} + +func (s *BoxService) Wake() { + s.pauseAccess.Lock() + defer s.pauseAccess.Unlock() + + if s.pauseTimer != nil { + s.pauseTimer.Stop() + s.pauseTimer = nil + return + } + + s.pauseManager.DeviceWake() + _ = s.instance.Router().ResetNetwork() +}