Skip to content
Snippets Groups Projects
Commit 32c9e319 authored by Pierre Nicolas's avatar Pierre Nicolas :joy:
Browse files

permission: fix infinite loop when permission refused

When permission is checked, it cause the fragment to pause.
After that, the fragment resumes causing the permission to be checked again.
= infinite loop

There is different place where this bug is occuring:
- QR code to share your username
- QR code to share your PIN
- Camera for android TV

GitLab: #1409

Change-Id: Idf84e9e7b990948fd80ced9437f297050b7ecff0
parent 92e5e984
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,19 @@ class QrCodePinInputFragment : Fragment() {
private val viewModel: QrCodePinInputViewModel by viewModels({ requireParentFragment() })
private lateinit var binding: QrCodePinInputBinding
private var cameraPermissionIsRefusedFlag = false // to not ask for permission again if refused
// check the permission to use the camera
private val requestCameraPermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
if (granted) {
showErrorPanel(isError = false)
initializeBarcode()
} else {
cameraPermissionIsRefusedFlag = true
showErrorPanel(isError = true)
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
......@@ -32,14 +45,11 @@ class QrCodePinInputFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.barcodeScanner.setStatusText("")
checkPermission()
}
override fun onResume() {
super.onResume()
if (checkPermission()) {
binding.barcodeScanner.resume()
}
manageCameraPermission()
}
override fun onPause() {
......@@ -92,27 +102,20 @@ class QrCodePinInputFragment : Fragment() {
}
}
// check the permission to use the camera
private val requestCameraPermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
if (granted) {
showErrorPanel(isError = false)
initializeBarcode()
} else {
showErrorPanel(isError = true)
}
}
private fun hasCameraPermission(): Boolean =
ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_GRANTED
private fun checkPermission(): Boolean {
private fun manageCameraPermission() {
if (!hasCameraPermission()) {
if (!cameraPermissionIsRefusedFlag) // if the permission is refused, don't ask again
requestCameraPermission.launch(Manifest.permission.CAMERA)
return false
else showErrorPanel(isError = true)
}
else{
showErrorPanel(isError = false)
initializeBarcode()
return true
binding.barcodeScanner.resume()
}
}
}
\ No newline at end of file
......@@ -42,12 +42,14 @@ class ScanFragment : Fragment() {
private var barcodeView: DecoratedBarcodeView? = null
private var mErrorMessageTextView: TextView? = null
private var cameraPermissionIsRefusedFlag = false // to not ask for permission again if refused
private val requestCameraPermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
if (granted) {
hideErrorPanel()
initializeBarcode()
} else {
cameraPermissionIsRefusedFlag = true
displayNoPermissionsError()
}
}
......@@ -126,6 +128,7 @@ class ScanFragment : Fragment() {
private fun checkPermission(): Boolean {
if (!hasCameraPermission()) {
if (!cameraPermissionIsRefusedFlag) // if the permission is refused, don't ask again
requestCameraPermission.launch(Manifest.permission.CAMERA)
return false
}
......
......@@ -79,6 +79,7 @@ class HomeActivity : FragmentActivity() {
private var input: Allocation? = null
private var out: Allocation? = null
private var mBlurOut: Allocation? = null
private var cameraPermissionIsRefusedFlag = false // to not ask for permission again if refused
private val mErrorCallback = ErrorCallback { error, camera ->
mBlurImage.visibility = View.INVISIBLE
......@@ -285,7 +286,7 @@ class HomeActivity : FragmentActivity() {
if (mDeviceRuntimeService.hasVideoPermission()) {
setUpCamera()
} else {
askCameraPermission()
if (!cameraPermissionIsRefusedFlag) askCameraPermission()
}
}
......@@ -297,10 +298,12 @@ class HomeActivity : FragmentActivity() {
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
ProfileCreationFragment.REQUEST_PERMISSION_CAMERA -> if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
ProfileCreationFragment.REQUEST_PERMISSION_CAMERA ->
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
cameraPermissionChanged(true)
setUpCamera()
}
else cameraPermissionIsRefusedFlag = true
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment