Skip to content

Commit

Permalink
Update QR Code Scanning UI (#3438)
Browse files Browse the repository at this point in the history
* Update qr code scan to use bottomsheetdialog ui

* Fix failing QrCodeCameraPermissionsDialogFragment tests

* Update tests for QR code scanning

* Update documentation on support for qr_code-widget extension

---------

Co-authored-by: Allan Onchuru <16164649+allan-on@users.noreply.github.com>
  • Loading branch information
LZRS and allan-on committed Aug 16, 2024
1 parent e2f89d9 commit 70ceb20
Show file tree
Hide file tree
Showing 18 changed files with 553 additions and 203 deletions.
11 changes: 10 additions & 1 deletion android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
accompanist = "0.23.1"
activity-compose = "1.8.2"
androidJunit5 = "1.8.2.1"
androidx-camera = "1.4.0-rc01"
androidx-paging = "3.3.0"
androidx-test= "1.6.1"
androidx-test = "1.6.1"
appcompat = "1.7.0"
benchmark-junit = "1.2.4"
cardview = "1.0.0"
Expand Down Expand Up @@ -56,6 +57,7 @@ leakcanary-android = "2.10"
lifecycle= "2.8.3"
mapbox-sdk-turf = "4.8.0"
material = "1.12.0"
mlkit-barcode-scanning = "17.3.0"
mockk = "1.13.8"
mockk-android = "1.13.8"
msg-simple = "1.2"
Expand Down Expand Up @@ -88,6 +90,11 @@ xercesImpl = "2.12.2"
accompanist-flowlayout = { group = "com.google.accompanist", name = "accompanist-flowlayout", version.ref = "accompanist" }
accompanist-placeholder = { group = "com.google.accompanist", name = "accompanist-placeholder", version.ref = "accompanist" }
activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activity-compose" }
androidx-camera-camera2 = { group = "androidx.camera", name = "camera-camera2", version.ref = "androidx-camera"}
androidx-camera-extensions = { group = "androidx.camera", name = "camera-extensions", version.ref = "androidx-camera"}
androidx-camera-lifecycle = { group = "androidx.camera", name = "camera-lifecycle", version.ref = "androidx-camera"}
androidx-camera-mlkit-vision = {group = "androidx.camera", name = "camera-mlkit-vision", version.ref = "androidx-camera"}
androidx-camera-view = {group = "androidx.camera", name = "camera-view", version.ref = "androidx-camera"}
activity-ktx = { group = "androidx.activity", name = "activity-ktx", version.ref = "activity-compose" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
benchmark-junit = { group = "androidx.benchmark", name = "benchmark-junit4", version.ref = "benchmark-junit" }
Expand Down Expand Up @@ -154,6 +161,7 @@ lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-
lifecycle-viewmodel-ktx = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-ktx", version.ref = "lifecycle" }
mapbox-sdk-turf = { group = "com.mapbox.mapboxsdk", name = "mapbox-sdk-turf", version.ref = "mapbox-sdk-turf" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
mlkit-barcode-scanning = { group = "com.google.mlkit", name = "barcode-scanning", version.ref = "mlkit-barcode-scanning"}
mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" }
mockk-android = { group = "io.mockk", name = "mockk-android", version.ref = "mockk-android" }
msg-simple = { group = "com.github.java-json-tools", name = "msg-simple", version.ref = "msg-simple" }
Expand Down Expand Up @@ -206,6 +214,7 @@ org-owasp-dependencycheck = { id = "org.owasp.dependencycheck", version.ref = "o

[bundles]
accompanist = ["accompanist-placeholder", "accompanist-flowlayout"]
cameraX = ["androidx-camera-camera2", "androidx-camera-extensions", "androidx-camera-lifecycle", "androidx-camera-mlkit-vision", "androidx-camera-view"]
compose = ["activity-compose","activity-ktx", "ui", "ui-tooling-preview", "constraintlayout-compose", "foundation","runtime-livedata"]
compose-ui-test = ["ui-test-junit4","ui-test-manifest"]
coroutines = ["kotlinx-coroutines-core", "kotlinx-coroutines-android"]
Expand Down
7 changes: 5 additions & 2 deletions android/quest/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.json.JSONObject
plugins {
`jacoco-report`
`project-properties`
`ktlint`
ktlint
id("com.android.application")
id("kotlin-android")
id("kotlin-kapt")
Expand All @@ -27,7 +27,7 @@ plugins {
sonar {
properties {
property("sonar.projectKey", "fhircore")
property("sonar.kotlin.source.version", libs.kotlin)
property("sonar.kotlin.source.version", libs.versions.kotlin)
property(
"sonar.androidLint.reportPaths",
"${project.layout.buildDirectory.get()}/reports/lint-results-opensrpDebug.xml",
Expand Down Expand Up @@ -429,6 +429,9 @@ dependencies {
implementation(libs.dagger.hilt.android)
implementation(libs.hilt.work)
implementation(libs.gms.play.services.location)
implementation(libs.mlkit.barcode.scanning)

implementation(libs.bundles.cameraX)

// Annotation processors
kapt(libs.hilt.compiler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,26 @@
package org.smartregister.fhircore.quest.ui.sdc.qrCode

import android.Manifest
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.VisibleForTesting
import androidx.core.os.bundleOf
import androidx.fragment.app.DialogFragment
import com.google.android.fhir.datacapture.contrib.views.barcode.mlkit.md.LiveBarcodeScanningFragment
import org.smartregister.fhircore.engine.util.location.PermissionUtils
import org.smartregister.fhircore.quest.R

class QrCodeCameraDialogFragment : DialogFragment(R.layout.fragment_camera_permission) {
class CameraPermissionsDialogFragment : DialogFragment(R.layout.fragment_camera_permission) {

@VisibleForTesting
val cameraPermissionRequest =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { permissionGranted ->
parentFragmentManager.setFragmentResult(
CAMERA_PERMISSION_REQUEST_RESULT_KEY,
bundleOf(CAMERA_PERMISSION_REQUEST_RESULT_KEY to permissionGranted),
)

if (permissionGranted) {
showQrCodeScanner()
dismiss()
} else {
Toast.makeText(
requireContext(),
requireContext().getString(R.string.barcode_camera_permission_denied),
Toast.LENGTH_SHORT,
)
.show()
dismiss()
}
}
Expand All @@ -49,41 +46,16 @@ class QrCodeCameraDialogFragment : DialogFragment(R.layout.fragment_camera_permi

when {
PermissionUtils.checkPermissions(requireContext(), listOf(Manifest.permission.CAMERA)) -> {
showQrCodeScanner()
dismiss()
}
else -> {
cameraPermissionRequest.launch(Manifest.permission.CAMERA)
}
}
}

private fun showQrCodeScanner() =
parentFragmentManager.apply {
setFragmentResultListener(
LiveBarcodeScanningFragment.RESULT_REQUEST_KEY,
requireActivity(),
) { _, result ->
val qrCode = result.getString(LiveBarcodeScanningFragment.RESULT_REQUEST_KEY)?.trim()
this.setFragmentResult(
RESULT_REQUEST_KEY,
bundleOf(RESULT_REQUEST_KEY to qrCode),
)
}

val qrCodeScannerFragment = this.findFragmentByTag(QR_CODE_SCANNER_FRAGMENT_TAG)
if (qrCodeScannerFragment == null) {
LiveBarcodeScanningFragment()
.show(
this@apply,
QR_CODE_SCANNER_FRAGMENT_TAG,
)
}

dismiss()
}

companion object {
const val RESULT_REQUEST_KEY = "qr-code-result"
const val QR_CODE_SCANNER_FRAGMENT_TAG = "QrCodeCameraDialogFragment"
const val CAMERA_PERMISSION_REQUEST_RESULT_KEY =
"quest.ui.sdc.qrCode.CameraPermissionsDialogFragment"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/*
* Copyright 2021-2024 Ona Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.smartregister.fhircore.quest.ui.sdc.qrCode.scan

import android.content.res.Resources
import android.graphics.RectF
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.VisibleForTesting
import androidx.camera.core.ImageAnalysis.COORDINATE_SYSTEM_VIEW_REFERENCED
import androidx.camera.mlkit.vision.MlKitAnalyzer
import androidx.camera.view.LifecycleCameraController
import androidx.camera.view.PreviewView
import androidx.core.content.ContextCompat
import androidx.core.graphics.toRect
import androidx.core.os.bundleOf
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.mlkit.vision.barcode.BarcodeScanner
import com.google.mlkit.vision.barcode.BarcodeScannerOptions
import com.google.mlkit.vision.barcode.BarcodeScanning
import com.google.mlkit.vision.barcode.common.Barcode
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import org.smartregister.fhircore.engine.util.location.PermissionUtils
import org.smartregister.fhircore.quest.R
import org.smartregister.fhircore.quest.ui.sdc.qrCode.CameraPermissionsDialogFragment

internal class QRCodeScannerDialogFragment :
BottomSheetDialogFragment(R.layout.fragment_qr_code_scan) {

private lateinit var cameraExecutor: ExecutorService
private lateinit var cameraController: LifecycleCameraController
private lateinit var mlKitImageAnalyzer: MlKitAnalyzer
private val barcodeScanner: BarcodeScanner by lazy {
val options = BarcodeScannerOptions.Builder().setBarcodeFormats(Barcode.FORMAT_QR_CODE).build()
BarcodeScanning.getClient(options)
}

private lateinit var previewView: PreviewView
private lateinit var cancelScanButton: ImageButton
private lateinit var viewFinderImageView: ImageView
private lateinit var placeQrCodeScanTextView: TextView

@VisibleForTesting
val viewFinderBounds: RectF
get() {
val viewFinderImageViewHeight = viewFinderImageView.height
val viewFinderImageViewWidth = viewFinderImageView.width
return RectF(
viewFinderImageView.x,
viewFinderImageView.y,
viewFinderImageView.x + viewFinderImageViewWidth + 10,
viewFinderImageView.y + viewFinderImageViewHeight + 10,
)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

previewView = view.findViewById(R.id.previewView)
viewFinderImageView = view.findViewById(R.id.viewFinderImageView)
placeQrCodeScanTextView = view.findViewById(R.id.placeQrCodeScanTextView)
cancelScanButton = view.findViewById(R.id.cancelImageButton)
cancelScanButton.setOnClickListener { dismiss() }

val parent = view.parent as View
val behavior = BottomSheetBehavior.from(parent)
val layoutParams = parent.layoutParams
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
parent.layoutParams = layoutParams
behavior.maxHeight = (0.8 * Resources.getSystem().displayMetrics.heightPixels).toInt()
behavior.peekHeight = (0.8 * Resources.getSystem().displayMetrics.heightPixels).toInt()
behavior.state = BottomSheetBehavior.STATE_EXPANDED
behavior.isDraggable = false

cameraExecutor = Executors.newSingleThreadExecutor()
cameraController = LifecycleCameraController(requireContext())
mlKitImageAnalyzer =
MlKitAnalyzer(
listOf(barcodeScanner),
COORDINATE_SYSTEM_VIEW_REFERENCED,
ContextCompat.getMainExecutor(requireActivity()),
) { result: MlKitAnalyzer.Result? ->
val barcodeResults = result?.getValue(barcodeScanner)
if (
(barcodeResults == null) || (barcodeResults.size == 0) || (barcodeResults.first() == null)
) {
return@MlKitAnalyzer
}

onQrCodeDetected(barcodeResults[0])
}

cameraController.setImageAnalysisAnalyzer(
ContextCompat.getMainExecutor(requireActivity()),
mlKitImageAnalyzer,
)

parentFragmentManager.setFragmentResultListener(
CameraPermissionsDialogFragment.CAMERA_PERMISSION_REQUEST_RESULT_KEY,
this,
) { _, result ->
val permissionGranted =
result.getBoolean(CameraPermissionsDialogFragment.CAMERA_PERMISSION_REQUEST_RESULT_KEY)
if (!permissionGranted) {
Toast.makeText(
requireActivity(),
requireContext().getString(R.string.barcode_camera_permission_denied),
Toast.LENGTH_SHORT,
)
.show()
dismiss()
}
}
}

override fun onResume() {
super.onResume()

if (
!PermissionUtils.checkPermissions(
requireContext(),
listOf(android.Manifest.permission.CAMERA),
)
) {
requestCameraPermissions()
} else {
bindCamera()
}
}

private fun requestCameraPermissions() {
CameraPermissionsDialogFragment().show(parentFragmentManager, TAG)
}

private fun bindCamera() {
cameraController.bindToLifecycle(this)
previewView.controller = cameraController
}

private fun isBarcodeWithinExpectedBounds(barcode: Barcode): Boolean {
return barcode.boundingBox?.let { viewFinderBounds.toRect().contains(it) } ?: false
}

@VisibleForTesting
fun onQrCodeDetected(barcode: Barcode) {
if (isBarcodeWithinExpectedBounds(barcode)) {
parentFragmentManager.setFragmentResult(
RESULT_REQUEST_KEY,
bundleOf(RESULT_REQUEST_KEY to barcode.rawValue),
)
dismiss()
} else {
placeQrCodeScanTextView.visibility = View.VISIBLE
}
}

override fun onDestroyView() {
super.onDestroyView()
cameraExecutor.shutdown()
barcodeScanner.close()
}

companion object {
private const val TAG = "QRCodeScannerDialogFragment"
const val RESULT_REQUEST_KEY = "quest.ui.sdc.qrCode.scan.QRCodeScannerDialogFragment"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.smartregister.fhircore.quest.util

import androidx.fragment.app.FragmentActivity
import org.smartregister.fhircore.quest.ui.sdc.qrCode.QrCodeCameraDialogFragment
import org.smartregister.fhircore.quest.ui.sdc.qrCode.scan.QRCodeScannerDialogFragment

object QrCodeScanUtils {

Expand All @@ -26,14 +26,14 @@ object QrCodeScanUtils {
fun scanQrCode(lifecycleOwner: FragmentActivity, onQrCodeScanResult: (String?) -> Unit) {
lifecycleOwner.supportFragmentManager.apply {
setFragmentResultListener(
QrCodeCameraDialogFragment.RESULT_REQUEST_KEY,
QRCodeScannerDialogFragment.RESULT_REQUEST_KEY,
lifecycleOwner,
) { _, result ->
val barcode = result.getString(QrCodeCameraDialogFragment.RESULT_REQUEST_KEY)
val barcode = result.getString(QRCodeScannerDialogFragment.RESULT_REQUEST_KEY)
onQrCodeScanResult.invoke(barcode)
}

QrCodeCameraDialogFragment().show(this@apply, QR_CODE_SCAN_UTILS_TAG)
QRCodeScannerDialogFragment().show(this@apply, QR_CODE_SCAN_UTILS_TAG)
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 70ceb20

Please sign in to comment.