Skip to content

Commit

Permalink
Change comment to English
Browse files Browse the repository at this point in the history
  • Loading branch information
markzhai committed Mar 4, 2016
1 parent 807fe29 commit e879120
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
import java.lang.reflect.Constructor;

/**
* <p>looper线程监控</p>
* Created by markzhai on 2015/9/25.
* Looper thread monitor.
*
* @author markzhai on 2015/9/25.
*/
public class BlockCanary {

private static final String TAG = "BlockCanary";

private static BlockCanary sInstance;
private BlockCanaryCore mBlockCanaryCore;
private boolean mLooperLoggingStarted = false;
Expand Down Expand Up @@ -96,25 +97,27 @@ public void upload() {
}

/**
* 记录开启监控的时间到preference,可以在release包收到push通知后调用。
* Record monitor start time to preference, you may use it when after push which tells start BlockCanary.
*/
public void recordStartTime() {
PreferenceManager.getDefaultSharedPreferences(BlockCanaryContext.get().getContext())
.edit().putLong("BlockCanary_StartTime", System.currentTimeMillis()).commit();
}

/**
* 是否监控时间结束,根据上次开启的时间(recordStartTime)和getConfigDuration计算出来。
* Is monitor duration end, compute from recordStartTime end getConfigDuration.
*
* @return true则结束
* @return true if ended
*/
public boolean isMonitorDurationEnd() {
long startTime = PreferenceManager.getDefaultSharedPreferences(
BlockCanaryContext.get().getContext()).getLong("BlockCanary_StartTime", 0);
return startTime != 0 &&
System.currentTimeMillis() - startTime > BlockCanaryContext.get().getConfigDuration() * 3600 * 1000;
System.currentTimeMillis() - startTime >
BlockCanaryContext.get().getConfigDuration() * 3600 * 1000;
}

@SuppressWarnings("unchecked")
private void initNotification() {
if (!BlockCanaryContext.get().isNeedDisplay()) {
return;
Expand All @@ -128,7 +131,7 @@ private void initNotification() {
Constructor<? extends OnBlockEventInterceptor> constructor = notifier.getConstructor();
mBlockCanaryCore.setOnBlockEventInterceptor(constructor.newInstance());
} catch (Exception e) {
Log.e(TAG, "initNotification: ",e );
Log.e(TAG, "initNotification: ", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import java.io.File;

/**
* 使用本库的应用实现该抽象类,提供运行环境给性能监控组件(包括使用配置和app相关的log如用户名和网络环境)
* <p>
* You should provide a real implementation of this class to use BlockCanary,
* which provides runtime environment to library (including configuration
* and app-related log like uid and network environment)
* <p/>
* Created by markzhai on 2015/9/25.
*/
public class BlockCanaryContext implements IBlockCanaryContext {
Expand Down Expand Up @@ -48,96 +50,112 @@ public Context getContext() {
}

/**
* 标示符,可以唯一标示该安装版本号,如版本+渠道名+编译平台
* qualifier which can specify this installation, like version + flavor
*
* @return apk唯一标示符
* @return apk qualifier
*/
public String getQualifier() {
return "Unspecified";
}

/**
* 用户id,方便联系用户和后台查询定位
* Get user id
*
* @return 用户id
* @return user id
*/
public String getUid() {
return "0";
}

/**
* 网络类型,应用通常都有自己的一套,且较重需要监听网络状态变化,不放在本库中实现
* Network type
*
* @return 2G/3G/4G/wifi等
* @return String like 2G, 3G, 4G, wifi, etc.
*/
public String getNetworkType() {
return "UNKNOWN";
}

/**
* 卡慢性能监控 持续时长(小时),开启后到达时长则关闭,配合{@link BlockCanary}的isMonitorDurationEnd
* Config monitor duration, after this time BlockCanary will stop, use
* with {@link BlockCanary}'s isMonitorDurationEnd
*
* @return 监控持续时长(小时)
* @return monitor last duration (in hour)
*/
public int getConfigDuration() {
return 99999;
}

/**
* 卡慢性能监控 间隔(毫秒),超出该间隔判定为卡慢。建议根据机器性能设置不同的数值,如2000/Cpu核数
* Config block threshold (in millis), dispatch over this duration is regarded as a BLOCK. You may set it
* from performance of device.
*
* @return 卡慢阙值(毫秒)
* @return threshold in mills
*/
public int getConfigBlockThreshold() {
return 1000;
}

/**
* 是否需要展示卡慢界面,如仅在Debug包开启
* If need notification and block ui
*
* @return 是否需要展示卡慢界面
* @return true if need, else if not need.
*/
public boolean isNeedDisplay() {
return true;
}

/**
* Log文件保存的位置,如"/blockcanary/log"
* Path to save log, like "/blockcanary/log"
*
* @return Log文件保存的位置
* @return path of log files
*/
@Override
public String getLogPath() {
return "/blockcanary/performance";
}

/**
* 压缩文件
* Zip log file
*
* @param src 压缩前的文件
* @param dest 压缩后的文件
* @return 压缩是否成功
* @param src files before compress
* @param dest files compressed
* @return true if compression is successful
*/
@Override
public boolean zipLogFile(File[] src, File dest) {
return false;
}

/**
* 上传日志
* Upload log file
*
* @param zippedFile 压缩后的文件
* @param zippedFile zipped file
*/
@Override
public void uploadLogFile(File zippedFile) {

}

/**
* Config string prefix to determine how to fold stack
*
* @return string prefix, null if use process name.
*/
@Override
public String getStackFoldPrefix() {
return null;
}

/**
* Thread stack dump interval, use when block happens, BlockCanary will dump on main thread
* stack according to current sample cycle.
* <p/>
* PS: Because the implementation mechanism of Looper, real dump interval would be longer than
* the period specified here (longer if cpu is busier)
*
* @return dump interval(in millis)
*/
@Override
public int getConfigDumpIntervalMillis() {
return getConfigBlockThreshold();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,8 @@
*/
public interface IBlockCanaryContext {

/**
* Config block threshold
*
* @return threshold in mills
*/
int getConfigBlockThreshold();

/**
* If need notification and list ui
*
* @return true if need, else if not need.
*/
boolean isNeedDisplay();

String getQualifier();
Expand All @@ -52,19 +42,7 @@ public interface IBlockCanaryContext {

void uploadLogFile(File zippedFile);

/**
* Config string prefix to determine how to fold stack
*
* @return string prefix, null if use process name.
*/
String getStackFoldPrefix();

/**
* 线程栈采样周期,确定发卡慢后,按照当前指定的采样周期,对主线程线程栈进行采样
* <p>
* 注意: 因为Looper是实现机制问题,实际采样周期必然会大于指定的采样周期(CPU越忙,实际采样周期越大)
*
* @return 采样周期(毫秒)
*/
int getConfigDumpIntervalMillis();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);

// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// toolbar.setTitle("BlockCanary");


getSupportFragmentManager().beginTransaction()
.add(R.id.container, DemoFragment.newInstance())
.commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,7 @@ public class DemoApplication extends Application {
public void onCreate() {
super.onCreate();
sContext = this;

BlockCanary.install(this, new AppBlockCanaryContext()).start();

// boolean shouldStart = true;
// if (!BuildConfig.DEBUG) {
// if (BlockCanary.get().isMonitorDurationEnd()) {
// shouldStart = false;
// }
// }
//
// if (shouldStart) {
// BlockCanary.get().start();
// }
}

public static Context getAppContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
public class DemoFragment extends Fragment implements View.OnClickListener {

public static DemoFragment newInstance() {
DemoFragment f = new DemoFragment();
return f;
return new DemoFragment();
}

@Override
Expand All @@ -40,8 +39,7 @@ public void onCreate(final Bundle savedInstanceState) {
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_main, null);
return v;
return inflater.inflate(R.layout.activity_main, null);
}

@Override
Expand Down
17 changes: 8 additions & 9 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

## Project-wide Gradle settings.
#
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

#
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

#
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# org.gradle.parallel=true
#Sat Mar 05 00:07:08 CST 2016
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=1080

0 comments on commit e879120

Please sign in to comment.