前言
在使用Android12为车机系统载体进行系统SystemUI开发的过程中发现一个很奇特的问题,当不同页面发生切换的时候,导航栏总是会闪一下,其实就是窗口焦点发生变化的时候,导航栏总是会消失一下再出现,虽然问题不是很严重,但这对于用户体验来说是极差的,本篇文章我们就来梳理一下为什么会出现这种现象。
一、窗口焦点发生变化
1、当窗口焦点发生变化的时候,首先会触发WindowManagerService的updateFocusedWindowLocked方法。
frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java
public class WindowManagerService extends IWindowManager.Stub implements Watchdog.Monitor, WindowManagerPolicy.WindowManagerFuncs {RootWindowContainer mRoot;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;}
}
WindowManagerService的updateFocusedWindowLocked方法会进一步调用RootWindowContainer的updateFocusedWindowLocked方法。
2、RootWindowContainer的updateFocusedWindowLocked方法如下所示。
frameworks/base/services/core/java/com/android/server/wm/RootWindowContainer.java
class RootWindowContainer extends WindowContainer<DisplayContent>implements DisplayManager.DisplayListener {// List of children for this window container. List is in z-order as the children appear on// screen with the top-most window container at the tail of the list.protected final WindowList<E> mChildren = new WindowList<E>();//注意,此属性是父类WindowContainer的属性boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows) {mTopFocusedAppByProcess.clear();boolean changed = false;int topFocusedDisplayId = INVALID_DISPLAY;for (int i = mChildren.size() - 1; i >= 0; --i) {final DisplayContent dc = mChildren.get(i);//循环调用集合子条目DisplayContent的updateFocusedWindowLocked方法changed |= dc.updateFocusedWindowLocked(mode, updateInputWindows, topFocusedDisplayId);final WindowState newFocus = dc.mCurrentFocus;if (newFocus != null) {final int pidOfNewFocus = newFocus.mSession.mPid;if (mTopFocusedAppByProcess.get(pidOfNewFocus) == null) {mTopFocusedAppByProcess.put(pidOfNewFocus, newFocus.mActivityRecord);}if (topFocusedDisplayId == INVALID_DISPLAY) {topFocusedDisplayId = dc.getDisplayId();}} else if (topFocusedDisplayId == INVALID_DISPLAY && dc.mFocusedApp != null) {// The top-most display that has a focused app should still be the top focused// display even when the app window is not ready yet (process not attached or// window not added yet).topFocusedDisplayId = dc.getDisplayId();}}if (topFocusedDisplayId == INVALID_DISPLAY) {topFocusedDisplayId = DEFAULT_DISPLAY;}if (mTopFocusedDisplayId != topFocusedDisplayId) {mTopFocusedDisplayId = topFocusedDisplayId;mWmService.mInputManager.setFocusedDisplay(topFocusedDisplayId);mWmService.mPolicy.setTopFocusedDisplay(topFocusedDisplayId);mWmService.mAccessibilityController.setFocusedDisplay(topFocusedDisplayId);ProtoLog.d(WM_DEBUG_FOCUS_LIGHT, "New topFocusedDisplayId=%d", topFocusedDisplayId);}return changed;}}
RootWindowContainer的updateFocusedWindowLocked方法会循环调用类型为DisplayContent的集合的每个条目的updateFocusedWindowLocked方法。
3、DisplayContent的updateFocusedWindowLocked方法如下所示。
frameworks/base/services/core/java/com/android/server/wm/DisplayContent.java
class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.DisplayContentInfo {boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows,int topFocusedDisplayId) {WindowState newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);if (mCurrentFocus == newFocus) {return false;}boolean imWindowChanged = false;final WindowState imWindow = mInputMethodWindow;if (imWindow != null) {final WindowState prevTarget = mImeLayeringTarget;final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);imWindowChanged = prevTarget != newTarget;if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS&& mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {assignWindowLayers(false /* setLayoutNeeded */);}if (imWindowChanged) {mWmService.mWindowsChanged = true;setLayoutNeeded();newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);}}ProtoLog.d(WM_DEBUG_FOCUS_LIGHT, "Changing focus from %s to %s displayId=%d Callers=%s",mCurrentFocus, newFocus, getDisplayId(), Debug.getCallers(4));final WindowState oldFocus = mCurrentFocus;mCurrentFocus = newFocus;if (newFocus != null) {mWinAddedSinceNullFocus.clear();mWinRemovedSinceNullFocus.clear();if (newFocus.canReceiveKeys()) {// Displaying a window implicitly causes dispatching to be unpaused.// This is to protect against bugs if someone pauses dispatching but// forgets to resume.newFocus.mToken.paused = false;}}//调用DisplayPolicy的focusChangedLwgetDisplayPolicy().focusChangedLw(oldFocus, newFocus);if (imWindowChanged && oldFocus != mInputMethodWindow) {// Focus of the input method window changed. Perform layout if needed.if (mode == UPDATE_FOCUS_PLACING_SURFACES) {performLayout(true /*initial*/, updateInputWindows);} else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {// Client will do the layout, but we need to assign layers// for handleNewWindowLocked() below.assignWindowLayers(false /* setLayoutNeeded */);}}if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {// If we defer assigning layers, then the caller is responsible for doing this part.getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);}adjustForImeIfNeeded();// We may need to schedule some toast windows to be removed. The toasts for an app that// does not have input focus are removed within a timeout to prevent apps to redress// other apps' UI.scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);if (mode == UPDATE_FOCUS_PLACING_SURFACES) {pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;}// Notify the accessibility manager for the change so it has the windows before the newly// focused one starts firing events.// TODO(b/151179149) investigate what info accessibility service needs before input can// dispatch focus to clients.if (mWmService.mAccessibilityController.hasCallbacks()) {mWmService.mH.sendMessage(PooledLambda.obtainMessage(this::updateAccessibilityOnWindowFocusChanged,mWmService.mAccessibilityController));}return true;}}
DisplayContent的updateFocusedWindowLocked方法会进一步调用DisplayPolicy的focusChangedLw方法。
4、DisplayPolicy的focusChangedLw方法如下所示。
frameworks/base/services/core/java/com/android/server/wm/DisplayPolicy.java
public class DisplayPolicy {/*** A new window has been focused.*/public void focusChangedLw(WindowState lastFocus, WindowState newFocus) {mFocusedWindow = newFocus;mLastFocusedWindow = lastFocus;if (mDisplayContent.isDefaultDisplay) {mService.mPolicy.onDefaultDisplayFocusChangedLw(newFocus);}updateSystemBarAttributes();}
}
DisplayPolicy的focusChangedLw方法最终会调用updateSystemBarAttributes方法来刷新系统栏属性。
二、更新系统栏属性
1、DisplayPolicy的updateSystemBarAttributes的方法如下所示。
public class DisplayPolicy {void updateSystemBarAttributes() {WindowState winCandidate = mFocusedWindow;if (winCandidate == null && mTopFullscreenOpaqueWindowState != null&& (mTopFullscreenOpaqueWindowState.mAttrs.flags& WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0) {// Only focusable window can take system bar control.winCandidate = mTopFullscreenOpaqueWindowState;}// If there is no window focused, there will be nobody to handle the events// anyway, so just hang on in whatever state we're in until things settle down.if (winCandidate == null) {return;}// The immersive mode confirmation should never affect the system bar visibility, otherwise// it will unhide the navigation bar and hide itself.if (winCandidate.getAttrs().token == mImmersiveModeConfirmation.getWindowToken()) {// The immersive mode confirmation took the focus from mLastFocusedWindow which was// controlling the system ui visibility. So if mLastFocusedWindow can still receive// keys, we let it keep controlling the visibility.final boolean lastFocusCanReceiveKeys =(mLastFocusedWindow != null && mLastFocusedWindow.canReceiveKeys());winCandidate = isKeyguardShowing() && !isKeyguardOccluded() ? mNotificationShade: lastFocusCanReceiveKeys ? mLastFocusedWindow: mTopFullscreenOpaqueWindowState;if (winCandidate == null) {return;}}final WindowState win = winCandidate;mSystemUiControllingWindow = win;final int displayId = getDisplayId();//final int disableFlags = win.getDisableFlags();//获取final int opaqueAppearance = updateSystemBarsLw(win, disableFlags);//更新系统栏参数...代码省略...}}
updateSystemBarAttributes方法首先获取当前的焦点窗口mFocusedWindow,将该窗口赋值给winCandidate,并判断该窗口是否可以操控系统栏,如果不允许会直接返回;如果允许,则会结合winCandidate的属性调用updateSystemBarsLw方法,来更新系统栏参数。
2、DisplayPolicy的updateSystemBarsLw方法如下所示。
public class DisplayPolicy {private int updateSystemBarsLw(WindowState win, int disableFlags) {final TaskDisplayArea defaultTaskDisplayArea = mDisplayContent.getDefaultTaskDisplayArea();final boolean multiWindowTaskVisible =defaultTaskDisplayArea.isRootTaskVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)|| defaultTaskDisplayArea.isRootTaskVisible(WINDOWING_MODE_MULTI_WINDOW);final boolean freeformRootTaskVisible =defaultTaskDisplayArea.isRootTaskVisible(WINDOWING_MODE_FREEFORM);//判断当前是否存在多任务窗口或者悬浮窗,如果存在则需要强制显示系统栏mForceShowSystemBars = multiWindowTaskVisible || freeformRootTaskVisible;//经测试发现,窗口焦点变化的时候,这行代码会触发导航栏闪一下mDisplayContent.getInsetsPolicy().updateBarControlTarget(win);//导航栏或状态栏不透明属性int appearance = APPEARANCE_OPAQUE_NAVIGATION_BARS | APPEARANCE_OPAQUE_STATUS_BARS;appearance = configureStatusBarOpacity(appearance);appearance = configureNavBarOpacity(appearance, multiWindowTaskVisible,freeformRootTaskVisible);//是否需要隐藏导航栏final boolean requestHideNavBar = !win.getRequestedVisibility(ITYPE_NAVIGATION_BAR);final long now = SystemClock.uptimeMillis();final boolean pendingPanic = mPendingPanicGestureUptime != 0&& now - mPendingPanicGestureUptime <= PANIC_GESTURE_EXPIRATION;final DisplayPolicy defaultDisplayPolicy =mService.getDefaultDisplayContentLocked().getDisplayPolicy();if (pendingPanic && requestHideNavBar && win != mNotificationShade&& getInsetsPolicy().isHidden(ITYPE_NAVIGATION_BAR)// TODO (b/111955725): Show keyguard presentation on all external displays&& defaultDisplayPolicy.isKeyguardDrawComplete()) {// The user performed the panic gesture recently, we're about to hide the bars,// we're no longer on the Keyguard and the screen is ready. We can now request the bars.mPendingPanicGestureUptime = 0;if (!isNavBarEmpty(disableFlags)) {mDisplayContent.getInsetsPolicy().showTransient(SHOW_TYPES_FOR_PANIC,true /* isGestureOnSystemBar */);}}// update navigation barboolean oldImmersiveMode = mLastImmersiveMode;boolean newImmersiveMode = isImmersiveMode(win);if (oldImmersiveMode != newImmersiveMode) {mLastImmersiveMode = newImmersiveMode;// The immersive confirmation window should be attached to the immersive window root.final RootDisplayArea root = win.getRootDisplayArea();final int rootDisplayAreaId = root == null ? FEATURE_UNDEFINED : root.mFeatureId;mImmersiveModeConfirmation.immersiveModeChangedLw(rootDisplayAreaId, newImmersiveMode,mService.mPolicy.isUserSetupComplete(),isNavBarEmpty(disableFlags));}return appearance;}}
以上代码经过测试发现,是mDisplayContent.getInsetsPolicy().updateBarControlTarget(win)这段代码导致窗口焦点发生变化的时候,导航栏会闪一下。
3、InsetsPolicy调用updateBarControlTarget更新当前可以控制系统栏的窗口对象。
frameworks/base/services/core/java/com/android/server/wm/InsetsPolicy.java
class InsetsPolicy {/** Updates the target which can control system bars. */void updateBarControlTarget(@Nullable WindowState focusedWin) {if (mFocusedWin != focusedWin){abortTransient();}mFocusedWin = focusedWin;final InsetsControlTarget statusControlTarget =getStatusControlTarget(focusedWin, false /* fake */);final InsetsControlTarget navControlTarget =getNavControlTarget(focusedWin, false /* fake */);mStateController.onBarControlTargetChanged(statusControlTarget,statusControlTarget == mDummyControlTarget? getStatusControlTarget(focusedWin, true /* fake */): null,navControlTarget,navControlTarget == mDummyControlTarget? getNavControlTarget(focusedWin, true /* fake */): null);mStatusBar.updateVisibility(statusControlTarget, ITYPE_STATUS_BAR);mNavBar.updateVisibility(navControlTarget, ITYPE_NAVIGATION_BAR);}}
InsetsPolicy的updateBarControlTarget方法的主要目的是更新当前可以控制系统栏的窗口对象,此方法会进一步调用InsetsStateController的onBarControlTargetChanged方法。
4、InsetsStateController的onBarControlTargetChanged方法如下所示。
frameworks/base/services/core/java/com/android/server/wm/InsetsStateController.java
class InsetsStateController {/*** Called when the focused window that is able to control the system bars changes.** @param statusControlling The target that is now able to control the status bar appearance* and visibility.* @param navControlling The target that is now able to control the nav bar appearance* and visibility.*/void onBarControlTargetChanged(@Nullable InsetsControlTarget statusControlling,@Nullable InsetsControlTarget fakeStatusControlling,@Nullable InsetsControlTarget navControlling,@Nullable InsetsControlTarget fakeNavControlling) {onControlChanged(ITYPE_STATUS_BAR, statusControlling);//状态栏onControlChanged(ITYPE_NAVIGATION_BAR, navControlling);//导航栏onControlChanged(ITYPE_CLIMATE_BAR, statusControlling);onControlChanged(ITYPE_EXTRA_NAVIGATION_BAR, navControlling);onControlFakeTargetChanged(ITYPE_STATUS_BAR, fakeStatusControlling);onControlFakeTargetChanged(ITYPE_NAVIGATION_BAR, fakeNavControlling);onControlFakeTargetChanged(ITYPE_CLIMATE_BAR, fakeStatusControlling);onControlFakeTargetChanged(ITYPE_EXTRA_NAVIGATION_BAR, fakeNavControlling);notifyPendingInsetsControlChanged();}
}