Skip to content

Commit

Permalink
Chain fine location and nearby devices permission requests
Browse files Browse the repository at this point in the history
Add null check for wifiP2pChannel when requesting device info
  • Loading branch information
Rkareko committed Sep 4, 2024
1 parent 0e9b5d7 commit cd3be88
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion p2p-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ afterEvaluate {
from(components["release"])
artifactId = "p2p-lib"
groupId = "org.smartregister"
version = "0.6.10-SNAPSHOT"
version = "0.6.11-SNAPSHOT"
pom {
name.set("Peer to Peer Library")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {
android.Manifest.permission.NEARBY_WIFI_DEVICES
)) != PackageManager.PERMISSION_GRANTED
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU
) {
logDebug(
"initiatePeerDiscoveryOnceAccessFineLocationGranted(): requesting ACCESS_FINE_LOCATION"
)
Expand Down Expand Up @@ -282,6 +284,9 @@ class WifiDirectDataSharingStrategy : DataSharingStrategy, P2PManagerListener {
onDeviceFound: OnDeviceFound,
onConnected: DataSharingStrategy.PairingListener
) {
if (wifiP2pChannel == null) {
initChannel(onDeviceFound = onDeviceFound, onConnected = onConnected)
}
wifiP2pChannel?.also { wifiP2pChannel ->
if (ActivityCompat.checkSelfPermission(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ import timber.log.Timber
class P2PDeviceSearchActivity : AppCompatActivity(), P2pModeSelectContract.View {

private val accessFineLocationPermissionRequestInt: Int = 12345
private val nearbyWifiDevicesPermissionRequestInt: Int = 67890
private val p2PReceiverViewModel by viewModels<P2PReceiverViewModel> {
P2PReceiverViewModel.Factory(
dataSharingStrategy = dataSharingStrategy,
Expand Down Expand Up @@ -290,7 +291,11 @@ class P2PDeviceSearchActivity : AppCompatActivity(), P2pModeSelectContract.View
OnSuccessListener<LocationSettingsResponse?> {
// All location settings are satisfied. The client can initialize
// location requests here.
checkEnableWifi()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
checkEnableWifi()
} else {
checkNearbyWifiDevicesPermissionEnabled()
}
}
)
result.addOnFailureListener(
Expand All @@ -316,6 +321,22 @@ class P2PDeviceSearchActivity : AppCompatActivity(), P2pModeSelectContract.View
)
}

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
fun checkNearbyWifiDevicesPermissionEnabled() {
when (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.NEARBY_WIFI_DEVICES)
) {
PackageManager.PERMISSION_GRANTED -> {
logDebug("P2PDeviceSearchActivity Wifi P2P: Nearby wifi devices granted")
checkEnableWifi()
}
else -> {
logDebug(
"P2PDeviceSearchActivity Wifi P2P: Requesting Nearby wifi devices granted permission"
)
requestNearbyWifiDevicesNotGranted()
}
}
}
fun createLocationRequest(): LocationRequest {
return LocationRequest.create().apply {
interval = 3600000
Expand Down Expand Up @@ -402,6 +423,20 @@ class P2PDeviceSearchActivity : AppCompatActivity(), P2pModeSelectContract.View
}
}

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
private fun requestNearbyWifiDevicesNotGranted() {
when (ActivityCompat.checkSelfPermission(this, Manifest.permission.NEARBY_WIFI_DEVICES)) {
PackageManager.PERMISSION_GRANTED -> logDebug("Wifi P2P: Nearby wifi devices granted")
else -> {
logDebug("Wifi P2P: Requesting access Nearby wifi devices permission")
return requestPermissions(
arrayOf(Manifest.permission.NEARBY_WIFI_DEVICES),
nearbyWifiDevicesPermissionRequestInt
)
}
}
}

override fun sendDeviceDetails() {
p2PSenderViewModel.sendDeviceDetails(getCurrentConnectedDevice())
}
Expand Down Expand Up @@ -483,6 +518,14 @@ class P2PDeviceSearchActivity : AppCompatActivity(), P2pModeSelectContract.View
) {
checkLocationEnabled()
}

if ((nearbyWifiDevicesPermissionRequestInt == requestCode &&
hasPermission(Manifest.permission.NEARBY_WIFI_DEVICES)) &&
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
) {
logDebug("onRequestPermissionsResult has nearby wifi devices permission")
checkNearbyWifiDevicesPermissionEnabled()
}
}

override fun onStop() {
Expand Down

0 comments on commit cd3be88

Please sign in to comment.