186 lines
5.6 KiB
Kotlin
186 lines
5.6 KiB
Kotlin
package com.sw.dualscreen.activity
|
|
|
|
import android.content.Context
|
|
import android.graphics.drawable.ColorDrawable
|
|
import android.hardware.display.DisplayManager
|
|
import android.os.Bundle
|
|
import android.util.Log
|
|
import android.view.Display
|
|
import android.view.KeyEvent
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import android.view.WindowManager
|
|
import android.widget.TextView
|
|
import androidx.activity.enableEdgeToEdge
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import androidx.viewbinding.ViewBinding
|
|
import com.sw.dualscreen.R
|
|
import com.sw.dualscreen.databinding.DialogWaitingBinding
|
|
import com.sw.dualscreen.model.IActivityResult
|
|
import com.sw.dualscreen.model.response.PostEvent
|
|
import com.sw.dualscreen.utils.FileUtil
|
|
import com.sw.dualscreen.view.CustomDialog
|
|
import com.sw.dualscreen.viewmodel.BaseViewModel
|
|
import com.sw.plate.utils.ScanGunKeyEventHelper
|
|
import com.sw.plate.utils.ToastUtils
|
|
import org.greenrobot.eventbus.EventBus
|
|
import org.greenrobot.eventbus.Subscribe
|
|
import org.greenrobot.eventbus.ThreadMode
|
|
import timber.log.Timber
|
|
|
|
abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
|
|
|
|
val displays: Array<Display> by lazy {
|
|
val displayManager = getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
|
|
displayManager.displays
|
|
}
|
|
|
|
protected lateinit var binding: VB
|
|
protected lateinit var context: Context
|
|
private var mDialogWaiting: CustomDialog? = null
|
|
private lateinit var viewModel: BaseViewModel
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
EventBus.getDefault().register(this)
|
|
enableEdgeToEdge()
|
|
context = this
|
|
|
|
//保持亮屏
|
|
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
|
|
|
binding = inflateViewBinding()
|
|
setContentView(binding.root)
|
|
viewModel = getViewModel()
|
|
initialize()
|
|
registerDataChange()
|
|
}
|
|
|
|
override fun onDestroy() {
|
|
keyEventHelper?.onDestroy()
|
|
EventBus.getDefault().unregister(this)
|
|
super.onDestroy()
|
|
}
|
|
|
|
abstract fun getViewModel(): BaseViewModel
|
|
|
|
protected abstract fun inflateViewBinding(): VB
|
|
|
|
open fun initialize() {
|
|
|
|
}
|
|
|
|
open fun registerDataChange() {
|
|
// lifecycleScope.launch {
|
|
// viewModel.showLoading.collect {
|
|
// Timber.d("registerDataChange 用户 showLoading = $it")
|
|
// if (it) {
|
|
// showWaitingDialog("")
|
|
// } else {
|
|
// hideWaitingDialog()
|
|
// }
|
|
// }
|
|
// }
|
|
}
|
|
|
|
/**
|
|
* 显示等待提示框
|
|
*/
|
|
fun showWaitingDialog(tip: String?) {
|
|
runOnUiThread {
|
|
hideWaitingDialog()
|
|
//val view = View.inflate(this, R.layout.dialog_waiting, null)
|
|
val dialogBinding = DialogWaitingBinding.inflate(LayoutInflater.from(this))
|
|
dialogBinding.tvTip.text = tip
|
|
mDialogWaiting = CustomDialog(this, dialogBinding.root)
|
|
mDialogWaiting?.show()
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 隐藏等待提示框
|
|
*/
|
|
fun hideWaitingDialog() {
|
|
runOnUiThread {
|
|
mDialogWaiting?.dismiss()
|
|
mDialogWaiting = null
|
|
}
|
|
}
|
|
|
|
fun showWaitingDialog2(tip: String?) {
|
|
if (mDialogWaiting == null) {
|
|
hideWaitingDialog()
|
|
val view = View.inflate(this, R.layout.dialog_waiting, null)
|
|
mDialogWaiting = CustomDialog(this, view, R.style.MyDialog)
|
|
mDialogWaiting?.show()
|
|
}
|
|
val contentView = mDialogWaiting?.findViewById<ViewGroup>(android.R.id.content)
|
|
val tvTip = contentView?.findViewById<TextView>(R.id.tvTip)
|
|
tvTip?.text = tip
|
|
}
|
|
|
|
private var launchPermissionCallback: IActivityResult.RequestPermissionCallback? = null
|
|
|
|
fun requestSinglePermissionResult(
|
|
permission: String,
|
|
permissionCallback: IActivityResult.RequestPermissionCallback?
|
|
) {
|
|
this.launchPermissionCallback = permissionCallback
|
|
permissionLauncher.launch(permission)
|
|
}
|
|
|
|
private val permissionLauncher = registerForActivityResult(
|
|
ActivityResultContracts.RequestPermission()
|
|
) { result: Boolean ->
|
|
launchPermissionCallback?.callback(result)
|
|
}
|
|
|
|
fun log(msg: String) {
|
|
Timber.d(msg)
|
|
FileUtil.saveLog(msg)
|
|
}
|
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
fun onPostEvent(event: PostEvent) {
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* 监听扫描枪扫描事件
|
|
*/
|
|
fun registerKeyEvent() {
|
|
keyEventHelper =
|
|
ScanGunKeyEventHelper(context, object : ScanGunKeyEventHelper.OnScanSuccessListener {
|
|
override fun onScanSuccess(barcode: String?) {
|
|
Timber.d("onScanSuccess barcode = $barcode")
|
|
if (barcode == null) return
|
|
handleScanKeyInfo(barcode)
|
|
}
|
|
})
|
|
}
|
|
protected var keyEventHelper: ScanGunKeyEventHelper? = null
|
|
|
|
/**
|
|
* 处理扫描枪数据
|
|
*/
|
|
protected open fun handleScanKeyInfo(scanInfo: String) {
|
|
var payCode = scanInfo
|
|
//if (scanInfo.length > 6) {
|
|
// payCode = scanInfo.take(6)
|
|
//}
|
|
Log.d("TAG", "handleScanKeyInfo: " + payCode)
|
|
}
|
|
|
|
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
|
|
if (keyEventHelper != null) {
|
|
if (keyEventHelper!!.isScanGunEvent(event)) {
|
|
keyEventHelper!!.analysisKeyEvent(event)
|
|
return true
|
|
}
|
|
}
|
|
return super.dispatchKeyEvent(event)
|
|
}
|
|
} |