Android AccessibilityService機制原始碼解析

張朝旭發表於2018-06-20

一、本文需要解決的問題

之前本人做了一個專案,需要用到AccessibilityService這個系統提供的擴充服務。這個服務本意是作為Android系統的一個輔助功能,去幫助殘疾人更好地使用手機。但是由於它的一些特性,給很多專案的實現提供了一個新的思路,例如之前大名鼎鼎的微信搶紅包外掛,本質上就是使用了這個服務。我研究AccessibilityService的目的是解決以下幾個我在使用過程中所思考的問題:

  1. AccessibilityService這個Service跟一般的Service有什麼區別?
  2. AccessibilityService是如何做到監控並捕捉使用者行為的?
  3. AccessibilityService是如何做到查詢控制元件,執行點選等操作的?

二、初步分析

本文基於Android 7.1的原始碼對AccessibilityService進行分析。 為了更好地理解和分析程式碼,我寫了一個demo,如果想學習具體的使用方法,可以參考Google官方文件AccessibilityService。本文不做AccessibilityService的具體使用教程。

建立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.xml

<?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服務具體開啟位置在設定--無障礙中。

執行應用,點選text為Test!的按鈕

會出現以下的日誌:

log.png

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

為什麼AcceesibilityService能捕獲並執行其他操作呢,接下來我將對原始碼進行解析~

三、原始碼解析

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;
            // ...         
        }
     }
}
複製程式碼

分析:

  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系統提供的一種服務,可以繫結此服務,用於捕捉介面的一些特定事件。

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類

四、有用程式碼記錄

  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;
}
複製程式碼

這篇文章會同步到我的個人日誌,如有問題,請大家踴躍提出,謝謝大家!

相關文章