mirror of
https://github.com/yggdrasil-network/crispa-android.git
synced 2024-11-09 20:11:01 +00:00
1. NPE crash fix
This commit is contained in:
parent
4b4ae6737a
commit
c0a779ce3c
@ -7,6 +7,7 @@ import android.content.*
|
|||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.net.VpnService
|
import android.net.VpnService
|
||||||
import android.net.wifi.p2p.WifiP2pDevice
|
import android.net.wifi.p2p.WifiP2pDevice
|
||||||
|
import android.net.wifi.p2p.WifiP2pDeviceList
|
||||||
import android.net.wifi.p2p.WifiP2pManager
|
import android.net.wifi.p2p.WifiP2pManager
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
@ -63,8 +64,11 @@ class MainActivity : AppCompatActivity() {
|
|||||||
private var currentPeers = setOf<PeerInfo>()
|
private var currentPeers = setOf<PeerInfo>()
|
||||||
private var currentDNS = setOf<DNSInfo>()
|
private var currentDNS = setOf<DNSInfo>()
|
||||||
|
|
||||||
|
private val wirelessPeers = mutableListOf<WifiP2pDevice>()
|
||||||
private val intentFilter = IntentFilter()
|
private val intentFilter = IntentFilter()
|
||||||
|
|
||||||
|
private val peerListListener = WirelessPeerList()
|
||||||
|
|
||||||
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)
|
||||||
@ -102,9 +106,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
val wifiDirect = findViewById<Switch>(R.id.wifiDirect)
|
val wifiDirect = findViewById<Switch>(R.id.wifiDirect)
|
||||||
wifiDirect.setOnCheckedChangeListener { _, isChecked ->
|
wifiDirect.setOnCheckedChangeListener { _, isChecked ->
|
||||||
mManager = getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager
|
|
||||||
mChannel = mManager!!.initialize(this, mainLooper, null);
|
|
||||||
|
|
||||||
if (ActivityCompat.checkSelfPermission(
|
if (ActivityCompat.checkSelfPermission(
|
||||||
this,
|
this,
|
||||||
Manifest.permission.ACCESS_FINE_LOCATION
|
Manifest.permission.ACCESS_FINE_LOCATION
|
||||||
@ -327,11 +328,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private class WiFiDirectBroadcastReceiver(mManager: WifiP2pManager,
|
inner class WiFiDirectBroadcastReceiver(mManager: WifiP2pManager,
|
||||||
mChannel: WifiP2pManager.Channel,
|
mChannel: WifiP2pManager.Channel): BroadcastReceiver() {
|
||||||
activity: MainActivity): BroadcastReceiver() {
|
|
||||||
var activity = activity
|
|
||||||
|
|
||||||
override fun onReceive(context: Context?, intent: Intent?) {
|
override fun onReceive(context: Context?, intent: Intent?) {
|
||||||
val action = intent!!.action
|
val action = intent!!.action
|
||||||
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION == action) {
|
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION == action) {
|
||||||
@ -339,19 +337,40 @@ class MainActivity : AppCompatActivity() {
|
|||||||
// the Activity.
|
// the Activity.
|
||||||
val state = intent!!.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1)
|
val state = intent!!.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1)
|
||||||
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
|
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
|
||||||
activity.setIsWifiP2pEnabled(true)
|
this@MainActivity.setIsWifiP2pEnabled(true)
|
||||||
} else {
|
} else {
|
||||||
activity.setIsWifiP2pEnabled(false)
|
this@MainActivity.setIsWifiP2pEnabled(false)
|
||||||
}
|
}
|
||||||
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION == action) {
|
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION == action) {
|
||||||
// The peer list has changed! We should probably do something about
|
// The peer list has changed! We should probably do something about
|
||||||
// that.
|
// that.
|
||||||
|
// Request available peers from the wifi p2p manager. This is an
|
||||||
|
// asynchronous call and the calling activity is notified with a
|
||||||
|
// callback on PeerListListener.onPeersAvailable()
|
||||||
|
if (mManager != null) {
|
||||||
|
if (ActivityCompat.checkSelfPermission(
|
||||||
|
this@MainActivity,
|
||||||
|
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
|
||||||
|
}
|
||||||
|
mManager!!.requestPeers(mChannel, peerListListener);
|
||||||
|
}
|
||||||
|
Log.d(TAG, "P2P peers changed");
|
||||||
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION == action) {
|
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION == action) {
|
||||||
|
|
||||||
// Connection state changed! We should probably do something about
|
// Connection state changed! We should probably do something about
|
||||||
// that.
|
// that.
|
||||||
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION == action) {
|
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION == action) {
|
||||||
activity.updateThisDevice(
|
this@MainActivity.updateThisDevice(
|
||||||
intent!!.getParcelableExtra(
|
intent!!.getParcelableExtra(
|
||||||
WifiP2pManager.EXTRA_WIFI_P2P_DEVICE
|
WifiP2pManager.EXTRA_WIFI_P2P_DEVICE
|
||||||
) as WifiP2pDevice
|
) as WifiP2pDevice
|
||||||
@ -371,7 +390,9 @@ class MainActivity : AppCompatActivity() {
|
|||||||
/** register the BroadcastReceiver with the intent values to be matched */
|
/** register the BroadcastReceiver with the intent values to be matched */
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
receiver = WiFiDirectBroadcastReceiver(mManager!!, mChannel!!, this)
|
mManager = getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager
|
||||||
|
mChannel = mManager!!.initialize(this, mainLooper, null);
|
||||||
|
receiver = WiFiDirectBroadcastReceiver(mManager!!, mChannel!!)
|
||||||
registerReceiver(receiver, intentFilter)
|
registerReceiver(receiver, intentFilter)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -379,4 +400,18 @@ class MainActivity : AppCompatActivity() {
|
|||||||
super.onPause()
|
super.onPause()
|
||||||
unregisterReceiver(receiver)
|
unregisterReceiver(receiver)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inner class WirelessPeerList:WifiP2pManager.PeerListListener{
|
||||||
|
|
||||||
|
override fun onPeersAvailable(peers: WifiP2pDeviceList?) {
|
||||||
|
// Out with the old, in with the new.
|
||||||
|
this@MainActivity.wirelessPeers.clear()
|
||||||
|
this@MainActivity.wirelessPeers.addAll(peers!!.deviceList);
|
||||||
|
// If an AdapterView is backed by this data, notify it // of the change. For instance, if you have a ListView of available // peers, trigger an update.
|
||||||
|
//((WiFiPeerListAdapter) getListAdapter()).notifyDataSetChanged(); if (peers.size() == 0) { Log.d(WiFiDirectActivity.TAG, "No devices found"); return; } }
|
||||||
|
//just show message
|
||||||
|
showToast("available peers:"+this@MainActivity.wirelessPeers.size)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user