CrashHandler.java 源代码
package base;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.os.Looper;
import android.util.Log;
import com.bytedance.sdk.openadsdk.downloadnew.core.TTDownloadField;
import com.hichip.base.HiLog;
import com.huawei.hms.framework.common.ContainerUtils;
import com.xiaomi.mipush.sdk.Constants;
import common.HiDataValue;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import utils.TimeUtil;
public class CrashHandler implements Thread.UncaughtExceptionHandler {
public static final String TAG = "CrashHandler";
private static CrashHandler instance;
private Context mContext;
private Thread.UncaughtExceptionHandler mDefaultHandler;
private Map<String, String> infos = new HashMap();
private DateFormat formatter = new SimpleDateFormat(TimeUtil.FORMAT_DATE_TIME_SPLIT);
private void sendCrashLog2PM(String str) {
}
private CrashHandler() {
}
public static synchronized CrashHandler getInstance() {
CrashHandler crashHandler;
synchronized (CrashHandler.class) {
if (instance == null) {
instance = new CrashHandler();
}
crashHandler = instance;
}
return crashHandler;
}
public void init(Context context) {
this.mContext = context;
this.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
}
@Override
public void uncaughtException(Thread thread, Throwable th) {
Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
if (!handleException(th) && (uncaughtExceptionHandler = this.mDefaultHandler) != null) {
uncaughtExceptionHandler.uncaughtException(thread, th);
return;
}
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
Log.e(TAG, "error : ", e);
}
System.exit(10);
}
private boolean handleException(Throwable th) {
if (th == null) {
return false;
}
collectDeviceInfo(this.mContext);
new Thread() {
@Override
public void run() {
Looper.prepare();
Looper.loop();
}
}.start();
saveCatchInfo2File(th);
return true;
}
public void collectDeviceInfo(Context context) {
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 1);
if (packageInfo != null) {
String str = packageInfo.versionName == null ? "null" : packageInfo.versionName;
String str2 = packageInfo.versionCode + "";
this.infos.put(TTDownloadField.TT_VERSION_NAME, str);
this.infos.put(TTDownloadField.TT_VERSION_CODE, str2);
}
this.infos.put("memoryinfo", "可用内存:" + MemoryInfo.getAvailMemory(context) + ":::" + MemoryInfo.getTotalMemory(context));
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "an error occured when collect package info", e);
}
for (Field field : Build.class.getDeclaredFields()) {
try {
field.setAccessible(true);
this.infos.put(field.getName(), field.get(null).toString());
Log.d(TAG, field.getName() + " : " + field.get(null));
} catch (Exception e2) {
Log.e(TAG, "an error occured when collect crash info", e2);
}
}
}
private String getFilePath() {
String str;
boolean equals = "mounted".equals(Environment.getExternalStorageState());
boolean exists = Environment.getExternalStorageDirectory().exists();
if (equals && exists) {
str = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Snapshot/crashlog/";
} else {
str = MyApplication.getInstance().getFilesDir().getAbsolutePath() + "/Snapshot/crashlog/";
}
HiLog.e("getFilePath():" + str);
return str;
}
private String saveCatchInfo2File(Throwable th) {
StringBuffer stringBuffer = new StringBuffer();
for (Map.Entry<String, String> entry : this.infos.entrySet()) {
stringBuffer.append(entry.getKey() + ContainerUtils.KEY_VALUE_DELIMITER + entry.getValue() + "\n");
}
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
th.printStackTrace(printWriter);
for (Throwable cause = th.getCause(); cause != null; cause = cause.getCause()) {
cause.printStackTrace(printWriter);
}
printWriter.close();
stringBuffer.append(stringWriter.toString());
try {
long currentTimeMillis = System.currentTimeMillis();
String str = "crash-" + this.formatter.format(new Date()) + Constants.ACCEPT_TIME_SEPARATOR_SERVER + currentTimeMillis + ".log";
String crashLogPath = HiDataValue.getCrashLogPath(this.mContext);
File file = new File(crashLogPath);
if (!file.exists()) {
file.mkdirs();
}
File file2 = new File(crashLogPath + str);
if (!file2.exists()) {
file2.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(file2);
fileOutputStream.write(stringBuffer.toString().getBytes());
sendCrashLog2PM(crashLogPath + str);
fileOutputStream.close();
return str;
} catch (Exception e) {
Log.e(TAG, "an error occured while writing file...", e);
return null;
}
}
}