Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RMET-2759 OSBarcodeLib-Android - Implement torch button #6

Merged
merged 24 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1de666f
feat: implement flashlight
alexgerardojacinto Nov 14, 2023
2b02478
refactor: use hasFlashUnit to determine if camera has flash
alexgerardojacinto Nov 14, 2023
b6f998c
chore: raise lib version to 0.0.15
alexgerardojacinto Nov 14, 2023
a377eb6
refactor: remove extra line
alexgerardojacinto Nov 14, 2023
ac6c8bc
feat: update button according to flashlight state
alexgerardojacinto Nov 14, 2023
6ceaecd
refactor: replace Column with Box
alexgerardojacinto Nov 14, 2023
dd4896b
chore: raise lib version to 0.0.16
alexgerardojacinto Nov 14, 2023
8ae91a8
fix: properly import R class
alexgerardojacinto Nov 14, 2023
c2f405c
refactor: use helper method for flashlight button
alexgerardojacinto Nov 14, 2023
d505615
chore: raise lib version to 0.0.17
alexgerardojacinto Nov 14, 2023
9ef7d46
chore: update changelog
alexgerardojacinto Nov 14, 2023
41f141b
test: use theme to remove action bar from activity
alexgerardojacinto Nov 14, 2023
b39c2a1
chore: raise lib version to 0.0.18
alexgerardojacinto Nov 14, 2023
195ce5f
Revert "test: use theme to remove action bar from activity"
alexgerardojacinto Nov 14, 2023
e7718a4
fix: hide action bar
alexgerardojacinto Nov 14, 2023
c128337
chore: raise lib version to 0.0.19
alexgerardojacinto Nov 14, 2023
8394489
Revert "fix: hide action bar"
alexgerardojacinto Nov 14, 2023
d0b8c37
feat: hide action bar (title bar) and fill the whole screen with Came…
alexgerardojacinto Nov 14, 2023
b4a9247
chore: raise lib version to 0.0.20
alexgerardojacinto Nov 14, 2023
47bb7af
misc: add extra line
alexgerardojacinto Nov 14, 2023
8914520
refactor: remove extra lines
alexgerardojacinto Nov 15, 2023
5ae992c
refactor: only use one catch
alexgerardojacinto Nov 15, 2023
4d5fc6a
refactor: remove unused import
alexgerardojacinto Nov 15, 2023
38251b3
fix: only set isFlashlightOn if enableTorch goes OK
alexgerardojacinto Nov 15, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]

