Skip to content

Commit be1331d

Browse files
committed
Allow setting KeepScreenOn window flag
1 parent e00ebc5 commit be1331d

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ override fun onCreate(savedInstanceState: Bundle?) {
116116
setShowCloseButton(true) // show or hide (default) close button
117117
setHorizontalFrameRatio(2.2f) // set the horizontal overlay ratio (default is 1 / square frame)
118118
setUseFrontCamera(true) // use the front camera
119+
setKeepScreenOn(true) // keep the device's screen turned on
119120
}
120121
)
121122
}

quickie/src/main/kotlin/io/github/g00fy2/quickie/QRScannerActivity.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.util.Size
1010
import android.view.HapticFeedbackConstants
1111
import android.view.KeyEvent
1212
import android.view.View
13+
import android.view.WindowManager
1314
import androidx.activity.result.contract.ActivityResultContracts
1415
import androidx.appcompat.app.AppCompatActivity
1516
import androidx.appcompat.view.ContextThemeWrapper
@@ -195,6 +196,8 @@ internal class QRScannerActivity : AppCompatActivity() {
195196
showTorchToggle = it.showTorchToggle
196197
useFrontCamera = it.useFrontCamera
197198
showCloseButton = it.showCloseButton
199+
200+
if (it.keepScreenOn) window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
198201
}
199202
}
200203

quickie/src/main/kotlin/io/github/g00fy2/quickie/config/ParcelableScannerConfig.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ internal class ParcelableScannerConfig(
1313
val horizontalFrameRatio: Float,
1414
val useFrontCamera: Boolean,
1515
val showCloseButton: Boolean,
16+
val keepScreenOn: Boolean,
1617
) : Parcelable

quickie/src/main/kotlin/io/github/g00fy2/quickie/config/ScannerConfig.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class ScannerConfig internal constructor(
1616
internal val horizontalFrameRatio: Float,
1717
internal val useFrontCamera: Boolean,
1818
internal val showCloseButton: Boolean,
19+
internal val keepScreenOn: Boolean,
1920
) {
2021

2122
public class Builder {
@@ -27,6 +28,7 @@ public class ScannerConfig internal constructor(
2728
private var horizontalFrameRatio: Float = 1f
2829
private var useFrontCamera: Boolean = false
2930
private var showCloseButton: Boolean = false
31+
private var keepScreenOn: Boolean = false
3032

3133
/**
3234
* Set a list of interested barcode formats. List must not be empty.
@@ -71,19 +73,25 @@ public class ScannerConfig internal constructor(
7173
*/
7274
public fun setShowCloseButton(enable: Boolean): Builder = apply { showCloseButton = enable }
7375

76+
/**
77+
* Keep the device's screen turned on as long as the scanner is visible.
78+
*/
79+
public fun setKeepScreenOn(enable: Boolean): Builder = apply { keepScreenOn = enable }
80+
7481
/**
7582
* Build the BarcodeConfig required by the ScanBarcode ActivityResultContract.
7683
*/
7784
public fun build(): ScannerConfig =
7885
ScannerConfig(
79-
barcodeFormats.map { it.value }.toIntArray(),
80-
overlayStringRes,
81-
overlayDrawableRes,
82-
hapticSuccessFeedback,
83-
showTorchToggle,
84-
horizontalFrameRatio,
85-
useFrontCamera,
86-
showCloseButton,
86+
formats = barcodeFormats.map { it.value }.toIntArray(),
87+
stringRes = overlayStringRes,
88+
drawableRes = overlayDrawableRes,
89+
hapticFeedback = hapticSuccessFeedback,
90+
showTorchToggle = showTorchToggle,
91+
horizontalFrameRatio = horizontalFrameRatio,
92+
useFrontCamera = useFrontCamera,
93+
showCloseButton = showCloseButton,
94+
keepScreenOn = keepScreenOn,
8795
)
8896
}
8997

quickie/src/main/kotlin/io/github/g00fy2/quickie/extensions/ScannerConfigExtensions.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ internal fun ScannerConfig.toParcelableConfig() =
1313
horizontalFrameRatio = horizontalFrameRatio,
1414
useFrontCamera = useFrontCamera,
1515
showCloseButton = showCloseButton,
16+
keepScreenOn = keepScreenOn,
1617
)

sample/src/main/kotlin/io/github/g00fy2/quickiesample/MainActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class MainActivity : AppCompatActivity() {
4949
setShowCloseButton(true) // show or hide (default) close button
5050
setHorizontalFrameRatio(2.2f) // set the horizontal overlay ratio (default is 1 / square frame)
5151
setUseFrontCamera(false) // use the front camera
52+
setKeepScreenOn(true) // keep the device's screen turned on
5253
}
5354
)
5455
}

0 commit comments

Comments
 (0)