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 |
| 38 private final UncaughtExceptionHandler defaultUEH; |
| 39 private final Context context; |
| 40 |
38 private NotificationManager notificationManager; | 41 private NotificationManager notificationManager; |
39 private Context context; | |
40 | |
41 private boolean generateReport; | 42 private boolean generateReport; |
42 private boolean restoreProxy; | 43 private boolean restoreProxy; |
43 private String host; | 44 private String host; |
44 private String port; | 45 private String port; |
45 private String excl; | 46 private String excl; |
46 | 47 |
47 public CrashHandler(Context context) | 48 public CrashHandler(final Context context) |
48 { | 49 { |
49 defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); | 50 defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); |
50 this.context = context; | 51 this.context = context; |
51 notificationManager = (NotificationManager) context.getSystemService(Context
.NOTIFICATION_SERVICE); | 52 notificationManager = (NotificationManager) context.getSystemService(Context
.NOTIFICATION_SERVICE); |
52 generateReport = false; | 53 generateReport = false; |
53 restoreProxy = false; | 54 restoreProxy = false; |
54 } | 55 } |
55 | 56 |
56 public UncaughtExceptionHandler getDefault() | 57 public UncaughtExceptionHandler getDefault() |
57 { | 58 { |
58 return defaultUEH; | 59 return defaultUEH; |
59 } | 60 } |
60 | 61 |
61 @Override | 62 @Override |
62 public void uncaughtException(Thread t, Throwable e) | 63 public void uncaughtException(final Thread t, final Throwable e) |
63 { | 64 { |
64 if (generateReport) | 65 if (generateReport) |
65 writeToFile(e, REPORT_FILE); | 66 writeToFile(e, REPORT_FILE); |
66 | 67 |
67 if (restoreProxy) | 68 if (restoreProxy) |
68 clearProxySettings(); | 69 clearProxySettings(); |
69 | 70 |
70 if (notificationManager != null) | 71 if (notificationManager != null) |
71 { | 72 { |
72 try | 73 try |
73 { | 74 { |
74 notificationManager.cancel(ProxyService.ONGOING_NOTIFICATION_ID); | 75 notificationManager.cancel(ProxyService.ONGOING_NOTIFICATION_ID); |
75 } | 76 } |
76 catch (Throwable ex) | 77 catch (final Throwable ex) |
77 { | 78 { |
78 ex.printStackTrace(); | 79 ex.printStackTrace(); |
79 } | 80 } |
80 } | 81 } |
81 notificationManager = null; | 82 notificationManager = null; |
82 | 83 |
83 defaultUEH.uncaughtException(t, e); | 84 defaultUEH.uncaughtException(t, e); |
84 } | 85 } |
85 | 86 |
86 public void generateReport(boolean report) | 87 public void generateReport(final boolean report) |
87 { | 88 { |
88 generateReport = report; | 89 generateReport = report; |
89 } | 90 } |
90 | 91 |
91 @SuppressLint("WorldReadableFiles") | 92 @SuppressLint("WorldReadableFiles") |
92 private void writeToFile(Throwable error, String filename) | 93 private void writeToFile(final Throwable error, final String filename) |
93 { | 94 { |
94 Log.e("DCR", "Writing crash report"); | 95 Log.e("DCR", "Writing crash report"); |
95 int versionCode = -1; | 96 int versionCode = -1; |
96 try | 97 try |
97 { | 98 { |
98 PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPac
kageName(), 0); | 99 final PackageInfo pi = context.getPackageManager().getPackageInfo(context.
getPackageName(), 0); |
99 versionCode = pi.versionCode; | 100 versionCode = pi.versionCode; |
100 } | 101 } |
101 catch (NameNotFoundException ex) | 102 catch (final NameNotFoundException ex) |
102 { | 103 { |
103 } | 104 } |
104 try | 105 try |
105 { | 106 { |
106 PrintWriter pw = new PrintWriter(context.openFileOutput(filename, Context.
MODE_WORLD_READABLE)); | 107 final PrintWriter pw = new PrintWriter(context.openFileOutput(filename, Co
ntext.MODE_WORLD_READABLE)); |
107 // Write Android version | 108 // Write Android version |
108 pw.println(Build.VERSION.SDK_INT); | 109 pw.println(Build.VERSION.SDK_INT); |
109 // Write application build number | 110 // Write application build number |
110 pw.println(versionCode); | 111 pw.println(versionCode); |
111 | 112 |
112 // Write exception data | 113 // Write exception data |
113 printThrowable(error, pw); | 114 printThrowable(error, pw); |
114 Throwable cause = error.getCause(); | 115 final Throwable cause = error.getCause(); |
115 // Write cause data | 116 // Write cause data |
116 if (cause != null) | 117 if (cause != null) |
117 { | 118 { |
118 pw.println("cause"); | 119 pw.println("cause"); |
119 printThrowable(cause, pw); | 120 printThrowable(cause, pw); |
120 } | 121 } |
121 pw.flush(); | 122 pw.flush(); |
122 pw.close(); | 123 pw.close(); |
123 } | 124 } |
124 catch (Throwable e) | 125 catch (final Throwable e) |
125 { | 126 { |
126 e.printStackTrace(); | 127 e.printStackTrace(); |
127 } | 128 } |
128 } | 129 } |
129 | 130 |
130 private void printThrowable(Throwable error, PrintWriter pw) | 131 private void printThrowable(final Throwable error, final PrintWriter pw) |
131 { | 132 { |
132 // Use simplest format for speed - we do not have much time | 133 // Use simplest format for speed - we do not have much time |
133 pw.println(error.getClass().getName()); | 134 pw.println(error.getClass().getName()); |
134 pw.println(error.getMessage()); | 135 pw.println(error.getMessage()); |
135 StackTraceElement[] trace = error.getStackTrace(); | 136 final StackTraceElement[] trace = error.getStackTrace(); |
136 for (StackTraceElement element : trace) | 137 for (final StackTraceElement element : trace) |
137 { | 138 { |
138 pw.print(element.getClassName()); | 139 pw.print(element.getClassName()); |
139 pw.print("|"); | 140 pw.print("|"); |
140 pw.print(element.getMethodName()); | 141 pw.print(element.getMethodName()); |
141 pw.print("|"); | 142 pw.print("|"); |
142 pw.print(element.isNativeMethod()); | 143 pw.print(element.isNativeMethod()); |
143 pw.print("|"); | 144 pw.print("|"); |
144 pw.print(element.getFileName()); | 145 pw.print(element.getFileName()); |
145 pw.print("|"); | 146 pw.print("|"); |
146 pw.print(element.getLineNumber()); | 147 pw.print(element.getLineNumber()); |
147 pw.println(); | 148 pw.println(); |
148 } | 149 } |
149 } | 150 } |
150 | 151 |
151 public void saveProxySettings(String host, String port, String excl) | 152 public void saveProxySettings(final String host, final String port, final Stri
ng excl) |
152 { | 153 { |
153 Log.e("DCR", "Saving proxy " + host + ":" + port + "/" + excl); | 154 Log.e("DCR", "Saving proxy " + host + ":" + port + "/" + excl); |
154 this.host = host; | 155 this.host = host; |
155 this.port = port; | 156 this.port = port; |
156 this.excl = excl; | 157 this.excl = excl; |
157 restoreProxy = true; | 158 restoreProxy = true; |
158 } | 159 } |
159 | 160 |
160 public void clearProxySettings() | 161 public void clearProxySettings() |
161 { | 162 { |
162 Log.e("DCR", "Clearing proxy"); | 163 Log.e("DCR", "Clearing proxy"); |
163 restoreProxy = false; | 164 restoreProxy = false; |
164 int p = 0; | 165 int p = 0; |
165 try | 166 try |
166 { | 167 { |
167 p = Integer.valueOf(port); | 168 p = Integer.valueOf(port); |
168 } | 169 } |
169 catch (NumberFormatException e) | 170 catch (final NumberFormatException e) |
170 { | 171 { |
171 // ignore - no valid port, it will be correctly processed later | 172 // ignore - no valid port, it will be correctly processed later |
172 } | 173 } |
173 ProxySettings.setConnectionProxy(context, host, p, excl); | 174 ProxySettings.setConnectionProxy(context, host, p, excl); |
174 } | 175 } |
175 } | 176 } |
OLD | NEW |