diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt index 7337d2a..f10e9bc 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt @@ -14,6 +14,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.preference.PreferenceManager import com.google.gson.Gson import io.github.chronosx88.yggdrasil.models.PeerInfo +import io.github.chronosx88.yggdrasil.models.config.PeerInfoListAdapter import java.util.* import kotlin.collections.ArrayList import kotlin.collections.HashSet @@ -22,7 +23,6 @@ import kotlin.collections.HashSet class MainActivity : AppCompatActivity() { companion object { - const val COMMAND = "COMMAND" const val STOP = "STOP" const val START = "START" @@ -34,14 +34,34 @@ class MainActivity : AppCompatActivity() { const val PEERS: String = "PEERS" const val PEER_LIST_CODE = 1000 const val PEER_LIST = "PEERS_LIST" - const val CURRENT_PEERS = "CURRENT_PEERS" + const val CURRENT_PEERS = "CURRENT_PEER_INFO" const val START_VPN = "START_VPN" private const val TAG="Yggdrasil" private const val VPN_REQUEST_CODE = 0x0F + + @JvmStatic + fun deserializeStringList2PeerInfoList(list: ArrayList): ArrayList { + var gson = Gson() + var out = ArrayList() + for(s in list) { + out.add(gson.fromJson(s, PeerInfo::class.java)) + } + return out + } + + @JvmStatic + fun serializePeerInfoList2StringList(list: ArrayList): ArrayList { + var gson = Gson() + var out = ArrayList() + for(p in list) { + out.add(gson.toJson(p)) + } + return out + } } private var startVpnFlag = false - private var currentPeers = arrayListOf() + private var currentPeers = arrayListOf() private var isStarted = false override fun onCreate(savedInstanceState: Bundle?) { @@ -51,18 +71,14 @@ class MainActivity : AppCompatActivity() { //save to shared preferences val preferences = PreferenceManager.getDefaultSharedPreferences(this.baseContext) - currentPeers = ArrayList(preferences.getStringSet(CURRENT_PEERS, HashSet())!!) - if(currentPeers.size==0) { - currentPeers.add("tcp://194.177.21.156:5066") - currentPeers.add("tcp://46.151.26.194:60575") - currentPeers.add("tcp://188.226.125.64:54321") - } - val adapter = ArrayAdapter(this, R.layout.peers_list_item, currentPeers) + currentPeers = deserializeStringList2PeerInfoList(ArrayList(preferences.getStringSet(CURRENT_PEERS, HashSet())!!)) + + val adapter = PeerInfoListAdapter(this, currentPeers) listView.adapter = adapter - val editBeersButton = findViewById