diff --git a/src/keepass2android/QueryCredentialsActivity.cs b/src/keepass2android/QueryCredentialsActivity.cs index fcc9ecc7..f0b8ebb8 100644 --- a/src/keepass2android/QueryCredentialsActivity.cs +++ b/src/keepass2android/QueryCredentialsActivity.cs @@ -139,7 +139,7 @@ namespace keepass2android //will return the results later Intent i = new Intent(this, typeof (SelectCurrentDbActivity)); //don't show user notifications when an entry is opened. - var task = new SearchUrlTask() {UrlToSearchFor = _requestedUrl, ShowUserNotifications = false}; + var task = new SearchUrlTask() {UrlToSearchFor = _requestedUrl, ShowUserNotifications = ShowUserNotificationsMode.WhenTotp}; task.ToIntent(i); StartActivityForResult(i, RequestCodeQuery); _startedQuery = true; diff --git a/src/keepass2android/ShareUrlResults.cs b/src/keepass2android/ShareUrlResults.cs index e4c8961d..b7b134de 100644 --- a/src/keepass2android/ShareUrlResults.cs +++ b/src/keepass2android/ShareUrlResults.cs @@ -149,7 +149,7 @@ namespace keepass2android createUrlEntry.Visibility = ViewStates.Visible; createUrlEntry.Click += (sender, e) => { - GroupActivity.Launch(this, new CreateEntryThenCloseTask { Url = url, ShowUserNotifications = (AppTask as SelectEntryTask)?.ShowUserNotifications ?? true }, new ActivityLaunchModeRequestCode(0)); + GroupActivity.Launch(this, new CreateEntryThenCloseTask { Url = url, ShowUserNotifications = (AppTask as SelectEntryTask)?.ShowUserNotifications ?? ShowUserNotificationsMode.Always }, new ActivityLaunchModeRequestCode(0)); Toast.MakeText(this, GetString(Resource.String.select_group_then_add, new Java.Lang.Object[] { GetString(Resource.String.add_entry) }), ToastLength.Long).Show(); }; } diff --git a/src/keepass2android/Totp/Kp2aTotp.cs b/src/keepass2android/Totp/Kp2aTotp.cs index 462a5575..b8f39424 100644 --- a/src/keepass2android/Totp/Kp2aTotp.cs +++ b/src/keepass2android/Totp/Kp2aTotp.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Text; using Android.App; +using KeePassLib; using KeePassLib.Utility; using PluginTOTP; @@ -12,18 +13,27 @@ namespace keepass2android readonly ITotpPluginAdapter[] _pluginAdapters = new ITotpPluginAdapter[] { new TrayTotpPluginAdapter(), new KeeOtpPluginAdapter(), new KeeWebOtpPluginAdapter() }; + public ITotpPluginAdapter TryGetAdapter(PwEntryOutput entry) + { + if (entry == null) + return null; + foreach (ITotpPluginAdapter adapter in _pluginAdapters) + { + TotpData totpData = adapter.GetTotpData(App.Kp2a.LastOpenedEntry.OutputStrings.ToDictionary(pair => StrUtil.SafeXmlString(pair.Key), pair => pair.Value.ReadString()), Application.Context, false); + if (totpData.IsTotpEnry) + { + return adapter; + } + } + + return null; + } + public void OnOpenEntry() { - if (App.Kp2a.LastOpenedEntry == null) - return; - foreach (ITotpPluginAdapter adapter in _pluginAdapters) - { - TotpData totpData = adapter.GetTotpData(App.Kp2a.LastOpenedEntry.OutputStrings.ToDictionary(pair => StrUtil.SafeXmlString(pair.Key), pair => pair.Value.ReadString()), Application.Context, false); - if (totpData.IsTotpEnry) - { - new UpdateTotpTimerTask(Application.Context, adapter).Run(); - } - } - } + var adapter = TryGetAdapter(App.Kp2a.LastOpenedEntry); + if (adapter != null) + new UpdateTotpTimerTask(Application.Context, adapter).Run(); + } } } diff --git a/src/keepass2android/app/AppTask.cs b/src/keepass2android/app/AppTask.cs index 8417917e..eff32244 100644 --- a/src/keepass2android/app/AppTask.cs +++ b/src/keepass2android/app/AppTask.cs @@ -348,7 +348,28 @@ namespace keepass2android { } - } + + + protected static bool GetBoolFromBundle(Bundle b, string key, bool defaultValue) + { + bool boolValue; + if (!Boolean.TryParse(b.GetString(key), out boolValue)) + { + boolValue = defaultValue; + } + return boolValue; + } + + protected static int GetIntFromBundle(Bundle b, string key, int defaultValue) + { + int intValue; + if (!Int32.TryParse(b.GetString(key), out intValue)) + { + intValue = defaultValue; + } + return intValue; + } + } /// /// Implementation of AppTask for "no task currently active" (Null pattern) @@ -474,21 +495,29 @@ namespace keepass2android } } - - /// - /// User is about to select an entry for use in another app - /// - public class SelectEntryTask: AppTask + + public enum ShowUserNotificationsMode + { + Never, + WhenTotp, + Always + } + /// + /// User is about to select an entry for use in another app + /// + public class SelectEntryTask: AppTask { public SelectEntryTask() { - ShowUserNotifications = true; + ShowUserNotifications = ShowUserNotificationsMode.Always; CloseAfterCreate = true; } public const String ShowUserNotificationsKey = "ShowUserNotifications"; - public bool ShowUserNotifications { get; set; } + + + public ShowUserNotificationsMode ShowUserNotifications { get; set; } public const String CloseAfterCreateKey = "CloseAfterCreate"; @@ -497,25 +526,16 @@ namespace keepass2android public override void Setup(Bundle b) { - ShowUserNotifications = GetBoolFromBundle(b, ShowUserNotificationsKey, true); + ShowUserNotifications = (ShowUserNotificationsMode) GetIntFromBundle(b, ShowUserNotificationsKey, (int)ShowUserNotificationsMode.Always); CloseAfterCreate = GetBoolFromBundle(b, CloseAfterCreateKey, true); } - private static bool GetBoolFromBundle(Bundle b, string key, bool defaultValue) - { - bool boolValue; - if (!Boolean.TryParse(b.GetString(key), out boolValue)) - { - boolValue = defaultValue; - } - return boolValue; - } - public override IEnumerable Extras + public override IEnumerable Extras { get { - yield return new StringExtra { Key = ShowUserNotificationsKey, Value = ShowUserNotifications.ToString() }; + yield return new StringExtra { Key = ShowUserNotificationsKey, Value = ((int)ShowUserNotifications).ToString() }; yield return new StringExtra { Key = CloseAfterCreateKey, Value = CloseAfterCreate.ToString() }; } } @@ -526,7 +546,8 @@ namespace keepass2android if (ctx == null) ctx = Application.Context; - if (ShowUserNotifications) + if ((ShowUserNotifications == ShowUserNotificationsMode.Always) + || ((ShowUserNotifications == ShowUserNotificationsMode.WhenTotp) && new Kp2aTotp().TryGetAdapter(new PwEntryOutput(activity.Entry, App.Kp2a.CurrentDb)) != null)) { //show the notifications activity.StartNotificationsService(CloseAfterCreate); @@ -604,7 +625,7 @@ namespace keepass2android { public CreateEntryThenCloseTask() { - ShowUserNotifications = true; + ShowUserNotifications = ShowUserNotificationsMode.Always; } public override bool CanActivateSearchViewOnStart @@ -644,17 +665,13 @@ namespace keepass2android public IList ProtectedFieldsList { get; set; } - public bool ShowUserNotifications { get; set; } + public ShowUserNotificationsMode ShowUserNotifications { get; set; } public override void Setup(Bundle b) { - bool showUserNotification; - if (!Boolean.TryParse(b.GetString(ShowUserNotificationsKey), out showUserNotification)) - { - showUserNotification = true; //default to true - } - ShowUserNotifications = showUserNotification; + + ShowUserNotifications = (ShowUserNotificationsMode)GetIntFromBundle(b,ShowUserNotificationsKey, (int)ShowUserNotificationsMode.Always); Url = b.GetString(UrlKey); AllFields = b.GetString(AllFieldsKey); diff --git a/src/keepass2android/services/Kp2aAutofill/ChooseForAutofillActivity.cs b/src/keepass2android/services/Kp2aAutofill/ChooseForAutofillActivity.cs index cd16021e..b9a3ff77 100644 --- a/src/keepass2android/services/Kp2aAutofill/ChooseForAutofillActivity.cs +++ b/src/keepass2android/services/Kp2aAutofill/ChooseForAutofillActivity.cs @@ -30,7 +30,7 @@ namespace keepass2android.services.Kp2aAutofill //will return the results later Intent i = new Intent(this, typeof(SelectCurrentDbActivity)); //don't show user notifications when an entry is opened. - var task = new SearchUrlTask() { UrlToSearchFor = requestedUrl, ShowUserNotifications = false, AutoReturnFromQuery = autoReturnFromQuery }; + var task = new SearchUrlTask() { UrlToSearchFor = requestedUrl, ShowUserNotifications = ShowUserNotificationsMode.WhenTotp, AutoReturnFromQuery = autoReturnFromQuery }; task.ToIntent(i); return i; }