OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 16 matching lines...) Expand all Loading... |
27 import android.content.pm.PackageManager.NameNotFoundException; | 27 import android.content.pm.PackageManager.NameNotFoundException; |
28 import android.os.Build; | 28 import android.os.Build; |
29 import android.util.Log; | 29 import android.util.Log; |
30 | 30 |
31 /** | 31 /** |
32 * Writes crash data in file. | 32 * Writes crash data in file. |
33 */ | 33 */ |
34 public class CrashHandler implements UncaughtExceptionHandler | 34 public class CrashHandler implements UncaughtExceptionHandler |
35 { | 35 { |
36 public static final String REPORT_FILE = "AdblockPlus_Crash_Report.txt"; | 36 public static final String REPORT_FILE = "AdblockPlus_Crash_Report.txt"; |
37 private UncaughtExceptionHandler defaultUEH; | 37 private final UncaughtExceptionHandler defaultUEH; |
38 private NotificationManager notificationManager; | 38 private NotificationManager notificationManager; |
39 private Context context; | 39 private final Context context; |
40 | 40 |
41 private boolean generateReport; | 41 private boolean generateReport; |
42 private boolean restoreProxy; | 42 private boolean restoreProxy; |
43 private String host; | 43 private String host; |
44 private String port; | 44 private String port; |
45 private String excl; | 45 private String excl; |
46 | 46 |
47 public CrashHandler(Context context) | 47 public CrashHandler(final Context context) |
48 { | 48 { |
49 defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); | 49 defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); |
50 this.context = context; | 50 this.context = context; |
51 notificationManager = (NotificationManager) context.getSystemService(Context
.NOTIFICATION_SERVICE); | 51 notificationManager = (NotificationManager) context.getSystemService(Context
.NOTIFICATION_SERVICE); |
52 generateReport = false; | 52 generateReport = false; |
53 restoreProxy = false; | 53 restoreProxy = false; |
54 } | 54 } |
55 | 55 |
56 public UncaughtExceptionHandler getDefault() | 56 public UncaughtExceptionHandler getDefault() |
57 { | 57 { |
58 return defaultUEH; | 58 return defaultUEH; |
59 } | 59 } |
60 | 60 |
61 @Override | 61 @Override |
62 public void uncaughtException(Thread t, Throwable e) | 62 public void uncaughtException(final Thread t, final Throwable e) |
63 { | 63 { |
64 if (generateReport) | 64 if (generateReport) |
65 writeToFile(e, REPORT_FILE); | 65 writeToFile(e, REPORT_FILE); |
66 | 66 |
67 if (restoreProxy) | 67 if (restoreProxy) |
68 clearProxySettings(); | 68 clearProxySettings(); |
69 | 69 |
70 if (notificationManager != null) | 70 if (notificationManager != null) |
71 { | 71 { |
72 try | 72 try |
73 { | 73 { |
74 notificationManager.cancel(ProxyService.ONGOING_NOTIFICATION_ID); | 74 notificationManager.cancel(ProxyService.ONGOING_NOTIFICATION_ID); |
75 } | 75 } |
76 catch (Throwable ex) | 76 catch (final Throwable ex) |
77 { | 77 { |
78 ex.printStackTrace(); | 78 ex.printStackTrace(); |
79 } | 79 } |
80 } | 80 } |
81 notificationManager = null; | 81 notificationManager = null; |
82 | 82 |
83 defaultUEH.uncaughtException(t, e); | 83 defaultUEH.uncaughtException(t, e); |
84 } | 84 } |
85 | 85 |
86 public void generateReport(boolean report) | 86 public void generateReport(final boolean report) |
87 { | 87 { |
88 generateReport = report; | 88 generateReport = report; |
89 } | 89 } |
90 | 90 |
91 @SuppressLint("WorldReadableFiles") | 91 @SuppressLint("WorldReadableFiles") |
92 private void writeToFile(Throwable error, String filename) | 92 private void writeToFile(final Throwable error, final String filename) |
93 { | 93 { |
94 Log.e("DCR", "Writing crash report"); | 94 Log.e("DCR", "Writing crash report"); |
95 int versionCode = -1; | 95 int versionCode = -1; |
96 try | 96 try |
97 { | 97 { |
98 PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPac
kageName(), 0); | 98 final PackageInfo pi = context.getPackageManager().getPackageInfo(context.
getPackageName(), 0); |
99 versionCode = pi.versionCode; | 99 versionCode = pi.versionCode; |
100 } | 100 } |
101 catch (NameNotFoundException ex) | 101 catch (final NameNotFoundException ex) |
102 { | 102 { |
103 } | 103 } |
104 try | 104 try |
105 { | 105 { |
106 PrintWriter pw = new PrintWriter(context.openFileOutput(filename, Context.
MODE_WORLD_READABLE)); | 106 final PrintWriter pw = new PrintWriter(context.openFileOutput(filename, Co
ntext.MODE_WORLD_READABLE)); |
107 // Write Android version | 107 // Write Android version |
108 pw.println(Build.VERSION.SDK_INT); | 108 pw.println(Build.VERSION.SDK_INT); |
109 // Write application build number | 109 // Write application build number |
110 pw.println(versionCode); | 110 pw.println(versionCode); |
111 | 111 |
112 // Write exception data | 112 // Write exception data |
113 printThrowable(error, pw); | 113 printThrowable(error, pw); |
114 Throwable cause = error.getCause(); | 114 final Throwable cause = error.getCause(); |
115 // Write cause data | 115 // Write cause data |
116 if (cause != null) | 116 if (cause != null) |
117 { | 117 { |
118 pw.println("cause"); | 118 pw.println("cause"); |
119 printThrowable(cause, pw); | 119 printThrowable(cause, pw); |
120 } | 120 } |
121 pw.flush(); | 121 pw.flush(); |
122 pw.close(); | 122 pw.close(); |
123 } | 123 } |
124 catch (Throwable e) | 124 catch (final Throwable e) |
125 { | 125 { |
126 e.printStackTrace(); | 126 e.printStackTrace(); |
127 } | 127 } |
128 } | 128 } |
129 | 129 |
130 private void printThrowable(Throwable error, PrintWriter pw) | 130 private void printThrowable(final Throwable error, final PrintWriter pw) |
131 { | 131 { |
132 // Use simplest format for speed - we do not have much time | 132 // Use simplest format for speed - we do not have much time |
133 pw.println(error.getClass().getName()); | 133 pw.println(error.getClass().getName()); |
134 pw.println(error.getMessage()); | 134 pw.println(error.getMessage()); |
135 StackTraceElement[] trace = error.getStackTrace(); | 135 final StackTraceElement[] trace = error.getStackTrace(); |
136 for (StackTraceElement element : trace) | 136 for (final StackTraceElement element : trace) |
137 { | 137 { |
138 pw.print(element.getClassName()); | 138 pw.print(element.getClassName()); |
139 pw.print("|"); | 139 pw.print("|"); |
140 pw.print(element.getMethodName()); | 140 pw.print(element.getMethodName()); |
141 pw.print("|"); | 141 pw.print("|"); |
142 pw.print(element.isNativeMethod()); | 142 pw.print(element.isNativeMethod()); |
143 pw.print("|"); | 143 pw.print("|"); |
144 pw.print(element.getFileName()); | 144 pw.print(element.getFileName()); |
145 pw.print("|"); | 145 pw.print("|"); |
146 pw.print(element.getLineNumber()); | 146 pw.print(element.getLineNumber()); |
147 pw.println(); | 147 pw.println(); |
148 } | 148 } |
149 } | 149 } |
150 | 150 |
151 public void saveProxySettings(String host, String port, String excl) | 151 public void saveProxySettings(final String host, final String port, final Stri
ng excl) |
152 { | 152 { |
153 Log.e("DCR", "Saving proxy " + host + ":" + port + "/" + excl); | 153 Log.e("DCR", "Saving proxy " + host + ":" + port + "/" + excl); |
154 this.host = host; | 154 this.host = host; |
155 this.port = port; | 155 this.port = port; |
156 this.excl = excl; | 156 this.excl = excl; |
157 restoreProxy = true; | 157 restoreProxy = true; |
158 } | 158 } |
159 | 159 |
160 public void clearProxySettings() | 160 public void clearProxySettings() |
161 { | 161 { |
162 Log.e("DCR", "Clearing proxy"); | 162 Log.e("DCR", "Clearing proxy"); |
163 restoreProxy = false; | 163 restoreProxy = false; |
164 int p = 0; | 164 int p = 0; |
165 try | 165 try |
166 { | 166 { |
167 p = Integer.valueOf(port); | 167 p = Integer.valueOf(port); |
168 } | 168 } |
169 catch (NumberFormatException e) | 169 catch (final NumberFormatException e) |
170 { | 170 { |
171 // ignore - no valid port, it will be correctly processed later | 171 // ignore - no valid port, it will be correctly processed later |
172 } | 172 } |
173 ProxySettings.setConnectionProxy(context, host, p, excl); | 173 ProxySettings.setConnectionProxy(context, host, p, excl); |
174 } | 174 } |
175 } | 175 } |
OLD | NEW |