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

82 lines
2.1 KiB
C#
Raw Normal View History

using static Android.App.Assist.AssistStructure;
using Android.App.Assist;
2017-11-18 01:15:42 +03:00
using Bit.App;
namespace Bit.Android.Autofill
{
public class Parser
{
private readonly AssistStructure _structure;
private string _uri;
private string _packageName;
public Parser(AssistStructure structure)
{
_structure = structure;
}
public FieldCollection FieldCollection { get; private set; } = new FieldCollection();
public string Uri
{
get
{
if(!string.IsNullOrWhiteSpace(_uri))
{
return _uri;
}
if(string.IsNullOrWhiteSpace(PackageName))
{
_uri = null;
}
else
{
_uri = string.Concat(Constants.AndroidAppProtocol, PackageName);
}
return _uri;
}
}
public string PackageName
{
get => _packageName;
set
{
2017-11-17 18:09:27 +03:00
if(string.IsNullOrWhiteSpace(value))
{
_packageName = _uri = null;
2017-11-17 18:09:27 +03:00
}
_packageName = value;
}
}
2017-11-18 01:15:42 +03:00
public void Parse()
{
for(var i = 0; i < _structure.WindowNodeCount; i++)
{
var node = _structure.GetWindowNodeAt(i);
2017-11-18 01:15:42 +03:00
ParseNode(node.RootViewNode);
}
}
2017-11-18 01:15:42 +03:00
private void ParseNode(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(PackageName == null)
{
PackageName = node.IdPackage;
}
2017-11-18 01:15:42 +03:00
FieldCollection.Add(new Field(node));
}
2017-11-17 05:58:04 +03:00
for(var i = 0; i < node.ChildCount; i++)
{
2017-11-18 01:15:42 +03:00
ParseNode(node.GetChildAt(i));
}
}
}
}