diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 102d634..9840d0f 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -3,8 +3,11 @@
xmlns:tools="http://schemas.android.com/tools"
package="io.github.chronosx88.yggdrasil">
+
+
+
()
private var currentDNS = setOf()
- private var startVpnFlag = false
+
+ private val intentFilter = IntentFilter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar))
- if(intent.extras!==null) {
- startVpnFlag = intent.extras!!.getBoolean(START_VPN, false)
- isStarted = true
- //startVpn()
- } else {
- isStarted = isYggServiceRunning(this)
- }
+ /*p2p part*/
+ // Indicates a change in the Wi-Fi P2P status.
+ intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
+ // Indicates a change in the list of available peers.
+ intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
+ // Indicates the state of Wi-Fi P2P connectivity has changed.
+ intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
+ // Indicates this device's details have changed.
+ intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
+
+ isStarted = isYggServiceRunning(this)
val switchOn = findViewById(R.id.switchOn)
switchOn.isChecked = isStarted
switchOn.setOnCheckedChangeListener { _, isChecked ->
@@ -90,6 +100,43 @@ class MainActivity : AppCompatActivity() {
}
}
+ val wifiDirect = findViewById(R.id.wifiDirect)
+ wifiDirect.setOnCheckedChangeListener { _, isChecked ->
+ mManager = getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager
+ mChannel = mManager!!.initialize(this, mainLooper, null);
+
+ if (ActivityCompat.checkSelfPermission(
+ this,
+ Manifest.permission.ACCESS_FINE_LOCATION
+ ) != PackageManager.PERMISSION_GRANTED
+ ) {
+ // TODO: Consider calling
+ // ActivityCompat#requestPermissions
+ // here to request the missing permissions, and then overriding
+ // public void onRequestPermissionsResult(int requestCode, String[] permissions,
+ // int[] grantResults)
+ // to handle the case where the user grants the permission. See the documentation
+ // for ActivityCompat#requestPermissions for more details.
+ return@setOnCheckedChangeListener
+ }
+ mManager!!.discoverPeers(mChannel, object : WifiP2pManager.ActionListener {
+ override fun onSuccess() {
+ // Code for when the discovery initiation is successful goes here.
+ // No services have actually been discovered yet, so this method
+ // can often be left blank. Code for peer discovery goes in the
+ // onReceive method, detailed below.
+ showToast("discover peers success")
+ }
+
+ override fun onFailure(reasonCode: Int) {
+ // Code for when the discovery initiation fails goes here.
+ // Alert the user that something went wrong.
+ showToast("discover peers failed, code="+reasonCode)
+ }
+ })
+
+ }
+
val peersListView = findViewById(R.id.peers)
//save to shared preferences
val preferences =
@@ -279,4 +326,57 @@ class MainActivity : AppCompatActivity() {
}
return false
}
+
+ private class WiFiDirectBroadcastReceiver(mManager: WifiP2pManager,
+ mChannel: WifiP2pManager.Channel,
+ activity: MainActivity): BroadcastReceiver() {
+ var activity = activity
+
+ override fun onReceive(context: Context?, intent: Intent?) {
+ val action = intent!!.action
+ if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION == action) {
+ // Determine if Wifi P2P mode is enabled or not, alert
+ // the Activity.
+ val state = intent!!.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1)
+ if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
+ activity.setIsWifiP2pEnabled(true)
+ } else {
+ activity.setIsWifiP2pEnabled(false)
+ }
+ } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION == action) {
+ // The peer list has changed! We should probably do something about
+ // that.
+ } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION == action) {
+
+ // Connection state changed! We should probably do something about
+ // that.
+ } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION == action) {
+ activity.updateThisDevice(
+ intent!!.getParcelableExtra(
+ WifiP2pManager.EXTRA_WIFI_P2P_DEVICE
+ ) as WifiP2pDevice
+ )
+ }
+ }
+ }
+
+ private fun updateThisDevice(wifiP2pDevice: WifiP2pDevice) {
+ showToast("update device:"+wifiP2pDevice.deviceName+" address:"+wifiP2pDevice.deviceAddress)
+ }
+
+ private fun setIsWifiP2pEnabled(b: Boolean) {
+ showToast("WifiP2pEnabled="+b)
+ }
+
+ /** register the BroadcastReceiver with the intent values to be matched */
+ override fun onResume() {
+ super.onResume()
+ receiver = WiFiDirectBroadcastReceiver(mManager!!, mChannel!!, this)
+ registerReceiver(receiver, intentFilter)
+ }
+
+ override fun onPause() {
+ super.onPause()
+ unregisterReceiver(receiver)
+ }
}
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index c804b25..dc8fd61 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -22,13 +22,25 @@
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
- android:layout_marginEnd="70dp"
+ android:layout_marginEnd="240dp"
android:background="?attr/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="@style/AppTheme.PopupOverlay" />
+
+
- app:layout_constraintTop_toTopOf="parent" />