1. added duplication peer records fix

This commit is contained in:
vadym 2020-06-21 23:31:55 -07:00
parent 68b294df14
commit 8faf0c67ab
5 changed files with 45 additions and 38 deletions

View File

@ -40,9 +40,9 @@ class MainActivity : AppCompatActivity() {
private const val VPN_REQUEST_CODE = 0x0F
@JvmStatic
fun deserializeStringList2PeerInfoList(list: ArrayList<String>): ArrayList<PeerInfo> {
fun deserializeStringList2PeerInfoSet(list: List<String>): MutableSet<PeerInfo> {
var gson = Gson()
var out = ArrayList<PeerInfo>()
var out = mutableSetOf<PeerInfo>()
for(s in list) {
out.add(gson.fromJson(s, PeerInfo::class.java))
}
@ -50,7 +50,17 @@ class MainActivity : AppCompatActivity() {
}
@JvmStatic
fun serializePeerInfoList2StringList(list: ArrayList<PeerInfo>): ArrayList<String> {
fun deserializeStringSet2PeerInfoSet(list: Set<String>): MutableSet<PeerInfo> {
var gson = Gson()
var out = mutableSetOf<PeerInfo>()
for(s in list) {
out.add(gson.fromJson(s, PeerInfo::class.java))
}
return out
}
@JvmStatic
fun serializePeerInfoSet2StringList(list: Set<PeerInfo>): ArrayList<String> {
var gson = Gson()
var out = ArrayList<String>()
for(p in list) {
@ -61,7 +71,7 @@ class MainActivity : AppCompatActivity() {
}
private var startVpnFlag = false
private var currentPeers = arrayListOf<PeerInfo>()
private var currentPeers = setOf<PeerInfo>()
private var isStarted = false
override fun onCreate(savedInstanceState: Bundle?) {
@ -71,14 +81,14 @@ class MainActivity : AppCompatActivity() {
//save to shared preferences
val preferences =
PreferenceManager.getDefaultSharedPreferences(this.baseContext)
currentPeers = deserializeStringList2PeerInfoList(ArrayList(preferences.getStringSet(CURRENT_PEERS, HashSet())!!))
currentPeers = deserializeStringSet2PeerInfoSet(preferences.getStringSet(CURRENT_PEERS, HashSet())!!)
val adapter = PeerInfoListAdapter(this, currentPeers)
val adapter = PeerInfoListAdapter(this, ArrayList(currentPeers))
listView.adapter = adapter
val editPeersButton = findViewById<Button>(R.id.edit)
editPeersButton.setOnClickListener {
val intent = Intent(this, PeerListActivity::class.java)
intent.putStringArrayListExtra(PEER_LIST, serializePeerInfoList2StringList(currentPeers))
intent.putStringArrayListExtra(PEER_LIST, serializePeerInfoSet2StringList(currentPeers))
startActivityForResult(intent, PEER_LIST_CODE)
}
if(intent.extras!==null) {
@ -120,7 +130,7 @@ class MainActivity : AppCompatActivity() {
val pi = createPendingResult(TASK_CODE, intent, 0)
intent.putExtra(PARAM_PINTENT, pi)
intent.putExtra(COMMAND, START)
intent.putStringArrayListExtra(PEERS, serializePeerInfoList2StringList(currentPeers))
intent.putStringArrayListExtra(PEERS, serializePeerInfoSet2StringList(currentPeers))
startService(intent)
}
if (requestCode == PEER_LIST_CODE && resultCode== Activity.RESULT_OK){
@ -129,8 +139,8 @@ class MainActivity : AppCompatActivity() {
if(currentPeers==null || currentPeers.size==0){
showNoPeersSelected()
} else {
this.currentPeers = deserializeStringList2PeerInfoList(currentPeers)
val adapter = PeerInfoListAdapter(this, this.currentPeers)
this.currentPeers = deserializeStringList2PeerInfoSet(currentPeers)
val adapter = PeerInfoListAdapter(this, ArrayList(this.currentPeers))
val listView = findViewById<ListView>(R.id.peers)
listView.adapter = adapter

View File

@ -156,24 +156,18 @@ class PeerListActivity : AppCompatActivity() {
}
if (extras != null) {
var cp = MainActivity.deserializeStringList2PeerInfoList(
var cp = MainActivity.deserializeStringList2PeerInfoSet(
extras.getStringArrayList(MainActivity.PEER_LIST)!!
)
var currentPeers = ArrayList(cp)
for (peerInfo in allPeers) {
if (currentPeers.contains(peerInfo)) {
currentPeers.remove(peerInfo)
}
}
for (currentPeer in currentPeers) {
allPeers.add(0, currentPeer)
}
var currentPeers = ArrayList(cp.sortedWith(compareBy { it.ping }))
allPeers.removeAll(currentPeers)
allPeers.addAll(0, currentPeers)
var adapter = SelectPeerInfoListAdapter(instance, allPeers, cp)
withContext(Dispatchers.Main) {
peerList.adapter = adapter
}
} else {
var adapter = SelectPeerInfoListAdapter(instance, allPeers, ArrayList())
var adapter = SelectPeerInfoListAdapter(instance, allPeers, mutableSetOf())
withContext(Dispatchers.Main) {
peerList.adapter = adapter
}
@ -196,7 +190,7 @@ class PeerListActivity : AppCompatActivity() {
var adapter = findViewById<ListView>(R.id.peerList).adapter as SelectPeerInfoListAdapter
val selectedPeers = adapter.getSelectedPeers()
if(selectedPeers.size>0) {
result.putExtra(MainActivity.PEER_LIST, MainActivity.serializePeerInfoList2StringList(adapter.getSelectedPeers()))
result.putExtra(MainActivity.PEER_LIST, MainActivity.serializePeerInfoSet2StringList(adapter.getSelectedPeers()))
setResult(Activity.RESULT_OK, result)
finish()
} else {

View File

@ -30,8 +30,8 @@ class YggdrasilTunService : VpnService() {
private const val TAG = "Yggdrasil-service"
@JvmStatic
fun convertPeerInfoList2PeerIdList(list: ArrayList<PeerInfo>): ArrayList<String> {
var out = ArrayList<String>()
fun convertPeerInfoSet2PeerIdSet(list: Set<PeerInfo>): Set<String> {
var out = mutableSetOf<String>()
for(p in list) {
out.add(p.toString())
}
@ -52,7 +52,7 @@ class YggdrasilTunService : VpnService() {
stopVpn(pi)
}
if (intent?.getStringExtra(MainActivity.COMMAND) == MainActivity.START) {
val peers = MainActivity.deserializeStringList2PeerInfoList(intent.getStringArrayListExtra(MainActivity.PEERS))
val peers = MainActivity.deserializeStringList2PeerInfoSet(intent.getStringArrayListExtra(MainActivity.PEERS))
val pi: PendingIntent = intent.getParcelableExtra(MainActivity.PARAM_PINTENT)
ygg = Yggdrasil()
setupTunInterface(pi, peers)
@ -61,7 +61,7 @@ class YggdrasilTunService : VpnService() {
return super.onStartCommand(intent, flags, startId)
}
private fun setupTunInterface(pi: PendingIntent, peers: ArrayList<PeerInfo>) {
private fun setupTunInterface(pi: PendingIntent, peers: Set<PeerInfo>) {
pi.send(MainActivity.STATUS_START)
val builder = Builder()
@ -102,13 +102,13 @@ class YggdrasilTunService : VpnService() {
pi.send(this, MainActivity.STATUS_FINISH, intent)
}
private fun fixConfig(config: MutableMap<Any?, Any?>, peers: ArrayList<PeerInfo>): MutableMap<Any?, Any?> {
private fun fixConfig(config: MutableMap<Any?, Any?>, peers: Set<PeerInfo>): MutableMap<Any?, Any?> {
val whiteList = arrayListOf<String>()
whiteList.add("")
val blackList = arrayListOf<String>()
blackList.add("")
config["Peers"] = convertPeerInfoList2PeerIdList(peers)
config["Peers"] = convertPeerInfoSet2PeerIdSet(peers)
config["Listen"] = ""
config["AdminListen"] = "tcp://localhost:9001"
config["IfName"] = "tun0"

View File

@ -11,18 +11,16 @@ import android.widget.ImageView
import android.widget.TextView
import io.github.chronosx88.yggdrasil.R
import io.github.chronosx88.yggdrasil.models.PeerInfo
import java.util.ArrayList
class SelectPeerInfoListAdapter(
context: Context,
allPeers: List<PeerInfo>,
currentPeers: ArrayList<PeerInfo>
currentPeers: MutableSet<PeerInfo>
) : ArrayAdapter<PeerInfo?> (context, 0, allPeers) {
private val mContext: Context = context
private var allPeers: List<PeerInfo> = allPeers
private var currentPeers: ArrayList<PeerInfo> = currentPeers
private var currentPeers: MutableSet<PeerInfo> = currentPeers
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var peerInfoHolder = PeerInfoHolder()
@ -64,7 +62,7 @@ class SelectPeerInfoListAdapter(
return listItem!!
}
fun getSelectedPeers(): ArrayList<PeerInfo> {
fun getSelectedPeers(): Set<PeerInfo> {
return currentPeers
}

View File

@ -4,13 +4,18 @@
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:id="@+id/countryFlag"
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="18dp"
android:scaleX="0.7"
android:scaleY="0.7"/>
android:layout_marginStart="20dp"
android:paddingTop="3dp"
android:paddingBottom="3dp">
<ImageView
android:id="@+id/countryFlag"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitCenter"/>
</FrameLayout>
<TextView
android:id="@+id/peerInfoText"
android:layout_width="match_parent"