Pause recurring tasks on device idle

This commit is contained in:
arm64v8a 2023-08-08 17:10:01 +09:00
parent d1da522700
commit 2c8264d7e8
2 changed files with 31 additions and 4 deletions

View File

@ -52,6 +52,16 @@ class BaseService {
Intent.ACTION_SHUTDOWN -> service.persistStats() Intent.ACTION_SHUTDOWN -> service.persistStats()
Action.RELOAD -> service.reload() Action.RELOAD -> service.reload()
// Action.SWITCH_WAKE_LOCK -> runOnDefaultDispatcher { service.switchWakeLock() } // Action.SWITCH_WAKE_LOCK -> runOnDefaultDispatcher { service.switchWakeLock() }
PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (SagerNet.power.isDeviceIdleMode) {
proxy?.box?.sleep()
} else {
proxy?.box?.wake()
}
}
}
Action.RESET_UPSTREAM_CONNECTIONS -> runOnDefaultDispatcher { Action.RESET_UPSTREAM_CONNECTIONS -> runOnDefaultDispatcher {
LibcoreUtil.resetAllConnections(true) LibcoreUtil.resetAllConnections(true)
runOnMainDispatcher { runOnMainDispatcher {
@ -323,6 +333,9 @@ class BaseService {
addAction(Intent.ACTION_SHUTDOWN) addAction(Intent.ACTION_SHUTDOWN)
addAction(Action.CLOSE) addAction(Action.CLOSE)
// addAction(Action.SWITCH_WAKE_LOCK) // addAction(Action.SWITCH_WAKE_LOCK)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED)
}
addAction(Action.RESET_UPSTREAM_CONNECTIONS) addAction(Action.RESET_UPSTREAM_CONNECTIONS)
}, "$packageName.SERVICE", null) }, "$packageName.SERVICE", null)
data.closeReceiverRegistered = true data.closeReceiverRegistered = true

View File

@ -22,6 +22,7 @@ import (
"github.com/sagernet/sing-box/common/dialer/conntrack" "github.com/sagernet/sing-box/common/dialer/conntrack"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/outbound" "github.com/sagernet/sing-box/outbound"
"github.com/sagernet/sing/service/pause"
) )
var mainInstance *BoxInstance var mainInstance *BoxInstance
@ -62,8 +63,9 @@ type BoxInstance struct {
cancel context.CancelFunc cancel context.CancelFunc
state int state int
v2api *boxapi.SbV2rayServer v2api *boxapi.SbV2rayServer
selector *outbound.Selector selector *outbound.Selector
pauseManager pause.Manager
ForTest bool ForTest bool
} }
@ -80,6 +82,8 @@ func NewSingBoxInstance(config string) (b *BoxInstance, err error) {
// create box // create box
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
sleepManager := pause.NewDefaultManager(ctx)
ctx = pause.ContextWithManager(ctx, sleepManager)
instance, err := boxbox.New(boxbox.Options{ instance, err := boxbox.New(boxbox.Options{
Options: options, Options: options,
Context: ctx, Context: ctx,
@ -91,8 +95,9 @@ func NewSingBoxInstance(config string) (b *BoxInstance, err error) {
} }
b = &BoxInstance{ b = &BoxInstance{
Box: instance, Box: instance,
cancel: cancel, cancel: cancel,
pauseManager: sleepManager,
} }
// fuck your sing-box platformFormatter // fuck your sing-box platformFormatter
@ -145,6 +150,15 @@ func (b *BoxInstance) Close() (err error) {
return nil return nil
} }
func (b *BoxInstance) Sleep() {
b.pauseManager.DevicePause()
_ = b.Box.Router().ResetNetwork()
}
func (b *BoxInstance) Wake() {
b.pauseManager.DeviceWake()
}
func (b *BoxInstance) SetAsMain() { func (b *BoxInstance) SetAsMain() {
mainInstance = b mainInstance = b
goServeProtect(true) goServeProtect(true)