Skip to content

Commit

Permalink
Add support for HID qr code scanners to the pretixSCAN setup
Browse files Browse the repository at this point in the history
  • Loading branch information
robbi5 committed Oct 2, 2024
1 parent 259b898 commit 839eccc
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.text.Editable
import android.util.Log
import android.view.KeyEvent
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import eu.pretix.libpretixsync.setup.*
Expand All @@ -31,13 +32,17 @@ import org.json.JSONObject
import java.io.IOException
import java.lang.Exception
import javax.net.ssl.SSLException
import android.text.method.TextKeyListener.Capitalize
import android.text.method.TextKeyListener

class SetupActivity : AppCompatActivity(), ScannerView.ResultHandler {
lateinit var binding: ActivitySetupBinding
val bgScope = CoroutineScope(Dispatchers.IO)
var lastScanTime = 0L
var lastScanValue = ""
var conf: AppConfig? = null
var tkl = TextKeyListener(Capitalize.NONE, false)
var keyboardEditable = Editable.Factory.getInstance().newEditable("")
private var ongoing_setup = false
private val dataWedgeHelper = DataWedgeHelper(this)
private val LOG_TAG = this::class.java.name
Expand Down Expand Up @@ -128,6 +133,25 @@ class SetupActivity : AppCompatActivity(), ScannerView.ResultHandler {
handleScan(rawResult.text)
}

override fun dispatchKeyEvent(event: KeyEvent): Boolean {
if (event.keyCode == KeyEvent.KEYCODE_ENTER) {
if (event.action == KeyEvent.ACTION_UP) {
handleScan(keyboardEditable.toString())
keyboardEditable.clear()
}
return true
}
val processed = when (event.action) {
KeyEvent.ACTION_DOWN -> tkl.onKeyDown(null, keyboardEditable, event.keyCode, event)
KeyEvent.ACTION_UP -> tkl.onKeyUp(null, keyboardEditable, event.keyCode, event)
else -> tkl.onKeyOther(null, keyboardEditable, event)
}
if (processed) {
return true
}
return super.dispatchKeyEvent(event)
}

fun handleScan(res: String) {
try {
val jd = JSONObject(res)
Expand Down

0 comments on commit 839eccc

Please sign in to comment.