code simplification and fix for Autofill not being able to save credentials, closes https://github.com/PhilippC/keepass2android/issues/2269

This commit is contained in:
Philipp Crocoll
2023-03-16 20:27:22 +01:00
parent 6f10a04589
commit 6110166af8
5 changed files with 30 additions and 19 deletions

View File

@@ -134,10 +134,10 @@ namespace Kp2aAutofillParser
/// </summary>
public class FilledAutofillFieldCollection<FieldT> where FieldT:InputField
{
public Dictionary<string, FilledAutofillField<FieldT>> HintMap { get; }
public Dictionary<string, FilledAutofillField> HintMap { get; }
public string DatasetName { get; set; }
public FilledAutofillFieldCollection(Dictionary<string, FilledAutofillField<FieldT>> hintMap, string datasetName = "")
public FilledAutofillFieldCollection(Dictionary<string, FilledAutofillField> hintMap, string datasetName = "")
{
//recreate hint map making sure we compare case insensitive
HintMap = BuildHintMap();
@@ -149,9 +149,9 @@ namespace Kp2aAutofillParser
public FilledAutofillFieldCollection() : this(BuildHintMap())
{ }
private static Dictionary<string, FilledAutofillField<FieldT>> BuildHintMap()
private static Dictionary<string, FilledAutofillField> BuildHintMap()
{
return new Dictionary<string, FilledAutofillField<FieldT>>(StringComparer.OrdinalIgnoreCase);
return new Dictionary<string, FilledAutofillField>(StringComparer.OrdinalIgnoreCase);
}
/// <summary>
@@ -159,7 +159,7 @@ namespace Kp2aAutofillParser
/// </summary>
/// <returns>The add.</returns>
/// <param name="filledAutofillField">Filled autofill field.</param>
public void Add(FilledAutofillField<FieldT> filledAutofillField)
public void Add(FilledAutofillField filledAutofillField)
{
foreach (string hint in filledAutofillField.AutofillHints)
{
@@ -572,7 +572,7 @@ namespace Kp2aAutofillParser
}
}
public class FilledAutofillField<FieldT> where FieldT : InputField
public class FilledAutofillField
{
private string[] _autofillHints;
public string TextValue { get; set; }
@@ -611,13 +611,13 @@ namespace Kp2aAutofillParser
public FilledAutofillField()
{ }
public FilledAutofillField(FieldT inputField)
public FilledAutofillField(InputField inputField)
: this(inputField, inputField.AutofillHints)
{
}
public FilledAutofillField(FieldT inputField, string[] hints)
public FilledAutofillField(InputField inputField, string[] hints)
{
string[] rawHints = AutofillHintsHelper.FilterForSupportedHints(hints);
@@ -665,6 +665,7 @@ namespace Kp2aAutofillParser
}
}
AutofillHints = AutofillHintsHelper.ConvertToCanonicalLowerCaseHints(hintList.ToArray()).ToArray();
inputField.FillFilledAutofillValue(this);
}
@@ -679,7 +680,7 @@ namespace Kp2aAutofillParser
if (this == obj) return true;
if (obj == null || GetType() != obj.GetType()) return false;
FilledAutofillField<FieldT> that = (FilledAutofillField<FieldT>)obj;
FilledAutofillField that = (FilledAutofillField)obj;
if (!TextValue?.Equals(that.TextValue) ?? that.TextValue != null)
return false;
@@ -717,6 +718,8 @@ namespace Kp2aAutofillParser
public string HtmlInfoTag { get; set; }
public string HtmlInfoTypeAttribute { get; set; }
public abstract void FillFilledAutofillValue(FilledAutofillField filledField);
}
/// <summary>

View File

@@ -18,6 +18,9 @@ namespace Kp2aAutofillParserTest
class TestInputField: InputField
{
public string[] ExpectedAssignedHints { get; set; }
public override void FillFilledAutofillValue(FilledAutofillField filledField)
{
}
}
[Fact]

