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

Bump min SDK version to 23 #38

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object Versions {

const val COMPILE_SDK_VERSION = 33
const val TARGET_SDK_VERSION = 33
const val MIN_SDK_VERSION = 21
const val MIN_SDK_VERSION = 23

private const val MAJOR = 0
private const val MINOR = 6
Expand Down
26 changes: 2 additions & 24 deletions library/src/main/java/com/vinted/coper/CoperFragment.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.vinted.coper

import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.annotation.VisibleForTesting
Expand Down Expand Up @@ -101,7 +100,7 @@ internal class CoperFragment : Fragment() {
getDeniedPermissionsResult(denied)
} else {
getPermissionsResult(
permissionChecks = requestPermissionsByVersion(denied.toTypedArray()),
permissionChecks = requestPermissionsAsync(denied.toTypedArray()).await(),
grantedPermissions = granted,
isSecondTime = true
)
Expand All @@ -122,21 +121,6 @@ internal class CoperFragment : Fragment() {
return PermissionResult.Denied(deniedPermissions)
}

// On devices with lower sdk than 23, there is no requesting permissions.
private suspend fun requestPermissionsByVersion(
permissions: Array<out String>
): List<PermissionCheckResult> {
return if (isDeviceSdkAtLeastMarshmallow()) {
requestPermissionsAsync(permissions).await()
} else {
permissions.map { PermissionCheckResult.Denied(it) }
}
}

private fun isDeviceSdkAtLeastMarshmallow(): Boolean {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
}

private fun requestPermissionsAsync(
permissions: Array<out String>
): Deferred<List<PermissionCheckResult>> {
Expand Down Expand Up @@ -237,13 +221,7 @@ internal class CoperFragment : Fragment() {
fun of(permissionResult: Int, permission: String): PermissionCheckResult {
return when (permissionResult) {
PermissionChecker.PERMISSION_GRANTED -> Granted(permission)
PermissionChecker.PERMISSION_DENIED,
PermissionChecker.PERMISSION_DENIED_APP_OP -> Denied(permission)
else -> {
val message = "Not expected permission result: $permissionResult " +
"for $permission"
throw IllegalStateException(message)
}
else -> Denied(permission)
}
}
}
Expand Down
26 changes: 7 additions & 19 deletions library/src/test/java/com/vinted/coper/CoperImplTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ class CoperImplTest {
}

@Test
@Config(sdk = [21, 23, 27])
@Config(sdk = [23, 27])
fun request_responseIsSuccessful() = runTest {
val response = fixture.request("test")

assertTrue(response.isGranted())
}

@Test
@Config(sdk = [21, 23, 27])
@Config(sdk = [23, 27])
fun request_permissionsIsGranted_grantedListConsistOfThisPermission() = runTest {
val permissionName = "granted"
mockCheckPermissions(permissionName, PackageManager.PERMISSION_GRANTED)
Expand All @@ -66,7 +66,7 @@ class CoperImplTest {
}

@Test
@Config(sdk = [21, 23, 27])
@Config(sdk = [23, 27])
fun request_twoPermissionsIsGranted_grantedListConsistOfThisPermissions() = runTest {
val firstPermission = "granted"
val secondPermission = "granted2"
Expand All @@ -79,18 +79,6 @@ class CoperImplTest {
assertEquals(listOf(firstPermission, secondPermission), response.grantedPermissions)
}

@Test
@Config(sdk = [21])
fun request_sdkUnder23AndPermissionsDenied_permissionResultDeniedWaitingResult() = runTest {
val permission = "denied"
mockCheckPermissions(permission, PackageManager.PERMISSION_DENIED)

val response = fixture.request(permission)

assertTrue(response.isDenied())
assertEquals(listOf(permission), response.getAllDeniedPermissions())
}

@Test
fun request_permissionsIsDeniedRationale_permissionDeniedRationaleResult() = runTest {
val permissionName = "denied"
Expand Down Expand Up @@ -669,7 +657,7 @@ class CoperImplTest {
}

@Test
@Config(sdk = [21, 23, 27])
@Config(sdk = [23, 27])
fun isPermissionsGranted_permissionsNotGranted_returnsFalse() = runTest {
mockCheckPermissions("not_granted", PermissionChecker.PERMISSION_DENIED)

Expand All @@ -679,7 +667,7 @@ class CoperImplTest {
}

@Test
@Config(sdk = [21, 23, 27])
@Config(sdk = [23, 27])
fun isPermissionsGranted_permissionsNotGrantedByOp_returnsFalse() = runTest {
mockCheckPermissions("not_granted_op", PermissionChecker.PERMISSION_DENIED_APP_OP)

Expand All @@ -689,7 +677,7 @@ class CoperImplTest {
}

@Test
@Config(sdk = [21, 23, 27])
@Config(sdk = [23, 27])
fun isPermissionsGranted_permissionsGranted_returnsTrue() = runTest {
mockCheckPermissions("granted", PermissionChecker.PERMISSION_GRANTED)

Expand Down Expand Up @@ -738,7 +726,7 @@ class CoperImplTest {
}

@Test
@Config(sdk = [21, 23, 27])
@Config(sdk = [23, 27])
fun request_runOnPausingDispatcher_responseIsSuccessful() = runTest {
val isCancelled = fixture.getFragmentSafely().lifecycleScope.launchWhenCreated {
fixture.request("test")
Expand Down