From d66eaf88552159616d3c60a2fc4fcff175c002e6 Mon Sep 17 00:00:00 2001 From: Matt Portune <59324545+mportune-bw@users.noreply.github.com> Date: Thu, 9 Apr 2020 14:57:06 -0400 Subject: [PATCH] Additional accessibility tweaks (#825) * Additional accessibility tweaks * Cleanup --- .../Accessibility/AccessibilityHelpers.cs | 41 ++++++++++--------- .../Accessibility/AccessibilityService.cs | 25 ++++++----- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/Android/Accessibility/AccessibilityHelpers.cs b/src/Android/Accessibility/AccessibilityHelpers.cs index b98ba377c..85b85e476 100644 --- a/src/Android/Accessibility/AccessibilityHelpers.cs +++ b/src/Android/Accessibility/AccessibilityHelpers.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using Android.Content; -using Android.Content.Res; using Android.Graphics; using Android.OS; using Android.Provider; @@ -11,7 +10,6 @@ using Android.Views.Accessibility; using Android.Widget; using Bit.App.Resources; using Bit.Core; -using Plugin.CurrentActivity; namespace Bit.Droid.Accessibility { @@ -368,8 +366,8 @@ namespace Bit.Droid.Accessibility return layoutParams; } - public static Point GetOverlayAnchorPosition(AccessibilityNodeInfo anchorView, int overlayViewHeight, - bool isOverlayAboveAnchor) + public static Point GetOverlayAnchorPosition(AccessibilityService service, AccessibilityNodeInfo anchorView, + int overlayViewHeight, bool isOverlayAboveAnchor) { var anchorViewRect = new Rect(); anchorView.GetBoundsInScreen(anchorViewRect); @@ -381,13 +379,14 @@ namespace Bit.Droid.Accessibility { anchorViewY -= overlayViewHeight; } - anchorViewY -= GetStatusBarHeight(); + anchorViewY -= GetStatusBarHeight(service); return new Point(anchorViewX, anchorViewY); } - public static Point GetOverlayAnchorPosition(AccessibilityNodeInfo anchorNode, AccessibilityNodeInfo root, - IEnumerable windows, int overlayViewHeight, bool isOverlayAboveAnchor) + public static Point GetOverlayAnchorPosition(AccessibilityService service, AccessibilityNodeInfo anchorNode, + AccessibilityNodeInfo root, IEnumerable windows, int overlayViewHeight, + bool isOverlayAboveAnchor) { Point point = null; if (anchorNode != null) @@ -420,9 +419,10 @@ namespace Bit.Droid.Accessibility { return null; } - var maxY = rootNodeHeight - GetNavigationBarHeight() - GetStatusBarHeight() - inputMethodHeight; + var maxY = rootNodeHeight - GetNavigationBarHeight(service) - GetStatusBarHeight(service) - + inputMethodHeight; - point = GetOverlayAnchorPosition(anchorNode, overlayViewHeight, isOverlayAboveAnchor); + point = GetOverlayAnchorPosition(service, anchorNode, overlayViewHeight, isOverlayAboveAnchor); if (point.Y < minY) { if (isOverlayAboveAnchor) @@ -501,6 +501,11 @@ namespace Bit.Droid.Accessibility } return inputMethodWindowHeight; } + + public static bool IsAutofillServicePromptVisible(IEnumerable windows) + { + return windows?.Any(w => w.Title?.ToLower().Contains("autofill") ?? false) ?? false; + } public static int GetNodeHeight(AccessibilityNodeInfo node) { @@ -515,26 +520,24 @@ namespace Bit.Droid.Accessibility return nodeRectHeight; } - private static int GetStatusBarHeight() + private static int GetStatusBarHeight(AccessibilityService service) { - return GetSystemResourceDimenPx("status_bar_height"); + return GetSystemResourceDimenPx(service, "status_bar_height"); } - private static int GetNavigationBarHeight() + private static int GetNavigationBarHeight(AccessibilityService service) { - return GetSystemResourceDimenPx("navigation_bar_height"); + return GetSystemResourceDimenPx(service, "navigation_bar_height"); } - private static int GetSystemResourceDimenPx(string resName) + private static int GetSystemResourceDimenPx(AccessibilityService service, string resName) { - var activity = (MainActivity)CrossCurrentActivity.Current.Activity; - var barHeight = 0; - var resourceId = activity.Resources.GetIdentifier(resName, "dimen", "android"); + var resourceId = service.Resources.GetIdentifier(resName, "dimen", "android"); if (resourceId > 0) { - barHeight = activity.Resources.GetDimensionPixelSize(resourceId); + return service.Resources.GetDimensionPixelSize(resourceId); } - return barHeight; + return 0; } } } diff --git a/src/Android/Accessibility/AccessibilityService.cs b/src/Android/Accessibility/AccessibilityService.cs index 66d957fd3..69011e649 100644 --- a/src/Android/Accessibility/AccessibilityService.cs +++ b/src/Android/Accessibility/AccessibilityService.cs @@ -4,9 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Android.App; using Android.Content; -using Android.Graphics; using Android.OS; -using Android.Provider; using Android.Runtime; using Android.Views; using Android.Views.Accessibility; @@ -15,7 +13,6 @@ using Bit.App.Resources; using Bit.Core; using Bit.Core.Abstractions; using Bit.Core.Utilities; -using Java.Util; namespace Bit.Droid.Accessibility { @@ -249,6 +246,12 @@ namespace Bit.Droid.Accessibility private void OverlayPromptToAutofill(AccessibilityNodeInfo root, AccessibilityEvent e) { + if (Java.Lang.JavaSystem.CurrentTimeMillis() - _lastAutoFillTime < 1000 || + AccessibilityHelpers.IsAutofillServicePromptVisible(Windows)) + { + return; + } + if (!AccessibilityHelpers.OverlayPermitted()) { if (!AccessibilityHelpers.IsAutofillTileAdded) @@ -267,11 +270,6 @@ namespace Bit.Droid.Accessibility CancelOverlayPrompt(); } - if (Java.Lang.JavaSystem.CurrentTimeMillis() - _lastAutoFillTime < 1000) - { - return; - } - var uri = AccessibilityHelpers.GetUri(root); var fillable = !string.IsNullOrWhiteSpace(uri); if (fillable) @@ -309,8 +307,8 @@ namespace Bit.Droid.Accessibility }; var layoutParams = AccessibilityHelpers.GetOverlayLayoutParams(); - var anchorPosition = AccessibilityHelpers.GetOverlayAnchorPosition(e.Source, _overlayViewHeight, - _isOverlayAboveAnchor); + var anchorPosition = AccessibilityHelpers.GetOverlayAnchorPosition(this, e.Source, + _overlayViewHeight, _isOverlayAboveAnchor); layoutParams.X = anchorPosition.X; layoutParams.Y = anchorPosition.Y; @@ -353,7 +351,8 @@ namespace Bit.Droid.Accessibility private void AdjustOverlayForScroll() { - if (_overlayView == null || _anchorNode == null) + if (_overlayView == null || _anchorNode == null || + AccessibilityHelpers.IsAutofillServicePromptVisible(Windows)) { CancelOverlayPrompt(); return; @@ -366,8 +365,8 @@ namespace Bit.Droid.Accessibility windows = Windows; } - var anchorPosition = AccessibilityHelpers.GetOverlayAnchorPosition(_anchorNode, root, windows, - _overlayViewHeight, _isOverlayAboveAnchor); + var anchorPosition = AccessibilityHelpers.GetOverlayAnchorPosition(this, _anchorNode, root, + windows, _overlayViewHeight, _isOverlayAboveAnchor); if (anchorPosition == null) { CancelOverlayPrompt();