Compare commits

...

42 Commits

Author SHA1 Message Date
ZhanGSKen
36af7947e8 <appbase>APK 1.4.1 release Publish. 2025-02-12 18:03:55 +08:00
ZhanGSKen
805edcf9c4 添加吐司控件 2025-02-12 16:43:57 +08:00
ZhanGSKen
11005e3356 <libappbase>Library Release 1.4.0 2025-02-12 15:01:51 +08:00
ZhanGSKen
75361b2313 <appbase>APK 1.4.0 release Publish. 2025-02-12 15:01:33 +08:00
ZhanGSKen
1d29ce81e8 编译测试 2025-02-12 14:59:38 +08:00
ZhanGSKen
0297e81090 精简配置 2025-02-12 14:59:00 +08:00
ZhanGSKen
df7203e985 更新类库 2025-02-12 14:57:50 +08:00
ZhanGSKen
8c4e8b8e23 <appbase>Start New Stage Version. 2025-02-12 14:14:36 +08:00
ZhanGSKen
d9c02f55e3 更新版本号 2025-02-12 14:14:15 +08:00
ZhanGSKen
a9cb6b471b <appbase>Start New Stage Version. 2025-02-12 14:10:53 +08:00
ZhanGSKen
e694063867 <appbase>Start New Stage Version. 2025-02-12 14:09:34 +08:00
ZhanGSKen
0ee13986dc <appbase>Start New Stage Version. 2025-02-12 14:09:05 +08:00
ZhanGSKen
3239778922 更新测试APP风格配置 2025-02-12 14:06:02 +08:00
ZhanGSKen
13510f45cf 应用风格架构基本完成 2025-02-12 13:28:58 +08:00
ZhanGSKen
49d300dd33 添加信号处理中心 2025-02-12 11:28:39 +08:00
ZhanGSKen
c42f677d48 <libappbase>Library Release 1.3.0 2025-02-11 13:11:35 +08:00
ZhanGSKen
9052a3924b <appbase>APK 1.3.0 release Publish. 2025-02-11 13:11:17 +08:00
ZhanGSKen
9642ad1966 精简代码 2025-02-11 13:09:33 +08:00
ZhanGSKen
6686da0e8f 调色架构1 2025-02-11 12:42:07 +08:00
ZhanGSKen
9e7aff09d1 <libappbase>Library Release 1.2.4 2025-02-11 00:32:09 +08:00
ZhanGSKen
494b2b7fbc <appbase>APK 1.2.4 release Publish. 2025-02-11 00:31:50 +08:00
ZhanGSKen
b1f9b74e28 源码重构整理,精简代码。 2025-02-11 00:27:54 +08:00
ZhanGSKen
32287e17c0 注释冗余代码 2025-02-09 19:05:27 +08:00
ZhanGSKen
8a0605c12e <libappbase>Library Release 1.2.3 2025-02-09 15:09:48 +08:00
ZhanGSKen
5768881a47 <appbase>APK 1.2.3 release Publish. 2025-02-09 15:09:34 +08:00
ZhanGSKen
6e02c130b2 类名重构,简化代码 2025-02-09 15:08:23 +08:00
ZhanGSKen
fb9443bbc7 <libappbase>Library Release 1.2.2 2025-02-09 13:09:48 +08:00
ZhanGSKen
bfc1180822 <appbase>APK 1.2.2 release Publish. 2025-02-09 13:09:33 +08:00
ZhanGSKen
ee52a6c28c 完成应用报告显示逻辑 2025-02-09 13:08:34 +08:00
ZhanGSKen
2f72f1795e <libappbase>Library Release 1.2.1 2025-02-09 11:41:09 +08:00
ZhanGSKen
db28447c0f <appbase>APK 1.2.1 release Publish. 2025-02-09 11:40:48 +08:00
ZhanGSKen
ad2c29d8ff 修复外部崩溃与内部崩溃的保险丝恢复逻辑 2025-02-09 11:39:24 +08:00
ZhanGSKen
b0e3bfe243 添加应用崩溃源APP名称显示 2025-02-09 11:33:27 +08:00
ZhanGSKen
7b1aeeae8b 统一内外部报告风格 2025-02-09 11:18:15 +08:00
ZhanGSKen
54cceeed3b 应用崩溃报告高级视图调试完成 2025-02-08 14:27:38 +08:00
ZhanGSKen
a72c5e1e6e 应用保险丝熔断测试通过 2025-02-08 12:26:59 +08:00
ZhanGSKen
26fcd9b584 日志模块初步调试完成 2025-02-07 19:39:25 +08:00
ZhanGSKen
ea7a0bda14 添加日志,文件,BaseBean类,未调试。 2025-02-07 17:06:16 +08:00
ZhanGSKen
b26b9dbbc4 <libappbase>Library Release 1.2.0 2025-02-06 07:12:51 +08:00
ZhanGSKen
8a405d33a7 <appbase>APK 1.2.0 release Publish. 2025-02-06 07:12:30 +08:00
ZhanGSKen
2b92da3e01 添加调试模式设置功能 2025-02-06 07:11:04 +08:00
ZhanGSKen
ac1c9fb9f5 <libappbase>Library Release 1.1.3 2025-02-04 08:46:57 +08:00
28 changed files with 1614 additions and 154 deletions

View File

@@ -29,7 +29,7 @@ android {
// versionName 更新后需要手动设置
// .winboll/winbollBuildProps.properties 文件的 stageCount=0
// Gradle编译环境下合起来的 versionName 就是 "${versionName}.0"
versionName "1.1"
versionName "1.4"
if(true) {
versionName = genVersionName("${versionName}")
}

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Tue Feb 04 08:46:36 HKT 2025
stageCount=4
#Wed Feb 12 18:03:55 HKT 2025
stageCount=2
libraryProject=libappbase
baseVersion=1.1
publishVersion=1.1.3
baseVersion=1.4
publishVersion=1.4.1
buildCount=0
baseBetaVersion=1.1.4
baseBetaVersion=1.4.2

View File

@@ -6,11 +6,18 @@ package cc.winboll.studio.appbase;
* @Describe APPbase 应用类
*/
import cc.winboll.studio.libappbase.GlobalApplication;
import cc.winboll.studio.libappbase.LogUtils;
public class App extends GlobalApplication {
public static final String TAG = "App";
@Override
public void onCreate() {
GlobalApplication.setIsDebuging(this, BuildConfig.DEBUG);
super.onCreate();
LogUtils.setLogLevel(LogUtils.LOG_LEVEL.Debug);
LogUtils.setALlTAGListEnable(true);
LogUtils.d(TAG, "LogUtils init");
}
}

View File

@@ -1,15 +1,30 @@
package cc.winboll.studio.appbase;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
package cc.winboll.studio.appbase;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import androidx.appcompat.app.AppCompatActivity;
import cc.winboll.studio.libappbase.GlobalApplication;
import androidx.appcompat.widget.Toolbar;
import com.hjq.toast.ToastUtils;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ToastUtils.show("onCreate");
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.activitymainToolbar1);
setSupportActionBar(toolbar);
CheckBox cbIsDebugMode = findViewById(R.id.activitymainCheckBox1);
cbIsDebugMode.setChecked(GlobalApplication.isDebuging());
}
public void onSwitchDebugMode(View view) {
GlobalApplication.setIsDebuging(this, ((CheckBox)view).isChecked());
}
}

