2019-04-30 18:35:58 +02:00
|
|
|
|
using static Android.App.Assist.AssistStructure;
|
|
|
|
|
using Android.App.Assist;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Bit.Core;
|
|
|
|
|
using Android.Content;
|
2019-06-03 17:08:33 +02:00
|
|
|
|
using Bit.Core.Abstractions;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-07-11 14:55:40 +02:00
|
|
|
|
using Android.OS;
|
2019-04-30 18:35:58 +02:00
|
|
|
|
|
|
|
|
|
namespace Bit.Droid.Autofill
|
|
|
|
|
{
|
|
|
|
|
public class Parser
|
|
|
|
|
{
|
|
|
|
|
public static HashSet<string> _excludedPackageIds = new HashSet<string>
|
|
|
|
|
{
|
|
|
|
|
"android"
|
|
|
|
|
};
|
|
|
|
|
private readonly AssistStructure _structure;
|
|
|
|
|
private string _uri;
|
|
|
|
|
private string _packageName;
|
2019-07-11 14:55:40 +02:00
|
|
|
|
private string _website;
|
2019-04-30 18:35:58 +02:00
|
|
|
|
|
|
|
|
|
public Parser(AssistStructure structure, Context applicationContext)
|
|
|
|
|
{
|
|
|
|
|
_structure = structure;
|
|
|
|
|
ApplicationContext = applicationContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Context ApplicationContext { get; set; }
|
|
|
|
|
public FieldCollection FieldCollection { get; private set; } = new FieldCollection();
|
|
|
|
|
|
|
|
|
|
public string Uri
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(_uri))
|
2019-04-30 18:35:58 +02:00
|
|
|
|
{
|
|
|
|
|
return _uri;
|
|
|
|
|
}
|
2019-07-11 14:55:40 +02:00
|
|
|
|
var websiteNull = string.IsNullOrWhiteSpace(Website);
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (websiteNull && string.IsNullOrWhiteSpace(PackageName))
|
2019-04-30 18:35:58 +02:00
|
|
|
|
{
|
|
|
|
|
_uri = null;
|
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
else if (!websiteNull)
|
2019-04-30 18:35:58 +02:00
|
|
|
|
{
|
2019-07-11 14:55:40 +02:00
|
|
|
|
_uri = Website;
|
2019-04-30 18:35:58 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_uri = string.Concat(Constants.AndroidAppProtocol, PackageName);
|
|
|
|
|
}
|
|
|
|
|
return _uri;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string PackageName
|
|
|
|
|
{
|
|
|
|
|
get => _packageName;
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(value))
|
2019-04-30 18:35:58 +02:00
|
|
|
|
{
|
|
|
|
|
_packageName = _uri = null;
|
|
|
|
|
}
|
|
|
|
|
_packageName = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 14:55:40 +02:00
|
|
|
|
public string Website
|
2019-04-30 18:35:58 +02:00
|
|
|
|
{
|
2019-07-11 14:55:40 +02:00
|
|
|
|
get => _website;
|
2019-04-30 18:35:58 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(value))
|
2019-04-30 18:35:58 +02:00
|
|
|
|
{
|
2019-07-11 14:55:40 +02:00
|
|
|
|
_website = _uri = null;
|
2019-04-30 18:35:58 +02:00
|
|
|
|
}
|
2019-07-11 14:55:40 +02:00
|
|
|
|
_website = value;
|
2019-04-30 18:35:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:08:33 +02:00
|
|
|
|
public async Task<bool> ShouldAutofillAsync(IStorageService storageService)
|
|
|
|
|
{
|
|
|
|
|
var fillable = !string.IsNullOrWhiteSpace(Uri) && !AutofillHelpers.BlacklistedUris.Contains(Uri) &&
|
|
|
|
|
FieldCollection != null && FieldCollection.Fillable;
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (fillable)
|
2019-06-03 17:08:33 +02:00
|
|
|
|
{
|
|
|
|
|
var blacklistedUris = await storageService.GetAsync<List<string>>(Constants.AutofillBlacklistedUrisKey);
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (blacklistedUris != null && blacklistedUris.Count > 0)
|
2019-06-03 17:08:33 +02:00
|
|
|
|
{
|
|
|
|
|
fillable = !new HashSet<string>(blacklistedUris).Contains(Uri);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fillable;
|
|
|
|
|
}
|
2019-04-30 18:35:58 +02:00
|
|
|
|
|
|
|
|
|
public void Parse()
|
|
|
|
|
{
|
2019-08-01 22:39:01 +02:00
|
|
|
|
string titlePackageId = null;
|
2020-03-28 14:16:28 +01:00
|
|
|
|
for (var i = 0; i < _structure.WindowNodeCount; i++)
|
2019-04-30 18:35:58 +02:00
|
|
|
|
{
|
|
|
|
|
var node = _structure.GetWindowNodeAt(i);
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (i == 0)
|
2019-08-01 22:39:01 +02:00
|
|
|
|
{
|
|
|
|
|
titlePackageId = GetTitlePackageId(node);
|
|
|
|
|
}
|
2019-04-30 18:35:58 +02:00
|
|
|
|
ParseNode(node.RootViewNode);
|
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(PackageName) && string.IsNullOrWhiteSpace(Website))
|
2019-08-01 22:39:01 +02:00
|
|
|
|
{
|
|
|
|
|
PackageName = titlePackageId;
|
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!AutofillHelpers.TrustedBrowsers.Contains(PackageName) &&
|
2019-04-30 18:35:58 +02:00
|
|
|
|
!AutofillHelpers.CompatBrowsers.Contains(PackageName))
|
|
|
|
|
{
|
2019-07-11 14:55:40 +02:00
|
|
|
|
Website = null;
|
2019-04-30 18:35:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ParseNode(ViewNode node)
|
|
|
|
|
{
|
|
|
|
|
SetPackageAndDomain(node);
|
|
|
|
|
var hints = node.GetAutofillHints();
|
|
|
|
|
var isEditText = node.ClassName == "android.widget.EditText" || node?.HtmlInfo?.Tag == "input";
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (isEditText || (hints?.Length ?? 0) > 0)
|
2019-04-30 18:35:58 +02:00
|
|
|
|
{
|
|
|
|
|
FieldCollection.Add(new Field(node));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FieldCollection.IgnoreAutofillIds.Add(node.AutofillId);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
|
for (var i = 0; i < node.ChildCount; i++)
|
2019-04-30 18:35:58 +02:00
|
|
|
|
{
|
|
|
|
|
ParseNode(node.GetChildAt(i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetPackageAndDomain(ViewNode node)
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(PackageName) && !string.IsNullOrWhiteSpace(node.IdPackage) &&
|
2019-04-30 18:35:58 +02:00
|
|
|
|
!_excludedPackageIds.Contains(node.IdPackage))
|
|
|
|
|
{
|
|
|
|
|
PackageName = node.IdPackage;
|
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(Website) && !string.IsNullOrWhiteSpace(node.WebDomain))
|
2019-04-30 18:35:58 +02:00
|
|
|
|
{
|
2019-07-11 14:55:40 +02:00
|
|
|
|
var scheme = "http";
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if ((int)Build.VERSION.SdkInt >= 28)
|
2019-07-11 14:55:40 +02:00
|
|
|
|
{
|
|
|
|
|
scheme = node.WebScheme;
|
|
|
|
|
}
|
|
|
|
|
Website = string.Format("{0}://{1}", scheme, node.WebDomain);
|
2019-04-30 18:35:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-01 22:39:01 +02:00
|
|
|
|
|
|
|
|
|
private string GetTitlePackageId(WindowNode node)
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (node != null && !string.IsNullOrWhiteSpace(node.Title))
|
2019-08-01 22:39:01 +02:00
|
|
|
|
{
|
|
|
|
|
var slashPosition = node.Title.IndexOf('/');
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (slashPosition > -1)
|
2019-08-01 22:39:01 +02:00
|
|
|
|
{
|
|
|
|
|
var packageId = node.Title.Substring(0, slashPosition);
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (packageId.Contains("."))
|
2019-08-01 22:39:01 +02:00
|
|
|
|
{
|
|
|
|
|
return packageId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-04-30 18:35:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|