mirror of
https://github.com/yggdrasil-network/yggdrasil-android.git
synced 2025-04-27 21:55:08 +03:00
Build 8 (improve always-on VPN)
This commit is contained in:
parent
5887ae47b2
commit
d10bb853a6
4 changed files with 27 additions and 25 deletions
|
@ -11,7 +11,7 @@ android {
|
|||
applicationId "eu.neilalexander.yggdrasil"
|
||||
minSdkVersion 21
|
||||
targetSdkVersion 29
|
||||
versionCode 6
|
||||
versionCode 7
|
||||
versionName "0.1"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="eu.neilalexander.yggdrasil">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package eu.neilalexander.yggdrasil
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
|
@ -10,6 +11,7 @@ import android.os.Bundle
|
|||
import android.widget.Switch
|
||||
import android.widget.TableRow
|
||||
import android.widget.TextView
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import mobile.Mobile
|
||||
|
@ -27,6 +29,18 @@ class MainActivity : AppCompatActivity() {
|
|||
private lateinit var peersRow: TableRow
|
||||
private lateinit var settingsRow: TableRow
|
||||
|
||||
private fun start() {
|
||||
val intent = Intent(this, PacketTunnelProvider::class.java)
|
||||
intent.action = PacketTunnelProvider.ACTION_START
|
||||
startService(intent)
|
||||
}
|
||||
|
||||
private var startVpnActivity = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
start()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
@ -44,16 +58,14 @@ class MainActivity : AppCompatActivity() {
|
|||
|
||||
enabledLabel.setTextColor(Color.GRAY)
|
||||
|
||||
VpnService.prepare(this)
|
||||
|
||||
enabledSwitch.setOnCheckedChangeListener { _, isChecked ->
|
||||
when (isChecked) {
|
||||
true -> {
|
||||
val vpnintent = VpnService.prepare(this)
|
||||
if (vpnintent != null) {
|
||||
startActivityForResult(vpnintent, 0)
|
||||
val vpnIntent = VpnService.prepare(this)
|
||||
if (vpnIntent != null) {
|
||||
startVpnActivity.launch(vpnIntent)
|
||||
} else {
|
||||
onActivityResult(0, RESULT_OK, vpnintent)
|
||||
start()
|
||||
}
|
||||
}
|
||||
false -> {
|
||||
|
@ -89,6 +101,7 @@ class MainActivity : AppCompatActivity() {
|
|||
when (intent.getStringExtra("type")) {
|
||||
"state" -> {
|
||||
enabledLabel.text = if (intent.getBooleanExtra("started", false)) {
|
||||
enabledSwitch.isChecked = true
|
||||
if (state.dhtCount() == 0) {
|
||||
enabledLabel.setTextColor(Color.RED)
|
||||
"No connectivity"
|
||||
|
@ -97,6 +110,7 @@ class MainActivity : AppCompatActivity() {
|
|||
"Enabled"
|
||||
}
|
||||
} else {
|
||||
enabledSwitch.isChecked = false
|
||||
enabledLabel.setTextColor(Color.GRAY)
|
||||
"Not enabled"
|
||||
}
|
||||
|
@ -117,15 +131,4 @@ class MainActivity : AppCompatActivity() {
|
|||
super.onPause()
|
||||
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver)
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
when (resultCode) {
|
||||
RESULT_OK -> {
|
||||
val intent = Intent(this, PacketTunnelProvider::class.java)
|
||||
intent.action = PacketTunnelProvider.ACTION_START
|
||||
startService(intent)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
package eu.neilalexander.yggdrasil
|
||||
|
||||
import android.content.Intent
|
||||
import android.content.*
|
||||
import android.net.Uri
|
||||
import android.net.VpnService
|
||||
import android.os.Handler
|
||||
|
@ -50,17 +50,17 @@ class PacketTunnelProvider: VpnService() {
|
|||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
if (intent == null) {
|
||||
Log.d("PacketTunnelProvider", "Intent is null")
|
||||
return START_NOT_STICKY
|
||||
}
|
||||
return when (intent.action ?: ACTION_STOP) {
|
||||
ACTION_START -> {
|
||||
start(); START_STICKY
|
||||
}
|
||||
ACTION_STOP -> {
|
||||
Log.d("PacketTunnelProvider", "Stopping...")
|
||||
stop(); START_NOT_STICKY
|
||||
}
|
||||
else -> {
|
||||
stop(); START_NOT_STICKY
|
||||
Log.d("PacketTunnelProvider", "Starting...")
|
||||
start(); START_STICKY
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -169,7 +169,7 @@ class PacketTunnelProvider: VpnService() {
|
|||
break@updates
|
||||
}
|
||||
try {
|
||||
Thread.sleep(2000)
|
||||
Thread.sleep(1000)
|
||||
} catch (e: java.lang.InterruptedException) {
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue