Skip to content

Commit

Permalink
Merge branch 'release/1.1.2' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
G00fY2 committed Jun 14, 2021
2 parents 30e800c + e46480a commit 3613d96
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 39 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ There are two different flavors available on `mavenCentral()`:
| V2 model is used (possibly faster, more accurate) | currently V1 model will be downloaded
```kotlin
// bundled:
implementation("io.github.g00fy2.quickie:quickie-bundled:1.1.1")
implementation("io.github.g00fy2.quickie:quickie-bundled:1.1.2")

// unbundled:
implementation("io.github.g00fy2.quickie:quickie-unbundled:1.1.1")
implementation("io.github.g00fy2.quickie:quickie-unbundled:1.1.2")
```

## Quick Start
Expand Down Expand Up @@ -61,7 +61,7 @@ Currently, supported subtypes are:
See the ML Kit [Barcode documentation](https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode#nested-class-summary) for further details.

### Customization
Use the `ScanCustomCode()` ActivityResultContract to create a configurable barcode scan. When launching the ActivityResultLauncher pass in a `ScannerConfig` object. You can set the supported `BarcodeFormat` list, `overlayStringRes` and `overlayDrawableRes` resource ID.
Use the `ScanCustomCode()` ActivityResultContract to create a configurable barcode scan. When launching the ActivityResultLauncher pass in a `ScannerConfig` object. You can set the supported `BarcodeFormat` list, `overlayStringRes` and `overlayDrawableRes` resource ID and control the `hapticSuccessFeedback`.

<details>
<summary>BarcodeFormat options</summary>
Expand Down Expand Up @@ -96,6 +96,7 @@ override fun onCreate(savedInstanceState: Bundle?) {
setBarcodeFormats(listOf(BarcodeFormat.FORMAT_CODE_128))
setOverlayStringRes(R.string.scan_barcode)
setOverlayDrawableRes(R.drawable.ic_scan_barcode)
setHapticSuccessFeedback(false)
}
)
}
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object Versions {
const val appcompat = "1.3.0"

const val cameraX = "1.0.0"
const val cameraView = "1.0.0-alpha24"
const val cameraView = "1.0.0-alpha25"

const val materialDesign = "1.3.0"

Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ case "`uname`" in
Darwin* )
darwin=true
;;
MINGW* )
MSYS* | MINGW* )
msys=true
;;
NONSTOP* )
Expand Down
2 changes: 1 addition & 1 deletion quickie/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dependencies {
}

group = "io.github.g00fy2.quickie"
version = "1.1.1"
version = "1.1.2"

tasks.register<Jar>("androidJavadocJar") {
archiveClassifier.set("javadoc")
Expand Down
27 changes: 12 additions & 15 deletions quickie/src/main/kotlin/io/github/g00fy2/quickie/QROverlayView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.github.g00fy2.quickie
import android.content.Context
import android.content.res.Resources.NotFoundException
import android.graphics.Bitmap
import android.graphics.Bitmap.Config.ARGB_8888
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
Expand All @@ -16,7 +15,6 @@ import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.ColorUtils
Expand All @@ -39,14 +37,14 @@ internal class QROverlayView @JvmOverloads constructor(
color = Color.TRANSPARENT
xfermode = PorterDuffXfermode(CLEAR)
}
private val radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, OUT_RADIUS, resources.displayMetrics)
private val innerRadius =
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, OUT_RADIUS - STROKE_WIDTH, resources.displayMetrics)
private val titleTextView: AppCompatTextView
private val outerRadius = OUT_RADIUS.toPx()
private val innerRadius = (OUT_RADIUS - STROKE_WIDTH).toPx()
private val titleTextView = QuickieTextviewBinding.inflate(LayoutInflater.from(context), this, true).root
private val outerFrame = RectF()
private val innerFrame = RectF()
private var maskBitmap: Bitmap? = null
private var maskCanvas: Canvas? = null
private var outerFrame = RectF()
private var innerFrame = RectF()

