mirror of
https://github.com/MatsuriDayo/NekoBoxForAndroid.git
synced 2025-12-19 14:40:06 +08:00
Pause recurring tasks on device idle
This commit is contained in:
parent
d1da522700
commit
2c8264d7e8
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
@ -64,6 +65,7 @@ type BoxInstance struct {
|
|||||||
|
|
||||||
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,
|
||||||
@ -93,6 +97,7 @@ 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)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user