之前分析按键的博客,我们分析了按键的流程,但是对按键具体在InputFlinger中分发给哪个进程这块没有分析。
WMS设置焦点
我们先来看WMS的updateFocusedWindowLocked函数,当Window窗口或者焦点有变化都会调用这个函数,这个函数先调用findFocusedWindowLocked来获取当前有焦点的窗口,然后会调用InputMonitor的setInputFocusLw函数.
我们先来看findFocusedWindowLocked函数。这个函数把WindowList逆序遍历,把最前面的window放在前面遍历,然后找该window的mAppToken是否在各个Task中存在,存在就认为当前window就是有焦点的window。
private WindowState findFocusedWindowLocked(DisplayContent displayContent) {
final WindowList windows = displayContent.getWindowList();
for (int i = windows.size() - 1; i >= 0; i--) {
final WindowState win = windows.get(i);
if (localLOGV || DEBUG_FOCUS) Slog.v(
TAG, "Looking for focus: " + i
+ " = " + win
+ ", flags=" + win.mAttrs.flags
+ ", canReceive=" + win.canReceiveKeys());
if (!win.canReceiveKeys()) {
continue;
}
AppWindowToken wtoken = win.mAppToken;
// If this window's application has been removed, just skip it.
if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
if (DEBUG_FOCUS) Slog.v(TAG, "Skipping " + wtoken + " because "
+ (wtoken.removed ? "removed" : "sendingToBottom"));
continue;
}
// Descend through all of the app tokens and find the first that either matches
// win.mAppToken (return win) or mFocusedApp (return null).
if (wtoken != null && win.mAttrs.type != TYPE_APPLICATION_STARTING &&
mFocusedApp != null) {
ArrayList<Task> tasks = displayContent.getTasks();
for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
AppTokenList tokens = tasks.get(taskNdx).mAppTokens;
int tokenNdx = tokens.size() - 1;
for ( ; tokenNdx >= 0; --tokenNdx) {
final AppWindowToken token = tokens.get(tokenNdx);
if (wtoken == token) {
break;
}
if (mFocusedApp == token) {
// Whoops, we are below the focused app... no focus for you!
if (localLOGV || DEBUG_FOCUS_LIGHT) Slog.v(TAG,
"findFocusedWindow: Reached focused app=" + mFocusedApp);
return null;
}
}
if (tokenNdx >= 0) {
// Early exit from loop, must have found the matching token.
break;
}
}
}
if (DEBUG_FOCUS_LIGHT) Slog.v(TAG, "findFocusedWindow: Found new focus @ " + i +
" = " + win);
return win;
}
if (DEBUG_FOCUS_LIGHT) Slog.v(TAG, "findFocusedWindow: No f