switch to main activity when locked

This commit is contained in:
Kyle Spearrin 2017-11-17 09:21:12 -05:00
parent 322b251def
commit 8c89b0e587
4 changed files with 94 additions and 36 deletions

View file

@ -58,12 +58,14 @@ namespace Bit.Android.Autofill
return null;
}
public static FillResponse BuildAuthResponse(Context context, FieldCollection fields)
public static FillResponse BuildAuthResponse(Context context, FieldCollection fields, string uri)
{
var responseBuilder = new FillResponse.Builder();
var view = BuildListView(context.PackageName, "Autofill with bitwarden",
"Vault locked", Resource.Drawable.icon);
var intent = new Intent(context, typeof(AuthActivity));
var intent = new Intent(context, typeof(MainActivity));
intent.PutExtra("uri", uri);
intent.PutExtra("autofillFramework", true);
var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.CancelCurrent);
responseBuilder.SetAuthentication(fields.AutofillIds.ToArray(), pendingIntent.IntentSender, view);
return responseBuilder.Build();

View file

@ -4,9 +4,7 @@ using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Service.Autofill;
using Android.Views;
using Bit.App.Abstractions;
using System.Collections.Generic;
using System.Linq;
using XLabs.Ioc;
@ -42,9 +40,10 @@ namespace Bit.Android.Autofill
_lockService = Resolver.Resolve<ILockService>();
}
if(true) // if locked
var isLocked = (await _lockService.GetLockTypeAsync(false)) != App.Enums.LockType.None;
if(isLocked)
{
var authResponse = AutofillHelpers.BuildAuthResponse(this, parser.FieldCollection);
var authResponse = AutofillHelpers.BuildAuthResponse(this, parser.FieldCollection, parser.Uri);
callback.OnSuccess(authResponse);
return;
}

View file

@ -1,28 +1,45 @@
using System;
using System.Collections.Generic;
using Android.Service.Autofill;
using Android.Views;
using Android.Views.Autofill;
using System.Linq;
using Android.Text;
using Bit.App.Models;
using Bit.App.Enums;
using Bit.App.Models.Page;
namespace Bit.Android.Autofill
{
public class CipherFilledItem : IFilledItem
{
private readonly Cipher _cipher;
private Lazy<string> _password;
public CipherFilledItem(Cipher cipher)
{
_cipher = cipher;
Name = cipher.Name?.Decrypt() ?? "--";
Type = cipher.Type;
switch(cipher.Type)
switch(Type)
{
case CipherType.Login:
Subtitle = _cipher.Login.Username?.Decrypt() ?? string.Empty;
Subtitle = cipher.Login.Username?.Decrypt() ?? string.Empty;
_password = new Lazy<string>(() => cipher.Login.Password?.Decrypt());
Icon = Resource.Drawable.login;
break;
default:
break;
}
}
public CipherFilledItem(VaultListPageModel.Cipher cipher)
{
Name = cipher.Name ?? "--";
Type = cipher.Type;
switch(Type)
{
case CipherType.Login:
Subtitle = cipher.LoginUsername ?? string.Empty;
_password = cipher.LoginPassword;
Icon = Resource.Drawable.login;
break;
default:
@ -33,10 +50,11 @@ namespace Bit.Android.Autofill
public string Name { get; set; }
public string Subtitle { get; set; } = string.Empty;
public int Icon { get; set; } = Resource.Drawable.login;
public CipherType Type { get; set; }
public bool ApplyToFields(FieldCollection fieldCollection, Dataset.Builder datasetBuilder)
{
if(_cipher.Type == CipherType.Login && _cipher.Login != null)
if(Type == CipherType.Login)
{
var passwordField = fieldCollection.Fields.FirstOrDefault(
f => f.InputType.HasFlag(InputTypes.TextVariationPassword));
@ -51,13 +69,12 @@ namespace Bit.Android.Autofill
return false;
}
var password = _cipher.Login.Password?.Decrypt();
if(string.IsNullOrWhiteSpace(password))
if(string.IsNullOrWhiteSpace(_password.Value))
{
return false;
}
datasetBuilder.SetValue(passwordField.AutofillId, AutofillValue.ForText(password));
datasetBuilder.SetValue(passwordField.AutofillId, AutofillValue.ForText(_password.Value));
var usernameField = fieldCollection.Fields.TakeWhile(f => f.Id != passwordField.Id).LastOrDefault();
if(usernameField != null)

View file

@ -19,6 +19,10 @@ using Android.Nfc;
using Android.Views.InputMethods;
using System.IO;
using System.Linq;
using Android.Views.Autofill;
using Android.App.Assist;
using Bit.Android.Autofill;
using System.Collections.Generic;
namespace Bit.Android
{
@ -131,7 +135,42 @@ namespace Bit.Android
private void ReturnCredentials(VaultListPageModel.Cipher cipher)
{
Intent data = new Intent();
if(Intent.GetBooleanExtra("autofillFramework", false))
{
if(cipher == null)
{
SetResult(Result.Canceled);
Finish();
return;
}
var structure = Intent.GetParcelableExtra(AutofillManager.ExtraAssistStructure) as AssistStructure;
if(structure == null)
{
SetResult(Result.Canceled);
Finish();
return;
}
var parser = new Parser(structure);
parser.ParseForFill();
if(!parser.FieldCollection.Fields.Any() || string.IsNullOrWhiteSpace(parser.Uri))
{
SetResult(Result.Canceled);
Finish();
return;
}
var items = new List<IFilledItem> { new CipherFilledItem(cipher) };
var response = AutofillHelpers.BuildFillResponse(this, parser.FieldCollection, items);
var replyIntent = new Intent();
replyIntent.PutExtra(AutofillManager.ExtraAuthenticationResult, response);
SetResult(Result.Ok, replyIntent);
Finish();
}
else
{
var data = new Intent();
if(cipher == null)
{
data.PutExtra("canceled", "true");
@ -161,6 +200,7 @@ namespace Bit.Android
Finish();
}
}
protected override void OnPause()
{