Skip to content

Commit 0ba2077

Browse files
committed
Merge branch 'feature/dev' into dev
2 parents c0682da + 68d7507 commit 0ba2077

File tree

63 files changed

+1748
-777
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1748
-777
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
![Matrix-icon](assets/img/readme/header.png)
22

3-
[![license](http://img.shields.io/badge/license-BSD3-brightgreen.svg?style=flat)](https://github.com/Tencent/matrix/blob/master/LICENSE)[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/matrix/pulls)[![WeChat Approved](https://img.shields.io/badge/Wechat%20Approved-0.5.2-red.svg)](https://github.com/Tencent/matrix/wiki)
3+
[![license](http://img.shields.io/badge/license-BSD3-brightgreen.svg?style=flat)](https://github.com/Tencent/matrix/blob/master/LICENSE)[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/matrix/pulls)[![WeChat Approved](https://img.shields.io/badge/Wechat%20Approved-0.5.4-red.svg)](https://github.com/Tencent/matrix/wiki)
44

55
(中文版本请参看[这里](#matrix_cn))
66

@@ -172,7 +172,7 @@ At this point, Matrix has been integrated into the app and is beginning to colle
172172

173173
1. Configure `MATRIX_VERSION` in gradle.properties.
174174
``` gradle
175-
MATRIX_VERSION=0.5.2
175+
MATRIX_VERSION=0.5.4
176176
```
177177

178178
2. Add `matrix-gradle-plugin` in your build.gradle:
@@ -289,11 +289,11 @@ You can get more about Matrix output at the wiki [The output of Matrix](https://
289289

290290
#### APK Checker Usage
291291

292-
APK Checker can run independently in Jar ([matrix-apk-canary-0.5.2.jar](https://jcenter.bintray.com/com/tencent/matrix/matrix-apk-canary/0.5.2/matrix-apk-canary-0.5.2.jar)) mode, usage:
292+
APK Checker can run independently in Jar ([matrix-apk-canary-0.5.4.jar](https://jcenter.bintray.com/com/tencent/matrix/matrix-apk-canary/0.5.4/matrix-apk-canary-0.5.4.jar)) mode, usage:
293293

294294

295295
```shell
296-
java -jar matrix-apk-canary-0.5.2.jar
296+
java -jar matrix-apk-canary-0.5.4.jar
297297
Usages:
298298
--config CONFIG-FILE-PATH
299299
or
@@ -352,14 +352,14 @@ Matrix is under the BSD license. See the [LICENSE](https://github.com/Tencent/Ma
352352
353353
# <a name="matrix_cn">Matrix</a>
354354
![Matrix-icon](assets/img/readme/header.png)
355-
[![license](http://img.shields.io/badge/license-BSD3-brightgreen.svg?style=flat)](https://github.com/Tencent/matrix/blob/master/LICENSE)[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/matrix/pulls) [![WeChat Approved](https://img.shields.io/badge/Wechat%20Approved-0.5.2-red.svg)](https://github.com/Tencent/matrix/wiki)
355+
[![license](http://img.shields.io/badge/license-BSD3-brightgreen.svg?style=flat)](https://github.com/Tencent/matrix/blob/master/LICENSE)[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/matrix/pulls) [![WeChat Approved](https://img.shields.io/badge/Wechat%20Approved-0.5.4-red.svg)](https://github.com/Tencent/matrix/wiki)
356356
357-
**Matrix** 是一款微信团队研发并日常使用的应用性能接入框架,支持iOS, macOS和Android。
357+
**Matrix** 是一款微信研发并日常使用的应用性能接入框架,支持iOS, macOS和Android。
358358
Matrix 通过接入各种性能监控方案,对性能监控项的异常数据进行采集和分析,输出相应的问题分析、定位与优化建议,从而帮助开发者开发出更高质量的应用。
359359
360360
# <a name='matrix_ios_cn'>Matrix for iOS/macOS </a>
361361
362-
Matrix-iOS 当前工具监控范围包括:崩溃、卡顿和爆内存,包含以下两款插件:
362+
当前工具监控范围包括:崩溃、卡顿和爆内存,包含以下两款插件:
363363
364364
* **WCCrashBlockMonitorPlugin:** 基于 [KSCrash](https://github.com/kstenerud/KSCrash) 框架开发,具有业界领先的卡顿堆栈捕获能力,同时兼备崩溃捕获能力。
365365
@@ -510,7 +510,7 @@ Matrix-android 当前监控范围包括:应用安装包大小,帧率变化
510510

511511
1. 在你项目根目录下的 gradle.properties 中配置要依赖的 Matrix 版本号,如:
512512
``` gradle
513-
MATRIX_VERSION=0.5.2
513+
MATRIX_VERSION=0.5.4
514514
```
515515

516516
2. 在你项目根目录下的 build.gradle 文件添加 Matrix 依赖,如:
@@ -624,10 +624,10 @@ Matrix 分析后的输出字段的含义请查看 [Matrix 输出内容的含义
624624
625625
#### APK Checker
626626
627-
APK Check 以独立的 jar 包提供 ([matrix-apk-canary-0.5.2.jar](https://jcenter.bintray.com/com/tencent/matrix/matrix-apk-canary/0.5.2/matrix-apk-canary-0.5.2.jar)),你可以运行:
627+
APK Check 以独立的 jar 包提供 ([matrix-apk-canary-0.5.4.jar](https://jcenter.bintray.com/com/tencent/matrix/matrix-apk-canary/0.5.4/matrix-apk-canary-0.5.4.jar)),你可以运行:
628628
629629
```cmd
630-
java -jar matrix-apk-canary-0.5.2.jar
630+
java -jar matrix-apk-canary-0.5.4.jar
631631
```
632632
633633
查看 Usages 来使用它。

matrix/matrix-android/gradle.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryErro
1010
#
1111
# When configured, Gradle will run in incubating parallel mode.
1212
# This option should only be used with decoupled projects. More details, visit
13-
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13+
# http://www.gradle.org/docs/current/userguide/multifa_project_builds.html#sec:decoupled_projects
1414
# org.gradle.parallel=true
1515
#Tue Jun 20 10:24:33 CST 2017
1616

17-
VERSION_NAME_PREFIX=0.5.2
17+
18+
VERSION_NAME_PREFIX=0.5.4
1819
VERSION_NAME_SUFFIX=

matrix/matrix-android/matrix-android-lib/build.gradle

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,12 @@ dependencies {
2323
testImplementation 'junit:junit:4.12'
2424

2525
androidTestImplementation 'com.android.support.test:runner:1.0.1'
26-
26+
implementation 'com.android.support:appcompat-v7:28.0.0'
27+
androidTestImplementation 'com.android.support:support-annotations:28.0.0'
2728
api 'com.android.support:support-annotations:27.0.2'
2829
}
2930

3031
version = rootProject.ext.VERSION_NAME
3132
group = rootProject.ext.GROUP
3233

3334
apply from: rootProject.file('gradle/android-publish.gradle')
34-
35-
36-
37-
//apply plugin: 'matrix-trace-plugin'
38-
//matrixTrace {
39-
// packageName = "com.tencent.matrix"
40-
// baseMethodMap = "${rootProject.buildDir}/outputs/matrix-trace/Release.methodmap"
41-
// methodMapDir = "${rootProject.buildDir}/outputs/matrix-trace"
42-
// mappingPath = "${project.buildDir}/outputs/mapping/release/mapping.txt"
43-
// jarDir = "${project.buildDir}/intermediates/transforms/proguard/release/jars/3/1f/main.jar"
44-
// blackListDir = "${rootProject.rootDir}/matrixTrace/blackMethodList.txt"
45-
//}
46-
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package com.tencent.matrix;
2+
3+
import android.app.Activity;
4+
import android.app.Application;
5+
import android.content.ComponentCallbacks2;
6+
import android.content.res.Configuration;
7+
import android.os.Build;
8+
import android.os.Bundle;
9+
import android.text.TextUtils;
10+
import android.util.ArrayMap;
11+
12+
import com.tencent.matrix.listeners.IAppForeground;
13+
import com.tencent.matrix.util.MatrixLog;
14+
15+
import java.lang.reflect.Field;
16+
import java.util.Collections;
17+
import java.util.HashMap;
18+
import java.util.HashSet;
19+
import java.util.Map;
20+
import java.util.Set;
21+
22+
23+
public enum AppActiveMatrixDelegate {
24+
25+
INSTANCE;
26+
27+
private static final String TAG = "Matrix.AppActiveMatrixDelegate";
28+
private Set<IAppForeground> listeners = Collections.synchronizedSet(new HashSet<IAppForeground>());
29+
private boolean isAppForeground = false;
30+
private String visibleScene = "default";
31+
private Controller controller = new Controller();
32+
private boolean isInited = false;
33+
private String currentFragmentName;
34+
35+
public void init(Application application) {
36+
if (isInited) {
37+
MatrixLog.e(TAG, "has inited!");
38+
return;
39+
}
40+
this.isInited = true;
41+
application.registerComponentCallbacks(controller);
42+
application.registerActivityLifecycleCallbacks(controller);
43+
}
44+
45+
public String getCurrentFragmentName() {
46+
return currentFragmentName;
47+
}
48+
49+
/**
50+
* must set after {@link Activity#onStart()}
51+
*
52+
* @param fragmentName
53+
*/
54+
public void setCurrentFragmentName(String fragmentName) {
55+
MatrixLog.i(TAG, "[setCurrentFragmentName] fragmentName:%s", fragmentName);
56+
this.currentFragmentName = fragmentName;
57+
updateScene(fragmentName);
58+
}
59+
60+
public String getVisibleScene() {
61+
return visibleScene;
62+
}
63+
64+
private void onDispatchForeground(String visibleScene) {
65+
if (isAppForeground || !isInited) {
66+
return;
67+
}
68+
69+
MatrixLog.i(TAG, "onForeground... visibleScene[%s]", visibleScene);
70+
try {
71+
for (IAppForeground listener : listeners) {
72+
listener.onForeground(true);
73+
}
74+
} finally {
75+
isAppForeground = true;
76+
}
77+
}
78+
79+
private void onDispatchBackground(String visibleScene) {
80+
if (!isAppForeground || !isInited) {
81+
return;
82+
}
83+
MatrixLog.i(TAG, "onBackground... visibleScene[%s]", visibleScene);
84+
try {
85+
for (IAppForeground listener : listeners) {
86+
listener.onForeground(false);
87+
}
88+
} finally {
89+
isAppForeground = false;
90+
}
91+
}
92+
93+
public boolean isAppForeground() {
94+
return isAppForeground;
95+
}
96+
97+
public void addListener(IAppForeground listener) {
98+
listeners.add(listener);
99+
}
100+
101+
public void removeListener(IAppForeground listener) {
102+
listeners.remove(listener);
103+
}
104+
105+
106+
private final class Controller implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 {
107+
108+
@Override
109+
public void onActivityStarted(Activity activity) {
110+
updateScene(activity);
111+
if (!isAppForeground) {
112+
onDispatchForeground(getVisibleScene());
113+
}
114+
}
115+
116+
117+
@Override
118+
public void onActivityStopped(Activity activity) {
119+
if (getTopActivityName() == null) {
120+
onDispatchBackground(getVisibleScene());
121+
}
122+
}
123+
124+
125+
@Override
126+
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
127+
128+
}
129+
130+
@Override
131+
public void onActivityDestroyed(Activity activity) {
132+
133+
}
134+
135+
@Override
136+
public void onActivityResumed(Activity activity) {
137+
138+
}
139+
140+
@Override
141+
public void onActivityPaused(Activity activity) {
142+
143+
}
144+
145+
@Override
146+
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
147+
148+
}
149+
150+
@Override
151+
public void onConfigurationChanged(Configuration newConfig) {
152+
153+
}
154+
155+
@Override
156+
public void onLowMemory() {
157+
158+
}
159+
160+
@Override
161+
public void onTrimMemory(int level) {
162+
MatrixLog.i(TAG, "[onTrimMemory] level:%s", level);
163+
if (level == TRIM_MEMORY_UI_HIDDEN && isAppForeground) { // fallback
164+
onDispatchBackground(visibleScene);
165+
}
166+
}
167+
}
168+
169+
private void updateScene(Activity activity) {
170+
visibleScene = activity.getClass().getName();
171+
}
172+
173+
private void updateScene(String currentFragmentName) {
174+
StringBuilder ss = new StringBuilder();
175+
ss.append(TextUtils.isEmpty(currentFragmentName) ? "?" : currentFragmentName);
176+
visibleScene = ss.toString();
177+
}
178+
179+
public static String getTopActivityName() {
180+
long start = System.currentTimeMillis();
181+
try {
182+
Class activityThreadClass = Class.forName("android.app.ActivityThread");
183+
Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
184+
Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
185+
activitiesField.setAccessible(true);
186+
187+
Map<Object, Object> activities;
188+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
189+
activities = (HashMap<Object, Object>) activitiesField.get(activityThread);
190+
} else {
191+
activities = (ArrayMap<Object, Object>) activitiesField.get(activityThread);
192+
}
193+
if (activities.size() < 1) {
194+
return null;
195+
}
196+
for (Object activityRecord : activities.values()) {
197+
Class activityRecordClass = activityRecord.getClass();
198+
Field pausedField = activityRecordClass.getDeclaredField("paused");
199+
pausedField.setAccessible(true);
200+
if (!pausedField.getBoolean(activityRecord)) {
201+
Field activityField = activityRecordClass.getDeclaredField("activity");
202+
activityField.setAccessible(true);
203+
Activity activity = (Activity) activityField.get(activityRecord);
204+
return activity.getClass().getName();
205+
}
206+
}
207+
} catch (Exception e) {
208+
e.printStackTrace();
209+
} finally {
210+
long cost = System.currentTimeMillis() - start;
211+
MatrixLog.d(TAG, "[getTopActivityName] Cost:%s", cost);
212+
}
213+
return null;
214+
}
215+
216+
}

matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/Matrix.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ public class Matrix {
3636
private static volatile Matrix sInstance;
3737

3838
private final HashSet<Plugin> plugins;
39-
private final Application application;
40-
private final PluginListener pluginListener;
39+
private final Application application;
40+
private final PluginListener pluginListener;
4141

4242
private Matrix(Application app, PluginListener listener, HashSet<Plugin> plugins) {
4343
this.application = app;
4444
this.pluginListener = listener;
4545
this.plugins = plugins;
46+
AppActiveMatrixDelegate.INSTANCE.init(application);
4647
for (Plugin plugin : plugins) {
4748
plugin.init(application, pluginListener);
4849
pluginListener.onInit(plugin);
@@ -125,8 +126,8 @@ public <T extends Plugin> T getPluginByClass(Class<T> pluginClass) {
125126
}
126127

127128
public static class Builder {
128-
private final Application application;
129-
private PluginListener pluginListener;
129+
private final Application application;
130+
private PluginListener pluginListener;
130131

131132
private HashSet<Plugin> plugins = new HashSet<>();
132133

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.tencent.matrix.listeners;
2+
3+
public interface IAppForeground {
4+
5+
void onForeground(boolean isForeground);
6+
}

0 commit comments

Comments
 (0)