mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-20 10:23:30 -04:00
Merge pull request #3433 from kiwix/Issue#3431
Fixes of KiwixWifiP2pBroadcastReceiver using some deprecated methods
This commit is contained in:
commit
0891512be7
@ -23,9 +23,8 @@ package org.kiwix.kiwixmobile.localFileTransfer
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.NetworkInfo
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.wifi.p2p.WifiP2pDevice
|
||||
import android.net.wifi.p2p.WifiP2pManager.EXTRA_NETWORK_INFO
|
||||
import android.net.wifi.p2p.WifiP2pManager.EXTRA_WIFI_P2P_DEVICE
|
||||
import android.net.wifi.p2p.WifiP2pManager.EXTRA_WIFI_STATE
|
||||
import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION
|
||||
@ -33,6 +32,8 @@ import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION
|
||||
import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION
|
||||
import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_ENABLED
|
||||
import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION
|
||||
import android.os.Build
|
||||
import org.kiwix.kiwixmobile.core.compat.CompatHelper.Companion.isNetworkAvailable
|
||||
|
||||
/**
|
||||
* Helper class for the local file sharing module.
|
||||
@ -41,7 +42,10 @@ import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION
|
||||
* the wifi p2p manager in [WifiDirectManager].
|
||||
*/
|
||||
|
||||
class KiwixWifiP2pBroadcastReceiver(private val p2pEventListener: P2pEventListener) :
|
||||
class KiwixWifiP2pBroadcastReceiver(
|
||||
private val p2pEventListener: P2pEventListener,
|
||||
private val connectivityManager: ConnectivityManager
|
||||
) :
|
||||
BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
when (intent.action) {
|
||||
@ -51,15 +55,18 @@ class KiwixWifiP2pBroadcastReceiver(private val p2pEventListener: P2pEventListen
|
||||
}
|
||||
WIFI_P2P_PEERS_CHANGED_ACTION -> p2pEventListener.onPeersChanged()
|
||||
WIFI_P2P_CONNECTION_CHANGED_ACTION -> {
|
||||
val networkInfo =
|
||||
intent.getParcelableExtra<NetworkInfo>(EXTRA_NETWORK_INFO)
|
||||
networkInfo?.let {
|
||||
p2pEventListener.onConnectionChanged(it.isConnected)
|
||||
}
|
||||
p2pEventListener.onConnectionChanged(connectivityManager.isNetworkAvailable())
|
||||
}
|
||||
WIFI_P2P_THIS_DEVICE_CHANGED_ACTION -> {
|
||||
val userDevice =
|
||||
intent.getParcelableExtra<WifiP2pDevice>(EXTRA_WIFI_P2P_DEVICE)
|
||||
val userDevice = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
intent.getParcelableArrayExtra(
|
||||
EXTRA_WIFI_P2P_DEVICE,
|
||||
WifiP2pDevice::class.java
|
||||
)?.get(0)
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
intent.getParcelableExtra(EXTRA_WIFI_P2P_DEVICE)
|
||||
}
|
||||
p2pEventListener.onDeviceChanged(userDevice)
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.localFileTransfer
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.IntentFilter
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.Uri
|
||||
import android.net.wifi.WpsInfo
|
||||
import android.net.wifi.p2p.WifiP2pConfig
|
||||
@ -61,7 +62,8 @@ class WifiDirectManager @Inject constructor(
|
||||
private val context: Context,
|
||||
private val sharedPreferenceUtil: SharedPreferenceUtil,
|
||||
private val alertDialogShower: AlertDialogShower,
|
||||
private val manager: WifiP2pManager?
|
||||
private val manager: WifiP2pManager?,
|
||||
private val connectivityManager: ConnectivityManager
|
||||
) : ChannelListener, PeerListListener, ConnectionInfoListener, P2pEventListener {
|
||||
var callbacks: Callbacks? = null
|
||||
|
||||
@ -96,7 +98,7 @@ class WifiDirectManager @Inject constructor(
|
||||
}
|
||||
|
||||
private fun registerWifiDirectBroadcastReceiver() {
|
||||
receiver = KiwixWifiP2pBroadcastReceiver(this)
|
||||
receiver = KiwixWifiP2pBroadcastReceiver(this, connectivityManager)
|
||||
|
||||
// For specifying broadcasts (of the P2P API) that the module needs to respond to
|
||||
val intentFilter = IntentFilter()
|
||||
|
Loading…
x
Reference in New Issue
Block a user