Developing an application that must grant permissions to the camera, in testing phase, I have discovered that on Huawei devices, the shouldShowRequestPermissionRationale function does not behave correctly, these would be the events.
- Request permission for the first time and the user denies it
- Request the permission for the second time and according to the Android documentation, the shouldShowRequestPermissionRationale function returns true, but in the particular case of Huawei, it returns false.
On all other devices, point 2 returns true.
However, if I proceed this way on Huawei
- Request permission for the first time and I grant it
- Go to settings and deny it
- shouldShowRequestPermissionRationale function returns true.
Attached an example code where you can see this behavior.
package com.example.huaweipermissions
import android.Manifest
import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import com.example.huaweipermissions.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.button.setOnClickListener {
launchPermission()
}
}
private fun askForPermissionsDialog() {
androidx.appcompat.app.AlertDialog.Builder(this)
.setCancelable(true)
.setMessage("This is the shouldShowRequestPermissionRationale dialog")
.setPositiveButton("Settings") { dialog, _ -> // Open app settings - https://stackoverflow.com/a/32983128
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val uri = Uri.fromParts("package", this.packageName, null)
intent.data = uri
this.startActivity(intent) }
.setNegativeButton("Cancel") { dialog, _ -> dialog.dismiss() }
.create()
.show()
}
private fun launchPermission(){
binding.editTextRationale.setText(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA).toString())
binding.editTextPermission.setText("")
if (this.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
askForPermissionsDialog()
}
else {
requestPermissionLauncher.launch(
Manifest.permission.CAMERA
)
}
}
private val requestPermissionLauncher =
this.registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (isGranted) {
binding.editTextPermission.setText("GRANTED")
} else {
binding.editTextPermission.setText("DENIED")
}
}
}