Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/org/adblockplus/android/CrashHandler.java

Issue 8487098: ABP/Android crash reporter (Closed)
Patch Set: ABP/Android crash reporter Created Nov. 1, 2012, 9:44 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « res/layout/crashreport.xml ('k') | src/org/adblockplus/android/CrashReportDialog.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 package org.adblockplus.android;
2
3 import java.io.PrintWriter;
4 import java.lang.Thread.UncaughtExceptionHandler;
5
6 import android.annotation.SuppressLint;
7 import android.app.NotificationManager;
8 import android.content.Context;
9 import android.content.pm.PackageInfo;
10 import android.content.pm.PackageManager.NameNotFoundException;
11 import android.os.Build;
12 import android.util.Log;
13
14 /**
15 * Writes crash data in file.
16 */
17 public class CrashHandler implements UncaughtExceptionHandler
18 {
19 public static final String REPORT_FILE = "AdblockPlus_Crash_Report.txt";
20 private UncaughtExceptionHandler defaultUEH;
21 private NotificationManager notificationManager;
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;
29
30 public CrashHandler(Context context)
31 {
32 defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
33 this.context = context;
34 notificationManager = (NotificationManager) context.getSystemService(Context .NOTIFICATION_SERVICE);
35 generateReport = false;
36 restoreProxy = false;
37 }
38
39 public UncaughtExceptionHandler getDefault()
40 {
41 return defaultUEH;
42 }
43
44 @Override
45 public void uncaughtException(Thread t, Throwable e)
46 {
47 if (generateReport)
48 writeToFile(e, REPORT_FILE);
49
50 if (restoreProxy)
51 clearProxySettings();
52
53 if (notificationManager != null)
54 {
55 try
56 {
57 notificationManager.cancel(ProxyService.ONGOING_NOTIFICATION_ID);
58 }
59 catch (Throwable ex)
60 {
61 ex.printStackTrace();
62 }
63 }
64 notificationManager = null;
65
66 defaultUEH.uncaughtException(t, e);
67 }
68
69 public void generateReport(boolean report)
70 {
71 generateReport = report;
72 }
73
74 @SuppressLint("WorldReadableFiles")
75 private void writeToFile(Throwable error, String filename)
76 {
77 Log.e("DCR", "Writing crash report");
78 int versionCode = -1;
79 try
80 {
81 PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPac kageName(), 0);
82 versionCode = pi.versionCode;
83 }
84 catch (NameNotFoundException ex)
85 {
86 }
87 try
88 {
89 PrintWriter pw = new PrintWriter(context.openFileOutput(filename, Context. MODE_WORLD_READABLE));
90 // Write Android version
91 pw.println(Build.VERSION.SDK_INT);
92 // Write application build number
93 pw.println(versionCode);
94
95 // Write exception data
96 printThrowable(error, pw);
97 Throwable cause = error.getCause();
98 // Write cause data
99 if (cause != null)
100 {
101 pw.println("cause");
102 printThrowable(cause, pw);
103 }
104 pw.flush();
105 pw.close();
106 }
107 catch (Throwable e)
108 {
109 e.printStackTrace();
110 }
111 }
112
113 private void printThrowable(Throwable error, PrintWriter pw)
114 {
115 // Use simplest format for speed - we do not have much time
116 pw.println(error.getClass().getName());
117 pw.println(error.getMessage());
118 StackTraceElement[] trace = error.getStackTrace();
119 for (StackTraceElement element : trace)
120 {
121 pw.print(element.getClassName());
122 pw.print("|");
123 pw.print(element.getMethodName());
124 pw.print("|");
125 pw.print(element.isNativeMethod());
126 pw.print("|");
127 pw.print(element.getFileName());
128 pw.print("|");
129 pw.print(element.getLineNumber());
130 pw.println();
131 }
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 }
158 }
OLDNEW
« no previous file with comments | « res/layout/crashreport.xml ('k') | src/org/adblockplus/android/CrashReportDialog.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld