From e4c6285fab9393e4a801618c052d8a560f97cbaf Mon Sep 17 00:00:00 2001 From: Philipp Crocoll Date: Sat, 30 Dec 2017 21:30:18 +0100 Subject: [PATCH] improve presentation of Autofill (#9), support credit card datasets created from templates & expiry date --- .../layout/autofill_service_list_item.xml | 5 ++ src/keepass2android/app/App.cs | 9 ++- .../services/AutofillBase/AutofillHelper.cs | 6 +- .../AutofillBase/AutofillServiceBase.cs | 4 +- .../Kp2aAutofill/ChooseForAutofillActivity.cs | 78 +++++++++++++++++-- .../Kp2aAutofill/Kp2aAutofillService.cs | 2 +- .../services/Kp2aAutofillIntentBuilder.cs | 5 ++ 7 files changed, 91 insertions(+), 18 deletions(-) diff --git a/src/keepass2android/Resources/layout/autofill_service_list_item.xml b/src/keepass2android/Resources/layout/autofill_service_list_item.xml index b238ba41..44c1cd74 100644 --- a/src/keepass2android/Resources/layout/autofill_service_list_item.xml +++ b/src/keepass2android/Resources/layout/autofill_service_list_item.xml @@ -36,6 +36,11 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" + + android:maxWidth="28dp" + android:maxHeight="28dp" + android:adjustViewBounds="true" + android:layout_marginEnd="?android:attr/listPreferredItemPaddingEnd" android:src="@drawable/ic_launcher" /> \ No newline at end of file diff --git a/src/keepass2android/app/App.cs b/src/keepass2android/app/App.cs index 656ac04c..fc859254 100644 --- a/src/keepass2android/app/App.cs +++ b/src/keepass2android/app/App.cs @@ -69,15 +69,16 @@ namespace keepass2android { #if DEBUG public const string AppName = "@string/app_name_debug"; + public const int AppNameResource = Resource.String.app_name_debug; #else public const string AppName = "@string/app_name"; + public const int AppNameResource = Resource.String.app_name; #endif - public const int AppNameResource = Resource.String.app_name; - public const string AppNameShort = "@string/short_app_name" + "DBG"; - public const string AppLauncherTitle = "@string/app_name" + " Debug"; + + #if DEBUG - public const string PackagePart = "keepass2android_debug"; + public const string PackagePart = "keepass2android_debug"; public const string Searchable = "@xml/searchable_debug"; #else public const string PackagePart = "keepass2android"; diff --git a/src/keepass2android/services/AutofillBase/AutofillHelper.cs b/src/keepass2android/services/AutofillBase/AutofillHelper.cs index d8f83f73..32c09a9e 100644 --- a/src/keepass2android/services/AutofillBase/AutofillHelper.cs +++ b/src/keepass2android/services/AutofillBase/AutofillHelper.cs @@ -33,16 +33,14 @@ namespace keepass2android.services.AutofillBase if (datasetAuth) { datasetBuilder = new Dataset.Builder - (NewRemoteViews(context.PackageName, datasetName, - Resource.Drawable.ic_launcher)); + (NewRemoteViews(context.PackageName, datasetName, intentBuilder.AppIconResource)); IntentSender sender = intentBuilder.GetAuthIntentSenderForDataset(context, datasetName); datasetBuilder.SetAuthentication(sender); } else { datasetBuilder = new Dataset.Builder - (NewRemoteViews(context.PackageName, datasetName, - Resource.Drawable.ic_launcher)); + (NewRemoteViews(context.PackageName, datasetName, intentBuilder.AppIconResource)); } var setValueAtLeastOnce = filledAutofillFieldCollection.ApplyToFields(autofillFields, datasetBuilder); if (setValueAtLeastOnce) diff --git a/src/keepass2android/services/AutofillBase/AutofillServiceBase.cs b/src/keepass2android/services/AutofillBase/AutofillServiceBase.cs index 66df9138..dbd16e9a 100644 --- a/src/keepass2android/services/AutofillBase/AutofillServiceBase.cs +++ b/src/keepass2android/services/AutofillBase/AutofillServiceBase.cs @@ -14,6 +14,8 @@ namespace keepass2android.services.AutofillBase IntentSender GetAuthIntentSenderForResponse(Context context, string query); IntentSender GetAuthIntentSenderForDataset(Context context, string dataset); Intent GetRestartAppIntent(Context context); + + int AppIconResource { get; } } public abstract class AutofillServiceBase: AutofillService @@ -67,7 +69,7 @@ namespace keepass2android.services.AutofillBase var responseBuilder = new FillResponse.Builder(); var sender = IntentBuilder.GetAuthIntentSenderForResponse(this, query); - RemoteViews presentation = AutofillHelper.NewRemoteViews(PackageName, GetString(Resource.String.autofill_sign_in_prompt),Resource.Drawable.ic_launcher); + RemoteViews presentation = AutofillHelper.NewRemoteViews(PackageName, GetString(Resource.String.autofill_sign_in_prompt), AppNames.LauncherIcon); var datasetBuilder = new Dataset.Builder(presentation); datasetBuilder.SetAuthentication(sender); diff --git a/src/keepass2android/services/Kp2aAutofill/ChooseForAutofillActivity.cs b/src/keepass2android/services/Kp2aAutofill/ChooseForAutofillActivity.cs index 25ca1f01..c51dad86 100644 --- a/src/keepass2android/services/Kp2aAutofill/ChooseForAutofillActivity.cs +++ b/src/keepass2android/services/Kp2aAutofill/ChooseForAutofillActivity.cs @@ -14,6 +14,7 @@ using keepass2android.services.AutofillBase; using keepass2android.services.AutofillBase.model; using Keepass2android.Pluginsdk; using KeePassLib; +using KeePassLib.Utility; namespace keepass2android.services.Kp2aAutofill { @@ -55,21 +56,82 @@ namespace keepass2android.services.Kp2aAutofill }; fieldCollection.Add(field); } - //TODO add support for Keepass templates - //TODO add values like expiration? - //TODO if cc-exp is there, also set cc-exp-month etc. + if (IsCreditCard(pwEntry) && pwEntry.Expires) + { + DateTime expTime = pwEntry.ExpiryTime; + FilledAutofillField field = + new FilledAutofillField + { + AutofillHints = new[] { View.AutofillHintCreditCardExpirationDate }, + DateValue = (long)(1000*TimeUtil.SerializeUnix(expTime)), + Protected = false + }; + fieldCollection.Add(field); + + field = + new FilledAutofillField + { + AutofillHints = new[] { View.AutofillHintCreditCardExpirationDay }, + TextValue = expTime.Day.ToString(), + Protected = false + }; + fieldCollection.Add(field); + + field = + new FilledAutofillField + { + AutofillHints = new[] { View.AutofillHintCreditCardExpirationMonth }, + TextValue = expTime.Month.ToString(), + Protected = false + }; + fieldCollection.Add(field); + + field = + new FilledAutofillField + { + AutofillHints = new[] { View.AutofillHintCreditCardExpirationYear }, + TextValue = expTime.Year.ToString(), + Protected = false + }; + fieldCollection.Add(field); + } + + fieldCollection.DatasetName = pwEntry.Strings.ReadSafe(PwDefs.TitleField); return fieldCollection; } - private static readonly Dictionary keyToHint = new Dictionary() + private bool IsCreditCard(PwEntry pwEntry) { - {PwDefs.UserNameField, View.AutofillHintUsername }, - {PwDefs.PasswordField, View.AutofillHintPassword }, - {PwDefs.UrlField, W3cHints.URL }, - }; + return pwEntry.Strings.Exists("cc-number") + || pwEntry.Strings.Exists("cc-csc") + || pwEntry.Strings.Exists(GetString(Resource.String.TemplateField_CreditCard_CVV)); + } + + private static readonly Dictionary keyToHint = BuildKeyToHint(); + + private static Dictionary BuildKeyToHint() + { + var result = new Dictionary + { + {PwDefs.UserNameField, View.AutofillHintUsername}, + {PwDefs.PasswordField, View.AutofillHintPassword}, + {PwDefs.UrlField, W3cHints.URL}, + { + Android.App.Application.Context.GetString(Resource.String.TemplateField_CreditCard_CVV), + View.AutofillHintCreditCardSecurityCode + }, + { + Android.App.Application.Context.GetString(Resource.String.TemplateField_CreditCard_Owner), + W3cHints.CC_NAME + }, + {Android.App.Application.Context.GetString(Resource.String.TemplateField_Number), View.AutofillHintCreditCardNumber}, + {Android.App.Application.Context.GetString(Resource.String.TemplateField_IdCard_Name), View.AutofillHintName}, + }; + return result; + } private string GetCanonicalHintFromKp2aField(PwEntry pwEntry, string key) { diff --git a/src/keepass2android/services/Kp2aAutofill/Kp2aAutofillService.cs b/src/keepass2android/services/Kp2aAutofill/Kp2aAutofillService.cs index fdb81185..90220048 100644 --- a/src/keepass2android/services/Kp2aAutofill/Kp2aAutofillService.cs +++ b/src/keepass2android/services/Kp2aAutofill/Kp2aAutofillService.cs @@ -8,7 +8,7 @@ using AutofillServiceBase = keepass2android.services.AutofillBase.AutofillServic namespace keepass2android.services { - [Service(Label = "Keepass2Android Autofill Service", Permission=Manifest.Permission.BindAutofillService)] + [Service(Label = AppNames.AppName, Permission=Manifest.Permission.BindAutofillService)] [IntentFilter(new [] {"android.service.autofill.AutofillService"})] [MetaData("android.autofill", Resource = "@xml/autofillservice")] [Register("keepass2android.services.Kp2aAutofillService")] diff --git a/src/keepass2android/services/Kp2aAutofillIntentBuilder.cs b/src/keepass2android/services/Kp2aAutofillIntentBuilder.cs index 010e5f4a..aca990fd 100644 --- a/src/keepass2android/services/Kp2aAutofillIntentBuilder.cs +++ b/src/keepass2android/services/Kp2aAutofillIntentBuilder.cs @@ -36,5 +36,10 @@ namespace keepass2android.services intent.AddFlags(ActivityFlags.ForwardResult); return intent; } + + public int AppIconResource + { + get { return AppNames.LauncherIcon; } + } } } \ No newline at end of file