[6.2.6][技术优化] 优化View post和Activity runOnUiThread逻辑
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user