UsageLabelDialog.kt 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package xn.hxp.weidith
  2. import android.app.Activity
  3. import android.app.Dialog
  4. import android.content.Context
  5. import android.content.IntentFilter
  6. import android.graphics.Color
  7. import android.graphics.drawable.ColorDrawable
  8. import android.hardware.usb.UsbManager
  9. import android.os.Bundle
  10. import android.os.Handler
  11. import android.os.Looper
  12. import android.os.Message
  13. import android.util.Log
  14. import android.view.*
  15. import android.widget.*
  16. import androidx.recyclerview.widget.LinearLayoutManager
  17. import androidx.recyclerview.widget.RecyclerView
  18. import com.blankj.utilcode.util.LogUtils
  19. import xn.hxp.R
  20. import xn.hxp.comm.Constants
  21. import xn.hxp.receiver.OnSerialScanListener
  22. import xn.hxp.receiver.PortScanHelper
  23. import xn.hxp.receiver.UsbReceiver
  24. import xn.hxp.ui.adapter.UsageLabelDialogAdapter
  25. import xn.hxp.utils.BigDecimalUtils
  26. import com.rc.httpcore.bean.ChemistryBean
  27. import org.greenrobot.eventbus.EventBus
  28. import org.greenrobot.eventbus.Subscribe
  29. import org.greenrobot.eventbus.ThreadMode
  30. //领用-请扫描化学品标签
  31. class UsageLabelDialog(
  32. private val ac: Activity,
  33. private var isLcok: Boolean,
  34. private var cabinet: String,
  35. private val lint: IIneSwipeLin
  36. ) : Dialog(ac) {
  37. private var mUsbReceiver: UsbReceiver? = null // 刷卡广播注册
  38. private var mHandleScanEvent = false //当前是否已经获取过 usb返回的参数
  39. private val mAdapter by lazy { UsageLabelDialogAdapter(ac) }
  40. private var mCounter = 0
  41. private var butClos: Button? = null
  42. override fun onStart() {
  43. super.onStart()
  44. EventBus.getDefault().register(this)
  45. }
  46. override fun onStop() {
  47. super.onStop()
  48. mPortScanHelper.onPause()
  49. ac.unregisterReceiver(mUsbReceiver)
  50. EventBus.getDefault().unregister(this)
  51. }
  52. init {
  53. requestWindowFeature(Window.FEATURE_NO_TITLE)
  54. setContentView(R.layout.usage_label_dialog)
  55. window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
  56. window?.setGravity(Gravity.CENTER)
  57. window?.setLayout(
  58. WindowManager.LayoutParams.MATCH_PARENT,
  59. WindowManager.LayoutParams.WRAP_CONTENT
  60. )
  61. // 设置点击外部空白处不关闭对话框
  62. setCanceledOnTouchOutside(false)
  63. }
  64. override fun onCreate(savedInstanceState: Bundle?) {
  65. super.onCreate(savedInstanceState)
  66. if (isLcok) {
  67. findViewById<Button>(R.id.butOpen).visibility = View.VISIBLE
  68. } else {
  69. findViewById<Button>(R.id.butOpen).visibility = View.GONE
  70. }
  71. findViewById<TextView>(R.id.cabinet).text = "$cabinet"
  72. findViewById<Button>(R.id.butClos).setOnClickListener {
  73. closHandle()
  74. dismiss()
  75. }
  76. findViewById<Button>(R.id.cloe).setOnClickListener {
  77. closHandle()
  78. lint.onCloses()
  79. dismiss()
  80. }
  81. //领用信息
  82. findViewById<Button>(R.id.useEn).setOnClickListener {
  83. if (mAdapter.data != null && mAdapter.data.size > 0) {
  84. val hasDuplicateCodes = hasDuplicateCodes(mAdapter.data)
  85. LogUtils.i("========hasDuplicateCodes $hasDuplicateCodes")
  86. if (!hasDuplicateCodes) {
  87. closHandle()
  88. lint.onUseClick(mAdapter.data)
  89. dismiss()
  90. } else {
  91. LogUtils.i("===========2")
  92. Toast.makeText(ac as Context, "不能重复领用", Toast.LENGTH_SHORT).show()
  93. }
  94. }
  95. }
  96. findViewById<Button>(R.id.butOpen).setOnClickListener {
  97. closHandle()
  98. dismiss()
  99. lint.openLock()
  100. }
  101. val findViewById = findViewById<RecyclerView>(R.id.reView)
  102. findViewById.layoutManager = LinearLayoutManager(ac)
  103. findViewById.adapter = mAdapter
  104. mPortScanHelper.onResume()
  105. registerUsbBroadcast()
  106. // mCountDownHandler.removeMessages(Constants.WHAT_COUNT_DOWN_TWO)
  107. // mCountDownHandler.sendEmptyMessage(Constants.WHAT_COUNT_DOWN_TWO)
  108. mAdapter.setOnItemChildClickListener { adapter, view, position ->
  109. // 在这里处理子View的点击事件
  110. when (view.id) {
  111. R.id.imgDelete -> {
  112. // mAdapter.data.removeAt(position)
  113. // mAdapter.notifyItemRemoved(position)
  114. lint.onPositions(position, mAdapter)
  115. }
  116. }
  117. }
  118. }
  119. private fun hasDuplicateCodes(list: List<ChemistryBean>): Boolean {
  120. val distinctCodes = list.map { it.code }.distinct()
  121. return distinctCodes.size != list.size
  122. }
  123. interface IIneSwipeLin {
  124. fun onCardInContent(cont: String)//刷卡返回
  125. fun onUseClick(data: MutableList<ChemistryBean>)//点击领用
  126. fun onCloses() //关闭清空数据
  127. fun openLock()//再次开门
  128. fun onPositions(position: Int, mData: UsageLabelDialogAdapter)//下标删除
  129. fun onTimerReply()//重置最外层弹框信息
  130. }
  131. override fun dispatchKeyEvent(event: KeyEvent): Boolean {
  132. if (event.keyCode == KeyEvent.KEYCODE_BACK) {
  133. // 在Dialog弹出时处理返回键事件
  134. // 处理完事件后返回true表示事件已经被消费,不再传递
  135. return true
  136. }
  137. mPortScanHelper.dispatchKeyEvent(event)
  138. return super.dispatchKeyEvent(event)
  139. }
  140. //必须写这个方法 防止注册失败
  141. @Subscribe(threadMode = ThreadMode.MAIN)
  142. fun onUpdateEventEvent(event: KeyEvent) {
  143. }
  144. private val mPortScanHelper by lazy {
  145. PortScanHelper(ac, object : OnSerialScanListener {
  146. override fun dispatchScanEvent(type: OnSerialScanListener.ScanType, content: String) {
  147. lint.onTimerReply()
  148. mCounter = 0
  149. if (!mHandleScanEvent) {
  150. if (content.isNotBlank()) {
  151. LogUtils.i("========a==当前usb返回参数$content")
  152. mHandleScanEvent = true
  153. // handleScanEvent(content)
  154. //所有通过查询二维码的参数 需要先判断 http开头的 然后进行截取
  155. if (content.startsWith("http")) {
  156. try {
  157. val currentDateTimeString =
  158. BigDecimalUtils.extractParameterValue(content, "code")
  159. LogUtils.i("=======ce $currentDateTimeString")
  160. lint.onCardInContent(currentDateTimeString!!)
  161. } catch (e: Exception) {
  162. mHandleScanEvent = false
  163. LogUtils.i("刷卡异常了")
  164. showToast("未获取到二维码信息,请重新扫描", 3000)
  165. }
  166. } else {
  167. lint.onCardInContent(content)
  168. }
  169. }
  170. }
  171. }
  172. })
  173. }
  174. private fun showToast(message: String, duration: Int) {
  175. Toast.makeText(ac, message, duration).show()
  176. }
  177. //刷卡usb链接
  178. private fun registerUsbBroadcast() {
  179. if (null == mUsbReceiver) {
  180. val filter = IntentFilter().apply {
  181. addAction(UsbReceiver.ACTION_USB_PERMISSION)
  182. addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
  183. addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
  184. addAction(UsbReceiver.ACTION_USB_STATE) // usb连接状态广播
  185. }
  186. mUsbReceiver = UsbReceiver()
  187. ac.registerReceiver(mUsbReceiver, filter)
  188. }
  189. }
  190. fun setMessage(message: String, dataList: List<ChemistryBean>) {
  191. LogUtils.i("====================数据过来了")
  192. mHandleScanEvent = false
  193. findViewById<ImageView>(R.id.img).visibility = View.GONE
  194. findViewById<Button>(R.id.butClos).visibility = View.GONE
  195. findViewById<LinearLayout>(R.id.linTow).visibility = View.VISIBLE
  196. findViewById<RecyclerView>(R.id.reView).visibility = View.VISIBLE // 在这里可以设置接收到的消息参数
  197. mAdapter.setNewInstance(dataList.toMutableList())
  198. }
  199. fun notifyData(mChemistryBean: MutableList<ChemistryBean>) {
  200. mHandleScanEvent = false
  201. mAdapter.data = mChemistryBean
  202. mAdapter.notifyDataSetChanged()
  203. }
  204. fun notifyData() {
  205. mHandleScanEvent = false
  206. }
  207. private fun closHandle() {
  208. // mCountDownHandler.removeMessages(Constants.WHAT_COUNT_DOWN_TWO)
  209. // mCountDownHandler.removeCallbacksAndMessages(null)
  210. }
  211. // private val mCountDownHandler = object : Handler(Looper.getMainLooper()) {
  212. //
  213. // override fun handleMessage(msg: Message) {
  214. // if (Constants.WHAT_COUNT_DOWN_TWO == msg.what) {
  215. //
  216. // val countDown = Constants.OFFTIME_TIME - mCounter
  217. //
  218. // Log.d("======返回时间", "$countDown")
  219. // if (countDown <= 0) {
  220. // dismiss()
  221. // } else {
  222. // sendEmptyMessageDelayed(Constants.WHAT_COUNT_DOWN_TWO, 1000)
  223. // }
  224. // mCounter++
  225. // }
  226. // }
  227. // }
  228. override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
  229. // 在触摸事件发生时被调用
  230. when (ev.action) {
  231. MotionEvent.ACTION_DOWN -> {
  232. // 用户按下屏幕时的处理逻辑
  233. onUserInteraction()
  234. }
  235. MotionEvent.ACTION_MOVE -> {
  236. // 用户在屏幕上移动手指时的处理逻辑
  237. }
  238. MotionEvent.ACTION_UP -> {
  239. // 用户抬起手指时的处理逻辑
  240. }
  241. }
  242. return super.dispatchTouchEvent(ev)
  243. }
  244. fun onUserInteraction() {
  245. // 在用户与对话框交互时的逻辑处理
  246. LogUtils.i("=========弹框触发了")
  247. lint.onTimerReply()
  248. mCounter = 0
  249. }
  250. }