之前梳理了Activity的启动时序图,想着Activity.onWindowFocusChanged()的调用流程又是怎样的?追了下源码,梳理出来分享下。
首先看ActivityThread.handleResumeActivity()
public final class ActivityThread {
final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
...
final Activity a = r.activity;
...
View decor = r.window.getDecorView();
...
ViewManager wm = a.getWindowManager();
...
wm.addView(decor, l);
}
}
ViewManager是一个接口,看Activity实现
public class Activity extends ContextThemeWrapper
implements LayoutInflater.Factory2,
Window.Callback, KeyEvent.Callback,
OnCreateContextMenuListener, ComponentCallbacks2,
Window.OnWindowDismissedCallback, WindowControllerCallback,
AutofillManager.AutofillClient {
private WindowManager mWindowManager;
public WindowManager getWindowManager() {
return mWindowManager;
}
private Window mWindow;
final void attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Window window, ActivityConfigCallback activityConfigCallback) {
...
mWindow = new PhoneWindow(this, window, activityConfigCallback);
...
// 打个点
mWindow.setCallback(this);
...
mWindowManager = mWindow.getWindowManager();
...
}
}
WindowManager也是一个接口,继承了ViewManager,初始化来源于Window.getWindowManager()
public abstract class Window {
private WindowManager mWindowManager;
public WindowManager getWindowManager() {
return mWindowManager;
}
public void setWindowManager(WindowManager wm, IBinder appToken, String appName,
boolean hardwareAccelerated) {
mAppToken = appToken;
mAppName = appName;
mHardwareAccelerated = hardwareAccelerated
|| SystemProperties.getBoolean(PROPERTY_HARDWARE_UI, false);
if (wm == null) {
wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
}
mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this);
}
public void setCallback(Callback callback) {
mCallback = callback;
}
public final Callback getCallback() {
return mCallback;
}
}
可以看到Window.mWindowManager赋值位置在setWindowManager()传进来的,具体是实现类是WindowManagerImpl
那么看WindowManagerImpl.addView()实现了什么
public final class WindowManagerImpl implements WindowManager {
private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();
@Override
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
applyDefaultToken(params);
mGlobal.addView(view, params, mContext.getDisplay(), mParentWindow);
}
}
public final class WindowManagerGlobal {
public void addView(View view, ViewGroup.LayoutParams params,
Display display, Window parentWindow) {
...
ViewRootImpl root;
...
root = new ViewRootImpl(view.getContext(), display);
...
try {
root.setView(view, wparams, panelParentView);
} catch (RuntimeException e) {
...
}
}
public static IWindowSession getWindowSession() {
synchronized (WindowManagerGlobal.class) {
if (sWindowSession == null) {
try {
InputMethodManager imm = InputMethodManager.getInstance();
IWindowManager windowManager = getWindowManagerService();
sWindowSession = windowManager.openSession(
new IWindowSessionCallback.Stub() {
@Override
public void onAnimatorScaleChanged(float scale) {
ValueAnimator.setDurationScale(scale);
}
},
imm.getClient(), imm.getInputContext());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
return sWindowSession;
}
}
}
到此,就是ViewRootImpl的事了
public final class ViewRootImpl {
final IWindowSession mWindowSession;
public ViewRootImpl(Context context, Display display) {
mWindowSession = WindowManagerGlobal.getWindowSession();
}
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
getHostVisibility(), mDisplay.getDisplayId(),
mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
mAttachInfo.mOutsets, mInputChannel);
}
}
mWindowSession是通过WindowManagerGlobal.getWindowSession()赋值,源码看上面,IWindowSession是通过IWindowManager.openSession()获取,而IWindowManager实现类是WindowManagerService
public class WindowManagerService extends IWindowManager.Stub {
@Override
public IWindowSession openSession(IWindowSessionCallback callback, IInputMethodClient client,
IInputContext inputContext) {
if (client == null) throw new IllegalArgumentException("null client");
if (inputContext == null) throw new IllegalArgumentException("null inputContext");
Session session = new Session(this, callback, client, inputContext);
return session;
}
public int addWindow(Session session, IWindow client, int seq,
WindowManager.LayoutParams attrs, int viewVisibility, int displayId,
Rect outContentInsets, Rect outStableInsets, Rect outOutsets,
InputChannel outInputChannel) {
...
focusChanged = updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS,
false /*updateInputWindows*/);
...
}
RootWindowContainer mRoot;
final H mH = new H();
boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows) {
WindowState newFocus = mRoot.computeFocusedWindow();
...
mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
...
mCurrentFocus = newFocus;
}
final class H extends android.os.Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case REPORT_FOCUS_CHANGE: {
WindowState newFocus;
...
newFocus = mCurrentFocus;
...
newFocus.reportFocusChangedSerialized(true, mInTouchMode);
...
} break;
}
}
}
}
IWindowSession实现类是Session,那么看Session对addToDisplay()的实现
public class Session extends IWindowSession.Stub implements IBinder.DeathRecipient {
final WindowManagerService mService;
@Override
public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs,
int viewVisibility, int displayId, Rect outContentInsets, Rect outStableInsets,
Rect outOutsets, InputChannel outInputChannel) {
return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId,
outContentInsets, outStableInsets, outOutsets, outInputChannel);
}
}
又跳回了WindowManagerService.addWindow(),看上面源码,跳进了WindowState了,往下跟
class WindowState {
WindowState(WindowManagerService service, Session s, IWindow c, WindowToken token,
WindowState parentWindow, int appOp, int seq, WindowManager.LayoutParams a,
int viewVisibility, int ownerId, boolean ownerCanAddInternalSystemWindow) {
mClient = c;
}
final IWindow mClient;
void reportFocusChangedSerialized(boolean focused, boolean inTouchMode) {
try {
mClient.windowFocusChanged(focused, inTouchMode);
} catch (RemoteException e) {
}
}
}
到此,起码看到了相似方法名了,那IWindow实现类是ViewRootImpl内部实现类
static class W extends IWindow.Stub {
private final WeakReference<ViewRootImpl> mViewAncestor;
private final IWindowSession mWindowSession;
W(ViewRootImpl viewAncestor) {
mViewAncestor = new WeakReference<ViewRootImpl>(viewAncestor);
mWindowSession = viewAncestor.mWindowSession;
}
@Override
public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
final ViewRootImpl viewAncestor = mViewAncestor.get();
if (viewAncestor != null) {
viewAncestor.windowFocusChanged(hasFocus, inTouchMode);
}
}
}
又回到了ViewRootImpl
public class ViewRootImpl {
final ViewRootHandler mHandler = new ViewRootHandler();
public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
Message msg = Message.obtain();
msg.what = MSG_WINDOW_FOCUS_CHANGED;
msg.arg1 = hasFocus ? 1 : 0;
msg.arg2 = inTouchMode ? 1 : 0;
mHandler.sendMessage(msg);
}
View mView;
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
synchronized (this) {
if (mView == null) {
mView = view;
...
}
}
}
final class ViewRootHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
...
case MSG_WINDOW_FOCUS_CHANGED: {
...
mView.dispatchWindowFocusChanged(hasWindowFocus);
...
} break;
...
}
}
}
}
终于看到了曙光,那么这个View从第一步知道就是DecorView
public class DecorView extends FrameLayout implements RootViewSurfaceTaker, WindowCallbacks {
private PhoneWindow mWindow;
DecorView(Context context, int featureId, PhoneWindow window,
WindowManager.LayoutParams params) {
...
setWindow(window);
...
}
void setWindow(PhoneWindow phoneWindow) {
mWindow = phoneWindow;
...
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
...
final Window.Callback cb = mWindow.getCallback();
if (cb != null && !mWindow.isDestroyed() && mFeatureId < 0) {
cb.onWindowFocusChanged(hasWindowFocus);
}
...
}
}
DecorView是整个Activity最外父容器,看到DecorView.onWindowFocusChanged()有个Window.Callback回调onWindowFocusChanged(),发现Callback来自PhoneWindow,看上面Window源码,Window提供了方法setCallback(Callback callback)和getCallback();那setCallback(Callback callback)是什么时候调用的,看上面Activity.attach()方法,发现mWindow.setCallback(this),而Activity是实现了Window.Callback接口的。
到此,Activity.onWindowFocusChanged(boolean hasFocus)触发流程结束。
对了,DecorView.setWindow(PhoneWindow phoneWindow)又是什么时候调用的?
回到第一步ActivityThread.handleResumeActivity(),发现View decor = r.window.getDecorView(),也就是DecorView是在这个地方出来的,跟进去发现在PhoneWindow里
public class PhoneWindow extends Window {
@Override
public final View getDecorView() {
if (mDecor == null || mForceDecorInstall) {
installDecor();
}
return mDecor;
}
private void installDecor() {
...
if (mDecor == null) {
mDecor = generateDecor(-1);
mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
mDecor.setIsRootNamespace(true);
if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
}
} else {
// 看这里
mDecor.setWindow(this);
}
...
}
protected DecorView generateDecor(int featureId) {
Context context;
if (mUseDecorContext) {
Context applicationContext = getContext().getApplicationContext();
if (applicationContext == null) {
context = getContext();
} else {
context = new DecorContext(applicationContext, getContext().getResources());
if (mTheme != -1) {
context.setTheme(mTheme);
}
}
} else {
context = getContext();
}
return new DecorView(context, featureId, this, getAttributes());
}
}
网友评论