Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #25 from vikulin/master
Browse files Browse the repository at this point in the history
Fixed bug with checkmark clean up while adding new peers in list
  • Loading branch information
vikulin authored Oct 17, 2020
2 parents 34d8113 + 8e0d575 commit 7294166
Show file tree
Hide file tree
Showing 17 changed files with 362 additions and 67 deletions.
16 changes: 16 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
compileSdkVersion 30
defaultConfig {
applicationId "io.github.chronosx88.yggdrasil"
minSdkVersion 15
targetSdkVersion 29
versionCode 4
versionName "1.4"
targetSdkVersion 30
versionCode 5
versionName "1.5"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
project.ext.set("archivesBaseName", project.getParent().name+"-"+versionName)
setProperty("archivesBaseName", project.getParent().name+"-"+versionName)
}
signingConfigs {
release {
Expand Down Expand Up @@ -58,8 +58,8 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(path: ':yggdrasil')

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation "androidx.preference:preference-ktx:1.1.1"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

Expand All @@ -69,6 +69,6 @@ dependencies {
implementation 'com.hbb20:ccp:2.4.0'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".AboutActivity"
android:parentActivityName=".MainActivity"
android:label="@string/title_activity_about"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait"/>
<activity
android:name=".PeerListActivity"
android:parentActivityName=".MainActivity"
Expand Down
205 changes: 205 additions & 0 deletions app/src/main/java/io/github/chronosx88/yggdrasil/AboutActivity.kt

Large diffs are not rendered by default.

75 changes: 52 additions & 23 deletions app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package io.github.chronosx88.yggdrasil
import android.app.Activity
import android.app.ActivityManager
import android.content.*
import android.net.ConnectivityManager
import android.net.Network
import android.net.VpnService
import android.os.Build
import android.os.Bundle
Expand All @@ -13,12 +11,12 @@ import android.view.Gravity
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SwitchCompat
import androidx.preference.PreferenceManager
import dalvik.system.DexFile
import io.github.chronosx88.yggdrasil.models.DNSInfo
import io.github.chronosx88.yggdrasil.models.PeerInfo
import io.github.chronosx88.yggdrasil.models.config.DNSInfoListAdapter
import io.github.chronosx88.yggdrasil.models.config.NetworkUtils
import io.github.chronosx88.yggdrasil.models.config.PeerInfoListAdapter
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.deserializePeerStringList2PeerInfoSet
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.deserializeStringList2DNSInfoSet
Expand Down Expand Up @@ -57,12 +55,16 @@ class MainActivity : AppCompatActivity() {
const val CURRENT_PEERS = "CURRENT_PEERS_v1.2.1"
const val CURRENT_DNS = "CURRENT_DNS_v1.2"
const val START_VPN = "START_VPN"
private const val TAG="Yggdrasil"
private const val TAG = "Yggdrasil"
private const val VPN_REQUEST_CODE = 0x0F

@JvmStatic var isStarted = false
@JvmStatic var isCancelled = false
@JvmStatic var address:String? = ""
@JvmStatic
var isStarted = false
@JvmStatic
var isCancelled = false
@JvmStatic
var address: String? = ""

}

private var currentPeers = setOf<PeerInfo>()
Expand All @@ -74,7 +76,7 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar))
isStarted = isYggServiceRunning(this)
val switchOn = findViewById<Switch>(R.id.switchOn)
val switchOn = findViewById<SwitchCompat>(R.id.switchOn)
switchOn.isChecked = isStarted

switchOn.setOnCheckedChangeListener { _, isChecked ->
Expand All @@ -92,12 +94,17 @@ class MainActivity : AppCompatActivity() {
//save to shared preferences
val preferences =
PreferenceManager.getDefaultSharedPreferences(this.baseContext)
val staticIP = findViewById<Switch>(R.id.staticIP)
val staticIP = findViewById<SwitchCompat>(R.id.staticIP)
staticIP.isChecked =
preferences.getString(STATIC_IP, null) != null
val peersListView = findViewById<ListView>(R.id.peers)

currentPeers = deserializeStringSet2PeerInfoSet(preferences.getStringSet(CURRENT_PEERS, HashSet())!!)
currentPeers = deserializeStringSet2PeerInfoSet(
preferences.getStringSet(
CURRENT_PEERS,
HashSet()
)!!
)
val adapter = PeerInfoListAdapter(this, currentPeers.sortedWith(compareBy { it.ping }))
peersListView.adapter = adapter
if(isStarted && this.currentPeers.isEmpty()) {
Expand Down Expand Up @@ -125,7 +132,12 @@ class MainActivity : AppCompatActivity() {
}

val listViewDNS = findViewById<ListView>(R.id.dns)
currentDNS = deserializeStringSet2DNSInfoSet(preferences.getStringSet(CURRENT_DNS, HashSet())!!)
currentDNS = deserializeStringSet2DNSInfoSet(
preferences.getStringSet(
CURRENT_DNS,
HashSet()
)!!
)
val adapterDns = DNSInfoListAdapter(this, currentDNS.sortedWith(compareBy { it.ping }))
listViewDNS.adapter = adapterDns
val editDnsButton = findViewById<Button>(R.id.editDNS)
Expand Down Expand Up @@ -193,10 +205,16 @@ class MainActivity : AppCompatActivity() {
val cl = classLoader
val c: Class<*> = dexFile.loadClass("dummy/Dummy", cl)
}
val versionName = findViewById<Button>(R.id.about)
versionName.text = """version:${BuildConfig.VERSION_NAME} build:${BuildConfig.VERSION_CODE}"""
versionName.setOnClickListener {
val intent = Intent(this@MainActivity, AboutActivity::class.java)
startActivity(intent)
}
}

private fun stopVpn(){
Log.i(TAG,"Stop")
Log.i(TAG, "Stop")
val intent = Intent(this, YggdrasilTunService::class.java)
val TASK_CODE = 100
val pi = createPendingResult(TASK_CODE, intent, 0)
Expand All @@ -206,7 +224,7 @@ class MainActivity : AppCompatActivity() {
}

private fun startVpn(){
Log.i(TAG,"Start")
Log.i(TAG, "Start")
val intent= VpnService.prepare(this)
if (intent!=null){
startActivityForResult(intent, VPN_REQUEST_CODE)
Expand All @@ -216,7 +234,7 @@ class MainActivity : AppCompatActivity() {
}

private fun updateDNS(){
Log.i(TAG,"Update DNS")
Log.i(TAG, "Update DNS")
val intent = Intent(this, YggdrasilTunService::class.java)
val TASK_CODE = 100
val pi = createPendingResult(TASK_CODE, intent, 0)
Expand All @@ -227,7 +245,7 @@ class MainActivity : AppCompatActivity() {
}

private fun updatePeers(){
Log.d(TAG,"Update Peers")
Log.d(TAG, "Update Peers")
val intent = Intent(this, YggdrasilTunService::class.java)
val TASK_CODE = 100
val pi = createPendingResult(TASK_CODE, intent, 0)
Expand Down Expand Up @@ -258,9 +276,13 @@ class MainActivity : AppCompatActivity() {
val pi = createPendingResult(TASK_CODE, intent, 0)
intent.putExtra(PARAM_PINTENT, pi)
intent.putExtra(COMMAND, START)
intent.putStringArrayListExtra(CURRENT_PEERS, serializePeerInfoSet2StringList(currentPeers))
intent.putStringArrayListExtra(
CURRENT_PEERS, serializePeerInfoSet2StringList(
currentPeers
)
)
intent.putStringArrayListExtra(CURRENT_DNS, serializeDNSInfoSet2StringList(currentDNS))
intent.putExtra(STATIC_IP, findViewById<Switch>(R.id.staticIP).isChecked)
intent.putExtra(STATIC_IP, findViewById<SwitchCompat>(R.id.staticIP).isChecked)

startService(intent)
}
Expand All @@ -272,7 +294,10 @@ class MainActivity : AppCompatActivity() {
var currentPeers = data.extras!!.getStringArrayList(PEER_LIST)
/*WiFi Direct test. need peer empty list*/
this.currentPeers = deserializeStringList2PeerInfoSet(currentPeers)
val adapter = PeerInfoListAdapter(this, this.currentPeers.sortedWith(compareBy { it.ping }))
val adapter = PeerInfoListAdapter(
this,
this.currentPeers.sortedWith(compareBy { it.ping })
)
val listView = findViewById<ListView>(R.id.peers)
listView.adapter = adapter

Expand All @@ -297,7 +322,10 @@ class MainActivity : AppCompatActivity() {
if(data!!.extras!=null){
var currentDNS = data.extras!!.getStringArrayList(DNS_LIST)
this.currentDNS = deserializeStringList2DNSInfoSet(currentDNS)
val adapter = DNSInfoListAdapter(this, this.currentDNS.sortedWith(compareBy { it.ping }))
val adapter = DNSInfoListAdapter(
this,
this.currentDNS.sortedWith(compareBy { it.ping })
)
val listView = findViewById<ListView>(R.id.dns)
listView.adapter = adapter
//save to shared preferences
Expand All @@ -313,7 +341,7 @@ class MainActivity : AppCompatActivity() {
when (resultCode) {
STATUS_START -> {
print("service started")
if(this.currentPeers.isEmpty()){
if (this.currentPeers.isEmpty()) {
checkPeers()
}
}
Expand All @@ -329,16 +357,17 @@ class MainActivity : AppCompatActivity() {
val ipLayout = findViewById<LinearLayout>(R.id.ipLayout)
ipLayout.visibility = View.GONE
}
STATUS_PEERS_UPDATE ->{
if(data!!.extras!=null) {
STATUS_PEERS_UPDATE -> {
if (data!!.extras != null) {
thread(start = true) {
val meshPeers = deserializePeerStringList2PeerInfoSet(
data.extras!!.getStringArrayList(MESH_PEERS)
)
val listView = findViewById<ListView>(R.id.peers)
val adapter = PeerInfoListAdapter(
this@MainActivity,
meshPeers.filter { it.schema!="self" }.sortedWith(compareBy { it.ping })
meshPeers.filter { it.schema != "self" }
.sortedWith(compareBy { it.ping })
)
runOnUiThread {
listView.adapter = adapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,6 @@ class PeerListActivity : AppCompatActivity() {
val result = Intent(this, MainActivity::class.java)
var adapter = findViewById<ListView>(R.id.peerList).adapter as SelectPeerInfoListAdapter
val selectedPeers = adapter.getSelectedPeers()
/* WiFi Direct test - no peers is needed
if(selectedPeers.size>0) {
result.putExtra(MainActivity.PEER_LIST, serializePeerInfoSet2StringList(selectedPeers))
setResult(Activity.RESULT_OK, result)
finish()
} else {
val text = "Select at least one peer"
val duration = Toast.LENGTH_SHORT
val toast = Toast.makeText(applicationContext, text, duration)
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()
}*/
result.putExtra(MainActivity.PEER_LIST, serializePeerInfoSet2StringList(selectedPeers))
setResult(Activity.RESULT_OK, result)
finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import mobile.Mobile
import mobile.Yggdrasil
import java.io.*
import java.net.Inet6Address
import kotlin.concurrent.thread

class YggdrasilTunService : VpnService() {

Expand All @@ -44,8 +45,6 @@ class YggdrasilTunService : VpnService() {
private const val TAG = "Yggdrasil-service"
}

private var scope: CoroutineScope? = null

private val FOREGROUND_ID = 1338

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Expand Down Expand Up @@ -124,15 +123,13 @@ class YggdrasilTunService : VpnService() {

setupIOStreams(dns)

val job = SupervisorJob()
scope = CoroutineScope(Dispatchers.Default + job)
scope!!.launch {
thread(start = true) {
val buffer = ByteArray(MAX_PACKET_SIZE)
while (!isClosed) {
readPacketsFromTun(yggConduitEndpoint, buffer)
}
}
scope!!.launch {
thread(start = true) {
while (!isClosed) {
writePacketsToTun(yggConduitEndpoint)
}
Expand Down Expand Up @@ -234,7 +231,6 @@ class YggdrasilTunService : VpnService() {

private fun stopVpn(pi: PendingIntent?) {
isClosed = true;
scope!!.coroutineContext.cancelChildren()
tunInputStream.close()
tunOutputStream.close()
tunInterface!!.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,13 @@ class SelectPeerInfoListAdapter(
peerInfoHolder.peerInfoText.setTextColor(Color.WHITE)
}
peerInfoHolder.checkbox.setOnCheckedChangeListener { _, isChecked ->
if(!isLoading) {
if (isChecked) {
if (!currentPeers.contains(currentPeer)) {
currentPeers.add(currentPeer)
}
} else {
if (currentPeers.contains(currentPeer)) {
currentPeers.remove(currentPeer)
}
if (isChecked) {
if (!currentPeers.contains(currentPeer)) {
currentPeers.add(currentPeer)
}
} else {
if (currentPeers.contains(currentPeer)) {
currentPeers.remove(currentPeer)
}
}
}
Expand Down
Loading

0 comments on commit 7294166

Please sign in to comment.