Android Open Source - ShareWifi F C Exception Handler






From Project

Back to project page ShareWifi.

License

The source code is released under:

GNU General Public License

If you think the Android project ShareWifi listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.app.sharewifi;
/*from   w ww .  j  a  va  2s.c om*/
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

public class FCExceptionHandler implements Thread.UncaughtExceptionHandler {

  private Thread.UncaughtExceptionHandler defaultUEH;

  private Activity app = null;

  public FCExceptionHandler(Activity app) {
    this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    this.app = app;
  }

  public void uncaughtException(Thread t, Throwable e) {
    StackTraceElement[] arr = e.getStackTrace();
    String report = "App version: " + getVersionName(this.app)
        + " Phone Model: " + android.os.Build.MANUFACTURER + "-"
        + android.os.Build.MODEL + "\n\n";
    report += e.toString() + "\n\n";
    report += "--------- Stack trace ---------\n\n";
    for (int i = 0; i < arr.length; i++) {
      report += "    " + arr[i].toString() + "\n";
    }
    report += "-------------------------------\n\n";

    // If the exception was thrown in a background thread inside
    // AsyncTask, then the actual exception can be found with getCause
    report += "--------- Cause ---------\n\n";
    Throwable cause = e.getCause();
    if (cause != null) {
      report += cause.toString() + "\n\n";
      arr = cause.getStackTrace();
      for (int i = 0; i < arr.length; i++) {
        report += "    " + arr[i].toString() + "\n";
      }
    }
    report += "-------------------------------\n\n";

    try {
      FileOutputStream trace = app.openFileOutput("stack.trace",
          Context.MODE_PRIVATE);
      trace.write(report.getBytes());
      trace.close();
    } catch (IOException ioe) {
      // ...
    }

    defaultUEH.uncaughtException(t, e);
  }

  public static String getVersionName(Context context) {
    PackageManager pm = context.getPackageManager();
    try {
      PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
      return pi.versionName;
    } catch (NameNotFoundException ex) {
    }
    return "";
  }
}




Java Source Code List

com.app.sharewifi.DBAdapter.java
com.app.sharewifi.FCExceptionHandler.java
com.app.sharewifi.Model.java
com.app.sharewifi.ShareWifiActivity.java