2017-11-15 00:46:40 +03:00
|
|
|
|
using static Android.App.Assist.AssistStructure;
|
|
|
|
|
using Android.App.Assist;
|
2017-11-18 01:15:42 +03:00
|
|
|
|
using Bit.App;
|
2017-12-20 19:55:16 +03:00
|
|
|
|
using System.Collections.Generic;
|
2017-11-15 00:46:40 +03:00
|
|
|
|
|
|
|
|
|
namespace Bit.Android.Autofill
|
|
|
|
|
{
|
2017-11-15 07:13:55 +03:00
|
|
|
|
public class Parser
|
2017-11-15 00:46:40 +03:00
|
|
|
|
{
|
2017-12-20 19:55:16 +03:00
|
|
|
|
public static HashSet<string> TrustedBrowsers = new HashSet<string>
|
|
|
|
|
{
|
|
|
|
|
"org.mozilla.focus","org.mozilla.firefox","org.mozilla.firefox_beta","com.microsoft.emmx",
|
|
|
|
|
"com.android.chrome","com.chrome.beta","com.android.browser","com.brave.browser","com.opera.browser",
|
|
|
|
|
"com.opera.browser.beta","com.opera.mini.native","com.chrome.dev","com.chrome.canary",
|
|
|
|
|
"com.google.android.apps.chrome","com.google.android.apps.chrome_dev","com.yandex.browser",
|
|
|
|
|
"com.sec.android.app.sbrowser","com.sec.android.app.sbrowser.beta","org.codeaurora.swe.browser",
|
2018-01-09 19:31:00 +03:00
|
|
|
|
"com.amazon.cloud9","org.mozilla.klar"
|
2017-12-20 19:55:16 +03:00
|
|
|
|
};
|
|
|
|
|
|
2017-11-15 00:46:40 +03:00
|
|
|
|
private readonly AssistStructure _structure;
|
2017-11-15 07:13:55 +03:00
|
|
|
|
private string _uri;
|
2017-11-18 06:47:08 +03:00
|
|
|
|
private string _packageName;
|
2017-12-20 19:55:16 +03:00
|
|
|
|
private string _webDomain;
|
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
|
|
|
|
|
{
|
2017-11-18 06:47:08 +03:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(_uri))
|
|
|
|
|
{
|
|
|
|
|
return _uri;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 19:55:16 +03:00
|
|
|
|
if(string.IsNullOrWhiteSpace(WebDomain) && string.IsNullOrWhiteSpace(PackageName))
|
2017-11-18 06:47:08 +03:00
|
|
|
|
{
|
|
|
|
|
_uri = null;
|
|
|
|
|
}
|
2017-12-20 19:55:16 +03:00
|
|
|
|
else if(!string.IsNullOrWhiteSpace(WebDomain))
|
|
|
|
|
{
|
|
|
|
|
_uri = string.Concat("http://", WebDomain);
|
|
|
|
|
}
|
2017-11-18 06:47:08 +03:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_uri = string.Concat(Constants.AndroidAppProtocol, PackageName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _uri;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public string PackageName
|
|
|
|
|
{
|
|
|
|
|
get => _packageName;
|
2017-11-15 07:13:55 +03:00
|
|
|
|
set
|
|
|
|
|
{
|
2017-11-17 18:09:27 +03:00
|
|
|
|
if(string.IsNullOrWhiteSpace(value))
|
|
|
|
|
{
|
2017-11-18 06:47:08 +03:00
|
|
|
|
_packageName = _uri = null;
|
2017-11-17 18:09:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-18 06:47:08 +03:00
|
|
|
|
_packageName = value;
|
2017-11-15 07:13:55 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-20 19:55:16 +03:00
|
|
|
|
public string WebDomain
|
|
|
|
|
{
|
|
|
|
|
get => _webDomain;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if(string.IsNullOrWhiteSpace(value))
|
|
|
|
|
{
|
|
|
|
|
_webDomain = _uri = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_webDomain = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-15 00:46:40 +03:00
|
|
|
|
|
2017-11-18 01:15:42 +03:00
|
|
|
|
public void Parse()
|
2017-11-15 00:46:40 +03:00
|
|
|
|
{
|
|
|
|
|
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-15 00:46:40 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-18 01:15:42 +03:00
|
|
|
|
private void ParseNode(ViewNode node)
|
2017-11-15 00:46:40 +03:00
|
|
|
|
{
|
2017-11-17 05:58:04 +03:00
|
|
|
|
var hints = node.GetAutofillHints();
|
2017-12-20 19:55:16 +03:00
|
|
|
|
var isEditText = node.ClassName == "android.widget.EditText" || node?.HtmlInfo?.Tag == "input";
|
2017-11-17 05:58:04 +03:00
|
|
|
|
if(isEditText || (hints?.Length ?? 0) > 0)
|
2017-11-15 00:46:40 +03:00
|
|
|
|
{
|
2017-11-18 06:47:08 +03:00
|
|
|
|
if(PackageName == null)
|
2017-11-17 22:38:56 +03:00
|
|
|
|
{
|
2017-11-18 06:47:08 +03:00
|
|
|
|
PackageName = node.IdPackage;
|
2017-11-17 22:38:56 +03:00
|
|
|
|
}
|
2017-12-20 19:55:16 +03:00
|
|
|
|
if(WebDomain == null && TrustedBrowsers.Contains(node.IdPackage))
|
|
|
|
|
{
|
|
|
|
|
WebDomain = node.WebDomain;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-18 01:15:42 +03:00
|
|
|
|
FieldCollection.Add(new Field(node));
|
2017-11-15 00:46:40 +03:00
|
|
|
|
}
|
2017-11-18 07:26:51 +03:00
|
|
|
|
else
|
|
|
|
|
{
|
2017-12-20 19:55:16 +03:00
|
|
|
|
if(WebDomain == null && TrustedBrowsers.Contains(node.IdPackage))
|
|
|
|
|
{
|
|
|
|
|
WebDomain = node.WebDomain;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-18 07:26:51 +03:00
|
|
|
|
FieldCollection.IgnoreAutofillIds.Add(node.AutofillId);
|
|
|
|
|
}
|
2017-11-15 00:46:40 +03:00
|
|
|
|
|
2017-11-17 05:58:04 +03:00
|
|
|
|
for(var i = 0; i < node.ChildCount; i++)
|
2017-11-15 00:46:40 +03:00
|
|
|
|
{
|
2017-11-18 01:15:42 +03:00
|
|
|
|
ParseNode(node.GetChildAt(i));
|
2017-11-15 00:46:40 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|