mirror of
https://github.com/bitwarden/android.git
synced 2024-10-31 23:25:45 +03:00
renaming things
This commit is contained in:
parent
0dd9ad43e8
commit
1694b5d6fd
4 changed files with 48 additions and 102 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,48 +5,39 @@ namespace Bit.Android.Autofill
|
|||
{
|
||||
public class FilledField
|
||||
{
|
||||
private IEnumerable<string> _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<string> GetHints()
|
||||
{
|
||||
return _hints;
|
||||
}
|
||||
public List<string> 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()
|
||||
|
|
|
@ -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 setValue;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
return autofillHints.Any(h => HintToFieldMap.ContainsKey(h) && !HintToFieldMap[h].IsNull());
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
public FilledFieldCollection GetClientFormData()
|
||||
{
|
||||
return _filledAutofillFieldCollection;
|
||||
ParseNode(forFill, node.GetChildAt(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue