mirror of
https://github.com/yggdrasil-network/crispa-android.git
synced 2025-01-22 16:06:30 +00:00
1. added duplication peer records fix
This commit is contained in:
parent
68b294df14
commit
8faf0c67ab
@ -40,9 +40,9 @@ class MainActivity : AppCompatActivity() {
|
|||||||
private const val VPN_REQUEST_CODE = 0x0F
|
private const val VPN_REQUEST_CODE = 0x0F
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun deserializeStringList2PeerInfoList(list: ArrayList<String>): ArrayList<PeerInfo> {
|
fun deserializeStringList2PeerInfoSet(list: List<String>): MutableSet<PeerInfo> {
|
||||||
var gson = Gson()
|
var gson = Gson()
|
||||||
var out = ArrayList<PeerInfo>()
|
var out = mutableSetOf<PeerInfo>()
|
||||||
for(s in list) {
|
for(s in list) {
|
||||||
out.add(gson.fromJson(s, PeerInfo::class.java))
|
out.add(gson.fromJson(s, PeerInfo::class.java))
|
||||||
}
|
}
|
||||||
@ -50,7 +50,17 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@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 gson = Gson()
|
||||||
var out = ArrayList<String>()
|
var out = ArrayList<String>()
|
||||||
for(p in list) {
|
for(p in list) {
|
||||||
@ -61,7 +71,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private var startVpnFlag = false
|
private var startVpnFlag = false
|
||||||
private var currentPeers = arrayListOf<PeerInfo>()
|
private var currentPeers = setOf<PeerInfo>()
|
||||||
private var isStarted = false
|
private var isStarted = false
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
@ -71,14 +81,14 @@ class MainActivity : AppCompatActivity() {
|
|||||||
//save to shared preferences
|
//save to shared preferences
|
||||||
val preferences =
|
val preferences =
|
||||||
PreferenceManager.getDefaultSharedPreferences(this.baseContext)
|
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
|
listView.adapter = adapter
|
||||||
val editPeersButton = findViewById<Button>(R.id.edit)
|
val editPeersButton = findViewById<Button>(R.id.edit)
|
||||||
editPeersButton.setOnClickListener {
|
editPeersButton.setOnClickListener {
|
||||||
val intent = Intent(this, PeerListActivity::class.java)
|
val intent = Intent(this, PeerListActivity::class.java)
|
||||||
intent.putStringArrayListExtra(PEER_LIST, serializePeerInfoList2StringList(currentPeers))
|
intent.putStringArrayListExtra(PEER_LIST, serializePeerInfoSet2StringList(currentPeers))
|
||||||
startActivityForResult(intent, PEER_LIST_CODE)
|
startActivityForResult(intent, PEER_LIST_CODE)
|
||||||
}
|
}
|
||||||
if(intent.extras!==null) {
|
if(intent.extras!==null) {
|
||||||
@ -120,7 +130,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
val pi = createPendingResult(TASK_CODE, intent, 0)
|
val pi = createPendingResult(TASK_CODE, intent, 0)
|
||||||
intent.putExtra(PARAM_PINTENT, pi)
|
intent.putExtra(PARAM_PINTENT, pi)
|
||||||
intent.putExtra(COMMAND, START)
|
intent.putExtra(COMMAND, START)
|
||||||
intent.putStringArrayListExtra(PEERS, serializePeerInfoList2StringList(currentPeers))
|
intent.putStringArrayListExtra(PEERS, serializePeerInfoSet2StringList(currentPeers))
|
||||||
startService(intent)
|
startService(intent)
|
||||||
}
|
}
|
||||||
if (requestCode == PEER_LIST_CODE && resultCode== Activity.RESULT_OK){
|
if (requestCode == PEER_LIST_CODE && resultCode== Activity.RESULT_OK){
|
||||||
@ -129,8 +139,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
if(currentPeers==null || currentPeers.size==0){
|
if(currentPeers==null || currentPeers.size==0){
|
||||||
showNoPeersSelected()
|
showNoPeersSelected()
|
||||||
} else {
|
} else {
|
||||||
this.currentPeers = deserializeStringList2PeerInfoList(currentPeers)
|
this.currentPeers = deserializeStringList2PeerInfoSet(currentPeers)
|
||||||
val adapter = PeerInfoListAdapter(this, this.currentPeers)
|
val adapter = PeerInfoListAdapter(this, ArrayList(this.currentPeers))
|
||||||
val listView = findViewById<ListView>(R.id.peers)
|
val listView = findViewById<ListView>(R.id.peers)
|
||||||
listView.adapter = adapter
|
listView.adapter = adapter
|
||||||
|
|
||||||
|
@ -156,24 +156,18 @@ class PeerListActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (extras != null) {
|
if (extras != null) {
|
||||||
var cp = MainActivity.deserializeStringList2PeerInfoList(
|
var cp = MainActivity.deserializeStringList2PeerInfoSet(
|
||||||
extras.getStringArrayList(MainActivity.PEER_LIST)!!
|
extras.getStringArrayList(MainActivity.PEER_LIST)!!
|
||||||
)
|
)
|
||||||
var currentPeers = ArrayList(cp)
|
var currentPeers = ArrayList(cp.sortedWith(compareBy { it.ping }))
|
||||||
for (peerInfo in allPeers) {
|
allPeers.removeAll(currentPeers)
|
||||||
if (currentPeers.contains(peerInfo)) {
|
allPeers.addAll(0, currentPeers)
|
||||||
currentPeers.remove(peerInfo)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (currentPeer in currentPeers) {
|
|
||||||
allPeers.add(0, currentPeer)
|
|
||||||
}
|
|
||||||
var adapter = SelectPeerInfoListAdapter(instance, allPeers, cp)
|
var adapter = SelectPeerInfoListAdapter(instance, allPeers, cp)
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
peerList.adapter = adapter
|
peerList.adapter = adapter
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var adapter = SelectPeerInfoListAdapter(instance, allPeers, ArrayList())
|
var adapter = SelectPeerInfoListAdapter(instance, allPeers, mutableSetOf())
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
peerList.adapter = adapter
|
peerList.adapter = adapter
|
||||||
}
|
}
|
||||||
@ -196,7 +190,7 @@ class PeerListActivity : AppCompatActivity() {
|
|||||||
var adapter = findViewById<ListView>(R.id.peerList).adapter as SelectPeerInfoListAdapter
|
var adapter = findViewById<ListView>(R.id.peerList).adapter as SelectPeerInfoListAdapter
|
||||||
val selectedPeers = adapter.getSelectedPeers()
|
val selectedPeers = adapter.getSelectedPeers()
|
||||||
if(selectedPeers.size>0) {
|
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)
|
setResult(Activity.RESULT_OK, result)
|
||||||
finish()
|
finish()
|
||||||
} else {
|
} else {
|
||||||
|
@ -30,8 +30,8 @@ class YggdrasilTunService : VpnService() {
|
|||||||
private const val TAG = "Yggdrasil-service"
|
private const val TAG = "Yggdrasil-service"
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun convertPeerInfoList2PeerIdList(list: ArrayList<PeerInfo>): ArrayList<String> {
|
fun convertPeerInfoSet2PeerIdSet(list: Set<PeerInfo>): Set<String> {
|
||||||
var out = ArrayList<String>()
|
var out = mutableSetOf<String>()
|
||||||
for(p in list) {
|
for(p in list) {
|
||||||
out.add(p.toString())
|
out.add(p.toString())
|
||||||
}
|
}
|
||||||
@ -52,7 +52,7 @@ class YggdrasilTunService : VpnService() {
|
|||||||
stopVpn(pi)
|
stopVpn(pi)
|
||||||
}
|
}
|
||||||
if (intent?.getStringExtra(MainActivity.COMMAND) == MainActivity.START) {
|
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)
|
val pi: PendingIntent = intent.getParcelableExtra(MainActivity.PARAM_PINTENT)
|
||||||
ygg = Yggdrasil()
|
ygg = Yggdrasil()
|
||||||
setupTunInterface(pi, peers)
|
setupTunInterface(pi, peers)
|
||||||
@ -61,7 +61,7 @@ class YggdrasilTunService : VpnService() {
|
|||||||
return super.onStartCommand(intent, flags, startId)
|
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)
|
pi.send(MainActivity.STATUS_START)
|
||||||
val builder = Builder()
|
val builder = Builder()
|
||||||
|
|
||||||
@ -102,13 +102,13 @@ class YggdrasilTunService : VpnService() {
|
|||||||
pi.send(this, MainActivity.STATUS_FINISH, intent)
|
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>()
|
val whiteList = arrayListOf<String>()
|
||||||
whiteList.add("")
|
whiteList.add("")
|
||||||
val blackList = arrayListOf<String>()
|
val blackList = arrayListOf<String>()
|
||||||
blackList.add("")
|
blackList.add("")
|
||||||
config["Peers"] = convertPeerInfoList2PeerIdList(peers)
|
config["Peers"] = convertPeerInfoSet2PeerIdSet(peers)
|
||||||
config["Listen"] = ""
|
config["Listen"] = ""
|
||||||
config["AdminListen"] = "tcp://localhost:9001"
|
config["AdminListen"] = "tcp://localhost:9001"
|
||||||
config["IfName"] = "tun0"
|
config["IfName"] = "tun0"
|
||||||
|
@ -11,18 +11,16 @@ import android.widget.ImageView
|
|||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import io.github.chronosx88.yggdrasil.R
|
import io.github.chronosx88.yggdrasil.R
|
||||||
import io.github.chronosx88.yggdrasil.models.PeerInfo
|
import io.github.chronosx88.yggdrasil.models.PeerInfo
|
||||||
import java.util.ArrayList
|
|
||||||
|
|
||||||
|
|
||||||
class SelectPeerInfoListAdapter(
|
class SelectPeerInfoListAdapter(
|
||||||
context: Context,
|
context: Context,
|
||||||
allPeers: List<PeerInfo>,
|
allPeers: List<PeerInfo>,
|
||||||
currentPeers: ArrayList<PeerInfo>
|
currentPeers: MutableSet<PeerInfo>
|
||||||
) : ArrayAdapter<PeerInfo?> (context, 0, allPeers) {
|
) : ArrayAdapter<PeerInfo?> (context, 0, allPeers) {
|
||||||
|
|
||||||
private val mContext: Context = context
|
private val mContext: Context = context
|
||||||
private var allPeers: List<PeerInfo> = allPeers
|
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 {
|
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
||||||
var peerInfoHolder = PeerInfoHolder()
|
var peerInfoHolder = PeerInfoHolder()
|
||||||
@ -64,7 +62,7 @@ class SelectPeerInfoListAdapter(
|
|||||||
return listItem!!
|
return listItem!!
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getSelectedPeers(): ArrayList<PeerInfo> {
|
fun getSelectedPeers(): Set<PeerInfo> {
|
||||||
return currentPeers
|
return currentPeers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,13 +4,18 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:orientation="horizontal"
|
android:orientation="horizontal"
|
||||||
android:gravity="center_vertical">
|
android:gravity="center_vertical">
|
||||||
|
<FrameLayout
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_marginStart="20dp"
|
||||||
|
android:paddingTop="3dp"
|
||||||
|
android:paddingBottom="3dp">
|
||||||
<ImageView
|
<ImageView
|
||||||
android:id="@+id/countryFlag"
|
android:id="@+id/countryFlag"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:layout_marginStart="18dp"
|
android:scaleType="fitCenter"/>
|
||||||
android:scaleX="0.7"
|
</FrameLayout>
|
||||||
android:scaleY="0.7"/>
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/peerInfoText"
|
android:id="@+id/peerInfoText"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user