### 14-11-2023
Android - Implement Torch Button to settings (https://outsystemsrd.atlassian.net/browse/RMET-2759)

### 13-11-2023
Android - Implement AlertDialog to settings (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.14</version>
<version>0.0.20</version>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,52 @@ import android.os.Bundle
import android.provider.Settings
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.camera.core.Camera
import androidx.camera.core.CameraSelector
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.camera.view.PreviewView
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.camera.core.ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST
import androidx.compose.ui.Modifier
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.content.ContextCompat
import com.outsystems.plugins.barcode.R
import com.outsystems.plugins.barcode.controller.OSBARCBarcodeAnalyzer
import com.outsystems.plugins.barcode.controller.OSBARCScanLibraryFactory
import com.outsystems.plugins.barcode.controller.helper.OSBARCMLKitHelper
import com.outsystems.plugins.barcode.controller.helper.OSBARCZXingHelper
import com.outsystems.plugins.barcode.model.OSBARCError
import com.outsystems.plugins.barcode.model.OSBARCScanParameters
import com.outsystems.plugins.barcode.view.ui.theme.BarcodeScannerTheme
import java.lang.Exception

/**
* This class is responsible for implementing the UI of the scanning screen using Jetpack Compose.
* It is also responsible for opening a camera stream using CameraX, and calling the class that
* implements the ImageAnalysis.Analyzer interface.
*/
class OSBARCScannerActivity : ComponentActivity() {

private lateinit var camera: Camera
private lateinit var selector: CameraSelector
private var permissionRequestCount = 0
private var showDialog by mutableStateOf(false)

Expand All @@ -63,10 +71,16 @@ class OSBARCScannerActivity : ComponentActivity() {
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
actionBar?.hide()

val parameters = intent.extras?.getSerializable(SCAN_PARAMETERS) as OSBARCScanParameters
selector = CameraSelector.Builder()
.requireLensFacing(if (parameters.cameraDirection == CAM_DIRECTION_FRONT) CameraSelector.LENS_FACING_FRONT else CameraSelector.LENS_FACING_BACK)
.build()

setContent {
BarcodeScannerTheme {
ScanScreen(intent.extras?.getSerializable(SCAN_PARAMETERS) as OSBARCScanParameters)
ScanScreen(parameters)
}
}
}
Expand Down Expand Up @@ -131,16 +145,22 @@ class OSBARCScannerActivity : ComponentActivity() {
ProcessCameraProvider.getInstance(context)
}

Column (
modifier = Modifier.fillMaxSize()
) {
try {
camera = cameraProviderFuture.get().bindToLifecycle(
lifecycleOwner,
selector
)
} catch (e: Exception) {
e.message?.let { Log.e(LOG_TAG, it) }
setResult(OSBARCError.SCANNING_GENERAL_ERROR.code)
finish()
}

Box(modifier = Modifier.fillMaxSize()) {
AndroidView(
factory = { context ->
val previewView = PreviewView(context)
val preview = Preview.Builder().build()
val selector = CameraSelector.Builder()
.requireLensFacing(if (parameters.cameraDirection == CAM_DIRECTION_FRONT) CameraSelector.LENS_FACING_FRONT else CameraSelector.LENS_FACING_BACK)
.build()
preview.setSurfaceProvider(previewView.surfaceProvider)
val imageAnalysis = ImageAnalysis.Builder()
.setBackpressureStrategy(STRATEGY_KEEP_ONLY_LATEST)
Expand All @@ -166,7 +186,7 @@ class OSBARCScannerActivity : ComponentActivity() {
)
)
try {
cameraProviderFuture.get().bindToLifecycle(
camera = cameraProviderFuture.get().bindToLifecycle(
lifecycleOwner,
selector,
preview,
Expand All @@ -179,7 +199,39 @@ class OSBARCScannerActivity : ComponentActivity() {
}
previewView
},
modifier = Modifier.weight(1f)
modifier = Modifier.fillMaxSize()
)

if (camera.cameraInfo.hasFlashUnit()) {
TorchButton()
}
}
}

@Composable
fun TorchButton() {
var isFlashlightOn by remember { mutableStateOf(false) }
val onIcon = painterResource(id = R.drawable.flash_on)
val offIcon = painterResource(id = R.drawable.flash_off)

Button(
onClick = {
try {
camera.cameraControl.enableTorch(!isFlashlightOn)
isFlashlightOn = !isFlashlightOn
} catch (e: Exception) {
e.message?.let { Log.e(LOG_TAG, it) }
}
},
colors = ButtonDefaults.buttonColors(
containerColor = if (isFlashlightOn) Color.White else Color.Black
),
shape = CircleShape
) {
val icon = if (isFlashlightOn) onIcon else offIcon
Image(
painter = icon,
contentDescription = null
)
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/res/drawable/flash_off.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="12dp"
android:height="23dp"
android:viewportWidth="12"
android:viewportHeight="23">
<path
android:pathData="M11.854,5.866C12.015,6.045 12.046,6.241 11.948,6.455L4.715,21.951C4.599,22.174 4.412,22.286 4.153,22.286C4.117,22.286 4.055,22.277 3.965,22.259C3.814,22.214 3.7,22.129 3.624,22.004C3.548,21.879 3.528,21.746 3.564,21.603L6.202,10.781L0.765,12.134C0.729,12.143 0.675,12.147 0.604,12.147C0.443,12.147 0.305,12.098 0.189,12C0.028,11.866 -0.03,11.692 0.015,11.478L2.707,0.429C2.742,0.304 2.814,0.201 2.921,0.121C3.028,0.04 3.153,0 3.296,0H7.689C7.858,0 8.001,0.056 8.117,0.167C8.233,0.279 8.291,0.411 8.291,0.563C8.291,0.634 8.269,0.714 8.224,0.804L5.934,7.004L11.238,5.692C11.309,5.674 11.363,5.665 11.399,5.665C11.568,5.665 11.72,5.732 11.854,5.866L11.854,5.866Z"
android:fillColor="#ffffff"
android:fillType="evenOdd"/>
</vector>
10 changes: 10 additions & 0 deletions src/main/res/drawable/flash_on.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="14dp"
android:height="25dp"
android:viewportWidth="14"
android:viewportHeight="25">
<path
android:pathData="M13.128,6.495C13.306,6.693 13.341,6.91 13.232,7.147L5.224,24.304C5.096,24.551 4.888,24.675 4.602,24.675C4.562,24.675 4.493,24.665 4.394,24.645C4.226,24.596 4.1,24.502 4.016,24.363C3.932,24.225 3.91,24.077 3.949,23.918L6.87,11.937L0.85,13.435C0.811,13.444 0.751,13.45 0.672,13.45C0.494,13.45 0.341,13.395 0.212,13.286C0.034,13.138 -0.03,12.945 0.02,12.708L3,0.475C3.04,0.336 3.119,0.222 3.237,0.133C3.356,0.044 3.494,0 3.653,0H8.516C8.704,0 8.862,0.062 8.991,0.185C9.119,0.309 9.184,0.455 9.184,0.623C9.184,0.702 9.159,0.791 9.11,0.89L6.574,7.755L12.446,6.302C12.525,6.282 12.584,6.272 12.624,6.272C12.812,6.272 12.98,6.347 13.128,6.495L13.128,6.495Z"
android:fillColor="#4F575E"
android:fillType="evenOdd"/>
</vector>