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;
|
2020-01-09 20:17:16 +03:00
|
|
|
|
using Android.Graphics;
|
2019-04-30 21:33:00 +03:00
|
|
|
|
using Android.OS;
|
2020-01-09 20:17:16 +03:00
|
|
|
|
using Android.Provider;
|
2019-04-30 21:33:00 +03:00
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
private string _lastNotificationUri = null;
|
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-01-09 20:17:16 +03:00
|
|
|
|
private IWindowManager _windowManager = null;
|
|
|
|
|
private LinearLayout _overlayView = null;
|
|
|
|
|
|
2019-04-30 21:33:00 +03:00
|
|
|
|
public override void OnAccessibilityEvent(AccessibilityEvent e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var powerManager = GetSystemService(PowerService) as PowerManager;
|
|
|
|
|
if(Build.VERSION.SdkInt > BuildVersionCodes.KitkatWatch && !powerManager.IsInteractive)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if(Build.VERSION.SdkInt < BuildVersionCodes.Lollipop && !powerManager.IsScreenOn)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(SkipPackage(e?.PackageName))
|
|
|
|
|
{
|
2020-01-09 20:17:16 +03:00
|
|
|
|
CancelOverlayPrompt();
|
2019-04-30 21:33:00 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var root = RootInActiveWindow;
|
|
|
|
|
if(root == null || root.PackageName != e.PackageName)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 20:17:16 +03:00
|
|
|
|
// AccessibilityHelpers.PrintTestData(root, e);
|
2019-04-30 21:33:00 +03:00
|
|
|
|
|
|
|
|
|
switch(e.EventType)
|
|
|
|
|
{
|
|
|
|
|
case EventTypes.ViewFocused:
|
2020-01-09 20:17:16 +03:00
|
|
|
|
case EventTypes.ViewClicked:
|
2020-01-09 21:17:17 +03:00
|
|
|
|
var isKnownBroswer = AccessibilityHelpers.SupportedBrowsers.ContainsKey(root.PackageName);
|
|
|
|
|
if(e.EventType == EventTypes.ViewClicked && isKnownBroswer)
|
2020-01-09 20:17:16 +03:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-01-09 21:17:17 +03:00
|
|
|
|
if(e.Source == null || !e.Source.Password)
|
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;
|
|
|
|
|
}
|
|
|
|
|
if(e.PackageName == BitwardenPackage)
|
|
|
|
|
{
|
2020-01-09 20:17:16 +03:00
|
|
|
|
CancelOverlayPrompt();
|
2019-04-30 21:33:00 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
2020-01-09 20:17:16 +03:00
|
|
|
|
if(ScanAndAutofill(root, e))
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-01-09 20:17:16 +03:00
|
|
|
|
CancelOverlayPrompt();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
OverlayPromptToAutofill(root, e);
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case EventTypes.WindowContentChanged:
|
|
|
|
|
case EventTypes.WindowStateChanged:
|
2020-01-09 21:17:17 +03:00
|
|
|
|
if(e.Source == null || e.Source.Password)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-01-09 20:17:16 +03:00
|
|
|
|
else if(AccessibilityHelpers.LastCredentials == null)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
|
|
|
|
if(string.IsNullOrWhiteSpace(_lastNotificationUri))
|
|
|
|
|
{
|
2020-01-09 20:17:16 +03:00
|
|
|
|
CancelOverlayPrompt();
|
2019-04-30 21:33:00 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
var uri = AccessibilityHelpers.GetUri(root);
|
2020-01-03 23:19:20 +03:00
|
|
|
|
if(uri != null && uri != _lastNotificationUri)
|
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
|
|
|
|
}
|
2020-01-03 23:19:20 +03:00
|
|
|
|
else if(uri != null && uri.StartsWith(Constants.AndroidAppProtocol))
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(e.PackageName == BitwardenPackage)
|
|
|
|
|
{
|
2020-01-09 20:17:16 +03:00
|
|
|
|
CancelOverlayPrompt();
|
2019-04-30 21:33:00 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 20:17:16 +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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.Dispose();
|
|
|
|
|
e.Dispose();
|
|
|
|
|
}
|
|
|
|
|
// Suppress exceptions so that service doesn't crash.
|
2020-01-09 21:17:17 +03:00
|
|
|
|
catch(Exception ex)
|
2020-01-09 20:17:16 +03:00
|
|
|
|
{
|
|
|
|
|
System.Diagnostics.Debug.WriteLine(">>> Exception: " + ex.StackTrace);
|
|
|
|
|
}
|
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;
|
2019-04-30 21:33:00 +03:00
|
|
|
|
var passwordNodes = AccessibilityHelpers.GetWindowNodes(root, e, n => n.Password, false);
|
|
|
|
|
if(passwordNodes.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var uri = AccessibilityHelpers.GetUri(root);
|
|
|
|
|
if(uri != null && !uri.Contains(BitwardenWebsite))
|
|
|
|
|
{
|
|
|
|
|
if(AccessibilityHelpers.NeedToAutofill(AccessibilityHelpers.LastCredentials, uri))
|
|
|
|
|
{
|
|
|
|
|
AccessibilityHelpers.GetNodesAndFill(root, e, passwordNodes);
|
2020-01-09 20:17:16 +03:00
|
|
|
|
filled = true;
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
2020-01-09 20:17:16 +03:00
|
|
|
|
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
|
|
|
|
AccessibilityHelpers.LastCredentials = null;
|
|
|
|
|
}
|
|
|
|
|
else if(AccessibilityHelpers.LastCredentials != null)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(1000);
|
|
|
|
|
AccessibilityHelpers.LastCredentials = null;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
passwordNodes.Dispose();
|
2020-01-09 20:17:16 +03:00
|
|
|
|
return filled;
|
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-09 20:17:16 +03:00
|
|
|
|
if(_windowManager == null || _overlayView == null)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-01-09 20:17:16 +03:00
|
|
|
|
|
|
|
|
|
_windowManager.RemoveViewImmediate(_overlayView);
|
|
|
|
|
System.Diagnostics.Debug.WriteLine(">>> Accessibility Overlay View Removed");
|
|
|
|
|
|
|
|
|
|
_overlayView = null;
|
2019-04-30 21:33:00 +03:00
|
|
|
|
_lastNotificationUri = null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 20:17:16 +03:00
|
|
|
|
private void OverlayPromptToAutofill(AccessibilityNodeInfo root, AccessibilityEvent e)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-01-10 18:20:19 +03:00
|
|
|
|
if(!AccessibilityHelpers.OverlayPermitted())
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-01-09 20:17:16 +03:00
|
|
|
|
System.Diagnostics.Debug.WriteLine(">>> Overlay Permission not granted");
|
|
|
|
|
Toast.MakeText(this, AppResources.AccessibilityOverlayPermissionAlert, ToastLength.Long).Show();
|
2019-04-30 21:33:00 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 20:17:16 +03:00
|
|
|
|
var uri = AccessibilityHelpers.GetUri(root);
|
2020-01-09 21:17:17 +03:00
|
|
|
|
if(string.IsNullOrWhiteSpace(uri))
|
2020-01-09 20:17:16 +03:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WindowManagerTypes windowManagerType;
|
2020-01-09 21:17:17 +03:00
|
|
|
|
if(Build.VERSION.SdkInt >= BuildVersionCodes.O)
|
2020-01-09 20:17:16 +03:00
|
|
|
|
{
|
|
|
|
|
windowManagerType = WindowManagerTypes.ApplicationOverlay;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
windowManagerType = WindowManagerTypes.Phone;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var layoutParams = new WindowManagerLayoutParams(
|
|
|
|
|
ViewGroup.LayoutParams.WrapContent,
|
|
|
|
|
ViewGroup.LayoutParams.WrapContent,
|
|
|
|
|
windowManagerType,
|
|
|
|
|
WindowManagerFlags.NotFocusable | WindowManagerFlags.NotTouchModal,
|
2020-01-09 21:17:17 +03:00
|
|
|
|
Format.Transparent);
|
2020-01-09 20:17:16 +03:00
|
|
|
|
|
|
|
|
|
var anchorPosition = AccessibilityHelpers.GetOverlayAnchorPosition(root, e);
|
|
|
|
|
|
|
|
|
|
layoutParams.Gravity = GravityFlags.Bottom | GravityFlags.Left;
|
|
|
|
|
layoutParams.X = anchorPosition.X;
|
|
|
|
|
layoutParams.Y = anchorPosition.Y;
|
|
|
|
|
|
2019-04-30 21:33:00 +03:00
|
|
|
|
var intent = new Intent(this, typeof(AccessibilityActivity));
|
|
|
|
|
intent.PutExtra("uri", uri);
|
|
|
|
|
intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.SingleTop | ActivityFlags.ClearTop);
|
|
|
|
|
|
2020-01-09 21:17:17 +03:00
|
|
|
|
if(_windowManager == null)
|
2020-01-09 20:17:16 +03:00
|
|
|
|
{
|
|
|
|
|
_windowManager = GetSystemService(WindowService).JavaCast<IWindowManager>();
|
|
|
|
|
}
|
2019-04-30 21:33:00 +03:00
|
|
|
|
|
2020-01-09 20:17:16 +03:00
|
|
|
|
var updateView = false;
|
2020-01-09 21:17:17 +03:00
|
|
|
|
if(_overlayView != null)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-01-09 20:17:16 +03:00
|
|
|
|
updateView = true;
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
2020-01-09 20:17:16 +03:00
|
|
|
|
|
|
|
|
|
_overlayView = AccessibilityHelpers.GetOverlayView(this);
|
2020-01-09 21:17:17 +03:00
|
|
|
|
_overlayView.Click += (sender, eventArgs) =>
|
|
|
|
|
{
|
2020-01-09 20:17:16 +03:00
|
|
|
|
CancelOverlayPrompt();
|
|
|
|
|
StartActivity(intent);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_lastNotificationUri = uri;
|
|
|
|
|
|
2020-01-09 21:17:17 +03:00
|
|
|
|
if(updateView)
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-01-09 20:17:16 +03:00
|
|
|
|
_windowManager.UpdateViewLayout(_overlayView, layoutParams);
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
2020-01-09 20:17:16 +03:00
|
|
|
|
else
|
2019-04-30 21:33:00 +03:00
|
|
|
|
{
|
2020-01-09 20:17:16 +03:00
|
|
|
|
_windowManager.AddView(_overlayView, layoutParams);
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 21:17:17 +03:00
|
|
|
|
System.Diagnostics.Debug.WriteLine(">>> Accessibility Overlay View {0} X:{1} Y:{2}",
|
|
|
|
|
updateView ? "Updated to" : "Added at", layoutParams.X, layoutParams.Y);
|
2019-04-30 21:33:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool SkipPackage(string eventPackageName)
|
|
|
|
|
{
|
|
|
|
|
if(string.IsNullOrWhiteSpace(eventPackageName) ||
|
|
|
|
|
AccessibilityHelpers.FilteredPackageNames.Contains(eventPackageName) ||
|
|
|
|
|
eventPackageName.Contains("launcher"))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if(_launcherPackageNames == null || _lastLauncherSetBuilt == null ||
|
|
|
|
|
(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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|