+public class CampusAppCrashHandler {
+ private static String CRASHLOG_UPLOAD = "http://dev.pk910.de/DHBWCampusCourses";
+ private String localPath;
+
+ private static boolean crashLogWindowOpened = false;
+ private static final Object crashWindowLock = new Object();
+
+ public void handleUncaughtException(Thread thread, Throwable e) {
+ // automatically send crash log to my server for debuggging.
+ // disable this for productive use! (CampusAppContext.DEBUG)
+
+ synchronized (crashWindowLock) {
+ if(crashLogWindowOpened)
+ return;
+ crashLogWindowOpened = true;
+ }
+
+ final Writer result = new StringWriter();
+ final PrintWriter printWriter = new PrintWriter(result);
+ e.printStackTrace(printWriter);
+ String stacktrace = result.toString();
+ printWriter.close();
+
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
+ String crashdate = dateFormat.format(new Date());
+ String filename = crashdate + ".stacktrace";
+
+ CampusAppContext context;
+ Activity mainActivity = null;
+ try {
+ /*Wir greifen nun auf einen Code zurück, aus welchem die unbehandelte Exception stammt.
+ * Dies ist womöglich keine gute Idee, daher müssen wir sämmtliche Exceptions abfangen und verwerfen (Exception inception :D)
+ * und hoffen, dass die App noch so weit "funktioniert" ;)
+ */
+ context = CampusAppContext.getInstance();
+
+ if(context != null) {
+ mainActivity = context.getMainActivity();
+ }
+ if(mainActivity != null) {
+ localPath = mainActivity.getApplicationContext().getFilesDir().getAbsolutePath();
+ }
+ } catch (Exception ex) {}
+
+ if(localPath != null)
+ writeToFile(stacktrace, filename);
+
+ // start crash screen
+ try {
+ Intent intent = new Intent();
+ intent.setAction("de.dhbw.campusapp.CRASH_LOG");
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // required when starting from Application
+ intent.putExtra("crashdate", crashdate);
+ mainActivity.startActivity(intent);
+ } catch (Exception ex) {
+ }
+ }