Android AccessibilityService機制源碼解析

1、本文須要解決的問題

以前本人作了一個項目,須要用到AccessibilityService這個系統提供的拓展服務。這個服務本意是做爲Android系統的一個輔助功能,去幫助殘疾人更好地使用手機。可是因爲它的一些特性,給不少項目的實現提供了一個新的思路,例如以前大名鼎鼎的微信搶紅包插件,本質上就是使用了這個服務。我研究AccessibilityService的目的是解決如下幾個我在使用過程當中所思考的問題:php

  1. AccessibilityService這個Service跟通常的Service有什麼區別?
  2. AccessibilityService是如何作到監控並捕捉用戶行爲的?
  3. AccessibilityService是如何作到查找控件,執行點擊等操做的?

2、初步分析

本文基於Android 7.1的源碼對AccessibilityService進行分析。 爲了更好地理解和分析代碼,我寫了一個demo,若是想學習具體的使用方法,能夠參考Google官方文檔AccessibilityService。本文不作AccessibilityService的具體使用教程。html

建立AccessibilityService
public class MyAccessibilityService extends AccessibilityService {

    private static final String TAG = "MyAccessibilityService";

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate");
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        int eventType = event.getEventType();
        switch (eventType) {
            case AccessibilityEvent.TYPE_VIEW_CLICKED:
                // 捕獲到點擊事件
                Log.i(TAG, "capture click event!");
                AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
                if (nodeInfo != null) {
                    // 查找text爲Test!的控件
                    List<AccessibilityNodeInfo> button = nodeInfo.findAccessibilityNodeInfosByText("Test!");
                    nodeInfo.recycle();
                    for (AccessibilityNodeInfo item : button) {
                        Log.i(TAG, "long-click button!");
                        // 執行長按操做
                        item.performAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onInterrupt() {
        Log.i(TAG, "onInterrupt");
    }
}
複製代碼
AccessibilityService配置

res/xml/accessibility_service_config.xmljava

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:accessibilityEventTypes="typeAllMask" android:accessibilityFeedbackType="feedbackSpoken" android:accessibilityFlags="flagRetrieveInteractiveWindows|flagRequestFilterKeyEvents" android:canRequestFilterKeyEvents="true" android:canRetrieveWindowContent="true" android:description="@string/app_name" android:notificationTimeout="100" android:packageNames="com.xu.accessibilitydemo" />
複製代碼
在manifest中進行註冊
<service android:name=".MyAccessibilityService" android:enabled="true" android:exported="true" android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
    <intent-filter>
        <action android:name="android.accessibilityservice.AccessibilityService"/>
    </intent-filter>

     <meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibility_service_config"/>
</service>
複製代碼
建立一個text爲Test!的button控件,設置監聽方法
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button);

        button.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Log.i(TAG, "onLongClick");
                return false;
            }
        });

    }
}
複製代碼
開啓AccessibilityService

AccessibilityService服務具體開啓位置在設置--無障礙中。node

運行應用,點擊text爲Test!的按鈕

會出現如下的日誌: android

log.png

具體解釋: 點擊按鈕即產生TYPE_VIEW_CLICKED事件 --> 被AcceesibilityService捕獲 --> 捕獲後執行長按按鈕操做 --> 執行長按回調方法。git

爲何AcceesibilityService能捕獲並執行其餘操做呢,接下來我將對源碼進行解析~github

3、源碼解析

AccessibilityService內部邏輯
AccessibilityService.java
public abstract class AccessibilityService extends Service {
      // 省略代碼
      public abstract void onAccessibilityEvent(AccessibilityEvent event);
      
      public abstract void onInterrupt();

