Skip to content

Commit

Permalink
Merge pull request #173 from G00fY2/feature/keep-screen-on
Browse files Browse the repository at this point in the history
Feature/keep screen on
  • Loading branch information
G00fY2 authored May 5, 2024
2 parents e00ebc5 + be1331d commit c0b8820
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ internal class ParcelableScannerConfig(
val horizontalFrameRatio: Float,
val useFrontCamera: Boolean,
val showCloseButton: Boolean,
val keepScreenOn: Boolean,
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -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,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ internal fun ScannerConfig.toParcelableConfig() =
horizontalFrameRatio = horizontalFrameRatio,
useFrontCamera = useFrontCamera,
showCloseButton = showCloseButton,
keepScreenOn = keepScreenOn,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)
}
Expand Down

0 comments on commit c0b8820

Please sign in to comment.