|
|
@@ -0,0 +1,127 @@
|
|
|
+package xn.hxp.receiver
|
|
|
+
|
|
|
+import android.content.Context
|
|
|
+import android.content.SharedPreferences
|
|
|
+import android.view.InputDevice
|
|
|
+import android.view.KeyEvent
|
|
|
+import com.blankj.utilcode.util.LogUtils
|
|
|
+
|
|
|
+/**
|
|
|
+ * 外置超高频RFID读卡器管理器(单例)
|
|
|
+ * 负责:设备学习、开关管理、白名单管理、设备识别
|
|
|
+ */
|
|
|
+object UhfRfidManager {
|
|
|
+
|
|
|
+ private const val PREF_NAME = "uhf_rfid_config"
|
|
|
+ private const val KEY_ENABLED = "uhf_enabled"
|
|
|
+ private const val KEY_DEVICE_VENDOR_ID = "uhf_vendor_id"
|
|
|
+ private const val KEY_DEVICE_PRODUCT_ID = "uhf_product_id"
|
|
|
+ private const val KEY_DEVICE_NAME = "uhf_device_name"
|
|
|
+ private const val KEY_WHITELIST = "uhf_whitelist"
|
|
|
+
|
|
|
+ // 白名单业务标识
|
|
|
+ const val PAGE_STORAGE_RFID = "storage_rfid" // 存储模块RFID绑定
|
|
|
+ const val PAGE_LABEL_RFID = "label_rfid" // 标签管理RFID绑定/更换
|
|
|
+ const val PAGE_INQUIRY = "inquiry" // 查询模块
|
|
|
+
|
|
|
+ // 学习模式
|
|
|
+ var isLearning = false
|
|
|
+ var learningCallback: ((deviceName: String) -> Unit)? = null
|
|
|
+
|
|
|
+ private fun getPrefs(context: Context): SharedPreferences {
|
|
|
+ return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun isEnabled(context: Context): Boolean {
|
|
|
+ return getPrefs(context).getBoolean(KEY_ENABLED, false)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun setEnabled(context: Context, enabled: Boolean) {
|
|
|
+ getPrefs(context).edit().putBoolean(KEY_ENABLED, enabled).apply()
|
|
|
+ }
|
|
|
+
|
|
|
+ fun getDeviceVendorId(context: Context): Int {
|
|
|
+ return getPrefs(context).getInt(KEY_DEVICE_VENDOR_ID, -1)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun getDeviceProductId(context: Context): Int {
|
|
|
+ return getPrefs(context).getInt(KEY_DEVICE_PRODUCT_ID, -1)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun getDeviceName(context: Context): String? {
|
|
|
+ return getPrefs(context).getString(KEY_DEVICE_NAME, null)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun saveDeviceInfo(context: Context, vendorId: Int, productId: Int, deviceName: String) {
|
|
|
+ getPrefs(context).edit()
|
|
|
+ .putInt(KEY_DEVICE_VENDOR_ID, vendorId)
|
|
|
+ .putInt(KEY_DEVICE_PRODUCT_ID, productId)
|
|
|
+ .putString(KEY_DEVICE_NAME, deviceName)
|
|
|
+ .apply()
|
|
|
+ LogUtils.i("UhfRfidManager: 学习设备成功 vendorId=$vendorId productId=$productId name=$deviceName")
|
|
|
+ }
|
|
|
+
|
|
|
+ fun hasLearnedDevice(context: Context): Boolean {
|
|
|
+ return getDeviceVendorId(context) != -1 && getDeviceProductId(context) != -1
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取白名单集合
|
|
|
+ */
|
|
|
+ fun getWhitelist(context: Context): Set<String> {
|
|
|
+ val str = getPrefs(context).getString(KEY_WHITELIST, "") ?: ""
|
|
|
+ if (str.isEmpty()) return emptySet()
|
|
|
+ return str.split(",").toSet()
|
|
|
+ }
|
|
|
+
|
|
|
+ fun saveWhitelist(context: Context, whitelist: Set<String>) {
|
|
|
+ getPrefs(context).edit().putString(KEY_WHITELIST, whitelist.joinToString(",")).apply()
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 开始学习模式,等待外置读卡器刷卡
|
|
|
+ */
|
|
|
+ fun startLearning(callback: (deviceName: String) -> Unit) {
|
|
|
+ isLearning = true
|
|
|
+ learningCallback = callback
|
|
|
+ }
|
|
|
+
|
|
|
+ fun isPageWhitelisted(context: Context, pageTag: String): Boolean {
|
|
|
+ return getWhitelist(context).contains(pageTag)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断KeyEvent是否来自已学习的外置超高频RFID读卡器
|
|
|
+ */
|
|
|
+ fun isUhfRfidEvent(context: Context, event: KeyEvent): Boolean {
|
|
|
+ if (!isEnabled(context)) return false
|
|
|
+ if (!hasLearnedDevice(context)) return false
|
|
|
+ val device = event.device ?: return false
|
|
|
+ val vendorId = getDeviceVendorId(context)
|
|
|
+ val productId = getDeviceProductId(context)
|
|
|
+ return device.vendorId == vendorId && device.productId == productId
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 学习模式下处理KeyEvent,记录设备信息
|
|
|
+ * 返回true表示已消费该事件
|
|
|
+ */
|
|
|
+ fun handleLearningEvent(context: Context, event: KeyEvent): Boolean {
|
|
|
+ if (!isLearning) return false
|
|
|
+ val device = event.device ?: return false
|
|
|
+ // 只在ACTION_DOWN时处理,避免重复
|
|
|
+ if (event.action != KeyEvent.ACTION_DOWN) return true
|
|
|
+ // 忽略内置虚拟键盘设备
|
|
|
+ if (!device.isExternal) return false
|
|
|
+
|
|
|
+ val vendorId = device.vendorId
|
|
|
+ val productId = device.productId
|
|
|
+ val deviceName = device.name ?: "Unknown"
|
|
|
+
|
|
|
+ saveDeviceInfo(context, vendorId, productId, deviceName)
|
|
|
+ isLearning = false
|
|
|
+ learningCallback?.invoke(deviceName)
|
|
|
+ learningCallback = null
|
|
|
+ return true
|
|
|
+ }
|
|
|
+}
|