CommonBindingActivity.kt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. package com.develop.common.ui
  2. import android.content.BroadcastReceiver
  3. import android.content.Context
  4. import android.content.Intent
  5. import android.content.IntentFilter
  6. import android.content.res.Resources
  7. import android.os.Bundle
  8. import android.os.IBinder
  9. import android.os.Looper
  10. import android.util.Log
  11. import android.view.MotionEvent
  12. import android.view.View
  13. import android.view.inputmethod.InputMethodManager
  14. import android.widget.EditText
  15. import androidx.viewbinding.ViewBinding
  16. import com.develop.base.ext.getModelNum
  17. import com.develop.base.ext.getSN
  18. import com.develop.base.ext.isBrand062
  19. import com.develop.base.ext.navigateTo
  20. import com.develop.base.ext.toJson
  21. import com.develop.base.mvvm.BaseBindingActivity
  22. import com.develop.base.mvvm.FullScreenTransparentDialog
  23. import com.develop.base.util.GlobalToast
  24. import com.develop.base.util.MMkvUtils
  25. import com.develop.base.util.TopResumedAtyHolder
  26. import com.develop.common.R
  27. import com.develop.common.data_repo.FoodDataProvider
  28. import com.develop.common.data_repo.net.TokenInvalidateEvent
  29. import com.develop.common.dialog.CancelConfirmDialog
  30. import com.develop.common.dialog.CancelConfirmImageDialog
  31. import com.develop.common.dialog.PlainDialogView
  32. import com.develop.common.food_sdk.FoodSdkUtils
  33. import com.develop.common.food_sdk.GlobalDevEvent
  34. import com.develop.common.router.Screens
  35. import com.develop.common.tag.CURRENT_USER_ID
  36. import com.develop.common.tag.SCREENSAVER
  37. import com.develop.common.utils.NoScreenEvent
  38. import com.develop.common.utils.TimeDownUtil
  39. import com.kuyuntech.cofarcooking.device.sdk.eventbus.event.DevPromptEvent
  40. import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK
  41. import me.jessyan.autosize.AutoSizeCompat
  42. import org.alee.component.skin.service.ThemeSkinService
  43. import org.greenrobot.eventbus.EventBus
  44. import org.greenrobot.eventbus.Subscribe
  45. /**
  46. * 带业务逻辑的基类
  47. */
  48. abstract class CommonBindingActivity<T : ViewBinding> : BaseBindingActivity<T>() {
  49. private var is062 = false
  50. private var screenReceiver: BroadcastReceiver? = null
  51. protected var hasShowScreenSaver = false
  52. private var isModelNum = ""
  53. private val plainDialogView by lazy {
  54. PlainDialogView(
  55. this, R.layout.dialog_easy_view
  56. )
  57. }
  58. private var isResume = false
  59. private val screenSaverDialog by lazy {
  60. CancelConfirmDialog()
  61. }
  62. protected var isWidget = false
  63. private var timeDownUtil: TimeDownUtil? = null
  64. private var screenSaverTime = 3 //minute
  65. override fun onCreate(savedInstanceState: Bundle?) {
  66. super.onCreate(savedInstanceState)
  67. is062 = isBrand062()
  68. isModelNum = getModelNum()
  69. screenReceiver = object : BroadcastReceiver() {
  70. override fun onReceive(context: Context, intent: Intent) {
  71. val action = intent.action
  72. if (action == Intent.ACTION_SCREEN_ON) {
  73. // 处理屏幕亮起事件
  74. Log.i("0525", "ACTION_SCREEN_ON")
  75. locktime = System.currentTimeMillis()
  76. } else if (action == Intent.ACTION_SCREEN_OFF) {
  77. // 处理屏幕关闭事件
  78. Log.i("0525", "ACTION_SCREEN_OFF")
  79. }
  80. }
  81. }
  82. val intentFilter = IntentFilter().apply {
  83. addAction(Intent.ACTION_SCREEN_ON)
  84. addAction(Intent.ACTION_SCREEN_OFF)
  85. }
  86. registerReceiver(screenReceiver, intentFilter)
  87. val time = MMkvUtils.getInt(SCREENSAVER)
  88. if (time != 0) {
  89. screenSaverTime = time
  90. }
  91. screenSaverDialog.onDialogClickListener =
  92. object : CancelConfirmDialog.OnDialogClickListener {
  93. override fun onConfirm() {
  94. navigateTo(Screens.Setting.SCREEN_SAVER)
  95. }
  96. override fun onCancel() {
  97. if (!hasShowScreenSaver) startScreenSaverCount()
  98. }
  99. }
  100. }
  101. override fun onResume() {
  102. EventBus.getDefault().register(this)
  103. super.onResume()
  104. isResume = true
  105. if (!hasShowScreenSaver) {
  106. startScreenSaverCount()
  107. } else {
  108. stopScreenSaverCount()
  109. }
  110. val intentFilter = IntentFilter()
  111. intentFilter.addAction(Intent.ACTION_SCREEN_ON)
  112. intentFilter.addAction(Intent.ACTION_SCREEN_OFF)
  113. registerReceiver(screenReceiver, intentFilter)
  114. }
  115. override fun onPause() {
  116. EventBus.getDefault().unregister(this)
  117. super.onPause()
  118. isResume = false
  119. stopScreenSaverCount()
  120. }
  121. @Subscribe
  122. fun noScreenEvent(event: NoScreenEvent) {
  123. if (event.noScreen) {
  124. stopScreenSaverCount()
  125. } else {
  126. startScreenSaverCount()
  127. }
  128. }
  129. var lock = false
  130. var locktime: Long = 0
  131. override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
  132. // Log.d("dddddd", "拦截事件:${ev.action}(${ev.x},${ev.y}),")
  133. if (((System.currentTimeMillis() - locktime) < 3000)) {
  134. return super.dispatchTouchEvent(ev)
  135. }
  136. locktime = 0L
  137. lock = false
  138. if (ev.x == 0f && ev.y == 0f) {
  139. return true
  140. }
  141. when (ev.action) {
  142. MotionEvent.ACTION_UP -> if (!hasShowScreenSaver) startScreenSaverCount()
  143. else -> stopScreenSaverCount()
  144. }
  145. if (ev.action == MotionEvent.ACTION_DOWN) {
  146. //全局点击外面关闭软键盘
  147. // 获得当前得到焦点的View,一般情况下就是EditText(特殊情况就是轨迹求或者实体案件会移动焦点)
  148. val v = currentFocus
  149. if (isShouldHideInput(v, ev)) {
  150. hideSoftInput(v?.windowToken)
  151. }
  152. }
  153. return super.dispatchTouchEvent(ev)
  154. }
  155. fun showPlainDialog(cancelable: Boolean = false) {
  156. plainDialogView.showDialog(cancelable)
  157. }
  158. fun dismissPlainDialog() {
  159. plainDialogView.hideDialog()
  160. }
  161. /**
  162. * 隐藏软键盘
  163. */
  164. private fun hideSoftInput(token: IBinder?) {
  165. if (token != null) {
  166. val manager: InputMethodManager =
  167. getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
  168. manager.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS)
  169. }
  170. }
  171. /**
  172. * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘
  173. * 因为当用户点击EditText时没必要隐藏
  174. */
  175. private fun isShouldHideInput(v: View?, event: MotionEvent): Boolean {
  176. Log.d("TAG dddddd", "isShouldHideInput:${v} event : $event ")
  177. if (v != null && v is EditText) {
  178. val l = intArrayOf(0, 0)
  179. v.getLocationInWindow(l)
  180. val left = l[0]
  181. val top = l[1]
  182. val bottom = top + v.height
  183. val right = (left + v.width)
  184. return !(event.x > left && event.x < right && event.y > top && event.y < bottom)
  185. }
  186. // 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditView上,和用户用轨迹球选择其他的焦点
  187. return false
  188. }
  189. private fun startScreenSaverCount() {
  190. if (FoodSdkUtils.isDevRunning()) {
  191. return
  192. }
  193. stopScreenSaverCount()
  194. val totalTime = screenSaverTime * 60 * 1000L
  195. timeDownUtil = object : TimeDownUtil(totalTime, 1000) {
  196. override fun onTick(millisUntilFinished: Long) {
  197. if (FoodSdkUtils.isDevRunning()) {
  198. return
  199. }
  200. if (millisUntilFinished == 30 * 1000L) {
  201. runOnUiThread {
  202. showScreenSaverDialog()
  203. }
  204. }
  205. }
  206. override fun onFinish() {
  207. if (FoodSdkUtils.isDevRunning()) {
  208. return
  209. }
  210. runOnUiThread {
  211. screenSaverDialog.removeSelf()
  212. navigateTo(Screens.Setting.SCREEN_SAVER)
  213. }
  214. }
  215. }
  216. timeDownUtil?.start()
  217. }
  218. private fun stopScreenSaverCount() {
  219. timeDownUtil?.cancel()
  220. timeDownUtil = null
  221. }
  222. private fun showScreenSaverDialog() {
  223. screenSaverDialog.title = getString(R.string.screen_saver_title)
  224. screenSaverDialog.showDialog(supportFragmentManager, "cancelConfirmDialog")
  225. }
  226. // @Subscribe
  227. // fun onCookDevMsgEvent(event: DevPromptEvent) {
  228. // if (isResume) {
  229. // GlobalDevEvent.globalDevMsgEvent(event, supportFragmentManager, resources)
  230. // }
  231. // }
  232. var lastMsg = ""
  233. private val cancelConfirmDialog = CancelConfirmDialog()
  234. private val cancelConfirmImageDialog = CancelConfirmImageDialog()
  235. @Subscribe
  236. fun onCookDevMsgEvent(event: DevPromptEvent) {
  237. Log.d(
  238. "TAG 设备返回",
  239. "msg=>${event.msg}==action=>${event.action}==errorCode=>${CofarSDK.devInfo().errCode}" +
  240. "==mode=>${CofarSDK.devInfo().mode.mode}" +
  241. "==devmodes=>${CofarSDK.devInfo().devMode} ==potCloverType=>${CofarSDK.devInfo().potCloverType} ==Activity==>${TopResumedAtyHolder.getCurrentActivity()?.localClassName}"
  242. )
  243. // Log.d("TAG 设备返回","event :${event}")
  244. // 打开锅盖锅 msg=>error_pot_clover_tips==action=>show==errorCode=>10==mode=>ADAPTED_COOKING
  245. if (!this.equals(TopResumedAtyHolder.getCurrentActivity())) {
  246. return
  247. }
  248. // 彩屏风扇不转,报错彩屏散热异常 无需弹窗
  249. if (CofarSDK.devInfo().errCode.toInt() == 15) {
  250. return
  251. }
  252. //所有模式程序运行结束时没有响三声蜂鸣声提示结果程序
  253. if (event.msg == "dev_end_of_run_tips") {
  254. CofarSDK.cfgBeep(0, 3)
  255. }
  256. //当前errcode不是0,并且提示是程序完成的提示不需要显示
  257. if (CofarSDK.devInfo().errCode.toInt() != 0 && event.msg == "dev_end_of_run_tips") {
  258. return
  259. }
  260. //称重模式下,非电子称重异常的提示,都不用提示
  261. if (CofarSDK.devInfo().isWeightStatus && event.msg != "dian_zi_cheng_tong_xin_yi_chan" && event.msg != "weight_overload_tips") {
  262. return
  263. }
  264. //切碎模式下,电子称重异常不用提示
  265. if (CofarSDK.devInfo().mode.mode == "TURBO" && event.msg == "dian_zi_cheng_tong_xin_yi_chan") {
  266. return
  267. }
  268. //由于 error_pot_clover_tips 这个错误的event.confirm.callback() 的方法会重新刷新模式,会导致ModeFragment的列表重新刷新
  269. //因5037需求 在模式上面无锅状态上默认显示汤锅模式古在base里面的HomeActivity 的页面 把 error_pot_clover_tips 的错误过滤掉,因为这个会切换模式
  270. if (isModelNum == "5037" && event.msg == "error_pot_clover_tips"
  271. && TopResumedAtyHolder.getCurrentActivity()?.localClassName == "com.develop.main.ui.HomeActivity"
  272. ) {
  273. return
  274. }
  275. // //做切换锅,有些页面保存当前页面
  276. // if (event.msg == "error_pot_clover_tips"&&event.action=="show"){
  277. // changePotDialog(event)
  278. // return
  279. // }
  280. //如果电子称重异常弹窗,恢复正常以后需要自动关闭
  281. if (lastMsg == "dian_zi_cheng_tong_xin_yi_chan" && CofarSDK.devInfo().errCode.toInt() == 0) {
  282. cancelConfirmDialog.let {
  283. if (it.isShow || it.isAdded) {
  284. lastMsg = ""
  285. it.removeSelf()
  286. return
  287. }
  288. }
  289. }
  290. if ("hide" == event.action) {
  291. lastMsg = "";
  292. cancelConfirmDialog.removeSelf()
  293. return
  294. }
  295. // //已显示弹窗不重复显示
  296. // if(FloatWindowManager.confirmCancelDialog.isAdded){
  297. // return
  298. // }
  299. if (lastMsg == event.msg) {
  300. return
  301. }
  302. lastMsg = event.msg
  303. if (is062 && event.msg == "dev_end_of_run_tips") {
  304. cancelConfirmImageDialog.showCancel = event.isShowCancelBtn
  305. cancelConfirmImageDialog.showConfirm = event.isShowConfirmBtn
  306. cancelConfirmImageDialog.cancelStr = resources.getString(
  307. resources.getIdentifier(
  308. event.cancelBtnText, "string", this.packageName
  309. )
  310. )
  311. cancelConfirmImageDialog.confirmStr = resources.getString(
  312. resources.getIdentifier(
  313. event.confirmButtonText, "string", this.packageName
  314. )
  315. )
  316. cancelConfirmImageDialog.title =
  317. resources.getString(resources.getIdentifier(event.msg, "string", this.packageName))
  318. cancelConfirmImageDialog.showDialog(supportFragmentManager, "confirmCancelDialog")
  319. cancelConfirmImageDialog.onDialogClickListener =
  320. object : CancelConfirmImageDialog.OnDialogClickListener {
  321. override fun onConfirm() {
  322. if (event.confirm != null) {
  323. event.confirm.callback();
  324. }
  325. cancelConfirmImageDialog.removeSelf();
  326. lastMsg = "";
  327. }
  328. override fun onCancel() {
  329. if (event.cancel != null) {
  330. event.cancel.callback();
  331. }
  332. cancelConfirmImageDialog.removeSelf();
  333. lastMsg = "";
  334. }
  335. }
  336. } else {
  337. cancelConfirmDialog.showCancel = event.isShowCancelBtn
  338. cancelConfirmDialog.showConfirm = event.isShowConfirmBtn
  339. cancelConfirmDialog.cancelStr = resources.getString(
  340. resources.getIdentifier(
  341. event.cancelBtnText, "string", this.packageName
  342. )
  343. )
  344. cancelConfirmDialog.confirmStr = resources.getString(
  345. resources.getIdentifier(
  346. event.confirmButtonText, "string", this.packageName
  347. )
  348. )
  349. cancelConfirmDialog.title =
  350. resources.getString(resources.getIdentifier(event.msg, "string", this.packageName))
  351. cancelConfirmDialog.showDialog(supportFragmentManager, "confirmCancelDialog")
  352. cancelConfirmDialog.onDialogClickListener =
  353. object : CancelConfirmDialog.OnDialogClickListener {
  354. override fun onConfirm() {
  355. if (event.confirm != null) {
  356. event.confirm.callback();
  357. }
  358. cancelConfirmDialog.removeSelf();
  359. lastMsg = "";
  360. }
  361. override fun onCancel() {
  362. if (event.cancel != null) {
  363. event.cancel.callback();
  364. }
  365. cancelConfirmDialog.removeSelf();
  366. lastMsg = "";
  367. }
  368. }
  369. }
  370. // if (isWidget && lastMsg != "dian_zi_cheng_tong_xin_yi_chan") {
  371. // return
  372. // }
  373. }
  374. @Subscribe
  375. fun closeEvent(event: FullScreenTransparentDialog.CloseDialogEvent) {
  376. cancelConfirmDialog.onConfirmClick()
  377. }
  378. override fun onDestroy() {
  379. super.onDestroy()
  380. GlobalDevEvent.dismissDialog()
  381. dismissPlainDialog()
  382. if (screenReceiver != null) unregisterReceiver(screenReceiver)
  383. }
  384. @Subscribe
  385. fun onTokenInvalidateEvent(event: TokenInvalidateEvent) {
  386. if (event.cancelAccount) {
  387. FoodDataProvider
  388. .getUserDatabase()
  389. .userInfoDao()
  390. .deleteUserInfoByUserId(CURRENT_USER_ID)
  391. } else {
  392. GlobalToast.showToast(getString(R.string.login_expire_tips))
  393. }
  394. navigateTo(Screens.Auth.MEMBER) {
  395. val bundle = Bundle()
  396. bundle.putBoolean("isTokenOut", true)
  397. with(bundle)
  398. }
  399. finish()
  400. }
  401. fun changePotDialog(event: DevPromptEvent) {
  402. if (lastMsg == event.msg) {
  403. return
  404. }
  405. lastMsg = event.msg
  406. cancelConfirmDialog.showCancel = event.isShowCancelBtn
  407. cancelConfirmDialog.showConfirm = event.isShowConfirmBtn
  408. cancelConfirmDialog.cancelStr = resources.getString(
  409. resources.getIdentifier(
  410. event.cancelBtnText, "string", this.packageName
  411. )
  412. )
  413. cancelConfirmDialog.confirmStr = resources.getString(
  414. resources.getIdentifier(
  415. event.confirmButtonText, "string", this.packageName
  416. )
  417. )
  418. cancelConfirmDialog.title =
  419. resources.getString(resources.getIdentifier(event.msg, "string", this.packageName))
  420. cancelConfirmDialog.showDialog(supportFragmentManager, "confirmCancelDialog")
  421. cancelConfirmDialog.onDialogClickListener =
  422. object : CancelConfirmDialog.OnDialogClickListener {
  423. override fun onConfirm() {
  424. if (event.confirm != null) {
  425. event.confirm.callback();
  426. }
  427. confirmChange()
  428. cancelConfirmDialog.removeSelf();
  429. lastMsg = "";
  430. }
  431. override fun onCancel() {
  432. cancelConfirmDialog.removeSelf();
  433. lastMsg = "";
  434. }
  435. }
  436. }
  437. private fun confirmChange() {
  438. }
  439. fun showMsgDialog(msg :String){
  440. lastMsg = msg
  441. cancelConfirmDialog.showCancel = false
  442. cancelConfirmDialog.showConfirm = true
  443. cancelConfirmDialog.confirmStr = resources.getString(
  444. resources.getIdentifier(
  445. "pmpt_confirm", "string", this.packageName
  446. )
  447. )
  448. cancelConfirmDialog.title =lastMsg
  449. // resources.getString(resources.getIdentifier("pmpt_msg", "string", this.packageName))
  450. cancelConfirmDialog.showDialog(supportFragmentManager, "confirmCancelDialog")
  451. cancelConfirmDialog.onDialogClickListener =
  452. object : CancelConfirmDialog.OnDialogClickListener {
  453. override fun onConfirm() {
  454. cancelConfirmDialog.removeSelf();
  455. lastMsg = "";
  456. }
  457. override fun onCancel() {
  458. cancelConfirmDialog.removeSelf();
  459. lastMsg = "";
  460. }
  461. }
  462. }
  463. }