      @Override
      public final IBinder onBind(Intent intent) {
          return new IAccessibilityServiceClientWrapper(this, getMainLooper(), new Callbacks() {
              @Override
              public void onServiceConnected() {
                  AccessibilityService.this.dispatchServiceConnected();
              }

              @Override
              public void onInterrupt() {
                  AccessibilityService.this.onInterrupt();
              }

              @Override
              public void onAccessibilityEvent(AccessibilityEvent event) {
                  AccessibilityService.this.onAccessibilityEvent(event);
              }

              @Override
              public void init(int connectionId, IBinder windowToken) {
                  mConnectionId = connectionId;
                  mWindowToken = windowToken;

                  // The client may have already obtained the window manager, so
                  // update the default token on whatever manager we gave them.
                  final WindowManagerImpl wm = (WindowManagerImpl) getSystemService(WINDOW_SERVICE);
                  wm.setDefaultToken(windowToken);
              }

              @Override
              public boolean onGesture(int gestureId) {
                  return AccessibilityService.this.onGesture(gestureId);
              }

              @Override
              public boolean onKeyEvent(KeyEvent event) {
                  return AccessibilityService.this.onKeyEvent(event);
              }

              @Override
              public void onMagnificationChanged(@NonNull Region region, float scale, float centerX, float centerY) {
                  AccessibilityService.this.onMagnificationChanged(region, scale, centerX, centerY);
              }

              @Override
              public void onSoftKeyboardShowModeChanged(int showMode) {
                  AccessibilityService.this.onSoftKeyboardShowModeChanged(showMode);
              }

              @Override
              public void onPerformGestureResult(int sequence, boolean completedSuccessfully) {
                  AccessibilityService.this.onPerformGestureResult(sequence, completedSuccessfully);
              }
          });
      }
}
複製代碼

分析:微信

  1. AccessibilityService是一個抽象類,繼承於Service,提供兩個抽象方法 onAccessibilityEvent() 和 onInterrupt();
  2. 雖然是抽象類,可是實現了最重要的 onBind() 方法,在其中建立了一個IAccessibilityServiceClientWrapper對象,實現Callbacks接口中的抽象方法。
IAccessibilityServiceClientWrapper
// 以分析onAccessibilityEvent爲例,省略部分代碼
public static class IAccessibilityServiceClientWrapper extends IAccessibilityServiceClient.Stub implements HandlerCaller.Callback {
    private final HandlerCaller mCaller;
    private final Callbacks mCallback;
    private int mConnectionId;
    
    public IAccessibilityServiceClientWrapper(Context context, Looper looper, Callbacks callback) {
        mCallback = callback;
        mCaller = new HandlerCaller(context, looper, this, true /*asyncHandler*/);
    }
    
    public void init(IAccessibilityServiceConnection connection, int connectionId, IBinder windowToken) {
        Message message = mCaller.obtainMessageIOO(DO_INIT, connectionId,
                    connection, windowToken);
        mCaller.sendMessage(message);
    }
    
    // 省略部分代碼 

    public void onAccessibilityEvent(AccessibilityEvent event) {
        Message message = mCaller.obtainMessageO(DO_ON_ACCESSIBILITY_EVENT, event);
        mCaller.sendMessage(message);
    }

    @Override
    public void executeMessage(Message message) {
        switch (message.what) {
            case DO_ON_ACCESSIBILITY_EVENT: {
                AccessibilityEvent event = (AccessibilityEvent) message.obj;
                if (event != null) {
                    AccessibilityInteractionClient.getInstance().onAccessibilityEvent(event);
                    mCallback.onAccessibilityEvent(event);
                    // Make sure the event is recycled.
                    try {
                        event.recycle();
                    } catch (IllegalStateException ise) {
                        /* ignore - best effort */
                    }
                }
            } return;
            // ... 
        }
     }
}
複製代碼

分析:app

  1. IAccessibilityServiceClientWrapper繼承於IAccessibilityServiceClient類,它是一個aidl接口,同時注意到它是繼承於IAccessibilityServiceClient.Stub類,能夠大概猜想到,AccessibilityService爲一個遠程Service,使用到跨進程通訊技術,後面我還會繼續分析這個;
  2. IAccessibilityServiceClientWrapper的類構造方法中,有兩個比較重要的參數,一個是looper,另外一個是Callbacks callback。Looper不用說,而Callbacks接口定義了不少方法,代碼以下:
