[6.2.6][技术优化] 优化View post和Activity runOnUiThread逻辑

This commit is contained in:
renwj
2023-12-26 15:08:59 +08:00
parent 99e093fc98
commit c5946610a8
3 changed files with 53 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
package com.mogo.launcher.lancet.jank.ui
import android.app.Activity
import android.os.SystemClock
import android.view.View
import com.knightboost.lancet.api.Scope
import com.knightboost.lancet.api.annotations.Group
@@ -10,11 +11,28 @@ import com.knightboost.lancet.api.annotations.TargetMethod
import com.knightboost.lancet.api.annotations.Weaver
import com.mogo.eagle.core.block.runtime.message.Message
import com.mogo.eagle.core.function.call.devatools.CallerDevaToolsManager
import com.mogo.launcher.R
@Weaver
@Group("main_block_check")
class UiPostLancet {
class ActionWrapper(private val extra:Map<String, String>, private val delegate: Runnable): Runnable {
override fun run() {
val startTime = SystemClock.uptimeMillis()
try {
delegate.run()
} finally {
val duration = SystemClock.uptimeMillis() - startTime
CallerDevaToolsManager.block()?.takeIf {
it.hasInit()
}?.recorder()?.recycle(Message.acquire(handler = null, action = delegate, duration = duration, extra = extra))
}
}
}
companion object {
@JvmStatic
@@ -22,12 +40,13 @@ class UiPostLancet {
@TargetMethod(methodName = "runOnUiThread")
@ReplaceInvoke
fun runOnUiThreadOfActivity(activity: Activity, action: Runnable) {
val extra = mapOf(activity.javaClass.name to "runOnUiThread")
try {
activity.runOnUiThread(action)
activity.runOnUiThread(ActionWrapper(extra, action))
} finally {
CallerDevaToolsManager.block()?.takeIf {
it.hasInit()
}?.recorder()?.insert(Message.acquire(handler = null, action = action, extra = mapOf(activity.javaClass.name to "runOnUiThread")))
}?.recorder()?.insert(Message.acquire(handler = null, action = action, extra = extra))
}
}
@@ -36,12 +55,20 @@ class UiPostLancet {
@TargetMethod(methodName = "post")
@ReplaceInvoke
fun post(view: View, action: Runnable): Boolean {
return try {
view.post(action)
val extra = mapOf(view.javaClass.name to "post")
try {
var map = view.getTag(R.id.action_wrapper_tag_id) as? HashMap<Runnable, ActionWrapper>
if (map == null) {
map = HashMap()
view.setTag(R.id.action_wrapper_tag_id, map)
}
val wrapper = ActionWrapper(extra, action)
map[action] = wrapper
return view.post(wrapper)
} finally {
CallerDevaToolsManager.block()?.takeIf {
it.hasInit()
}?.recorder()?.insert(Message.acquire(handler = null, action = action, extra = mapOf(view.javaClass.name to "post")))
}?.recorder()?.insert(Message.acquire(handler = null, action = action, extra = extra))
}
}
@@ -50,12 +77,20 @@ class UiPostLancet {
@TargetMethod(methodName = "postDelayed")
@ReplaceInvoke
fun postDelayed(view: View, action: Runnable, delayMillis: Long): Boolean {
return try {
view.postDelayed(action, delayMillis)
val extra = mapOf(view.javaClass.name to "postDelayed")
try {
var map = view.getTag(R.id.action_wrapper_tag_id) as? HashMap<Runnable, ActionWrapper>
if (map == null) {
map = HashMap()
view.setTag(R.id.action_wrapper_tag_id, map)
}
val wrapper = ActionWrapper(extra, action)
map[action] = wrapper
return view.postDelayed(wrapper, delayMillis)
} finally {
CallerDevaToolsManager.block()?.takeIf {
it.hasInit()
}?.recorder()?.insert(Message.acquire(handler = null, action = action, delay = delayMillis, extra = mapOf(view.javaClass.name to "postDelayed")))
}?.recorder()?.insert(Message.acquire(handler = null, action = action, delay = delayMillis, extra = extra))
}
}
@@ -64,8 +99,14 @@ class UiPostLancet {
@TargetMethod(methodName = "removeCallbacks")
@ReplaceInvoke
fun removeCallbacks(view: View, action: Runnable): Boolean {
return try {
view.removeCallbacks(action)
try {
val tag = view.getTag(R.id.action_wrapper_tag_id)
if (tag != null && tag is HashMap<*, *>) {
(tag.remove(action) as? Runnable)?.also {
return view.removeCallbacks(it)
}
}
return false
} finally {
CallerDevaToolsManager.block()?.takeIf {
it.hasInit()