View File

@@ -4,13 +4,50 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello, WinBoll!"
android:onClick="onHello"/>
android:id="@+id/activitymainToolbar1"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, WinBoll!"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right|center_vertical">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Debug Mode"
android:layout_weight="1.0"
android:onClick="onSwitchDebugMode"
android:id="@+id/activitymainCheckBox1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Application CrashReport"
android:textAllCaps="false"
android:onClick="onTestApplicationCrashReport"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

View File

@@ -1,9 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:colorAccent">@color/colorAccent</item>
<item name="android:navigationBarColor">?android:colorPrimary</item>
</style>
</resources>

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#009688</color>
<color name="colorPrimaryDark">#00796B</color>
<color name="colorAccent">#FF9800</color>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#005800FF</color>
<color name="colorPrimaryDark">#005800FF</color>
<color name="colorAccent">#005800FF</color>
</resources>

View File

@@ -1,5 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="APPBaseTheme">
<item name="themeGlobalCrashActivity">@style/MyGlobalCrashActivityTheme</item>
</style>
<style name="MyGlobalCrashActivityTheme" parent="GlobalCrashActivityTheme">
<item name="colorTittle">#FFFFFFFF</item>
<item name="colorTittleBackgound">#FF00A4B3</item>
<item name="colorText">#FFFFFFFF</item>
<item name="colorTextBackgound">#FF000000</item>
</style>
</resources>

View File

@@ -26,5 +26,11 @@ android {
}
dependencies {
api 'com.github.getActivity:ToastUtils:10.5'
api 'androidx.appcompat:appcompat:1.3.1'
//api 'androidx.vectordrawable:vectordrawable:1.1.0'
//api 'androidx.vectordrawable:vectordrawable-animated:1.1.0'
api fileTree(dir: 'libs', include: ['*.jar'])
}

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Tue Feb 04 08:46:36 HKT 2025
stageCount=4
#Wed Feb 12 18:03:55 HKT 2025
stageCount=2
libraryProject=libappbase
baseVersion=1.1
publishVersion=1.1.3
baseVersion=1.4
publishVersion=1.4.1
buildCount=0
baseBetaVersion=1.1.4
baseBetaVersion=1.4.2

View File

@@ -1,13 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cc.winboll.studio.libappbase" >
<?xml version='1.0' encoding='utf-8'?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="cc.winboll.studio.libappbase">
<!-- 拥有完全的网络访问权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<application>
<activity
android:name=".CrashHandler$CrashActiviy"
android:label="CrashActiviy"
android:name=".CrashHandler$CrashActivity"
android:label="CrashActivity"
android:launchMode="standard"/>
<activity
android:name=".GlobalCrashActivity"
android:label="GlobalCrashActivity"
android:launchMode="standard"/>
<service android:name=".SimpleOperateSignalCenterService"/>
</application>
</manifest>
</manifest>

View File

@@ -0,0 +1,283 @@
package cc.winboll.studio.libappbase;
/**
* @Author ZhanGSKen@QQ.COM
* @Date 2025/01/15 11:11:52
* @Describe Json Bean 基础类。
*/
import android.content.Context;
import android.util.JsonReader;
import android.util.JsonWriter;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
public abstract class BaseBean<T extends BaseBean> {
public static final String TAG = "BaseBean";
static final String BEAN_NAME = "BeanName";
public BaseBean() {}
public abstract String getName();
public String getBeanJsonFilePath(Context context) {
return context.getExternalFilesDir(TAG) + "/" + getName() + ".json";
}
public String getBeanListJsonFilePath(Context context) {
return context.getExternalFilesDir(TAG) + "/" + getName() + "_List.json";
}
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
jsonWriter.name(BEAN_NAME).value(getName());
}
public boolean initObjectsFromJsonReader(JsonReader jsonReader, String name) throws IOException {
return false;
}
abstract public T readBeanFromJsonReader(JsonReader jsonReader) throws IOException;
public static <T extends BaseBean> String checkIsTheSameBeanListAndFile(String szFilePath, Class<T> clazz) {
StringBuilder sbResult = new StringBuilder();
String szErrorInfo = "Check Is The Same Bean List And File Error : ";
try {
int nSameCount = 0;
int nBeanListCout = 0;
T beanTemp = clazz.newInstance();
String szBeanSimpleName = beanTemp.getName();
String szListJson = UTF8FileUtils.readStringFromFile(szFilePath);
StringReader stringReader = new StringReader(szListJson);
JsonReader jsonReader = new JsonReader(stringReader);
jsonReader.beginArray();
while (jsonReader.hasNext()) {
nBeanListCout++;
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
if (name.equals(BEAN_NAME)) {
if (szBeanSimpleName.equals(jsonReader.nextString())) {
nSameCount++;
}
} else {
jsonReader.skipValue();
}
}
jsonReader.endObject();
}
jsonReader.endArray();
// 返回检查结果
if (nSameCount == nBeanListCout) {
// 检查一致直接返回空串
return "";
} else {
// 检查不一致返回对比信息
sbResult.append("Total : ");
sbResult.append(nBeanListCout);
sbResult.append(" Diff : ");
sbResult.append(nBeanListCout - nSameCount);
}
} catch (InstantiationException e) {
sbResult.append(szErrorInfo);
sbResult.append(e);
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
} catch (IllegalAccessException e) {
sbResult.append(szErrorInfo);
sbResult.append(e);
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
} catch (IOException e) {
sbResult.append(szErrorInfo);
sbResult.append(e);
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
}
return sbResult.toString();
}
public static <T extends BaseBean> T parseStringToBean(String szBean, Class<T> clazz) throws IOException {
// 创建 JsonWriter 对象
StringReader stringReader = new StringReader(szBean);
JsonReader jsonReader = new JsonReader(stringReader);
try {
T beanTemp = clazz.newInstance();
return (T)beanTemp.readBeanFromJsonReader(jsonReader);
} catch (InstantiationException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
} catch (IllegalAccessException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
}
return null;
}
public static <T extends BaseBean> boolean parseStringToBeanList(String szBeanList, ArrayList<T> beanList, Class<T> clazz) {
try {
beanList.clear();
StringReader stringReader = new StringReader(szBeanList);
JsonReader jsonReader = new JsonReader(stringReader);
jsonReader.beginArray();
while (jsonReader.hasNext()) {
T beanTemp = clazz.newInstance();
T bean = (T)beanTemp.readBeanFromJsonReader(jsonReader);
if (bean != null) {
beanList.add(bean);
//LogUtils.d(TAG, "beanList.add(bean)");
}
}
jsonReader.endArray();
return true;
//LogUtils.d(TAG, "beanList.size() is " + Integer.toString(beanList.size()));
} catch (InstantiationException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
} catch (IllegalAccessException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
} catch (IOException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
}
return false;
}
@Override
public String toString() {
// 创建 JsonWriter 对象
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter);
jsonWriter.setIndent(" ");
try {// 开始 JSON 对象
jsonWriter.beginObject();
// 写入键值对
writeThisToJsonWriter(jsonWriter);
// 结束 JSON 对象
jsonWriter.endObject();
return stringWriter.toString();
} catch (IOException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
}
// 获取 JSON 字符串
return "";
}
public static <T extends BaseBean> String toStringByBeanList(ArrayList<T> beanList) {
try {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter);
jsonWriter.setIndent(" ");
jsonWriter.beginArray();
for (int i = 0; i < beanList.size(); i++) {
// 开始 JSON 对象
jsonWriter.beginObject();
// 写入键值对
beanList.get(i).writeThisToJsonWriter(jsonWriter);
// 结束 JSON 对象
jsonWriter.endObject();
}
jsonWriter.endArray();
jsonWriter.close();
return stringWriter.toString();
} catch (IOException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
}
return "";
}
public static <T extends BaseBean> T loadBean(Context context, Class<T> clazz) {
try {
T beanTemp = clazz.newInstance();
return loadBeanFromFile(beanTemp.getBeanJsonFilePath(context), clazz);
} catch (InstantiationException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
} catch (IllegalAccessException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
}
return null;
}
public static <T extends BaseBean> T loadBeanFromFile(String szFilePath, Class<T> clazz) {
try {
try {
File fTemp = new File(szFilePath);
if (fTemp.exists()) {
T beanTemp = clazz.newInstance();String szJson = UTF8FileUtils.readStringFromFile(szFilePath);
return beanTemp.parseStringToBean(szJson, clazz);
}
} catch (InstantiationException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
} catch (IllegalAccessException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
}
} catch (IOException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
}
return null;
}
public static <T extends BaseBean> boolean saveBean(Context context, T bean) {
return saveBeanToFile(bean.getBeanJsonFilePath(context), bean);
}
public static <T extends BaseBean> boolean saveBeanToFile(String szFilePath, T bean) {
try {
String szJson = bean.toString();
UTF8FileUtils.writeStringToFile(szFilePath, szJson);
return true;
} catch (IOException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
}
return false;
}
public static <T extends BaseBean> boolean loadBeanList(Context context, ArrayList<T> beanListDst, Class<T> clazz) {
try {
T beanTemp = clazz.newInstance();
return loadBeanListFromFile(beanTemp.getBeanListJsonFilePath(context), beanListDst, clazz);
} catch (InstantiationException e) {} catch (IllegalAccessException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
}
return false;
}
public static <T extends BaseBean> boolean loadBeanListFromFile(String szFilePath, ArrayList<T> beanList, Class<T> clazz) {
try {
File fTemp = new File(szFilePath);
if (fTemp.exists()) {
String szListJson = UTF8FileUtils.readStringFromFile(szFilePath);
return parseStringToBeanList(szListJson, beanList, clazz);
}
} catch (IOException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
}
return false;
}
public static <T extends BaseBean> boolean saveBeanList(Context context, ArrayList<T> beanList, Class<T> clazz) {
try {
T beanTemp = clazz.newInstance();
return saveBeanListToFile(beanTemp.getBeanListJsonFilePath(context), beanList);
} catch (InstantiationException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
} catch (IllegalAccessException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
}
return false;
}
public static <T extends BaseBean> boolean saveBeanListToFile(String szFilePath, ArrayList<T> beanList) {
try {
String szJson = toStringByBeanList(beanList);
UTF8FileUtils.writeStringToFile(szFilePath, szJson);
//LogUtils.d(TAG, "FileUtil.writeFile beanList.size() is " + Integer.toString(beanList.size()));
return true;
} catch (IOException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
}
return false;
}
}

