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) catch (Exception e)
{ {
Kp2aLog.LogUnexpectedError(e); if (!(e is InvalidCompositeKeyException))
Kp2aLog.LogUnexpectedError(e);
Finish(false, _app.GetResourceString(UiStringKey.ErrorOcurred) + " " + e.Message, Exception); Finish(false, _app.GetResourceString(UiStringKey.ErrorOcurred) + " " + e.Message, Exception);
return; 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/>. along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System;
using Android.App; using Android.App;
using Android.Content; using Android.Content;
using Android.Widget; using Android.Widget;
@ -73,7 +74,7 @@ namespace keepass2android
/// </summary> /// </summary>
[Activity(Label = AppNames.AppName, MainLauncher = false, Theme = "@style/MyTheme_Blue")] [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" })] [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 ExitNormal = Result.FirstUser;
public const Result ExitLock = Result.FirstUser+1; public const Result ExitLock = Result.FirstUser+1;
@ -184,13 +185,40 @@ namespace keepass2android
} }
else 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"; 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++; _appnameclickCount++;
if (_appnameclickCount == 6) if (_appnameclickCount == 6)
{
Kp2aLog.LogUnexpectedError(new Exception("some blabla"));
Toast.MakeText(this, "Once again and the app will crash.", ToastLength.Long).Show(); Toast.MakeText(this, "Once again and the app will crash.", ToastLength.Long).Show();
}
if (_appnameclickCount == 7) 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 (to test uncaught exceptions.");
throw new Exception("This is an easter egg crash (for testing unhandled exceptions.)");
} }

View File

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

View File

@ -755,9 +755,8 @@ namespace keepass2android
#endif #endif
#endif #endif
public class App : Application { 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 PrefErrorreportmode = "pref_ErrorReportMode";
public const string PrefHaspendingerrorreport = "pref_hasPendingErrorReport";
public App (IntPtr javaReference, JniHandleOwnership transfer) public App (IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer) : base(javaReference, transfer)
@ -782,39 +781,27 @@ namespace keepass2android
AndroidEnvironment.UnhandledExceptionRaiser += MyApp_UnhandledExceptionHandler; AndroidEnvironment.UnhandledExceptionRaiser += MyApp_UnhandledExceptionHandler;
Kp2aLog.OnUnexpectedError += (sender, exception) => Kp2aLog.OnUnexpectedError += (sender, exception) =>
{ {
var currentErrorReportMode = GetErrorReportMode(); var currentErrorReportMode = GetErrorReportMode(ApplicationContext);
if (currentErrorReportMode == ErrorReportMode.AskAgain) if (currentErrorReportMode != ErrorReportMode.Disabled)
{ {
NotificationCompat.Builder builder = new NotificationCompat.Builder(ApplicationContext); Xamarin.Insights.Report(exception);
var notification = if (Xamarin.Insights.DisableDataTransmission)
builder.SetContentTitle(Resources.GetString(Resource.String.ErrorReportTitle)) {
.SetContentText(Resources.GetString(Resource.String.ErrorReportText) + " " + PreferenceManager.GetDefaultSharedPreferences(ApplicationContext)
Resources.GetString(Resource.String.ErrorReportPromise)) .Edit().PutBoolean(PrefHaspendingerrorreport, true).Commit();
.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.Initialize("fed2b273ed2a964d0ba6acc3743e68f7a04da957", ApplicationContext); Xamarin.Insights.Initialize("fed2b273ed2a964d0ba6acc3743e68f7a04da957", ApplicationContext);
Xamarin.Insights.DisableExceptionCatching = true; Xamarin.Insights.DisableExceptionCatching = true;
var errorReportMode = GetErrorReportMode(); var errorReportMode = GetErrorReportMode(ApplicationContext);
Xamarin.Insights.DisableDataTransmission = errorReportMode != ErrorReportMode.Enabled; SetErrorReportMode(ApplicationContext, errorReportMode);
} }
private ErrorReportMode GetErrorReportMode() public static ErrorReportMode GetErrorReportMode(Context ctx)
{ {
ErrorReportMode errorReportMode; ErrorReportMode errorReportMode;
Enum.TryParse(PreferenceManager.GetDefaultSharedPreferences(this.ApplicationContext) Enum.TryParse(PreferenceManager.GetDefaultSharedPreferences(ctx)
.GetString(PrefErrorreportmode, ErrorReportMode.AskAgain.ToString()), out errorReportMode); .GetString(PrefErrorreportmode, ErrorReportMode.AskAgain.ToString()), out errorReportMode);
return errorReportMode; return errorReportMode;
} }
@ -825,7 +812,7 @@ namespace keepass2android
Kp2aLog.LogUnexpectedError(e.Exception); Kp2aLog.LogUnexpectedError(e.Exception);
Xamarin.Insights.Save(); Xamarin.Insights.Save();
// Do your error handling here. // Do your error handling here.
throw e.Exception; //throw e.Exception;
} }
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
@ -841,25 +828,23 @@ namespace keepass2android
} }
} public static void SetErrorReportMode(Context ctx, ErrorReportMode mode)
public class ErrorReportSettingsReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{ {
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.PutBoolean(PrefHaspendingerrorreport, false);
.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.PutString(PrefErrorreportmode, mode.ToString());
edit.Commit();
} }
} }
} }

View File

@ -406,26 +406,15 @@ namespace keepass2android
try 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)); var errorReportModePref = (ListPreference)FindPreference(App.PrefErrorreportmode);
Preference hideQuickUnlockIconPref = FindPreference(GetString(Resource.String.QuickUnlockIconHidden16_key));
var quickUnlockScreen = ((PreferenceScreen)FindPreference(GetString(Resource.String.QuickUnlock_prefs_key))); #if NoNet
if ((int)Android.OS.Build.VERSION.SdkInt >= 16) ((PreferenceScreen)FindPreference(Resource.String.app_key)).RemovePreference(errorReportModePref);
{ #else
quickUnlockScreen.RemovePreference(hideQuickUnlockTranspIconPref); SetupErrorReportModePref(errorReportModePref);
FindPreference(GetString(Resource.String.ShowUnlockedNotification_key)).PreferenceChange += (sender, args) => App.Kp2a.UpdateOngoingNotification(); #endif
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)));
}
}
catch (Exception ex) catch (Exception ex)
{ {
Kp2aLog.LogUnexpectedError(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) private void PrepareTemplates(Database db)
{ {
Preference pref = FindPreference("AddTemplates_pref_key"); 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) private void OnUseOfflineCacheChanged(object sender, Preference.PreferenceChangeEventArgs e)
{ {
//ensure the user gets a matching database //ensure the user gets a matching database