blob: 1401542389779e7c7b458aa2ef936957d45d5eb4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
package cgeo.geocaching.utils;
import android.os.Environment;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
public class OOMDumpingUncaughtExceptionHandler implements UncaughtExceptionHandler {
private UncaughtExceptionHandler defaultHandler = null;
private boolean defaultReplaced = false;
public static boolean activateHandler() {
final OOMDumpingUncaughtExceptionHandler handler = new OOMDumpingUncaughtExceptionHandler();
return handler.activate();
}
private boolean activate() {
defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
// replace default handler if that has not been done already
if (!(defaultHandler instanceof OOMDumpingUncaughtExceptionHandler)) {
Thread.setDefaultUncaughtExceptionHandler(this);
defaultReplaced = true;
} else {
defaultHandler = null;
defaultReplaced = false;
}
return defaultReplaced;
}
public static boolean resetToDefault() {
boolean defaultResetted = false;
final UncaughtExceptionHandler unspecificHandler = Thread.getDefaultUncaughtExceptionHandler();
if (unspecificHandler instanceof OOMDumpingUncaughtExceptionHandler) {
final OOMDumpingUncaughtExceptionHandler handler = (OOMDumpingUncaughtExceptionHandler) unspecificHandler;
defaultResetted = handler.reset();
}
return defaultResetted;
}
private boolean reset() {
final boolean resetted = defaultReplaced;
if (defaultReplaced) {
Thread.setDefaultUncaughtExceptionHandler(defaultHandler);
defaultReplaced = false;
}
return resetted;
}
@Override
public void uncaughtException(final Thread thread, final Throwable ex) {
Log.e("UncaughtException", ex);
Throwable exx = ex;
while (exx.getCause() != null) {
exx = exx.getCause();
}
if (exx.getClass().equals(OutOfMemoryError.class)) {
try {
Log.e("OutOfMemory");
android.os.Debug.dumpHprofData(Environment.getExternalStorageDirectory().getPath() + "/dump.hprof");
} catch (final IOException e) {
Log.e("Error writing dump", e);
}
}
defaultHandler.uncaughtException(thread, ex);
}
}
|