From 2c012d5f71e6f77d0e85af6e3d6223722526893e Mon Sep 17 00:00:00 2001 From: Philipp Crocoll Date: Wed, 13 Jan 2016 03:44:15 +0100 Subject: [PATCH] allow to configure Error report settings Fix issue in DatabaseSettings implementation (deleting a preference twice) --- src/Kp2aBusinessLogic/database/edit/LoadDB.cs | 3 +- src/keepass2android/KeePass.cs | 36 +++++++++- src/keepass2android/PasswordActivity.cs | 7 +- .../Resources/xml/preferences.xml | 6 +- src/keepass2android/app/App.cs | 69 ++++++++----------- .../settings/DatabaseSettingsActivity.cs | 63 +++++++++-------- 6 files changed, 109 insertions(+), 75 deletions(-) diff --git a/src/Kp2aBusinessLogic/database/edit/LoadDB.cs b/src/Kp2aBusinessLogic/database/edit/LoadDB.cs index d54f4693..b5fc06f3 100644 --- a/src/Kp2aBusinessLogic/database/edit/LoadDB.cs +++ b/src/Kp2aBusinessLogic/database/edit/LoadDB.cs @@ -107,7 +107,8 @@ namespace keepass2android } catch (Exception e) { - Kp2aLog.LogUnexpectedError(e); + if (!(e is InvalidCompositeKeyException)) + Kp2aLog.LogUnexpectedError(e); Finish(false, _app.GetResourceString(UiStringKey.ErrorOcurred) + " " + e.Message, Exception); return; } diff --git a/src/keepass2android/KeePass.cs b/src/keepass2android/KeePass.cs index c02eb6bd..96cf9f62 100644 --- a/src/keepass2android/KeePass.cs +++ b/src/keepass2android/KeePass.cs @@ -15,6 +15,7 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file along with Keepass2Android. If not, see . */ +using System; using Android.App; using Android.Content; using Android.Widget; @@ -73,7 +74,7 @@ namespace keepass2android /// [Activity(Label = AppNames.AppName, MainLauncher = false, Theme = "@style/MyTheme_Blue")] [IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { "android.intent.category.LAUNCHER", "android.intent.category.MULTIWINDOW_LAUNCHER" })] - public class KeePass : LifecycleDebugActivity + public class KeePass : LifecycleDebugActivity, IDialogInterfaceOnDismissListener { public const Result ExitNormal = Result.FirstUser; public const Result ExitLock = Result.FirstUser+1; @@ -184,13 +185,40 @@ namespace keepass2android } else { - LaunchNextActivity(); + var pref = PreferenceManager.GetDefaultSharedPreferences(this); + if ((pref.GetBoolean(App.PrefHaspendingerrorreport, false) + && (App.GetErrorReportMode(this) == App.ErrorReportMode.AskAgain)) + ) + { + ShowErrorReportQuestion(LaunchNextActivity); + } + else + LaunchNextActivity(); } } + private void ShowErrorReportQuestion(Action launchNextActivity) + { + AlertDialog.Builder b = new AlertDialog.Builder(this); + b.SetTitle(Resource.String.ErrorReportTitle); + b.SetMessage(GetString(Resource.String.ErrorReportText) + " " + GetString(Resource.String.ErrorReportPromise)); + b.SetPositiveButton(Resource.String.ErrorReportEnable, (sender, args) => + { + App.SetErrorReportMode(this, App.ErrorReportMode.Enabled); + launchNextActivity(); + }); + b.SetNegativeButton(Resource.String.ErrorReportDisable, (sender, args) => + { + App.SetErrorReportMode(this, App.ErrorReportMode.Disabled); + launchNextActivity(); + }); + b.SetOnDismissListener(this); + + b.Show(); + } private static String SELECT_RUNTIME_PROPERTY = "persist.sys.dalvik.vm.lib"; @@ -314,6 +342,10 @@ namespace keepass2android } + public void OnDismiss(IDialogInterface dialog) + { + LaunchNextActivity(); + } } } diff --git a/src/keepass2android/PasswordActivity.cs b/src/keepass2android/PasswordActivity.cs index 19ba4797..487e0f4e 100644 --- a/src/keepass2android/PasswordActivity.cs +++ b/src/keepass2android/PasswordActivity.cs @@ -1008,11 +1008,14 @@ namespace keepass2android { _appnameclickCount++; if (_appnameclickCount == 6) + { + Kp2aLog.LogUnexpectedError(new Exception("some blabla")); Toast.MakeText(this, "Once again and the app will crash.", ToastLength.Long).Show(); + } + if (_appnameclickCount == 7) { - Xamarin.Insights.Report(new Exception("blabla"), new Dictionary() { { "key", "the value"}}); - throw new Exception("This is an easter egg crash (for testing unhandled exceptions.)"); + throw new Exception("this is an easter egg crash (to test uncaught exceptions."); } diff --git a/src/keepass2android/Resources/xml/preferences.xml b/src/keepass2android/Resources/xml/preferences.xml index 1d93ba66..52cb05c3 100644 --- a/src/keepass2android/Resources/xml/preferences.xml +++ b/src/keepass2android/Resources/xml/preferences.xml @@ -456,7 +456,11 @@ android:key="@string/TrayTotp_SettingsField_key" /> - + + diff --git a/src/keepass2android/app/App.cs b/src/keepass2android/app/App.cs index 02a53b21..a1e00b9d 100644 --- a/src/keepass2android/app/App.cs +++ b/src/keepass2android/app/App.cs @@ -755,9 +755,8 @@ namespace keepass2android #endif #endif public class App : Application { - public const string Kp2AActionDisableerrorreport = "kp2a.action.DisableErrorReport"; - public const string Kp2AActionEnableerrorreport = "kp2a.action.EnableErrorReport"; public const string PrefErrorreportmode = "pref_ErrorReportMode"; + public const string PrefHaspendingerrorreport = "pref_hasPendingErrorReport"; public App (IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) @@ -782,39 +781,27 @@ namespace keepass2android AndroidEnvironment.UnhandledExceptionRaiser += MyApp_UnhandledExceptionHandler; Kp2aLog.OnUnexpectedError += (sender, exception) => { - var currentErrorReportMode = GetErrorReportMode(); - if (currentErrorReportMode == ErrorReportMode.AskAgain) + var currentErrorReportMode = GetErrorReportMode(ApplicationContext); + if (currentErrorReportMode != ErrorReportMode.Disabled) { - NotificationCompat.Builder builder = new NotificationCompat.Builder(ApplicationContext); - var notification = - builder.SetContentTitle(Resources.GetString(Resource.String.ErrorReportTitle)) - .SetContentText(Resources.GetString(Resource.String.ErrorReportText) + " " + - Resources.GetString(Resource.String.ErrorReportPromise)) - .AddAction(Android.Resource.Drawable.IcMenuCloseClearCancel, Resource.String.ErrorReportDisable, - PendingIntent.GetBroadcast(ApplicationContext, 0, new Intent(Kp2AActionDisableerrorreport), - PendingIntentFlags.CancelCurrent)) - .AddAction(Android.Resource.Drawable.IcMenuCloseClearCancel, Resource.String.ErrorReportEnable, - PendingIntent.GetBroadcast(ApplicationContext, 0, new Intent(Kp2AActionEnableerrorreport), - PendingIntentFlags.CancelCurrent)) - .Build(); - ((NotificationManager) GetSystemService(NotificationService)).Notify(18919, notification); - - var filter = new IntentFilter(); - filter.AddAction(Kp2AActionDisableerrorreport); - filter.AddAction(Kp2AActionEnableerrorreport); - RegisterReceiver(new ErrorReportSettingsReceiver(), filter); + Xamarin.Insights.Report(exception); + if (Xamarin.Insights.DisableDataTransmission) + { + PreferenceManager.GetDefaultSharedPreferences(ApplicationContext) + .Edit().PutBoolean(PrefHaspendingerrorreport, true).Commit(); + } } }; Xamarin.Insights.Initialize("fed2b273ed2a964d0ba6acc3743e68f7a04da957", ApplicationContext); Xamarin.Insights.DisableExceptionCatching = true; - var errorReportMode = GetErrorReportMode(); - Xamarin.Insights.DisableDataTransmission = errorReportMode != ErrorReportMode.Enabled; + var errorReportMode = GetErrorReportMode(ApplicationContext); + SetErrorReportMode(ApplicationContext, errorReportMode); } - private ErrorReportMode GetErrorReportMode() + public static ErrorReportMode GetErrorReportMode(Context ctx) { ErrorReportMode errorReportMode; - Enum.TryParse(PreferenceManager.GetDefaultSharedPreferences(this.ApplicationContext) + Enum.TryParse(PreferenceManager.GetDefaultSharedPreferences(ctx) .GetString(PrefErrorreportmode, ErrorReportMode.AskAgain.ToString()), out errorReportMode); return errorReportMode; } @@ -825,7 +812,7 @@ namespace keepass2android Kp2aLog.LogUnexpectedError(e.Exception); Xamarin.Insights.Save(); // Do your error handling here. - throw e.Exception; + //throw e.Exception; } protected override void Dispose(bool disposing) @@ -840,26 +827,24 @@ namespace keepass2android Kp2a.OnTerminate(); } - - } - public class ErrorReportSettingsReceiver : BroadcastReceiver - { - public override void OnReceive(Context context, Intent intent) + public static void SetErrorReportMode(Context ctx, ErrorReportMode mode) { - if (intent.Action == App.Kp2AActionDisableerrorreport) + Xamarin.Insights.DisableCollection = (mode == ErrorReportMode.Disabled); + Xamarin.Insights.DisableDataTransmission = mode != ErrorReportMode.Enabled; + + var pref = PreferenceManager.GetDefaultSharedPreferences(ctx); + var edit = pref.Edit(); + if (mode != ErrorReportMode.AskAgain) { - PreferenceManager.GetDefaultSharedPreferences(context) - .Edit() - .PutString(App.PrefErrorreportmode, App.ErrorReportMode.Disabled.ToString()); - } - if (intent.Action == App.Kp2AActionEnableerrorreport) - { - PreferenceManager.GetDefaultSharedPreferences(context) - .Edit() - .PutString(App.PrefErrorreportmode, App.ErrorReportMode.Enabled.ToString()); + edit.PutBoolean(PrefHaspendingerrorreport, false); } + edit.PutString(PrefErrorreportmode, mode.ToString()); + edit.Commit(); + + } } + } diff --git a/src/keepass2android/settings/DatabaseSettingsActivity.cs b/src/keepass2android/settings/DatabaseSettingsActivity.cs index ef47d230..10715896 100644 --- a/src/keepass2android/settings/DatabaseSettingsActivity.cs +++ b/src/keepass2android/settings/DatabaseSettingsActivity.cs @@ -406,26 +406,15 @@ namespace keepass2android try { - //depending on Android version, we offer to use a transparent icon for QuickUnlock or use the notification priority (since API level 16) - Preference hideQuickUnlockTranspIconPref = FindPreference(GetString(Resource.String.QuickUnlockIconHidden_key)); - Preference hideQuickUnlockIconPref = FindPreference(GetString(Resource.String.QuickUnlockIconHidden16_key)); - var quickUnlockScreen = ((PreferenceScreen)FindPreference(GetString(Resource.String.QuickUnlock_prefs_key))); - if ((int)Android.OS.Build.VERSION.SdkInt >= 16) - { - quickUnlockScreen.RemovePreference(hideQuickUnlockTranspIconPref); - FindPreference(GetString(Resource.String.ShowUnlockedNotification_key)).PreferenceChange += (sender, args) => App.Kp2a.UpdateOngoingNotification(); - hideQuickUnlockIconPref.PreferenceChange += OnQuickUnlockHiddenChanged; - } - else - { - //old version: only show transparent quickUnlock and no option to hide unlocked icon: - quickUnlockScreen.RemovePreference(hideQuickUnlockIconPref); - FindPreference(GetString(Resource.String.QuickUnlockIconHidden_key)).PreferenceChange += - delegate { App.Kp2a.UpdateOngoingNotification(); }; - ((PreferenceScreen)FindPreference(GetString(Resource.String.display_prefs_key))).RemovePreference( - FindPreference(GetString(Resource.String.ShowUnlockedNotification_key))); - } - } + + var errorReportModePref = (ListPreference)FindPreference(App.PrefErrorreportmode); + +#if NoNet + ((PreferenceScreen)FindPreference(Resource.String.app_key)).RemovePreference(errorReportModePref); +#else + SetupErrorReportModePref(errorReportModePref); +#endif + } catch (Exception ex) { Kp2aLog.LogUnexpectedError(ex); @@ -435,6 +424,33 @@ namespace keepass2android } + private void SetupErrorReportModePref(ListPreference errorReportModePref) + { + errorReportModePref.SetEntries(new string[] + { + GetString(Resource.String.ErrorReportEnable), + GetString(Resource.String.ErrorReportAsk), + GetString(Resource.String.ErrorReportDisable) + }); + var entryValues = new string[] + { + App.ErrorReportMode.Enabled.ToString(), + App.ErrorReportMode.AskAgain.ToString(), + App.ErrorReportMode.Disabled.ToString(), + }; + errorReportModePref.SetEntryValues(entryValues); + errorReportModePref.SetDefaultValue(App.ErrorReportMode.Disabled.ToString()); + string currentValue = PreferenceManager.GetDefaultSharedPreferences(Activity) + .GetString(App.PrefErrorreportmode, App.ErrorReportMode.Disabled.ToString()); + errorReportModePref.SetValueIndex(entryValues.Select((v, index) => new {value = v, index}).First(el => el.value == currentValue).index); + errorReportModePref.PreferenceChange += (sender, args) => + { + App.ErrorReportMode mode; + Enum.TryParse((string) args.NewValue, out mode); + App.SetErrorReportMode(Activity, mode); + }; + } + private void PrepareTemplates(Database db) { Preference pref = FindPreference("AddTemplates_pref_key"); @@ -557,13 +573,6 @@ namespace keepass2android } } - - - private void OnQuickUnlockHiddenChanged(object sender, Preference.PreferenceChangeEventArgs e) - { - App.Kp2a.UpdateOngoingNotification(); - } - private void OnUseOfflineCacheChanged(object sender, Preference.PreferenceChangeEventArgs e) { //ensure the user gets a matching database