OLD | NEW |
(Empty) | |
| 1 package org.adblockplus.android; |
| 2 |
| 3 import java.io.PrintWriter; |
| 4 import java.lang.Thread.UncaughtExceptionHandler; |
| 5 |
| 6 import android.app.NotificationManager; |
| 7 import android.content.Context; |
| 8 import android.content.pm.PackageInfo; |
| 9 import android.content.pm.PackageManager.NameNotFoundException; |
| 10 import android.os.Build; |
| 11 import android.util.Log; |
| 12 |
| 13 /** |
| 14 * Writes crash data in file. |
| 15 */ |
| 16 public class CrashHandler implements UncaughtExceptionHandler |
| 17 { |
| 18 public static final String REPORT_FILE = "AdblockPlus_Crash_Report.txt"; |
| 19 private UncaughtExceptionHandler defaultUEH; |
| 20 private NotificationManager notificationManager; |
| 21 private Context mContext; |
| 22 |
| 23 public CrashHandler(Context context) |
| 24 { |
| 25 defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); |
| 26 mContext = context; |
| 27 notificationManager = (NotificationManager) context.getSystemService(Context
.NOTIFICATION_SERVICE); |
| 28 } |
| 29 |
| 30 public UncaughtExceptionHandler getDefault() |
| 31 { |
| 32 return defaultUEH; |
| 33 } |
| 34 |
| 35 @Override |
| 36 public void uncaughtException(Thread t, Throwable e) |
| 37 { |
| 38 writeToFile(e, REPORT_FILE); |
| 39 if (notificationManager != null) |
| 40 { |
| 41 try |
| 42 { |
| 43 notificationManager.cancel(ProxyService.ONGOING_NOTIFICATION_ID); |
| 44 } |
| 45 catch (Throwable ex) |
| 46 { |
| 47 ex.printStackTrace(); |
| 48 } |
| 49 } |
| 50 notificationManager = null; |
| 51 |
| 52 defaultUEH.uncaughtException(t, e); |
| 53 } |
| 54 |
| 55 private void writeToFile(Throwable error, String filename) |
| 56 { |
| 57 Log.e("DCR", "Writing crash report"); |
| 58 int versionCode = -1; |
| 59 try |
| 60 { |
| 61 PackageInfo pi = mContext.getPackageManager().getPackageInfo(mContext.getP
ackageName(), 0); |
| 62 versionCode = pi.versionCode; |
| 63 } |
| 64 catch (NameNotFoundException ex) |
| 65 { |
| 66 } |
| 67 try |
| 68 { |
| 69 PrintWriter pw = new PrintWriter(mContext.openFileOutput(filename, Context
.MODE_WORLD_READABLE)); |
| 70 // Write Android version |
| 71 pw.println(Build.VERSION.SDK_INT); |
| 72 // Write application build number |
| 73 pw.println(versionCode); |
| 74 |
| 75 // Write exception data |
| 76 printThrowable(error, pw); |
| 77 Throwable cause = error.getCause(); |
| 78 // Write cause data |
| 79 if (cause != null) |
| 80 { |
| 81 pw.println("cause"); |
| 82 printThrowable(cause, pw); |
| 83 } |
| 84 pw.flush(); |
| 85 pw.close(); |
| 86 } |
| 87 catch (Throwable e) |
| 88 { |
| 89 e.printStackTrace(); |
| 90 } |
| 91 } |
| 92 |
| 93 private void printThrowable(Throwable error, PrintWriter pw) |
| 94 { |
| 95 // Use simplest format for speed - we do not have much time |
| 96 pw.println(error.getClass().getName()); |
| 97 pw.println(error.getMessage()); |
| 98 StackTraceElement[] trace = error.getStackTrace(); |
| 99 for (StackTraceElement element : trace) |
| 100 { |
| 101 pw.print(element.getClassName()); |
| 102 pw.print("|"); |
| 103 pw.print(element.getMethodName()); |
| 104 pw.print("|"); |
| 105 pw.print(element.isNativeMethod()); |
| 106 pw.print("|"); |
| 107 pw.print(element.getFileName()); |
| 108 pw.print("|"); |
| 109 pw.print(element.getLineNumber()); |
| 110 pw.println(); |
| 111 } |
| 112 } |
| 113 } |
OLD | NEW |