Jaison Brooks
Jaison Brooks

Reputation: 5826

Sending Crash Report via Email from Android App

hey guys i need help configuring some source code to basically when a error happens to send a crash report via email to me. Here is some code im playing with i just need someone with a little more experience with java to overlook it and help me clean it up.

Thanks,

public final class EmailUtils {

private EmailUtils()
 {
 }

private static int bytesToMb(long l)
 {
    return (int)((double)l / 1024D / 1024D);
 }

private static int getAvailableInternalMemorySize()
 {
    StatFs statfs = new StatFs(Environment.getDataDirectory().getPath());
    return bytesToMb((long)statfs.getBlockSize() * (long)statfs.getAvailableBlocks());
 }

private static String getEmailText(Context context)
 {
    String s = "";
    String s11;
    PackageInfo packageinfo =  context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
    String s1 = packageinfo.versionName;
    int i = packageinfo.versionCode;
    String s2 = packageinfo.packageName;
    String s3 = Build.MODEL;
    String s4 = android.os.Build.VERSION.RELEASE;
    String s5 = android.os.Build.VERSION.SDK;
    String s6 = Build.DEVICE;
    String s7 = Build.DISPLAY;
    String s8 = Build.ID;
    String s9 = (new  StringBuilder(String.valueOf(Build.BRAND))).append("/").append(Build.MANUFACTURER).toString();
    String s10 = Build.PRODUCT;
    s = (new StringBuilder(String.valueOf(s))).append("\n\n------------------\n").toString();
    s = (new StringBuilder(String.valueOf(s))).append("Version: ").append(s1).toString();
    s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
    s = (new StringBuilder(String.valueOf(s))).append("Version num: ").append(i).toString();
    s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
    s = (new StringBuilder(String.valueOf(s))).append("Package: ").append(s2).toString();
    s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
    s = (new StringBuilder(String.valueOf(s))).append("Phone Model: ").append(s3).toString();
    s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
    s = (new StringBuilder(String.valueOf(s))).append("Android Version: ").append(s4).toString();
    s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
    s = (new StringBuilder(String.valueOf(s))).append("SDK Version: ").append(s5).toString();
    s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
    s = (new StringBuilder(String.valueOf(s))).append("Device: ").append(s6).toString();
    s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
    s = (new StringBuilder(String.valueOf(s))).append("Product: ").append(s10).toString();
    s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
    s = (new StringBuilder(String.valueOf(s))).append("Brand: ").append(s9).toString();
    s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
    s = (new StringBuilder(String.valueOf(s))).append("Disp: ").append(s7).toString();
    s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
    s = (new StringBuilder(String.valueOf(s))).append("Id: ").append(s8).toString();
    s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
    s = (new StringBuilder(String.valueOf(s))).append("Available Internal memory: ").append(getAvailableInternalMemorySize()).append("/").append(getTotalInternalMemorySize()).append(" Mb").toString();
    s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
    s = (new StringBuilder(String.valueOf(s))).append("Heap size: ").append(getHeapSizeMb()).append(" Mb").toString();
    s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
    s = (new StringBuilder(String.valueOf(s))).append("Mar: GO").toString();
    s11 = (new StringBuilder(String.valueOf(s))).append("\n").toString();
    s = s11;

    android.content.pm.PackageManager.NameNotFoundException namenotfoundexception;

 //   namenotfoundexception.printStackTrace();
  //  if(true) 
  //    goto

}

private static int getHeapSizeMb()
 {
    return bytesToMb(Runtime.getRuntime().maxMemory());
 }

private static int getTotalInternalMemorySize()
 {
    StatFs statfs = new StatFs(Environment.getDataDirectory().getPath());
    return bytesToMb((long)statfs.getBlockSize() * (long)statfs.getBlockCount());
 }

public static void sendContactEmail(Context context)
 {
    String s = context.getResources().getString(0x7f040000);
    String s1 = "";
    String s2;
    String s3;
    Intent intent;
    try
    {
        s1 = context.getPackageManager().getPackageInfo(context.getPackageName(),  0).versionName;
    }
    catch(android.content.pm.PackageManager.NameNotFoundException  namenotfoundexception) { }
    s2 = (new StringBuilder("[SUPPORT] ")).append(s).append(" ").append(s1).toString();
    s3 = getEmailText(context);
    intent = new Intent("android.intent.action.SEND");
    intent.setType("plain/text");
    intent.putExtra("android.intent.extra.EMAIL", new String[] {
        "[email protected]"
    });
    intent.putExtra("android.intent.extra.SUBJECT", s2);
    intent.putExtra("android.intent.extra.TEXT", s3);
    if(context.getPackageManager().queryIntentActivities(intent, 0).size() > 0)
    {
        context.startActivity(Intent.createChooser(intent, "Send mail..."));
    } else
    {
        CustomAlertDialogBuilder customalertdialogbuilder = new CustomAlertDialogBuilder(context);
        customalertdialogbuilder.setTitle("Contact us");
        customalertdialogbuilder.setIcon(R.drawable.ic_launcher);
        customalertdialogbuilder.setMessage(Html.fromHtml("No application found to send emails.<br /><br />Please contact us at: <b>[email protected]</b>."));
        customalertdialogbuilder.setNegativeButton("Close", null);
        customalertdialogbuilder.show();
       }
    }
}

Upvotes: 2

Views: 1602

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006564

My Main goal is to pull system data and intent out. Do you recommend using the ACRA way or using my string method?

Definitely ACRA. Again, it already exists, is supported, offers multiple means of getting the data to you (including ways that do not disclose your email address), integrates with third-party services like BugSense, etc.

To be a bit more specific about two of my code comments:

  • Never never never never never never never hard-code a resource ID hex value, such as: String s = context.getResources().getString(0x7f040000);. Those values can change with each build, particularly when you add/remove resources. Always use the resource ID symbol (e.g., R.string.foo).

  • In getEmailText(), please create one StringBuilder object and append to it. Do not create 30 or so of them, using each once and discarding it.

Upvotes: 6

Related Questions