/
OsmApplication.java
122 lines (101 loc) · 3.64 KB
/
OsmApplication.java
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package org.osmdroid;
import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import com.squareup.leakcanary.LeakCanary;
import org.acra.ACRA;
import org.acra.annotation.ReportsCrashes;
import org.acra.collector.CrashReportData;
import org.acra.sender.ReportSender;
import org.acra.sender.ReportSenderException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* This is the base application for the sample app. We only use to catch errors during development cycles
* Created by alex on 7/4/16.
*/
@ReportsCrashes( formUri = "")
public class OsmApplication extends Application{
@Override
public void onCreate(){
super.onCreate();
LeakCanary.install(this);
Thread.currentThread().setUncaughtExceptionHandler(new OsmUncaughtExceptionHandler());
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// Initialise ACRA
ACRA.init(this);
ACRA.getErrorReporter().setReportSender(new ErrorFileWriter());
}
public static class OsmUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e("UncaughtException", "Got an uncaught exception: "+ex.toString());
if(ex.getClass().equals(OutOfMemoryError.class))
{
writeHprof();
}
ex.printStackTrace();
}
}
/**
* writes the current heap to the file system at /sdcard/osmdroid/trace-{timestamp}.hprof
*/
public static void writeHprof(){
try {
android.os.Debug.dumpHprofData(Environment.getExternalStorageDirectory().getAbsolutePath() + "/osmdroid/trace-" + System.currentTimeMillis()+ ".hprof");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Writes hard crash stack traces to a file on the SD card.
*/
private static class ErrorFileWriter implements ReportSender {
@Override
public void send(Context context, CrashReportData crashReportData) throws ReportSenderException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss",
Locale.US);
String timeStamp = sdf.format(new Date());
String rootDirectory = Environment.getExternalStorageDirectory()
.getAbsolutePath();
File f = new File(rootDirectory
+ File.separatorChar
+ "TCE"
+ File.separatorChar
+ "logs"
+ File.separatorChar
+ "crash"
+ File.separatorChar);
f.mkdirs();
f = new File(rootDirectory
+ File.separatorChar
+ "TCE"
+ File.separatorChar
+ "logs"
+ File.separatorChar
+ "crash"
+ File.separatorChar
+ "CRASH_"
+ BuildConfig.APPLICATION_ID+ "_"
+ timeStamp + ".log");
f.delete();
try {
f.createNewFile();
PrintWriter pw = new PrintWriter(new FileWriter(f));
pw.println(crashReportData.toString());
pw.close();
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
}