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