View File

@@ -142,7 +142,7 @@ namespace keepass2android.services.AutofillBase
{
foreach (AutofillFieldMetadata autofillFieldMetadata in autofillFieldMetadataCollection.GetFieldsForHint(hint))
{
FilledAutofillField<ViewNodeInputField> filledAutofillField;
FilledAutofillField filledAutofillField;
if (!filledAutofillFieldCollection.HintMap.TryGetValue(hint, out filledAutofillField) || (filledAutofillField == null))
{
continue;

View File

@@ -29,7 +29,7 @@ namespace keepass2android.services.AutofillBase
[JsonIgnore]
public AssistStructure.ViewNode ViewNode { get; set; }
public void FillFilledAutofillValue(FilledAutofillField<ViewNodeInputField> filledField)
public override void FillFilledAutofillValue(FilledAutofillField filledField)
{
AutofillValue autofillValue = ViewNode.AutofillValue;
if (autofillValue != null)
@@ -159,8 +159,12 @@ namespace keepass2android.services.AutofillBase
protected override AutofillTargetId Parse(bool forFill, bool isManualRequest, AutofillView<ViewNodeInputField> autofillView)
{
if (autofillView == null)
Kp2aLog.Log("Received null autofill view!");
var result = base.Parse(forFill, isManualRequest, autofillView);
Kp2aLog.Log("Parsing done");
if (forFill)
{
foreach (var p in FieldsMappedToHints)
@@ -168,8 +172,9 @@ namespace keepass2android.services.AutofillBase
}
else
{
ClientFormData = new FilledAutofillFieldCollection<ViewNodeInputField>();
foreach (var p in FieldsMappedToHints)
ClientFormData.Add(new FilledAutofillField<ViewNodeInputField>(p.Key, p.Value));
ClientFormData.Add(new FilledAutofillField(p.Key, p.Value));
}

View File

@@ -61,8 +61,8 @@ namespace keepass2android.services.Kp2aAutofill
foreach (string key in pwEntryOutput.OutputStrings.GetKeys())
{
FilledAutofillField<ViewNodeInputField> field =
new FilledAutofillField<ViewNodeInputField>
FilledAutofillField field =
new FilledAutofillField
{
AutofillHints = GetCanonicalHintsFromKp2aField(key).ToArray(),
TextValue = pwEntryOutput.OutputStrings.ReadSafe(key)
@@ -73,8 +73,8 @@ namespace keepass2android.services.Kp2aAutofill
if (IsCreditCard(pwEntry, context) && pwEntry.Expires)
{
DateTime expTime = pwEntry.ExpiryTime;
FilledAutofillField<ViewNodeInputField> field =
new FilledAutofillField<ViewNodeInputField>
FilledAutofillField field =
new FilledAutofillField
{
AutofillHints = new[] {View.AutofillHintCreditCardExpirationDate},
DateValue = (long) (1000 * TimeUtil.SerializeUnix(expTime))
@@ -82,7 +82,7 @@ namespace keepass2android.services.Kp2aAutofill
fieldCollection.Add(field);
field =
new FilledAutofillField<ViewNodeInputField>
new FilledAutofillField
{
AutofillHints = new[] {View.AutofillHintCreditCardExpirationDay},
TextValue = expTime.Day.ToString()
@@ -90,7 +90,7 @@ namespace keepass2android.services.Kp2aAutofill
fieldCollection.Add(field);
field =
new FilledAutofillField<ViewNodeInputField>
new FilledAutofillField
{
AutofillHints = new[] {View.AutofillHintCreditCardExpirationMonth},
TextValue = expTime.Month.ToString()
@@ -98,7 +98,7 @@ namespace keepass2android.services.Kp2aAutofill
fieldCollection.Add(field);
field =
new FilledAutofillField<ViewNodeInputField>
new FilledAutofillField
{
AutofillHints = new[] {View.AutofillHintCreditCardExpirationYear},
TextValue = expTime.Year.ToString()