123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- package com.develop.common.ui
- import android.content.BroadcastReceiver
- import android.content.Context
- import android.content.Intent
- import android.content.IntentFilter
- import android.content.res.Resources
- import android.os.Bundle
- import android.os.IBinder
- import android.os.Looper
- import android.util.Log
- import android.view.MotionEvent
- import android.view.View
- import android.view.inputmethod.InputMethodManager
- import android.widget.EditText
- import androidx.viewbinding.ViewBinding
- import com.develop.base.ext.getModelNum
- import com.develop.base.ext.getSN
- import com.develop.base.ext.isBrand062
- import com.develop.base.ext.navigateTo
- import com.develop.base.ext.toJson
- import com.develop.base.mvvm.BaseBindingActivity
- import com.develop.base.mvvm.FullScreenTransparentDialog
- import com.develop.base.util.GlobalToast
- import com.develop.base.util.MMkvUtils
- import com.develop.base.util.TopResumedAtyHolder
- import com.develop.common.R
- import com.develop.common.data_repo.FoodDataProvider
- import com.develop.common.data_repo.net.TokenInvalidateEvent
- import com.develop.common.dialog.CancelConfirmDialog
- import com.develop.common.dialog.CancelConfirmImageDialog
- import com.develop.common.dialog.PlainDialogView
- import com.develop.common.food_sdk.FoodSdkUtils
- import com.develop.common.food_sdk.GlobalDevEvent
- import com.develop.common.router.Screens
- import com.develop.common.tag.CURRENT_USER_ID
- import com.develop.common.tag.SCREENSAVER
- import com.develop.common.utils.NoScreenEvent
- import com.develop.common.utils.TimeDownUtil
- import com.kuyuntech.cofarcooking.device.sdk.eventbus.event.DevPromptEvent
- import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK
- import me.jessyan.autosize.AutoSizeCompat
- import org.alee.component.skin.service.ThemeSkinService
- import org.greenrobot.eventbus.EventBus
- import org.greenrobot.eventbus.Subscribe
- /**
- * 带业务逻辑的基类
- */
- abstract class CommonBindingActivity<T : ViewBinding> : BaseBindingActivity<T>() {
- private var is062 = false
- private var screenReceiver: BroadcastReceiver? = null
- protected var hasShowScreenSaver = false
- private var isModelNum = ""
- private val plainDialogView by lazy {
- PlainDialogView(
- this, R.layout.dialog_easy_view
- )
- }
- private var isResume = false
- private val screenSaverDialog by lazy {
- CancelConfirmDialog()
- }
- protected var isWidget = false
- private var timeDownUtil: TimeDownUtil? = null
- private var screenSaverTime = 3 //minute
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- is062 = isBrand062()
- isModelNum = getModelNum()
- screenReceiver = object : BroadcastReceiver() {
- override fun onReceive(context: Context, intent: Intent) {
- val action = intent.action
- if (action == Intent.ACTION_SCREEN_ON) {
- // 处理屏幕亮起事件
- Log.i("0525", "ACTION_SCREEN_ON")
- locktime = System.currentTimeMillis()
- } else if (action == Intent.ACTION_SCREEN_OFF) {
- // 处理屏幕关闭事件
- Log.i("0525", "ACTION_SCREEN_OFF")
- }
- }
- }
- val intentFilter = IntentFilter().apply {
- addAction(Intent.ACTION_SCREEN_ON)
- addAction(Intent.ACTION_SCREEN_OFF)
- }
- registerReceiver(screenReceiver, intentFilter)
- val time = MMkvUtils.getInt(SCREENSAVER)
- if (time != 0) {
- screenSaverTime = time
- }
- screenSaverDialog.onDialogClickListener =
- object : CancelConfirmDialog.OnDialogClickListener {
- override fun onConfirm() {
- navigateTo(Screens.Setting.SCREEN_SAVER)
- }
- override fun onCancel() {
- if (!hasShowScreenSaver) startScreenSaverCount()
- }
- }
- }
- override fun onResume() {
- EventBus.getDefault().register(this)
- super.onResume()
- isResume = true
- if (!hasShowScreenSaver) {
- startScreenSaverCount()
- } else {
- stopScreenSaverCount()
- }
- val intentFilter = IntentFilter()
- intentFilter.addAction(Intent.ACTION_SCREEN_ON)
- intentFilter.addAction(Intent.ACTION_SCREEN_OFF)
- registerReceiver(screenReceiver, intentFilter)
- }
- override fun onPause() {
- EventBus.getDefault().unregister(this)
- super.onPause()
- isResume = false
- stopScreenSaverCount()
- }
- @Subscribe
- fun noScreenEvent(event: NoScreenEvent) {
- if (event.noScreen) {
- stopScreenSaverCount()
- } else {
- startScreenSaverCount()
- }
- }
- var lock = false
- var locktime: Long = 0
- override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
- // Log.d("dddddd", "拦截事件:${ev.action}(${ev.x},${ev.y}),")
- if (((System.currentTimeMillis() - locktime) < 3000)) {
- return super.dispatchTouchEvent(ev)
- }
- locktime = 0L
- lock = false
- if (ev.x == 0f && ev.y == 0f) {
- return true
- }
- when (ev.action) {
- MotionEvent.ACTION_UP -> if (!hasShowScreenSaver) startScreenSaverCount()
- else -> stopScreenSaverCount()
- }
- if (ev.action == MotionEvent.ACTION_DOWN) {
- //全局点击外面关闭软键盘
- // 获得当前得到焦点的View,一般情况下就是EditText(特殊情况就是轨迹求或者实体案件会移动焦点)
- val v = currentFocus
- if (isShouldHideInput(v, ev)) {
- hideSoftInput(v?.windowToken)
- }
- }
- return super.dispatchTouchEvent(ev)
- }
- fun showPlainDialog(cancelable: Boolean = false) {
- plainDialogView.showDialog(cancelable)
- }
- fun dismissPlainDialog() {
- plainDialogView.hideDialog()
- }
- /**
- * 隐藏软键盘
- */
- private fun hideSoftInput(token: IBinder?) {
- if (token != null) {
- val manager: InputMethodManager =
- getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
- manager.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS)
- }
- }
- /**
- * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘
- * 因为当用户点击EditText时没必要隐藏
- */
- private fun isShouldHideInput(v: View?, event: MotionEvent): Boolean {
- Log.d("TAG dddddd", "isShouldHideInput:${v} event : $event ")
- if (v != null && v is EditText) {
- val l = intArrayOf(0, 0)
- v.getLocationInWindow(l)
- val left = l[0]
- val top = l[1]
- val bottom = top + v.height
- val right = (left + v.width)
- return !(event.x > left && event.x < right && event.y > top && event.y < bottom)
- }
- // 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditView上,和用户用轨迹球选择其他的焦点
- return false
- }
- private fun startScreenSaverCount() {
- if (FoodSdkUtils.isDevRunning()) {
- return
- }
- stopScreenSaverCount()
- val totalTime = screenSaverTime * 60 * 1000L
- timeDownUtil = object : TimeDownUtil(totalTime, 1000) {
- override fun onTick(millisUntilFinished: Long) {
- if (FoodSdkUtils.isDevRunning()) {
- return
- }
- if (millisUntilFinished == 30 * 1000L) {
- runOnUiThread {
- showScreenSaverDialog()
- }
- }
- }
- override fun onFinish() {
- if (FoodSdkUtils.isDevRunning()) {
- return
- }
- runOnUiThread {
- screenSaverDialog.removeSelf()
- navigateTo(Screens.Setting.SCREEN_SAVER)
- }
- }
- }
- timeDownUtil?.start()
- }
- private fun stopScreenSaverCount() {
- timeDownUtil?.cancel()
- timeDownUtil = null
- }
- private fun showScreenSaverDialog() {
- screenSaverDialog.title = getString(R.string.screen_saver_title)
- screenSaverDialog.showDialog(supportFragmentManager, "cancelConfirmDialog")
- }
- // @Subscribe
- // fun onCookDevMsgEvent(event: DevPromptEvent) {
- // if (isResume) {
- // GlobalDevEvent.globalDevMsgEvent(event, supportFragmentManager, resources)
- // }
- // }
- var lastMsg = ""
- private val cancelConfirmDialog = CancelConfirmDialog()
- private val cancelConfirmImageDialog = CancelConfirmImageDialog()
- @Subscribe
- fun onCookDevMsgEvent(event: DevPromptEvent) {
- Log.d(
- "TAG 设备返回",
- "msg=>${event.msg}==action=>${event.action}==errorCode=>${CofarSDK.devInfo().errCode}" +
- "==mode=>${CofarSDK.devInfo().mode.mode}" +
- "==devmodes=>${CofarSDK.devInfo().devMode} ==potCloverType=>${CofarSDK.devInfo().potCloverType} ==Activity==>${TopResumedAtyHolder.getCurrentActivity()?.localClassName}"
- )
- // Log.d("TAG 设备返回","event :${event}")
- // 打开锅盖锅 msg=>error_pot_clover_tips==action=>show==errorCode=>10==mode=>ADAPTED_COOKING
- if (!this.equals(TopResumedAtyHolder.getCurrentActivity())) {
- return
- }
- // 彩屏风扇不转,报错彩屏散热异常 无需弹窗
- if (CofarSDK.devInfo().errCode.toInt() == 15) {
- return
- }
- //所有模式程序运行结束时没有响三声蜂鸣声提示结果程序
- if (event.msg == "dev_end_of_run_tips") {
- CofarSDK.cfgBeep(0, 3)
- }
- //当前errcode不是0,并且提示是程序完成的提示不需要显示
- if (CofarSDK.devInfo().errCode.toInt() != 0 && event.msg == "dev_end_of_run_tips") {
- return
- }
- //称重模式下,非电子称重异常的提示,都不用提示
- if (CofarSDK.devInfo().isWeightStatus && event.msg != "dian_zi_cheng_tong_xin_yi_chan" && event.msg != "weight_overload_tips") {
- return
- }
- //切碎模式下,电子称重异常不用提示
- if (CofarSDK.devInfo().mode.mode == "TURBO" && event.msg == "dian_zi_cheng_tong_xin_yi_chan") {
- return
- }
- //由于 error_pot_clover_tips 这个错误的event.confirm.callback() 的方法会重新刷新模式,会导致ModeFragment的列表重新刷新
- //因5037需求 在模式上面无锅状态上默认显示汤锅模式古在base里面的HomeActivity 的页面 把 error_pot_clover_tips 的错误过滤掉,因为这个会切换模式
- if (isModelNum == "5037" && event.msg == "error_pot_clover_tips"
- && TopResumedAtyHolder.getCurrentActivity()?.localClassName == "com.develop.main.ui.HomeActivity"
- ) {
- return
- }
- // //做切换锅,有些页面保存当前页面
- // if (event.msg == "error_pot_clover_tips"&&event.action=="show"){
- // changePotDialog(event)
- // return
- // }
- //如果电子称重异常弹窗,恢复正常以后需要自动关闭
- if (lastMsg == "dian_zi_cheng_tong_xin_yi_chan" && CofarSDK.devInfo().errCode.toInt() == 0) {
- cancelConfirmDialog.let {
- if (it.isShow || it.isAdded) {
- lastMsg = ""
- it.removeSelf()
- return
- }
- }
- }
- if ("hide" == event.action) {
- lastMsg = "";
- cancelConfirmDialog.removeSelf()
- return
- }
- // //已显示弹窗不重复显示
- // if(FloatWindowManager.confirmCancelDialog.isAdded){
- // return
- // }
- if (lastMsg == event.msg) {
- return
- }
- lastMsg = event.msg
- if (is062 && event.msg == "dev_end_of_run_tips") {
- cancelConfirmImageDialog.showCancel = event.isShowCancelBtn
- cancelConfirmImageDialog.showConfirm = event.isShowConfirmBtn
- cancelConfirmImageDialog.cancelStr = resources.getString(
- resources.getIdentifier(
- event.cancelBtnText, "string", this.packageName
- )
- )
- cancelConfirmImageDialog.confirmStr = resources.getString(
- resources.getIdentifier(
- event.confirmButtonText, "string", this.packageName
- )
- )
- cancelConfirmImageDialog.title =
- resources.getString(resources.getIdentifier(event.msg, "string", this.packageName))
- cancelConfirmImageDialog.showDialog(supportFragmentManager, "confirmCancelDialog")
- cancelConfirmImageDialog.onDialogClickListener =
- object : CancelConfirmImageDialog.OnDialogClickListener {
- override fun onConfirm() {
- if (event.confirm != null) {
- event.confirm.callback();
- }
- cancelConfirmImageDialog.removeSelf();
- lastMsg = "";
- }
- override fun onCancel() {
- if (event.cancel != null) {
- event.cancel.callback();
- }
- cancelConfirmImageDialog.removeSelf();
- lastMsg = "";
- }
- }
- } else {
- cancelConfirmDialog.showCancel = event.isShowCancelBtn
- cancelConfirmDialog.showConfirm = event.isShowConfirmBtn
- cancelConfirmDialog.cancelStr = resources.getString(
- resources.getIdentifier(
- event.cancelBtnText, "string", this.packageName
- )
- )
- cancelConfirmDialog.confirmStr = resources.getString(
- resources.getIdentifier(
- event.confirmButtonText, "string", this.packageName
- )
- )
- cancelConfirmDialog.title =
- resources.getString(resources.getIdentifier(event.msg, "string", this.packageName))
- cancelConfirmDialog.showDialog(supportFragmentManager, "confirmCancelDialog")
- cancelConfirmDialog.onDialogClickListener =
- object : CancelConfirmDialog.OnDialogClickListener {
- override fun onConfirm() {
- if (event.confirm != null) {
- event.confirm.callback();
- }
- cancelConfirmDialog.removeSelf();
- lastMsg = "";
- }
- override fun onCancel() {
- if (event.cancel != null) {
- event.cancel.callback();
- }
- cancelConfirmDialog.removeSelf();
- lastMsg = "";
- }
- }
- }
- // if (isWidget && lastMsg != "dian_zi_cheng_tong_xin_yi_chan") {
- // return
- // }
- }
- @Subscribe
- fun closeEvent(event: FullScreenTransparentDialog.CloseDialogEvent) {
- cancelConfirmDialog.onConfirmClick()
- }
- override fun onDestroy() {
- super.onDestroy()
- GlobalDevEvent.dismissDialog()
- dismissPlainDialog()
- if (screenReceiver != null) unregisterReceiver(screenReceiver)
- }
- @Subscribe
- fun onTokenInvalidateEvent(event: TokenInvalidateEvent) {
- if (event.cancelAccount) {
- FoodDataProvider
- .getUserDatabase()
- .userInfoDao()
- .deleteUserInfoByUserId(CURRENT_USER_ID)
- } else {
- GlobalToast.showToast(getString(R.string.login_expire_tips))
- }
- navigateTo(Screens.Auth.MEMBER) {
- val bundle = Bundle()
- bundle.putBoolean("isTokenOut", true)
- with(bundle)
- }
- finish()
- }
- fun changePotDialog(event: DevPromptEvent) {
- if (lastMsg == event.msg) {
- return
- }
- lastMsg = event.msg
- cancelConfirmDialog.showCancel = event.isShowCancelBtn
- cancelConfirmDialog.showConfirm = event.isShowConfirmBtn
- cancelConfirmDialog.cancelStr = resources.getString(
- resources.getIdentifier(
- event.cancelBtnText, "string", this.packageName
- )
- )
- cancelConfirmDialog.confirmStr = resources.getString(
- resources.getIdentifier(
- event.confirmButtonText, "string", this.packageName
- )
- )
- cancelConfirmDialog.title =
- resources.getString(resources.getIdentifier(event.msg, "string", this.packageName))
- cancelConfirmDialog.showDialog(supportFragmentManager, "confirmCancelDialog")
- cancelConfirmDialog.onDialogClickListener =
- object : CancelConfirmDialog.OnDialogClickListener {
- override fun onConfirm() {
- if (event.confirm != null) {
- event.confirm.callback();
- }
- confirmChange()
- cancelConfirmDialog.removeSelf();
- lastMsg = "";
- }
- override fun onCancel() {
- cancelConfirmDialog.removeSelf();
- lastMsg = "";
- }
- }
- }
- private fun confirmChange() {
- }
- fun showMsgDialog(msg :String){
- lastMsg = msg
- cancelConfirmDialog.showCancel = false
- cancelConfirmDialog.showConfirm = true
- cancelConfirmDialog.confirmStr = resources.getString(
- resources.getIdentifier(
- "pmpt_confirm", "string", this.packageName
- )
- )
- cancelConfirmDialog.title =lastMsg
- // resources.getString(resources.getIdentifier("pmpt_msg", "string", this.packageName))
- cancelConfirmDialog.showDialog(supportFragmentManager, "confirmCancelDialog")
- cancelConfirmDialog.onDialogClickListener =
- object : CancelConfirmDialog.OnDialogClickListener {
- override fun onConfirm() {
- cancelConfirmDialog.removeSelf();
- lastMsg = "";
- }
- override fun onCancel() {
- cancelConfirmDialog.removeSelf();
- lastMsg = "";
- }
- }
- }
- }
|