From 8cda4e89195809a78634a6edcf64c525c3832184 Mon Sep 17 00:00:00 2001 From: Philipp Crocoll Date: Wed, 20 Jan 2016 21:14:24 +0100 Subject: [PATCH] integrate KP2A Accessibility Service for AutoFill into main app --- src/keepass2android/Credentials.cs | 21 ++ .../Kp2aAccessibilityService.cs | 272 ++++++++++++++++++ .../drawable-xhdpi/ic_notify_autofill.png | Bin 0 -> 1295 bytes .../Resources/values/strings.xml | 7 +- .../Resources/xml/accserviceconfig.xml | 10 + src/keepass2android/keepass2android.csproj | 10 + .../services/CopyToClipboardService.cs | 3 + 7 files changed, 322 insertions(+), 1 deletion(-) create mode 100644 src/keepass2android/Credentials.cs create mode 100644 src/keepass2android/Kp2aAccessibilityService.cs create mode 100644 src/keepass2android/Resources/drawable-xhdpi/ic_notify_autofill.png create mode 100644 src/keepass2android/Resources/xml/accserviceconfig.xml diff --git a/src/keepass2android/Credentials.cs b/src/keepass2android/Credentials.cs new file mode 100644 index 00000000..8f09f772 --- /dev/null +++ b/src/keepass2android/Credentials.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using Android.App; +using Android.Content; +using Android.OS; +using Android.Runtime; +using Android.Views; +using Android.Widget; + +namespace keepass2android.AutoFillPlugin +{ + public class Credentials + { + public string User; + public string Password; + public string Url; + } +} \ No newline at end of file diff --git a/src/keepass2android/Kp2aAccessibilityService.cs b/src/keepass2android/Kp2aAccessibilityService.cs new file mode 100644 index 00000000..645b58e0 --- /dev/null +++ b/src/keepass2android/Kp2aAccessibilityService.cs @@ -0,0 +1,272 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using Android.App; +using Android.Content; +using Android.OS; +using Android.Runtime; +using Android.Views; +using Android.Views.Accessibility; +using Android.Widget; +using KeePassLib; + + +namespace keepass2android.AutoFill +{ + // + [Service(Enabled =true, Permission= "android.permission.BIND_ACCESSIBILITY_SERVICE")] + [IntentFilter(new[] { "android.accessibilityservice.AccessibilityService" })] + [MetaData("android.accessibilityservice", Resource = "@xml/accserviceconfig")] + public class Kp2aAccessibilityService : Android.AccessibilityServices.AccessibilityService + { + private static bool _hasUsedData; + const string _logTag = "KP2AAS"; + private const int autoFillNotificationId = 98810; + private const string androidAppPrefix = "androidapp://"; + + public override void OnCreate() + { + base.OnCreate(); + Android.Util.Log.Debug(_logTag, "OnCreate Service"); + } + + protected override void OnServiceConnected() + { + Android.Util.Log.Debug(_logTag, "service connected"); + base.OnServiceConnected(); + } + + public override void OnAccessibilityEvent(AccessibilityEvent e) + { + + Android.Util.Log.Debug(_logTag, "OnAccEvent"); + if (e.EventType == EventTypes.WindowContentChanged || e.EventType == EventTypes.WindowStateChanged) + { + Android.Util.Log.Debug(_logTag, "event: " + e.EventType + ", package = " + e.PackageName); + if (e.PackageName == "com.android.systemui") + return; //avoid that the notification is cancelled when pulling down notif drawer + var root = RootInActiveWindow; + if ((ExistsNodeOrChildren(root, n => n.WindowId == e.WindowId) && !ExistsNodeOrChildren(root, n => (n.ViewIdResourceName != null) && (n.ViewIdResourceName.StartsWith("com.android.systemui"))))) + { + bool cancelNotification = true; + + var allEditTexts = GetNodeOrChildren(root, n=> { return IsEditText(n); }); + + var usernameEdit = allEditTexts.TakeWhile(edit => (edit.Password == false)).LastOrDefault(); + + string searchString = androidAppPrefix + root.PackageName; + + string url = androidAppPrefix + root.PackageName; + + if (root.PackageName == "com.android.chrome") + { + var addressField = root.FindAccessibilityNodeInfosByViewId("com.android.chrome:id/url_bar").FirstOrDefault(); + UrlFromAddressField(ref url, addressField); + + } + else if (root.PackageName == "com.android.browser") + { + var addressField = root.FindAccessibilityNodeInfosByViewId("com.android.browser:id/url").FirstOrDefault(); + UrlFromAddressField(ref url, addressField); + } + + List emptyPasswordFields = GetNodeOrChildren(root, n => { return IsPasswordField(n); }).ToList(); + if (emptyPasswordFields.Any()) + { + if ((LastReceivedCredentialsUser != null) && IsSame(GetCredentialsField(PwDefs.UrlField), url)) + { + Android.Util.Log.Debug ("KP2AAS", "Filling credentials for " + url); + + FillPassword(url, usernameEdit, emptyPasswordFields); + } + else + { + Android.Util.Log.Debug ("KP2AAS", "Notif for " + url ); + if (LastReceivedCredentialsUser != null) + { + Android.Util.Log.Debug ("KP2AAS", GetCredentialsField(PwDefs.UrlField)); + Android.Util.Log.Debug ("KP2AAS", url); + } + + AskFillPassword(url, usernameEdit, emptyPasswordFields); + cancelNotification = false; + } + + } + if (cancelNotification) + { + ((NotificationManager)GetSystemService(NotificationService)).Cancel(autoFillNotificationId); + Android.Util.Log.Debug ("KP2AAS","Cancel notif"); + } + } + + } + GC.Collect(); + + + } + private static void UrlFromAddressField(ref string url, AccessibilityNodeInfo addressField) + { + if (addressField != null) + { + url = addressField.Text; + if (!url.Contains("://")) + url = "http://" + url; + } + + } + + private bool IsSame(string url1, string url2) + { + if (url1.StartsWith ("androidapp://")) + return url1 == url2; + return KeePassLib.Utility.UrlUtil.GetHost (url1) == KeePassLib.Utility.UrlUtil.GetHost (url2); + } + + private static bool IsPasswordField(AccessibilityNodeInfo n) + { + //if (n.Password) Android.Util.Log.Debug(_logTag, "pwdx with " + (n.Text == null ? "null" : n.Text)); + var res = n.Password && string.IsNullOrEmpty(n.Text); + // if (n.Password) Android.Util.Log.Debug(_logTag, "pwd with " + n.Text + res); + return res; + } + + private static bool IsEditText(AccessibilityNodeInfo n) + { + //it seems like n.Editable is not a good check as this is false for some fields which are actually editable, at least in tests with Chrome. + return (n.ClassName != null) && (n.ClassName.Contains("EditText")); + } + + private void AskFillPassword(string url, AccessibilityNodeInfo usernameEdit, IEnumerable passwordFields) + { + + Intent startKp2aIntent = PackageManager.GetLaunchIntentForPackage(ApplicationContext.PackageName); + if (startKp2aIntent != null) + { + startKp2aIntent.AddCategory(Intent.CategoryLauncher); + startKp2aIntent.AddFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask); + string taskName = "SearchUrlTask"; + startKp2aIntent.PutExtra("KP2A_APPTASK", taskName); + startKp2aIntent.PutExtra("UrlToSearch", url); + } + + + var pending = PendingIntent.GetActivity(this, 0, startKp2aIntent, PendingIntentFlags.UpdateCurrent); + var targetName = url; + + if (url.StartsWith(androidAppPrefix)) + { + var packageName = url.Substring(androidAppPrefix.Length); + try + { + var appInfo = PackageManager.GetApplicationInfo(packageName, 0); + targetName = (string) (appInfo != null ? PackageManager.GetApplicationLabel(appInfo) : packageName); + } + catch (Exception e) + { + Android.Util.Log.Debug(_logTag, e.ToString()); + targetName = packageName; + } + } + else + { + targetName = KeePassLib.Utility.UrlUtil.GetHost(url); + } + + + var builder = new Notification.Builder(this); + //TODO icon + //TODO plugin icon + builder.SetSmallIcon(Resource.Drawable.ic_notify_autofill) + .SetContentText(GetString(Resource.String.NotificationContentText, new Java.Lang.Object[] { targetName })) + .SetContentTitle(GetString(Resource.String.NotificationTitle)) + .SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis()) + .SetVisibility(Android.App.NotificationVisibility.Secret) + .SetContentIntent(pending); + var notificationManager = (NotificationManager)GetSystemService(NotificationService); + notificationManager.Notify(autoFillNotificationId, builder.Build()); + + } + + private void FillPassword(string url, AccessibilityNodeInfo usernameEdit, IEnumerable passwordFields) + { + if ((Keepass2android.Kbbridge.KeyboardData.HasData) && (_hasUsedData == false)) + { + FillDataInTextField(usernameEdit, LastReceivedCredentialsUser); + foreach (var pwd in passwordFields) + FillDataInTextField(pwd, LastReceivedCredentialsPassword); + + _hasUsedData = true; + } + + + + //LookupCredentialsActivity.LastReceivedCredentials = null; + } + + public string LastReceivedCredentialsPassword + { + get { return GetCredentialsField(PwDefs.PasswordField); } + } + + public string GetCredentialsField(string key) + { + var field = Keepass2android.Kbbridge.KeyboardData.AvailableFields + .Cast().SingleOrDefault(x => x.Key == key); + if (field == null) + return null; + return field.Value; + } + + public string LastReceivedCredentialsUser + { + get { return GetCredentialsField(PwDefs.UserNameField); } + } + + private static void FillDataInTextField(AccessibilityNodeInfo edit, string newValue) + { + if (newValue == null) + return; + Bundle b = new Bundle(); + b.PutString(AccessibilityNodeInfo.ActionArgumentSetTextCharsequence, newValue); + edit.PerformAction(Android.Views.Accessibility.Action.SetText, b); + } + + private bool ExistsNodeOrChildren(AccessibilityNodeInfo n, Func p) + { + return GetNodeOrChildren(n, p).Any(); + } + + private IEnumerable GetNodeOrChildren(AccessibilityNodeInfo n, Func p) + { + if (n != null) + { + if (p(n)) + yield return n; + for (int i = 0; i < n.ChildCount; i++) + { + foreach (var x in GetNodeOrChildren(n.GetChild(i), p)) + yield return x; + } + } + + } + + public override void OnInterrupt() + { + + } + + public void OnCancel(IDialogInterface dialog) + { + + } + + public static void NotifyNewData() + { + _hasUsedData = false; + } + } +} \ No newline at end of file diff --git a/src/keepass2android/Resources/drawable-xhdpi/ic_notify_autofill.png b/src/keepass2android/Resources/drawable-xhdpi/ic_notify_autofill.png new file mode 100644 index 0000000000000000000000000000000000000000..b7ffa4864664fc654e42e7597c6901bd80ea56a9 GIT binary patch literal 1295 zcmV+q1@QWbP)C)=<-Y&`1e{4k zK~!ko?U+l5TvZT;zv}MWJ!$ip#5f_upurajIzAE0bW5h4l(aUr;fQKA?`k@%QFHxUyw5kV!Pqh{hTGwJEa?;>^KaMRP>)3>{AH3gS* z@9F9~bxu|NRdwJZ{mV$wvM$RrABTIbc+SUq5nu%{1eAe4fYZPU;9FqA%s!R$JTPWvqml|{R_=!tGCr<# z(;tyEr3qxCXcl*x>)E_!t520S}nj*?7Hf zTVS1}AA=Jt`}=2-Zjy9qn{tLFjY`@dSRi(@NlB|6v8-E)XB{$sOSr+Wl5TgNsGUUv zD31L5dPygOn3?gzS<~(0H6EOnB)!`#F?F!cYl*-=E@@FJD+HLoC!zeC3BEfe9h7u` zGT7&lJ1Xgbq^A<=>`GYpIY*~VnoVqwbS6NV&waiiDKF`ApD#Pl)+Vy@p3JI&yvGxp z_|WHL)VeH8l#M9U0x};4h8=09fx9FPdBUjzR|ja44Zi1FE8~1~K6e8^fH>P-Zr#6M zH?!)3UDE??nWUqENndEdQ;_tD|Ndn&bH}xiq&J-ZcS)-o6n49VWLK?JDuqs5Cm&?m ziVNG)EV*@3%Ov>&*Oa7krBbO+Pfu@OkPEp|RbahOm4P$B_yt+;d~(bA!jYNP+q7;$ zQoaFy&Bw1LMN?B#)l#W+b2E930>sR$?gbjaB|zTH&Na%AW>)nLx0m|OYJsASsOlAh z6^q3vilSS(S_fUEE>kiHmjip|aS8oXdO55bu+Pj6HH(2oUTOAQ;Z{`h{P6{Fs8tZh ziD}R_P!AH^yc@^{_4y#ulK*xP_n-~UyH({+#>{4Wi9$u--k{a%Sa^7pu`jsBhUroS8XL@NuN@+Lza#?zlbNzw{n0(i?Cl2gEq zEsD5y=wwS2YSqHE2(-MJ%}Uw}JOWGtF9GkE*)eZg(jZ^0>-R-Xu1_{e`Xz4A`&G=1 z0AHmjCF;Op;4w3MU($ELUuL#h(qiDSyKS}uxf$SDGkd#P4CH`yDG;azDisable Ask after error Send error reports - + + Look up credentials + KP2A AutoFillPlugin + Monitors apps and websites for password fields. Offers to look up credentials from Keepass2Android and auto-fill them into the forms. + Keepass2Android AutoFill + AutoFill form for %1$s Version 0.9.8c\n diff --git a/src/keepass2android/Resources/xml/accserviceconfig.xml b/src/keepass2android/Resources/xml/accserviceconfig.xml new file mode 100644 index 00000000..ec890aac --- /dev/null +++ b/src/keepass2android/Resources/xml/accserviceconfig.xml @@ -0,0 +1,10 @@ + + diff --git a/src/keepass2android/keepass2android.csproj b/src/keepass2android/keepass2android.csproj index d1dfb0da..911b0e54 100644 --- a/src/keepass2android/keepass2android.csproj +++ b/src/keepass2android/keepass2android.csproj @@ -128,6 +128,7 @@ + @@ -154,6 +155,7 @@ + @@ -1669,6 +1671,14 @@ + + + + + + Designer + + diff --git a/src/keepass2android/services/CopyToClipboardService.cs b/src/keepass2android/services/CopyToClipboardService.cs index 278913b5..e6a7e6e1 100644 --- a/src/keepass2android/services/CopyToClipboardService.cs +++ b/src/keepass2android/services/CopyToClipboardService.cs @@ -30,6 +30,7 @@ using Android.Preferences; using KeePassLib; using KeePassLib.Utility; using Android.Views.InputMethods; +using keepass2android.AutoFill; using KeePass.Util.Spr; namespace keepass2android @@ -527,6 +528,8 @@ namespace keepass2android kbdataBuilder.Commit(); Keepass2android.Kbbridge.KeyboardData.EntryName = entry.OutputStrings.ReadSafe(PwDefs.TitleField); Keepass2android.Kbbridge.KeyboardData.EntryId = entry.Uuid.ToHexString(); + if (hasData) + Kp2aAccessibilityService.NotifyNewData(); return hasData; #endif