1. wifi direct first commit

This commit is contained in:
vadym 2020-06-28 08:23:18 -07:00
parent 5bb9ddc2de
commit 4b4ae6737a
3 changed files with 131 additions and 14 deletions

View File

@ -3,8 +3,11 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
package="io.github.chronosx88.yggdrasil"> package="io.github.chronosx88.yggdrasil">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:required="true" android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:required="true" android:name="android.permission.CHANGE_WIFI_STATE"/>
<application <application
android:allowBackup="true" android:allowBackup="true"

View File

@ -1,18 +1,20 @@
package io.github.chronosx88.yggdrasil package io.github.chronosx88.yggdrasil
import android.Manifest
import android.app.Activity import android.app.Activity
import android.app.ActivityManager import android.app.ActivityManager
import android.content.ClipData import android.content.*
import android.content.ClipboardManager import android.content.pm.PackageManager
import android.content.Context
import android.content.Intent
import android.net.VpnService import android.net.VpnService
import android.net.wifi.p2p.WifiP2pDevice
import android.net.wifi.p2p.WifiP2pManager
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.Gravity import android.view.Gravity
import android.view.View import android.view.View
import android.widget.* import android.widget.*
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import io.github.chronosx88.yggdrasil.models.DNSInfo import io.github.chronosx88.yggdrasil.models.DNSInfo
import io.github.chronosx88.yggdrasil.models.PeerInfo import io.github.chronosx88.yggdrasil.models.PeerInfo
@ -55,22 +57,30 @@ class MainActivity : AppCompatActivity() {
@JvmStatic var address = "" @JvmStatic var address = ""
} }
private var receiver: MainActivity.WiFiDirectBroadcastReceiver? = null
private var mChannel: WifiP2pManager.Channel? = null
private var mManager: WifiP2pManager? = null
private var currentPeers = setOf<PeerInfo>() private var currentPeers = setOf<PeerInfo>()
private var currentDNS = setOf<DNSInfo>() private var currentDNS = setOf<DNSInfo>()
private var startVpnFlag = false
private val intentFilter = IntentFilter()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar)) setSupportActionBar(findViewById(R.id.toolbar))
if(intent.extras!==null) { /*p2p part*/
startVpnFlag = intent.extras!!.getBoolean(START_VPN, false) // Indicates a change in the Wi-Fi P2P status.
isStarted = true intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
//startVpn() // Indicates a change in the list of available peers.
} else { intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
isStarted = isYggServiceRunning(this) // 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<Switch>(R.id.switchOn) val switchOn = findViewById<Switch>(R.id.switchOn)
switchOn.isChecked = isStarted switchOn.isChecked = isStarted
switchOn.setOnCheckedChangeListener { _, isChecked -> switchOn.setOnCheckedChangeListener { _, isChecked ->
@ -90,6 +100,43 @@ class MainActivity : AppCompatActivity() {
} }
} }
val wifiDirect = findViewById<Switch>(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<ListView>(R.id.peers) val peersListView = findViewById<ListView>(R.id.peers)
//save to shared preferences //save to shared preferences
val preferences = val preferences =
@ -279,4 +326,57 @@ class MainActivity : AppCompatActivity() {
} }
return false 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)
}
} }

View File

@ -22,13 +22,25 @@
android:id="@+id/toolbar" android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" android:layout_height="?attr/actionBarSize"
android:layout_marginEnd="70dp" android:layout_marginEnd="240dp"
android:background="?attr/colorPrimary" android:background="?attr/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:popupTheme="@style/AppTheme.PopupOverlay" /> app:popupTheme="@style/AppTheme.PopupOverlay" />
<Switch
android:id="@+id/wifiDirect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:theme="@style/SwitchTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/switchOn"
app:layout_constraintTop_toTopOf="parent"
android:text="Wifi-Direct"
android:textColor="@color/white"/>
<Switch <Switch
android:id="@+id/switchOn" android:id="@+id/switchOn"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -37,8 +49,10 @@
android:theme="@style/SwitchTheme" android:theme="@style/SwitchTheme"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Overlay"
android:textColor="@color/white"/>
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>