Skip to content

Commit

Permalink
update RxCrashTool.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamsiree committed Feb 27, 2020
1 parent fdfa837 commit 1832a99
Show file tree
Hide file tree
Showing 15 changed files with 183 additions and 37 deletions.
3 changes: 2 additions & 1 deletion RxDemo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".activity.ActivityTVideoTimeline"></activity>
<activity android:name=".activity.ActivityOnCrash"></activity>
<activity android:name=".activity.ActivityTVideoTimeline" />
<activity android:name=".activity.ActivityOtherEffect" />
<activity android:name=".activity.ActivityTSectionTabLayout" />
<activity android:name=".activity.ActivityTGlideTabLayout" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ private void initData() {
mDataFunction.add(new ModelMainItem("压缩与加密的艺术", R.drawable.circle_zip, ActivityZipEncrypt.class));
mDataFunction.add(new ModelMainItem("PULL解析XML", R.drawable.circle_swap_vert, ActivityXmlParse.class));
mDataFunction.add(new ModelMainItem("支付宝支付Demo", R.drawable.circle_alipay, ActivityAliPay.class));
mDataFunction.add(new ModelMainItem("Hold住崩溃界面", R.drawable.circle_alipay, ActivityOnCrash.class));

mDataFunction.add(new ModelMainItem("app检测更新与安装", R.mipmap.ic_launcher, ActivitySplash.class));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.tamsiree.rxdemo.activity;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.tamsiree.rxdemo.R;
import com.tamsiree.rxui.activity.ActivityBase;

public class ActivityOnCrash extends ActivityBase {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_on_crash);


Button crashMainThreadButton = findViewById(R.id.button_crash_main_thread);
Button crashBgThreadButton = findViewById(R.id.button_crash_bg_thread);
Button crashWithDelayButton = findViewById(R.id.button_crash_with_delay);

crashMainThreadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
throw new RuntimeException("I'm a cool exception and I crashed the main thread!");
}
});

crashBgThreadButton.setOnClickListener(new View.OnClickListener() {
@SuppressLint("StaticFieldLeak") //For demo purposes we don't care about leaks
@Override
public void onClick(View view) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
throw new RuntimeException("I'm also cool, and I crashed the background thread!");
}
}.execute();
}
});

crashWithDelayButton.setOnClickListener(new View.OnClickListener() {
@SuppressLint("StaticFieldLeak") //For demo purposes we don't care about leaks
@Override
public void onClick(View view) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
//meh
}
throw new RuntimeException("I am a not so cool exception, and I am delayed, so you can check if the app crashes when in background!");
}
}.execute();
}
});
}
}
53 changes: 53 additions & 0 deletions RxDemo/src/main/res/layout/activity_on_crash.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.ActivityOnCrash">

<com.tamsiree.rxui.view.RxTitle
android:id="@+id/rx_title"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_55"
android:background="@color/colorPrimary"
app:title="Hold住崩溃界面" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:text="@string/hello" />

<Button
android:id="@+id/button_crash_main_thread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/crash_main_thread" />

<Button
android:id="@+id/button_crash_bg_thread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/crash_background_thread" />

<Button
android:id="@+id/button_crash_with_delay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/crash_with_delay" />

</LinearLayout>

</LinearLayout>
4 changes: 2 additions & 2 deletions RxDemo/src/main/res/layout/activity_svg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

<com.jaredrummler.android.widget.AnimatedSvgView
android:id="@+id/animated_svg_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
app:animatedSvgFillColors="@array/google_glyph_colors"
Expand Down
4 changes: 2 additions & 2 deletions RxDemo/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorPrimary">#52BA97</color>
<color name="colorPrimaryDark">#409075</color>
<color name="colorAccent">#FF4081</color>

<color name="google_red">#EA4335</color>
Expand Down
14 changes: 14 additions & 0 deletions RxDemo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,18 @@
<string name="current_float_time">修改移动速度,当前%1$s</string>
<string name="current_rotate_time">修改旋转速度,当前%1$s</string>

