Context-detection API for Android developed as a university project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExceptionHandler.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package uk.co.md87.android.common;
  23. import android.content.Context;
  24. import android.content.pm.PackageManager.NameNotFoundException;
  25. import android.telephony.TelephonyManager;
  26. import java.io.IOException;
  27. import java.io.PrintWriter;
  28. import java.io.StringWriter;
  29. import java.io.Writer;
  30. import java.lang.Thread.UncaughtExceptionHandler;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import org.apache.http.NameValuePair;
  34. import org.apache.http.client.entity.UrlEncodedFormEntity;
  35. import org.apache.http.client.methods.HttpPost;
  36. import org.apache.http.impl.client.DefaultHttpClient;
  37. import org.apache.http.message.BasicNameValuePair;
  38. import org.apache.http.protocol.HTTP;
  39. /**
  40. * An exception handler which reports any uncaught exceptions to the context
  41. * API's website, in order to facilitate remote diagnostics of user errors.
  42. *
  43. * @author chris
  44. */
  45. public class ExceptionHandler implements UncaughtExceptionHandler {
  46. private UncaughtExceptionHandler defaultUEH;
  47. private String appname, url, version, imei;
  48. public ExceptionHandler(String appname, String url, String version, String imei) {
  49. this.appname = appname;
  50. this.url = url;
  51. this.version = version;
  52. this.imei = imei;
  53. this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
  54. }
  55. public ExceptionHandler(Context context) {
  56. this(getAppName(context), "http://chris.smith.name/android/upload",
  57. getVersionName(context), getIMEI(context));
  58. }
  59. private static String getVersionName(final Context context) {
  60. try {
  61. return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
  62. } catch (NameNotFoundException ex) {
  63. return "Unknown";
  64. }
  65. }
  66. private static String getAppName(final Context context) {
  67. try {
  68. return context.getPackageManager().getApplicationLabel(context
  69. .getPackageManager().getPackageInfo(context.getPackageName(), 0)
  70. .applicationInfo).toString().replaceAll("[^A-Za-z]", "");
  71. } catch (NameNotFoundException ex) {
  72. return "Unknown";
  73. }
  74. }
  75. private static String getIMEI(final Context context) {
  76. return ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
  77. }
  78. public void uncaughtException(Thread t, Throwable e) {
  79. String timestamp = String.valueOf(System.currentTimeMillis());
  80. final Writer result = new StringWriter();
  81. final PrintWriter printWriter = new PrintWriter(result);
  82. e.printStackTrace(printWriter);
  83. String stacktrace = result.toString();
  84. printWriter.close();
  85. String filename = timestamp + ".stacktrace";
  86. sendToServer(stacktrace, filename);
  87. defaultUEH.uncaughtException(t, e);
  88. }
  89. private void sendToServer(String stacktrace, String filename) {
  90. DefaultHttpClient httpClient = new DefaultHttpClient();
  91. HttpPost httpPost = new HttpPost(url);
  92. httpPost.setHeader("x-application", appname + "-exception");
  93. httpPost.setHeader("x-version", version);
  94. httpPost.setHeader("x-imei", imei);
  95. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  96. nvps.add(new BasicNameValuePair("filename", filename));
  97. nvps.add(new BasicNameValuePair("stacktrace", stacktrace));
  98. try {
  99. httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
  100. httpClient.execute(httpPost);
  101. } catch (IOException e) {
  102. // Do nothing
  103. }
  104. }
  105. }