From 0a6767209d8df5c18fe3d0faf015c34ba4f7a357 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 16 Nov 2017 22:34:19 -0500 Subject: [PATCH] layout updates --- src/Android/Autofill/AutofillHelpers.cs | 95 +++++++------------ src/Android/Autofill/AutofillService.cs | 2 +- src/Android/Autofill/CipherFilledItem.cs | 2 + src/Android/Autofill/FilledFieldCollection.cs | 4 +- src/Android/Autofill/IFilledItem.cs | 1 + .../Resources/layout/autofill_listitem.axml | 9 +- 6 files changed, 47 insertions(+), 66 deletions(-) diff --git a/src/Android/Autofill/AutofillHelpers.cs b/src/Android/Autofill/AutofillHelpers.cs index 0b5c6b204..ae4055ab1 100644 --- a/src/Android/Autofill/AutofillHelpers.cs +++ b/src/Android/Autofill/AutofillHelpers.cs @@ -4,60 +4,13 @@ using Android.Content; using Android.Service.Autofill; using Android.Views; using Android.Widget; -using System.Diagnostics; using System.Linq; namespace Bit.Android.Autofill { public static class AutofillHelpers { - /** - * Wraps autofill data in a LoginCredential Dataset object which can then be sent back to the - * client View. - */ - public static Dataset NewDataset(Context context, FieldCollection fields, IFilledItem filledItem, bool auth) - { - var itemName = filledItem.Name; - if(itemName != null) - { - Dataset.Builder datasetBuilder; - if(auth) - { - datasetBuilder = new Dataset.Builder( - NewRemoteViews(context.PackageName, itemName, filledItem.Subtitle, Resource.Drawable.fa_lock)); - //IntentSender sender = AuthActivity.getAuthIntentSenderForDataset(context, datasetName); - //datasetBuilder.SetAuthentication(sender); - } - else - { - datasetBuilder = new Dataset.Builder( - NewRemoteViews(context.PackageName, itemName, filledItem.Subtitle, Resource.Drawable.user)); - } - - var setValue = filledItem.ApplyToFields(fields, datasetBuilder); - if(setValue) - { - return datasetBuilder.Build(); - } - } - - return null; - } - - public static RemoteViews NewRemoteViews(string packageName, string text, string subtext, int iconId) - { - var views = new RemoteViews(packageName, Resource.Layout.autofill_listitem); - views.SetTextViewText(Resource.Id.text, text); - views.SetTextViewText(Resource.Id.text2, subtext); - views.SetImageViewResource(Resource.Id.icon, iconId); - return views; - } - - /** - * Wraps autofill data in a Response object (essentially a series of Datasets) which can then - * be sent back to the client View. - */ - public static FillResponse NewResponse(Context context, bool auth, FieldCollection fields, + public static FillResponse BuildFillResponse(Context context, bool auth, FieldCollection fields, IDictionary items) { var responseBuilder = new FillResponse.Builder(); @@ -65,7 +18,7 @@ namespace Bit.Android.Autofill { foreach(var datasetName in items.Keys) { - var dataset = NewDataset(context, fields, items[datasetName], auth); + var dataset = BuildDataset(context, fields, items[datasetName], auth); if(dataset != null) { responseBuilder.AddDataset(dataset); @@ -73,27 +26,47 @@ namespace Bit.Android.Autofill } } - if(true || fields.SaveType != SaveDataType.Generic) + var info = new SaveInfo.Builder(fields.SaveType, fields.AutofillIds.ToArray()).Build(); + responseBuilder.SetSaveInfo(info); + return responseBuilder.Build(); + } + + public static Dataset BuildDataset(Context context, FieldCollection fields, IFilledItem filledItem, bool auth) + { + Dataset.Builder datasetBuilder; + if(auth) { - var info = new SaveInfo.Builder(fields.SaveType, fields.AutofillIds.ToArray()).Build(); - responseBuilder.SetSaveInfo(info); - return responseBuilder.Build(); + datasetBuilder = new Dataset.Builder( + BuildListView(context.PackageName, filledItem.Name, filledItem.Subtitle, Resource.Drawable.fa_lock)); + //IntentSender sender = AuthActivity.getAuthIntentSenderForDataset(context, datasetName); + //datasetBuilder.SetAuthentication(sender); } else { - Debug.WriteLine("These fields are not meant to be saved by autofill."); - return null; + datasetBuilder = new Dataset.Builder( + BuildListView(context.PackageName, filledItem.Name, filledItem.Subtitle, filledItem.Icon)); } + + if(filledItem.ApplyToFields(fields, datasetBuilder)) + { + return datasetBuilder.Build(); + } + + return null; + } + + public static RemoteViews BuildListView(string packageName, string text, string subtext, int iconId) + { + var view = new RemoteViews(packageName, Resource.Layout.autofill_listitem); + view.SetTextViewText(Resource.Id.text, text); + view.SetTextViewText(Resource.Id.text2, subtext); + view.SetImageViewResource(Resource.Id.icon, iconId); + return view; } public static List FilterForSupportedHints(string[] hints) { - if(hints == null) - { - return new List(); - } - - return hints.Where(h => IsValidHint(h)).ToList(); + return hints?.Where(h => IsValidHint(h)).ToList() ?? new List(); } public static bool IsValidHint(string hint) diff --git a/src/Android/Autofill/AutofillService.cs b/src/Android/Autofill/AutofillService.cs index c522a3d43..bbae14a58 100644 --- a/src/Android/Autofill/AutofillService.cs +++ b/src/Android/Autofill/AutofillService.cs @@ -60,7 +60,7 @@ namespace Bit.Android.Autofill return; } - var response = AutofillHelpers.NewResponse(this, false, parser.FieldCollection, items); + var response = AutofillHelpers.BuildFillResponse(this, false, parser.FieldCollection, items); callback.OnSuccess(response); } diff --git a/src/Android/Autofill/CipherFilledItem.cs b/src/Android/Autofill/CipherFilledItem.cs index f3e60abe0..b99819793 100644 --- a/src/Android/Autofill/CipherFilledItem.cs +++ b/src/Android/Autofill/CipherFilledItem.cs @@ -23,6 +23,7 @@ namespace Bit.Android.Autofill { case CipherType.Login: Subtitle = _cipher.Login.Username?.Decrypt() ?? string.Empty; + Icon = Resource.Drawable.login; break; default: break; @@ -31,6 +32,7 @@ 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 bool ApplyToFields(FieldCollection fieldCollection, Dataset.Builder datasetBuilder) { diff --git a/src/Android/Autofill/FilledFieldCollection.cs b/src/Android/Autofill/FilledFieldCollection.cs index bd1954e69..4b0d57796 100644 --- a/src/Android/Autofill/FilledFieldCollection.cs +++ b/src/Android/Autofill/FilledFieldCollection.cs @@ -18,12 +18,14 @@ namespace Bit.Android.Autofill { HintToFieldMap = hintMap; Name = datasetName; - Subtitle = "username"; + Subtitle = "subtitle"; + Icon = Resource.Drawable.login; } public IDictionary HintToFieldMap { get; private set; } public string Name { get; set; } public string Subtitle { get; set; } + public int Icon { get; set; } public void Add(FilledField filledField) { diff --git a/src/Android/Autofill/IFilledItem.cs b/src/Android/Autofill/IFilledItem.cs index 5a81808af..ca41ee9c9 100644 --- a/src/Android/Autofill/IFilledItem.cs +++ b/src/Android/Autofill/IFilledItem.cs @@ -7,6 +7,7 @@ namespace Bit.Android.Autofill { string Name { get; set; } string Subtitle { get; set; } + int Icon { get; set; } bool ApplyToFields(FieldCollection fieldCollection, Dataset.Builder datasetBuilder); } } \ No newline at end of file diff --git a/src/Android/Resources/layout/autofill_listitem.axml b/src/Android/Resources/layout/autofill_listitem.axml index 2e1f41eaa..9c8390753 100644 --- a/src/Android/Resources/layout/autofill_listitem.axml +++ b/src/Android/Resources/layout/autofill_listitem.axml @@ -2,7 +2,10 @@ + android:layout_marginEnd="10dp" + android:src="@drawable/login" />