public interface Callbacks {
    public void onAccessibilityEvent(AccessibilityEvent event);
    public void onInterrupt();
    public void onServiceConnected();
    public void init(int connectionId, IBinder windowToken);
    public boolean onGesture(int gestureId);
    public boolean onKeyEvent(KeyEvent event);
    public void onMagnificationChanged(@NonNull Region region, float scale, float centerX, float centerY);
    public void onSoftKeyboardShowModeChanged(int showMode);
    public void onPerformGestureResult(int sequence, boolean completedSuccessfully);
}
複製代碼
  1. IAccessibilityServiceClientWrapper同時也實現了HandlerCaller.Callback接口,HandlerCaller類經過命名也能夠知道,它內部含有一個Handler實例,因此能夠把它當作一個Handler,而處理信息的方法就是HandlerCaller.Callback#executeMessage(msg)方法
  2. 代碼有點繞,故簡單總結一下流程: AccessibilityEvent產生   -> Binder驅動    -> IAccessibilityServiceClientWrapper#onAccessibilityEvent(AccessibilityEvent)     -> HandlerCaller#sendMessage(message); // message中包括AccessibilityEvent      -> IAccessibilityServiceClientWrapper#executeMessage();       -> Callbacks#onAccessibilityEvent(event);        -> AccessibilityService.this.onAccessibilityEvent(event);

到這裏解決了咱們的第一個問題:AccessibilityService一樣繼承於Service類,它屬於遠程服務類,是Android系統提供的一種服務,能夠綁定此服務,用於捕捉界面的一些特定事件。async

AccessibilityService外部邏輯

前面分析了接收到AccessibilityEvent以後的代碼邏輯,那麼,這些AccessibilityEvent是怎樣產生的呢,並且,在回調執行以後是怎麼作到點擊等操做的(如demo所示)?咱們接下來繼續分析相關的源碼~

咱們從demo做爲例子開始入手,首先咱們知道,一個點擊事件的產生,實際代碼邏輯是在View#onTouchEvent() -> View#performClick()中:

public boolean performClick() {
    final boolean result;
    final ListenerInfo li = mListenerInfo;
    if (li != null && li.mOnClickListener != null) {
        playSoundEffect(SoundEffectConstants.CLICK);
        li.mOnClickListener.onClick(this);
        result = true;
    } else {
        result = false;
    }
    // !!!
    sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
    return result;
}
複製代碼

這裏找到一個重點方法sendAccessibilityEvent(),繼續跟進去,最後走到View#sendAccessibilityEventUncheckedInternal()方法:

public void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
    if (!isShown()) {
        return;
    }
    onInitializeAccessibilityEvent(event);
    // Only a subset of accessibility events populates text content.
    if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
        dispatchPopulateAccessibilityEvent(event);
    }
    // In the beginning we called #isShown(), so we know that getParent() is not null.
    getParent().requestSendAccessibilityEvent(this, event);
}
複製代碼

這裏的getParent()會返回一個實現ViewParent接口的對象。 咱們能夠簡單理解爲,它會讓View的父類執行requestSendAccessibilityEvent()方法,而View的父類通常爲ViewGroup,咱們查看ViewGroup#requestSendAccessibilityEvent()方法

@Override
public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) {
    ViewParent parent = mParent;
    if (parent == null) {
        return false;
    }
    final boolean propagate = onRequestSendAccessibilityEvent(child, event);
    if (!propagate) {
        return false;
    }
    return parent.requestSendAccessibilityEvent(this, event);
}
複製代碼

這裏涉及到一個變量mParent,咱們要找到這個mParent變量是在哪裏被賦值的。 首先咱們在View類中找到一個相關的方法View#assignParent():

void assignParent(ViewParent parent) {
    if (mParent == null) {
        mParent = parent;
    } else if (parent == null) {
        mParent = null;
    } else {
        throw new RuntimeException("view " + this + " being added, but" + " it already has a parent");
    }
}
複製代碼

可是View類中並無調用此方法,猜想是View的父類進行調用。 經過對源碼進行搜索,發現最後是在ViewRootImpl#setView()中進行調用,賦值的是this即ViewRootImpl自己。 直接跳到ViewRootImpl#requestSendAccessibilityEvent()方法:

@Override
public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) {
    if (mView == null || mStopped || mPausedForTransition) {
        return false;
    }
    // Intercept accessibility focus events fired by virtual nodes to keep
    // track of accessibility focus position in such nodes.
    final int eventType = event.getEventType();
    switch (eventType) {
        case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED:
            {
                final long sourceNodeId = event.getSourceNodeId();
                final int accessibilityViewId = AccessibilityNodeInfo.getAccessibilityViewId(sourceNodeId);
                View source = mView.findViewByAccessibilityId(accessibilityViewId);
                if (source != null) {
                    AccessibilityNodeProvider provider = source.getAccessibilityNodeProvider();
                    if (provider != null) {
                        final int virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(sourceNodeId);
                        final AccessibilityNodeInfo node;
                        if (virtualNodeId == AccessibilityNodeInfo.UNDEFINED_ITEM_ID) {
                            node = provider.createAccessibilityNodeInfo(AccessibilityNodeProvider.HOST_VIEW_ID);
                        } else {
                            node = provider.createAccessibilityNodeInfo(virtualNodeId);
                        }
                        setAccessibilityFocus(source, node);
                    }
                }
            }
            break;
            // 省略部分代碼
    }
    // !!!
    mAccessibilityManager.sendAccessibilityEvent(event);
    return true;
}
複製代碼

