2017-11-15 07:13:55 +03:00
|
|
|
|
using Android;
|
|
|
|
|
using Android.App;
|
2017-11-17 08:16:45 +03:00
|
|
|
|
using Android.Content;
|
2017-11-15 07:13:55 +03:00
|
|
|
|
using Android.OS;
|
|
|
|
|
using Android.Runtime;
|
|
|
|
|
using Android.Service.Autofill;
|
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using XLabs.Ioc;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Android.Autofill
|
|
|
|
|
{
|
|
|
|
|
[Service(Permission = Manifest.Permission.BindAutofillService, Label = "bitwarden")]
|
|
|
|
|
[IntentFilter(new string[] { "android.service.autofill.AutofillService" })]
|
|
|
|
|
[MetaData("android.autofill", Resource = "@xml/autofillservice")]
|
|
|
|
|
[Register("com.x8bit.bitwarden.Autofill.AutofillService")]
|
|
|
|
|
public class AutofillService : global::Android.Service.Autofill.AutofillService
|
|
|
|
|
{
|
|
|
|
|
private ICipherService _cipherService;
|
2017-11-17 08:16:45 +03:00
|
|
|
|
private ILockService _lockService;
|
2017-11-15 07:13:55 +03:00
|
|
|
|
|
|
|
|
|
public async override void OnFillRequest(FillRequest request, CancellationSignal cancellationSignal, FillCallback callback)
|
|
|
|
|
{
|
|
|
|
|
var structure = request.FillContexts?.LastOrDefault()?.Structure;
|
|
|
|
|
if(structure == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var parser = new Parser(structure);
|
|
|
|
|
parser.ParseForFill();
|
|
|
|
|
|
|
|
|
|
if(!parser.FieldCollection.Fields.Any() || string.IsNullOrWhiteSpace(parser.Uri))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-17 08:16:45 +03:00
|
|
|
|
if(_lockService == null)
|
2017-11-15 07:13:55 +03:00
|
|
|
|
{
|
2017-11-17 08:16:45 +03:00
|
|
|
|
_lockService = Resolver.Resolve<ILockService>();
|
2017-11-15 07:13:55 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-17 17:21:12 +03:00
|
|
|
|
var isLocked = (await _lockService.GetLockTypeAsync(false)) != App.Enums.LockType.None;
|
|
|
|
|
if(isLocked)
|
2017-11-17 08:16:45 +03:00
|
|
|
|
{
|
2017-11-17 17:21:12 +03:00
|
|
|
|
var authResponse = AutofillHelpers.BuildAuthResponse(this, parser.FieldCollection, parser.Uri);
|
2017-11-17 08:16:45 +03:00
|
|
|
|
callback.OnSuccess(authResponse);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(_cipherService == null)
|
2017-11-15 07:13:55 +03:00
|
|
|
|
{
|
2017-11-17 08:16:45 +03:00
|
|
|
|
_cipherService = Resolver.Resolve<ICipherService>();
|
2017-11-15 07:13:55 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-17 08:16:45 +03:00
|
|
|
|
// build response
|
|
|
|
|
var items = await AutofillHelpers.GetFillItemsAsync(_cipherService, parser.Uri);
|
2017-11-15 07:13:55 +03:00
|
|
|
|
if(!items.Any())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-17 08:16:45 +03:00
|
|
|
|
var response = AutofillHelpers.BuildFillResponse(this, parser.FieldCollection, items);
|
2017-11-15 07:13:55 +03:00
|
|
|
|
callback.OnSuccess(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnSaveRequest(SaveRequest request, SaveCallback callback)
|
|
|
|
|
{
|
|
|
|
|
var structure = request.FillContexts?.LastOrDefault()?.Structure;
|
|
|
|
|
if(structure == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var clientState = request.ClientState;
|
|
|
|
|
|
|
|
|
|
var parser = new Parser(structure);
|
|
|
|
|
parser.ParseForSave();
|
2017-11-17 05:58:04 +03:00
|
|
|
|
var filledAutofillFieldCollection = parser.FilledFieldCollection;
|
2017-11-15 07:13:55 +03:00
|
|
|
|
//SaveFilledAutofillFieldCollection(filledAutofillFieldCollection);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|