From 1694b5d6fd5d7572fb1930b1fb44fe8b7b3801fa Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 16 Nov 2017 21:58:04 -0500 Subject: [PATCH] renaming things --- src/Android/Autofill/AutofillService.cs | 2 +- src/Android/Autofill/FilledField.cs | 37 ++++------ src/Android/Autofill/FilledFieldCollection.cs | 74 ++++++------------- src/Android/Autofill/Parser.cs | 37 +++------- 4 files changed, 48 insertions(+), 102 deletions(-) diff --git a/src/Android/Autofill/AutofillService.cs b/src/Android/Autofill/AutofillService.cs index 53787ec16..c522a3d43 100644 --- a/src/Android/Autofill/AutofillService.cs +++ b/src/Android/Autofill/AutofillService.cs @@ -76,7 +76,7 @@ namespace Bit.Android.Autofill var parser = new Parser(structure); parser.ParseForSave(); - var filledAutofillFieldCollection = parser.GetClientFormData(); + var filledAutofillFieldCollection = parser.FilledFieldCollection; //SaveFilledAutofillFieldCollection(filledAutofillFieldCollection); } } diff --git a/src/Android/Autofill/FilledField.cs b/src/Android/Autofill/FilledField.cs index 7b7a5076f..deffaddf0 100644 --- a/src/Android/Autofill/FilledField.cs +++ b/src/Android/Autofill/FilledField.cs @@ -5,48 +5,39 @@ namespace Bit.Android.Autofill { public class FilledField { - private IEnumerable _hints = null; - public FilledField() { } public FilledField(ViewNode node) { - _hints = AutofillHelpers.FilterForSupportedHints(node.GetAutofillHints()); - var autofillValue = node.AutofillValue; - if(autofillValue == null) + Hints = AutofillHelpers.FilterForSupportedHints(node.GetAutofillHints()); + + if(node.AutofillValue == null) { return; } - if(autofillValue.IsList) + if(node.AutofillValue.IsList) { var autofillOptions = node.GetAutofillOptions(); - int index = autofillValue.ListValue; if(autofillOptions != null && autofillOptions.Length > 0) { - TextValue = autofillOptions[index]; + TextValue = autofillOptions[node.AutofillValue.ListValue]; } } - else if(autofillValue.IsDate) + else if(node.AutofillValue.IsDate) { - DateValue = autofillValue.DateValue; + DateValue = node.AutofillValue.DateValue; } - else if(autofillValue.IsText) + else if(node.AutofillValue.IsText) { - // Using toString of AutofillValue.getTextValue in order to save it to - // SharedPreferences. - TextValue = autofillValue.TextValue; + TextValue = node.AutofillValue.TextValue; } } public string TextValue { get; set; } public long? DateValue { get; set; } public bool? ToggleValue { get; set; } - - public IEnumerable GetHints() - { - return _hints; - } + public List Hints { get; set; } public bool IsNull() { @@ -65,18 +56,18 @@ namespace Bit.Android.Autofill return false; } - var that = o as FilledField; - if(TextValue != null ? !TextValue.Equals(that.TextValue) : that.TextValue != null) + var field = o as FilledField; + if(TextValue != null ? !TextValue.Equals(field.TextValue) : field.TextValue != null) { return false; } - if(DateValue != null ? !DateValue.Equals(that.DateValue) : that.DateValue != null) + if(DateValue != null ? !DateValue.Equals(field.DateValue) : field.DateValue != null) { return false; } - return ToggleValue != null ? ToggleValue.Equals(that.ToggleValue) : that.ToggleValue == null; + return ToggleValue != null ? ToggleValue.Equals(field.ToggleValue) : field.ToggleValue == null; } public override int GetHashCode() diff --git a/src/Android/Autofill/FilledFieldCollection.cs b/src/Android/Autofill/FilledFieldCollection.cs index f103387f9..bd1954e69 100644 --- a/src/Android/Autofill/FilledFieldCollection.cs +++ b/src/Android/Autofill/FilledFieldCollection.cs @@ -25,87 +25,72 @@ namespace Bit.Android.Autofill public string Name { get; set; } public string Subtitle { get; set; } - /** - * Adds a {@code FilledAutofillField} to the collection, indexed by all of its hints. - */ - public void Add(FilledField filledAutofillField) + public void Add(FilledField filledField) { - if(filledAutofillField == null) + if(filledField == null) { - throw new ArgumentNullException(nameof(filledAutofillField)); + throw new ArgumentNullException(nameof(filledField)); } - var autofillHints = filledAutofillField.GetHints(); - foreach(var hint in autofillHints) + foreach(var hint in filledField.Hints) { - HintToFieldMap.Add(hint, filledAutofillField); + HintToFieldMap.Add(hint, filledField); } } - /** - * Populates a {@link Dataset.Builder} with appropriate values for each {@link AutofillId} - * in a {@code AutofillFieldMetadataCollection}. - * - * In other words, it constructs an autofill - * {@link Dataset.Builder} by applying saved values (from this {@code FilledAutofillFieldCollection}) - * to Views specified in a {@code AutofillFieldMetadataCollection}, which represents the current - * page the user is on. - */ public bool ApplyToFields(FieldCollection fieldCollection, Dataset.Builder datasetBuilder) { - var setValueAtLeastOnce = false; - var allHints = fieldCollection.Hints; - for(var hintIndex = 0; hintIndex < allHints.Count; hintIndex++) + var setValue = false; + foreach(var hint in fieldCollection.Hints) { - var hint = allHints[hintIndex]; if(!fieldCollection.HintToFieldsMap.ContainsKey(hint)) { continue; } - var fillableAutofillFields = fieldCollection.HintToFieldsMap[hint]; - for(var autofillFieldIndex = 0; autofillFieldIndex < fillableAutofillFields.Count; autofillFieldIndex++) + var fillableFields = fieldCollection.HintToFieldsMap[hint]; + for(var i = 0; i < fillableFields.Count; i++) { if(!HintToFieldMap.ContainsKey(hint)) { continue; } + var field = fillableFields[i]; var filledField = HintToFieldMap[hint]; - var fieldMetadata = fillableAutofillFields[autofillFieldIndex]; - var autofillId = fieldMetadata.AutofillId; - switch(fieldMetadata.AutofillType) + + switch(field.AutofillType) { case AutofillType.List: - int listValue = fieldMetadata.GetAutofillOptionIndex(filledField.TextValue); + int listValue = field.GetAutofillOptionIndex(filledField.TextValue); if(listValue != -1) { - datasetBuilder.SetValue(autofillId, AutofillValue.ForList(listValue)); - setValueAtLeastOnce = true; + datasetBuilder.SetValue(field.AutofillId, AutofillValue.ForList(listValue)); + setValue = true; } break; case AutofillType.Date: var dateValue = filledField.DateValue; if(dateValue != null) { - datasetBuilder.SetValue(autofillId, AutofillValue.ForDate(dateValue.Value)); - setValueAtLeastOnce = true; + datasetBuilder.SetValue(field.AutofillId, AutofillValue.ForDate(dateValue.Value)); + setValue = true; } break; case AutofillType.Text: var textValue = filledField.TextValue; if(textValue != null) { - datasetBuilder.SetValue(autofillId, AutofillValue.ForText(textValue)); - setValueAtLeastOnce = true; + datasetBuilder.SetValue(field.AutofillId, AutofillValue.ForText(textValue)); + setValue = true; } break; case AutofillType.Toggle: var toggleValue = filledField.ToggleValue; if(toggleValue != null) { - datasetBuilder.SetValue(autofillId, AutofillValue.ForToggle(toggleValue.Value)); - setValueAtLeastOnce = true; + datasetBuilder.SetValue(field.AutofillId, AutofillValue.ForToggle(toggleValue.Value)); + setValue = true; } break; case AutofillType.None: @@ -115,24 +100,9 @@ namespace Bit.Android.Autofill } } - if(!setValueAtLeastOnce) - { - var password = fieldCollection.Fields.FirstOrDefault(f => f.InputType == InputTypes.TextVariationPassword); - // datasetBuilder.SetValue(password.AutofillId, AutofillValue.ForText()); - if(password != null) - { - var username = fieldCollection.Fields.TakeWhile(f => f.Id != password.Id).LastOrDefault(); - } - } - - return setValueAtLeastOnce; + return setValue; } - /** - * Takes in a list of autofill hints (`autofillHints`), usually associated with a View or set of - * Views. Returns whether any of the filled fields on the page have at least 1 of these - * `autofillHint`s. - */ public bool HelpsWithHints(List autofillHints) { return autofillHints.Any(h => HintToFieldMap.ContainsKey(h) && !HintToFieldMap[h].IsNull()); diff --git a/src/Android/Autofill/Parser.cs b/src/Android/Autofill/Parser.cs index ec43d5e33..f356e58d2 100644 --- a/src/Android/Autofill/Parser.cs +++ b/src/Android/Autofill/Parser.cs @@ -7,7 +7,6 @@ namespace Bit.Android.Autofill { private readonly AssistStructure _structure; private string _uri; - private FilledFieldCollection _filledAutofillFieldCollection; public Parser(AssistStructure structure) { @@ -15,6 +14,7 @@ namespace Bit.Android.Autofill } public FieldCollection FieldCollection { get; private set; } = new FieldCollection(); + public FilledFieldCollection FilledFieldCollection { get; private set; } = new FilledFieldCollection(); public string Uri { get => _uri; @@ -34,54 +34,39 @@ namespace Bit.Android.Autofill Parse(false); } - /** - * Traverse AssistStructure and add ViewNode metadata to a flat list. - */ private void Parse(bool forFill) { - _filledAutofillFieldCollection = new FilledFieldCollection(); - for(var i = 0; i < _structure.WindowNodeCount; i++) { var node = _structure.GetWindowNodeAt(i); - var view = node.RootViewNode; - ParseLocked(forFill, view); + ParseNode(forFill, node.RootViewNode); } } - private void ParseLocked(bool forFill, ViewNode viewNode) + private void ParseNode(bool forFill, ViewNode node) { - var autofillHints = viewNode.GetAutofillHints(); - var autofillType = viewNode.AutofillType; - var inputType = viewNode.InputType; - var isEditText = viewNode.ClassName == "android.widget.EditText"; - if(isEditText || (autofillHints?.Length ?? 0) > 0) + var hints = node.GetAutofillHints(); + var isEditText = node.ClassName == "android.widget.EditText"; + if(isEditText || (hints?.Length ?? 0) > 0) { if(forFill) { - var f = new Field(viewNode); - FieldCollection.Add(f); - + FieldCollection.Add(new Field(node)); if(Uri == null) { - Uri = viewNode.IdPackage; + Uri = node.IdPackage; } } else { - _filledAutofillFieldCollection.Add(new FilledField(viewNode)); + FilledFieldCollection.Add(new FilledField(node)); } } - for(var i = 0; i < viewNode.ChildCount; i++) + for(var i = 0; i < node.ChildCount; i++) { - ParseLocked(forFill, viewNode.GetChildAt(i)); + ParseNode(forFill, node.GetChildAt(i)); } } - - public FilledFieldCollection GetClientFormData() - { - return _filledAutofillFieldCollection; - } } } \ No newline at end of file