重點:AccessibilityManager#sendAccessibilityEvent(event)

public void sendAccessibilityEvent(AccessibilityEvent event) {
    final IAccessibilityManager service;
    final int userId;
    synchronized(mLock) {
        service = getServiceLocked();
        if (service == null) {
            return;
        }
        if (!mIsEnabled) {
            Looper myLooper = Looper.myLooper();
            if (myLooper == Looper.getMainLooper()) {
                throw new IllegalStateException("Accessibility off. Did you forget to check that?");
            } else {
                // If we're not running on the thread with the main looper, it's possible for
                // the state of accessibility to change between checking isEnabled and
                // calling this method. So just log the error rather than throwing the
                // exception.
                Log.e(LOG_TAG, "AccessibilityEvent sent with accessibility disabled");
                return;
            }
        }
        userId = mUserId;
    }
    boolean doRecycle = false;
    try {
        event.setEventTime(SystemClock.uptimeMillis());
        // it is possible that this manager is in the same process as the service but
        // client using it is called through Binder from another process. Example: MMS
        // app adds a SMS notification and the NotificationManagerService calls this method
        long identityToken = Binder.clearCallingIdentity();
        // !!!
        doRecycle = service.sendAccessibilityEvent(event, userId);
        Binder.restoreCallingIdentity(identityToken);
        if (DEBUG) {
            Log.i(LOG_TAG, event + " sent");
        }
    } catch (RemoteException re) {
        Log.e(LOG_TAG, "Error during sending " + event + " ", re);
    } finally {
        if (doRecycle) {
            event.recycle();
        }
    }
}

private IAccessibilityManager getServiceLocked() {
    if (mService == null) {
        tryConnectToServiceLocked(null);
    }
    return mService;
}

private void tryConnectToServiceLocked(IAccessibilityManager service) {
    if (service == null) {
        IBinder iBinder = ServiceManager.getService(Context.ACCESSIBILITY_SERVICE);
        if (iBinder == null) {
            return;
        }
        service = IAccessibilityManager.Stub.asInterface(iBinder);
    }
    try {
        final int stateFlags = service.addClient(mClient, mUserId);
        setStateLocked(stateFlags);
        mService = service;
    } catch (RemoteException re) {
        Log.e(LOG_TAG, "AccessibilityManagerService is dead", re);
    }
}
複製代碼

這裏有使用到Android Binder機制,重點爲IAccessibilityManager#sendAccessibilityEvent()方法,這裏調用的是代理方法,實際代碼邏輯在AccessibilityManagerService#sendAccessibilityEvent():

@Override
public boolean sendAccessibilityEvent(AccessibilityEvent event, int userId) {
    synchronized(mLock) {
        // We treat calls from a profile as if made by its parent as profiles
        // share the accessibility state of the parent. The call below
        // performs the current profile parent resolution..
        final int resolvedUserId = mSecurityPolicy.resolveCallingUserIdEnforcingPermissionsLocked(userId);
        // This method does nothing for a background user.
        if (resolvedUserId != mCurrentUserId) {
            return true; // yes, recycle the event
        }
        if (mSecurityPolicy.canDispatchAccessibilityEventLocked(event)) {
            mSecurityPolicy.updateActiveAndAccessibilityFocusedWindowLocked(event.getWindowId(), event.getSourceNodeId(), event.getEventType(), event.getAction());
            mSecurityPolicy.updateEventSourceLocked(event);
            // !!!
            notifyAccessibilityServicesDelayedLocked(event, false);
            notifyAccessibilityServicesDelayedLocked(event, true);
        }
        if (mHasInputFilter && mInputFilter != null) {
            mMainHandler.obtainMessage(MainHandler.MSG_SEND_ACCESSIBILITY_EVENT_TO_INPUT_FILTER, AccessibilityEvent.obtain(event)).sendToTarget();
        }
        event.recycle();
    }
    return (OWN_PROCESS_ID != Binder.getCallingPid());
}

