renaming things

This commit is contained in:
Kyle Spearrin 2017-11-16 21:58:04 -05:00
parent 0dd9ad43e8
commit 1694b5d6fd
4 changed files with 48 additions and 102 deletions

View file

@ -76,7 +76,7 @@ namespace Bit.Android.Autofill
var parser = new Parser(structure); var parser = new Parser(structure);
parser.ParseForSave(); parser.ParseForSave();
var filledAutofillFieldCollection = parser.GetClientFormData(); var filledAutofillFieldCollection = parser.FilledFieldCollection;
//SaveFilledAutofillFieldCollection(filledAutofillFieldCollection); //SaveFilledAutofillFieldCollection(filledAutofillFieldCollection);
} }
} }

View file

@ -5,48 +5,39 @@ namespace Bit.Android.Autofill
{ {
public class FilledField public class FilledField
{ {
private IEnumerable<string> _hints = null;
public FilledField() { } public FilledField() { }
public FilledField(ViewNode node) public FilledField(ViewNode node)
{ {
_hints = AutofillHelpers.FilterForSupportedHints(node.GetAutofillHints()); Hints = AutofillHelpers.FilterForSupportedHints(node.GetAutofillHints());
var autofillValue = node.AutofillValue;
if(autofillValue == null) if(node.AutofillValue == null)
{ {
return; return;
} }
if(autofillValue.IsList) if(node.AutofillValue.IsList)
{ {
var autofillOptions = node.GetAutofillOptions(); var autofillOptions = node.GetAutofillOptions();
int index = autofillValue.ListValue;
if(autofillOptions != null && autofillOptions.Length > 0) 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 TextValue = node.AutofillValue.TextValue;
// SharedPreferences.
TextValue = autofillValue.TextValue;
} }
} }
public string TextValue { get; set; } public string TextValue { get; set; }
public long? DateValue { get; set; } public long? DateValue { get; set; }
public bool? ToggleValue { get; set; } public bool? ToggleValue { get; set; }
public List<string> Hints { get; set; }
public IEnumerable<string> GetHints()
{
return _hints;
}
public bool IsNull() public bool IsNull()
{ {
@ -65,18 +56,18 @@ namespace Bit.Android.Autofill
return false; return false;
} }
var that = o as FilledField; var field = o as FilledField;
if(TextValue != null ? !TextValue.Equals(that.TextValue) : that.TextValue != null) if(TextValue != null ? !TextValue.Equals(field.TextValue) : field.TextValue != null)
{ {
return false; 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 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() public override int GetHashCode()

View file

@ -25,87 +25,72 @@ namespace Bit.Android.Autofill
public string Name { get; set; } public string Name { get; set; }
public string Subtitle { get; set; } public string Subtitle { get; set; }
/** public void Add(FilledField filledField)
* Adds a {@code FilledAutofillField} to the collection, indexed by all of its hints.
*/
public void Add(FilledField filledAutofillField)
{ {
if(filledAutofillField == null) if(filledField == null)
{ {
throw new ArgumentNullException(nameof(filledAutofillField)); throw new ArgumentNullException(nameof(filledField));
} }
var autofillHints = filledAutofillField.GetHints(); foreach(var hint in filledField.Hints)
foreach(var hint in autofillHints)
{ {
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) public bool ApplyToFields(FieldCollection fieldCollection, Dataset.Builder datasetBuilder)
{ {
var setValueAtLeastOnce = false; var setValue = false;
var allHints = fieldCollection.Hints; foreach(var hint in fieldCollection.Hints)
for(var hintIndex = 0; hintIndex < allHints.Count; hintIndex++)
{ {
var hint = allHints[hintIndex];
if(!fieldCollection.HintToFieldsMap.ContainsKey(hint)) if(!fieldCollection.HintToFieldsMap.ContainsKey(hint))
{ {
continue; continue;
} }
var fillableAutofillFields = fieldCollection.HintToFieldsMap[hint]; var fillableFields = fieldCollection.HintToFieldsMap[hint];
for(var autofillFieldIndex = 0; autofillFieldIndex < fillableAutofillFields.Count; autofillFieldIndex++) for(var i = 0; i < fillableFields.Count; i++)
{ {
if(!HintToFieldMap.ContainsKey(hint)) if(!HintToFieldMap.ContainsKey(hint))
{ {
continue; continue;
} }
var field = fillableFields[i];
var filledField = HintToFieldMap[hint]; var filledField = HintToFieldMap[hint];
var fieldMetadata = fillableAutofillFields[autofillFieldIndex];
var autofillId = fieldMetadata.AutofillId; switch(field.AutofillType)
switch(fieldMetadata.AutofillType)
{ {
case AutofillType.List: case AutofillType.List:
int listValue = fieldMetadata.GetAutofillOptionIndex(filledField.TextValue); int listValue = field.GetAutofillOptionIndex(filledField.TextValue);
if(listValue != -1) if(listValue != -1)
{ {
datasetBuilder.SetValue(autofillId, AutofillValue.ForList(listValue)); datasetBuilder.SetValue(field.AutofillId, AutofillValue.ForList(listValue));
setValueAtLeastOnce = true; setValue = true;
} }
break; break;
case AutofillType.Date: case AutofillType.Date:
var dateValue = filledField.DateValue; var dateValue = filledField.DateValue;
if(dateValue != null) if(dateValue != null)
{ {
datasetBuilder.SetValue(autofillId, AutofillValue.ForDate(dateValue.Value)); datasetBuilder.SetValue(field.AutofillId, AutofillValue.ForDate(dateValue.Value));
setValueAtLeastOnce = true; setValue = true;
} }
break; break;
case AutofillType.Text: case AutofillType.Text:
var textValue = filledField.TextValue; var textValue = filledField.TextValue;
if(textValue != null) if(textValue != null)
{ {
datasetBuilder.SetValue(autofillId, AutofillValue.ForText(textValue)); datasetBuilder.SetValue(field.AutofillId, AutofillValue.ForText(textValue));
setValueAtLeastOnce = true; setValue = true;
} }
break; break;
case AutofillType.Toggle: case AutofillType.Toggle:
var toggleValue = filledField.ToggleValue; var toggleValue = filledField.ToggleValue;
if(toggleValue != null) if(toggleValue != null)
{ {
datasetBuilder.SetValue(autofillId, AutofillValue.ForToggle(toggleValue.Value)); datasetBuilder.SetValue(field.AutofillId, AutofillValue.ForToggle(toggleValue.Value));
setValueAtLeastOnce = true; setValue = true;
} }
break; break;
case AutofillType.None: case AutofillType.None:
@ -115,24 +100,9 @@ namespace Bit.Android.Autofill
} }
} }
if(!setValueAtLeastOnce) return setValue;
{
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;
}
/**
* 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<String> autofillHints) public bool HelpsWithHints(List<String> autofillHints)
{ {
return autofillHints.Any(h => HintToFieldMap.ContainsKey(h) && !HintToFieldMap[h].IsNull()); return autofillHints.Any(h => HintToFieldMap.ContainsKey(h) && !HintToFieldMap[h].IsNull());

View file

@ -7,7 +7,6 @@ namespace Bit.Android.Autofill
{ {
private readonly AssistStructure _structure; private readonly AssistStructure _structure;
private string _uri; private string _uri;
private FilledFieldCollection _filledAutofillFieldCollection;
public Parser(AssistStructure structure) public Parser(AssistStructure structure)
{ {
@ -15,6 +14,7 @@ namespace Bit.Android.Autofill
} }
public FieldCollection FieldCollection { get; private set; } = new FieldCollection(); public FieldCollection FieldCollection { get; private set; } = new FieldCollection();
public FilledFieldCollection FilledFieldCollection { get; private set; } = new FilledFieldCollection();
public string Uri public string Uri
{ {
get => _uri; get => _uri;
@ -34,54 +34,39 @@ namespace Bit.Android.Autofill
Parse(false); Parse(false);
} }
/**
* Traverse AssistStructure and add ViewNode metadata to a flat list.
*/
private void Parse(bool forFill) private void Parse(bool forFill)
{ {
_filledAutofillFieldCollection = new FilledFieldCollection();
for(var i = 0; i < _structure.WindowNodeCount; i++) for(var i = 0; i < _structure.WindowNodeCount; i++)
{ {
var node = _structure.GetWindowNodeAt(i); var node = _structure.GetWindowNodeAt(i);
var view = node.RootViewNode; ParseNode(forFill, node.RootViewNode);
ParseLocked(forFill, view);
} }
} }
private void ParseLocked(bool forFill, ViewNode viewNode) private void ParseNode(bool forFill, ViewNode node)
{ {
var autofillHints = viewNode.GetAutofillHints(); var hints = node.GetAutofillHints();
var autofillType = viewNode.AutofillType; var isEditText = node.ClassName == "android.widget.EditText";
var inputType = viewNode.InputType; if(isEditText || (hints?.Length ?? 0) > 0)
var isEditText = viewNode.ClassName == "android.widget.EditText";
if(isEditText || (autofillHints?.Length ?? 0) > 0)
{ {
if(forFill) if(forFill)
{ {
var f = new Field(viewNode); FieldCollection.Add(new Field(node));
FieldCollection.Add(f);
if(Uri == null) if(Uri == null)
{ {
Uri = viewNode.IdPackage; Uri = node.IdPackage;
} }
} }
else 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;
}
} }
} }