package de.dhbwloe.campusapp;
import android.app.Activity;
-import android.os.Bundle;
-import android.view.View;
-import android.view.Window;
-import android.webkit.WebView;
-import android.widget.Button;
-import android.widget.EditText;
-import android.widget.ProgressBar;
+import android.content.Intent;
+import android.util.Log;
+
+import com.loopj.android.http.AsyncHttpClient;
+import com.loopj.android.http.AsyncHttpResponseHandler;
+import com.loopj.android.http.RequestParams;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+import cz.msebera.android.httpclient.Header;
/**
* Created by pk910 on 11.03.2016.
*/
-public class CampusAppCrashHandler extends Activity {
+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();
- private String crashdate, stacktrace;
- private CampusAppExceptionHandler exceptionHandler;
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
+ 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) {
- int p = android.os.Process.myPid();
- android.os.Process.killProcess(p);
+ if(crashLogWindowOpened)
return;
- }
crashLogWindowOpened = true;
}
- requestWindowFeature(Window.FEATURE_NO_TITLE); // make a window without a titlebar
- setContentView(R.layout.crash_log);
- final Activity that = this;
+ final Writer result = new StringWriter();
+ final PrintWriter printWriter = new PrintWriter(result);
+ e.printStackTrace(printWriter);
+ String stacktrace = result.toString();
+ printWriter.close();
- Bundle extras = getIntent().getExtras();
- crashdate = (extras != null ? extras.getString("crashdate") : null);
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
+ String crashdate = dateFormat.format(new Date());
+ String filename = crashdate + ".stacktrace";
- EditText crashLog = (EditText) findViewById(R.id.crashLog);
- exceptionHandler = new CampusAppExceptionHandler();
- stacktrace = exceptionHandler.getCrashStacktrace(that, crashdate);
- crashLog.setText(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();
- Button sendButton = (Button) findViewById(R.id.sendCrashLog);
- sendButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Button sendButton = (Button) v;
- ProgressBar sendProgress = (ProgressBar) findViewById(R.id.sendProgressBar);
+ 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) {
+ }
+ }
+ private void writeToFile(String stacktrace, String filename) {
+ try {
+ BufferedWriter bos = new BufferedWriter(new FileWriter(
+ localPath + "/" + filename));
+ bos.write(stacktrace);
+ bos.flush();
+ bos.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
- sendButton.setEnabled(false);
- sendProgress.setVisibility(View.VISIBLE);
+ public interface PostProcessingCallback {
+ public void onPostProcessFinished(String response);
+ }
- exceptionHandler.postprocessException(crashdate, stacktrace, new CampusAppExceptionHandler.PostProcessingCallback() {
- @Override
- public void onPostProcessFinished(String response) {
- ProgressBar sendProgress = (ProgressBar) findViewById(R.id.sendProgressBar);
- WebView reportResponse = (WebView) findViewById(R.id.crashReportResponse);
+ public String getCrashStacktrace(Activity crashLogScreen, String crashdate) {
+ localPath = crashLogScreen.getApplicationContext().getFilesDir().getAbsolutePath();
+ String fileName = localPath + "/" + crashdate + ".stacktrace";
+ StringBuilder stackTrace = new StringBuilder();
- sendProgress.setVisibility(View.GONE);
- reportResponse.setVisibility(View.VISIBLE);
- reportResponse.loadData(response, "text/html", null);
- }
- });
+ try {
+ BufferedReader br = new BufferedReader(new FileReader(fileName));
+ String line;
+
+ while ((line = br.readLine()) != null) {
+ stackTrace.append(line);
+ stackTrace.append('\n');
+ }
+ br.close();
+ }
+ catch (IOException e) {
+ //You'll need to add proper error handling here
+ }
+ return stackTrace.toString();
+ }
+
+ public void postprocessException(String crashdate, String stacktrace, PostProcessingCallback callback) {
+ sendToServer(CRASHLOG_UPLOAD, stacktrace, crashdate, callback);
+ }
+
+ private void sendToServer(String url, String stacktrace, String crashdate, final PostProcessingCallback callback) {
+ AsyncHttpClient client = new AsyncHttpClient();
+ RequestParams params = new RequestParams();
+ params.add("stacktrace", stacktrace);
+ params.add("crashdate", crashdate);
+
+ client.post(url, params, new AsyncHttpResponseHandler() {
+
+ @Override
+ public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
+ callback.onPostProcessFinished((responseBody != null ? new String(responseBody) : null));
+ }
+
+ @Override
+ public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
+ callback.onPostProcessFinished("Failed: "+(responseBody != null ? new String(responseBody) : null));
}
});
}
+
}