ExceptionHandler.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author LukeOwncloud
  5. * Copyright (C) 2016 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.ui.errorhandling;
  21. import java.io.PrintWriter;
  22. import java.io.StringWriter;
  23. import android.app.Activity;
  24. import android.content.Intent;
  25. import android.os.Build;
  26. import android.util.Log;
  27. public class ExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler {
  28. private final Activity mContext;
  29. private final String LINE_SEPARATOR = "\n";
  30. private static final String TAG = ExceptionHandler.class.getSimpleName();
  31. public ExceptionHandler(Activity context) {
  32. mContext = context;
  33. }
  34. public void uncaughtException(Thread thread, Throwable exception) {
  35. Log.e(TAG, "ExceptionHandler caught UncaughtException", exception);
  36. StringWriter stackTrace = new StringWriter();
  37. exception.printStackTrace(new PrintWriter(stackTrace));
  38. StringBuilder errorReport = new StringBuilder();
  39. errorReport.append("************ CAUSE OF ERROR ************\n\n");
  40. errorReport.append(stackTrace.toString());
  41. errorReport.append("\n************ DEVICE INFORMATION ***********\n");
  42. errorReport.append("Brand: ");
  43. errorReport.append(Build.BRAND);
  44. errorReport.append(LINE_SEPARATOR);
  45. errorReport.append("Device: ");
  46. errorReport.append(Build.DEVICE);
  47. errorReport.append(LINE_SEPARATOR);
  48. errorReport.append("Model: ");
  49. errorReport.append(Build.MODEL);
  50. errorReport.append(LINE_SEPARATOR);
  51. errorReport.append("Id: ");
  52. errorReport.append(Build.ID);
  53. errorReport.append(LINE_SEPARATOR);
  54. errorReport.append("Product: ");
  55. errorReport.append(Build.PRODUCT);
  56. errorReport.append(LINE_SEPARATOR);
  57. errorReport.append("\n************ FIRMWARE ************\n");
  58. errorReport.append("SDK: ");
  59. errorReport.append(Build.VERSION.SDK_INT);
  60. errorReport.append(LINE_SEPARATOR);
  61. errorReport.append("Release: ");
  62. errorReport.append(Build.VERSION.RELEASE);
  63. errorReport.append(LINE_SEPARATOR);
  64. errorReport.append("Incremental: ");
  65. errorReport.append(Build.VERSION.INCREMENTAL);
  66. errorReport.append(LINE_SEPARATOR);
  67. Log.e(TAG, "An exception was thrown and handled by ExceptionHandler:", exception);
  68. Intent intent = new Intent(mContext, ErrorShowActivity.class);
  69. intent.putExtra("error", errorReport.toString());
  70. mContext.startActivity(intent);
  71. android.os.Process.killProcess(android.os.Process.myPid());
  72. System.exit(1000);
  73. }
  74. }