【DisplayContext】
/**
* Window that is currently interacting with the user. This window is responsible for receiving
* key events and pointer events from the user.
*/
WindowState mCurrentFocus = null;
/**
* The foreground app of this display. Windows below this app cannot be the focused window. If
* the user taps on the area outside of the task of the focused app, we will notify AM about the
* new task the user wants to interact with.
*/
ActivityRecord mFocusedApp = null;
设置焦点应用DC.setFocusedApp
/**
* Set the new focused app to this display.
*
* @param newFocus the new focused {@link ActivityRecord}.
* @return true if the focused app is changed.
*/
boolean setFocusedApp(ActivityRecord newFocus) {
if (newFocus != null) {
final DisplayContent appDisplay = newFocus.getDisplayContent();
if (appDisplay != this) {
throw new IllegalStateException(newFocus + " is not on " + getName()
+ " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
}
// Called even if the focused app is not changed in case the app is moved to a different
// TaskDisplayArea.
onLastFocusedTaskDisplayAreaChanged(newFocus.getDisplayArea());
}
if (mFocusedApp == newFocus) {
return false;
}
ProtoLog.i(WM_DEBUG_FOCUS_LIGHT, "setFocusedApp %s displayId=%d Callers=%s",
newFocus, getDisplayId(), Debug.getCallers(4));
final Task oldTask = mFocusedApp != null ? mFocusedApp.getTask() : null;
final Task newTask = newFocus != null ? newFocus.getTask() : null;
//1. 设置焦点应用
mFocusedApp = newFocus;
if (oldTask != newTask) {
if (oldTask != null) oldTask.onAppFocusChanged(false);
if (newTask != null) newTask.onAppFocusChanged(true);
}
//2. 更新input的焦点应用
getInputMonitor().setFocusedAppLw(newFocus);
updateTouchExcludeRegion();
return true;
}
设置焦点窗口
boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows) {
Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "wmUpdateFocus");
boolean changed = mRoot.updateFocusedWindowLocked(mode, updateInputWindows);
Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
return changed;
}
/**
* Update the focused window and make some adjustments if the focus has changed.
*
* @param mode Indicates the situation we are in. Possible modes are:
* {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
* {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
* {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
* {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
* @param updateInputWindows Whether to sync the window information to the input module.
* @param topFocusedDisplayId Display id of current top focused display.
* @return {@code true} if the focused window has changed.
*/
boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows,
int topFocusedDisplayId) {
[inputmonitor.java]
/**
* Immediately update the input transaction and merge into the passing Transaction that could be
* collected and applied later.
*/
void updateInputWindowsImmediately(SurfaceControl.Transaction t) {
mHandler.removeCallbacks(mUpdateInputWindows);
mUpdateInputWindowsImmediately = true;
mUpdateInputWindows.run();
mUpdateInputWindowsImmediately = false;
t.merge(mInputTransaction);
}