From 12da6fbd1818ce660684f1b8f33d4fb11ffbe7f0 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 23 Jun 2017 23:21:39 -0400 Subject: [PATCH] launch for main activity and catch exceptions --- src/Android/MainActivity.cs | 9 +++++++++ src/Android/MainApplication.cs | 17 +++------------- src/Android/Utilities.cs | 37 +++++++++++++++++++++++++++------- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/Android/MainActivity.cs b/src/Android/MainActivity.cs index 643a7b7ef..a461e67e7 100644 --- a/src/Android/MainActivity.cs +++ b/src/Android/MainActivity.cs @@ -15,6 +15,7 @@ using Xamarin.Forms; using System.Threading.Tasks; using Bit.App.Models.Page; using Bit.App; +using Android.Runtime; namespace Bit.Android { @@ -29,6 +30,8 @@ namespace Bit.Android protected override void OnCreate(Bundle bundle) { + AndroidEnvironment.UnhandledExceptionRaiser += AndroidEnvironment_UnhandledExceptionRaiser; + var uri = Intent.GetStringExtra("uri"); if(!Resolver.IsSet) { @@ -104,6 +107,12 @@ namespace Bit.Android }); } + private void AndroidEnvironment_UnhandledExceptionRaiser(object sender, RaiseThrowableEventArgs e) + { + var message = Utilities.AppendExceptionToMessage("", e.Exception); + Utilities.SendCrashEmail(this, e.Exception, true); + } + private void ReturnCredentials(VaultListPageModel.Login login) { Intent data = new Intent(); diff --git a/src/Android/MainApplication.cs b/src/Android/MainApplication.cs index b2a85201b..a2f9c9138 100644 --- a/src/Android/MainApplication.cs +++ b/src/Android/MainApplication.cs @@ -24,9 +24,9 @@ using SimpleInjector; namespace Bit.Android { #if DEBUG - [Application(Debuggable = true)] + //[Application(Debuggable = true)] #else - [Application(Debuggable = false)] + //[Application(Debuggable = false)] #endif public class MainApplication : Application, Application.IActivityLifecycleCallbacks { @@ -48,22 +48,11 @@ namespace Bit.Android private void AndroidEnvironment_UnhandledExceptionRaiser(object sender, RaiseThrowableEventArgs e) { - var message = AppendExceptionToMessage("", e.Exception); + var message = Utilities.AppendExceptionToMessage("", e.Exception); //Utilities.SaveCrashFile(message, true); Utilities.SendCrashEmail(message, false); } - private string AppendExceptionToMessage(string message, Exception ex) - { - message += ("\n\n" + ex.Message + "\n\n" + ex.StackTrace); - if(ex.InnerException != null) - { - return AppendExceptionToMessage(message, ex.InnerException); - } - - return message; - } - public override void OnCreate() { base.OnCreate(); diff --git a/src/Android/Utilities.cs b/src/Android/Utilities.cs index 0aa865d93..bc1714c18 100644 --- a/src/Android/Utilities.cs +++ b/src/Android/Utilities.cs @@ -13,6 +13,11 @@ namespace Bit.Android SendCrashEmail(e.Message + "\n\n" + e.StackTrace, includeSecurityProviders); } + public static void SendCrashEmail(Activity act, Exception e, bool includeSecurityProviders = true) + { + SendCrashEmail(act, e.Message + "\n\n" + e.StackTrace, includeSecurityProviders); + } + public static void SaveCrashFile(Exception e, bool includeSecurityProviders = true) { SaveCrashFile(e.Message + "\n\n" + e.StackTrace, includeSecurityProviders); @@ -30,15 +35,22 @@ namespace Bit.Android Application.Context.StartActivity(Intent.CreateChooser(emailIntent, "Send mail...")); } + public static void SendCrashEmail(Activity act, string text, bool includeSecurityProviders = true) + { + var emailIntent = new Intent(Intent.ActionSend); + + emailIntent.SetType("plain/text"); + emailIntent.PutExtra(Intent.ExtraEmail, new String[] { "hello@bitwarden.com" }); + emailIntent.PutExtra(Intent.ExtraSubject, "bitwarden Crash Report"); + emailIntent.PutExtra(Intent.ExtraText, FormatText(text, includeSecurityProviders)); + + act.StartActivity(Intent.CreateChooser(emailIntent, "Send mail...")); + } + public static void SaveCrashFile(string text, bool includeSecurityProviders = true) { - var path = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath; - var dir = Path.Combine(path, "bitwarden"); - if(!Directory.Exists(dir)) - { - Directory.CreateDirectory(dir); - } - var filename = Path.Combine(dir, $"crash-{Java.Lang.JavaSystem.CurrentTimeMillis()}.txt"); + var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal); + var filename = Path.Combine(path, $"crash-{Java.Lang.JavaSystem.CurrentTimeMillis()}.txt"); using(var streamWriter = new StreamWriter(filename, true)) { streamWriter.WriteLine(FormatText(text, includeSecurityProviders)); @@ -70,5 +82,16 @@ namespace Bit.Android text += "\n\n ==================================================== \n\n" + crashMessage; return text; } + + public static string AppendExceptionToMessage(string message, Exception ex) + { + message += ("\n\n" + ex.Message + "\n\n" + ex.StackTrace); + if(ex.InnerException != null) + { + return AppendExceptionToMessage(message, ex.InnerException); + } + + return message; + } } } \ No newline at end of file