View File

@@ -12,23 +12,35 @@ import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;
import cc.winboll.studio.libappbase.R;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread.UncaughtExceptionHandler;
@@ -38,9 +50,22 @@ import java.util.Locale;
public final class CrashHandler {
public static final String TAG = "CrashHandler";
public static final String TITTLE = "CrashReport";
public static final String EXTRA_CRASH_INFO = "crashInfo";
final static String PREFS = CrashHandler.class.getName() + "PREFS";
final static String PREFS_CRASHHANDLER_ISCRASHHAPPEN = "PREFS_CRASHHANDLER_ISCRASHHAPPEN";
public static String _CrashCountFilePath;
public static final UncaughtExceptionHandler DEFAULT_UNCAUGHT_EXCEPTION_HANDLER = Thread.getDefaultUncaughtExceptionHandler();
public static void init(Application app) {
_CrashCountFilePath = app.getExternalFilesDir("CrashHandler") + "/IsCrashHandlerCrashHappen.dat";
LogUtils.d(TAG, String.format("_CrashCountFilePath %s", _CrashCountFilePath));
init(app, null);
}
@@ -59,6 +84,9 @@ public final class CrashHandler {
}
private void tryUncaughtException(Thread thread, Throwable throwable) {
// 每到这里就燃烧一次保险丝
AppCrashSafetyWire.getInstance().burnSafetyWire();
final String time = new SimpleDateFormat("yyyy_MM_dd-HH_mm_ss", Locale.getDefault()).format(new Date());
File crashFile = new File(TextUtils.isEmpty(crashDir) ? new File(app.getExternalFilesDir(null), "crash")
: new File(crashDir), "crash_" + time + ".txt");
@@ -82,13 +110,13 @@ public final class CrashHandler {
StringBuilder sb = new StringBuilder();
sb.append("************* Crash Head ****************\n");
sb.append("Time Of Crash : ").append(time).append("\n");
sb.append("Device Manufacturer: ").append(Build.MANUFACTURER).append("\n");
sb.append("Device Model : ").append(Build.MODEL).append("\n");
sb.append("Android Version : ").append(Build.VERSION.RELEASE).append("\n");
sb.append("Android SDK : ").append(Build.VERSION.SDK_INT).append("\n");
sb.append("App VersionName : ").append(versionName).append("\n");
sb.append("App VersionCode : ").append(versionCode).append("\n");
sb.append("Time Of Crash : ").append(time).append("\n");
sb.append("Device Manufacturer : ").append(Build.MANUFACTURER).append("\n");
sb.append("Device Model : ").append(Build.MODEL).append("\n");
sb.append("Android Version : ").append(Build.VERSION.RELEASE).append("\n");
sb.append("Android SDK : ").append(Build.VERSION.SDK_INT).append("\n");
sb.append("App VersionName : ").append(versionName).append("\n");
sb.append("App VersionCode : ").append(versionCode).append("\n");
sb.append("************* Crash Head ****************\n");
sb.append("\n").append(fullStackTrace);
@@ -99,12 +127,33 @@ public final class CrashHandler {
} catch (IOException ignored) {}
gotoCrashActiviy: {
Intent intent = new Intent(app, CrashActiviy.class);
Intent intent = new Intent();
LogUtils.d(TAG, "gotoCrashActiviy: ");
if (AppCrashSafetyWire.getInstance().isAppCrashSafetyWireOK()) {
LogUtils.d(TAG, "gotoCrashActiviy: isAppCrashSafetyWireOK");
intent.setClass(app, GlobalCrashActivity.class);
intent.putExtra(EXTRA_CRASH_INFO, errorLog);
// 如果发生了 CrashHandler 内部崩溃, 就调用基础的应用崩溃显示类
// intent.setClass(app, GlobalCrashActiviy.class);
// intent.putExtra(GlobalCrashActiviy.EXTRA_CRASH_INFO, errorLog);
} else {
LogUtils.d(TAG, "gotoCrashActiviy: else");
// 正常状态调用进阶的应用崩溃显示页
intent.setClass(app, CrashActivity.class);
intent.putExtra(EXTRA_CRASH_INFO, errorLog);
}
// intent.setClass(app, CrashActiviy.class);
// intent.putExtra(CrashActiviy.EXTRA_CRASH_INFO, errorLog);
intent.addFlags(
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK
);
intent.putExtra(CrashActiviy.EXTRA_CRASH_INFO, errorLog);
try {
app.startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid());
@@ -113,8 +162,13 @@ public final class CrashHandler {
e.printStackTrace();
if (DEFAULT_UNCAUGHT_EXCEPTION_HANDLER != null)
DEFAULT_UNCAUGHT_EXCEPTION_HANDLER.uncaughtException(thread, throwable);
} catch (Exception e) {
e.printStackTrace();
if (DEFAULT_UNCAUGHT_EXCEPTION_HANDLER != null)
DEFAULT_UNCAUGHT_EXCEPTION_HANDLER.uncaughtException(thread, throwable);
}
}
}
private void writeFile(File file, String content) throws IOException {
@@ -133,10 +187,136 @@ public final class CrashHandler {
});
}
public static final class CrashActiviy extends Activity implements MenuItem.OnMenuItemClickListener {
//
// 应用崩溃保险丝
//
public static final class AppCrashSafetyWire {
private static final String EXTRA_CRASH_INFO = "crashInfo";
volatile static AppCrashSafetyWire _AppCrashSafetyWire;
volatile Integer currentSafetyLevel; // 熔断值,为 0 表示熔断了。
private static final int _MINI = 1;
private static final int _MAX = 2;
AppCrashSafetyWire() {
LogUtils.d(TAG, "AppCrashSafetyWire()");
currentSafetyLevel = loadCurrentSafetyLevel();
}
public static synchronized AppCrashSafetyWire getInstance() {
if (_AppCrashSafetyWire == null) {
_AppCrashSafetyWire = new AppCrashSafetyWire();
}
return _AppCrashSafetyWire;
}
public void setCurrentSafetyLevel(int currentSafetyLevel) {
this.currentSafetyLevel = currentSafetyLevel;
}
public int getCurrentSafetyLevel() {
return currentSafetyLevel;
}
public void saveCurrentSafetyLevel(int currentSafetyLevel) {
LogUtils.d(TAG, "saveCurrentSafetyLevel()");
this.currentSafetyLevel = currentSafetyLevel;
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(_CrashCountFilePath));
oos.writeInt(currentSafetyLevel);
oos.flush();
oos.close();
LogUtils.d(TAG, String.format("saveCurrentSafetyLevel writeInt currentSafetyLevel %d", currentSafetyLevel));
} catch (IOException e) {
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
}
}
public int loadCurrentSafetyLevel() {
LogUtils.d(TAG, "loadCurrentSafetyLevel()");
try {
File f = new File(_CrashCountFilePath);
if (f.exists()) {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(_CrashCountFilePath));
currentSafetyLevel = ois.readInt();
LogUtils.d(TAG, String.format("loadCurrentSafetyLevel() readInt currentSafetyLevel %d", currentSafetyLevel));
} else {
currentSafetyLevel = _MAX;
LogUtils.d(TAG, String.format("loadCurrentSafetyLevel() currentSafetyLevel init to _MAX->%d", _MAX));
saveCurrentSafetyLevel(currentSafetyLevel);
}
} catch (IOException e) {
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
}
return currentSafetyLevel;
}
boolean burnSafetyWire() {
LogUtils.d(TAG, "burnSafetyWire()");
// 崩溃计数进入崩溃保险值
int safeLevel = loadCurrentSafetyLevel();
if (isSafetyWireWorking(safeLevel)) {
// 如果保险丝未熔断, 就增加一次熔断值
LogUtils.d(TAG, "burnSafetyWire() use");
saveCurrentSafetyLevel(safeLevel - 1);
return isSafetyWireWorking(safeLevel - 1);
}
return false;
}
boolean isSafetyWireWorking(int safetyLevel) {
LogUtils.d(TAG, "isSafetyWireOK()");
//safetyLevel = _MINI;
//safetyLevel = _MINI - 1;
//safetyLevel = _MINI + 1;
//safetyLevel = _MAX;
//safetyLevel = _MAX + 1;
LogUtils.d(TAG, String.format("SafetyLevel %d", safetyLevel));
if (safetyLevel >= _MINI && safetyLevel <= _MAX) {
// 如果在保险值之内
LogUtils.d(TAG, String.format("In Safety Level"));
return true;
}
LogUtils.d(TAG, String.format("Out of Safety Level"));
return false;
}
void resumeToMaximumImmediately() {
LogUtils.d(TAG, "resumeToMaximumImmediately() call saveCurrentSafetyLevel(_MAX)");
AppCrashSafetyWire.getInstance().saveCurrentSafetyLevel(_MAX);
}
void off() {
LogUtils.d(TAG, "off()");
saveCurrentSafetyLevel(_MINI);
}
boolean isAppCrashSafetyWireOK() {
LogUtils.d(TAG, "isAppCrashSafetyWireOK()");
currentSafetyLevel = loadCurrentSafetyLevel();
return isSafetyWireWorking(currentSafetyLevel);
}
// 调用函数以启用持续崩溃保险,从而调用 CrashHandler 内部崩溃处理窗口
void postResumeCrashSafetyWireHandler(final Context context) {
new Handler(Looper.getMainLooper()).postDelayed(new Runnable(){
@Override
public void run() {
LogUtils.d(TAG, "Handler run()");
if (!AppCrashSafetyWire.getInstance().isSafetyWireWorking(currentSafetyLevel - 1)) {
// 如果下一次应用崩溃时,保险丝熔断,则先恢复保险丝满能状态
// 进程持续运行时,恢复保险丝熔断值
//Resume to maximum
AppCrashSafetyWire.getInstance().resumeToMaximumImmediately();
LogUtils.d(TAG, "postResumeCrashSafetyWireHandler");
}
}
}, 500);
}
}
public static final class CrashActivity extends Activity implements MenuItem.OnMenuItemClickListener {
private static final int MENUITEM_COPY = 0;
private static final int MENUITEM_RESTART = 1;
@@ -145,11 +325,14 @@ public final class CrashHandler {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppCrashSafetyWire.getInstance().postResumeCrashSafetyWireHandler(getApplicationContext());
mLog = getIntent().getStringExtra(EXTRA_CRASH_INFO);
setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar);
setContentView: {
ScrollView contentView = new ScrollView(this);
contentView.setFillViewport(true);
HorizontalScrollView hw = new HorizontalScrollView(this);
hw.setBackgroundColor(Color.GRAY);
TextView message = new TextView(this); {
@@ -159,8 +342,11 @@ public final class CrashHandler {
message.setTextIsSelectable(true);
}
hw.addView(message);
contentView.addView(hw, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
setContentView(contentView);
getActionBar().setTitle(TITTLE);
getActionBar().setSubtitle(GlobalApplication.class.getSimpleName() + " Error");
}
}
@@ -198,6 +384,7 @@ public final class CrashHandler {
Toast.makeText(getApplication(), "The text is copied.", Toast.LENGTH_SHORT).show();
break;
case MENUITEM_RESTART:
AppCrashSafetyWire.getInstance().resumeToMaximumImmediately();
restart();
break;
}

View File

@@ -5,51 +5,46 @@ package cc.winboll.studio.libappbase;
* @Date 2025/01/05 10:10:23
* @Describe 全局应用类
*/
import android.app.Activity;
import android.app.Application;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.Thread.UncaughtExceptionHandler;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import android.content.pm.ApplicationInfo;
import android.content.res.AssetManager;
import android.view.Gravity;
import com.hjq.toast.ToastUtils;
import com.hjq.toast.style.WhiteToastStyle;
public class GlobalApplication extends Application {
public static final String TAG = "GlobalApplication";
final static String PREFS = GlobalApplication.class.getName() + "PREFS";
final static String PREFS_ISDEBUGING = "PREFS_ISDEBUGING";
private static Handler MAIN_HANDLER = new Handler(Looper.getMainLooper());
// 是否处于调试状态
volatile static boolean isDebuging = false;
public static void setIsDebuging(Context context, boolean isDebuging) {
GlobalApplication.isDebuging = isDebuging;
// 获取SharedPreferences实例
SharedPreferences sharedPreferences = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
// 获取编辑器
SharedPreferences.Editor editor = sharedPreferences.edit();
// 保存数据
editor.putBoolean(PREFS_ISDEBUGING, GlobalApplication.isDebuging);
// 提交更改
editor.apply();
}
public static boolean isDebuging() {
return isDebuging;
}
@Override
public Context getApplicationContext() {
return super.getApplicationContext();
@@ -62,45 +57,38 @@ public class GlobalApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//GlobalApplication.isDebuging = true;
//GlobalApplication.setIsDebuging(this, true);
LogUtils.init(this);
//LogUtils.setLogLevel(LogUtils.LOG_LEVEL.Debug);
//LogUtils.setTAGListEnable(GlobalApplication.TAG, true);
//LogUtils.setALlTAGListEnable(true);
//LogUtils.d(TAG, "LogUtils init");
// 设置应用异常处理窗口
CrashHandler.init(this);
// 设置应用调试状态
//SharedPreferences sharedPreferences = getSharedPreferences(PREFS, Context.MODE_PRIVATE);
//GlobalApplication.isDebuging = sharedPreferences.getBoolean(PREFS_ISDEBUGING, GlobalApplication.isDebuging);
// 初始化 Toast 框架
ToastUtils.init(this);
// 设置 Toast 布局样式
//ToastUtils.setView(R.layout.toast_custom_view);
ToastUtils.setStyle(new WhiteToastStyle());
ToastUtils.setGravity(Gravity.BOTTOM, 0, 200);
}
public static void write(InputStream input, OutputStream output) throws IOException {
byte[] buf = new byte[1024 * 8];
int len;
while ((len = input.read(buf)) != -1) {
output.write(buf, 0, len);
}
}
public static void write(File file, byte[] data) throws IOException {
File parent = file.getParentFile();
if (parent != null && !parent.exists()) parent.mkdirs();
ByteArrayInputStream input = new ByteArrayInputStream(data);
FileOutputStream output = new FileOutputStream(file);
public static String getAppName(Context context) {
PackageManager packageManager = context.getPackageManager();
try {
write(input, output);
} finally {
closeIO(input, output);
}
}
public static String toString(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
write(input, output);
try {
return output.toString("UTF-8");
} finally {
closeIO(input, output);
}
}
public static void closeIO(Closeable... closeables) {
for (Closeable closeable : closeables) {
try {
if (closeable != null) closeable.close();
} catch (IOException ignored) {}
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(
context.getPackageName(), 0);
return (String) packageManager.getApplicationLabel(applicationInfo);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
}

View File

@@ -0,0 +1,119 @@
package cc.winboll.studio.libappbase;
/**
* @Author ZhanGSKen@AliYun.Com
* @Date 2025/02/11 00:14:05
*/
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import cc.winboll.studio.libappbase.R;
import androidx.appcompat.app.AppCompatActivity;
public final class GlobalCrashActivity extends AppCompatActivity implements MenuItem.OnMenuItemClickListener {
private static final int MENUITEM_COPY = 0;
private static final int MENUITEM_RESTART = 1;
GlobalCrashReportView mGlobalCrashReportView;
String mLog;
public static final String TAG = "GlobalCrashActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CrashHandler.AppCrashSafetyWire.getInstance().postResumeCrashSafetyWireHandler(getApplicationContext());
mLog = getIntent().getStringExtra(CrashHandler.EXTRA_CRASH_INFO);
//setTheme(android.R.style.Theme_Holo_Light_NoActionBar);
//setTheme(R.style.APPBaseTheme);
setContentView(R.layout.activity_globalcrash);
mGlobalCrashReportView = findViewById(R.id.activityglobalcrashGlobalCrashReportView1);
mGlobalCrashReportView.setReport(mLog);
setSupportActionBar(mGlobalCrashReportView.getToolbar());
getSupportActionBar().setTitle(CrashHandler.TITTLE);
getSupportActionBar().setSubtitle(GlobalApplication.getAppName(getApplicationContext()));
}
@Override
public void onBackPressed() {
restart();
}
private void restart() {
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(getPackageName());
if (intent != null) {
intent.addFlags(
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK
);
startActivity(intent);
}
finish();
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case MENUITEM_COPY:
ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
cm.setPrimaryClip(ClipData.newPlainText(getPackageName(), mLog));
Toast.makeText(getApplication(), "The text is copied.", Toast.LENGTH_SHORT).show();
break;
case MENUITEM_RESTART:
CrashHandler.AppCrashSafetyWire.getInstance().resumeToMaximumImmediately();
restart();
break;
}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENUITEM_COPY, 0, "Copy").setOnMenuItemClickListener(this)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add(0, MENUITEM_RESTART, 0, "Restart").setOnMenuItemClickListener(this)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
// 更新菜单文字风格
mGlobalCrashReportView.updateMenuStyle();
return true;
}
void joinQQGroup(String key) {
// 创建Intent
Intent intent = new Intent();
// 设置动作
intent.setAction("android.intent.action.VIEW");
// 设置数据为网址的URI
Uri content_url = Uri.parse("https://www.winboll.cc");
intent.setData(content_url);
// 添加标志
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 设置类名和活动名
intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
// 启动Activity
startActivity(intent);
}
}

View File

@@ -0,0 +1,138 @@
package cc.winboll.studio.libappbase;
/**
* @Author ZhanGSKen@AliYun.Com
* @Date 2025/02/11 20:18:30
* @Describe 应用崩溃报告视图
*/
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import cc.winboll.studio.libappbase.R;
public class GlobalCrashReportView extends LinearLayout {
public static final String TAG = "GlobalCrashReportView";
Context mContext;
Toolbar mToolbar;
int colorTittle;
int colorTittleBackground;
int colorText;
int colorTextBackground;
TextView mtvReport;
public GlobalCrashReportView(Context context) {
super(context);
mContext = context;
//initView();
}
public GlobalCrashReportView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
initView(attrs);
}
public GlobalCrashReportView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
//initView();
}
public GlobalCrashReportView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mContext = context;
//initView();
}
public void setColorTittle(int colorTittle) {
this.colorTittle = colorTittle;
}
public int getColorTittle() {
return colorTittle;
}
public void setColorTittleBackground(int colorTittleBackground) {
this.colorTittleBackground = colorTittleBackground;
}
public int getColorTittleBackground() {
return colorTittleBackground;
}
public void setColorText(int colorText) {
this.colorText = colorText;
}
public int getColorText() {
return colorText;
}
public void setColorTextBackground(int colorTextBackground) {
this.colorTextBackground = colorTextBackground;
}
public int getColorTextBackground() {
return colorTextBackground;
}
void initView(AttributeSet attrs) {
TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.GlobalCrashActivity, R.attr.themeGlobalCrashActivity, 0);
this.colorTittle = a.getColor(R.styleable.GlobalCrashActivity_colorTittle, Color.WHITE);
this.colorTittleBackground = a.getColor(R.styleable.GlobalCrashActivity_colorTittleBackgound, Color.BLACK);
this.colorText = a.getColor(R.styleable.GlobalCrashActivity_colorText, Color.BLACK);
this.colorTextBackground = a.getColor(R.styleable.GlobalCrashActivity_colorTextBackgound, Color.WHITE);
// 返回一个绑定资源结束的信号给资源
a.recycle();
/*this.colorTittle = Color.WHITE;
this.colorTittleBackground = Color.BLACK;
this.colorText = Color.BLACK;
this.colorTextBackground = Color.WHITE;
*/
inflate(mContext, R.layout.view_globalcrashreport, this);
LinearLayout llMain = findViewById(R.id.viewglobalcrashreportLinearLayout1);
llMain.setBackgroundColor(this.colorTextBackground);
mToolbar = findViewById(R.id.viewglobalcrashreportToolbar1);
mToolbar.setBackgroundColor(this.colorTittleBackground);
mToolbar.setTitleTextColor(this.colorTittle);
mToolbar.setSubtitleTextColor(this.colorTittle);
mtvReport = findViewById(R.id.viewglobalcrashreportTextView1);
mtvReport.setTextColor(this.colorText);
mtvReport.setBackgroundColor(this.colorTextBackground);
}
public void setReport(String report) {
mtvReport.setText(report);
}
public Toolbar getToolbar() {
return mToolbar;
}
//
// 更新菜单文字风格
//
public void updateMenuStyle() {
// 设置菜单文本颜色
Menu menu = mToolbar.getMenu();
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
SpannableString spanString = new SpannableString(item.getTitle().toString());
spanString.setSpan(new ForegroundColorSpan(this.colorTittle), 0, spanString.length(), 0);
item.setTitle(spanString);
}
}
}

