UncaughtExceptionHandlerImpl.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package com.emato.ich.crash;
  2. import android.annotation.SuppressLint;
  3. import android.app.AlarmManager;
  4. import android.app.PendingIntent;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.pm.PackageInfo;
  8. import android.content.pm.PackageManager;
  9. import android.os.Build;
  10. import android.os.Environment;
  11. import android.os.Looper;
  12. import com.emato.ich.utils.Log;
  13. import android.widget.Toast;
  14. import com.emato.ich.MainActivity;
  15. import com.emato.ich.message.ICHPublishClient;
  16. import com.emato.ich.message.ICHTopic;
  17. import com.emato.ich.utils.BaseUtils;
  18. import com.emato.ich.utils.LoggingUtils;
  19. import org.eclipse.paho.client.mqttv3.MqttMessage;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.PrintWriter;
  24. import java.io.StringWriter;
  25. import java.io.Writer;
  26. import java.lang.reflect.Field;
  27. import java.text.DateFormat;
  28. import java.text.SimpleDateFormat;
  29. import java.util.Date;
  30. import java.util.HashMap;
  31. import java.util.Iterator;
  32. import java.util.Map;
  33. public class UncaughtExceptionHandlerImpl implements Thread.UncaughtExceptionHandler {
  34. private static final String TAG = UncaughtExceptionHandlerImpl.class.getName();
  35. private static UncaughtExceptionHandlerImpl INSTANCE;
  36. private Thread.UncaughtExceptionHandler mDefaultHandler;
  37. private Context mContext;
  38. private Map<String, String> infos = new HashMap();
  39. @SuppressLint({"SimpleDateFormat"})
  40. private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
  41. private boolean mIsDebug;
  42. private boolean mIsRestartApp;
  43. private long mRestartTime;
  44. private Class mRestartActivity;
  45. private String mTips;
  46. private UncaughtExceptionHandlerImpl() {
  47. }
  48. public static UncaughtExceptionHandlerImpl getInstance() {
  49. if (INSTANCE == null) {
  50. INSTANCE = new UncaughtExceptionHandlerImpl();
  51. }
  52. return INSTANCE;
  53. }
  54. public void init(Context context, boolean isDebug, boolean isRestartApp, long restartTime, Class restartActivity) {
  55. this.mIsRestartApp = isRestartApp;
  56. this.mRestartTime = restartTime;
  57. this.mRestartActivity = restartActivity;
  58. this.init(context, isDebug);
  59. }
  60. public void init(Context context, boolean isDebug) {
  61. this.mTips = "很抱歉,程序出现异常,即将退出...";
  62. this.mIsDebug = isDebug;
  63. this.mContext = context;
  64. this.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
  65. Thread.setDefaultUncaughtExceptionHandler(this);
  66. }
  67. @SuppressLint("WrongConstant")
  68. public void uncaughtException(Thread thread, Throwable ex) {
  69. LoggingUtils.sendErrorLog("系统异常: 崩溃异常! ", ex);
  70. if (!this.handleException(ex) && this.mDefaultHandler != null) {
  71. this.mDefaultHandler.uncaughtException(thread, ex);
  72. } else {
  73. try {
  74. Thread.sleep(1000L);
  75. } catch (InterruptedException var6) {
  76. Log.e(TAG, "error 出现未知异常: ", var6);
  77. LoggingUtils.sendErrorLog("系统异常: 线程中断异常! ", var6);
  78. }
  79. if (this.mIsRestartApp) {
  80. Intent intent = new Intent(this.mContext.getApplicationContext(), this.mRestartActivity);
  81. @SuppressLint("WrongConstant") AlarmManager mAlarmManager = (AlarmManager)this.mContext.getSystemService(Context.ALARM_SERVICE);
  82. @SuppressLint("WrongConstant") PendingIntent restartIntent = PendingIntent.getActivity(this.mContext.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  83. mAlarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + this.mRestartTime, restartIntent);
  84. }
  85. // android.os.Process.killProcess(android.os.Process.myPid());
  86. ((CrashApplication)this.mContext.getApplicationContext()).removeAllActivity();
  87. }
  88. }
  89. private boolean handleException(final Throwable ex) {
  90. if (ex == null) {
  91. return false;
  92. } else {
  93. (new Thread() {
  94. @SuppressLint("WrongConstant")
  95. public void run() {
  96. Looper.prepare();
  97. // Toast.makeText(UncaughtExceptionHandlerImpl.this.mContext, UncaughtExceptionHandlerImpl.this.getTips(ex), 1).show();
  98. Toast.makeText(UncaughtExceptionHandlerImpl.this.mContext, "很抱歉,程序出现异常,即将退出...", 1).show();
  99. Looper.loop();
  100. }
  101. }).start();
  102. return true;
  103. }
  104. }
  105. private String getTips(Throwable ex) {
  106. if (ex instanceof SecurityException) {
  107. if (ex.getMessage().contains("android.permission.CAMERA")) {
  108. this.mTips = "请授予应用相机权限,程序出现异常,即将退出.";
  109. } else if (ex.getMessage().contains("android.permission.RECORD_AUDIO")) {
  110. this.mTips = "请授予应用麦克风权限,程序出现异常,即将退出。";
  111. } else if (ex.getMessage().contains("android.permission.WRITE_EXTERNAL_STORAGE")) {
  112. this.mTips = "请授予应用存储权限,程序出现异常,即将退出。";
  113. } else if (ex.getMessage().contains("android.permission.READ_PHONE_STATE")) {
  114. this.mTips = "请授予应用电话权限,程序出现异常,即将退出。";
  115. } else if (!ex.getMessage().contains("android.permission.ACCESS_COARSE_LOCATION") && !ex.getMessage().contains("android.permission.ACCESS_FINE_LOCATION")) {
  116. this.mTips = "很抱歉,程序出现异常,即将退出,请检查应用权限设置。";
  117. } else {
  118. this.mTips = "请授予应用位置信息权,很抱歉,程序出现异常,即将退出。";
  119. }
  120. }
  121. return this.mTips;
  122. }
  123. public void collectDeviceInfo(Context ctx) {
  124. try {
  125. PackageManager pm = ctx.getPackageManager();
  126. @SuppressLint("WrongConstant") PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), 1);
  127. if (pi != null) {
  128. String versionName = pi.versionName == null ? "null" : pi.versionName;
  129. String versionCode = pi.versionCode + "";
  130. this.infos.put("versionName", versionName);
  131. this.infos.put("versionCode", versionCode);
  132. }
  133. } catch (PackageManager.NameNotFoundException var9) {
  134. Log.e(TAG, "an error occured when collect package info", var9);
  135. LoggingUtils.sendErrorLog("业务异常: an error occured when collect package info! ", var9);
  136. }
  137. Field[] fields = Build.class.getDeclaredFields();
  138. Field[] var11 = fields;
  139. int var12 = fields.length;
  140. for(int var13 = 0; var13 < var12; ++var13) {
  141. Field field = var11[var13];
  142. try {
  143. field.setAccessible(true);
  144. this.infos.put(field.getName(), field.get((Object)null).toString());
  145. Log.i(TAG, field.getName() + " : " + field.get((Object)null));
  146. } catch (Exception var8) {
  147. Log.e(TAG, "an error occured when collect crash info", var8);
  148. LoggingUtils.sendErrorLog("业务异常: an error occured when collect crash info! ", var8);
  149. }
  150. }
  151. }
  152. private String saveCrashInfo2File(Throwable ex) {
  153. StringBuffer sb = new StringBuffer();
  154. Iterator var3 = this.infos.entrySet().iterator();
  155. String result;
  156. while(var3.hasNext()) {
  157. Map.Entry<String, String> entry = (Map.Entry)var3.next();
  158. String key = (String)entry.getKey();
  159. result = (String)entry.getValue();
  160. sb.append(key + "=" + result + "\n");
  161. }
  162. Writer writer = new StringWriter();
  163. PrintWriter printWriter = new PrintWriter(writer);
  164. ex.printStackTrace(printWriter);
  165. for(Throwable cause = ex.getCause(); cause != null; cause = cause.getCause()) {
  166. cause.printStackTrace(printWriter);
  167. }
  168. printWriter.close();
  169. result = writer.toString();
  170. sb.append(result);
  171. try {
  172. long timestamp = System.currentTimeMillis();
  173. String time = this.formatter.format(new Date());
  174. String fileName = "crash-" + time + "-" + timestamp + ".log";
  175. if (Environment.getExternalStorageState().equals("mounted")) {
  176. String path = "/sdcard/" + this.mContext.getPackageName() + "/crash/";
  177. File dir = new File(path);
  178. if (!dir.exists()) {
  179. dir.mkdirs();
  180. }
  181. FileOutputStream fos = new FileOutputStream(path + fileName);
  182. fos.write(sb.toString().getBytes());
  183. fos.close();
  184. }
  185. return fileName;
  186. } catch (Exception var14) {
  187. Log.e(TAG, "an error occured while writing file...", var14);
  188. LoggingUtils.sendErrorLog("业务异常: an error occured while writing file... ", var14);
  189. return null;
  190. }
  191. }
  192. }