CommonBindingActivity.kt 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.navigateTo
  17. import com.develop.base.mvvm.BaseBindingActivity
  18. import com.develop.base.util.MMkvUtils
  19. import com.develop.base.util.TopResumedAtyHolder
  20. import com.develop.common.R
  21. import com.develop.common.dialog.CancelConfirmDialog
  22. import com.develop.common.dialog.PlainDialogView
  23. import com.develop.common.food_sdk.FoodSdkUtils
  24. import com.develop.common.food_sdk.GlobalDevEvent
  25. import com.develop.common.router.Screens
  26. import com.develop.common.tag.SCREENSAVER
  27. import com.develop.common.utils.TimeDownUtil
  28. import com.kuyuntech.cofarcooking.device.sdk.eventbus.event.DevPromptEvent
  29. import me.jessyan.autosize.AutoSizeCompat
  30. import org.alee.component.skin.service.ThemeSkinService
  31. import org.greenrobot.eventbus.EventBus
  32. import org.greenrobot.eventbus.Subscribe
  33. /**
  34. * 带业务逻辑的基类
  35. */
  36. abstract class CommonBindingActivity<T : ViewBinding> : BaseBindingActivity<T>() {
  37. private var screenReceiver: BroadcastReceiver? = null
  38. protected var hasShowScreenSaver = false
  39. private val plainDialogView by lazy {
  40. PlainDialogView(
  41. this, R.layout.dialog_easy_view
  42. )
  43. }
  44. private var isResume = false
  45. private val screenSaverDialog by lazy {
  46. CancelConfirmDialog()
  47. }
  48. private var timeDownUtil: TimeDownUtil? = null
  49. private var screenSaverTime = 3 //minute
  50. override fun onCreate(savedInstanceState: Bundle?) {
  51. EventBus.getDefault().register(this)
  52. super.onCreate(savedInstanceState)
  53. screenReceiver = object : BroadcastReceiver() {
  54. override fun onReceive(context: Context, intent: Intent) {
  55. val action = intent.action
  56. if (action == Intent.ACTION_SCREEN_ON) {
  57. // 处理屏幕亮起事件
  58. Log.i("0525","ACTION_SCREEN_ON")
  59. locktime = System.currentTimeMillis()
  60. } else if (action == Intent.ACTION_SCREEN_OFF) {
  61. // 处理屏幕关闭事件
  62. Log.i("0525","ACTION_SCREEN_OFF")
  63. }
  64. }
  65. }
  66. val intentFilter = IntentFilter().apply {
  67. addAction(Intent.ACTION_SCREEN_ON)
  68. addAction(Intent.ACTION_SCREEN_OFF)
  69. }
  70. registerReceiver(screenReceiver, intentFilter)
  71. val time = MMkvUtils.getInt(SCREENSAVER)
  72. if (time != 0) {
  73. screenSaverTime = time
  74. }
  75. screenSaverDialog.onDialogClickListener =
  76. object : CancelConfirmDialog.OnDialogClickListener {
  77. override fun onConfirm() {
  78. navigateTo(Screens.Setting.SCREEN_SAVER)
  79. }
  80. override fun onCancel() {
  81. if (!hasShowScreenSaver) startScreenSaverCount()
  82. }
  83. }
  84. }
  85. override fun onResume() {
  86. super.onResume()
  87. isResume = true
  88. if (!hasShowScreenSaver) {
  89. startScreenSaverCount()
  90. } else {
  91. stopScreenSaverCount()
  92. }
  93. val intentFilter = IntentFilter()
  94. intentFilter.addAction(Intent.ACTION_SCREEN_ON)
  95. intentFilter.addAction(Intent.ACTION_SCREEN_OFF)
  96. registerReceiver(screenReceiver, intentFilter)
  97. }
  98. override fun onPause() {
  99. super.onPause()
  100. isResume = false
  101. stopScreenSaverCount()
  102. }
  103. var lock = false
  104. var locktime:Long = 0
  105. override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
  106. if(((System.currentTimeMillis() - locktime) < 3000)){
  107. return true
  108. }
  109. locktime = 0L
  110. lock = false
  111. if (ev.x == 0f && ev.y == 0f) {
  112. return true
  113. }
  114. when (ev.action) {
  115. MotionEvent.ACTION_UP -> if (!hasShowScreenSaver) startScreenSaverCount()
  116. else -> stopScreenSaverCount()
  117. }
  118. if (ev.action == MotionEvent.ACTION_DOWN) {
  119. //全局点击外面关闭软键盘
  120. // 获得当前得到焦点的View,一般情况下就是EditText(特殊情况就是轨迹求或者实体案件会移动焦点)
  121. val v = currentFocus
  122. if (isShouldHideInput(v, ev)) {
  123. hideSoftInput(v?.windowToken)
  124. }
  125. }
  126. return super.dispatchTouchEvent(ev)
  127. }
  128. fun showPlainDialog(cancelable: Boolean = false) {
  129. plainDialogView.showDialog(cancelable)
  130. }
  131. fun dismissPlainDialog() {
  132. plainDialogView.hideDialog()
  133. }
  134. /**
  135. * 隐藏软键盘
  136. */
  137. private fun hideSoftInput(token: IBinder?) {
  138. if (token != null) {
  139. val manager: InputMethodManager =
  140. getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
  141. manager.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS)
  142. }
  143. }
  144. /**
  145. * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘
  146. * 因为当用户点击EditText时没必要隐藏
  147. */
  148. private fun isShouldHideInput(v: View?, event: MotionEvent): Boolean {
  149. if (v != null && v is EditText) {
  150. val l = intArrayOf(0, 0)
  151. v.getLocationInWindow(l)
  152. val left = l[0]
  153. val top = l[1]
  154. val bottom = top + v.height
  155. val right = (left + v.width)
  156. return !(event.x > left && event.x < right && event.y > top && event.y < bottom)
  157. }
  158. // 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditView上,和用户用轨迹球选择其他的焦点
  159. return false
  160. }
  161. private fun startScreenSaverCount() {
  162. if (FoodSdkUtils.isDevRunning()) {
  163. return
  164. }
  165. stopScreenSaverCount()
  166. val totalTime = screenSaverTime * 60 * 1000L
  167. timeDownUtil = object : TimeDownUtil(totalTime, 1000) {
  168. override fun onTick(millisUntilFinished: Long) {
  169. if (FoodSdkUtils.isDevRunning()) {
  170. return
  171. }
  172. if (millisUntilFinished == 30 * 1000L) {
  173. runOnUiThread {
  174. showScreenSaverDialog()
  175. }
  176. }
  177. }
  178. override fun onFinish() {
  179. if (FoodSdkUtils.isDevRunning()) {
  180. return
  181. }
  182. runOnUiThread {
  183. screenSaverDialog.removeSelf()
  184. navigateTo(Screens.Setting.SCREEN_SAVER)
  185. }
  186. }
  187. }
  188. timeDownUtil?.start()
  189. }
  190. private fun stopScreenSaverCount() {
  191. timeDownUtil?.cancel()
  192. timeDownUtil = null
  193. }
  194. private fun showScreenSaverDialog() {
  195. screenSaverDialog.title = getString(R.string.screen_saver_title)
  196. screenSaverDialog.showDialog(supportFragmentManager, "cancelConfirmDialog")
  197. }
  198. // @Subscribe
  199. // fun onCookDevMsgEvent(event: DevPromptEvent) {
  200. // if (isResume) {
  201. // GlobalDevEvent.globalDevMsgEvent(event, supportFragmentManager, resources)
  202. // }
  203. // }
  204. var lastMsg = ""
  205. private val cancelConfirmDialog = CancelConfirmDialog()
  206. @Subscribe
  207. fun onCookDevMsgEvent(event: DevPromptEvent) {
  208. if (!this.equals(TopResumedAtyHolder.getCurrentActivity())) {
  209. return
  210. }
  211. // //已显示弹窗不重复显示
  212. // if(FloatWindowManager.confirmCancelDialog.isAdded){
  213. // return
  214. // }
  215. if (lastMsg == event.msg) {
  216. return
  217. }
  218. lastMsg = event.msg
  219. var confirmCancelDialog = CancelConfirmDialog()
  220. confirmCancelDialog.showCancel = event.isShowCancelBtn
  221. confirmCancelDialog.showConfirm = event.isShowConfirmBtn
  222. confirmCancelDialog.cancelStr = resources.getString(
  223. resources.getIdentifier(
  224. event.cancelBtnText, "string", this.packageName
  225. )
  226. )
  227. confirmCancelDialog.confirmStr = resources.getString(
  228. resources.getIdentifier(
  229. event.confirmButtonText, "string", this.packageName
  230. )
  231. )
  232. confirmCancelDialog.title =
  233. resources.getString(resources.getIdentifier(event.msg, "string", this.packageName))
  234. confirmCancelDialog.showDialog(supportFragmentManager, "confirmCancelDialog")
  235. confirmCancelDialog.onDialogClickListener =
  236. object : CancelConfirmDialog.OnDialogClickListener {
  237. override fun onConfirm() {
  238. if (event.confirm != null) {
  239. event.confirm.callback();
  240. }
  241. cancelConfirmDialog.removeSelf();
  242. lastMsg = "";
  243. }
  244. override fun onCancel() {
  245. if (event.cancel != null) {
  246. event.cancel.callback();
  247. }
  248. cancelConfirmDialog.removeSelf();
  249. lastMsg = "";
  250. }
  251. }
  252. }
  253. override fun onDestroy() {
  254. super.onDestroy()
  255. EventBus.getDefault().unregister(this)
  256. GlobalDevEvent.dismissDialog()
  257. dismissPlainDialog()
  258. if(screenReceiver != null) unregisterReceiver(screenReceiver)
  259. }
  260. }