View File

@@ -0,0 +1,370 @@
package cc.winboll.studio.libappbase;
/**
* @Author ZhanGSKen@QQ.COM
* @Date 2024/08/12 13:44:06
* @Describe LogUtils
* @Describe 应用日志类
*/
import android.content.Context;
import cc.winboll.studio.libappbase.GlobalApplication;
import dalvik.system.DexFile;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class LogUtils {
public static final String TAG = "LogUtils";
public static enum LOG_LEVEL { Off, Error, Warn, Info, Debug, Verbose }
static volatile boolean _IsInited = false;
static Context _mContext;
// 日志显示时间格式
static SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("[yyyyMMdd_HHmmSS]", Locale.getDefault());
// 应用日志文件夹
static File _mfLogCacheDir;
static File _mfLogDataDir;
// 应用日志文件
static File _mfLogCatchFile;
static File _mfLogUtilsBeanFile;
static LogUtilsBean _mLogUtilsBean;
public static Map<String, Boolean> mapTAGList = new HashMap<String, Boolean>();
//
// 初始化函数
//
public static void init(Context context) {
_mContext = context;
init(context, LOG_LEVEL.Off);
}
//
// 初始化函数
//
public static void init(Context context, LOG_LEVEL logLevel) {
if (GlobalApplication.isDebuging()) {
// 初始化日志缓存文件路径
_mfLogCacheDir = new File(context.getApplicationContext().getExternalCacheDir(), TAG);
if (!_mfLogCacheDir.exists()) {
_mfLogCacheDir.mkdirs();
}
_mfLogCatchFile = new File(_mfLogCacheDir, "log.txt");
// 初始化日志配置文件路径
_mfLogDataDir = context.getApplicationContext().getExternalFilesDir(TAG);
if (!_mfLogDataDir.exists()) {
_mfLogDataDir.mkdirs();
}
_mfLogUtilsBeanFile = new File(_mfLogDataDir, TAG + ".json");
} else {
// 初始化日志缓存文件路径
_mfLogCacheDir = new File(context.getApplicationContext().getCacheDir(), TAG);
if (!_mfLogCacheDir.exists()) {
_mfLogCacheDir.mkdirs();
}
_mfLogCatchFile = new File(_mfLogCacheDir, "log.txt");
// 初始化日志配置文件路径
_mfLogDataDir = new File(context.getApplicationContext().getFilesDir(), TAG);
if (!_mfLogDataDir.exists()) {
_mfLogDataDir.mkdirs();
}
_mfLogUtilsBeanFile = new File(_mfLogDataDir, TAG + ".json");
}
// Toast.makeText(context,
// "_mfLogUtilsBeanFile : " + _mfLogUtilsBeanFile
// + "\n_mfLogCatchFile : " + _mfLogCatchFile,
// Toast.LENGTH_SHORT).show();
//
_mLogUtilsBean = LogUtilsBean.loadBeanFromFile(_mfLogUtilsBeanFile.getPath(), LogUtilsBean.class);
if (_mLogUtilsBean == null) {
_mLogUtilsBean = new LogUtilsBean();
_mLogUtilsBean.saveBeanToFile(_mfLogUtilsBeanFile.getPath(), _mLogUtilsBean);
}
// 加载当前应用下的所有类的 TAG
addClassTAGList();
loadTAGBeanSettings();
_IsInited = true;
LogUtils.d(TAG, String.format("mapTAGList : %s", mapTAGList.toString()));
}
public static Map<String, Boolean> getMapTAGList() {
return mapTAGList;
}
static void loadTAGBeanSettings() {
ArrayList<LogUtilsClassTAGBean> list = new ArrayList<LogUtilsClassTAGBean>();
LogUtilsClassTAGBean.loadBeanList(_mContext, list, LogUtilsClassTAGBean.class);
for (int i = 0; i < list.size(); i++) {
LogUtilsClassTAGBean beanSetting = list.get(i);
for (Map.Entry<String, Boolean> entry : mapTAGList.entrySet()) {
if (entry.getKey().equals(beanSetting.getTag())) {
entry.setValue(beanSetting.getEnable());
}
}
}
}
static void saveTAGBeanSettings() {
ArrayList<LogUtilsClassTAGBean> list = new ArrayList<LogUtilsClassTAGBean>();
for (Map.Entry<String, Boolean> entry : mapTAGList.entrySet()) {
list.add(new LogUtilsClassTAGBean(entry.getKey(), entry.getValue()));
}
LogUtilsClassTAGBean.saveBeanList(_mContext, list, LogUtilsClassTAGBean.class);
}
static void addClassTAGList() {
//ClassLoader classLoader = getClass().getClassLoader();
try {
//String packageName = context.getPackageName();
String packageNamePrefix = "cc.winboll.studio";
List<String> classNames = new ArrayList<>();
String apkPath = _mContext.getPackageCodePath();
//Log.d("APK_PATH", "The APK path is: " + apkPath);
LogUtils.d(TAG, String.format("apkPath : %s", apkPath));
//String apkPath = "/data/app/" + packageName + "-";
//DexFile dexfile = new DexFile(apkPath + "1/base.apk");
DexFile dexfile = new DexFile(apkPath);
int countTemp = 0;
Enumeration<String> entries = dexfile.entries();
while (entries.hasMoreElements()) {
countTemp++;
String className = entries.nextElement();
if (className.startsWith(packageNamePrefix)) {
classNames.add(className);
}
}
LogUtils.d(TAG, String.format("countTemp : %d\nClassNames size : %d", countTemp, classNames.size()));
for (String className : classNames) {
try {
Class<?> clazz = Class.forName(className);
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (Modifier.isStatic(field.getModifiers()) && Modifier.isPublic(field.getModifiers()) && field.getType() == String.class && "TAG".equals(field.getName())) {
String tagValue = (String) field.get(null);
//Log.d("TAG_INFO", "Class: " + className + ", TAG value: " + tagValue);
//LogUtils.d(TAG, String.format("Tag Value : %s", tagValue));
//mapTAGList.put(tagValue, true);
mapTAGList.put(tagValue, false);
}
}
} catch (NoClassDefFoundError | ClassNotFoundException | IllegalAccessException e) {
LogUtils.d(TAG, e.getMessage(), Thread.currentThread().getStackTrace());
//LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
//Toast.makeText(context, TAG + " : " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
} catch (IOException e) {
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
//Toast.makeText(context, TAG + " : " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
public static void setTAGListEnable(String tag, boolean isEnable) {
Iterator<Map.Entry<String, Boolean>> iterator = mapTAGList.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Boolean> entry = iterator.next();
if (tag.equals(entry.getKey())) {
entry.setValue(isEnable);
//System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
break;
}
}
saveTAGBeanSettings();
LogUtils.d(TAG, String.format("mapTAGList : %s", mapTAGList.toString()));
}
public static void setALlTAGListEnable(boolean isEnable) {
Iterator<Map.Entry<String, Boolean>> iterator = mapTAGList.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Boolean> entry = iterator.next();
entry.setValue(isEnable);
//System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
saveTAGBeanSettings();
LogUtils.d(TAG, String.format("mapTAGList : %s", mapTAGList.toString()));
}
public static void setLogLevel(LOG_LEVEL logLevel) {
LogUtils._mLogUtilsBean.setLogLevel(logLevel);
_mLogUtilsBean.saveBeanToFile(_mfLogUtilsBeanFile.getPath(), _mLogUtilsBean);
}
public static LOG_LEVEL getLogLevel() {
return LogUtils._mLogUtilsBean.getLogLevel();
}
static boolean isLoggable(String tag, LOG_LEVEL logLevel) {
return _IsInited && mapTAGList.get(tag) && isInTheLevel(logLevel);
}
static boolean isInTheLevel(LOG_LEVEL logLevel) {
return (LogUtils._mLogUtilsBean.getLogLevel().ordinal() == logLevel.ordinal()
|| LogUtils._mLogUtilsBean.getLogLevel().ordinal() > logLevel.ordinal());
}
//
// 获取应用日志文件夹
//
public static File getLogCacheDir() {
return _mfLogCacheDir;
}
//
// 调试日志写入函数
//
public static void e(String szTAG, String szMessage) {
if (isLoggable(szTAG, LogUtils.LOG_LEVEL.Error)) {
saveLog(szTAG, LogUtils.LOG_LEVEL.Error, szMessage);
}
}
//
// 调试日志写入函数
//
public static void w(String szTAG, String szMessage) {
if (isLoggable(szTAG, LogUtils.LOG_LEVEL.Warn)) {
saveLog(szTAG, LogUtils.LOG_LEVEL.Warn, szMessage);
}
}
//
// 调试日志写入函数
//
public static void i(String szTAG, String szMessage) {
if (isLoggable(szTAG, LogUtils.LOG_LEVEL.Info)) {
saveLog(szTAG, LogUtils.LOG_LEVEL.Info, szMessage);
}
}
//
// 调试日志写入函数
//
public static void d(String szTAG, String szMessage) {
if (isLoggable(szTAG, LogUtils.LOG_LEVEL.Debug)) {
saveLog(szTAG, LogUtils.LOG_LEVEL.Debug, szMessage);
}
}
//
// 调试日志写入函数
// 包含线程调试堆栈信息
//
public static void d(String szTAG, String szMessage, StackTraceElement[] listStackTrace) {
if (isLoggable(szTAG, LogUtils.LOG_LEVEL.Debug)) {
StringBuilder sbMessage = new StringBuilder(szMessage);
sbMessage.append(" \nAt ");
sbMessage.append(listStackTrace[2].getMethodName());
sbMessage.append(" (");
sbMessage.append(listStackTrace[2].getFileName());
sbMessage.append(":");
sbMessage.append(listStackTrace[2].getLineNumber());
sbMessage.append(")");
saveLog(szTAG, LogUtils.LOG_LEVEL.Debug, sbMessage.toString());
}
}
//
// 调试日志写入函数
// 包含异常信息和线程调试堆栈信息
//
public static void d(String szTAG, Exception e, StackTraceElement[] listStackTrace) {
if (isLoggable(szTAG, LogUtils.LOG_LEVEL.Debug)) {
StringBuilder sbMessage = new StringBuilder(e.getClass().toGenericString());
sbMessage.append(" : ");
sbMessage.append(e.getMessage());
sbMessage.append(" \nAt ");
sbMessage.append(listStackTrace[2].getMethodName());
sbMessage.append(" (");
sbMessage.append(listStackTrace[2].getFileName());
sbMessage.append(":");
sbMessage.append(listStackTrace[2].getLineNumber());
sbMessage.append(")");
saveLog(szTAG, LogUtils.LOG_LEVEL.Debug, sbMessage.toString());
}
}
//
// 调试日志写入函数
//
public static void v(String szTAG, String szMessage) {
if (isLoggable(szTAG, LogUtils.LOG_LEVEL.Verbose)) {
saveLog(szTAG, LogUtils.LOG_LEVEL.Verbose, szMessage);
}
}
//
// 日志文件保存函数
//
static void saveLog(String szTAG, LogUtils.LOG_LEVEL logLevel, String szMessage) {
try {
BufferedWriter out = null;
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(_mfLogCatchFile, true), "UTF-8"));
out.write("[" + logLevel + "] " + mSimpleDateFormat.format(System.currentTimeMillis()) + " [" + szTAG + "]\n" + szMessage + "\n");
out.close();
} catch (IOException e) {
LogUtils.d(TAG, "IOException : " + e.getMessage());
}
}
//
// 历史日志加载函数
//
public static String loadLog() {
if (_mfLogCatchFile.exists()) {
StringBuffer sb = new StringBuffer();
try {
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(new FileInputStream(_mfLogCatchFile), "UTF-8"));
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
} catch (IOException e) {
LogUtils.d(TAG, "IOException : " + e.getMessage());
}
return sb.toString();
}
return "";
}
//
// 清理日志函数
//
public static void cleanLog() {
if (_mfLogCatchFile.exists()) {
try {
UTF8FileUtils.writeStringToFile(_mfLogCatchFile.getPath(), "");
//LogUtils.d(TAG, "cleanLog");
} catch (IOException e) {
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
}
}
}
}

