From be1331d8b8a2d3b8cd406f677dead05a51606158 Mon Sep 17 00:00:00 2001 From: Thomas Wirth Date: Sun, 5 May 2024 14:45:16 +0200 Subject: [PATCH] Allow setting KeepScreenOn window flag --- README.md | 1 + .../g00fy2/quickie/QRScannerActivity.kt | 3 +++ .../quickie/config/ParcelableScannerConfig.kt | 1 + .../g00fy2/quickie/config/ScannerConfig.kt | 24 ++++++++++++------- .../extensions/ScannerConfigExtensions.kt | 1 + .../g00fy2/quickiesample/MainActivity.kt | 1 + 6 files changed, 23 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d928d6eb..27d1f281 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,7 @@ override fun onCreate(savedInstanceState: Bundle?) { setShowCloseButton(true) // show or hide (default) close button setHorizontalFrameRatio(2.2f) // set the horizontal overlay ratio (default is 1 / square frame) setUseFrontCamera(true) // use the front camera + setKeepScreenOn(true) // keep the device's screen turned on } ) } diff --git a/quickie/src/main/kotlin/io/github/g00fy2/quickie/QRScannerActivity.kt b/quickie/src/main/kotlin/io/github/g00fy2/quickie/QRScannerActivity.kt index eeff7712..79401434 100644 --- a/quickie/src/main/kotlin/io/github/g00fy2/quickie/QRScannerActivity.kt +++ b/quickie/src/main/kotlin/io/github/g00fy2/quickie/QRScannerActivity.kt @@ -10,6 +10,7 @@ import android.util.Size import android.view.HapticFeedbackConstants import android.view.KeyEvent import android.view.View +import android.view.WindowManager import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.view.ContextThemeWrapper @@ -195,6 +196,8 @@ internal class QRScannerActivity : AppCompatActivity() { showTorchToggle = it.showTorchToggle useFrontCamera = it.useFrontCamera showCloseButton = it.showCloseButton + + if (it.keepScreenOn) window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) } } diff --git a/quickie/src/main/kotlin/io/github/g00fy2/quickie/config/ParcelableScannerConfig.kt b/quickie/src/main/kotlin/io/github/g00fy2/quickie/config/ParcelableScannerConfig.kt index 78e1e427..3f9d34b0 100644 --- a/quickie/src/main/kotlin/io/github/g00fy2/quickie/config/ParcelableScannerConfig.kt +++ b/quickie/src/main/kotlin/io/github/g00fy2/quickie/config/ParcelableScannerConfig.kt @@ -13,4 +13,5 @@ internal class ParcelableScannerConfig( val horizontalFrameRatio: Float, val useFrontCamera: Boolean, val showCloseButton: Boolean, + val keepScreenOn: Boolean, ) : Parcelable \ No newline at end of file diff --git a/quickie/src/main/kotlin/io/github/g00fy2/quickie/config/ScannerConfig.kt b/quickie/src/main/kotlin/io/github/g00fy2/quickie/config/ScannerConfig.kt index 03e6bbb5..dc4f2945 100644 --- a/quickie/src/main/kotlin/io/github/g00fy2/quickie/config/ScannerConfig.kt +++ b/quickie/src/main/kotlin/io/github/g00fy2/quickie/config/ScannerConfig.kt @@ -16,6 +16,7 @@ public class ScannerConfig internal constructor( internal val horizontalFrameRatio: Float, internal val useFrontCamera: Boolean, internal val showCloseButton: Boolean, + internal val keepScreenOn: Boolean, ) { public class Builder { @@ -27,6 +28,7 @@ public class ScannerConfig internal constructor( private var horizontalFrameRatio: Float = 1f private var useFrontCamera: Boolean = false private var showCloseButton: Boolean = false + private var keepScreenOn: Boolean = false /** * Set a list of interested barcode formats. List must not be empty. @@ -71,19 +73,25 @@ public class ScannerConfig internal constructor( */ public fun setShowCloseButton(enable: Boolean): Builder = apply { showCloseButton = enable } + /** + * Keep the device's screen turned on as long as the scanner is visible. + */ + public fun setKeepScreenOn(enable: Boolean): Builder = apply { keepScreenOn = enable } + /** * Build the BarcodeConfig required by the ScanBarcode ActivityResultContract. */ public fun build(): ScannerConfig = ScannerConfig( - barcodeFormats.map { it.value }.toIntArray(), - overlayStringRes, - overlayDrawableRes, - hapticSuccessFeedback, - showTorchToggle, - horizontalFrameRatio, - useFrontCamera, - showCloseButton, + formats = barcodeFormats.map { it.value }.toIntArray(), + stringRes = overlayStringRes, + drawableRes = overlayDrawableRes, + hapticFeedback = hapticSuccessFeedback, + showTorchToggle = showTorchToggle, + horizontalFrameRatio = horizontalFrameRatio, + useFrontCamera = useFrontCamera, + showCloseButton = showCloseButton, + keepScreenOn = keepScreenOn, ) } diff --git a/quickie/src/main/kotlin/io/github/g00fy2/quickie/extensions/ScannerConfigExtensions.kt b/quickie/src/main/kotlin/io/github/g00fy2/quickie/extensions/ScannerConfigExtensions.kt index 7d24d8d3..b2cfc837 100644 --- a/quickie/src/main/kotlin/io/github/g00fy2/quickie/extensions/ScannerConfigExtensions.kt +++ b/quickie/src/main/kotlin/io/github/g00fy2/quickie/extensions/ScannerConfigExtensions.kt @@ -13,4 +13,5 @@ internal fun ScannerConfig.toParcelableConfig() = horizontalFrameRatio = horizontalFrameRatio, useFrontCamera = useFrontCamera, showCloseButton = showCloseButton, + keepScreenOn = keepScreenOn, ) \ No newline at end of file diff --git a/sample/src/main/kotlin/io/github/g00fy2/quickiesample/MainActivity.kt b/sample/src/main/kotlin/io/github/g00fy2/quickiesample/MainActivity.kt index 1142eee8..acf21d6a 100644 --- a/sample/src/main/kotlin/io/github/g00fy2/quickiesample/MainActivity.kt +++ b/sample/src/main/kotlin/io/github/g00fy2/quickiesample/MainActivity.kt @@ -49,6 +49,7 @@ class MainActivity : AppCompatActivity() { setShowCloseButton(true) // show or hide (default) close button setHorizontalFrameRatio(2.2f) // set the horizontal overlay ratio (default is 1 / square frame) setUseFrontCamera(false) // use the front camera + setKeepScreenOn(true) // keep the device's screen turned on } ) }