├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ └── layout
│ │ │ │ └── activity_main.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── yph
│ │ │ │ └── blockcollecttest
│ │ │ │ ├── App.java
│ │ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── yph
│ │ │ └── blockcollect
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── yph
│ │ └── blockcollect
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── .idea
├── .name
├── copyright
│ └── profiles_settings.xml
├── markdown-navigator
│ └── profiles_settings.xml
├── encodings.xml
├── vcs.xml
├── modules.xml
├── runConfigurations.xml
├── gradle.xml
├── compiler.xml
└── misc.xml
├── blockcollect
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ └── values
│ │ │ │ └── strings.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── yph
│ │ │ └── blockcollect
│ │ │ ├── BuglyCar.java
│ │ │ ├── BlockDetect.java
│ │ │ ├── ReflectUtil.java
│ │ │ ├── StackTraceUtil.java
│ │ │ └── LogMonitor.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── yph
│ │ │ └── blockcollect
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── yph
│ │ └── blockcollect
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── screenshot
├── buglylog.png
└── locallog.png
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── README.md
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | blockcollect-parent
--------------------------------------------------------------------------------
/blockcollect/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/screenshot/buglylog.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/y-grey/BlockCollect/HEAD/screenshot/buglylog.png
--------------------------------------------------------------------------------
/screenshot/locallog.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/y-grey/BlockCollect/HEAD/screenshot/locallog.png
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':blockcollect'
2 | rootProject.name = 'blockcollect-parent'
3 |
4 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | BlockCollect
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/y-grey/BlockCollect/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/blockcollect/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | blockcollect
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/y-grey/BlockCollect/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/y-grey/BlockCollect/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/y-grey/BlockCollect/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/y-grey/BlockCollect/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/y-grey/BlockCollect/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/.idea/markdown-navigator/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/y-grey/BlockCollect/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/y-grey/BlockCollect/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/y-grey/BlockCollect/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/y-grey/BlockCollect/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/y-grey/BlockCollect/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Mar 14 23:07:22 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/blockcollect/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/yph/blockcollect/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.yph.blockcollect;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/blockcollect/src/test/java/com/yph/blockcollect/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.yph.blockcollect;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yph/blockcollecttest/App.java:
--------------------------------------------------------------------------------
1 | package com.yph.blockcollecttest;
2 |
3 | import android.app.Application;
4 |
5 | import com.tencent.bugly.crashreport.CrashReport;
6 | import com.yph.blockcollect.BlockDetect;
7 |
8 | /**
9 | * Created by yph on 2018/3/15.
10 | */
11 |
12 | public class App extends Application{
13 | @Override
14 | public void onCreate() {
15 | super.onCreate();
16 | CrashReport.initCrashReport(this, "你的AppId", false);
17 | BlockDetect.start(true);//是否开车
18 | // BlockDetect.start(true,1000,6);//1是否开车,2阈值,3采样
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yph/blockcollecttest/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.yph.blockcollecttest;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.widget.TextView;
6 |
7 | public class MainActivity extends AppCompatActivity {
8 |
9 | @Override
10 | protected void onCreate(Bundle savedInstanceState) {
11 | super.onCreate(savedInstanceState);
12 | setContentView(R.layout.activity_main);
13 | try {
14 | Thread.sleep(10);
15 | Thread.sleep(800);
16 | } catch (InterruptedException e) {
17 | e.printStackTrace();
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/yph/blockcollect/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.yph.blockcollect;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.yph.blockcollect", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/blockcollect/src/androidTest/java/com/yph/blockcollect/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.yph.blockcollect;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.yph.blockcollect.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\dell1\android-studio\android-sdks/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/blockcollect/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\dell1\android-studio\android-sdks/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | defaultConfig {
6 | applicationId "com.yph.blockcollecttest"
7 | minSdkVersion 16
8 | targetSdkVersion 25
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(include: ['*.jar'], dir: 'libs')
23 | androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
24 | exclude group: 'com.android.support', module: 'support-annotations'
25 | })
26 | implementation 'com.android.support:appcompat-v7:27.1.1'
27 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
28 | testImplementation 'junit:junit:4.12'
29 | implementation 'com.tencent.bugly:crashreport:latest.release'
30 | implementation project(':blockcollect')
31 | }
32 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/blockcollect/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'bintray-release'//添加
3 |
4 | android {
5 | compileSdkVersion 27
6 |
7 | defaultConfig {
8 | minSdkVersion 16
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 |
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 |
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | }
23 |
24 | dependencies {
25 | implementation fileTree(dir: 'libs', include: ['*.jar'])
26 | androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
27 | exclude group: 'com.android.support', module: 'support-annotations'
28 | })
29 | implementation 'com.android.support:appcompat-v7:27.1.1'
30 | testImplementation 'junit:junit:4.12'
31 | }
32 | //添加
33 | publish {
34 | artifactId = 'blockcollect'
35 | userOrg = 'yph'
36 | groupId = 'com.yph'
37 | publishVersion = '1.0.2'
38 | desc = 'Block Collect Tool'
39 | website = 'https://github.com/qq542391099/BlockCollect'
40 | licences = ['Apache-2.0']
41 | }
42 |
--------------------------------------------------------------------------------
/blockcollect/src/main/java/com/yph/blockcollect/BuglyCar.java:
--------------------------------------------------------------------------------
1 | package com.yph.blockcollect;
2 |
3 | import android.os.Looper;
4 | import android.util.Log;
5 |
6 | /**
7 | * Created by _yph on 2018/3/14 0014.
8 | */
9 |
10 | public class BuglyCar {
11 |
12 | public static void push(StackTraceElement[] stacks){
13 | Throwable throwable = new Throwable("卡顿监测");
14 | throwable.setStackTrace(stacks);
15 | try {
16 | Object c = ReflectUtil.getStaticField("com.tencent.bugly.crashreport.crash.c","q");
17 | Object e = ReflectUtil.getField(c,"r");
18 | Object b = ReflectUtil.getField(e,"b");
19 | Object crashDetailBean = ReflectUtil.invokeMethod(e, "b",
20 | new Class[]{Thread.class,Throwable.class,boolean.class,String.class,byte[].class},
21 | new Object[]{Looper.getMainLooper().getThread(),throwable,true,null,null});
22 | ReflectUtil.invokeMethod(b, "a",
23 | new Class[]{crashDetailBean.getClass(),long.class,boolean.class},
24 | new Object[]{crashDetailBean,3000L,true});
25 | } catch (Exception e) {
26 | Log.e("BlockCollect","BuglyCar翻车了:"+e.toString());
27 | e.printStackTrace();
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/blockcollect/src/main/java/com/yph/blockcollect/BlockDetect.java:
--------------------------------------------------------------------------------
1 | package com.yph.blockcollect;
2 |
3 | import android.util.Log;
4 | import android.view.Choreographer;
5 |
6 | /**
7 | * Created by _yph on 2018/3/14 0014.
8 | */
9 |
10 | public class BlockDetect {
11 |
12 | public static void start(boolean toBugly, int timeBlock, int frequency) {
13 | LogMonitor.get().setParam(timeBlock, frequency);
14 | start(toBugly);
15 | }
16 |
17 | public static void start(boolean toBugly) {
18 | LogMonitor.get().setToBugly(toBugly);
19 | LogMonitor.get().reStartMonitor();
20 | Choreographer.getInstance().postFrameCallback(new Choreographer.FrameCallback() {
21 | @Override
22 | public void doFrame(long frameTimeNanos) {
23 | LogMonitor.get().reStartMonitor();
24 | plusSM();
25 | Choreographer.getInstance().postFrameCallback(this);
26 | }
27 | });
28 | }
29 |
30 | private static long nowTime = 1;
31 | private static int sm = 1;
32 |
33 | private static void plusSM() {
34 | long t = System.currentTimeMillis();
35 | if (nowTime == 1) {
36 | nowTime = t;
37 | }
38 | if (nowTime / 1000 == t / 1000) {
39 | sm++;
40 | } else if (t / 1000 - nowTime / 1000 >= 1) {
41 | Log.e("BlockCollect", "sm:" + sm);
42 | sm = 1;
43 | nowTime = t;
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/blockcollect/src/main/java/com/yph/blockcollect/ReflectUtil.java:
--------------------------------------------------------------------------------
1 | package com.yph.blockcollect;
2 |
3 | import java.lang.reflect.Field;
4 | import java.lang.reflect.Method;
5 |
6 | /**
7 | * Created by yph on 2017/6/29.
8 | */
9 |
10 | public class ReflectUtil {
11 |
12 | public static T getField(Object object, String fieldName) throws Exception {
13 | Class> forName = object.getClass();
14 | Field field = forName.getDeclaredField(fieldName);
15 | field.setAccessible(true);
16 | return (T) field.get(object);
17 | }
18 | public static T getStaticField(String clsName, String fieldName) throws Exception {
19 | Class> forName = Class.forName(clsName);
20 | Field field = forName.getDeclaredField(fieldName);
21 | field.setAccessible(true);
22 | return (T) field.get(null);
23 | }
24 | public static Object invokeMethod(Object owner, String methodName, Class[] argsClass, Object[] args) throws Exception {
25 | Class ownerClass = owner.getClass();
26 | Method method = ownerClass.getDeclaredMethod(methodName, argsClass);
27 | method.setAccessible(true);
28 | return method.invoke(owner, args);
29 | }
30 |
31 | public static Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception {
32 | Class ownerClass = owner.getClass();
33 | Class[] argsClass = new Class[args.length];
34 | for (int i = 0, j = args.length; i < j; i++) {
35 | argsClass[i] = args[i].getClass();
36 | }
37 | Method method = ownerClass.getDeclaredMethod(methodName, argsClass);
38 | method.setAccessible(true);
39 | return method.invoke(owner, args);
40 | }
41 |
42 | public static Object invokeMethod(Object owner, String methodName) throws Exception {
43 | Class ownerClass = owner.getClass();
44 | Method method = ownerClass.getDeclaredMethod(methodName);
45 | method.setAccessible(true);
46 | return method.invoke(owner);
47 | }
48 |
49 | public static Object invokeMethod(Object owner, String methodName, int args) throws Exception {
50 | Class ownerClass = owner.getClass();
51 | Method method = ownerClass.getDeclaredMethod(methodName, int.class);
52 | method.setAccessible(true);
53 | return method.invoke(owner, args);
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 简介
2 | ============
3 |
4 | BlockCollect是一个兼具卡顿监测和远程收集日志的工具.
5 |
6 | * 卡顿监测利用Android系统每隔16.6ms发出VSYNC信号,来通知界面进行重绘、渲染的原理.
7 | * 远程收集日志利用Bugly这趟顺风车.
8 |
9 | 本地日志:
10 |
11 | 
12 |
13 | 远程日志:
14 |
15 | 
16 |
17 | 使用
18 | --------
19 | __1、去Bugly官网创建应用获取AppId__
20 |
21 | __2、填写Bugly所需要的权限__
22 | ```xml
23 |
24 |
25 |
26 |
27 |
28 | ```
29 | __3、添加依赖__
30 | ```groovy
31 | dependencies {
32 | compile 'com.tencent.bugly:crashreport:latest.release'
33 | compile 'com.yph:blockcollect:1.0.1'
34 | }
35 | ```
36 | __4、初始化__
37 | ```java
38 | public class App extends Application{
39 | @Override
40 | public void onCreate() {
41 | super.onCreate();
42 | CrashReport.initCrashReport(this, "你的AppId", false);
43 | BlockDetect.start(true);//是否开车
44 | // BlockDetect.start(true,800,6);//1是否开车,2阈值,3采样
45 | }
46 | }
47 | ```
48 | 其中布尔值代表是否push到bugly,第二个参数为阈值,默认800ms,第三个采样频次,默认6次
49 |
50 |
51 | 原理
52 | --------
53 |
54 | [卡顿监测之真正轻量级的卡顿监测工具BlockDetectUtil(仅一个类)](http://blog.csdn.net/u012874222/article/details/79400154)
55 |
56 | [卡顿监测之远程收集log(潜入Bugly这趟顺风车)](http://blog.csdn.net/u012874222/article/details/79417549)
57 |
58 | Thanks
59 | --------
60 | [Bugly](https://bugly.qq.com)
61 |
62 | License
63 | -------
64 | Copyright [2018] [yph]
65 |
66 | Licensed under the Apache License, Version 2.0 (the "License");
67 | you may not use this file except in compliance with the License.
68 | You may obtain a copy of the License at
69 |
70 | http://www.apache.org/licenses/LICENSE-2.0
71 |
72 | Unless required by applicable law or agreed to in writing, software
73 | distributed under the License is distributed on an "AS IS" BASIS,
74 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
75 | See the License for the specific language governing permissions and
76 | limitations under the License.
77 |
78 |
--------------------------------------------------------------------------------
/blockcollect/src/main/java/com/yph/blockcollect/StackTraceUtil.java:
--------------------------------------------------------------------------------
1 | package com.yph.blockcollect;
2 |
3 | /**
4 | * Created by _yph on 2018/9/14 0014.
5 | */
6 | public class StackTraceUtil {
7 |
8 | public static String getCurThread() {
9 | return toString(Thread.currentThread().getStackTrace());
10 | }
11 |
12 | /**
13 | * 前三行为本工具堆栈应过滤掉
14 | */
15 | public static String getCurThreadSimplified() {
16 | return toSimpleString(Thread.currentThread().getStackTrace(), 3);
17 | }
18 |
19 | public static String toString(StackTraceElement[] stacktrace) {
20 | StringBuilder sb = new StringBuilder();
21 | for (StackTraceElement s : stacktrace) {
22 | sb.append(s.toString()).append("\n");
23 | }
24 | return sb.toString();
25 | }
26 |
27 | public static String toSimpleString(StackTraceElement[] stacktrace) {
28 | return toSimpleString(stacktrace, 0);
29 | }
30 |
31 | /**
32 | * 简化堆栈,去除以下字段的开头连续几行,但保留这连续几行中的最后一行
33 | */
34 | private static String toSimpleString(StackTraceElement[] stacktrace, int startIndex) {
35 | StringBuilder sb = new StringBuilder();
36 | boolean start = false;
37 | String lastLine = "";
38 | for (; startIndex < stacktrace.length; startIndex++) {
39 | String line = stacktrace[startIndex].toString();
40 | if (!start) {
41 | if (line.startsWith("com.android.")
42 | || line.startsWith("java")
43 | || line.startsWith("android")
44 | || line.startsWith("sun.")
45 | || line.startsWith("org.")
46 | || line.startsWith("dalvik.")
47 | || line.startsWith("com.google.")
48 | || line.startsWith("libcore.")) {
49 | lastLine = line;
50 | continue;
51 | }
52 | sb.append(lastLine).append("\n");
53 | start = true;
54 | }
55 | sb.append(line).append("\n");
56 | }
57 | return sb.toString();
58 | }
59 |
60 | /**
61 | * 取第二行堆栈,相同则认为是相同堆栈,需确保传入的堆栈经过简化,即调用了
62 | *
63 | * @see #toSimpleString(StackTraceElement[], int)
64 | */
65 | public static String getFeature(String stacktrace) {
66 | String[] arr = stacktrace.split("\n");
67 | if (arr.length > 1) {
68 | return arr[0] + arr[1];
69 | }else if(arr.length == 1){
70 | return arr[0];
71 | }
72 | return "";
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/blockcollect/src/main/java/com/yph/blockcollect/LogMonitor.java:
--------------------------------------------------------------------------------
1 | package com.yph.blockcollect;
2 |
3 | import android.os.Debug;
4 | import android.os.Handler;
5 | import android.os.HandlerThread;
6 | import android.os.Looper;
7 | import android.text.TextUtils;
8 | import android.util.Log;
9 |
10 | import java.util.HashMap;
11 | import java.util.Map;
12 | import java.util.concurrent.atomic.AtomicBoolean;
13 |
14 | /**
15 | * Created by _yph on 2018/3/14 0014.
16 | */
17 |
18 | public class LogMonitor {
19 |
20 | private int timeBlock = 800;
21 | private int frequency = 6;
22 | private boolean toBugly;
23 |
24 | private AtomicBoolean isReStart = new AtomicBoolean();
25 | private static LogMonitor sInstance = new LogMonitor();
26 | private Handler mIoHandler;
27 |
28 | private LogMonitor() {
29 | HandlerThread mLogThread = new HandlerThread("yph");
30 | mLogThread.start();
31 | mIoHandler = new Handler(mLogThread.getLooper());
32 | }
33 |
34 | private Runnable mLogRunnable = new Runnable() {
35 | private HashMap stackMap = new HashMap<>();
36 | private String lastStackTraceStr;
37 | private int time = frequency;
38 | @Override
39 | public void run() {
40 | if(Debug.isDebuggerConnected())return;
41 | if(isReStart.compareAndSet(true,false)){
42 | stackMap.clear();
43 | lastStackTraceStr = null;
44 | time = frequency;
45 | }
46 | StackTraceElement[] stackTrace = Looper.getMainLooper().getThread().getStackTrace();
47 | String stackTraceStr = StackTraceUtil.toSimpleString(stackTrace);
48 | if (!TextUtils.isEmpty(stackTraceStr)
49 | && !TextUtils.isEmpty(lastStackTraceStr)
50 | && lastStackTraceStr.equals(stackTraceStr)
51 | && !stackMap.containsKey(stackTraceStr)){//连续两次相同的堆栈才保存,捕捉耗时方法
52 | stackMap.put(stackTraceStr,stackTrace);
53 | }
54 | time -- ;
55 | if(time == 0) {
56 | if(!TextUtils.isEmpty(stackTraceStr)
57 | && stackMap.isEmpty()){//未捕捉到耗时方法,说明发生了执行连续多个不耗时方法所造成的卡顿
58 | stackMap.put(stackTraceStr,stackTrace);//取最后一个卡顿堆栈用于分析
59 | }
60 | if(!stackMap.isEmpty()) {
61 | for (Map.Entry entry : stackMap.entrySet()) {
62 | Log.e("BlockCollect", entry.getKey());
63 | if (toBugly) {
64 | BuglyCar.push(entry.getValue());
65 | }
66 | }
67 | }
68 | isReStart.set(true);
69 | }else {
70 | lastStackTraceStr = stackTraceStr;
71 | mIoHandler.postDelayed(mLogRunnable, timeBlock / frequency);
72 | }
73 | }
74 | };
75 |
76 | static LogMonitor get() {
77 | return sInstance;
78 | }
79 |
80 | void reStartMonitor() {
81 | mIoHandler.removeCallbacks(mLogRunnable);
82 | isReStart.set(true);
83 | mIoHandler.postDelayed(mLogRunnable, timeBlock / frequency);
84 | }
85 |
86 | void setParam(int timeBlock , int frequency) {
87 | this.timeBlock = timeBlock;
88 | this.frequency = frequency;
89 | }
90 |
91 | public void setToBugly(boolean toBugly) {
92 | this.toBugly = toBugly;
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 | 1.7
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
--------------------------------------------------------------------------------