allow to configure Error report settings

Fix issue in DatabaseSettings implementation (deleting a preference twice)
This commit is contained in:
Philipp Crocoll 2016-01-13 03:44:15 +01:00
parent 82b288ae71
commit 2c012d5f71
6 changed files with 109 additions and 75 deletions

View File

@ -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;
}

View File

@ -15,6 +15,7 @@ This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file
along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using Android.App;
using Android.Content;
using Android.Widget;
@ -73,7 +74,7 @@ namespace keepass2android
/// </summary>
[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();
}
}
}

View File

@ -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<string, string>() { { "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.");
}

View File

@ -456,6 +456,10 @@
android:key="@string/TrayTotp_SettingsField_key" />
</PreferenceScreen>
<ListPreference
android:key="pref_ErrorReportMode"
android:title="@string/ErrorReportPrefTitle"
android:summary="@string/ErrorReportPromise"/>
</PreferenceScreen>

View File

@ -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)
@ -841,25 +828,23 @@ namespace keepass2android
}
}
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();
}
}
}

View File

@ -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