mirror of
https://github.com/yggdrasil-network/yggdrasil-android.git
synced 2025-04-27 21:55:08 +03:00
Made fast reconnect when network becomes up. (#28)
* Made fast reconnect when network becomes up. * Use `retryPeersNow`
This commit is contained in:
parent
41569a9ee2
commit
c9476a7b00
5 changed files with 58 additions and 3 deletions
|
@ -11,8 +11,8 @@ android {
|
||||||
applicationId "eu.neilalexander.yggdrasil"
|
applicationId "eu.neilalexander.yggdrasil"
|
||||||
minSdkVersion 21
|
minSdkVersion 21
|
||||||
targetSdkVersion 29
|
targetSdkVersion 29
|
||||||
versionCode 9
|
versionCode 10
|
||||||
versionName "0.1"
|
versionName "0.1-010"
|
||||||
|
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
package="eu.neilalexander.yggdrasil">
|
package="eu.neilalexander.yggdrasil">
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
|
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:name=".GlobalApplication"
|
android:name=".GlobalApplication"
|
||||||
|
|
|
@ -9,6 +9,7 @@ class GlobalApplication: Application() {
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
config = ConfigurationProxy(applicationContext)
|
config = ConfigurationProxy(applicationContext)
|
||||||
|
val callback = NetworkStateCallback(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun subscribe() {
|
fun subscribe() {
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
package eu.neilalexander.yggdrasil
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.*
|
||||||
|
import android.util.Log
|
||||||
|
|
||||||
|
|
||||||
|
private const val TAG = "Network"
|
||||||
|
|
||||||
|
class NetworkStateCallback(val context: Context) : ConnectivityManager.NetworkCallback() {
|
||||||
|
|
||||||
|
init {
|
||||||
|
val request = NetworkRequest.Builder()
|
||||||
|
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
|
||||||
|
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
|
||||||
|
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||||
|
manager.registerNetworkCallback(request, this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onAvailable(network: Network) {
|
||||||
|
super.onAvailable(network)
|
||||||
|
Log.d(TAG, "onAvailable")
|
||||||
|
|
||||||
|
Thread {
|
||||||
|
// The message often arrives before the connection is fully established
|
||||||
|
Thread.sleep(1000)
|
||||||
|
val intent = Intent(context, PacketTunnelProvider::class.java)
|
||||||
|
intent.action = PacketTunnelProvider.ACTION_CONNECT
|
||||||
|
context.startService(intent)
|
||||||
|
}.start()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onLost(network: Network) {
|
||||||
|
super.onLost(network)
|
||||||
|
Log.d(TAG, "onLost")
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,6 +21,7 @@ class PacketTunnelProvider: VpnService() {
|
||||||
|
|
||||||
const val ACTION_START = "eu.neilalexander.yggdrasil.PacketTunnelProvider.START"
|
const val ACTION_START = "eu.neilalexander.yggdrasil.PacketTunnelProvider.START"
|
||||||
const val ACTION_STOP = "eu.neilalexander.yggdrasil.PacketTunnelProvider.STOP"
|
const val ACTION_STOP = "eu.neilalexander.yggdrasil.PacketTunnelProvider.STOP"
|
||||||
|
const val ACTION_CONNECT = "eu.neilalexander.yggdrasil.PacketTunnelProvider.CONNECT"
|
||||||
}
|
}
|
||||||
|
|
||||||
private var yggdrasil = Yggdrasil()
|
private var yggdrasil = Yggdrasil()
|
||||||
|
@ -56,6 +57,10 @@ class PacketTunnelProvider: VpnService() {
|
||||||
Log.d(TAG, "Stopping...")
|
Log.d(TAG, "Stopping...")
|
||||||
stop(); START_NOT_STICKY
|
stop(); START_NOT_STICKY
|
||||||
}
|
}
|
||||||
|
ACTION_CONNECT -> {
|
||||||
|
Log.d(TAG, "Connecting...")
|
||||||
|
connect(); START_STICKY
|
||||||
|
}
|
||||||
else -> {
|
else -> {
|
||||||
Log.d(TAG, "Starting...")
|
Log.d(TAG, "Starting...")
|
||||||
start(); START_STICKY
|
start(); START_STICKY
|
||||||
|
@ -181,6 +186,13 @@ class PacketTunnelProvider: VpnService() {
|
||||||
stopSelf()
|
stopSelf()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun connect() {
|
||||||
|
if (!started.get()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
yggdrasil.retryPeersNow()
|
||||||
|
}
|
||||||
|
|
||||||
private fun updater() {
|
private fun updater() {
|
||||||
updates@ while (started.get()) {
|
updates@ while (started.get()) {
|
||||||
if ((application as GlobalApplication).needUiUpdates()) {
|
if ((application as GlobalApplication).needUiUpdates()) {
|
||||||
|
@ -260,4 +272,4 @@ class PacketTunnelProvider: VpnService() {
|
||||||
readerStream = null
|
readerStream = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue