launch for main activity and catch exceptions

This commit is contained in:
Kyle Spearrin 2017-06-23 23:21:39 -04:00
parent 573ff15925
commit 12da6fbd18
3 changed files with 42 additions and 21 deletions

View file

@ -15,6 +15,7 @@ using Xamarin.Forms;
using System.Threading.Tasks; using System.Threading.Tasks;
using Bit.App.Models.Page; using Bit.App.Models.Page;
using Bit.App; using Bit.App;
using Android.Runtime;
namespace Bit.Android namespace Bit.Android
{ {
@ -29,6 +30,8 @@ namespace Bit.Android
protected override void OnCreate(Bundle bundle) protected override void OnCreate(Bundle bundle)
{ {
AndroidEnvironment.UnhandledExceptionRaiser += AndroidEnvironment_UnhandledExceptionRaiser;
var uri = Intent.GetStringExtra("uri"); var uri = Intent.GetStringExtra("uri");
if(!Resolver.IsSet) 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) private void ReturnCredentials(VaultListPageModel.Login login)
{ {
Intent data = new Intent(); Intent data = new Intent();

View file

@ -24,9 +24,9 @@ using SimpleInjector;
namespace Bit.Android namespace Bit.Android
{ {
#if DEBUG #if DEBUG
[Application(Debuggable = true)] //[Application(Debuggable = true)]
#else #else
[Application(Debuggable = false)] //[Application(Debuggable = false)]
#endif #endif
public class MainApplication : Application, Application.IActivityLifecycleCallbacks public class MainApplication : Application, Application.IActivityLifecycleCallbacks
{ {
@ -48,22 +48,11 @@ namespace Bit.Android
private void AndroidEnvironment_UnhandledExceptionRaiser(object sender, RaiseThrowableEventArgs e) private void AndroidEnvironment_UnhandledExceptionRaiser(object sender, RaiseThrowableEventArgs e)
{ {
var message = AppendExceptionToMessage("", e.Exception); var message = Utilities.AppendExceptionToMessage("", e.Exception);
//Utilities.SaveCrashFile(message, true); //Utilities.SaveCrashFile(message, true);
Utilities.SendCrashEmail(message, false); 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() public override void OnCreate()
{ {
base.OnCreate(); base.OnCreate();

View file

@ -13,6 +13,11 @@ namespace Bit.Android
SendCrashEmail(e.Message + "\n\n" + e.StackTrace, includeSecurityProviders); 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) public static void SaveCrashFile(Exception e, bool includeSecurityProviders = true)
{ {
SaveCrashFile(e.Message + "\n\n" + e.StackTrace, includeSecurityProviders); SaveCrashFile(e.Message + "\n\n" + e.StackTrace, includeSecurityProviders);
@ -30,15 +35,22 @@ namespace Bit.Android
Application.Context.StartActivity(Intent.CreateChooser(emailIntent, "Send mail...")); 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) public static void SaveCrashFile(string text, bool includeSecurityProviders = true)
{ {
var path = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath; var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var dir = Path.Combine(path, "bitwarden"); var filename = Path.Combine(path, $"crash-{Java.Lang.JavaSystem.CurrentTimeMillis()}.txt");
if(!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
var filename = Path.Combine(dir, $"crash-{Java.Lang.JavaSystem.CurrentTimeMillis()}.txt");
using(var streamWriter = new StreamWriter(filename, true)) using(var streamWriter = new StreamWriter(filename, true))
{ {
streamWriter.WriteLine(FormatText(text, includeSecurityProviders)); streamWriter.WriteLine(FormatText(text, includeSecurityProviders));
@ -70,5 +82,16 @@ namespace Bit.Android
text += "\n\n ==================================================== \n\n" + crashMessage; text += "\n\n ==================================================== \n\n" + crashMessage;
return text; 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;
}
} }
} }