mirror of
https://github.com/MatsuriDayo/NekoBoxForAndroid.git
synced 2025-12-19 22:50:05 +08:00
improve usage
This commit is contained in:
parent
d6b8a98b7d
commit
d94e385df8
@ -38,9 +38,6 @@
|
||||
android:required="false" />
|
||||
|
||||
<queries>
|
||||
<intent>
|
||||
<action android:name="com.github.shadowsocks.plugin.ACTION_NATIVE_PLUGIN" />
|
||||
</intent>
|
||||
<intent>
|
||||
<action android:name="io.nekohasekai.sagernet.plugin.ACTION_NATIVE_PLUGIN" />
|
||||
</intent>
|
||||
@ -294,18 +291,6 @@
|
||||
android:resource="@xml/cache_paths" />
|
||||
</provider>
|
||||
|
||||
<receiver
|
||||
android:name="io.nekohasekai.sagernet.BootReceiver"
|
||||
android:enabled="false"
|
||||
android:exported="true"
|
||||
android:process=":bg">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.BOOT_COMPLETED" />
|
||||
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
|
||||
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<provider
|
||||
android:name="androidx.startup.InitializationProvider"
|
||||
android:authorities="${applicationId}.androidx-startup"
|
||||
|
||||
@ -80,7 +80,9 @@ class BaseService {
|
||||
override fun getProfileName(): String = data?.proxy?.profile?.displayName() ?: "Idle"
|
||||
|
||||
override fun registerCallback(cb: ISagerNetServiceCallback, id: Int) {
|
||||
callbacks.register(cb)
|
||||
if (!callbackIdMap.contains(cb)) {
|
||||
callbacks.register(cb)
|
||||
}
|
||||
callbackIdMap[cb] = id
|
||||
}
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@ import io.nekohasekai.sagernet.database.DataStore
|
||||
import io.nekohasekai.sagernet.ktx.runOnMainDispatcher
|
||||
|
||||
class SagerConnection(
|
||||
private val connectionId: Int,
|
||||
private var connectionId: Int,
|
||||
private var listenForDeath: Boolean = false
|
||||
) : ServiceConnection, IBinder.DeathRecipient {
|
||||
|
||||
@ -28,9 +28,10 @@ class SagerConnection(
|
||||
else -> throw UnknownError()
|
||||
}.java
|
||||
|
||||
val CONNECTION_ID_SHORTCUT = 0
|
||||
val CONNECTION_ID_TILE = 1
|
||||
val CONNECTION_ID_MAINACTIVITY = 2
|
||||
const val CONNECTION_ID_SHORTCUT = 0
|
||||
const val CONNECTION_ID_TILE = 1
|
||||
const val CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND = 2
|
||||
const val CONNECTION_ID_MAIN_ACTIVITY_BACKGROUND = 3
|
||||
}
|
||||
|
||||
interface Callback {
|
||||
@ -102,6 +103,11 @@ class SagerConnection(
|
||||
|
||||
var service: ISagerNetService? = null
|
||||
|
||||
fun updateConnectionId(id: Int) {
|
||||
connectionId = id
|
||||
service?.registerCallback(serviceCallback, id)
|
||||
}
|
||||
|
||||
override fun onServiceConnected(name: ComponentName?, binder: IBinder) {
|
||||
this.binder = binder
|
||||
val service = ISagerNetService.Stub.asInterface(binder)!!
|
||||
|
||||
@ -56,10 +56,16 @@ class TrafficLooper
|
||||
Logs.d("select traffic count $TAG_PROXY to $id, old id is $selectorNowId")
|
||||
val oldData = items[selectorNowId]
|
||||
val data = items[id] ?: return
|
||||
oldData?.tag = selectorNowFakeTag
|
||||
oldData?.apply {
|
||||
tag = selectorNowFakeTag
|
||||
ignore = true
|
||||
}
|
||||
selectorNowFakeTag = data.tag
|
||||
data.tag = TAG_PROXY
|
||||
selectorNowId = id
|
||||
data.apply {
|
||||
tag = TAG_PROXY
|
||||
ignore = false
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun loop() {
|
||||
@ -94,6 +100,7 @@ class TrafficLooper
|
||||
tag = tag,
|
||||
rx = ent.rx,
|
||||
tx = ent.tx,
|
||||
ignore = proxy.config.selectorGroupId >= 0L,
|
||||
)
|
||||
if (tag == TAG_PROXY && itemMain == null) {
|
||||
itemMain = item
|
||||
@ -135,14 +142,18 @@ class TrafficLooper
|
||||
)
|
||||
|
||||
// broadcast (MainActivity)
|
||||
data.binder.broadcast { b ->
|
||||
if (data.binder.callbackIdMap[b] == SagerConnection.CONNECTION_ID_MAINACTIVITY) {
|
||||
b.cbSpeedUpdate(speed)
|
||||
if (profileTrafficStatistics) {
|
||||
items.forEach { (id, item) ->
|
||||
b.cbTrafficUpdate(
|
||||
TrafficData(id = id, rx = item.rx, tx = item.tx) // display
|
||||
)
|
||||
if (data.state == BaseService.State.Connected
|
||||
&& data.binder.callbackIdMap.containsValue(SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND)
|
||||
) {
|
||||
data.binder.broadcast { b ->
|
||||
if (data.binder.callbackIdMap[b] == SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND) {
|
||||
b.cbSpeedUpdate(speed)
|
||||
if (profileTrafficStatistics) {
|
||||
items.forEach { (id, item) ->
|
||||
b.cbTrafficUpdate(
|
||||
TrafficData(id = id, rx = item.rx, tx = item.tx) // display
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,9 +13,10 @@ class TrafficUpdater(
|
||||
var txRate: Long = 0,
|
||||
var rxRate: Long = 0,
|
||||
var lastUpdate: Long = 0,
|
||||
var ignore: Boolean = false,
|
||||
)
|
||||
|
||||
private suspend fun updateOne(item: TrafficLooperData): TrafficLooperData {
|
||||
private fun updateOne(item: TrafficLooperData): TrafficLooperData {
|
||||
// last update
|
||||
val now = System.currentTimeMillis()
|
||||
val interval = now - item.lastUpdate
|
||||
@ -48,6 +49,7 @@ class TrafficUpdater(
|
||||
suspend fun updateAll() {
|
||||
val updated = mutableMapOf<String, TrafficLooperData>() // diffs
|
||||
items.forEach { item ->
|
||||
if (item.ignore) return@forEach
|
||||
var diff = updated[item.tag]
|
||||
// query a tag only once
|
||||
if (diff == null) {
|
||||
|
||||
@ -381,7 +381,7 @@ class MainActivity : ThemedActivity(),
|
||||
}
|
||||
}
|
||||
|
||||
val connection = SagerConnection(SagerConnection.CONNECTION_ID_MAINACTIVITY, true)
|
||||
val connection = SagerConnection(SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND, true)
|
||||
override fun onServiceConnected(service: ISagerNetService) = changeState(
|
||||
try {
|
||||
BaseService.State.values()[service.state]
|
||||
@ -426,10 +426,12 @@ class MainActivity : ThemedActivity(),
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
connection.updateConnectionId(SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND)
|
||||
super.onStart()
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
connection.updateConnectionId(SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_BACKGROUND)
|
||||
super.onStop()
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user