Skip to content

Commit

Permalink
Merge pull request #5 from OutSystems/feat/RMET-2764/dialog-to-settings
Browse files Browse the repository at this point in the history
RMET-2764 OSBarcodeLib-Android - Dialog to settings when permission is denied
  • Loading branch information
alexgerardojacinto committed Nov 14, 2023
2 parents e5f8f14 + 1a9b4fe commit ca7f64b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ The changes documented here do not include those from the original repository.

## [Unreleased]

### 13-11-2023
Android - Implement AlertDialog to settings (https://outsystemsrd.atlassian.net/browse/RMET-2764)

### 13-11-2023
Android - Select Camera (Back or Front) (https://outsystemsrd.atlassian.net/browse/RMET-2764)

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.outsystems</groupId>
<artifactId>osbarcode-android</artifactId>
<version>0.0.12</version>
<version>0.0.14</version>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.outsystems.plugins.barcode.view

import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.sp

@Composable
fun CameraPermissionRequiredDialog(
onDismissRequest: () -> Unit,
onConfirmation: () -> Unit,
shouldShowDialog: Boolean,
dialogTitle: String,
dialogText: String,
confirmButtonText: String,
dismissButtonText: String
) {
if (shouldShowDialog) {
AlertDialog(
title = {
Text(
text = dialogTitle,
fontSize = 20.sp
)
},
text = {
Text(text = dialogText)
},
onDismissRequest = {
onDismissRequest()
},
confirmButton = {
TextButton(
onClick = {
onConfirmation()
}
) {
Text(confirmButtonText)
}
},
dismissButton = {
TextButton(
onClick = {
onDismissRequest()
}
) {
Text(dismissButtonText)
}
}
)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.outsystems.plugins.barcode.view

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand Down Expand Up @@ -43,6 +47,9 @@ import java.lang.Exception
*/
class OSBARCScannerActivity : ComponentActivity() {

private var permissionRequestCount = 0
private var showDialog by mutableStateOf(false)

companion object {
private const val SCAN_SUCCESS_RESULT_CODE = -1
private const val SCAN_RESULT = "scanResult"
Expand All @@ -64,6 +71,11 @@ class OSBARCScannerActivity : ComponentActivity() {
}
}

override fun onResume() {
super.onResume()
showDialog = !hasCameraPermission(this.applicationContext)
}

/**
* Composable function, responsible for declaring the UI of the screen,
* as well as creating an instance of OSBARCBarcodeAnalyzer for image analysis.
Expand All @@ -72,29 +84,52 @@ class OSBARCScannerActivity : ComponentActivity() {
fun ScanScreen(parameters: OSBARCScanParameters) {
val lifecycleOwner = LocalLifecycleOwner.current
val context = LocalContext.current
var permissionGiven by remember { mutableStateOf(true) }

// permissions
val requestPermissionLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
// do nothing, continue
permissionGiven = true
showDialog = false
} else {
this.setResult(OSBARCError.CAMERA_PERMISSION_DENIED_ERROR.code)
this.finish()
permissionGiven = false
showDialog = true
}
}
SideEffect {
requestPermissionLauncher.launch(Manifest.permission.CAMERA)
if (permissionRequestCount == 0) {
permissionRequestCount++
requestPermissionLauncher.launch(Manifest.permission.CAMERA)
}
}

if (!permissionGiven) {
CameraPermissionRequiredDialog(
onDismissRequest = {
this.setResult(OSBARCError.CAMERA_PERMISSION_DENIED_ERROR.code)
this.finish()
},
onConfirmation = {
val intent = Intent().apply {
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
data = Uri.fromParts("package", context.packageName, null)
}
context.startActivity(intent)
},
shouldShowDialog = showDialog,
dialogTitle = "Camera Access Not Enabled",
dialogText = "To continue, please go to the Settings app and enable it.",
confirmButtonText = "Settings",
dismissButtonText = "Ok"
)
}

// rest of the UI
val cameraProviderFuture = remember {
ProcessCameraProvider.getInstance(context)
}
var barcode by remember {
mutableStateOf("")
}

Column (
modifier = Modifier.fillMaxSize()
Expand All @@ -117,9 +152,8 @@ class OSBARCScannerActivity : ComponentActivity() {
parameters.androidScanningLibrary ?: "",
OSBARCZXingHelper(),
OSBARCMLKitHelper()
), // temporary
),
{ result ->
barcode = result
val resultIntent = Intent()
resultIntent.putExtra(SCAN_RESULT, result)
setResult(SCAN_SUCCESS_RESULT_CODE, resultIntent)
Expand Down Expand Up @@ -150,4 +184,11 @@ class OSBARCScannerActivity : ComponentActivity() {
}
}

private fun hasCameraPermission(context: Context): Boolean {
return ContextCompat.checkSelfPermission(
context,
Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED
}

}

0 comments on commit ca7f64b

Please sign in to comment.