View File

@@ -0,0 +1,70 @@
package cc.winboll.studio.libappbase;
/**
* @Author ZhanGSKen@QQ.COM
* @Date 2024/08/23 15:39:07
* @Describe LogUtils 数据配置类。
*/
import android.util.JsonReader;
import android.util.JsonWriter;
import java.io.IOException;
public class LogUtilsBean extends BaseBean {
public static final String TAG = "LogUtilsBean";
LogUtils.LOG_LEVEL logLevel;
public LogUtilsBean() {
this.logLevel = LogUtils.LOG_LEVEL.Off;
}
public LogUtilsBean(LogUtils.LOG_LEVEL logLevel) {
this.logLevel = logLevel;
}
public void setLogLevel(LogUtils.LOG_LEVEL logLevel) {
this.logLevel = logLevel;
}
public LogUtils.LOG_LEVEL getLogLevel() {
return logLevel;
}
@Override
public String getName() {
return LogUtilsBean.class.getName();
}
@Override
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
super.writeThisToJsonWriter(jsonWriter);
LogUtilsBean bean = this;
jsonWriter.name("logLevel").value(bean.getLogLevel().ordinal());
}
@Override
public boolean initObjectsFromJsonReader(JsonReader jsonReader, String name) throws IOException {
if (super.initObjectsFromJsonReader(jsonReader, name)) { return true; } else {
if (name.equals("logLevel")) {
setLogLevel(LogUtils.LOG_LEVEL.values()[jsonReader.nextInt()]);
} else {
return false;
}
}
return true;
}
@Override
public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
if (!initObjectsFromJsonReader(jsonReader, name)) {
jsonReader.skipValue();
}
}
// 结束 JSON 对象
jsonReader.endObject();
return this;
}
}