private void notifyAccessibilityServicesDelayedLocked(AccessibilityEvent event, boolean isDefault) {
    try {
        UserState state = getCurrentUserStateLocked();
        for (int i = 0, count = state.mBoundServices.size(); i < count; i++) {
            Service service = state.mBoundServices.get(i);
            if (service.mIsDefault == isDefault) {
                if (canDispatchEventToServiceLocked(service, event)) {
                    service.notifyAccessibilityEvent(event);
                }
            }
        }
    } catch (IndexOutOfBoundsException oobe) {
        // An out of bounds exception can happen if services are going away
        // as the for loop is running. If that happens, just bail because
        // there are no more services to notify.
    }
}
複製代碼
  1. 在方法中,最後會調用notifyAccessibilityServicesDelayedLocked()方法,而後將event進行回收;
  2. 在notifyAccessibilityServicesDelayedLocked()方法中,會得到全部Bound即綁定的Service,執行notifyAccessibilityEvent()方法,經過跟蹤代碼邏輯,最後會調用綁定Service的onAccessibilityEvent()方法。綁定的Service是指咱們本身實現的繼承於AccessibilityService的Service類,當你在設置-無障礙中開啓服務以後即將服務綁定到AccessibilityManagerService中。

這樣咱們解決了第二個問題: AccessibilityService是如何作到監控捕捉用戶行爲的:(以點擊事件爲例) AccessibilityEvent產生: View#performClick()   -> View#sendAccessibilityEventUncheckedInternal()    -> ViewGroup#requestSendAccessibilityEvent()     -> ViewRootImpl#requestSendAccessibilityEvent()      -> AccessibilityManager#sendAccessibilityEvent(event)       -> AccessibilityManagerService#sendAccessibilityEvent()        -> AccessibilityManagerService#notifyAccessibilityServicesDelayedLocked()         -> Service#notifyAccessibilityEvent(event)

AccessibilityEvent處理: AccessibilityEvent   -> Binder驅動    -> IAccessibilityServiceClientWrapper#onAccessibilityEvent(AccessibilityEvent)     -> HandlerCaller#sendMessage(message); // message中包括AccessibilityEvent      -> IAccessibilityServiceClientWrapper#executeMessage();       -> Callbacks#onAccessibilityEvent(event);        -> AccessibilityService.this.onAccessibilityEvent(event);

AccessibilityService交互之查找控件

在demo中,咱們在MyAccessibilityService中調用了getRootInActiveWindow()方法獲取被監控的View的全部結點,這些結點都封裝成一個AccessibilityNodeInfo對象中。同時也調用AccessibilityNodeInfo#findAccessibilityNodeInfosByText()方法查找相應的控件。 這些方法的本質是調用了AccessibilityInteractionClient類的對應方法。 以AccessibilityInteractionClient#findAccessibilityNodeInfosByText()爲例:

public List<AccessibilityNodeInfo> findAccessibilityNodeInfosByText(int connectionId, int accessibilityWindowId, long accessibilityNodeId, String text) {
    try {
        IAccessibilityServiceConnection connection = getConnection(connectionId);
        if (connection != null) {
            final int interactionId = mInteractionIdCounter.getAndIncrement();
            final long identityToken = Binder.clearCallingIdentity();
            final boolean success = connection.findAccessibilityNodeInfosByText(accessibilityWindowId, accessibilityNodeId, text, interactionId, this, Thread.currentThread().getId());
            Binder.restoreCallingIdentity(identityToken);
            if (success) {
                List<AccessibilityNodeInfo> infos = getFindAccessibilityNodeInfosResultAndClear(interactionId);
                if (infos != null) {
                    finalizeAndCacheAccessibilityNodeInfos(infos, connectionId);
                    return infos;
                }
            }
        } else {
            if (DEBUG) {
                Log.w(LOG_TAG, "No connection for connection id: " + connectionId);
            }
        }
    } catch (RemoteException re) {
        Log.w(LOG_TAG, "Error while calling remote" + " findAccessibilityNodeInfosByViewText", re);
    }
    return Collections.emptyList();
}
複製代碼

