一、簡介
TracePlugin這個模塊中包含EvilMethodTracer(函數(shù)耗時檢測)、StartupTracer(啟動時間檢測)、FrameTracer(幀率檢測)、AnrTracer(ANR檢測)。
二、功能簡介
TracePlugin中主要根據(jù)Activity生命周期的對4個Tracer的開啟、關(guān)閉進(jìn)行管理。
三、主要方法解析
1、init方法
該方法主要是對4個Tracer進(jìn)行初始化操作,代碼如下:
@Override
public void init(Application app, PluginListener listener) {
super.init(app, listener);
MatrixLog.i(TAG, "trace plugin init, trace config: %s", traceConfig.toString());
//APi小于16 不支持
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
MatrixLog.e(TAG, "[FrameBeat] API is low Build.VERSION_CODES.JELLY_BEAN(16), TracePlugin is not supported");
unSupportPlugin();
return;
}
anrTracer = new AnrTracer(traceConfig);
frameTracer = new FrameTracer(traceConfig);
evilMethodTracer = new EvilMethodTracer(traceConfig);
startupTracer = new StartupTracer(traceConfig);
}
2、TracePlugin.start()
在start方法中主要是啟動UIThreadMonitor、AppMethodBeat和4個Tracer。關(guān)于UIThreadMonitor和AppMethodBeat的作用后續(xù)在解析Tracer時會分析。
@Override
public void start() {
super.start();
if (!isSupported()) {
MatrixLog.w(TAG, "[start] Plugin is unSupported!");
return;
}
MatrixLog.w(TAG, "start!");
Runnable runnable = new Runnable() {
@Override
public void run() {
//初始化 UIThreadMonitor
if (!UIThreadMonitor.getMonitor().isInit()) {
try {
UIThreadMonitor.getMonitor().init(traceConfig);
} catch (java.lang.RuntimeException e) {
MatrixLog.e(TAG, "[start] RuntimeException:%s", e);
return;
}
}
//啟動 AppMethodBeat
AppMethodBeat.getInstance().onStart();
//啟動 UIThreadMonitor
UIThreadMonitor.getMonitor().onStart();
anrTracer.onStartTrace();
frameTracer.onStartTrace();
evilMethodTracer.onStartTrace();
startupTracer.onStartTrace();
}
};
if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
runnable.run();
} else {
//提示 warning TracePlugin 應(yīng)該在 主線程啟動
MatrixLog.w(TAG, "start TracePlugin in Thread[%s] but not in mainThread!", Thread.currentThread().getId());
//post到主線程啟動
MatrixHandlerThread.getDefaultMainHandler().post(runnable);
}
}
3、TracePlugin.stop()
stop()中主要是在主線程中關(guān)閉和停止Tracer、AppMethodBeat、UIThreadMonitor,代碼如下:
@Override
public void stop() {
super.stop();
if (!isSupported()) {
MatrixLog.w(TAG, "[stop] Plugin is unSupported!");
return;
}
MatrixLog.w(TAG, "stop!");
Runnable runnable = new Runnable() {
@Override
public void run() {
AppMethodBeat.getInstance().onStop();
UIThreadMonitor.getMonitor().onStop();
anrTracer.onCloseTrace();
frameTracer.onCloseTrace();
evilMethodTracer.onCloseTrace();
startupTracer.onCloseTrace();
}
};
if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
runnable.run();
} else {
MatrixLog.w(TAG, "stop TracePlugin in Thread[%s] but not in mainThread!", Thread.currentThread().getId());
MatrixHandlerThread.getDefaultMainHandler().post(runnable);
}
}
4、TracePlugin.onForeground()
onForeground就是將App當(dāng)前處于前臺或者是后臺的狀態(tài)分發(fā)給各個Tracer。代碼如下:
@Override
public void onForeground(boolean isForeground) {
super.onForeground(isForeground);
if (!isSupported()) {
return;
}
if (frameTracer != null) {
frameTracer.onForeground(isForeground);
}
if (anrTracer != null) {
anrTracer.onForeground(isForeground);
}
if (evilMethodTracer != null) {
evilMethodTracer.onForeground(isForeground);
}
if (startupTracer != null) {
startupTracer.onForeground(isForeground);
}
}
總結(jié)
- TracePlugin中有4個主要方法,分別是init,onStart,onStop,onForeground。
- TracePlugin中主要是對Tracer、AppMethodBeat、UIThreadMonitor進(jìn)行初始化、啟動、暫停以及App前后臺狀態(tài)分發(fā)等操作。