View File

@@ -0,0 +1,87 @@
package cc.winboll.studio.libappbase;
/**
* @Author ZhanGSKen@QQ.COM
* @Date 2025/01/04 14:17:02
* @Describe 日志类class TAG 标签数据类
*/
import android.util.JsonReader;
import android.util.JsonWriter;
import java.io.IOException;
public class LogUtilsClassTAGBean extends BaseBean {
public static final String TAG = "LogUtilsClassTAGBean";
// 标签名
String tag;
// 是否启用
Boolean enable;
public LogUtilsClassTAGBean() {
this.tag = TAG;
this.enable = true;
}
public LogUtilsClassTAGBean(String tag, Boolean enable) {
this.tag = tag;
this.enable = enable;
}
public void setTag(String tag) {
this.tag = tag;
}
public String getTag() {
return tag;
}
public void setEnable(Boolean enable) {
this.enable = enable;
}
public Boolean getEnable() {
return enable;
}
@Override
public String getName() {
return LogUtilsClassTAGBean.class.getName();
}
@Override
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
super.writeThisToJsonWriter(jsonWriter);
LogUtilsClassTAGBean bean = this;
jsonWriter.name("tag").value(bean.getTag());
jsonWriter.name("enable").value(bean.getEnable());
}
@Override
public boolean initObjectsFromJsonReader(JsonReader jsonReader, String name) throws IOException {
if (super.initObjectsFromJsonReader(jsonReader, name)) { return true; } else {
if (name.equals("tag")) {
setTag(jsonReader.nextString());
} else if (name.equals("enable")) {
setEnable(jsonReader.nextBoolean());
} else {
return false;
}
}
return true;
}
@Override
public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
if (!initObjectsFromJsonReader(jsonReader, name)) {
jsonReader.skipValue();
}
}
// 结束 JSON 对象
jsonReader.endObject();
return this;
}
}

