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

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

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

Powered by Google App Engine
This is Rietveld