2017-11-15 00:46:40 +03:00
|
|
|
|
using static Android.App.Assist.AssistStructure;
|
|
|
|
|
using Android.App.Assist;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Android.Autofill
|
|
|
|
|
{
|
2017-11-15 07:13:55 +03:00
|
|
|
|
public class Parser
|
2017-11-15 00:46:40 +03:00
|
|
|
|
{
|
|
|
|
|
private readonly AssistStructure _structure;
|
2017-11-15 07:13:55 +03:00
|
|
|
|
private string _uri;
|
|
|
|
|
private FilledFieldCollection _filledAutofillFieldCollection;
|
2017-11-15 00:46:40 +03:00
|
|
|
|
|
2017-11-15 07:13:55 +03:00
|
|
|
|
public Parser(AssistStructure structure)
|
2017-11-15 00:46:40 +03:00
|
|
|
|
{
|
|
|
|
|
_structure = structure;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-15 07:13:55 +03:00
|
|
|
|
public FieldCollection FieldCollection { get; private set; } = new FieldCollection();
|
|
|
|
|
public string Uri
|
|
|
|
|
{
|
|
|
|
|
get => _uri;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_uri = $"androidapp://{value}";
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-15 00:46:40 +03:00
|
|
|
|
|
|
|
|
|
public void ParseForFill()
|
|
|
|
|
{
|
|
|
|
|
Parse(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ParseForSave()
|
|
|
|
|
{
|
|
|
|
|
Parse(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Traverse AssistStructure and add ViewNode metadata to a flat list.
|
|
|
|
|
*/
|
|
|
|
|
private void Parse(bool forFill)
|
|
|
|
|
{
|
2017-11-15 07:13:55 +03:00
|
|
|
|
_filledAutofillFieldCollection = new FilledFieldCollection();
|
2017-11-15 00:46:40 +03:00
|
|
|
|
|
|
|
|
|
for(var i = 0; i < _structure.WindowNodeCount; i++)
|
|
|
|
|
{
|
|
|
|
|
var node = _structure.GetWindowNodeAt(i);
|
|
|
|
|
var view = node.RootViewNode;
|
|
|
|
|
ParseLocked(forFill, view);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ParseLocked(bool forFill, ViewNode viewNode)
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
if(forFill)
|
|
|
|
|
{
|
2017-11-15 07:13:55 +03:00
|
|
|
|
var f = new Field(viewNode);
|
|
|
|
|
FieldCollection.Add(f);
|
|
|
|
|
|
|
|
|
|
if(Uri == null)
|
|
|
|
|
{
|
|
|
|
|
Uri = viewNode.IdPackage;
|
|
|
|
|
}
|
2017-11-15 00:46:40 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-11-15 07:13:55 +03:00
|
|
|
|
_filledAutofillFieldCollection.Add(new FilledField(viewNode));
|
2017-11-15 00:46:40 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(var i = 0; i < viewNode.ChildCount; i++)
|
|
|
|
|
{
|
|
|
|
|
ParseLocked(forFill, viewNode.GetChildAt(i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-15 07:13:55 +03:00
|
|
|
|
public FilledFieldCollection GetClientFormData()
|
2017-11-15 00:46:40 +03:00
|
|
|
|
{
|
|
|
|
|
return _filledAutofillFieldCollection;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|