0

Hey guys I want to handle denied permission in jetpack compose. I found PermissionsRequired method in which helpful in my case till now. I got a problem which I explain in detail later. First I'll explain what I did inside this function.

class BluetoothRequestActivity : BaseActivity() {

    val permissions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        listOf(
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.BLUETOOTH_SCAN,
            Manifest.permission.BLUETOOTH_CONNECT,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
        )
    } else {
        listOf(
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AbcTheme {
                AppBarScaffold() {
                    val multiplePermissionsState = rememberMultiplePermissionsState(permissions = permissions)
                    PermissionsRequired(
                        multiplePermissionsState = multiplePermissionsState,
                        permissionsNotGrantedContent = {
                            setupBluetoothRequest(multiplePermissionsState)
                        },
                        permissionsNotAvailableContent = {
                            setupBluetoothRequest(openPermissionSetting = true)
                        }
                    ) {}
                }
            }
        }
    }

    @Composable
    private fun setupBluetoothRequest(
        multiplePermissionsState: MultiplePermissionsState? = null,
        openPermissionSetting: Boolean = false
    ) {
        BluetoothRequest(
            multiplePermissionsState = multiplePermissionsState,
            openPermissionSetting = openPermissionSetting,
            onCancelClick = {
                finish()
            }
        )
    }
}

When first time user open the activity permissionsNotGrantedContent will open it shows the Button to ask the permission. Now when user denied the permission and click not ask again options, I changed the view through permissionsNotAvailableContent will tigger and Button functionality change to open System setting. I did this without any problem.

MAIN PROBLEM

All things happen then I closed my activity and Reopen the activity. My PermissionsRequired always trigger permissionsNotGrantedContent this function which is wrong. Right now according to flow user permanently disable the dialog so why this is trigger I want to handle this function setupBluetoothRequest(openPermissionSetting = true). Any idea how can I fix this problem? Thanks

UPDATE

I have 4 scenario when first user land on screen I want to show Text - Abc wants permission and Button. When user click on Button it asked runtime permission. It asked 2 options Allow and Denied. When user Denied 2 times I want to show in Text- you denied please manual open setting and give permission. If user Allow then I want to show Text - Permission granted on screen. I did all these 3 scenario without any problem. When user Denied the permission 2 times it shows Text- you denied please manual open setting and give permission but when user close the screen and reopen screen I am getting Text - Abc wants permission and Button on screen which I don't want. So how can I solve this problem.

setContent {
            AbcTheme {
                AppBarScaffold() {
                    val multiplePermissionsState = rememberMultiplePermissionsState(permissions = permissions)
                    when {
                        multiplePermissionsState.shouldShowRationale -> {
                            setupBluetoothRequest(multiplePermissionsState)
                        }
                        multiplePermissionsState.permissionRequested -> {
                            setupBluetoothRequest(openPermissionSetting = true)
                        }
                    }
                }
            }
        }

UPDATE 2

after updating from 0.23.1 to 0.25.1 I don't see the options of permissionRequested anymore.

when {
        multiplePermissionsState.allPermissionsGranted -> {
            Text(text = "Permission Granted", color = Color.White)
        }
        multiplePermissionsState.shouldShowRationale ->{
            Text(text = "shouldShowRationale", color = Color.White)
        }
    }

So how can I deal now?

Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127
  • Hi yo. Do you call `permissionsNotAvailableContent` if `multiplePermissionsState.shouldShowRationale` is `true`? I suggest following their sample code: https://github.com/google/accompanist/blob/main/sample/src/main/java/com/google/accompanist/sample/permissions/RequestMultiplePermissionsSample.kt – Shlomi Katriel Sep 21 '22 at 06:30
  • I believe `PermissionsRequired` has being removed in latest accompanist versions, update it and follow [documentation sample](https://google.github.io/accompanist/permissions/) – Phil Dukhov Sep 21 '22 at 06:39
  • @ShlomiKatriel the example is just for grant and denied logic. – Kotlin Learner Sep 21 '22 at 07:49
  • @PhilDukhov I updated my code. But it still the same issue. Can you please guide me on. – Kotlin Learner Sep 21 '22 at 07:57
  • @ShlomiKatriel can you please have a look on updated section in question.. Thanks – Kotlin Learner Sep 21 '22 at 07:57

1 Answers1

0

The field permissionRequested was deleted. The new way that you can check if the permissions were requested is creating a flag initially set to false, and set true when rememberMultiplePermissionsState callback is called.

Try something like this:

var permissionRequested by remember { mutableStateOf(false) }
val permissionsState = rememberMultiplePermissionsState(permissions) {
    permissionRequested = true
}
// Use permissionRequested 

permissionRequested will be set to true when the permission dialog is closed