代碼邏輯比較簡單,就是直接調用IAccessibilityServiceConnection#findAccessibilityNodeInfosByText()方法。 IAccessibilityServiceConnection是一個aidl接口,從註釋看,它是AccessibilitySerivce和AccessibilityManagerService之間溝通的橋樑。 猜測代碼真正的實如今AccessibilityManagerService中。 AccessibilityManagerService.Service#findAccessibilityNodeInfosByText():

@Override
public boolean findAccessibilityNodeInfosByText(int accessibilityWindowId, long accessibilityNodeId, String text, int interactionId, IAccessibilityInteractionConnectionCallback callback, long interrogatingTid) throws RemoteException {
    final int resolvedWindowId;
    IAccessibilityInteractionConnection connection = null;
    Region partialInteractiveRegion = Region.obtain();
    synchronized(mLock) {
        if (!isCalledForCurrentUserLocked()) {
            return false;
        }
        resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
        final boolean permissionGranted = mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
        if (!permissionGranted) {
            return false;
        } else {
            connection = getConnectionLocked(resolvedWindowId);
            if (connection == null) {
                return false;
            }
        }
        if (!mSecurityPolicy.computePartialInteractiveRegionForWindowLocked(resolvedWindowId, partialInteractiveRegion)) {
            partialInteractiveRegion.recycle();
            partialInteractiveRegion = null;
        }
    }
    final int interrogatingPid = Binder.getCallingPid();
    final long identityToken = Binder.clearCallingIdentity();
    MagnificationSpec spec = getCompatibleMagnificationSpecLocked(resolvedWindowId);
    try {
        connection.findAccessibilityNodeInfosByText(accessibilityNodeId, text, partialInteractiveRegion, interactionId, callback, mFetchFlags, interrogatingPid, interrogatingTid, spec);
        return true;
    } catch (RemoteException re) {
        if (DEBUG) {
            Slog.e(LOG_TAG, "Error calling findAccessibilityNodeInfosByText()");
        }
    } finally {
        Binder.restoreCallingIdentity(identityToken);
        // Recycle if passed to another process.
        if (partialInteractiveRegion != null && Binder.isProxy(connection)) {
            partialInteractiveRegion.recycle();
        }
    }
    return false;
}
複製代碼
  1. 此方法在AccessibilityManagerService的內部類Service中實現,這個Service繼承於IAccessibilityServiceConnection.Stub,驗證了我上面的猜測是正確的;
  2. 代碼重點是調用connection.findAccessibilityNodeInfosByText(),這裏的connection實例與上面不一樣,它隸屬於IAccessibilityInteractionConnection類。這個類一樣是一個aidl接口,從註釋上看,它又是AccessibilityManagerService與指定窗口的ViewRoot之間溝通的橋樑。 再次猜測,真正的代碼邏輯在ViewRootImpl中。 查看ViewRootImpl.AccessibilityInteractionConnection#findAccessibilityNodeInfosByText():
@Override
public void findAccessibilityNodeInfosByText(long accessibilityNodeId, String text, Region interactiveRegion, int interactionId, IAccessibilityInteractionConnectionCallback callback, int flags, int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
    ViewRootImpl viewRootImpl = mViewRootImpl.get();
    if (viewRootImpl != null && viewRootImpl.mView != null) {
        viewRootImpl.getAccessibilityInteractionController().findAccessibilityNodeInfosByTextClientThread(accessibilityNodeId, text, interactiveRegion, interactionId, callback, flags, interrogatingPid, interrogatingTid, spec);
    } else {
        // We cannot make the call and notify the caller so it does not wait.
        try {
            callback.setFindAccessibilityNodeInfosResult(null, interactionId);
        } catch (RemoteException re) {
            /* best effort - ignore */
        }
    }
}
複製代碼
  1. 一樣的,此方法在ViewRootImpl的內部類AccessibilityInteractionConnection中實現,這個內部類繼承於IAccessibilityServiceConnection.Stub,驗證了個人猜測;
  2. 查找控件等操做,ViewRootImpl並非直接處理,而是交給AccessibilityInteractionController類去查找,查找到的結果會保存到一個callback中,這個callback爲IAccessibilityInteractionConnectionCallback類型,它也是一個aidl接口,而AccessibilityInteractionClient類繼承了IAccessibilityInteractionConnectionCallback.Stub,即最後查詢後的結果會回調到AccessibilityInteractionClient類中,如上面AccessibilityInteractionClient#findAccessibilityNodeInfosByText()方法,最後會調用getFindAccessibilityNodeInfosResultAndClear()方法獲取結果。具體如何尋找指定控件則再也不分析代碼。
