Checking microphone toggle state for recording audio permission android 12+

282 Views Asked by At

I have a method that checks the user's permission to record audio:

private fun checkPermissions() {
        val compositePermissionsListener = someListenner()
        Dexter.withContext(this)
            .withPermissions(
                Manifest.permission.RECORD_AUDIO
            )
            .withListener(compositePermissionsListener)
            .onSameThread()
            .check()
    }

This method is called onCreate and onResume if the user has a problem. In this case, he sees a dialog asking him, depending on the Android version, to go to settings (and check the android 12+ microphone switch)

The permissions listener is simple

@RequiresApi(Build.VERSION_CODES.S)
    override fun onPermissionsChecked(report: MultiplePermissionsReport) {
        recordAudioAccess = PermissionChecker.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PermissionChecker.PERMISSION_GRANTED
        // if android is higher than 12, check the switches. by default it is set to false
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            val sensorPrivacyManager = applicationContext
                .getSystemService(SensorPrivacyManager::class.java) as SensorPrivacyManager
            supportsMicrophoneToggle = sensorPrivacyManager
                .supportsSensorToggle(SensorPrivacyManager.Sensors.MICROPHONE)
        }
    }

I'm having problems when the user allows audio recording but disallows audio through the microphone switch. In this case recordAudioAccess == false. I cannot check what state this switch is in, but only check if it is on the device via the supportsMicrophoneToggle variable.

Because of this I can only offer different text in the dialog box to the user. Is there any other way to check this permission ?

0

There are 0 best solutions below