LEFT | RIGHT |
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 15 matching lines...) Expand all Loading... |
26 import android.content.pm.PackageInfo; | 26 import android.content.pm.PackageInfo; |
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 final static String REPORT_FILE = "AdblockPlus_Crash_Report.txt"; | 36 public static final String REPORT_FILE = "AdblockPlus_Crash_Report.txt"; |
37 | |
38 private final UncaughtExceptionHandler defaultUEH; | 37 private final UncaughtExceptionHandler defaultUEH; |
| 38 private NotificationManager notificationManager; |
39 private final Context context; | 39 private final Context context; |
40 | 40 |
41 private NotificationManager notificationManager; | |
42 private boolean generateReport; | 41 private boolean generateReport; |
43 private boolean restoreProxy; | 42 private boolean restoreProxy; |
44 private String host; | 43 private String host; |
45 private String port; | 44 private String port; |
46 private String excl; | 45 private String excl; |
47 | 46 |
48 public CrashHandler(final Context context) | 47 public CrashHandler(final Context context) |
49 { | 48 { |
50 this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); | 49 defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); |
51 this.context = context; | 50 this.context = context; |
52 this.notificationManager = (NotificationManager)context.getSystemService(Con
text.NOTIFICATION_SERVICE); | 51 notificationManager = (NotificationManager) context.getSystemService(Context
.NOTIFICATION_SERVICE); |
53 this.generateReport = false; | 52 generateReport = false; |
54 this.restoreProxy = false; | 53 restoreProxy = false; |
55 } | 54 } |
56 | 55 |
57 public UncaughtExceptionHandler getDefault() | 56 public UncaughtExceptionHandler getDefault() |
58 { | 57 { |
59 return this.defaultUEH; | 58 return defaultUEH; |
60 } | 59 } |
61 | 60 |
62 @Override | 61 @Override |
63 public void uncaughtException(final Thread t, final Throwable e) | 62 public void uncaughtException(final Thread t, final Throwable e) |
64 { | 63 { |
65 if (this.generateReport) | 64 if (generateReport) |
66 { | 65 writeToFile(e, REPORT_FILE); |
67 this.writeToFile(e, REPORT_FILE); | |
68 } | |
69 | 66 |
70 if (this.restoreProxy) | 67 if (restoreProxy) |
71 { | 68 clearProxySettings(); |
72 this.clearProxySettings(); | |
73 } | |
74 | 69 |
75 if (this.notificationManager != null) | 70 if (notificationManager != null) |
76 { | 71 { |
77 try | 72 try |
78 { | 73 { |
79 this.notificationManager.cancel(ProxyService.ONGOING_NOTIFICATION_ID); | 74 notificationManager.cancel(ProxyService.ONGOING_NOTIFICATION_ID); |
80 } | 75 } |
81 catch (final Throwable ex) | 76 catch (final Throwable ex) |
82 { | 77 { |
83 ex.printStackTrace(); | 78 ex.printStackTrace(); |
84 } | 79 } |
85 } | 80 } |
86 this.notificationManager = null; | 81 notificationManager = null; |
87 | 82 |
88 this.defaultUEH.uncaughtException(t, e); | 83 defaultUEH.uncaughtException(t, e); |
89 } | 84 } |
90 | 85 |
91 public void generateReport(final boolean report) | 86 public void generateReport(final boolean report) |
92 { | 87 { |
93 this.generateReport = report; | 88 generateReport = report; |
94 } | 89 } |
95 | 90 |
96 @SuppressLint("WorldReadableFiles") | 91 @SuppressLint("WorldReadableFiles") |
97 private void writeToFile(final Throwable error, final String filename) | 92 private void writeToFile(final Throwable error, final String filename) |
98 { | 93 { |
99 Log.e("DCR", "Writing crash report"); | 94 Log.e("DCR", "Writing crash report"); |
100 int versionCode = -1; | 95 int versionCode = -1; |
101 try | 96 try |
102 { | 97 { |
103 final PackageInfo pi = this.context.getPackageManager().getPackageInfo(thi
s.context.getPackageName(), 0); | 98 final PackageInfo pi = context.getPackageManager().getPackageInfo(context.
getPackageName(), 0); |
104 versionCode = pi.versionCode; | 99 versionCode = pi.versionCode; |
105 } | 100 } |
106 catch (final NameNotFoundException ex) | 101 catch (final NameNotFoundException ex) |
107 { | 102 { |
108 } | 103 } |
109 try | 104 try |
110 { | 105 { |
111 final PrintWriter pw = new PrintWriter(this.context.openFileOutput(filenam
e, Context.MODE_WORLD_READABLE)); | 106 final PrintWriter pw = new PrintWriter(context.openFileOutput(filename, Co
ntext.MODE_WORLD_READABLE)); |
112 // Write Android version | 107 // Write Android version |
113 pw.println(Build.VERSION.SDK_INT); | 108 pw.println(Build.VERSION.SDK_INT); |
114 // Write application build number | 109 // Write application build number |
115 pw.println(versionCode); | 110 pw.println(versionCode); |
116 | 111 |
117 // Write exception data | 112 // Write exception data |
118 this.printThrowable(error, pw); | 113 printThrowable(error, pw); |
119 final Throwable cause = error.getCause(); | 114 final Throwable cause = error.getCause(); |
120 // Write cause data | 115 // Write cause data |
121 if (cause != null) | 116 if (cause != null) |
122 { | 117 { |
123 pw.println("cause"); | 118 pw.println("cause"); |
124 this.printThrowable(cause, pw); | 119 printThrowable(cause, pw); |
125 } | 120 } |
126 pw.flush(); | 121 pw.flush(); |
127 pw.close(); | 122 pw.close(); |
128 } | 123 } |
129 catch (final Throwable e) | 124 catch (final Throwable e) |
130 { | 125 { |
131 e.printStackTrace(); | 126 e.printStackTrace(); |
132 } | 127 } |
133 } | 128 } |
134 | 129 |
(...skipping 17 matching lines...) Expand all Loading... |
152 pw.println(); | 147 pw.println(); |
153 } | 148 } |
154 } | 149 } |
155 | 150 |
156 public void saveProxySettings(final String host, final String port, final Stri
ng excl) | 151 public void saveProxySettings(final String host, final String port, final Stri
ng excl) |
157 { | 152 { |
158 Log.e("DCR", "Saving proxy " + host + ":" + port + "/" + excl); | 153 Log.e("DCR", "Saving proxy " + host + ":" + port + "/" + excl); |
159 this.host = host; | 154 this.host = host; |
160 this.port = port; | 155 this.port = port; |
161 this.excl = excl; | 156 this.excl = excl; |
162 this.restoreProxy = true; | 157 restoreProxy = true; |
163 } | 158 } |
164 | 159 |
165 public void clearProxySettings() | 160 public void clearProxySettings() |
166 { | 161 { |
167 Log.e("DCR", "Clearing proxy"); | 162 Log.e("DCR", "Clearing proxy"); |
168 this.restoreProxy = false; | 163 restoreProxy = false; |
169 int p = 0; | 164 int p = 0; |
170 try | 165 try |
171 { | 166 { |
172 p = Integer.valueOf(this.port); | 167 p = Integer.valueOf(port); |
173 } | 168 } |
174 catch (final NumberFormatException e) | 169 catch (final NumberFormatException e) |
175 { | 170 { |
176 // ignore - no valid port, it will be correctly processed later | 171 // ignore - no valid port, it will be correctly processed later |
177 } | 172 } |
178 ProxySettings.setConnectionProxy(this.context, this.host, p, this.excl); | 173 ProxySettings.setConnectionProxy(context, host, p, excl); |
179 } | 174 } |
180 } | 175 } |
LEFT | RIGHT |