<!-- Main activity strings -->
<string name="hello">你好!按下以下按钮之一,使应用程序崩溃</string>
<string name="crash_main_thread">崩溃主线程</string>
<string name="crash_background_thread">后台线程崩溃</string>
<string name="crash_with_delay">延迟 (5s) 后崩溃</string>

<!-- These strings are only for our CUSTOM error activity, if you are using the default activity, they are not needed! -->
<string name="error_title">情况不妙!</string>
<string name="error_occurred">发生了一个错误. 我们深感抱歉.</string>
<string name="error_details">错误详情:</string>
<string name="unknown_exception">未知错误</string>
<string name="restart_app">重启APP</string>
<string name="close_app">关闭APP</string>

</resources>
34 changes: 22 additions & 12 deletions RxKit/src/main/java/com/tamsiree/rxkit/RxLogTool.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.tamsiree.rxkit;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedWriter;
Expand All @@ -15,25 +14,24 @@
import java.util.Date;

/**
*
* @author tamsiree
* @date 2016/1/24
*/
public class RxLogTool {

private final static SimpleDateFormat LOG_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 日志的输出格式
private final static SimpleDateFormat FILE_SUFFIX = new SimpleDateFormat("yyyy-MM-dd");// 日志文件格式
private final static SimpleDateFormat LOG_FORMAT = new SimpleDateFormat("yyyy年MM月dd日_HH点mm分ss秒");// 日志的输出格式
private final static SimpleDateFormat FILE_SUFFIX = new SimpleDateFormat("yyyy年MM月dd日_HH点mm分ss秒");// 日志文件格式
private static Boolean LOG_SWITCH = true; // 日志文件总开关
private static Boolean LOG_TO_FILE = false; // 日志写入文件开关
private static String LOG_TAG = "TAG"; // 默认的tag
private static Boolean LOG_TO_FILE = true; // 日志写入文件开关
private static String LOG_TAG = "RxLogTool"; // 默认的tag
private static char LOG_TYPE = 'v';// 输入日志类型,v代表输出所有信息,w则只输出警告...
private static int LOG_SAVE_DAYS = 7;// sd卡中日志文件的最多保存天数
private static String LOG_FILE_PATH; // 日志文件保存路径
private static String LOG_FILE_NAME;// 日志文件保存名称

public static void init(Context context) { // 在Application中初始化
LOG_FILE_PATH = Environment.getExternalStorageDirectory().getPath() + File.separator + context.getPackageName();
LOG_FILE_NAME = "Log";
LOG_FILE_PATH = RxFileTool.getRootPath().getPath() + File.separator + context.getPackageName() + File.separator + "Log";
LOG_FILE_NAME = "RxLogTool_";
}

/****************************
Expand Down Expand Up @@ -131,8 +129,19 @@ private static void log(String tag, String msg, Throwable tr, char level) {
} else {
Log.v(tag, msg, tr);
}
if (LOG_TO_FILE)
log2File(String.valueOf(level), tag, msg + tr == null ? "" : "\n" + Log.getStackTraceString(tr));
if (LOG_TO_FILE) {
String content = "";
if (!RxDataTool.isNullString(msg)) {
content += msg;
}
if (tr != null) {
content += "\n";
content += Log.getStackTraceString(tr);
}

log2File(String.valueOf(level), tag, content);

}
}
}

Expand All @@ -144,12 +153,12 @@ private static void log(String tag, String msg, Throwable tr, char level) {
private synchronized static void log2File(String mylogtype, String tag, String text) {
Date nowtime = new Date();
String date = FILE_SUFFIX.format(nowtime);
String dateLogContent = LOG_FORMAT.format(nowtime) + ":" + mylogtype + ":" + tag + ":" + text; // 日志输出格式
String dateLogContent = "Date:" + LOG_FORMAT.format(nowtime) + "\nLogType:" + mylogtype + "\nTag:" + tag + "\nContent:\n" + text; // 日志输出格式
File destDir = new File(LOG_FILE_PATH);
if (!destDir.exists()) {
destDir.mkdirs();
}
File file = new File(LOG_FILE_PATH, LOG_FILE_NAME + date);
File file = new File(LOG_FILE_PATH, LOG_FILE_NAME + date + ".txt");
try {
FileWriter filerWriter = new FileWriter(file, true);
BufferedWriter bufWriter = new BufferedWriter(filerWriter);
Expand Down Expand Up @@ -206,4 +215,5 @@ public static void saveLogFile(String message) {
e.printStackTrace();
}
}

}
1 change: 1 addition & 0 deletions RxKit/src/main/java/com/tamsiree/rxkit/RxTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class RxTool {
public static void init(Context context) {
RxTool.context = context.getApplicationContext();
// RxCrashTool.init(context);
RxLogTool.init(context);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
import androidx.fragment.app.FragmentActivity;

import com.tamsiree.rxkit.R;
import com.tamsiree.rxkit.crash.CaocConfig;
import com.tamsiree.rxkit.RxLogTool;
import com.tamsiree.rxkit.crash.RxCrashConfig;
import com.tamsiree.rxkit.crash.RxCrashTool;

public class ActivityCrash extends FragmentActivity {
Expand All @@ -45,7 +46,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
//It is recommended that you follow this logic if implementing a custom error activity.
Button restartButton = findViewById(R.id.crash_error_activity_restart_button);

final CaocConfig config = RxCrashTool.getConfigFromIntent(getIntent());
final RxCrashConfig config = RxCrashTool.getConfigFromIntent(getIntent());

if (config == null) {
//This should never happen - Just finish the activity to avoid a recursive crash.
Expand All @@ -69,6 +70,8 @@ public void onClick(View v) {
}
});
}
String message = RxCrashTool.getAllErrorDetailsFromIntent(ActivityCrash.this, getIntent());
RxLogTool.e(message);

Button moreInfoButton = findViewById(R.id.crash_error_activity_more_info_button);

Expand All @@ -78,9 +81,10 @@ public void onClick(View v) {
public void onClick(View v) {
//We retrieve all the error data and show it


AlertDialog dialog = new AlertDialog.Builder(ActivityCrash.this)
.setTitle(R.string.crash_error_details_title)
.setMessage(RxCrashTool.getAllErrorDetailsFromIntent(ActivityCrash.this, getIntent()))
.setMessage(message)
.setPositiveButton(R.string.crash_error_details_close, null)
.setNeutralButton(R.string.crash_error_details_copy,
new DialogInterface.OnClickListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.lang.reflect.Modifier;


public class CaocConfig implements Serializable {
public class RxCrashConfig implements Serializable {

public static final int BACKGROUND_MODE_SILENT = 0;
public static final int BACKGROUND_MODE_SHOW_CUSTOM = 1;
Expand Down Expand Up @@ -147,14 +147,14 @@ public void setEventListener(@Nullable RxCrashTool.EventListener eventListener)
}

public static class Builder {
private CaocConfig config;
private RxCrashConfig config;

@NonNull
public static Builder create() {
Builder builder = new Builder();
CaocConfig currentConfig = RxCrashTool.getConfig();
RxCrashConfig currentConfig = RxCrashTool.getConfig();

CaocConfig config = new CaocConfig();
RxCrashConfig config = new RxCrashConfig();
config.backgroundMode = currentConfig.backgroundMode;
config.enabled = currentConfig.enabled;
config.showErrorDetails = currentConfig.showErrorDetails;
Expand Down Expand Up @@ -313,7 +313,7 @@ public Builder eventListener(@Nullable RxCrashTool.EventListener eventListener)
}

@NonNull
public CaocConfig get() {
public RxCrashConfig get() {
return config;
}

Expand Down
Loading

0 comments on commit 1832a99

Please sign in to comment.