[dev_arch_opt_3.0] 优化UiThreadHandler和ThreadUtils切换主线程的逻辑,避免主线程消息队列过多,造成卡顿
This commit is contained in:
@@ -65,8 +65,7 @@ public class BusAnalyticsManager {
|
||||
}
|
||||
|
||||
private void removeWaitingCallback() {
|
||||
if (startAutopilotRunnable != null &&
|
||||
UiThreadHandler.getsUiHandler().hasCallbacks(startAutopilotRunnable)) {
|
||||
if (startAutopilotRunnable != null) {
|
||||
UiThreadHandler.removeCallbacks(startAutopilotRunnable);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,8 +68,7 @@ public class SweeperAnalyticsManager {
|
||||
|
||||
if (send) {
|
||||
// 开启成功,上报埋点
|
||||
if (startAutopilotRunnable != null &&
|
||||
UiThreadHandler.getsUiHandler().hasCallbacks(startAutopilotRunnable)) {
|
||||
if (startAutopilotRunnable != null) {
|
||||
UiThreadHandler.removeCallbacks(startAutopilotRunnable);
|
||||
}
|
||||
mStartAutopilotParams.put(SweeperConst.EVENT_PARAM_START_RESULT, true);
|
||||
|
||||
@@ -15,7 +15,7 @@ public class OrderServiceImpl extends ILeftMenuService.Stub {
|
||||
@Override
|
||||
public void transmissionIndex(int index) throws RemoteException {
|
||||
CallerLogger.INSTANCE.d(SceneConstant.M_TAXI_P + TAG, "跨进程 transmissionIndex"+index);
|
||||
UiThreadHandler.getsUiHandler().post(() -> {
|
||||
UiThreadHandler.post(() -> {
|
||||
OverlayLeftViewUtils.INSTANCE.transmissionIndexGet(index);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -123,25 +123,25 @@ object LeftMenuOpen {
|
||||
windowManager?.let { windowManager ->
|
||||
if (it.x > NEGATIVEDEVIATION && movedX > 0) {
|
||||
open?.let { it1 ->
|
||||
UiThreadHandler.getsUiHandler().post {
|
||||
UiThreadHandler.post {
|
||||
it1(windowView, windowManager)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//更新悬浮球控件位置
|
||||
UiThreadHandler.getsUiHandler().post {
|
||||
UiThreadHandler.post {
|
||||
windowManager.updateViewLayout(windowView, it)
|
||||
}
|
||||
}
|
||||
if (it.x < OverlayLeftViewUtils.DEVIATION_WIDTH + DEVIATION && movedX < 0) {
|
||||
close?.let { it1 ->
|
||||
UiThreadHandler.getsUiHandler().post {
|
||||
UiThreadHandler.post {
|
||||
it1(windowView, windowManager)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//更新悬浮球控件位置
|
||||
UiThreadHandler.getsUiHandler().post {
|
||||
UiThreadHandler.post {
|
||||
windowManager.updateViewLayout(windowView, it)
|
||||
}
|
||||
}
|
||||
@@ -157,14 +157,14 @@ object LeftMenuOpen {
|
||||
if (startX > OverlayLeftViewUtils.DEVIATION_WIDTH / 2 && startX < 0) {
|
||||
//拖动距离大于一半 自动打开
|
||||
open?.let {
|
||||
UiThreadHandler.getsUiHandler().post {
|
||||
UiThreadHandler.post {
|
||||
it(windowView!!, windowManager)
|
||||
}
|
||||
}
|
||||
} else if (startX < OverlayLeftViewUtils.DEVIATION_WIDTH / 2 && startX >= OverlayLeftViewUtils.DEVIATION_WIDTH) {
|
||||
// 拖动距离小于一半自动关闭
|
||||
close?.let {
|
||||
UiThreadHandler.getsUiHandler().post {
|
||||
UiThreadHandler.post {
|
||||
it(windowView!!, windowManager)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,13 +177,13 @@ object FloatingDistanceInfoUtils {
|
||||
fun setSpeed(speed: Float) {
|
||||
val newSpeed: Int = (Math.abs(speed) * 3.6f).toInt() // 倒车时工控机反馈定位信息中speed为负值
|
||||
|
||||
UiThreadHandler.getsUiHandler().post {
|
||||
UiThreadHandler.post {
|
||||
actvSpeed?.text = "$newSpeed"
|
||||
}
|
||||
}
|
||||
|
||||
fun setDistance(meters:Long,remainDis: String?, disUnit: String, time: Int, arriveTime: String) {
|
||||
UiThreadHandler.getsUiHandler().post {
|
||||
UiThreadHandler.post {
|
||||
actvDistancew?.text = "$remainDis"
|
||||
actvDistancewUnit?.text = "距离($disUnit)"
|
||||
actvSurplusTime?.text = "$time"
|
||||
|
||||
@@ -78,8 +78,7 @@ public class TaxiPassengerAnalyticsManager {
|
||||
}
|
||||
|
||||
private void removeWaitingCallback() {
|
||||
if (startAutopilotRunnable != null &&
|
||||
UiThreadHandler.getsUiHandler().hasCallbacks(startAutopilotRunnable)) {
|
||||
if (startAutopilotRunnable != null) {
|
||||
UiThreadHandler.removeCallbacks(startAutopilotRunnable);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,8 +47,7 @@ public class TaxiAnalyticsManager {
|
||||
}
|
||||
|
||||
private void removeWaitingCallback() {
|
||||
if (startAutopilotRunnable != null &&
|
||||
UiThreadHandler.getsUiHandler().hasCallbacks(startAutopilotRunnable)) {
|
||||
if (startAutopilotRunnable != null) {
|
||||
UiThreadHandler.removeCallbacks(startAutopilotRunnable);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,6 +64,9 @@ if (!isAndroidTestBuild()) {
|
||||
memory_leak {
|
||||
enable true
|
||||
}
|
||||
object_hashcode {
|
||||
enable true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import androidx.lifecycle.*
|
||||
import androidx.lifecycle.Lifecycle.Event
|
||||
import androidx.lifecycle.Lifecycle.Event.ON_DESTROY
|
||||
import com.knightboost.lancet.api.*
|
||||
import com.knightboost.lancet.api.Scope.LEAF
|
||||
import com.knightboost.lancet.api.annotations.*
|
||||
import com.knightboost.lancet.api.annotations.Weaver
|
||||
import com.mogo.eagle.core.utilcode.kotlin.*
|
||||
@@ -24,8 +25,9 @@ import java.util.concurrent.ConcurrentHashMap
|
||||
@Group("memory_leak")
|
||||
class MemoryLeakFix {
|
||||
|
||||
@Insert
|
||||
@ImplementedInterface("java.lang.Runnable")
|
||||
@NameRegex("(com\\.mogo|com\\.zhidao|com\\.elegant|com\\.zhidaoauto|com\\.zhjt).*")
|
||||
@Insert(mayCreateSuper = true)
|
||||
@ImplementedInterface("java.lang.Runnable", scope = LEAF)
|
||||
@TargetMethod(methodName = "run")
|
||||
fun runProxy() {
|
||||
if (AccessSyntheticUtils.isTargetAlive(This.get())) {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.mogo.launcher.lancet;
|
||||
|
||||
import androidx.annotation.Keep;
|
||||
import com.knightboost.lancet.api.Scope;
|
||||
import com.knightboost.lancet.api.annotations.Group;
|
||||
import com.knightboost.lancet.api.annotations.ImplementedInterface;
|
||||
import com.knightboost.lancet.api.annotations.NameRegex;
|
||||
import com.knightboost.lancet.api.annotations.ReplaceInvoke;
|
||||
import com.knightboost.lancet.api.annotations.TargetMethod;
|
||||
import com.knightboost.lancet.api.annotations.Weaver;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Weaver
|
||||
@Group("object_hashcode")
|
||||
@Keep
|
||||
public class ObjectHashCodeProxy {
|
||||
|
||||
public static final ConcurrentHashMap<String, Integer> hashCodes = new ConcurrentHashMap<>();
|
||||
|
||||
@NameRegex("(com\\.mogo\\.eagle\\.core\\.utilcode\\.util\\.ThreadUtils|com\\.mogo\\.eagle\\.core\\.utilcode\\.util\\.UiThreadHandler)")
|
||||
@ImplementedInterface(value = "java.lang.Object",scope = Scope.SELF)
|
||||
@TargetMethod(methodName = "hashCode")
|
||||
@ReplaceInvoke
|
||||
public static int hashCodeProxy(Object r) {
|
||||
Class<?> clazz = r.getClass();
|
||||
String className = clazz.getName();
|
||||
if (hashCodes.containsKey(className)) {
|
||||
Integer value = hashCodes.get(className);
|
||||
if (value != null) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
int hashCode = System.identityHashCode(r);
|
||||
hashCodes.put(className, hashCode);
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.mogo.eagle.core.utilcode.util;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -61,20 +62,24 @@ public final class ThreadUtils {
|
||||
return Looper.myLooper() == Looper.getMainLooper();
|
||||
}
|
||||
|
||||
public static Handler getMainHandler() {
|
||||
return HANDLER;
|
||||
}
|
||||
|
||||
public static void runOnUiThread(final Runnable runnable) {
|
||||
public static void runOnUiThread(final Runnable r) {
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
runnable.run();
|
||||
r.run();
|
||||
} else {
|
||||
HANDLER.post(runnable);
|
||||
int what = r.hashCode();
|
||||
HANDLER.removeMessages(what);
|
||||
Message msg = Message.obtain(HANDLER, r);
|
||||
msg.what = what;
|
||||
HANDLER.sendMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void runOnUiThreadDelayed(final Runnable runnable, long delayMillis) {
|
||||
HANDLER.postDelayed(runnable, delayMillis);
|
||||
public static void runOnUiThreadDelayed(final Runnable r, long delayMillis) {
|
||||
int what = r.hashCode();
|
||||
HANDLER.removeMessages(what);
|
||||
Message msg = Message.obtain(HANDLER, r);
|
||||
msg.what = what;
|
||||
HANDLER.sendMessageDelayed(msg, delayMillis);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,40 +2,32 @@ package com.mogo.eagle.core.utilcode.util;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
|
||||
public class UiThreadHandler {
|
||||
|
||||
private static final Handler sUiHandler = new Handler( Looper.getMainLooper() );
|
||||
|
||||
private static final Object sToken = new Object();
|
||||
|
||||
public UiThreadHandler() {
|
||||
}
|
||||
private UiThreadHandler() { }
|
||||
|
||||
public static boolean post( Runnable r ) {
|
||||
return sUiHandler != null && sUiHandler.post( r );
|
||||
int what = r.hashCode();
|
||||
sUiHandler.removeMessages(what);
|
||||
Message msg = Message.obtain(sUiHandler, r);
|
||||
msg.what = what;
|
||||
return sUiHandler.sendMessage(msg);
|
||||
}
|
||||
|
||||
public static boolean postDelayed( Runnable r, long delayMillis ) {
|
||||
return sUiHandler != null && sUiHandler.postDelayed( r, delayMillis );
|
||||
int what = r.hashCode();
|
||||
sUiHandler.removeMessages(what);
|
||||
Message msg = Message.obtain(sUiHandler, r);
|
||||
msg.what = what;
|
||||
return sUiHandler.sendMessageDelayed(msg, delayMillis);
|
||||
}
|
||||
|
||||
public static Handler getsUiHandler() {
|
||||
return sUiHandler;
|
||||
}
|
||||
|
||||
public static boolean postOnceDelayed( Runnable r, long delayMillis ) {
|
||||
if ( sUiHandler == null ) {
|
||||
return false;
|
||||
} else {
|
||||
sUiHandler.removeCallbacks( r, sToken );
|
||||
return sUiHandler.postDelayed( r, delayMillis );
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeCallbacks( Runnable runnable ) {
|
||||
if ( sUiHandler != null ) {
|
||||
sUiHandler.removeCallbacks( runnable );
|
||||
}
|
||||
public static void removeCallbacks( Runnable r ) {
|
||||
int what = r.hashCode();
|
||||
sUiHandler.removeMessages(what);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user