var isHighlighted = false
set(value) {
field = value
Expand All @@ -55,24 +53,21 @@ internal class QROverlayView @JvmOverloads constructor(

init {
setWillNotDraw(false)
titleTextView = QuickieTextviewBinding.inflate(LayoutInflater.from(context), this, true).root
}

override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
super.onLayout(changed, left, top, right, bottom)

if (maskBitmap == null && width > 0 && height > 0) {
maskBitmap = Bitmap.createBitmap(width, height, ARGB_8888).apply {
maskCanvas = Canvas(this)
}
maskBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).apply { maskCanvas = Canvas(this) }
calculateFrameAndTitlePos()
}
}

override fun onDraw(canvas: Canvas) {
strokePaint.color = if (isHighlighted) highlightedStrokeColor else strokeColor
maskCanvas!!.drawColor(backgroundColor)
maskCanvas!!.drawRoundRect(outerFrame, radius, radius, strokePaint)
maskCanvas!!.drawRoundRect(outerFrame, outerRadius, outerRadius, strokePaint)
maskCanvas!!.drawRoundRect(innerFrame, innerRadius, innerRadius, transparentPaint)
canvas.drawBitmap(maskBitmap!!, 0f, 0f, alphaPaint)
super.onDraw(canvas)
Expand Down Expand Up @@ -102,7 +97,7 @@ internal class QROverlayView @JvmOverloads constructor(
val centralY = height / 2
val minLength = min(centralX, centralY)
val strokeLength = minLength - (minLength * FRAME_MARGIN_RATIO)
val strokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, STROKE_WIDTH, resources.displayMetrics)
val strokeWidth = STROKE_WIDTH.toPx()
outerFrame.set(
centralX - strokeLength,
centralY - strokeLength,
Expand Down Expand Up @@ -136,7 +131,7 @@ internal class QROverlayView @JvmOverloads constructor(
}

private fun Drawable.limitDrawableSize(): Drawable {
val heightLimit = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ICON_MAX_HEIGHT, resources.displayMetrics)
val heightLimit = ICON_MAX_HEIGHT.toPx()
val scale = heightLimit / minimumHeight
if (scale < 1) {
setBounds(0, 0, (minimumWidth * scale).roundToInt(), (minimumHeight * scale).roundToInt())
Expand All @@ -146,6 +141,8 @@ internal class QROverlayView @JvmOverloads constructor(
return this
}

private fun Float.toPx() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this, resources.displayMetrics)

companion object {
private const val BACKGROUND_ALPHA = 0.77 * 255
private const val STROKE_WIDTH = 4f
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.g00fy2.quickie

import android.Manifest
import android.Manifest.permission.CAMERA
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
Expand Down Expand Up @@ -32,6 +32,7 @@ internal class QRScannerActivity : AppCompatActivity() {
private lateinit var binding: QuickieScannerActivityBinding
private lateinit var analysisExecutor: ExecutorService
private var barcodeFormats = intArrayOf(Barcode.FORMAT_QR_CODE)
private var hapticFeedback = true

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -99,10 +100,12 @@ internal class QRScannerActivity : AppCompatActivity() {

private fun onSuccess(result: Barcode) {
binding.overlayView.isHighlighted = true
binding.overlayView.performHapticFeedback(
HapticFeedbackConstants.KEYBOARD_TAP,
HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING or HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING
)
if (hapticFeedback) {
binding.overlayView.performHapticFeedback(
HapticFeedbackConstants.KEYBOARD_TAP,
HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING or HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING
)
}
setResult(
Activity.RESULT_OK,
Intent().apply {
Expand All @@ -122,9 +125,7 @@ internal class QRScannerActivity : AppCompatActivity() {
private fun setupEdgeToEdgeUI() {
WindowCompat.setDecorFitsSystemWindows(window, false)
ViewCompat.setOnApplyWindowInsetsListener(binding.overlayView) { v, insets ->
insets.getInsets(WindowInsetsCompat.Type.systemBars()).let {
v.setPadding(it.left, it.top, it.right, it.bottom)
}
insets.getInsets(WindowInsetsCompat.Type.systemBars()).let { v.setPadding(it.left, it.top, it.right, it.bottom) }
WindowInsetsCompat.CONSUMED
}
}
Expand All @@ -133,17 +134,15 @@ internal class QRScannerActivity : AppCompatActivity() {
intent?.getParcelableExtra<ParcelableScannerConfig>(EXTRA_CONFIG)?.let {
barcodeFormats = it.formats
binding.overlayView.setCustomTextAndIcon(it.stringRes, it.drawableRes)
hapticFeedback = it.hapticFeedback
}
}

private fun requestCameraPermissionIfMissing(onResult: ((Boolean) -> Unit)) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, CAMERA) == PackageManager.PERMISSION_GRANTED) {
onResult(true)
} else {
// register the activity result here is allowed since we call this in onCreate only
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
onResult(it)
}.launch(Manifest.permission.CAMERA)
registerForActivityResult(ActivityResultContracts.RequestPermission()) { onResult(it) }.launch(CAMERA)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
internal class ParcelableScannerConfig(val formats: IntArray, val stringRes: Int, val drawableRes: Int) : Parcelable
internal class ParcelableScannerConfig(
val formats: IntArray,
val stringRes: Int,
val drawableRes: Int,
val hapticFeedback: Boolean
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import androidx.annotation.StringRes
public class ScannerConfig internal constructor(
internal val formats: IntArray,
internal val stringRes: Int,
internal val drawableRes: Int
internal val drawableRes: Int,
internal val hapticFeedback: Boolean,
) {

public class Builder {
private var barcodeFormats: List<BarcodeFormat> = listOf(BarcodeFormat.FORMAT_ALL_FORMATS)
private var overlayStringRes: Int = 0
private var overlayDrawableRes: Int = 0
private var hapticSuccessFeedback: Boolean = true

/**
* Set a list of interested barcode formats. List must not be empty.
Expand All @@ -34,11 +36,21 @@ public class ScannerConfig internal constructor(
public fun setOverlayDrawableRes(@DrawableRes drawableRes: Int): Builder =
apply { overlayDrawableRes = drawableRes }

/**
* Enable (default) or disable haptic feedback when a barcode code was detected.
*/
public fun setHapticSuccessFeedback(enable: Boolean): Builder = apply { hapticSuccessFeedback = enable }

/**
* Build the BarcodeConfig required by the ScanBarcode ActivityResultContract.
*/
public fun build(): ScannerConfig =
ScannerConfig(barcodeFormats.map { it.value }.toIntArray(), overlayStringRes, overlayDrawableRes)
ScannerConfig(
barcodeFormats.map { it.value }.toIntArray(),
overlayStringRes,
overlayDrawableRes,
hapticSuccessFeedback
)
}

public companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ import io.github.g00fy2.quickie.config.ParcelableScannerConfig
import io.github.g00fy2.quickie.config.ScannerConfig

internal fun ScannerConfig.toParcelableConfig() =
ParcelableScannerConfig(formats = formats, stringRes = stringRes, drawableRes = drawableRes)
ParcelableScannerConfig(
formats = formats,
stringRes = stringRes,
drawableRes = drawableRes,
hapticFeedback = hapticFeedback
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class MainActivity : AppCompatActivity() {
setBarcodeFormats(listOf(selectedBarcodeFormat))
setOverlayStringRes(R.string.scan_barcode)
setOverlayDrawableRes(R.drawable.ic_scan_barcode)
setHapticSuccessFeedback(false)
}
)
}
Expand Down

0 comments on commit 3613d96

Please sign in to comment.