2020-01-09 20:17:16 +03:00
|
|
|
|
using System;
|
2019-04-30 21:33:00 +03:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Android.App;
|
|
|
|
|
using Android.Content;
|
|
|
|
|
using Android.OS;
|
|
|
|
|
using Android.Runtime;
|
2020-01-09 20:17:16 +03:00
|
|
|
|
using Android.Views;
|
2019-04-30 21:33:00 +03:00
|
|
|
|
using Android.Views.Accessibility;
|
2020-01-09 20:17:16 +03:00
|
|
|
|
using Android.Widget;
|
2019-04-30 21:33:00 +03:00
|
|
|
|
using Bit.App.Resources;
|
|
|
|
|
using Bit.Core;
|
|
|
|
|
using Bit.Core.Abstractions;
|
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Droid.Accessibility
|
|
|
|
|
{
|
|
|
|
|
[Service(Permission = Android.Manifest.Permission.BindAccessibilityService, Label = "Bitwarden")]
|
|
|
|
|
[IntentFilter(new string[] { "android.accessibilityservice.AccessibilityService" })]
|
|
|
|
|
[MetaData("android.accessibilityservice", Resource = "@xml/accessibilityservice")]
|
|
|
|
|
[Register("com.x8bit.bitwarden.Accessibility.AccessibilityService")]
|
|
|
|
|
public class AccessibilityService : Android.AccessibilityServices.AccessibilityService
|
|
|
|
|
{
|
|
|
|
|
private const string BitwardenPackage = "com.x8bit.bitwarden";
|
|
|
|
|
private const string BitwardenWebsite = "vault.bitwarden.com";
|
|
|
|
|
|
2020-02-14 02:33:37 +03:00
|
|
|
|
private IStorageService _storageService;
|
2020-03-26 19:15:33 +03:00
|
|
|
|
private IBroadcasterService _broadcasterService;
|
2020-02-14 02:33:37 +03:00
|
|
|
|
private DateTime? _lastSettingsReload = null;
|
|
|
|
|
private TimeSpan _settingsReloadSpan = TimeSpan.FromMinutes(1);
|
|
|
|
|
private HashSet<string> _blacklistedUris;
|
2020-01-28 01:36:20 +03:00
|
|
|
|
private AccessibilityNodeInfo _anchorNode = null;
|
2020-01-29 16:46:21 +03:00
|
|
|
|
private int _lastAnchorX = 0;
|
|
|
|
|
private int _lastAnchorY = 0;
|
2020-02-06 03:40:44 +03:00
|
|
|
|
private bool _isOverlayAboveAnchor = false;
|
2020-01-28 01:36:20 +03:00
|
|
|
|
private static bool _overlayAnchorObserverRunning = false;
|
|
|
|
|
private IWindowManager _windowManager = null;
|
|
|
|
|
private LinearLayout _overlayView = null;
|
2020-02-06 03:40:44 +03:00
|
|
|
|
private int _overlayViewHeight = 0;
|
2020-01-28 01:36:20 +03:00
|
|
|
|
private long _lastAutoFillTime = 0;
|
|
|
|
|
private Java.Lang.Runnable _overlayAnchorObserverRunnable = null;
|
|
|
|
|
private Handler _handler = new Handler(Looper.MainLooper);
|
2020-01-09 20:17:16 +03:00
|
|
|
|
|
2019-04-30 21:33:00 +03:00
|
|
|
|
private HashSet<string> _launcherPackageNames = null;
|
|
|
|
|
private DateTime? _lastLauncherSetBuilt = null;
|
|
|
|
|
private TimeSpan _rebuildLauncherSpan = TimeSpan.FromHours(1);
|
|
|
|
|
|
2020-03-26 19:15:33 +03:00
|
|
|
|
public override void OnCreate()
|
|
|
|
|
{
|
|
|
|
|
base.OnCreate();
|
|
|
|
|
LoadServices();
|
|
|
|
|
var settingsTask = LoadSettingsAsync();
|
|
|
|
|
_broadcasterService.Subscribe(nameof(AccessibilityService), (message) =>
|
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (message.Command == "OnAutofillTileClick")
|
2020-03-26 19:15:33 +03:00
|
|
|
|
{
|
|
|
|
|
var runnable = new Java.Lang.Runnable(OnAutofillTileClick);
|
|
|
|
|
_handler.PostDelayed(runnable, 250);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
AccessibilityHelpers.IsAccessibilityBroadcastReady = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
AccessibilityHelpers.IsAccessibilityBroadcastReady = false;
|
|
|
|
|
_broadcasterService.Unsubscribe(nameof(AccessibilityService));
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-30 21:33:00 +03:00
|
|
|
|
public override void OnAccessibilityEvent(AccessibilityEvent e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var powerManager = GetSystemService(PowerService) as PowerManager;
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (Build.VERSION.SdkInt > BuildVersionCodes.KitkatWatch && !powerManager.IsInteractive)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-28 16:16:28 +03:00
|
|
|
|
else if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop && !powerManager.IsScreenOn)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (SkipPackage(e?.PackageName))
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (e?.PackageName != "com.android.systemui")
|
2020-01-28 01:36:20 +03:00
|
|
|
|
{
|
|
|
|
|
CancelOverlayPrompt();
|
|
|
|
|
}
|
2019-04-30 21:33:00 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-29 16:46:21 +03:00
|
|
|
|
// AccessibilityHelpers.PrintTestData(RootInActiveWindow, e);
|
2019-04-30 21:33:00 +03:00
|
|
|
|
|
2020-02-14 02:33:37 +03:00
|
|
|
|
LoadServices();
|
|
|
|
|
var settingsTask = LoadSettingsAsync();
|
2020-01-28 01:36:20 +03:00
|
|
|
|
AccessibilityNodeInfo root = null;
|
|
|
|
|
|
2020-03-28 16:16:28 +03:00
|
|
|
|
switch (e.EventType)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
|
|
|
|
case EventTypes.ViewFocused:
|
2020-01-09 20:17:16 +03:00
|
|
|
|
case EventTypes.ViewClicked:
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (e.Source == null || e.PackageName == BitwardenPackage)
|
2020-01-28 01:36:20 +03:00
|
|
|
|
{
|
|
|
|
|
CancelOverlayPrompt();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root = RootInActiveWindow;
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (root == null || root.PackageName != e.PackageName)
|
2020-01-28 01:36:20 +03:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-01-29 16:46:21 +03:00
|
|
|
|
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (!(e.Source?.Password ?? false) && !AccessibilityHelpers.IsUsernameEditText(root, e))
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-01-09 20:17:16 +03:00
|
|
|
|
CancelOverlayPrompt();
|
2019-04-30 21:33:00 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (ScanAndAutofill(root, e))
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-01-28 01:36:20 +03:00
|
|
|
|
CancelOverlayPrompt();
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
2020-01-09 20:17:16 +03:00
|
|
|
|
else
|
|
|
|
|
{
|
2020-01-28 01:36:20 +03:00
|
|
|
|
OverlayPromptToAutofill(root, e);
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case EventTypes.WindowContentChanged:
|
|
|
|
|
case EventTypes.WindowStateChanged:
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (AccessibilityHelpers.LastCredentials == null)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (e.PackageName == BitwardenPackage)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-01-28 01:36:20 +03:00
|
|
|
|
CancelOverlayPrompt();
|
2019-04-30 21:33:00 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 01:36:20 +03:00
|
|
|
|
root = RootInActiveWindow;
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (root == null || root.PackageName != e.PackageName)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (ScanAndAutofill(root, e))
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-01-09 20:17:16 +03:00
|
|
|
|
CancelOverlayPrompt();
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Suppress exceptions so that service doesn't crash.
|
2020-03-28 16:16:28 +03:00
|
|
|
|
catch (Exception ex)
|
2020-01-09 20:17:16 +03:00
|
|
|
|
{
|
2020-01-28 01:36:20 +03:00
|
|
|
|
System.Diagnostics.Debug.WriteLine(">>> {0}: {1}", ex.GetType(), ex.StackTrace);
|
2020-01-09 20:17:16 +03:00
|
|
|
|
}
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnInterrupt()
|
|
|
|
|
{
|
|
|
|
|
// Do nothing.
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 20:17:16 +03:00
|
|
|
|
public bool ScanAndAutofill(AccessibilityNodeInfo root, AccessibilityEvent e)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-01-09 20:17:16 +03:00
|
|
|
|
var filled = false;
|
2020-05-06 16:48:34 +03:00
|
|
|
|
var uri = AccessibilityHelpers.GetUri(root);
|
|
|
|
|
if (uri != null && !uri.Contains(BitwardenWebsite) &&
|
|
|
|
|
AccessibilityHelpers.NeedToAutofill(AccessibilityHelpers.LastCredentials, uri))
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-05-06 16:48:34 +03:00
|
|
|
|
var allEditTexts = AccessibilityHelpers.GetWindowNodes(root, e, n => AccessibilityHelpers.EditText(n), false);
|
|
|
|
|
var usernameEditText = AccessibilityHelpers.GetUsernameEditText(uri, allEditTexts);
|
|
|
|
|
var passwordNodes = AccessibilityHelpers.GetWindowNodes(root, e, n => n.Password, false);
|
|
|
|
|
if (usernameEditText != null || passwordNodes.Count > 0)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-05-06 16:48:34 +03:00
|
|
|
|
AccessibilityHelpers.FillCredentials(usernameEditText, passwordNodes);
|
|
|
|
|
filled = true;
|
|
|
|
|
_lastAutoFillTime = Java.Lang.JavaSystem.CurrentTimeMillis();
|
|
|
|
|
AccessibilityHelpers.LastCredentials = null;
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
2020-05-06 16:48:34 +03:00
|
|
|
|
allEditTexts.Dispose();
|
|
|
|
|
passwordNodes.Dispose();
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
2020-05-06 16:48:34 +03:00
|
|
|
|
if (AccessibilityHelpers.LastCredentials != null)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(1000);
|
|
|
|
|
AccessibilityHelpers.LastCredentials = null;
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-01-09 20:17:16 +03:00
|
|
|
|
return filled;
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
2020-03-26 19:15:33 +03:00
|
|
|
|
|
|
|
|
|
private void OnAutofillTileClick()
|
|
|
|
|
{
|
|
|
|
|
CancelOverlayPrompt();
|
|
|
|
|
|
|
|
|
|
var root = RootInActiveWindow;
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (root != null && root.PackageName != BitwardenPackage &&
|
2020-03-26 19:15:33 +03:00
|
|
|
|
root.PackageName != AccessibilityHelpers.SystemUiPackage &&
|
|
|
|
|
!SkipPackage(root.PackageName))
|
|
|
|
|
{
|
|
|
|
|
var uri = AccessibilityHelpers.GetUri(root);
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(uri))
|
2020-03-26 19:15:33 +03:00
|
|
|
|
{
|
|
|
|
|
var intent = new Intent(this, typeof(AccessibilityActivity));
|
|
|
|
|
intent.PutExtra("uri", uri);
|
|
|
|
|
intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.SingleTop | ActivityFlags.ClearTop);
|
|
|
|
|
StartActivity(intent);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Toast.MakeText(this, AppResources.AutofillTileUriNotFound, ToastLength.Long).Show();
|
|
|
|
|
}
|
2019-04-30 21:33:00 +03:00
|
|
|
|
|
2020-01-09 20:17:16 +03:00
|
|
|
|
private void CancelOverlayPrompt()
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-01-28 01:36:20 +03:00
|
|
|
|
_overlayAnchorObserverRunning = false;
|
|
|
|
|
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (_windowManager != null && _overlayView != null)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-03-26 19:15:33 +03:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_windowManager.RemoveViewImmediate(_overlayView);
|
|
|
|
|
System.Diagnostics.Debug.WriteLine(">>> Accessibility Overlay View Removed");
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
2020-01-09 20:17:16 +03:00
|
|
|
|
|
|
|
|
|
_overlayView = null;
|
2020-01-14 01:14:57 +03:00
|
|
|
|
_lastAnchorX = 0;
|
|
|
|
|
_lastAnchorY = 0;
|
2020-02-06 03:40:44 +03:00
|
|
|
|
_isOverlayAboveAnchor = false;
|
2020-01-28 01:36:20 +03:00
|
|
|
|
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (_anchorNode != null)
|
2020-01-28 01:36:20 +03:00
|
|
|
|
{
|
|
|
|
|
_anchorNode.Recycle();
|
|
|
|
|
_anchorNode = null;
|
|
|
|
|
}
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 20:17:16 +03:00
|
|
|
|
private void OverlayPromptToAutofill(AccessibilityNodeInfo root, AccessibilityEvent e)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-04-09 21:57:06 +03:00
|
|
|
|
if (Java.Lang.JavaSystem.CurrentTimeMillis() - _lastAutoFillTime < 1000 ||
|
|
|
|
|
AccessibilityHelpers.IsAutofillServicePromptVisible(Windows))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (!AccessibilityHelpers.OverlayPermitted())
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (!AccessibilityHelpers.IsAutofillTileAdded)
|
2020-03-26 19:15:33 +03:00
|
|
|
|
{
|
|
|
|
|
// The user has the option of only using the autofill tile and leaving the overlay permission
|
|
|
|
|
// disabled, so only show this toast if they're using accessibility without overlay permission and
|
|
|
|
|
// have _not_ added the autofill tile
|
|
|
|
|
System.Diagnostics.Debug.WriteLine(">>> Overlay Permission not granted");
|
|
|
|
|
Toast.MakeText(this, AppResources.AccessibilityOverlayPermissionAlert, ToastLength.Long).Show();
|
|
|
|
|
}
|
2020-01-28 01:36:20 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (_overlayView != null || _anchorNode != null || _overlayAnchorObserverRunning)
|
2020-01-28 01:36:20 +03:00
|
|
|
|
{
|
|
|
|
|
CancelOverlayPrompt();
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 20:17:16 +03:00
|
|
|
|
var uri = AccessibilityHelpers.GetUri(root);
|
2020-02-14 02:33:37 +03:00
|
|
|
|
var fillable = !string.IsNullOrWhiteSpace(uri);
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (fillable)
|
2020-02-14 02:33:37 +03:00
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (_blacklistedUris != null && _blacklistedUris.Any())
|
2020-02-14 02:33:37 +03:00
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (Uri.TryCreate(uri, UriKind.Absolute, out var parsedUri) && parsedUri.Scheme.StartsWith("http"))
|
2020-02-14 02:33:37 +03:00
|
|
|
|
{
|
|
|
|
|
fillable = !_blacklistedUris.Contains(
|
|
|
|
|
string.Format("{0}://{1}", parsedUri.Scheme, parsedUri.Host));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fillable = !_blacklistedUris.Contains(uri);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (!fillable)
|
2020-01-09 20:17:16 +03:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-02-14 02:33:37 +03:00
|
|
|
|
|
2020-01-28 01:36:20 +03:00
|
|
|
|
var intent = new Intent(this, typeof(AccessibilityActivity));
|
|
|
|
|
intent.PutExtra("uri", uri);
|
|
|
|
|
intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.SingleTop | ActivityFlags.ClearTop);
|
|
|
|
|
|
|
|
|
|
_overlayView = AccessibilityHelpers.GetOverlayView(this);
|
2020-02-14 02:33:37 +03:00
|
|
|
|
_overlayView.Measure(View.MeasureSpec.MakeMeasureSpec(0, 0),
|
2020-02-06 03:40:44 +03:00
|
|
|
|
View.MeasureSpec.MakeMeasureSpec(0, 0));
|
|
|
|
|
_overlayViewHeight = _overlayView.MeasuredHeight;
|
2020-01-28 01:36:20 +03:00
|
|
|
|
_overlayView.Click += (sender, eventArgs) =>
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-01-28 01:36:20 +03:00
|
|
|
|
CancelOverlayPrompt();
|
|
|
|
|
StartActivity(intent);
|
|
|
|
|
};
|
2020-01-09 20:17:16 +03:00
|
|
|
|
|
2020-02-06 03:40:44 +03:00
|
|
|
|
var layoutParams = AccessibilityHelpers.GetOverlayLayoutParams();
|
2020-04-09 21:57:06 +03:00
|
|
|
|
var anchorPosition = AccessibilityHelpers.GetOverlayAnchorPosition(this, e.Source,
|
|
|
|
|
_overlayViewHeight, _isOverlayAboveAnchor);
|
2020-02-06 03:40:44 +03:00
|
|
|
|
layoutParams.X = anchorPosition.X;
|
|
|
|
|
layoutParams.Y = anchorPosition.Y;
|
|
|
|
|
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (_windowManager == null)
|
2020-02-06 03:40:44 +03:00
|
|
|
|
{
|
|
|
|
|
_windowManager = GetSystemService(WindowService).JavaCast<IWindowManager>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 01:36:20 +03:00
|
|
|
|
_anchorNode = e.Source;
|
|
|
|
|
_lastAnchorX = anchorPosition.X;
|
|
|
|
|
_lastAnchorY = anchorPosition.Y;
|
2020-01-14 01:14:57 +03:00
|
|
|
|
|
2020-01-28 01:36:20 +03:00
|
|
|
|
_windowManager.AddView(_overlayView, layoutParams);
|
2020-01-09 20:17:16 +03:00
|
|
|
|
|
2020-01-28 01:36:20 +03:00
|
|
|
|
System.Diagnostics.Debug.WriteLine(">>> Accessibility Overlay View Added at X:{0} Y:{1}",
|
|
|
|
|
layoutParams.X, layoutParams.Y);
|
2020-01-09 20:17:16 +03:00
|
|
|
|
|
2020-01-28 01:36:20 +03:00
|
|
|
|
StartOverlayAnchorObserver();
|
|
|
|
|
}
|
2020-01-14 01:14:57 +03:00
|
|
|
|
|
2020-01-28 01:36:20 +03:00
|
|
|
|
private void StartOverlayAnchorObserver()
|
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (_overlayAnchorObserverRunning)
|
2020-01-28 01:36:20 +03:00
|
|
|
|
{
|
|
|
|
|
return;
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
2020-01-14 01:14:57 +03:00
|
|
|
|
|
2020-01-29 16:46:21 +03:00
|
|
|
|
_overlayAnchorObserverRunning = true;
|
2020-01-28 01:36:20 +03:00
|
|
|
|
_overlayAnchorObserverRunnable = new Java.Lang.Runnable(() =>
|
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (_overlayAnchorObserverRunning)
|
2020-01-28 01:36:20 +03:00
|
|
|
|
{
|
|
|
|
|
AdjustOverlayForScroll();
|
|
|
|
|
_handler.PostDelayed(_overlayAnchorObserverRunnable, 250);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_handler.PostDelayed(_overlayAnchorObserverRunnable, 250);
|
2020-01-14 01:14:57 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 01:36:20 +03:00
|
|
|
|
private void AdjustOverlayForScroll()
|
2020-01-14 01:14:57 +03:00
|
|
|
|
{
|
2020-04-09 21:57:06 +03:00
|
|
|
|
if (_overlayView == null || _anchorNode == null ||
|
|
|
|
|
AccessibilityHelpers.IsAutofillServicePromptVisible(Windows))
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-01-28 01:36:20 +03:00
|
|
|
|
CancelOverlayPrompt();
|
2020-01-14 01:14:57 +03:00
|
|
|
|
return;
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 01:36:20 +03:00
|
|
|
|
var root = RootInActiveWindow;
|
|
|
|
|
IEnumerable<AccessibilityWindowInfo> windows = null;
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (Build.VERSION.SdkInt > BuildVersionCodes.Kitkat)
|
2020-01-28 01:36:20 +03:00
|
|
|
|
{
|
|
|
|
|
windows = Windows;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 21:57:06 +03:00
|
|
|
|
var anchorPosition = AccessibilityHelpers.GetOverlayAnchorPosition(this, _anchorNode, root,
|
|
|
|
|
windows, _overlayViewHeight, _isOverlayAboveAnchor);
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (anchorPosition == null)
|
2020-01-14 01:14:57 +03:00
|
|
|
|
{
|
2020-01-28 01:36:20 +03:00
|
|
|
|
CancelOverlayPrompt();
|
2020-01-14 01:14:57 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-28 16:16:28 +03:00
|
|
|
|
else if (anchorPosition.X == -1 && anchorPosition.Y == -1)
|
2020-01-14 01:14:57 +03:00
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (_overlayView.Visibility != ViewStates.Gone)
|
2020-01-28 01:36:20 +03:00
|
|
|
|
{
|
|
|
|
|
_overlayView.Visibility = ViewStates.Gone;
|
2020-02-06 03:40:44 +03:00
|
|
|
|
System.Diagnostics.Debug.WriteLine(">>> Accessibility Overlay View Hidden");
|
2020-01-28 01:36:20 +03:00
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-28 16:16:28 +03:00
|
|
|
|
else if (anchorPosition.X == -1)
|
2020-02-06 03:40:44 +03:00
|
|
|
|
{
|
|
|
|
|
_isOverlayAboveAnchor = false;
|
|
|
|
|
System.Diagnostics.Debug.WriteLine(">>> Accessibility Overlay View Below Anchor");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-28 16:16:28 +03:00
|
|
|
|
else if (anchorPosition.Y == -1)
|
2020-02-06 03:40:44 +03:00
|
|
|
|
{
|
|
|
|
|
_isOverlayAboveAnchor = true;
|
|
|
|
|
System.Diagnostics.Debug.WriteLine(">>> Accessibility Overlay View Above Anchor");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-28 16:16:28 +03:00
|
|
|
|
else if (anchorPosition.X == _lastAnchorX && anchorPosition.Y == _lastAnchorY)
|
2020-01-28 01:36:20 +03:00
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (_overlayView.Visibility != ViewStates.Visible)
|
2020-01-28 01:36:20 +03:00
|
|
|
|
{
|
|
|
|
|
_overlayView.Visibility = ViewStates.Visible;
|
|
|
|
|
}
|
2020-01-14 01:14:57 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var layoutParams = AccessibilityHelpers.GetOverlayLayoutParams();
|
|
|
|
|
layoutParams.X = anchorPosition.X;
|
|
|
|
|
layoutParams.Y = anchorPosition.Y;
|
|
|
|
|
|
|
|
|
|
_lastAnchorX = anchorPosition.X;
|
|
|
|
|
_lastAnchorY = anchorPosition.Y;
|
|
|
|
|
|
2020-01-28 01:36:20 +03:00
|
|
|
|
_windowManager.UpdateViewLayout(_overlayView, layoutParams);
|
|
|
|
|
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (_overlayView.Visibility != ViewStates.Visible)
|
2020-01-28 01:36:20 +03:00
|
|
|
|
{
|
|
|
|
|
_overlayView.Visibility = ViewStates.Visible;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-14 01:14:57 +03:00
|
|
|
|
System.Diagnostics.Debug.WriteLine(">>> Accessibility Overlay View Updated to X:{0} Y:{1}",
|
|
|
|
|
layoutParams.X, layoutParams.Y);
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool SkipPackage(string eventPackageName)
|
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (string.IsNullOrWhiteSpace(eventPackageName) ||
|
2019-04-30 21:33:00 +03:00
|
|
|
|
AccessibilityHelpers.FilteredPackageNames.Contains(eventPackageName) ||
|
|
|
|
|
eventPackageName.Contains("launcher"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (_launcherPackageNames == null || _lastLauncherSetBuilt == null ||
|
2019-04-30 21:33:00 +03:00
|
|
|
|
(DateTime.Now - _lastLauncherSetBuilt.Value) > _rebuildLauncherSpan)
|
|
|
|
|
{
|
|
|
|
|
// refresh launcher list every now and then
|
|
|
|
|
_lastLauncherSetBuilt = DateTime.Now;
|
|
|
|
|
var intent = new Intent(Intent.ActionMain);
|
|
|
|
|
intent.AddCategory(Intent.CategoryHome);
|
|
|
|
|
var resolveInfo = PackageManager.QueryIntentActivities(intent, 0);
|
|
|
|
|
_launcherPackageNames = resolveInfo.Select(ri => ri.ActivityInfo.PackageName).ToHashSet();
|
|
|
|
|
}
|
|
|
|
|
return _launcherPackageNames.Contains(eventPackageName);
|
|
|
|
|
}
|
2020-02-14 02:33:37 +03:00
|
|
|
|
|
|
|
|
|
private void LoadServices()
|
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (_storageService == null)
|
2020-02-14 02:33:37 +03:00
|
|
|
|
{
|
|
|
|
|
_storageService = ServiceContainer.Resolve<IStorageService>("storageService");
|
|
|
|
|
}
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (_broadcasterService == null)
|
2020-03-26 19:15:33 +03:00
|
|
|
|
{
|
|
|
|
|
_broadcasterService = ServiceContainer.Resolve<IBroadcasterService>("broadcasterService");
|
|
|
|
|
}
|
2020-02-14 02:33:37 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task LoadSettingsAsync()
|
|
|
|
|
{
|
|
|
|
|
var now = DateTime.UtcNow;
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (_lastSettingsReload == null || (now - _lastSettingsReload.Value) > _settingsReloadSpan)
|
2020-02-14 02:33:37 +03:00
|
|
|
|
{
|
|
|
|
|
_lastSettingsReload = now;
|
|
|
|
|
var uris = await _storageService.GetAsync<List<string>>(Constants.AutofillBlacklistedUrisKey);
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (uris != null)
|
2020-02-14 02:33:37 +03:00
|
|
|
|
{
|
|
|
|
|
_blacklistedUris = new HashSet<string>(uris);
|
|
|
|
|
}
|
2020-03-26 19:15:33 +03:00
|
|
|
|
var isAutoFillTileAdded = await _storageService.GetAsync<bool?>(Constants.AutofillTileAdded);
|
|
|
|
|
AccessibilityHelpers.IsAutofillTileAdded = isAutoFillTileAdded.GetValueOrDefault();
|
2020-02-14 02:33:37 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
|
|
|
|
}
|