| LEFT | RIGHT |
| 1 package org.adblockplus.android; | 1 package org.adblockplus.android; |
| 2 | 2 |
| 3 import java.io.PrintWriter; | 3 import java.io.PrintWriter; |
| 4 import java.lang.Thread.UncaughtExceptionHandler; | 4 import java.lang.Thread.UncaughtExceptionHandler; |
| 5 | 5 |
| 6 import android.annotation.SuppressLint; |
| 6 import android.app.NotificationManager; | 7 import android.app.NotificationManager; |
| 7 import android.content.Context; | 8 import android.content.Context; |
| 8 import android.content.pm.PackageInfo; | 9 import android.content.pm.PackageInfo; |
| 9 import android.content.pm.PackageManager.NameNotFoundException; | 10 import android.content.pm.PackageManager.NameNotFoundException; |
| 10 import android.os.Build; | 11 import android.os.Build; |
| 11 import android.util.Log; | 12 import android.util.Log; |
| 12 | 13 |
| 13 /** | 14 /** |
| 14 * Writes crash data in file. | 15 * Writes crash data in file. |
| 15 */ | 16 */ |
| 16 public class CrashHandler implements UncaughtExceptionHandler | 17 public class CrashHandler implements UncaughtExceptionHandler |
| 17 { | 18 { |
| 18 public static final String REPORT_FILE = "AdblockPlus_Crash_Report.txt"; | 19 public static final String REPORT_FILE = "AdblockPlus_Crash_Report.txt"; |
| 19 private UncaughtExceptionHandler defaultUEH; | 20 private UncaughtExceptionHandler defaultUEH; |
| 20 private NotificationManager notificationManager; | 21 private NotificationManager notificationManager; |
| 21 private Context mContext; | 22 private Context context; |
| 23 |
| 24 private boolean generateReport; |
| 25 private boolean restoreProxy; |
| 26 private String host; |
| 27 private String port; |
| 28 private String excl; |
| 22 | 29 |
| 23 public CrashHandler(Context context) | 30 public CrashHandler(Context context) |
| 24 { | 31 { |
| 25 defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); | 32 defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); |
| 26 mContext = context; | 33 this.context = context; |
| 27 notificationManager = (NotificationManager) context.getSystemService(Context
.NOTIFICATION_SERVICE); | 34 notificationManager = (NotificationManager) context.getSystemService(Context
.NOTIFICATION_SERVICE); |
| 35 generateReport = false; |
| 36 restoreProxy = false; |
| 28 } | 37 } |
| 29 | 38 |
| 30 public UncaughtExceptionHandler getDefault() | 39 public UncaughtExceptionHandler getDefault() |
| 31 { | 40 { |
| 32 return defaultUEH; | 41 return defaultUEH; |
| 33 } | 42 } |
| 34 | 43 |
| 35 @Override | 44 @Override |
| 36 public void uncaughtException(Thread t, Throwable e) | 45 public void uncaughtException(Thread t, Throwable e) |
| 37 { | 46 { |
| 38 writeToFile(e, REPORT_FILE); | 47 if (generateReport) |
| 48 writeToFile(e, REPORT_FILE); |
| 49 |
| 50 if (restoreProxy) |
| 51 clearProxySettings(); |
| 52 |
| 39 if (notificationManager != null) | 53 if (notificationManager != null) |
| 40 { | 54 { |
| 41 try | 55 try |
| 42 { | 56 { |
| 43 notificationManager.cancel(ProxyService.ONGOING_NOTIFICATION_ID); | 57 notificationManager.cancel(ProxyService.ONGOING_NOTIFICATION_ID); |
| 44 } | 58 } |
| 45 catch (Throwable ex) | 59 catch (Throwable ex) |
| 46 { | 60 { |
| 47 ex.printStackTrace(); | 61 ex.printStackTrace(); |
| 48 } | 62 } |
| 49 } | 63 } |
| 50 notificationManager = null; | 64 notificationManager = null; |
| 51 | 65 |
| 52 defaultUEH.uncaughtException(t, e); | 66 defaultUEH.uncaughtException(t, e); |
| 53 } | 67 } |
| 54 | 68 |
| 69 public void generateReport(boolean report) |
| 70 { |
| 71 generateReport = report; |
| 72 } |
| 73 |
| 74 @SuppressLint("WorldReadableFiles") |
| 55 private void writeToFile(Throwable error, String filename) | 75 private void writeToFile(Throwable error, String filename) |
| 56 { | 76 { |
| 57 Log.e("DCR", "Writing crash report"); | 77 Log.e("DCR", "Writing crash report"); |
| 58 int versionCode = -1; | 78 int versionCode = -1; |
| 59 try | 79 try |
| 60 { | 80 { |
| 61 PackageInfo pi = mContext.getPackageManager().getPackageInfo(mContext.getP
ackageName(), 0); | 81 PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPac
kageName(), 0); |
| 62 versionCode = pi.versionCode; | 82 versionCode = pi.versionCode; |
| 63 } | 83 } |
| 64 catch (NameNotFoundException ex) | 84 catch (NameNotFoundException ex) |
| 65 { | 85 { |
| 66 } | 86 } |
| 67 try | 87 try |
| 68 { | 88 { |
| 69 PrintWriter pw = new PrintWriter(mContext.openFileOutput(filename, Context
.MODE_WORLD_READABLE)); | 89 PrintWriter pw = new PrintWriter(context.openFileOutput(filename, Context.
MODE_WORLD_READABLE)); |
| 70 // Write Android version | 90 // Write Android version |
| 71 pw.println(Build.VERSION.SDK_INT); | 91 pw.println(Build.VERSION.SDK_INT); |
| 72 // Write application build number | 92 // Write application build number |
| 73 pw.println(versionCode); | 93 pw.println(versionCode); |
| 74 | 94 |
| 75 // Write exception data | 95 // Write exception data |
| 76 printThrowable(error, pw); | 96 printThrowable(error, pw); |
| 77 Throwable cause = error.getCause(); | 97 Throwable cause = error.getCause(); |
| 78 // Write cause data | 98 // Write cause data |
| 79 if (cause != null) | 99 if (cause != null) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 103 pw.print(element.getMethodName()); | 123 pw.print(element.getMethodName()); |
| 104 pw.print("|"); | 124 pw.print("|"); |
| 105 pw.print(element.isNativeMethod()); | 125 pw.print(element.isNativeMethod()); |
| 106 pw.print("|"); | 126 pw.print("|"); |
| 107 pw.print(element.getFileName()); | 127 pw.print(element.getFileName()); |
| 108 pw.print("|"); | 128 pw.print("|"); |
| 109 pw.print(element.getLineNumber()); | 129 pw.print(element.getLineNumber()); |
| 110 pw.println(); | 130 pw.println(); |
| 111 } | 131 } |
| 112 } | 132 } |
| 133 |
| 134 public void saveProxySettings(String host, String port, String excl) |
| 135 { |
| 136 Log.e("DCR", "Saving proxy " + host + ":" + port + "/" + excl); |
| 137 this.host = host; |
| 138 this.port = port; |
| 139 this.excl = excl; |
| 140 restoreProxy = true; |
| 141 } |
| 142 |
| 143 public void clearProxySettings() |
| 144 { |
| 145 Log.e("DCR", "Clearing proxy"); |
| 146 restoreProxy = false; |
| 147 int p = 0; |
| 148 try |
| 149 { |
| 150 p = Integer.valueOf(port); |
| 151 } |
| 152 catch (NumberFormatException e) |
| 153 { |
| 154 // ignore - no valid port, it will be correctly processed later |
| 155 } |
| 156 ProxySettings.setConnectionProxy(context, host, p, excl); |
| 157 } |
| 113 } | 158 } |
| LEFT | RIGHT |