implements #1233
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / ExceptionHandler.java
1 package com.owncloud.android.utils;
2
3 import android.content.Context;
4 import android.content.Intent;
5 import android.os.Build;
6 import android.os.Handler;
7 import android.os.Looper;
8
9 import com.owncloud.android.MainApp;
10 import com.owncloud.android.ui.activity.ErrorReportActivity;
11
12 import java.io.PrintWriter;
13 import java.io.StringWriter;
14
15 // from https://stackoverflow.com/questions/23486627/catching-error-and-user-information#answer-23486834
16 public class ExceptionHandler implements Thread.UncaughtExceptionHandler {
17
18 Context context;
19 private final String LINE_SEPARATOR = "\n";
20
21 public ExceptionHandler() {
22 // TODO Auto-generated constructor stub
23 context = MainApp.getAppContext();
24 }
25
26 @Override
27 public void uncaughtException(Thread arg0, Throwable arg1) {
28 // TODO Auto-generated method stub
29
30 StringWriter stackTrace = new StringWriter();
31 arg1.printStackTrace(new PrintWriter(stackTrace));
32 final StringBuilder errorReport = new StringBuilder();
33 errorReport.append("************ CAUSE OF ERROR ************\n\n");
34 errorReport.append(stackTrace.toString());
35
36 errorReport.append("\n************ DEVICE INFORMATION ***********\n");
37 errorReport.append("Brand: ");
38 errorReport.append(Build.BRAND);
39 errorReport.append(LINE_SEPARATOR);
40 errorReport.append("Device: ");
41 errorReport.append(Build.DEVICE);
42 errorReport.append(LINE_SEPARATOR);
43 errorReport.append("Model: ");
44 errorReport.append(Build.MODEL);
45 errorReport.append(LINE_SEPARATOR);
46 errorReport.append("Id: ");
47 errorReport.append(Build.ID);
48 errorReport.append(LINE_SEPARATOR);
49 errorReport.append("Product: ");
50 errorReport.append(Build.PRODUCT);
51 errorReport.append(LINE_SEPARATOR);
52 errorReport.append("\n************ FIRMWARE ************\n");
53 errorReport.append("SDK: ");
54 errorReport.append(Build.VERSION.SDK);
55 errorReport.append(LINE_SEPARATOR);
56 errorReport.append("Release: ");
57 errorReport.append(Build.VERSION.RELEASE);
58 errorReport.append(LINE_SEPARATOR);
59 errorReport.append("Incremental: ");
60 errorReport.append(Build.VERSION.INCREMENTAL);
61 errorReport.append(LINE_SEPARATOR);
62
63
64 //after this you can do whatever you want , like i start an activity and show error log there
65
66 if (isUIThread()) {
67 invokeLogActivity(errorReport);
68 } else { //handle non UI thread throw uncaught exception
69
70 new Handler(Looper.getMainLooper()).post(new Runnable() {
71 @Override
72 public void run() {
73 invokeLogActivity(errorReport);
74 }
75 });
76 }
77 }
78 private void invokeLogActivity(StringBuilder errorReport){
79 // Intent sendIntent = new Intent();
80 // sendIntent.setAction(Intent.ACTION_SEND);
81 // sendIntent.putExtra(Intent.EXTRA_TEXT, errorReport.toString());
82 // sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
83 // sendIntent.setType("text/plain");
84 // context.startActivity(sendIntent);
85
86 Intent sendIntent = new Intent(context, ErrorReportActivity.class);
87 sendIntent.putExtra(Intent.EXTRA_TEXT, errorReport.toString());
88 sendIntent.setAction(Intent.ACTION_SEND);
89 sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
90 sendIntent.setType("text/plain");
91 context.startActivity(sendIntent);
92
93
94 System.exit(1);
95 // android.os.Process.killProcess(android.os.Process.myPid());
96
97 }
98
99 private boolean isUIThread(){
100 return Looper.getMainLooper().getThread() == Thread.currentThread();
101 }
102 }