@@ -579,10 +579,8 @@ class OSIABWebViewActivity : AppCompatActivity() {
579579 pendingAcceptTypes = acceptTypes
580580 pendingCaptureEnabled = captureEnabled
581581
582- // if camera permission is not granted, request permission
583- if (ContextCompat .checkSelfPermission(
584- this @OSIABWebViewActivity, Manifest .permission.CAMERA ) != PackageManager .PERMISSION_GRANTED
585- ) {
582+ // if camera permission is declared in manifest but is not granted, request it
583+ if (hasCameraPermissionDeclared() && ! isCameraPermissionGranted()) {
586584 ActivityCompat .requestPermissions(
587585 this @OSIABWebViewActivity,
588586 arrayOf(Manifest .permission.CAMERA ),
@@ -630,11 +628,11 @@ class OSIABWebViewActivity : AppCompatActivity() {
630628
631629 private fun launchFileChooser (acceptTypes : String = "", isCaptureEnabled : Boolean = false) {
632630 val intentList = mutableListOf<Intent >()
633- val permissionGranted = ContextCompat .checkSelfPermission(
634- this @OSIABWebViewActivity, Manifest .permission.CAMERA ) == PackageManager .PERMISSION_GRANTED
631+ val permissionNotDeclaredOrGranted = hasCameraPermissionDeclared().not () || isCameraPermissionGranted()
635632
636633 // photo capture
637- if (permissionGranted) {
634+ // if permission isn't declared, we don't need it
635+ if (permissionNotDeclaredOrGranted) {
638636 if (acceptTypes.contains(" image" ) || acceptTypes.isEmpty()) {
639637 currentPhotoFile = createTempFile(this @OSIABWebViewActivity, " IMG_" , " .jpg" ).also { file ->
640638 currentPhotoUri = FileProvider .getUriForFile(
@@ -670,7 +668,7 @@ class OSIABWebViewActivity : AppCompatActivity() {
670668 }
671669
672670 // only camera intents (no gallery)
673- if (isCaptureEnabled && permissionGranted ) {
671+ if (isCaptureEnabled && permissionNotDeclaredOrGranted ) {
674672 val chooser = if (intentList.size == 1 ) {
675673 // with single option we launch the camera directly
676674 intentList[0 ]
@@ -699,21 +697,47 @@ class OSIABWebViewActivity : AppCompatActivity() {
699697
700698 val chooser = Intent (Intent .ACTION_CHOOSER ).apply {
701699 putExtra(Intent .EXTRA_INTENT , contentIntent)
702- if (permissionGranted ) {
700+ if (permissionNotDeclaredOrGranted ) {
703701 if (intentList.isNotEmpty()) {
704702 putExtra(Intent .EXTRA_INITIAL_INTENTS , intentList.toTypedArray())
705703 }
706704 }
707705 }
708706 fileChooserLauncher.launch(chooser)
709707 } else {
710- // capture enabled but permission not granted
708+ // capture enabled but permission declared and not granted
711709 // as our only option is to capture, we can't do anything
712710 cancelFileChooser()
713711 return
714712 }
715713 }
716714
715+ private fun isCameraPermissionGranted (): Boolean {
716+ return ContextCompat .checkSelfPermission(
717+ this @OSIABWebViewActivity, Manifest .permission.CAMERA
718+ ) == PackageManager .PERMISSION_GRANTED
719+ }
720+
721+ private fun hasCameraPermissionDeclared (): Boolean {
722+ // The CAMERA permission does not need to be requested unless it is declared in AndroidManifest.xml
723+ // If it's declared, camera intents will throw SecurityException if permission is not granted
724+ try {
725+ val packageManager = this @OSIABWebViewActivity.packageManager
726+ val permissionsInPackage = packageManager.getPackageInfo(
727+ this @OSIABWebViewActivity.packageName,
728+ PackageManager .GET_PERMISSIONS
729+ ).requestedPermissions ? : arrayOf()
730+ for (permission in permissionsInPackage) {
731+ if (permission == Manifest .permission.CAMERA ) {
732+ return true
733+ }
734+ }
735+ } catch (e: Exception ) {
736+ Log .d(LOG_TAG , e.message.toString())
737+ }
738+ return false
739+ }
740+
717741 }
718742
719743
0 commit comments