Skip to content

Commit

Permalink
Add ability to auto-accept scanned QR code (closes #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
EbenezerGH committed Jun 7, 2018
1 parent f78ef73 commit c04cf7d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .idea/dictionaries/ackoneb.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,20 @@ class DemoActivity : AppCompatActivity() {
launch.setOnClickListener {
startActivityForResult(Intent(this, ScanQrActivity::class.java).apply {
putExtra(ScanQrActivity.OPTION_SHOW_BARCODE_BOX, BuildConfig.DEBUG)
putExtra(ScanQrActivity.AUTOACCEPT_RESULT, false)
}, REQUEST_SCAN)
}

// Auto-accept scanner
launch_autoaccept.setOnClickListener {
startActivityForResult(Intent(this, ScanQrActivity::class.java).apply {
putExtra(ScanQrActivity.OPTION_SHOW_BARCODE_BOX, BuildConfig.DEBUG)
putExtra(ScanQrActivity.AUTOACCEPT_RESULT, true)
}, REQUEST_SCAN)
}

// Custom scanner
launch_xpub.setOnClickListener {
launch_xpub.setOnClickListener {
startActivityForResult(Intent(this, XPubScannerActivity::class.java).apply {
putExtra(ScanQrActivity.OPTION_SHOW_BARCODE_BOX, BuildConfig.DEBUG)
}, REQUEST_SCAN)
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/res/layout/content_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,24 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/launch_scanner"
app:layout_constraintBottom_toTopOf="@+id/launch_xpub"
app:layout_constraintBottom_toTopOf="@+id/launch_autoaccept"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>


<Button
android:id="@+id/launch_autoaccept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/launch_scanner_auto_accept"
app:layout_constraintBottom_toTopOf="@+id/launch_xpub"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/launch"/>

<Button
android:id="@+id/launch_xpub"
android:layout_width="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<string name="title_activity_scan_xpub">Scan XPub</string>

<string name="launch_scanner">Launch scanner</string>
<string name="launch_scanner_auto_accept">Launch scanner auto-accept</string>
<string name="launch_xpub_scanner">Launch xpub scanner</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@

package io.github.novacrypto.qrscanner

internal class Options(val showBarcodeBounds: Boolean)
internal class Options(val showBarcodeBounds: Boolean,
val autoAcceptResult: Boolean)
18 changes: 12 additions & 6 deletions qrScanner/src/main/java/io/github/novacrypto/qrscanner/QrCamera.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,21 @@ internal class QrCamera(
if (value == field) return
field = value
value?.let {
Snackbar.make(cameraSurfaceView, it, Snackbar.LENGTH_INDEFINITE)
.setAction(android.R.string.ok, { _ ->
Timber.d("Accepted barcode")
onAccept(it)
})
.show()
if (options.autoAcceptResult) {
Timber.d("Accepted barcode automatically")
onAccept(it)
} else {
Snackbar.make(cameraSurfaceView, it, Snackbar.LENGTH_INDEFINITE)
.setAction(android.R.string.ok, { _ ->
Timber.d("Accepted barcode")
onAccept(it)
})
.show()
}
}
}


fun start() {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
cameraSurfaceView.start(cameraSource, options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ open class ScanQrActivity : AppCompatActivity() {
companion object {
const val OPTION_SHOW_BARCODE_BOX = "SHOW_BARCODE_BOX"
const val BARCODE_DATA = "BARCODE_DATA"
const val AUTOACCEPT_RESULT = "AUTOACCEPT_RESULT"
internal const val CAMERA_PERMISSION_RESPONSE = 23
}

Expand Down Expand Up @@ -75,8 +76,10 @@ open class ScanQrActivity : AppCompatActivity() {
super.onDestroy()
}

private fun Intent?.toOptions() =
Options(this?.extras?.getBoolean(OPTION_SHOW_BARCODE_BOX) ?: false)
private fun Intent?.toOptions() = Options(
this?.extras?.getBoolean(OPTION_SHOW_BARCODE_BOX) ?: false,
this?.extras?.getBoolean(AUTOACCEPT_RESULT) ?: false)


override fun onRequestPermissionsResult(
requestCode: Int,
Expand Down

0 comments on commit c04cf7d

Please sign in to comment.