mirror of
https://github.com/MatsuriDayo/NekoBoxForAndroid.git
synced 2025-12-19 22:50:05 +08:00
optimize traffic looper
This commit is contained in:
parent
ccbc71c8a9
commit
9d78e4f287
@ -48,6 +48,8 @@ class ServiceNotification(
|
||||
}
|
||||
}
|
||||
|
||||
var listenPostSpeed = true
|
||||
|
||||
fun postNotificationSpeedUpdate(stats: SpeedDisplayData) {
|
||||
builder.apply {
|
||||
if (showDirectSpeed) {
|
||||
@ -119,7 +121,6 @@ class ServiceNotification(
|
||||
Theme.apply(service)
|
||||
builder.color = service.getColorAttr(R.attr.colorPrimary)
|
||||
|
||||
updateCallback(SagerNet.power.isInteractive)
|
||||
service.registerReceiver(this, IntentFilter().apply {
|
||||
addAction(Intent.ACTION_SCREEN_ON)
|
||||
addAction(Intent.ACTION_SCREEN_OFF)
|
||||
@ -167,23 +168,19 @@ class ServiceNotification(
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
if (service.data.state == BaseService.State.Connected) updateCallback(intent.action == Intent.ACTION_SCREEN_ON)
|
||||
if (service.data.state == BaseService.State.Connected) {
|
||||
listenPostSpeed = intent.action == Intent.ACTION_SCREEN_ON
|
||||
}
|
||||
}
|
||||
|
||||
var listenPostSpeed = false
|
||||
|
||||
private fun updateCallback(screenOn: Boolean) {
|
||||
if (DataStore.speedInterval == 0) return
|
||||
listenPostSpeed = screenOn
|
||||
}
|
||||
|
||||
private fun show() = (service as Service).startForeground(notificationId, builder.build())
|
||||
private fun update() =
|
||||
NotificationManagerCompat.from(service as Service).notify(notificationId, builder.build())
|
||||
|
||||
fun destroy() {
|
||||
listenPostSpeed = false
|
||||
(service as Service).stopForeground(true)
|
||||
service.unregisterReceiver(this)
|
||||
updateCallback(false)
|
||||
}
|
||||
}
|
||||
|
||||
@ -89,9 +89,8 @@ class TrafficLooper
|
||||
var proxy: ProxyInstance?
|
||||
|
||||
// for display
|
||||
var itemMain: TrafficUpdater.TrafficLooperData? = null
|
||||
var itemMainBase: TrafficUpdater.TrafficLooperData? = null
|
||||
var itemBypass: TrafficUpdater.TrafficLooperData? = null
|
||||
val itemBypass = TrafficUpdater.TrafficLooperData(tag = TAG_BYPASS)
|
||||
val itemMainList = mutableListOf<TrafficUpdater.TrafficLooperData>()
|
||||
|
||||
while (sc.isActive) {
|
||||
delay(delayMs)
|
||||
@ -100,7 +99,6 @@ class TrafficLooper
|
||||
if (trafficUpdater == null) {
|
||||
if (!proxy.isInitialized()) continue
|
||||
items.clear()
|
||||
itemBypass = TrafficUpdater.TrafficLooperData(tag = "bypass")
|
||||
items[-1] = itemBypass
|
||||
//
|
||||
val tags = hashSetOf(TAG_PROXY, TAG_BYPASS)
|
||||
@ -111,25 +109,16 @@ class TrafficLooper
|
||||
tag = tag,
|
||||
rx = ent.rx,
|
||||
tx = ent.tx,
|
||||
rxBase = ent.rx,
|
||||
txBase = ent.tx,
|
||||
ignore = proxy.config.selectorGroupId >= 0L,
|
||||
)
|
||||
if (tag == TAG_PROXY && itemMain == null) {
|
||||
itemMain = item
|
||||
itemMainBase = TrafficUpdater.TrafficLooperData(
|
||||
tag = tag,
|
||||
rx = ent.rx,
|
||||
tx = ent.tx,
|
||||
)
|
||||
Logs.d("traffic count $tag to main to ${ent.id}")
|
||||
}
|
||||
items[ent.id] = item
|
||||
itemMainList += item
|
||||
Logs.d("traffic count $tag to ${ent.id}")
|
||||
}
|
||||
}
|
||||
if (proxy.config.selectorGroupId >= 0L) {
|
||||
itemMain = TrafficUpdater.TrafficLooperData(tag = TAG_PROXY)
|
||||
itemMainBase = TrafficUpdater.TrafficLooperData(tag = TAG_PROXY)
|
||||
items[-2] = itemMain!!
|
||||
selectMain(proxy.config.mainEntId)
|
||||
}
|
||||
//
|
||||
@ -142,14 +131,26 @@ class TrafficLooper
|
||||
trafficUpdater.updateAll()
|
||||
if (!sc.isActive) return
|
||||
|
||||
// add all non-bypass to "main"
|
||||
var mainTxRate = 0L
|
||||
var mainRxRate = 0L
|
||||
var mainTx = 0L
|
||||
var mainRx = 0L
|
||||
itemMainList.forEach {
|
||||
mainTxRate += it.txRate
|
||||
mainRxRate += it.rxRate
|
||||
mainTx += it.tx - it.txBase
|
||||
mainRx += it.rx - it.rxBase
|
||||
}
|
||||
|
||||
// speed
|
||||
val speed = SpeedDisplayData(
|
||||
itemMain!!.txRate,
|
||||
itemMain!!.rxRate,
|
||||
if (showDirectSpeed) itemBypass!!.txRate else 0L,
|
||||
if (showDirectSpeed) itemBypass!!.rxRate else 0L,
|
||||
itemMain!!.tx - itemMainBase!!.tx,
|
||||
itemMain!!.rx - itemMainBase!!.rx
|
||||
mainTxRate,
|
||||
mainRxRate,
|
||||
if (showDirectSpeed) itemBypass.txRate else 0L,
|
||||
if (showDirectSpeed) itemBypass.rxRate else 0L,
|
||||
mainTx,
|
||||
mainRx
|
||||
)
|
||||
|
||||
// broadcast (MainActivity)
|
||||
|
||||
@ -10,6 +10,8 @@ class TrafficUpdater(
|
||||
var tag: String,
|
||||
var tx: Long = 0,
|
||||
var rx: Long = 0,
|
||||
var txBase: Long = 0,
|
||||
var rxBase: Long = 0,
|
||||
var txRate: Long = 0,
|
||||
var rxRate: Long = 0,
|
||||
var lastUpdate: Long = 0,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user