start implementing notifications when searching, e.g. for Autofill

This commit is contained in:
Philipp Crocoll
2020-02-15 03:26:35 +01:00
parent 406723fab0
commit 7ba17b8552
5 changed files with 70 additions and 43 deletions

View File

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

View File

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

View File

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

View File

@@ -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;
}
}
/// <summary>
/// Implementation of AppTask for "no task currently active" (Null pattern)
@@ -474,21 +495,29 @@ namespace keepass2android
}
}
/// <summary>
/// User is about to select an entry for use in another app
/// </summary>
public class SelectEntryTask: AppTask
public enum ShowUserNotificationsMode
{
Never,
WhenTotp,
Always
}
/// <summary>
/// User is about to select an entry for use in another app
/// </summary>
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<IExtra> Extras
public override IEnumerable<IExtra> 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<string> 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);

View File

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