|
@@ -0,0 +1,242 @@
|
|
|
|
+package com.wdkl.ncs.android.component.home.util;
|
|
|
|
+
|
|
|
|
+import android.app.AlarmManager;
|
|
|
|
+import android.app.Application;
|
|
|
|
+import android.app.PendingIntent;
|
|
|
|
+import android.content.Context;
|
|
|
|
+import android.content.Intent;
|
|
|
|
+import android.os.Environment;
|
|
|
|
+import android.util.Log;
|
|
|
|
+
|
|
|
|
+import com.github.anrwatchdog.ANRError;
|
|
|
|
+import com.github.anrwatchdog.ANRWatchDog;
|
|
|
|
+import com.wdkl.ncs.android.component.home.activity.HomeActivity;
|
|
|
|
+import com.wdkl.ncs.android.component.nursehome.common.Constants;
|
|
|
|
+import com.wdkl.ncs.android.middleware.api.UrlManager;
|
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.channel.DeviceChannel;
|
|
|
|
+import com.wdkl.skywebrtc.CallSession;
|
|
|
|
+import com.wdkl.skywebrtc.EnumType;
|
|
|
|
+import com.wdkl.skywebrtc.SkyEngineKit;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.PrintWriter;
|
|
|
|
+import java.io.StringWriter;
|
|
|
|
+import java.io.Writer;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+import okhttp3.Call;
|
|
|
|
+import okhttp3.Callback;
|
|
|
|
+import okhttp3.FormBody;
|
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
|
+import okhttp3.Request;
|
|
|
|
+import okhttp3.Response;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Created by dengzhe on 2018/4/2.
|
|
|
|
+ * //=========================FC&ANR异常处理类=========================//
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+public class AnrFcExceptionUtil implements Thread.UncaughtExceptionHandler {
|
|
|
|
+
|
|
|
|
+ private static ANRWatchDog mANRWatchDog;
|
|
|
|
+ private Thread.UncaughtExceptionHandler mDefaultHandler;
|
|
|
|
+ public static final String TAG = "AnrFcExceptionUtil";
|
|
|
|
+ private static Application application;
|
|
|
|
+
|
|
|
|
+ private static AnrFcExceptionUtil mAnrFcExceptionUtil;
|
|
|
|
+
|
|
|
|
+ private OkHttpClient okHttpClient;
|
|
|
|
+ private UrlManager urlManager = UrlManager.Companion.build();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 存储异常和参数信息
|
|
|
|
+ */
|
|
|
|
+ private Map<String, String> paramsMap = new HashMap<>();
|
|
|
|
+ /**
|
|
|
|
+ * 格式化时间
|
|
|
|
+ */
|
|
|
|
+ private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static AnrFcExceptionUtil getInstance(Application application) {
|
|
|
|
+ if (mAnrFcExceptionUtil == null) {
|
|
|
|
+ mAnrFcExceptionUtil = new AnrFcExceptionUtil(application);
|
|
|
|
+ }
|
|
|
|
+ return mAnrFcExceptionUtil;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private AnrFcExceptionUtil(Application application) {
|
|
|
|
+ //获取系统默认的UncaughtException处理器
|
|
|
|
+ mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
|
|
|
|
+ this.application = application;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void uncaughtException(Thread thread, Throwable ex) {
|
|
|
|
+ if (!handleException(ex) && mDefaultHandler != null) {
|
|
|
|
+ //如果用户没有处理则让系统默认的异常处理器来处理
|
|
|
|
+ mDefaultHandler.uncaughtException(thread, ex);
|
|
|
|
+ } else {
|
|
|
|
+ try {
|
|
|
|
+ Thread.sleep(3000);
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
+ Log.e(TAG, "error : ", e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ restartApp();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void restartApp() {
|
|
|
|
+ DeviceChannel.calling = false;
|
|
|
|
+ CallSession session= SkyEngineKit.Instance().getCurrentSession();
|
|
|
|
+ if(session!=null&&session.getState()!= EnumType.CallState.Idle){
|
|
|
|
+ SkyEngineKit.Instance().endCall();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //重新启动app
|
|
|
|
+ Intent mStartActivity = new Intent(application.getApplicationContext(), HomeActivity.class);
|
|
|
|
+ int mPendingIntentId = 123456;
|
|
|
|
+ PendingIntent mPendingIntent = PendingIntent.getActivity(application.getApplicationContext(), mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
|
|
|
|
+ AlarmManager mgr = (AlarmManager) application.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
|
|
|
|
+ mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1500, mPendingIntent);
|
|
|
|
+
|
|
|
|
+ android.os.Process.killProcess(android.os.Process.myPid());
|
|
|
|
+ System.exit(0);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
|
|
|
|
+ *
|
|
|
|
+ * @param ex
|
|
|
|
+ * @return true:如果处理了该异常信息;否则返回false.
|
|
|
|
+ */
|
|
|
|
+ private boolean handleException(Throwable ex) {
|
|
|
|
+ if (ex == null) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ saveCrashInfo2File(ex);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void uploadingErrorLog(String class_name, String err_msg, String exception_name, String method_name, String stack_trace) {
|
|
|
|
+ if(okHttpClient == null){
|
|
|
|
+ okHttpClient = new OkHttpClient();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ FormBody.Builder formBody = new FormBody.Builder();
|
|
|
|
+ formBody.add("class_name",class_name);
|
|
|
|
+ formBody.add("method_name",method_name);
|
|
|
|
+ formBody.add("exception_name",exception_name);
|
|
|
|
+ formBody.add("err_msg",err_msg);
|
|
|
|
+ formBody.add("stack_trace",stack_trace);
|
|
|
|
+
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
+ .url(urlManager.getDevice_url() + "device/error_log")
|
|
|
|
+ .post(formBody.build())
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ okHttpClient.newCall(request).enqueue(new Callback() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onFailure(Call call, IOException e) {
|
|
|
|
+ Log.e(TAG,"错误日志上传失败"+e.getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onResponse(Call call, Response response) throws IOException {
|
|
|
|
+ Log.d(TAG,"错误日志上传成功");
|
|
|
|
+ String data = response.body().string();
|
|
|
|
+ Log.d(TAG,"错误日志数据 data "+data);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 保存错误信息到文件中
|
|
|
|
+ *
|
|
|
|
+ * @param ex
|
|
|
|
+ * @return 返回文件名称
|
|
|
|
+ */
|
|
|
|
+ private String saveCrashInfo2File(Throwable ex) {
|
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
|
+ for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
|
|
|
|
+ String key = entry.getKey();
|
|
|
|
+ String value = entry.getValue();
|
|
|
|
+ sb.append(key + "=" + value + "\n");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Writer writer = new StringWriter();
|
|
|
|
+ PrintWriter printWriter = new PrintWriter(writer);
|
|
|
|
+ ex.printStackTrace(printWriter);
|
|
|
|
+ Throwable cause = ex.getCause();
|
|
|
|
+ while (cause != null) {
|
|
|
|
+ cause.printStackTrace(printWriter);
|
|
|
|
+ cause = cause.getCause();
|
|
|
|
+ }
|
|
|
|
+ printWriter.close();
|
|
|
|
+ String result = writer.toString();
|
|
|
|
+ sb.append(result);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ long timestamp = System.currentTimeMillis();
|
|
|
|
+ String time = format.format(new Date());
|
|
|
|
+ String fileName = "crash-" + time + "-" + timestamp + ".txt";
|
|
|
|
+ if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
|
|
|
+ String path = Environment.getExternalStorageDirectory() + "/crash/";
|
|
|
|
+ File dir = new File(path);
|
|
|
|
+ if (!dir.exists()) {
|
|
|
|
+ dir.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ FileOutputStream fos = new FileOutputStream(path + fileName);
|
|
|
|
+ fos.write(sb.toString().getBytes());
|
|
|
|
+ Log.i(TAG, "saveCrashInfo2File: "+sb.toString());
|
|
|
|
+ fos.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //上传错误日志
|
|
|
|
+ uploadingErrorLog(application.getPackageName(), "crash", "crash", "", sb.toString());
|
|
|
|
+
|
|
|
|
+ return fileName;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ Log.e(TAG, "an error occured while writing file...", e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * ===================================================崩溃异常处理===================================================
|
|
|
|
+ */
|
|
|
|
+ public void initFCException() {
|
|
|
|
+ //设置该CrashHandler为程序的默认处理器
|
|
|
|
+ AnrFcExceptionUtil catchExcep = AnrFcExceptionUtil.getInstance(application);
|
|
|
|
+ Thread.setDefaultUncaughtExceptionHandler(catchExcep);
|
|
|
|
+ mANRWatchDog = new ANRWatchDog(10000);
|
|
|
|
+ mANRWatchDog.setInterruptionListener(new ANRWatchDog.InterruptionListener() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onInterrupted(InterruptedException exception) {
|
|
|
|
+ }
|
|
|
|
+ }).setIgnoreDebugger(true).setANRListener(new ANRWatchDog.ANRListener() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onAppNotResponding(ANRError error) {
|
|
|
|
+ /*Intent mStartActivity = new Intent(application.getApplicationContext(), Constants.ANR_FC);
|
|
|
|
+ int mPendingIntentId = 123456;
|
|
|
|
+ PendingIntent mPendingIntent = PendingIntent.getActivity(application.getApplicationContext(), mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
|
|
|
|
+ AlarmManager mgr = (AlarmManager) application.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
|
|
|
|
+ mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1500, mPendingIntent);
|
|
|
|
+ android.os.Process.killProcess(android.os.Process.myPid());*/
|
|
|
|
+
|
|
|
|
+ Log.d("anr", "Anr restart app...");
|
|
|
|
+ restartApp();
|
|
|
|
+ }
|
|
|
|
+ }).start();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|