From 44e32e4a1402ff1109344f7d11662f080ccc811b Mon Sep 17 00:00:00 2001 From: ricky barrette Date: Sun, 13 Feb 2011 23:29:51 +0000 Subject: [PATCH] updated to use the new exceptionhandler library --- TravelPost/.classpath | 1 + TravelPost/.project | 5 + TravelPost/default.properties | 1 + .../com/TwentyCodes/android/exception/R.java | 62 +++++ .../PostMortemReportExceptionHandler.java | 220 ------------------ .../android/TravelPost/TravelPost.java | 8 +- 6 files changed, 73 insertions(+), 224 deletions(-) create mode 100644 TravelPost/gen/com/TwentyCodes/android/exception/R.java delete mode 100644 TravelPost/src/com/TwentyCodes/android/TravelPost/PostMortemReportExceptionHandler.java diff --git a/TravelPost/.classpath b/TravelPost/.classpath index fee4f71..2fc4b68 100644 --- a/TravelPost/.classpath +++ b/TravelPost/.classpath @@ -11,5 +11,6 @@ + diff --git a/TravelPost/.project b/TravelPost/.project index 68cba3e..0c7114e 100644 --- a/TravelPost/.project +++ b/TravelPost/.project @@ -31,6 +31,11 @@ org.eclipse.jdt.core.javanature + + ExceptionHandlerLib_src + 2 + _android_ExceptionHandlerLib_7d68b46a/src + FacebookLib_src 2 diff --git a/TravelPost/default.properties b/TravelPost/default.properties index af7b5fb..219555b 100644 --- a/TravelPost/default.properties +++ b/TravelPost/default.properties @@ -13,3 +13,4 @@ proguard.config=proguard.cfg android.library.reference.1=../FacebookLib/ android.library.reference.2=../LocationLib +android.library.reference.3=../ExceptionHandlerLib diff --git a/TravelPost/gen/com/TwentyCodes/android/exception/R.java b/TravelPost/gen/com/TwentyCodes/android/exception/R.java new file mode 100644 index 0000000..89bd491 --- /dev/null +++ b/TravelPost/gen/com/TwentyCodes/android/exception/R.java @@ -0,0 +1,62 @@ +/* AUTO-GENERATED FILE. DO NOT MODIFY. + * + * This class was automatically generated by the + * aapt tool from the resource data it found. It + * should not be modified by hand. + */ + +package com.TwentyCodes.android.exception; + +public final class R { + public static final class attr { + } + public static final class drawable { + public static final int facebook_icon=0x7f020000; + public static final int icon=0x7f020001; + public static final int logoidea3=0x7f020002; + public static final int sign_post_icon=0x7f020003; + public static final int sign_post_icon_mod=0x7f020004; + public static final int skyhook_logo=0x7f020005; + public static final int skyhook_logo_alpha=0x7f020006; + public static final int skyhook_logo_alpha_small=0x7f020007; + public static final int tc_logo_complete=0x7f020008; + public static final int user=0x7f020009; + public static final int widget=0x7f02000a; + } + public static final class id { + public static final int authCode=0x7f060007; + public static final int authOkButton=0x7f060006; + public static final int authWebView=0x7f060008; + public static final int post=0x7f060001; + public static final int postOkButton=0x7f060000; + public static final int skyhook_img=0x7f060002; + public static final int tc_img=0x7f060004; + public static final int text=0x7f060003; + public static final int widgetbutton=0x7f060005; + } + public static final class layout { + public static final int main=0x7f030000; + public static final int post=0x7f030001; + public static final int powered_by_skyhook=0x7f030002; + public static final int travelpostwidget=0x7f030003; + public static final int webauth=0x7f030004; + } + public static final class string { + public static final int about=0x7f050007; + public static final int app_name=0x7f050000; + public static final int fb_signin=0x7f050005; + public static final int gps_fix=0x7f050001; + public static final int hello=0x7f050003; + public static final int location_settings=0x7f050006; + public static final int post_hint=0x7f05000b; + public static final int post_settings=0x7f050008; + public static final int save_a_post=0x7f050009; + public static final int services=0x7f05000a; + public static final int sorry_theres_trouble=0x7f050002; + public static final int twitter_signin=0x7f050004; + } + public static final class xml { + public static final int settings=0x7f040000; + public static final int travelpostwidgetinfo=0x7f040001; + } +} diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/PostMortemReportExceptionHandler.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/PostMortemReportExceptionHandler.java deleted file mode 100644 index be15c1d..0000000 --- a/TravelPost/src/com/TwentyCodes/android/TravelPost/PostMortemReportExceptionHandler.java +++ /dev/null @@ -1,220 +0,0 @@ -/** - * @author Twenty Codes - * @author ricky barrette - */ - -package com.TwentyCodes.android.TravelPost; - -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.lang.Thread.UncaughtExceptionHandler; -import java.lang.reflect.Field; -import java.text.SimpleDateFormat; -import java.util.Date; - -import android.app.Activity; -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.content.pm.PackageManager.NameNotFoundException; -import android.os.Build; - -/** - * The Twenty Codes, LLC Exception handler.
- * This exception handler will generate an email with the stacktrace, cause, device & eviroment, and complete logcat

- * to use create a field variable, and add the following lines to your onCreate() - * mExceptionReport.run(); - Thread.setDefaultUncaughtExceptionHandler(mExceptionReport);

- * Don't forget the manifest tag android.permission.READ_LOGS - * @author ricky barette - */ -public class PostMortemReportExceptionHandler implements UncaughtExceptionHandler, Runnable { - public static final String ExceptionReportFilename = "postmortem.trace"; - - private static final String MSG_SUBJECT_TAG = "Exception Report"; //"app title + this tag" = email subject - private static final String MSG_SENDTO = "twentycodes@gmail.com"; //email will be sent to this account - //the following may be something you wish to consider localizing - private static final String MSG_BODY = "Just click send to help make this application better. "+ - "No personal information is being sent (you can check by reading the rest of the email)."; - - private Thread.UncaughtExceptionHandler mDefaultUEH; - private Activity mApp = null; - - public PostMortemReportExceptionHandler(Activity aApp) { - mDefaultUEH = Thread.getDefaultUncaughtExceptionHandler(); - mApp = aApp; - } - - public void uncaughtException(Thread t, Throwable e) { - submit(e); - //do not forget to pass this exception through up the chain - mDefaultUEH.uncaughtException(t,e); - } - - public String getDebugReport(Throwable aException) { - -// NumberFormat theFormatter = new DecimalFormat("#0."); - //stack trace - StackTraceElement[] theStackTrace = aException.getStackTrace(); - - StringBuffer report = new StringBuffer(); - - report.append("--------- Application ---------\n\n"); - - report.append(mApp.getPackageName()+" generated the following exception:\n\n"); - - report.append(aException.toString() + "\n\n"); - - report.append("-------------------------------\n\n"); - - report.append("--------- Stack trace ---------\n\n"); - for (int i = 0; i < theStackTrace.length; i++) { - report.append(" " + theStackTrace[i].toString() + "\n"); - } - report.append("-------------------------------\n\n"); - - //app environment - PackageManager pm = mApp.getPackageManager(); - PackageInfo pi; - try { - pi = pm.getPackageInfo(mApp.getPackageName(), 0); - } catch (NameNotFoundException eNnf) { - //doubt this will ever run since we want info about our own package - pi = new PackageInfo(); - pi.versionName = "unknown"; - pi.versionCode = 69; - } - - Date theDate = new Date(); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd_HH.mm.ss_zzz"); - report.append("-------- Environment --------\n"); - report.append("Time\t="+sdf.format(theDate)+"\n"); - report.append("Device\t="+Build.FINGERPRINT+"\n"); - try { - Field theMfrField = Build.class.getField("MANUFACTURER"); - report.append("Make\t="+theMfrField.get(null)+"\n"); - } catch (SecurityException e) { - } catch (NoSuchFieldException e) { - } catch (IllegalArgumentException e) { - } catch (IllegalAccessException e) { - } - report.append("Device: " + Build.DEVICE + "\n"); - report.append("Brand: " + Build.BRAND + "\n"); - report.append("Model: "+Build.MODEL+"\n"); - report.append("Product: "+Build.PRODUCT+"\n"); - report.append("App:\t "+mApp.getPackageName()+", version "+pi.versionName+" (build "+pi.versionCode+")\n"); - report.append("Locale: "+mApp.getResources().getConfiguration().locale.getDisplayName()+"\n"); - report.append("-----------------------------\n\n"); - - report.append("--------- Firmware ---------\n\n"); - report.append("SDK: " + Build.VERSION.SDK + "\n"); - report.append("Release: " + Build.VERSION.RELEASE + "\n"); - report.append("Incremental: " + Build.VERSION.INCREMENTAL + "\n"); - report.append("Build Id: " + Build.ID + "\n"); - report.append("-------------------------------\n\n"); - - // If the exception was thrown in a background thread inside - // AsyncTask, then the actual exception can be found with getCause - report.append("--------- Cause ---------\n\n"); - Throwable cause = aException.getCause(); - if (cause != null) { - report.append(cause.toString() + "\n\n"); - theStackTrace = cause.getStackTrace(); - for (int i = 0; i < theStackTrace.length; i++) { - report.append(" " + theStackTrace[i].toString() + "\n"); - } - } - report.append("-------------------------------\n\n"); - - report.append("--------- Complete Logcat ---------\n\n"); - report.append(getLog().toString()); - report.append("-------------------------------\n\n"); - - report.append("END REPORT"); - - return report.toString(); - } - - protected void saveDebugReport(String aReport) { - //save report to file - try { - FileOutputStream theFile = mApp.openFileOutput(ExceptionReportFilename, Context.MODE_PRIVATE); - theFile.write(aReport.getBytes()); - theFile.close(); - } catch(IOException ioe) { - //error during error report needs to be ignored, do not wish to start infinite loop - } - } - - public void sendDebugReportToAuthor() { - String theLine = ""; - StringBuffer theTrace = new StringBuffer(); - try { - BufferedReader theReader = new BufferedReader( - new InputStreamReader(mApp.openFileInput(ExceptionReportFilename))); - while ((theLine = theReader.readLine())!=null) { - theTrace.append(theLine+"\n"); - } - if (sendDebugReportToAuthor(theTrace.toString())) { - mApp.deleteFile(ExceptionReportFilename); - } - } catch (FileNotFoundException eFnf) { - // nothing to do - } catch(IOException eIo) { - // not going to report - } - } - - public Boolean sendDebugReportToAuthor(String aReport) { - if (aReport!=null) { - Intent theIntent = new Intent(Intent.ACTION_SEND); - String theSubject = mApp.getTitle()+" "+MSG_SUBJECT_TAG; - String theBody = "\n"+MSG_BODY+"\n\n"+aReport+"\n\n"; - theIntent.putExtra(Intent.EXTRA_EMAIL,new String[] {MSG_SENDTO}); - theIntent.putExtra(Intent.EXTRA_TEXT, theBody); - theIntent.putExtra(Intent.EXTRA_SUBJECT, theSubject); - theIntent.setType("message/rfc822"); - Boolean hasSendRecipients = (mApp.getPackageManager().queryIntentActivities(theIntent,0).size()>0); - if (hasSendRecipients) { - mApp.startActivity(theIntent); - return true; - } else { - return false; - } - } else { - return true; - } - } - - public void run() { - sendDebugReportToAuthor(); - } - - public void submit(Throwable e) { - String theErrReport = getDebugReport(e); - saveDebugReport(theErrReport); - //try to send file contents via email (need to do so via the UI thread) - mApp.runOnUiThread(this); - } - - protected StringBuilder getLog(){ - final StringBuilder log = new StringBuilder(); - try{ - Process process = Runtime.getRuntime().exec("logcat -d"); - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); - - String line; - while ((line = bufferedReader.readLine()) != null){ - log.append(line); - log.append("\n"); - } - } - catch (IOException e){ - } - return log; - } -} diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPost.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPost.java index e484055..b1a494a 100644 --- a/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPost.java +++ b/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPost.java @@ -20,6 +20,7 @@ import android.util.Log; import android.widget.RemoteViews; import com.TwentyCodes.android.Facebook.FacebookAuth; +import com.TwentyCodes.android.exception.ExceptionHandler; import com.TwentyCodes.android.location.LocationService; /** @@ -29,7 +30,7 @@ import com.TwentyCodes.android.location.LocationService; */ public class TravelPost extends PreferenceActivity implements OnPreferenceClickListener { - private PostMortemReportExceptionHandler mExceptionReport = new PostMortemReportExceptionHandler(this); + private ExceptionHandler mExceptionReport = new ExceptionHandler(this); private TwitterServices mTwitterServices = null; public static final String SETTINGS = "settings"; private static final int TWITTER_AUTH_REQUEST_CODE = 0; @@ -37,7 +38,6 @@ public class TravelPost extends PreferenceActivity implements OnPreferenceClickL public static final String SAVED_POST = "saved_post"; public static final String SAVE_A_POST = "save_a_post"; public static final String USERS_POST = "users_post"; - private FacebookAuth mFbAuth; /** * called when the web auth activity returns its result @@ -115,8 +115,8 @@ public class TravelPost extends PreferenceActivity implements OnPreferenceClickL e.printStackTrace(); } } else if (preference.getKey().equals("facebook_sign_in")) { - mFbAuth = new FacebookAuth(this, this); - mFbAuth.authorize(); + FacebookAuth fbAuth = new FacebookAuth(this, new Activity()); + fbAuth.authorize(); // try { // mFbAuth.postToWall("Another test"); // } catch (SessionNotValidException e) {