View File

@@ -0,0 +1,22 @@
package cc.winboll.studio.libappbase;
/**
* @Author ZhanGSKen@AliYun.Com
* @Date 2025/02/12 11:12:25
* @Describe 简单信号服务中心
*/
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class SimpleOperateSignalCenterService extends Service {
public static final String TAG = "SimpleOperateSignalCenterService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

View File

@@ -0,0 +1,49 @@
package cc.winboll.studio.libappbase;
/**
* @Author ZhanGSKen@QQ.COM
* @Date 2024/07/19 14:30:57
* @Describe UTF-8编码文件工具类
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
public class UTF8FileUtils {
public static final String TAG = "FileUtils";
//
// 把字符串写入文件,指定 UTF-8 编码
//
public static void writeStringToFile(String szFilePath, String szContent) throws IOException {
File file = new File(szFilePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileOutputStream outputStream = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
writer.write(szContent);
writer.close();
}
//
// 读取文件到字符串,指定 UTF-8 编码
//
public static String readStringFromFile(String szFilePath) throws IOException {
File file = new File(szFilePath);
FileInputStream inputStream = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
StringBuilder content = new StringBuilder();
int character;
while ((character = reader.read()) != -1) {
content.append((char) character);
}
reader.close();
return content.toString();
}
}

View File

@@ -0,0 +1,15 @@
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<cc.winboll.studio.libappbase.GlobalCrashReportView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activityglobalcrashGlobalCrashReportView1"/>
</LinearLayout>

View File

@@ -0,0 +1,35 @@
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewglobalcrashreportLinearLayout1">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/viewglobalcrashreportToolbar1"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:id="@+id/viewglobalcrashreportTextView1"/>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>

View File

@@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="@android:style/Theme.Material.Light">
</style>
</resources>

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="themeGlobalCrashActivity" format="reference"/>
<declare-styleable name="GlobalCrashActivity">
<attr name="colorTittle" format="color" />
<attr name="colorTittleBackgound" format="color" />
<attr name="colorText" format="color" />
<attr name="colorTextBackgound" format="color" />
</declare-styleable>
</resources>

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#FFFFE200</color>
<color name="colorPrimaryDark">#FFFFE200</color>
<color name="colorAccent">#FFFFE200</color>
<color name="colorText">#FFFFE200</color>
</resources>

View File

@@ -1,5 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
<style name="APPBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="themeGlobalCrashActivity">@style/GlobalCrashActivityTheme</item>
</style>
<style name="GlobalCrashActivityTheme" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar">
<item name="colorTittle">#FFFFF600</item>
<item name="colorTittleBackgound">#FF00B322</item>
<item name="colorText">#FF00B322</item>
<item name="colorTextBackgound">#FF000000</item>
</style>
</resources>
</resources>

View File

@@ -1,10 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- WinBoll 默认方案 -->
<color name="colorPrimary">#FF196ABC</color>
<color name="colorPrimaryDark">#FF002B57</color>
<color name="colorAccent">#FF80BFFF</color>
<color name="colorToastFrame">#FFA9A9A9</color>
<color name="colorToastShadow">#FF000000</color>
<color name="colorToastBackgroung">#FFFFFFFF</color>
</resources>