improve usage

This commit is contained in:
arm64v8a 2023-03-29 17:31:18 +09:00
parent d6b8a98b7d
commit d94e385df8
6 changed files with 40 additions and 32 deletions

View File

@ -38,9 +38,6 @@
android:required="false" /> android:required="false" />
<queries> <queries>
<intent>
<action android:name="com.github.shadowsocks.plugin.ACTION_NATIVE_PLUGIN" />
</intent>
<intent> <intent>
<action android:name="io.nekohasekai.sagernet.plugin.ACTION_NATIVE_PLUGIN" /> <action android:name="io.nekohasekai.sagernet.plugin.ACTION_NATIVE_PLUGIN" />
</intent> </intent>
@ -294,18 +291,6 @@
android:resource="@xml/cache_paths" /> android:resource="@xml/cache_paths" />
</provider> </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 <provider
android:name="androidx.startup.InitializationProvider" android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup" android:authorities="${applicationId}.androidx-startup"

View File

@ -80,7 +80,9 @@ class BaseService {
override fun getProfileName(): String = data?.proxy?.profile?.displayName() ?: "Idle" override fun getProfileName(): String = data?.proxy?.profile?.displayName() ?: "Idle"
override fun registerCallback(cb: ISagerNetServiceCallback, id: Int) { override fun registerCallback(cb: ISagerNetServiceCallback, id: Int) {
callbacks.register(cb) if (!callbackIdMap.contains(cb)) {
callbacks.register(cb)
}
callbackIdMap[cb] = id callbackIdMap[cb] = id
} }

View File

@ -16,7 +16,7 @@ import io.nekohasekai.sagernet.database.DataStore
import io.nekohasekai.sagernet.ktx.runOnMainDispatcher import io.nekohasekai.sagernet.ktx.runOnMainDispatcher
class SagerConnection( class SagerConnection(
private val connectionId: Int, private var connectionId: Int,
private var listenForDeath: Boolean = false private var listenForDeath: Boolean = false
) : ServiceConnection, IBinder.DeathRecipient { ) : ServiceConnection, IBinder.DeathRecipient {
@ -28,9 +28,10 @@ class SagerConnection(
else -> throw UnknownError() else -> throw UnknownError()
}.java }.java
val CONNECTION_ID_SHORTCUT = 0 const val CONNECTION_ID_SHORTCUT = 0
val CONNECTION_ID_TILE = 1 const val CONNECTION_ID_TILE = 1
val CONNECTION_ID_MAINACTIVITY = 2 const val CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND = 2
const val CONNECTION_ID_MAIN_ACTIVITY_BACKGROUND = 3
} }
interface Callback { interface Callback {
@ -102,6 +103,11 @@ class SagerConnection(
var service: ISagerNetService? = null var service: ISagerNetService? = null
fun updateConnectionId(id: Int) {
connectionId = id
service?.registerCallback(serviceCallback, id)
}
override fun onServiceConnected(name: ComponentName?, binder: IBinder) { override fun onServiceConnected(name: ComponentName?, binder: IBinder) {
this.binder = binder this.binder = binder
val service = ISagerNetService.Stub.asInterface(binder)!! val service = ISagerNetService.Stub.asInterface(binder)!!

View File

@ -56,10 +56,16 @@ class TrafficLooper
Logs.d("select traffic count $TAG_PROXY to $id, old id is $selectorNowId") Logs.d("select traffic count $TAG_PROXY to $id, old id is $selectorNowId")
val oldData = items[selectorNowId] val oldData = items[selectorNowId]
val data = items[id] ?: return val data = items[id] ?: return
oldData?.tag = selectorNowFakeTag oldData?.apply {
tag = selectorNowFakeTag
ignore = true
}
selectorNowFakeTag = data.tag selectorNowFakeTag = data.tag
data.tag = TAG_PROXY
selectorNowId = id selectorNowId = id
data.apply {
tag = TAG_PROXY
ignore = false
}
} }
private suspend fun loop() { private suspend fun loop() {
@ -94,6 +100,7 @@ class TrafficLooper
tag = tag, tag = tag,
rx = ent.rx, rx = ent.rx,
tx = ent.tx, tx = ent.tx,
ignore = proxy.config.selectorGroupId >= 0L,
) )
if (tag == TAG_PROXY && itemMain == null) { if (tag == TAG_PROXY && itemMain == null) {
itemMain = item itemMain = item
@ -135,14 +142,18 @@ class TrafficLooper
) )
// broadcast (MainActivity) // broadcast (MainActivity)
data.binder.broadcast { b -> if (data.state == BaseService.State.Connected
if (data.binder.callbackIdMap[b] == SagerConnection.CONNECTION_ID_MAINACTIVITY) { && data.binder.callbackIdMap.containsValue(SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND)
b.cbSpeedUpdate(speed) ) {
if (profileTrafficStatistics) { data.binder.broadcast { b ->
items.forEach { (id, item) -> if (data.binder.callbackIdMap[b] == SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND) {
b.cbTrafficUpdate( b.cbSpeedUpdate(speed)
TrafficData(id = id, rx = item.rx, tx = item.tx) // display if (profileTrafficStatistics) {
) items.forEach { (id, item) ->
b.cbTrafficUpdate(
TrafficData(id = id, rx = item.rx, tx = item.tx) // display
)
}
} }
} }
} }

View File

@ -13,9 +13,10 @@ class TrafficUpdater(
var txRate: Long = 0, var txRate: Long = 0,
var rxRate: Long = 0, var rxRate: Long = 0,
var lastUpdate: 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 // last update
val now = System.currentTimeMillis() val now = System.currentTimeMillis()
val interval = now - item.lastUpdate val interval = now - item.lastUpdate
@ -48,6 +49,7 @@ class TrafficUpdater(
suspend fun updateAll() { suspend fun updateAll() {
val updated = mutableMapOf<String, TrafficLooperData>() // diffs val updated = mutableMapOf<String, TrafficLooperData>() // diffs
items.forEach { item -> items.forEach { item ->
if (item.ignore) return@forEach
var diff = updated[item.tag] var diff = updated[item.tag]
// query a tag only once // query a tag only once
if (diff == null) { if (diff == null) {

View File

@ -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( override fun onServiceConnected(service: ISagerNetService) = changeState(
try { try {
BaseService.State.values()[service.state] BaseService.State.values()[service.state]
@ -426,10 +426,12 @@ class MainActivity : ThemedActivity(),
} }
override fun onStart() { override fun onStart() {
connection.updateConnectionId(SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND)
super.onStart() super.onStart()
} }
override fun onStop() { override fun onStop() {
connection.updateConnectionId(SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_BACKGROUND)
super.onStop() super.onStop()
} }