AccessibilityService交互之執行控件操做

相似的,與上面的流程基本相同,只是回調的時候,返回的是執行操做的返回值(True or False)。

到這裏,咱們解決了最後一個問題: AccessibilityService是如何作到查找控件,執行點擊等操做的? 總結: 尋找指定控件/執行操做   -> 交給AccessibilityInteractionClient類處理     -> Binder       -> AccessibilityManagerService類進行查找/執行操做         -> Binder           -> 指定窗口的ViewRoot(ViewRootImpl)進行查找/執行操做         <- Binder     <- 結果回調到AccessibilityInteractionClient類

4、有用代碼記錄

  1. HandlerCaller類:結合Handler類和自定義的接口類(Caller.java),利用Handler的消息循環機制來分發消息,將最終的處理函數交給Caller#executeMessage():
// HandlerCaller.java
public class HandlerCaller {
    final Looper mMainLooper;
    final Handler mH;

    final Callback mCallback;

    class MyHandler extends Handler {
        MyHandler(Looper looper, boolean async) {
            super(looper, null, async);
        }

        @Override
        public void handleMessage(Message msg) {
            mCallback.executeMessage(msg);
        }
    }

    public interface Callback {
        public void executeMessage(Message msg);
    }

    public HandlerCaller(Context context, Looper looper, Callback callback, boolean asyncHandler) {
        mMainLooper = looper != null ? looper : context.getMainLooper();
        mH = new MyHandler(mMainLooper, asyncHandler);
        mCallback = callback;
    }

    public Handler getHandler() {
        return mH;
    }

    public void executeOrSendMessage(Message msg) {
        // If we are calling this from the main thread, then we can call
        // right through. Otherwise, we need to send the message to the
        // main thread.
        if (Looper.myLooper() == mMainLooper) {
            mCallback.executeMessage(msg);
            msg.recycle();
            return;
        }
        
        mH.sendMessage(msg);
    }

    public void sendMessageDelayed(Message msg, long delayMillis) {
        mH.sendMessageDelayed(msg, delayMillis);
    }

    public boolean hasMessages(int what) {
        return mH.hasMessages(what);
    }
    
    public void removeMessages(int what) {
        mH.removeMessages(what);
    }
    
    public void removeMessages(int what, Object obj) {
        mH.removeMessages(what, obj);
    }
    
    public void sendMessage(Message msg) {
        mH.sendMessage(msg);
    }

    public SomeArgs sendMessageAndWait(Message msg) {
        if (Looper.myLooper() == mH.getLooper()) {
            throw new IllegalStateException("Can't wait on same thread as looper");
        }
        SomeArgs args = (SomeArgs)msg.obj;
        args.mWaitState = SomeArgs.WAIT_WAITING;
        mH.sendMessage(msg);
        synchronized (args) {
            while (args.mWaitState == SomeArgs.WAIT_WAITING) {
                try {
                    args.wait();
                } catch (InterruptedException e) {
                    return null;
                }
            }
        }
        args.mWaitState = SomeArgs.WAIT_NONE;
        return args;
    }

    public Message obtainMessage(int what) {
        return mH.obtainMessage(what);
    }
     
    // 省略部分代碼
}
複製代碼
  1. HandlerCaller#sendMessageAndWait():
public SomeArgs sendMessageAndWait(Message msg) {
    if (Looper.myLooper() == mH.getLooper()) {
        throw new IllegalStateException("Can't wait on same thread as looper");
    }
    SomeArgs args = (SomeArgs) msg.obj;
    args.mWaitState = SomeArgs.WAIT_WAITING;
    mH.sendMessage(msg);
    synchronized(args) {
        while (args.mWaitState == SomeArgs.WAIT_WAITING) {
            try {
                args.wait();
            } catch (InterruptedException e) {
                return null;
            }
        }
    }
    args.mWaitState = SomeArgs.WAIT_NONE;
    return args;
}
複製代碼

這篇文章會同步到個人我的日誌,若有問題,請你們踊躍提出,謝謝你們!

相關文章
相關標籤/搜索