[3.4.0][Block] 卡顿检测代码性能优化
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package com.mogo.launcher.lancet;
|
||||
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
|
||||
import androidx.annotation.Keep;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.knightboost.lancet.api.Scope;
|
||||
import com.knightboost.lancet.api.annotations.Group;
|
||||
import com.knightboost.lancet.api.annotations.ReplaceInvoke;
|
||||
import com.knightboost.lancet.api.annotations.TargetClass;
|
||||
import com.knightboost.lancet.api.annotations.TargetMethod;
|
||||
import com.knightboost.lancet.api.annotations.Weaver;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
import kotlin.coroutines.CoroutineContext;
|
||||
import kotlinx.coroutines.CoroutineExceptionHandler;
|
||||
import kotlinx.coroutines.Dispatchers;
|
||||
import kotlinx.coroutines.MainCoroutineDispatcher;
|
||||
|
||||
|
||||
@Keep
|
||||
@Weaver
|
||||
@Group("main_block_check")
|
||||
public class AsyncHandlerReflectLancet {
|
||||
|
||||
|
||||
@TargetClass(value = "java.lang.reflect.Method",scope = Scope.SELF)
|
||||
@TargetMethod(methodName = "invoke")
|
||||
@ReplaceInvoke
|
||||
public static Object hookMethodInvoke(Method method, Object obj, Object... args) throws InvocationTargetException, IllegalAccessException {
|
||||
Object ret = method.invoke(obj, args);
|
||||
if (ret instanceof Handler && "createAsync".equals(method.getName())) {
|
||||
CopyOnWriteArraySet<WeakReference<Handler>> asyncHandlers = MainBlockCheck.Companion.getAsyncHandlers();
|
||||
asyncHandlers.add(new WeakReference<>((Handler) ret));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@TargetClass(value = "android.os.Handler",scope = Scope.SELF)
|
||||
@TargetMethod(methodName = "createAsync")
|
||||
@ReplaceInvoke(isStatic = true)
|
||||
public static Handler createAsync(Looper looper) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
Handler handler = Handler.createAsync(looper);
|
||||
CopyOnWriteArraySet<WeakReference<Handler>> asyncHandlers = MainBlockCheck.Companion.getAsyncHandlers();
|
||||
asyncHandlers.add(new WeakReference<>(handler));
|
||||
return handler;
|
||||
} else {
|
||||
return new Handler(looper);
|
||||
}
|
||||
}
|
||||
|
||||
@TargetClass(value = "android.os.Handler",scope = Scope.SELF)
|
||||
@TargetMethod(methodName = "createAsync")
|
||||
@ReplaceInvoke(isStatic = true)
|
||||
public static Handler createAsync(Looper looper, Handler.Callback callback) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
Handler handler = Handler.createAsync(looper, callback);
|
||||
CopyOnWriteArraySet<WeakReference<Handler>> asyncHandlers = MainBlockCheck.Companion.getAsyncHandlers();
|
||||
asyncHandlers.add(new WeakReference<>(handler));
|
||||
return handler;
|
||||
} else {
|
||||
return new Handler(looper, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user