-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add official interface callback
copy from sing-box-for-android Closes #19 refactor: use defaultNetwork instead of underlying network Signed-off-by: HystericalDragon <HystericalDragons@proton.me> fix #33 Signed-off-by: HystericalDragon <HystericalDragons@proton.me>
- Loading branch information
1 parent
36e5a15
commit de7be95
Showing
15 changed files
with
453 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
app/src/main/java/io/nekohasekai/sagernet/bg/DefaultNetworkMonitor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.nekohasekai.sagernet.bg | ||
|
||
import android.net.Network | ||
import android.os.Build | ||
import libcore.InterfaceUpdateListener | ||
import io.nekohasekai.sagernet.SagerNet | ||
import java.net.NetworkInterface | ||
|
||
object DefaultNetworkMonitor { | ||
var defaultNetwork: Network? = null | ||
private var listener: InterfaceUpdateListener? = null | ||
|
||
suspend fun start() { | ||
DefaultNetworkListener.start(this) { | ||
defaultNetwork = it | ||
checkDefaultInterfaceUpdate(it) | ||
} | ||
defaultNetwork = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
SagerNet.connectivity.activeNetwork | ||
} else { | ||
DefaultNetworkListener.get() | ||
} | ||
} | ||
|
||
suspend fun stop() { | ||
DefaultNetworkListener.stop(this) | ||
} | ||
|
||
fun setListener(listener: InterfaceUpdateListener?) { | ||
this.listener = listener | ||
checkDefaultInterfaceUpdate(defaultNetwork) | ||
} | ||
|
||
private fun checkDefaultInterfaceUpdate( | ||
newNetwork: Network? | ||
) { | ||
val listener = listener ?: return | ||
if (newNetwork != null) { | ||
val interfaceName = | ||
(SagerNet.connectivity.getLinkProperties(newNetwork) ?: return).interfaceName | ||
for (times in 0 until 10) { | ||
var interfaceIndex: Int | ||
try { | ||
interfaceIndex = NetworkInterface.getByName(interfaceName).index | ||
} catch (e: Exception) { | ||
Thread.sleep(100) | ||
continue | ||
} | ||
listener.updateDefaultInterface(interfaceName, interfaceIndex) | ||
} | ||
} else { | ||
listener.updateDefaultInterface("", -1) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.