Skip to content

Commit 6516ba5

Browse files
refactor: split launchFileChooser in multiple functions for readability
References: https://outsystemsrd.atlassian.net/browse/RMET-4466
1 parent aaa3d41 commit 6516ba5

1 file changed

Lines changed: 44 additions & 44 deletions

File tree

src/main/java/com.outsystems.plugins.inappbrowser/osinappbrowserlib/views/OSIABWebViewActivity.kt

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -630,11 +630,27 @@ class OSIABWebViewActivity : AppCompatActivity() {
630630
}
631631

632632
private fun launchFileChooser(acceptTypes: String = "", isCaptureEnabled: Boolean = false) {
633+
val intentList = buildPhotoVideoIntents(acceptTypes)
634+
val permissionNotDeclaredOrGranted = hasCameraPermissionDeclared().not() || isCameraPermissionGranted()
635+
636+
if (isCaptureEnabled && permissionNotDeclaredOrGranted) {
637+
// if capture is enabled, we only show the camera and video options
638+
launchCameraChooser(intentList)
639+
} else if (!isCaptureEnabled) {
640+
// if capture is not enabled, we show the full chooser
641+
launchFullChooser(intentList, acceptTypes, permissionNotDeclaredOrGranted)
642+
} else {
643+
// capture is enabled but permission declared and not granted,
644+
// as our only option is to capture, we cannot proceed
645+
cancelFileChooser()
646+
return
647+
}
648+
}
649+
650+
private fun buildPhotoVideoIntents(acceptTypes: String): MutableList<Intent> {
633651
val intentList = mutableListOf<Intent>()
634652
val permissionNotDeclaredOrGranted = hasCameraPermissionDeclared().not() || isCameraPermissionGranted()
635653

636-
// photo capture
637-
// if permission isn't declared, we don't need it
638654
if (permissionNotDeclaredOrGranted) {
639655
if (acceptTypes.contains("image") || acceptTypes.isEmpty()) {
640656
currentPhotoFile = createTempFile(this@OSIABWebViewActivity, "IMG_", ".jpg").also { file ->
@@ -644,14 +660,11 @@ class OSIABWebViewActivity : AppCompatActivity() {
644660
file
645661
)
646662
}
647-
648663
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
649664
putExtra(MediaStore.EXTRA_OUTPUT, currentPhotoUri)
650665
}
651666
intentList.add(takePictureIntent)
652667
}
653-
654-
// video capture
655668
if (acceptTypes.contains("video") || acceptTypes.isEmpty()) {
656669
currentVideoFile = createTempFile(this@OSIABWebViewActivity, "VID_", ".mp4").also { file ->
657670
currentVideoFile = file
@@ -661,56 +674,43 @@ class OSIABWebViewActivity : AppCompatActivity() {
661674
file
662675
)
663676
}
664-
665677
val takeVideoIntent = Intent(MediaStore.ACTION_VIDEO_CAPTURE).apply {
666678
putExtra(MediaStore.EXTRA_OUTPUT, currentVideoUri)
667679
}
668680
intentList.add(takeVideoIntent)
669681
}
670-
671682
}
683+
return intentList
684+
}
672685

673-
// only camera intents (no gallery)
674-
if (isCaptureEnabled && permissionNotDeclaredOrGranted) {
675-
val chooser = if (intentList.size == 1) {
676-
// with single option we launch the camera directly
677-
intentList[0]
678-
} else {
679-
Intent(Intent.ACTION_CHOOSER).apply {
680-
// with multiple options we show the chooser
681-
putExtra(Intent.EXTRA_INTENT, intentList[0])
682-
putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.drop(1).toTypedArray())
683-
}
684-
}
685-
fileChooserLauncher.launch(chooser)
686-
} else if (!isCaptureEnabled) {
687-
// if capture is not enabled, we always show the full chooser
688-
689-
// flow with gallery/file picker
690-
val contentIntent = Intent(Intent.ACTION_GET_CONTENT).apply {
691-
addCategory(Intent.CATEGORY_OPENABLE)
692-
type = when {
693-
acceptTypes.contains("video") -> "video/*"
694-
acceptTypes.contains("image") -> "image/*"
695-
else -> "*/*"
696-
}
686+
private fun launchCameraChooser(intentList: List<Intent>) {
687+
val chooser = if (intentList.size == 1) {
688+
intentList[0]
689+
} else {
690+
Intent(Intent.ACTION_CHOOSER).apply {
691+
putExtra(Intent.EXTRA_INTENT, intentList[0])
692+
putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.drop(1).toTypedArray())
697693
}
694+
}
695+
fileChooserLauncher.launch(chooser)
696+
}
698697

699-
val chooser = Intent(Intent.ACTION_CHOOSER).apply {
700-
putExtra(Intent.EXTRA_INTENT, contentIntent)
701-
if (permissionNotDeclaredOrGranted) {
702-
if (intentList.isNotEmpty()) {
703-
putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toTypedArray())
704-
}
705-
}
698+
private fun launchFullChooser(intentList: List<Intent>, acceptTypes: String, permissionNotDeclaredOrGranted: Boolean) {
699+
val contentIntent = Intent(Intent.ACTION_GET_CONTENT).apply {
700+
addCategory(Intent.CATEGORY_OPENABLE)
701+
type = when {
702+
acceptTypes.contains("video") -> "video/*"
703+
acceptTypes.contains("image") -> "image/*"
704+
else -> "*/*"
705+
}
706+
}
707+
val chooser = Intent(Intent.ACTION_CHOOSER).apply {
708+
putExtra(Intent.EXTRA_INTENT, contentIntent)
709+
if (permissionNotDeclaredOrGranted && intentList.isNotEmpty()) {
710+
putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toTypedArray())
706711
}
707-
fileChooserLauncher.launch(chooser)
708-
} else {
709-
// capture enabled but permission declared and not granted
710-
// as our only option is to capture, we can't do anything
711-
cancelFileChooser()
712-
return
713712
}
713+
fileChooserLauncher.launch(chooser)
714714
}
715715

716716
private fun isCameraPermissionGranted(): Boolean {

0 commit comments

Comments
 (0)