Forum Xamarin.Android

how to tap on the screen from accessibility service

i need to tap on the screen by x,y
it should tap no matter which application is focused,
should run from accessibility service
DispatchTouchEvent but it should run only from my View
any idea?

Answers

  • YorkGoYorkGo CNMember, Xamarin Team Xamurai
    edited February 2019

    how to tap on the screen from accessibility service

     
    Please refer to:

    And here is a BaseAccessibilityService:
    ~~~

    class BaseAccessibilityService : AccessibilityService
        {
            private AccessibilityManager mAccessibilityManager;
            private Context mContext;
            private static BaseAccessibilityService mInstance;
     
            public void init(Context context)
            {
                mContext = Android.App.Application.Context;
                mAccessibilityManager = (AccessibilityManager)mContext.GetSystemService(Context.AccessibilityService);
            }
     
            public static BaseAccessibilityService getInstance()
           {
                if (mInstance == null)
                {
                    mInstance = new BaseAccessibilityService();
                }
                return mInstance;
            }
     
            private bool checkAccessibilityEnabled(String serviceName)
            {
                var accessibilityServices =
                        mAccessibilityManager.GetEnabledAccessibilityServiceList(Android.AccessibilityServices.FeedbackFlags.Generic);
                foreach (AccessibilityServiceInfo info in accessibilityServices)
                {
                    if (info.Id.Equals(serviceName))
                    {
                        return true;
                    }
                }
                return false;
            }
     
            public void goAccess()
            {
                Intent intent = new Intent(Android.Provider.Settings.ActionAccessibilitySettings);
                intent.SetFlags(ActivityFlags.NewTask);
                mContext.StartActivity(intent);
            }
     
            public void performViewClick(AccessibilityNodeInfo nodeInfo)
            {
                if (nodeInfo == null)
                {
                    return;
                }
                while (nodeInfo != null)
                {
                    if (nodeInfo.Clickable)
                    {
                        nodeInfo.PerformAction(Android.Views.Accessibility.Action.Click);
                        break;
                    }
                    nodeInfo = nodeInfo.Parent;
                }
            }
     
            public void performBackClick()
            {
                try
                {
                    Java.Lang.Thread.Sleep(500);
                }
                catch (Java.Lang.InterruptedException e)
                {
                    e.PrintStackTrace();
                }
                PerformGlobalAction(GlobalAction.Back);
            }
     
            public void performScrollBackward()
            {
                try
                {
                    Java.Lang.Thread.Sleep(500);
                }
                catch (Java.Lang.InterruptedException e)
                {
                    e.PrintStackTrace();
                }
                PerformGlobalAction(Android.Views.Accessibility.Action.ScrollBackward);
            }
     
            public void performScrollForward()
            {
                try
                {
                    Java.Lang.Thread.Sleep(500);
                }
                catch (Java.Lang.InterruptedException e)
                {
                    e.PrintStackTrace();
                }
                PerformGlobalAction(Android.Views.Accessibility.Action.ScrollForward);
            }
     
            public AccessibilityNodeInfo findViewByText(String text)
            {
                return findViewByText(text, false);
            }
     
            public AccessibilityNodeInfo findViewByText(String text, bool clickable)
            {
                AccessibilityNodeInfo accessibilityNodeInfo = RootInActiveWindow;
                if (accessibilityNodeInfo == null)
                {
                    return null;
                }
                var nodeInfoList = (List<AccessibilityNodeInfo>)accessibilityNodeInfo.FindAccessibilityNodeInfosByText(text);
                if (nodeInfoList != null && !nodeInfoList.Any())
                {
                    foreach (AccessibilityNodeInfo nodeInfo in nodeInfoList)
                    {
                        if (nodeInfo != null && (nodeInfo.Checkable == clickable))
                        {
                            return nodeInfo;
                        }
                    }
                }
                return null;
            }
     
            public AccessibilityNodeInfo findViewByID(String id)
            {
                AccessibilityNodeInfo accessibilityNodeInfo = RootInActiveWindow;
                if (accessibilityNodeInfo == null)
                {
                    return null;
                }
                var nodeInfoList = accessibilityNodeInfo.FindAccessibilityNodeInfosByViewId(id);
                if (nodeInfoList != null && !nodeInfoList.Any())
                {
                    foreach (AccessibilityNodeInfo nodeInfo in nodeInfoList)
                    {
                        if (nodeInfo != null)
                        {
                            return nodeInfo;
                        }
                    }
                }
                return null;
            }
     
            public void clickTextViewByText(String text)
            {
                AccessibilityNodeInfo accessibilityNodeInfo = RootInActiveWindow;
                if (accessibilityNodeInfo == null)
                {
                    return;
                }
                var nodeInfoList = accessibilityNodeInfo.FindAccessibilityNodeInfosByText(text);
                if (nodeInfoList != null && !nodeInfoList.Any())
                {
                    foreach (AccessibilityNodeInfo nodeInfo in nodeInfoList)
                   {
                        if (nodeInfo != null)
                        {
                            performViewClick(nodeInfo);
                            break;
                        }
                    }
                }
            }
     
            public void clickTextViewByID(String id)
            {
                AccessibilityNodeInfo accessibilityNodeInfo = RootInActiveWindow;
                if (accessibilityNodeInfo == null)
                {
                    return;
                }
                var nodeInfoList = accessibilityNodeInfo.FindAccessibilityNodeInfosByViewId(id);
                if (nodeInfoList != null && !nodeInfoList.Any())
                {
                    foreach (AccessibilityNodeInfo nodeInfo in nodeInfoList)
                    {
                        if (nodeInfo != null)
                        {
                            performViewClick(nodeInfo);
                            break;
                        }
                    }
                }
            }
     
            public void inputText(AccessibilityNodeInfo nodeInfo, String text)
            {
                if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
                {
                    Bundle arguments = new Bundle();
                    arguments.PutCharSequence(AccessibilityNodeInfo.ActionArgumentSetTextCharsequence, text);
                    nodeInfo.PerformAction(Android.Views.Accessibility.Action.SetText, arguments);
                }
                else if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBeanMr2)
                {
                    ClipboardManager clipboard = (ClipboardManager)GetSystemService(Context.ClipboardService);
                    ClipData clip = ClipData.NewPlainText("label", text);
                    clipboard.PrimaryClip = clip;
                    nodeInfo.PerformAction(Android.Views.Accessibility.Action.Focus);
                    nodeInfo.PerformAction(Android.Views.Accessibility.Action.Paste);
                }
            }
     
     
            private void PerformGlobalAction(object gLOBAL_ACTION_BACK)
            {
            }
     
            public override void OnAccessibilityEvent(AccessibilityEvent e)
            {
            }
     
            public override void OnInterrupt()
            {
            }
        }
    
Sign In or Register to comment.