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;
|
2017-11-18 01:15:42 +03:00
|
|
|
|
using Android.Widget;
|
|
|
|
|
using Bit.App;
|
2017-11-15 07:13:55 +03:00
|
|
|
|
using Bit.App.Abstractions;
|
2017-11-17 22:38:56 +03:00
|
|
|
|
using Bit.App.Enums;
|
2017-11-15 07:13:55 +03:00
|
|
|
|
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);
|
2017-11-18 01:15:42 +03:00
|
|
|
|
parser.Parse();
|
2017-11-15 07:13:55 +03:00
|
|
|
|
|
2017-11-17 18:05:13 +03:00
|
|
|
|
if(!parser.FieldCollection.Fields.Any() || string.IsNullOrWhiteSpace(parser.Uri) ||
|
2017-11-17 18:18:18 +03:00
|
|
|
|
parser.Uri == "androidapp://com.x8bit.bitwarden" || parser.Uri == "androidapp://android")
|
2017-11-15 07:13:55 +03:00
|
|
|
|
{
|
|
|
|
|
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 22:38:56 +03:00
|
|
|
|
var isLocked = (await _lockService.GetLockTypeAsync(false)) != LockType.None;
|
2017-11-17 17:21:12 +03:00
|
|
|
|
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);
|
|
|
|
|
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 parser = new Parser(structure);
|
2017-11-18 01:15:42 +03:00
|
|
|
|
parser.Parse();
|
|
|
|
|
|
|
|
|
|
var savedItem = parser.FieldCollection.GetSavedItem();
|
|
|
|
|
if(savedItem == null)
|
|
|
|
|
{
|
|
|
|
|
Toast.MakeText(this, "Unable to save this form.", ToastLength.Short).Show();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-11-17 22:38:56 +03:00
|
|
|
|
|
|
|
|
|
var intent = new Intent(this, typeof(MainActivity));
|
2017-11-18 01:15:42 +03:00
|
|
|
|
intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTop);
|
2017-11-17 22:38:56 +03:00
|
|
|
|
intent.PutExtra("autofillFramework", true);
|
|
|
|
|
intent.PutExtra("autofillFrameworkSave", true);
|
2017-11-18 01:15:42 +03:00
|
|
|
|
intent.PutExtra("autofillFrameworkType", (int)savedItem.Type);
|
|
|
|
|
switch(savedItem.Type)
|
|
|
|
|
{
|
|
|
|
|
case CipherType.Login:
|
|
|
|
|
intent.PutExtra("autofillFrameworkName", parser.Uri.Replace(Constants.AndroidAppProtocol, string.Empty));
|
|
|
|
|
intent.PutExtra("autofillFrameworkUri", parser.Uri);
|
|
|
|
|
intent.PutExtra("autofillFrameworkUsername", savedItem.Login.Username);
|
|
|
|
|
intent.PutExtra("autofillFrameworkPassword", savedItem.Login.Password);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Toast.MakeText(this, "Unable to save this type of form.", ToastLength.Short).Show();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-11-17 22:38:56 +03:00
|
|
|
|
StartActivity(intent);
|
2017-11-15 07:13:55 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|