bitwarden-android/src/Android/Autofill/Parser.cs

79 lines
2 KiB
C#
Raw Normal View History

using static Android.App.Assist.AssistStructure;
using Android.App.Assist;
namespace Bit.Android.Autofill
{
public class Parser
{
private readonly AssistStructure _structure;
private string _uri;
public Parser(AssistStructure structure)
{
_structure = structure;
}
public FieldCollection FieldCollection { get; private set; } = new FieldCollection();
2017-11-17 05:58:04 +03:00
public FilledFieldCollection FilledFieldCollection { get; private set; } = new FilledFieldCollection();
public string Uri
{
get => _uri;
set
{
2017-11-17 18:09:27 +03:00
if(string.IsNullOrWhiteSpace(value))
{
_uri = null;
return;
}
_uri = $"androidapp://{value}";
}
}
public void ParseForFill()
{
Parse(true);
}
public void ParseForSave()
{
Parse(false);
}
private void Parse(bool forFill)
{
for(var i = 0; i < _structure.WindowNodeCount; i++)
{
var node = _structure.GetWindowNodeAt(i);
2017-11-17 05:58:04 +03:00
ParseNode(forFill, node.RootViewNode);
}
}
2017-11-17 05:58:04 +03:00
private void ParseNode(bool forFill, ViewNode node)
{
2017-11-17 05:58:04 +03:00
var hints = node.GetAutofillHints();
var isEditText = node.ClassName == "android.widget.EditText";
if(isEditText || (hints?.Length ?? 0) > 0)
{
if(Uri == null)
{
Uri = node.IdPackage;
}
if(forFill)
{
2017-11-17 05:58:04 +03:00
FieldCollection.Add(new Field(node));
}
else
{
2017-11-17 05:58:04 +03:00
FilledFieldCollection.Add(new FilledField(node));
}
}
2017-11-17 05:58:04 +03:00
for(var i = 0; i < node.ChildCount; i++)
{
2017-11-17 05:58:04 +03:00
ParseNode(forFill, node.GetChildAt(i));
}
}
}
}