Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chain fine location and nearby devices permission requests #78

Merged
merged 8 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -801,10 +801,10 @@ class WifiDirectDataSharingStrategyTest : RobolectricTest() {

coVerify { dataInputStream.readLong() }
coVerify { dataInputStream.read(any(), 0, bytePayload.size) }
verify { wifiDirectDataSharingStrategy invoke "logDebug" withArguments listOf("file size 0") }
coVerify { wifiDirectDataSharingStrategy invoke "logDebug" withArguments listOf("file size 0") }

val bytePayloadSlot = slot<BytePayload>()
verify { payloadReceiptListener.onPayloadReceived(capture(bytePayloadSlot)) }
coVerify { payloadReceiptListener.onPayloadReceived(capture(bytePayloadSlot)) }
Assert.assertArrayEquals(bytePayload, bytePayloadSlot.captured.payload)
}

Expand Down
Loading