[2.15.0] 优化子线程切主线程操作

This commit is contained in:
renwj
2023-03-27 20:40:51 +08:00
parent e14bde9129
commit 0beb152ab0
5 changed files with 64 additions and 53 deletions

View File

@@ -0,0 +1,40 @@
package com.mogo.launcher.lancet;
import android.os.Looper;
import android.util.Log;
import android.widget.TextView;
import com.knightboost.lancet.api.Scope;
import com.knightboost.lancet.api.annotations.Group;
import com.knightboost.lancet.api.annotations.NameRegex;
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;
@Weaver
@Group("textview_opt")
public class TextViewOpt {
@NameRegex("(com\\.mogo|com\\.zhidao|com\\.elegant|com\\.zhidaoauto|com\\.zhjt).*")
@TargetClass(value = "android.widget.TextView",scope = Scope.ALL)
@TargetMethod(methodName = "setText")
@ReplaceInvoke
public static void setText(TextView view, CharSequence text) {
if (Looper.myLooper() != Looper.getMainLooper()) {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
StringBuilder sb = new StringBuilder();
for (StackTraceElement trace : stackTrace) {
sb.append(trace.getClassName()).append(".").append(trace.getMethodName()).append("(").append(trace.getLineNumber()).append(")").append("\n");
}
if (sb.length() > 1) {
sb.setLength(sb.length() - 1);
}
Log.e("ALERT", sb.toString());
return;
}
CharSequence old = view.getText();
if (old != null && !old.equals(text)) {
view.setText(text);
}
}
}