From a458b9bc880192b6e83ef5a559c06d6534a816b4 Mon Sep 17 00:00:00 2001 From: Matt Portune <59324545+mportune-bw@users.noreply.github.com> Date: Tue, 16 Jun 2020 11:09:26 -0400 Subject: [PATCH] Workaround for disabling predictive text in visible password fields (#983) * Workaround for disabling predictive text in visible password fields * Fix for non-master branch iOS builds (#984) * Enable extra workflow steps to allow iOS builds to succeed from non-master branch * re-enable provisioning profile setup --- src/Android/Renderers/CustomEntryRenderer.cs | 43 +++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/Android/Renderers/CustomEntryRenderer.cs b/src/Android/Renderers/CustomEntryRenderer.cs index 046ceb7c8..bbe6e1f88 100644 --- a/src/Android/Renderers/CustomEntryRenderer.cs +++ b/src/Android/Renderers/CustomEntryRenderer.cs @@ -1,5 +1,9 @@ -using Android.Content; +using System.ComponentModel; +using Android.Content; +using Android.Graphics; +using Android.Text; using Android.Views.InputMethods; +using Android.Widget; using Bit.Droid.Renderers; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; @@ -24,5 +28,42 @@ namespace Bit.Droid.Renderers (ImeAction)ImeFlags.NoExtractUi; } } + + // Workaround for failure to disable text prediction on non-password fields + // see https://github.com/xamarin/Xamarin.Forms/issues/10857 + protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) + { + base.OnElementPropertyChanged(sender, e); + + // Check if changed property is "IsPassword", otherwise ignore + if (e.PropertyName == Entry.IsPasswordProperty.PropertyName) + { + // Check if field type is text, otherwise ignore (numeric passwords, etc.) + EditText.InputType = Element.Keyboard.ToInputType(); + if ((EditText.InputType & InputTypes.ClassText) == InputTypes.ClassText) + { + if (Element.IsPassword) + { + // Element is a password field, set inputType to TextVariationPassword which disables + // predictive text by default + EditText.InputType = EditText.InputType | InputTypes.TextVariationPassword; + } + else + { + // Element is not a password field, set inputType to TextVariationVisiblePassword to + // disable predictive text while still displaying the content. + EditText.InputType = EditText.InputType | InputTypes.TextVariationVisiblePassword; + } + + // The workaround above forces a reset of the style properties, so we need to re-apply the font. + // see https://xamarin.github.io/bugzilla-archives/33/33666/bug.html + var typeface = Typeface.CreateFromAsset(Context.Assets, "RobotoMono_Regular.ttf"); + if (Control is TextView label) + { + label.Typeface = typeface; + } + } + } + } } }