From 487c07a1be1f824cf0860882e5432f6592013934 Mon Sep 17 00:00:00 2001 From: renwj Date: Wed, 19 Apr 2023 08:57:44 +0800 Subject: [PATCH] =?UTF-8?q?[2.15.0]=20=E4=BC=98=E5=8C=96UiThreadHandler?= =?UTF-8?q?=E5=92=8CThreadUtils=E5=BE=80=E4=B8=BB=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E9=98=9F=E5=88=97=E6=B7=BB=E5=8A=A0=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E7=9A=84=E6=96=B9=E5=BC=8F=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=9C=80=E6=96=B0=E5=92=8C=E6=8E=92=E9=98=9F=E4=B8=A4=E7=A7=8D?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mogo/launcher/lancet/ANRFix.kt | 130 +++++++++--------- .../hmi/ui/msgbox/DriverMsgBoxBubbleView.kt | 4 +- .../eagle/core/utilcode/util/ThreadUtils.java | 49 +++++-- .../core/utilcode/util/UiThreadHandler.java | 58 ++++++-- 4 files changed, 152 insertions(+), 89 deletions(-) diff --git a/app/src/main/java/com/mogo/launcher/lancet/ANRFix.kt b/app/src/main/java/com/mogo/launcher/lancet/ANRFix.kt index 746a875ee0..63ddc771a6 100644 --- a/app/src/main/java/com/mogo/launcher/lancet/ANRFix.kt +++ b/app/src/main/java/com/mogo/launcher/lancet/ANRFix.kt @@ -19,70 +19,70 @@ class ANRFix { companion object { - @JvmStatic - @TargetClass(value = "android.os.Handler", scope = ALL) - @TargetMethod(methodName = "post") - @ReplaceInvoke - fun handlerPostProxy(handler: Handler, action: Runnable): Boolean { - if (Looper.getMainLooper() != handler.looper) { - return handler.post(action) - } - val what = ObjectHashCodeUtils.getHashCodeIfNeed(action) - handler.removeMessages(what) - val msg = Message.obtain(handler, action) - msg.what = what - return handler.sendMessage(msg) - } - - @JvmStatic - @TargetClass(value = "android.os.Handler", scope = ALL) - @TargetMethod(methodName = "postDelayed") - @ReplaceInvoke - fun handlerPostDelayProxy(handler: Handler, action: Runnable, delayMillis: Long): Boolean { - if (Looper.getMainLooper() != handler.looper) { - return handler.postDelayed(action, delayMillis) - } - val what = ObjectHashCodeUtils.getHashCodeIfNeed(action) - handler.removeMessages(what) - val msg = Message.obtain(handler, action) - msg.what = what - return handler.sendMessageDelayed(msg, delayMillis) - } - - @JvmStatic - @TargetClass(value = "android.app.Activity", scope = ALL) - @TargetMethod(methodName = "post") - @ReplaceInvoke - fun runOnUiThreadOfActivityProxy(activity: Activity, action: Runnable) { - if (Looper.myLooper() != Looper.getMainLooper()) { - UiThreadHandler.post(action) - } else { - activity.runOnUiThread(action) - } - } - - @JvmStatic - @TargetClass(value = "android.view.View", scope = ALL) - @TargetMethod(methodName = "post") - @ReplaceInvoke - fun postOfViewProxy(view: View, action: Runnable): Boolean { - return if (Looper.myLooper() != Looper.getMainLooper()) { - UiThreadHandler.post(action) - } else { - view.post(action) - } - } - - @JvmStatic - @TargetClass(value = "android.view.View", scope = ALL) - @TargetMethod(methodName = "postDelayed") - @ReplaceInvoke - fun postDelayedOfViewProxy(view: View, action: Runnable, delayMillis: Long): Boolean { - return if (Looper.myLooper() != Looper.getMainLooper()) { - UiThreadHandler.postDelayed(action, delayMillis) - } else { - view.postDelayed(action, delayMillis) - } - } +// @JvmStatic +// @TargetClass(value = "android.os.Handler", scope = ALL) +// @TargetMethod(methodName = "post") +// @ReplaceInvoke +// fun handlerPostProxy(handler: Handler, action: Runnable): Boolean { +// if (Looper.getMainLooper() != handler.looper) { +// return handler.post(action) +// } +// val what = ObjectHashCodeUtils.getHashCodeIfNeed(action) +// handler.removeMessages(what) +// val msg = Message.obtain(handler, action) +// msg.what = what +// return handler.sendMessage(msg) +// } +// +// @JvmStatic +// @TargetClass(value = "android.os.Handler", scope = ALL) +// @TargetMethod(methodName = "postDelayed") +// @ReplaceInvoke +// fun handlerPostDelayProxy(handler: Handler, action: Runnable, delayMillis: Long): Boolean { +// if (Looper.getMainLooper() != handler.looper) { +// return handler.postDelayed(action, delayMillis) +// } +// val what = ObjectHashCodeUtils.getHashCodeIfNeed(action) +// handler.removeMessages(what) +// val msg = Message.obtain(handler, action) +// msg.what = what +// return handler.sendMessageDelayed(msg, delayMillis) +// } +// +// @JvmStatic +// @TargetClass(value = "android.app.Activity", scope = ALL) +// @TargetMethod(methodName = "post") +// @ReplaceInvoke +// fun runOnUiThreadOfActivityProxy(activity: Activity, action: Runnable) { +// if (Looper.myLooper() != Looper.getMainLooper()) { +// UiThreadHandler.post(action) +// } else { +// activity.runOnUiThread(action) +// } +// } +// +// @JvmStatic +// @TargetClass(value = "android.view.View", scope = ALL) +// @TargetMethod(methodName = "post") +// @ReplaceInvoke +// fun postOfViewProxy(view: View, action: Runnable): Boolean { +// return if (Looper.myLooper() != Looper.getMainLooper()) { +// UiThreadHandler.post(action) +// } else { +// view.post(action) +// } +// } +// +// @JvmStatic +// @TargetClass(value = "android.view.View", scope = ALL) +// @TargetMethod(methodName = "postDelayed") +// @ReplaceInvoke +// fun postDelayedOfViewProxy(view: View, action: Runnable, delayMillis: Long): Boolean { +// return if (Looper.myLooper() != Looper.getMainLooper()) { +// UiThreadHandler.postDelayed(action, delayMillis) +// } else { +// view.postDelayed(action, delayMillis) +// } +// } } } \ No newline at end of file diff --git a/core/function-impl/mogo-core-function-hmi/src/main/java/com/mogo/eagle/core/function/hmi/ui/msgbox/DriverMsgBoxBubbleView.kt b/core/function-impl/mogo-core-function-hmi/src/main/java/com/mogo/eagle/core/function/hmi/ui/msgbox/DriverMsgBoxBubbleView.kt index 0bf5097f02..28c772f1db 100644 --- a/core/function-impl/mogo-core-function-hmi/src/main/java/com/mogo/eagle/core/function/hmi/ui/msgbox/DriverMsgBoxBubbleView.kt +++ b/core/function-impl/mogo-core-function-hmi/src/main/java/com/mogo/eagle/core/function/hmi/ui/msgbox/DriverMsgBoxBubbleView.kt @@ -70,7 +70,7 @@ class DriverMsgBoxBubbleView @JvmOverloads constructor( } override fun onDataChanged(category: MsgCategory, msgBoxBean: MsgBoxBean) { - UiThreadHandler.post { + UiThreadHandler.post({ when (category) { MsgCategory.NOTICE -> { MsgBoxConfig.noticeList.add(msgBoxBean) @@ -98,7 +98,7 @@ class DriverMsgBoxBubbleView @JvmOverloads constructor( driverMsgBoxBubbleAdapter?.setData(dataList) } } - } + }, UiThreadHandler.MODE.QUEUE) } override fun onAttachedToWindow() { diff --git a/core/mogo-core-utils/src/main/java/com/mogo/eagle/core/utilcode/util/ThreadUtils.java b/core/mogo-core-utils/src/main/java/com/mogo/eagle/core/utilcode/util/ThreadUtils.java index 42044644ee..171817dd7d 100644 --- a/core/mogo-core-utils/src/main/java/com/mogo/eagle/core/utilcode/util/ThreadUtils.java +++ b/core/mogo-core-utils/src/main/java/com/mogo/eagle/core/utilcode/util/ThreadUtils.java @@ -53,6 +53,18 @@ public final class ThreadUtils { private static Executor sDeliver; + public enum MODE { + /** + * 多次post同一个Runnable对象,只会执行最后一次 + */ + LATEST, + + /** + * 多次post,在主线程消息队列中排队执行 + */ + QUEUE + } + /** * Return whether the thread is the main thread. * @@ -63,23 +75,40 @@ public final class ThreadUtils { } public static void runOnUiThread(final Runnable r) { + runOnUiThread(r, MODE.LATEST); + } + + public static void runOnUiThread(final Runnable r, MODE mode) { if (Looper.myLooper() == Looper.getMainLooper()) { r.run(); } else { - int what = ObjectHashCodeUtils.getHashCodeIfNeed(r); - HANDLER.removeMessages(what); - Message msg = Message.obtain(HANDLER, r); - msg.what = what; - HANDLER.sendMessage(msg); + if (mode == MODE.LATEST) { + int what = ObjectHashCodeUtils.getHashCodeIfNeed(r); + HANDLER.removeMessages(what); + Message msg = Message.obtain(HANDLER, r); + msg.what = what; + HANDLER.sendMessage(msg); + } else { + HANDLER.post(r); + } } } public static void runOnUiThreadDelayed(final Runnable r, long delayMillis) { - int what = ObjectHashCodeUtils.getHashCodeIfNeed(r); - HANDLER.removeMessages(what); - Message msg = Message.obtain(HANDLER, r); - msg.what = what; - HANDLER.sendMessageDelayed(msg, delayMillis); + runOnUiThreadDelayed(r, delayMillis, MODE.LATEST); + } + + public static void runOnUiThreadDelayed(final Runnable r, long delayMillis, MODE mode) { + if (mode == MODE.LATEST) { + int what = ObjectHashCodeUtils.getHashCodeIfNeed(r); + HANDLER.removeMessages(what); + Message msg = Message.obtain(HANDLER, r); + msg.what = what; + HANDLER.sendMessageDelayed(msg, delayMillis); + } else { + HANDLER.postDelayed(r, delayMillis); + } + } /** diff --git a/core/mogo-core-utils/src/main/java/com/mogo/eagle/core/utilcode/util/UiThreadHandler.java b/core/mogo-core-utils/src/main/java/com/mogo/eagle/core/utilcode/util/UiThreadHandler.java index a8fc918cda..e4d3f8803a 100644 --- a/core/mogo-core-utils/src/main/java/com/mogo/eagle/core/utilcode/util/UiThreadHandler.java +++ b/core/mogo-core-utils/src/main/java/com/mogo/eagle/core/utilcode/util/UiThreadHandler.java @@ -10,24 +10,58 @@ public class UiThreadHandler { private UiThreadHandler() { } + public enum MODE { + /** + * 多次post同一个Runnable对象,只会执行最后一次 + */ + LATEST, + + /** + * 多次post,在主线程消息队列中排队执行 + */ + QUEUE + } + public static boolean post( Runnable r ) { - int what = ObjectHashCodeUtils.getHashCodeIfNeed(r); - sUiHandler.removeMessages(what); - Message msg = Message.obtain(sUiHandler, r); - msg.what = what; - return sUiHandler.sendMessage(msg); + return post(r, MODE.LATEST); + } + + public static boolean post(Runnable r, MODE mode) { + if (mode == MODE.LATEST) { + int what = ObjectHashCodeUtils.getHashCodeIfNeed(r); + sUiHandler.removeMessages(what); + Message msg = Message.obtain(sUiHandler, r); + msg.what = what; + return sUiHandler.sendMessage(msg); + } + return sUiHandler.post(r); } public static boolean postDelayed( Runnable r, long delayMillis ) { - int what = ObjectHashCodeUtils.getHashCodeIfNeed(r); - sUiHandler.removeMessages(what); - Message msg = Message.obtain(sUiHandler, r); - msg.what = what; - return sUiHandler.sendMessageDelayed(msg, delayMillis); + return postDelayed(r, delayMillis, MODE.LATEST); + } + + public static boolean postDelayed( Runnable r, long delayMillis, MODE mode ) { + if (mode == MODE.LATEST) { + int what = ObjectHashCodeUtils.getHashCodeIfNeed(r); + sUiHandler.removeMessages(what); + Message msg = Message.obtain(sUiHandler, r); + msg.what = what; + return sUiHandler.sendMessageDelayed(msg, delayMillis); + } + return sUiHandler.postDelayed(r, delayMillis); } public static void removeCallbacks( Runnable r ) { - int what = ObjectHashCodeUtils.getHashCodeIfNeed(r); - sUiHandler.removeMessages(what); + removeCallbacks(r, MODE.LATEST); + } + + public static void removeCallbacks( Runnable r, MODE mode ) { + if (mode == MODE.LATEST) { + int what = ObjectHashCodeUtils.getHashCodeIfNeed(r); + sUiHandler.removeMessages(what); + } else { + sUiHandler.removeCallbacks(r); + } } } \ No newline at end of file