Activity WMS ViewRootImpl三者关系(Activity创建窗口 按键分发等)

转载: https://blog.csdn.net/kc58236582/article/details/52088224

今天我们来梳理下Activity ViewRootImpl和WMS三者的关系,这里面看了网上的博客,也看了一些书,加上自己的总结,写了这篇博客。

1. Activity

我们先来看Activity,在ActivityThread中的performLaunchActivity函数中, 先创建了Activity,然后调用了Activity的attach函数


   
   
  1. ......
  2. Activity activity = null;
  3. try {
  4. java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
  5. activity = mInstrumentation.newActivity(
  6. cl, component.getClassName(), r.intent);
  7. ......
  8. if (activity != null) {
  9. Context appContext = createBaseContextForActivity(r, activity);
  10. CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
  11. Configuration config = new Configuration(mCompatConfiguration);
  12. if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
  13. + r.activityInfo.name + " with config " + config);
  14. activity.attach(appContext, this, getInstrumentation(), r.token,
  15. r.ident, app, r.intent, r.activityInfo, title, r.parent,
  16. r.embeddedID, r.lastNonConfigurationInstances, config,
  17. r.referrer, r.voiceInteractor);

下面我们再来看看Activity的attach函数,先创建了PhoneWindow对象给了mWindow,然后调用其setWindowManager设置其WindowManager,最后再调用mWindow的getWindowManager方法作为Activity的mWindowManager成员变量


   
   
  1. ......
  2. mWindow = new PhoneWindow( this); //新建PhoneWindow对象
  3. mWindow.setCallback( this); //这window中设置回调,在按键事件分发的时候中用到。如果有这个回调,就调用Activity的dispatchKeyEvent
  4. mWindow.setOnWindowDismissedCallback( this);
  5. ......
  6. mWindow.setWindowManager(
  7. (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
  8. mToken, mComponent.flattenToString(),
  9. (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
  10. if (mParent != null) {
  11. mWindow.setContainer(mParent.getWindow());
  12. }
  13. mWindowManager = mWindow.getWindowManager();

再来看看Window类的setWindowManager方法,也是最后调用了WindowManagerImpl的createLocalWindowManager


   
   
  1. public void setWindowManager(WindowManager wm, IBinder appToken, String appName,
  2. boolean hardwareAccelerated) {
  3. mAppToken = appToken;
  4. mAppName = appName;
  5. mHardwareAccelerated = hardwareAccelerated
  6. || SystemProperties.getBoolean(PROPERTY_HARDWARE_UI, false);
  7. if (wm == null) {
  8. wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
  9. }
  10. mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager( this);
  11. }

而createLocalWindowManager函数就是创建了一个WindowManagerImpl对象,因此Activity的mWindowManager成员变量最后就是WindowManagerImpl对象。


   
   
  1. public WindowManagerImpl createLocalWindowManager(Window parentWindow) {
  2. return new WindowManagerImpl(mDisplay, parentWindow);
  3. }

而在WindowManagerImpl中有一个mGlobal的变量,最后都是通过这个变量调用的方法,因此我们来看看这个类。


   
   
  1. public final class WindowManagerImpl implements WindowManager {
  2. private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();

WindowManagerGlobal类中主要有3个非常重要的变量


   
   
  1. private final ArrayList<View> mViews = new ArrayList<View>();
  2. private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
  3. private final ArrayList<WindowManager.LayoutParams> mParams =
  4. new ArrayList<WindowManager.LayoutParams>();

mViews保存的是View对象,DecorView

mRoots保存和顶层View关联的ViewRootImpl对象

mParams保存的是创建顶层View的layout参数。

而WindowManagerGlobal类也负责和WMS通信。


2. PhoneWindow

在上一节Activity中,我们在Activity的attach函数中新建了PhoneWindow对象

2.1 创建DecorView

在PhoneWindow的setContentView函数中会调用installDector来创建DecorView对象


   
   
  1. @ Override
  2. public void setContentView (View view, ViewGroup.LayoutParams params) {
  3. // Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
  4. // decor, when theme attributes and the like are crystalized. Do not check the feature
  5. // before this happens.
  6. if (mContentParent == null) {
  7. installDecor();
  8. } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
  9. mContentParent.removeAllViews();
  10. }
  11. if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
  12. view.setLayoutParams(params);
  13. final Scene newScene = new Scene(mContentParent, view);
  14. transitionTo(newScene);
  15. } else {
  16. mContentParent.addView(view, params);
  17. }
  18. mContentParent.requestApplyInsets();
  19. final Callback cb = getCallback();
  20. if (cb != null && !isDestroyed()) {
  21. cb.onContentChanged();
  22. }
  23. }

在installDecor函数中调用了generateDecor函数来创建按DecorView对象。


   
   
  1. private void installDecor() {
  2. if (mDecor == null) {
  3. mDecor = generateDecor();
  4. mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
  5. mDecor.setIsRootNamespace( true);
  6. if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
  7. mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
  8. }
  9. }
  10. ......

   
   
  1. protected DecorView generateDecor() {
  2. return new DecorView(getContext(), -1);
  3. }


2.2 DecorView的按键处理

而DecorView是PhoneWindow类的一个嵌入类。我们来看下按键事件在DecorView的处理过程,先是在ViewRootImpl的processKeyEvent函数


   
   
  1. final class ViewPostImeInputStage extends InputStage {
  2. public ViewPostImeInputStage(InputStage next) {
  3. super(next);
  4. }
  5. @ Override
  6. protected int onProcess (QueuedInputEvent q) {
  7. if (q.mEvent instanceof KeyEvent) {
  8. return processKeyEvent(q);
  9. } else {
  10. // If delivering a new non-key event, make sure the window is
  11. // now allowed to start updating.
  12. handleDispatchWindowAnimationStopped();
  13. final int source = q.mEvent.getSource();
  14. if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
  15. return processPointerEvent(q);
  16. } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
  17. return processTrackballEvent(q);
  18. } else {
  19. return processGenericMotionEvent(q);
  20. }
  21. }
  22. }

然后在processKeyEvent调用了了mView的dispatchKeyEvent函数,就是调用了DecorView的dispatchKeyEvent函数


   
   
  1. private int processKeyEvent(QueuedInputEvent q) {
  2. final KeyEvent event = (KeyEvent)q.mEvent;
  3. if (event.getAction() != KeyEvent.ACTION_UP) {
  4. // If delivering a new key event, make sure the window is
  5. // now allowed to start updating.
  6. handleDispatchWindowAnimationStopped();
  7. }
  8. // Deliver the key to the view hierarchy.
  9. if (mView.dispatchKeyEvent(event)) {
  10. return FINISH_HANDLED;
  11. }

然后在DecorView的dispatchKeyEvent中,最后调用getCallBack,如果有回调,就调用回调的dispatchKeyEvent,这个就是在Activity的attach中将Activity设置为PhoneWindow的回调,因此最后dispatchKeyEvent就会到Activity的dispatchKeyEvent函数中


   
   
  1. public boolean dispatchKeyEvent(KeyEvent event) {
  2. final int keyCode = event.getKeyCode();
  3. final int action = event.getAction();
  4. final boolean isDown = action == KeyEvent.ACTION_DOWN;
  5. if (isDown && (event.getRepeatCount() == 0)) {
  6. // First handle chording of panel key: if a panel key is held
  7. // but not released, try to execute a shortcut in it.
  8. if ((mPanelChordingKey > 0) && (mPanelChordingKey != keyCode)) {
  9. boolean handled = dispatchKeyShortcutEvent(event);
  10. if (handled) {
  11. return true;
  12. }
  13. }
  14. // If a panel is open, perform a shortcut on it without the
  15. // chorded panel key
  16. if ((mPreparedPanel != null) && mPreparedPanel.isOpen) {
  17. if (performPanelShortcut(mPreparedPanel, keyCode, event, 0)) {
  18. return true;
  19. }
  20. }
  21. }
  22. if (!isDestroyed()) {
  23. final Callback cb = getCallback();
  24. final boolean handled = cb != null && mFeatureId < 0 ? cb.dispatchKeyEvent(event)
  25. : super.dispatchKeyEvent(event);
  26. if (handled) {
  27. return true;
  28. }
  29. }
  30. return isDown ? PhoneWindow. this.onKeyDown(mFeatureId, event.getKeyCode(), event)
  31. : PhoneWindow. this.onKeyUp(mFeatureId, event.getKeyCode(), event);
  32. }
Activity的attach中调用了PhoneWindow的setCallBack函数将回调设置成Activity,最后dispatchKeyEvent就会调用到Activity的dispatchKeyEvent中。

   
   
  1. final void attach(Context context, ActivityThread aThread,
  2. Instrumentation instr, IBinder token, int ident,
  3. Application application, Intent intent, ActivityInfo info,
  4. CharSequence title, Activity parent, String id,
  5. NonConfigurationInstances lastNonConfigurationInstances,
  6. Configuration config, String referrer, IVoiceInteractor voiceInteractor) {
  7. attachBaseContext(context);
  8. mFragments.attachHost(null /*parent*/);
  9. mWindow = new PhoneWindow( this);
  10. mWindow.setCallback( this);


3. ViewRootImpl

在ActivityThread的handleResumeActivity函数中会调用WindowManager的addView函数,而这个WindowManager就是Activity的mWindowManager。


   
   
  1. if (r.window == null && !a.mFinished && willBeVisible) {
  2. r.window = r.activity.getWindow();
  3. View decor = r.window.getDecorView();
  4. decor.setVisibility(View.INVISIBLE);
  5. ViewManager wm = a.getWindowManager();
  6. WindowManager.LayoutParams l = r.window.getAttributes();
  7. a.mDecor = decor;
  8. l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
  9. l.softInputMode |= forwardBit;
  10. if (a.mVisibleFromClient) {
  11. a.mWindowAdded = true;
  12. wm.addView(decor, l);
  13. }
  14. // If the window has already been added, but during resume
  15. // we started another activity, then don't yet make the
  16. // window visible.
  17. }

这个我们在Activity一节中分析过了,因此最后addView函数在WindowManagerImpl中实现,我们再来看看WindowManagerImpl的addView函数。


   
   
  1. @ Override
  2. public void addView (@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
  3. applyDefaultToken(params);
  4. mGlobal.addView(view, params, mDisplay, mParentWindow);
  5. }

而在WindowManagerImpl最后是调用了WindowManagerGlobal的addView函数, 在这个函数中我们新建ViewRootImpl对象,然后调用了ViewRootImpl的setView函数,这里的view就是Activity的Window对象的DecorView。并且在这个函数中,把view root param这3个保存在mViews mRoots mParams这3个成员变量中了。


   
   
  1. ......
  2. ViewRootImpl root;
  3. View panelParentView = null;
  4. synchronized (mLock) {
  5. // Start watching for system property changes.
  6. if (mSystemPropertyUpdater == null) {
  7. mSystemPropertyUpdater = new Runnable() {
  8. @Override public void run() {
  9. synchronized (mLock) {
  10. for ( int i = mRoots.size() - 1; i >= 0; --i) {
  11. mRoots.get(i).loadSystemProperties();
  12. }
  13. }
  14. }
  15. };
  16. SystemProperties.addChangeCallback(mSystemPropertyUpdater);
  17. }
  18. int index = findViewLocked(view, false);
  19. if (index >= 0) {
  20. if (mDyingViews.contains(view)) {
  21. // Don't wait for MSG_DIE to make it's way through root's queue.
  22. mRoots.get(index).doDie();
  23. } else {
  24. throw new IllegalStateException( "View " + view
  25. + " has already been added to the window manager.");
  26. }
  27. // The previous removeView() had not completed executing. Now it has.
  28. }
  29. // If this is a panel window, then find the window it is being
  30. // attached to for future reference.
  31. if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
  32. wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
  33. final int count = mViews.size();
  34. for ( int i = 0; i < count; i++) {
  35. if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
  36. panelParentView = mViews.get(i);
  37. }
  38. }
  39. }
  40. root = new ViewRootImpl(view.getContext(), display); //创建ViewRootImpl
  41. view.setLayoutParams(wparams);
  42. mViews.add(view);
  43. mRoots.add(root); //保存各个成员变量
  44. mParams.add(wparams);
  45. }
  46. // do this last because it fires off messages to start doing things
  47. try {
  48. root.setView(view, wparams, panelParentView); //调用ViewRootImpl的setView函数
  49. } catch (RuntimeException e) {
  50. // BadTokenException or InvalidDisplayException, clean up.
  51. synchronized (mLock) {
  52. final int index = findViewLocked(view, false);
  53. if (index >= 0) {
  54. removeViewLocked(index, true);
  55. }
  56. }
  57. throw e;
  58. }
  59. }

在ViewRootImpl的setView函数中,先调用了requestLayout来绘制view,然后调用了mWindowSession的addToDisplay函数和WMS通信


   
   
  1. ......
  2. requestLayout(); //绘制View
  3. if ((mWindowAttributes.inputFeatures
  4. & WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
  5. mInputChannel = new InputChannel();
  6. }
  7. try {
  8. mOrigWindowType = mWindowAttributes.type;
  9. mAttachInfo.mRecomputeGlobalAttributes = true;
  10. collectViewAttributes();
  11. res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
  12. getHostVisibility(), mDisplay.getDisplayId(),
  13. mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
  14. mAttachInfo.mOutsets, mInputChannel);
  15. }

我们先来看下mWindowSession,就是通过WMS获取到WindowSession的Binder,并且自己设置了一个回调在WMS中


   
   
  1. public static IWindowSession getWindowSession() {
  2. synchronized (WindowManagerGlobal.class) {
  3. if (sWindowSession == null) {
  4. try {
  5. InputMethodManager imm = InputMethodManager.getInstance();
  6. IWindowManager windowManager = getWindowManagerService();
  7. sWindowSession = windowManager.openSession(
  8. new IWindowSessionCallback.Stub() {
  9. @Override
  10. public void onAnimatorScaleChanged( float scale) {
  11. ValueAnimator.setDurationScale(scale);
  12. }
  13. },
  14. imm.getClient(), imm.getInputContext());
  15. } catch (RemoteException e) {
  16. Log.e(TAG, "Failed to open window session", e);
  17. }
  18. }
  19. return sWindowSession;
  20. }
  21. }

最后BInder调用到Session的addToDisplay,也会调用WMS的addWindow函数,这个我们稍后在WMS中分析。


   
   
  1. public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs,
  2. int viewVisibility, int displayId, Rect outContentInsets, Rect outStableInsets,
  3. Rect outOutsets, InputChannel outInputChannel) {
  4. return mService.addWindow( this, window, seq, attrs, viewVisibility, displayId,
  5. outContentInsets, outStableInsets, outOutsets, outInputChannel);
  6. }

我们再来看ViewRootImpl中调用mWindowSession.addToDisplay函数传入的一个参数mWindow,

        mWindow = new W(this);
   
   

W是ViewRootImpl的一个嵌入类,也是一个Binder服务。通过mWindowSession.addToDisplay函数传入WMS,用来在WMS中通过Binder回调。


   
   
  1. static class W extends IWindow.Stub {
  2. private final WeakReference<ViewRootImpl> mViewAncestor;
  3. private final IWindowSession mWindowSession;
  4. W(ViewRootImpl viewAncestor) {
  5. mViewAncestor = new WeakReference<ViewRootImpl>(viewAncestor);
  6. mWindowSession = viewAncestor.mWindowSession;
  7. }
  8. @ Override
  9. public void resized (Rect frame, Rect overscanInsets, Rect contentInsets,
  10. Rect visibleInsets, Rect stableInsets, Rect outsets, boolean reportDraw,
  11. Configuration newConfig) {
  12. final ViewRootImpl viewAncestor = mViewAncestor.get();
  13. if (viewAncestor != null) {
  14. viewAncestor.dispatchResized(frame, overscanInsets, contentInsets,
  15. visibleInsets, stableInsets, outsets, reportDraw, newConfig);
  16. }
  17. }
  18. ......


4. WMS

WMS指的是WindowManagerService,下面我们来看下WMS


   
   
  1. public class WindowManagerService extends IWindowManager.Stub
  2. implements Watchdog. Monitor, WindowManagerPolicy. WindowManagerFuncs {
  3. ......
  4. final WindowManagerPolicy mPolicy = new PhoneWindowManager();
  5. ......
  6. /**
  7. * All currently active sessions with clients.
  8. */
  9. final ArraySet<Session> mSessions = new ArraySet<>();
  10. /**
  11. * Mapping from an IWindow IBinder to the server's Window object.
  12. * This is also used as the lock for all of our state.
  13. * NOTE: Never call into methods that lock ActivityManagerService while holding this object.
  14. */
  15. final HashMap<IBinder, WindowState> mWindowMap = new HashMap<>();
  16. /**
  17. * Mapping from a token IBinder to a WindowToken object.
  18. */
  19. final HashMap<IBinder, WindowToken> mTokenMap = new HashMap<>();

mPolicy是WMS所执行的窗口管理策略类,现在android只有PhoneWindowManager一个策略类。

mSessions存储的是Session服务类,每个应用都在WMS中有一个对应的Session对象保存在mSessions中。

mTokenMap保存的是所有窗口的WindowToken对象。

mWindowMap保存的是所有窗口的WindowState对象。

4.1 WindowToken对象

在WMS中有两种常见的Token,WindowToken和AppWindowToken。先来看WindowToken的定义:


   
   
  1. class WindowToken {
  2. // The window manager!
  3. final WindowManagerService service;
  4. // The actual token.
  5. final IBinder token;
  6. // The type of window this token is for, as per WindowManager.LayoutParams.
  7. final int windowType;
  8. // Set if this token was explicitly added by a client, so should
  9. // not be removed when all windows are removed.
  10. final boolean explicit;
  11. // For printing.
  12. String stringName;
  13. // If this is an AppWindowToken, this is non-null.
  14. AppWindowToken appWindowToken;
  15. // All of the windows associated with this token.
  16. final WindowList windows = new WindowList();

WindowToken的成员变量token是IBinder对象,具有系统唯一性。因此,向WMS的mWindowMap或者mTokenMap插入对象时都是使用token值作为索引。

WindowToken用来表示一组窗口对象,windowType表示窗口类型。

explicit为false表示这个Token是在WMS中创建的,true表示在其他模块通过调用addAppToken或者addWindowToken方法显示创建的。

windows类型是WindowList,包含一个WindowState对象的列表,所有拥有相同WindowToken的窗口都在这个列表中。一般而言窗口和依附于它的子窗口拥有相同的WindowToken对象。

APPWindowToken从WindowToken类派生,是一种比较特殊的WindowToken,代表应用窗口,主要是Activity中创建的顶层窗口。一个WindowToken对象的成员变量APPWindowToken如果为NULL,那么它就不是APPWindowToken,否则就是APPWindowToken对象。

APPWindowToken中增加了几个特有的成员变量,如下:


   
   
  1. class AppWindowToken extends WindowToken {
  2. // Non-null only for application tokens.
  3. final IApplicationToken appToken;
  4. // All of the windows and child windows that are included in this
  5. // application token. Note this list is NOT sorted!
  6. final WindowList allAppWindows = new WindowList();
  7. final AppWindowAnimator mAppAnimator;
  8. final WindowAnimator mAnimator;

其中appToken用来表示应用的Token,它是在AMS中创建的,代表一个应用。

allAppWindows也是一个WindowList,保存了所有相同APPWindowToken的应用窗口及其子窗口。

在WMS中定义了addAppToken用来向WMS添加一个APPWindowToken类型的Token。

4.2 窗口类型

在WMS中定义了3中类型的窗口:应用窗口,子窗口,系统窗口。

应用窗口:

应用窗口是Activity使用的全屏窗口。

1.TYPE_BASE_APPLICATION 应用中所有窗口都位于其上层

2.TYPE_APPLICATION Activity的顶层窗口,也是最常见的窗口

3.TYPE_APPLICATION_STARTING 应用启动时系统创建的窗口,显示一些信息,直到应用窗口显示出来

子窗口:

子窗口是依附于应用窗口或者系统窗口的窗口类型,它们一般随所依附的窗口一起出现或者切到后台。

TYPE_APPLICATION_PANEL:应用面板窗口,显示于所依附窗口上层

TYPE_APPLICATION_MEDIA:应用Media窗口,通常独立的Surface,用于播放Video或显示照相机预览画面。显示于所依附窗口的下层

TYPE_APPLICATION_SUB_PANEL:子面板窗口,显示在所有子窗口的上层

TYPE_APPLICATION_ATTACHED_DIALOG:和面板窗口类似的窗口,但是layout的方式和Activity的顶层窗口类似

TYPE_APPLICATION_MEDIA_OVERLAY:一种显示在Media窗口和所依附窗口之间的半透明窗口,用于显示Video字幕或者相机操作提示界面

系统窗口:

系统窗口大都用于系统生成的UI中,比较独立,种类较多。

状态条,搜索,通话,输入法等。


   
   
  1. import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW; //第一个应用窗口
  2. import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW; //第一个子窗口
  3. import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
  4. import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
  5. import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
  6. import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
  7. import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
  8. import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
  9. import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW; //最后一个应用窗口
  10. import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW; //最后一个子窗口
  11. import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
  12. import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
  13. import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION;
  14. import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
  15. import static android.view.WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
  16. import static android.view.WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
  17. import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION; //Activity顶层窗口,应用中最常见窗口
  18. import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING; //应用启动时由系统创建的窗口,显示一些信息,直到应用创建的窗口显示出来
  19. import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION; //基础应用窗口,应用中所有窗口都位于其上层。只能又系统创建
  20. import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
  21. import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
  22. import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
  23. import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
  24. import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
  25. import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_SCRIM;
  26. import static android.view.WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION;
  27. import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR; //状态条窗口
  28. import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; //显示状态条弹出对话框
  29. import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR; //系统内部错误窗口
  30. import static android.view.WindowManager.LayoutParams.TYPE_VOICE_INTERACTION;
  31. import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
  32. import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
  33. import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;

还有一些在WindowManager中的LayoutParams中


   
   
  1. public static final int TYPE_APPLICATION_PANEL = FIRST_SUB_WINDOW;
  2. /**
  3. * Window type: window for showing media (such as video). These windows
  4. * are displayed behind their attached window.
  5. */
  6. public static final int TYPE_APPLICATION_MEDIA = FIRST_SUB_WINDOW + 1;
  7. /**
  8. * Window type: a sub-panel on top of an application window. These
  9. * windows are displayed on top their attached window and any
  10. * {@link #TYPE_APPLICATION_PANEL} panels.
  11. */
  12. public static final int TYPE_APPLICATION_SUB_PANEL = FIRST_SUB_WINDOW + 2;
  13. /** Window type: like {@link #TYPE_APPLICATION_PANEL}, but layout
  14. * of the window happens as that of a top-level window, <em>not</em>
  15. * as a child of its container.
  16. */
  17. public static final int TYPE_APPLICATION_ATTACHED_DIALOG = FIRST_SUB_WINDOW + 3;
  18. /**
  19. * Window type: window for showing overlays on top of media windows.
  20. * These windows are displayed between TYPE_APPLICATION_MEDIA and the
  21. * application window. They should be translucent to be useful. This
  22. * is a big ugly hack so:
  23. * @hide
  24. */
  25. public static final int TYPE_APPLICATION_MEDIA_OVERLAY = FIRST_SUB_WINDOW + 4;


4.3 新建窗口的过程

每次新建Activity会增加窗口,弹出菜单同样会在WMS增加窗口。而最终都是通过Session的addToDisplay向WMS增加一个窗口。WMS最终会调用addWindow来实现这个功能。

我们来回顾下之前Activity是如何调用增加一个窗口的。

新建Activity的时候先调用handleLaunchActivity,然后在这个函数中会调用handleResumeActivity函数然后在这个函数中会调用WindowManagerGlobal的addView函数,而在WindowManagerGlobal的addView函数中会新加ViewRootImpl对象,然后调用其setView函数,在setView中会调用mWindowSession.addToDisplay函数,最后到WMS的addWindow函数。


4.3.1 参数检查

   
   
  1. public int addWindow(Session session, IWindow client, int seq,
  2. WindowManager.LayoutParams attrs, int viewVisibility, int displayId,
  3. Rect outContentInsets, Rect outStableInsets, Rect outOutsets,
  4. InputChannel outInputChannel) {
  5. ......
  6. //检查mWindowMap是否有该窗口,如果有重复了,直接退出
  7. if (mWindowMap.containsKey(client.asBinder())) {
  8. Slog.w(TAG, "Window " + client + " is already added");
  9. return WindowManagerGlobal.ADD_DUPLICATE_ADD;
  10. }
  11. //子窗口类型,父窗口类型必须存在且不能是子窗口类型,否则退出
  12. if (type >= FIRST_SUB_WINDOW && type <= LAST_SUB_WINDOW) {
  13. attachedWindow = windowForClientLocked(null, attrs.token, false);
  14. if (attachedWindow == null) {
  15. Slog.w(TAG, "Attempted to add window with token that is not a window: "
  16. + attrs.token + ". Aborting.");
  17. return WindowManagerGlobal.ADD_BAD_SUBWINDOW_TOKEN;
  18. }
  19. if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
  20. && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
  21. Slog.w(TAG, "Attempted to add window with token that is a sub-window: "
  22. + attrs.token + ". Aborting.");
  23. return WindowManagerGlobal.ADD_BAD_SUBWINDOW_TOKEN;
  24. }
  25. }
  26. //私有窗口类型,显示设备不是私有类型,退出
  27. if (type == TYPE_PRIVATE_PRESENTATION && !displayContent.isPrivate()) {
  28. Slog.w(TAG, "Attempted to add private presentation window to a non-private display. Aborting.");
  29. return WindowManagerGlobal.ADD_PERMISSION_DENIED;
  30. }
  31. boolean addToken = false;
  32. WindowToken token = mTokenMap.get(attrs.token);
  33. //如果mTokenMap中没有,但是窗口类型是应用窗口,输入法窗口,壁纸,Dream则退出
  34. if (token == null) {
  35. if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {
  36. Slog.w(TAG, "Attempted to add application window with unknown token "
  37. + attrs.token + ". Aborting.");
  38. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  39. }
  40. if (type == TYPE_INPUT_METHOD) {
  41. Slog.w(TAG, "Attempted to add input method window with unknown token "
  42. + attrs.token + ". Aborting.");
  43. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  44. }
  45. if (type == TYPE_VOICE_INTERACTION) {
  46. Slog.w(TAG, "Attempted to add voice interaction window with unknown token "
  47. + attrs.token + ". Aborting.");
  48. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  49. }
  50. if (type == TYPE_WALLPAPER) {
  51. Slog.w(TAG, "Attempted to add wallpaper window with unknown token "
  52. + attrs.token + ". Aborting.");
  53. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  54. }
  55. if (type == TYPE_DREAM) {
  56. Slog.w(TAG, "Attempted to add Dream window with unknown token "
  57. + attrs.token + ". Aborting.");
  58. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  59. }
  60. if (type == TYPE_ACCESSIBILITY_OVERLAY) {
  61. Slog.w(TAG, "Attempted to add Accessibility overlay window with unknown token "
  62. + attrs.token + ". Aborting.");
  63. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  64. }
  65. //其他类型的窗口新建token
  66. token = new WindowToken( this, attrs.token, -1, false);
  67. addToken = true;
  68. } else if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {
  69. //应用类型窗口
  70. AppWindowToken atoken = token.appWindowToken;
  71. if (atoken == null) { //应用类型窗口必须有appWindowToken
  72. Slog.w(TAG, "Attempted to add window with non-application token "
  73. + token + ". Aborting.");
  74. return WindowManagerGlobal.ADD_NOT_APP_TOKEN;
  75. } else if (atoken.removed) {
  76. Slog.w(TAG, "Attempted to add window with exiting application token "
  77. + token + ". Aborting.");
  78. return WindowManagerGlobal.ADD_APP_EXITING;
  79. }
  80. if (type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
  81. //还在启动中,不能添加窗口
  82. // No need for this guy!
  83. if (localLOGV) Slog.v(
  84. TAG, "**** NO NEED TO START: " + attrs.getTitle());
  85. return WindowManagerGlobal.ADD_STARTING_NOT_NEEDED;
  86. }
  87. } else if (type == TYPE_INPUT_METHOD) { //输入法
  88. if (token.windowType != TYPE_INPUT_METHOD) {
  89. Slog.w(TAG, "Attempted to add input method window with bad token "
  90. + attrs.token + ". Aborting.");
  91. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  92. }
  93. } else if (type == TYPE_VOICE_INTERACTION) { //
  94. if (token.windowType != TYPE_VOICE_INTERACTION) {
  95. Slog.w(TAG, "Attempted to add voice interaction window with bad token "
  96. + attrs.token + ". Aborting.");
  97. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  98. }
  99. } else if (type == TYPE_WALLPAPER) { //壁纸
  100. if (token.windowType != TYPE_WALLPAPER) {
  101. Slog.w(TAG, "Attempted to add wallpaper window with bad token "
  102. + attrs.token + ". Aborting.");
  103. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  104. }
  105. } else if (type == TYPE_DREAM) { //Dream
  106. if (token.windowType != TYPE_DREAM) {
  107. Slog.w(TAG, "Attempted to add Dream window with bad token "
  108. + attrs.token + ". Aborting.");
  109. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  110. }
  111. } else if (type == TYPE_ACCESSIBILITY_OVERLAY) {
  112. if (token.windowType != TYPE_ACCESSIBILITY_OVERLAY) {
  113. Slog.w(TAG, "Attempted to add Accessibility overlay window with bad token "
  114. + attrs.token + ". Aborting.");
  115. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  116. }
  117. } else if (token.appWindowToken != null) {
  118. Slog.w(TAG, "Non-null appWindowToken for system window of type=" + type);
  119. // It is not valid to use an app token with other system types; we will
  120. // instead make a new token for it (as if null had been passed in for the token).
  121. attrs.token = null;
  122. token = new WindowToken( this, null, -1, false);
  123. addToken = true;
  124. }

这段代码主要是进行参数检查,大部分参数从客户进程传过来的。为了保证数据的一致性,重点检查窗口的WindowToken对象中的窗口类型和之前在WMS的WindowToken的windowType是否一致。这种检查主要针对应用窗口、输入法、壁纸、dream、TYPE_VOICE_INTERACTION、TYPE_ACCESSIBILITY_OVERLAY,因为这几种窗口的WindowToken对象在addWindow前已经创建好并加入WMS的mTokenMap中,这里需要检查是否一致。

一些特殊的窗口,入输入法窗口,必须由InputManagerService允许后才能创建。这些服务会预先调用WMS的addWindowToken方法插入WindowToken到WMS的mTokenMap中。如果应用不通过服务获取相应的WindowToken将无法创建这些特殊窗口,方便控制。

对于其他类型的窗口,addWindow方法会为它们创建WindowToken对象。


4.3.2 创建窗口对象

   
   
  1. //创建WindowState对象
  2. WindowState win = new WindowState( this, session, client, token,
  3. attachedWindow, appOp[ 0], seq, attrs, viewVisibility, displayContent);
  4. if (win.mDeathRecipient == null) {
  5. // Client has apparently died, so there is no reason to
  6. // continue.
  7. Slog.w(TAG, "Adding window client " + client.asBinder()
  8. + " that is dead, aborting.");
  9. return WindowManagerGlobal.ADD_APP_EXITING;
  10. }
  11. if (win.getDisplayContent() == null) {
  12. Slog.w(TAG, "Adding window to Display that has been removed.");
  13. return WindowManagerGlobal.ADD_INVALID_DISPLAY;
  14. }
  15. mPolicy.adjustWindowParamsLw(win.mAttrs);
  16. //检查和设置查看是否能够让其他用户看见
  17. win.setShowToOwnerOnlyLocked(mPolicy.checkShowToOwnerOnly(attrs));
  18. res = mPolicy.prepareAddWindowLw(win, attrs);
  19. if (res != WindowManagerGlobal.ADD_OKAY) {
  20. return res;
  21. }
  22. //为按键创建和应用的通信通道
  23. if (outInputChannel != null && (attrs.inputFeatures
  24. & WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
  25. String name = win.makeInputChannelName();
  26. InputChannel[] inputChannels = InputChannel.openInputChannelPair(name);
  27. win.setInputChannel(inputChannels[ 0]);
  28. inputChannels[ 1].transferTo(outInputChannel);
  29. mInputManager.registerInputChannel(win.mInputChannel, win.mInputWindowHandle);
  30. }
  31. // From now on, no exceptions or errors allowed!
  32. res = WindowManagerGlobal.ADD_OKAY;
  33. origId = Binder.clearCallingIdentity();
  34. if (addToken) { //如果是新的Token,加入mTokenMap
  35. mTokenMap.put(attrs.token, token);
  36. }
  37. win.attach();
  38. mWindowMap.put(client.asBinder(), win); //加入WindowState对象列表中
  39. ......

这段代码主要是保存WindowState对象,放入mWindowMap中。

mPolicy的adjustWindowParamsLw方法检查窗口类型是否是TYPE_SYSTEM_OVERLAY或者TYPE_SERCURE_SYSTEM_OVERLAY,如果是将窗口标记上FLAG_NOT_FOCUSABLE FLAG_NOT_TOUCHABLE FLAG_WATCH_OUTSIDE_TOUCH.这些窗口不能获取焦点。


4.3.3 将窗口加入Display列表

   
   
  1. boolean imMayMove = true;
  2. if (type == TYPE_INPUT_METHOD) { //如果窗口类是输入法窗口
  3. win.mGivenInsetsPending = true;
  4. mInputMethodWindow = win;
  5. addInputMethodWindowToListLocked(win); //插入输入法窗口到应用窗口上层
  6. imMayMove = false;
  7. } else if (type == TYPE_INPUT_METHOD_DIALOG) { //如果窗口是输入法对话框
  8. mInputMethodDialogs.add(win);
  9. addWindowToListInOrderLocked(win, true); //插入到正常位置
  10. moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked( true)); //调整对话框位置
  11. imMayMove = false;
  12. } else {
  13. addWindowToListInOrderLocked(win, true);
  14. if (type == TYPE_WALLPAPER) {
  15. mLastWallpaperTimeoutTime = 0;
  16. displayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
  17. } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
  18. displayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
  19. } else if (mWallpaperTarget != null
  20. && mWallpaperTarget.mLayer >= win.mBaseLayer) {
  21. // If there is currently a wallpaper being shown, and
  22. // the base layer of the new window is below the current
  23. // layer of the target window, then adjust the wallpaper.
  24. // This is to avoid a new window being placed between the
  25. // wallpaper and its target.
  26. displayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
  27. }
  28. }

DisplayContent类的mWindows列表按Z序保存了每个窗口,这段代码就是在根据窗口类型把窗口加入到DisplayContent合适位置。

addInputMethodWindowToListLocked方法作用就是一个输入法窗口放子啊需要显示输入法窗口的上面。

addWindowToListInOrderLocked将一个窗口插入到窗口堆栈的当前位置。


4.3.4 调整窗口的次序


   
   
  1. winAnimator.mEnterAnimationPending = true;
  2. winAnimator.mEnteringAnimation = true;
  3. if (displayContent.isDefaultDisplay) {
  4. mPolicy.getInsetHintLw(win.mAttrs, mRotation, outContentInsets, outStableInsets, //计算窗口大小
  5. outOutsets);
  6. } else {
  7. outContentInsets.setEmpty();
  8. outStableInsets.setEmpty();
  9. }
  10. if (mInTouchMode) {
  11. res |= WindowManagerGlobal.ADD_FLAG_IN_TOUCH_MODE;
  12. }
  13. if (win.mAppToken == null || !win.mAppToken.clientHidden) {
  14. res |= WindowManagerGlobal.ADD_FLAG_APP_VISIBLE;
  15. }
  16. mInputMonitor.setUpdateInputWindowsNeededLw();
  17. boolean focusChanged = false;
  18. if (win.canReceiveKeys()) { //如果窗口能接受输入,计算是否引起焦点变化
  19. focusChanged = updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS,
  20. false /*updateInputWindows*/);
  21. if (focusChanged) {
  22. imMayMove = false;
  23. }
  24. }
  25. if (imMayMove) {
  26. moveInputMethodWindowsIfNeededLocked( false);
  27. }
  28. assignLayersLocked(displayContent.getWindowList());
  29. // Don't do layout here, the window must call
  30. // relayout to be displayed, so we'll do it there.
  31. if (focusChanged) {
  32. mInputMonitor.setInputFocusLw(mCurrentFocus, false /*updateInputWindows*/);
  33. }
  34. mInputMonitor.updateInputWindowsLw( false /*force*/);
  35. if (localLOGV || DEBUG_ADD_REMOVE) Slog.v(TAG, "addWindow: New client "
  36. + client.asBinder() + ": window=" + win + " Callers=" + Debug.getCallers( 5));
  37. if (win.isVisibleOrAdding() && updateOrientationFromAppTokensLocked( false)) {
  38. reportNewConfig = true; //如果窗口的配置发生变化
  39. }
  40. }
  41. if (reportNewConfig) {
  42. sendNewConfiguration(); //发生新的配置
  43. }
  44. Binder.restoreCallingIdentity(origId);
  45. return res;

后续还有确定窗口Z轴位置,窗口尺寸,窗口动画后续接触到再分析吧。



转载: https://blog.csdn.net/kc58236582/article/details/52088224

今天我们来梳理下Activity ViewRootImpl和WMS三者的关系,这里面看了网上的博客,也看了一些书,加上自己的总结,写了这篇博客。

1. Activity

我们先来看Activity,在ActivityThread中的performLaunchActivity函数中, 先创建了Activity,然后调用了Activity的attach函数


 
 
  1. ......
  2. Activity activity = null;
  3. try {
  4. java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
  5. activity = mInstrumentation.newActivity(
  6. cl, component.getClassName(), r.intent);
  7. ......
  8. if (activity != null) {
  9. Context appContext = createBaseContextForActivity(r, activity);
  10. CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
  11. Configuration config = new Configuration(mCompatConfiguration);
  12. if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
  13. + r.activityInfo.name + " with config " + config);
  14. activity.attach(appContext, this, getInstrumentation(), r.token,
  15. r.ident, app, r.intent, r.activityInfo, title, r.parent,
  16. r.embeddedID, r.lastNonConfigurationInstances, config,
  17. r.referrer, r.voiceInteractor);

下面我们再来看看Activity的attach函数,先创建了PhoneWindow对象给了mWindow,然后调用其setWindowManager设置其WindowManager,最后再调用mWindow的getWindowManager方法作为Activity的mWindowManager成员变量


 
 
  1. ......
  2. mWindow = new PhoneWindow( this); //新建PhoneWindow对象
  3. mWindow.setCallback( this); //这window中设置回调,在按键事件分发的时候中用到。如果有这个回调,就调用Activity的dispatchKeyEvent
  4. mWindow.setOnWindowDismissedCallback( this);
  5. ......
  6. mWindow.setWindowManager(
  7. (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
  8. mToken, mComponent.flattenToString(),
  9. (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
  10. if (mParent != null) {
  11. mWindow.setContainer(mParent.getWindow());
  12. }
  13. mWindowManager = mWindow.getWindowManager();

再来看看Window类的setWindowManager方法,也是最后调用了WindowManagerImpl的createLocalWindowManager


 
 
  1. public void setWindowManager(WindowManager wm, IBinder appToken, String appName,
  2. boolean hardwareAccelerated) {
  3. mAppToken = appToken;
  4. mAppName = appName;
  5. mHardwareAccelerated = hardwareAccelerated
  6. || SystemProperties.getBoolean(PROPERTY_HARDWARE_UI, false);
  7. if (wm == null) {
  8. wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
  9. }
  10. mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager( this);
  11. }

而createLocalWindowManager函数就是创建了一个WindowManagerImpl对象,因此Activity的mWindowManager成员变量最后就是WindowManagerImpl对象。


 
 
  1. public WindowManagerImpl createLocalWindowManager(Window parentWindow) {
  2. return new WindowManagerImpl(mDisplay, parentWindow);
  3. }

而在WindowManagerImpl中有一个mGlobal的变量,最后都是通过这个变量调用的方法,因此我们来看看这个类。


 
 
  1. public final class WindowManagerImpl implements WindowManager {
  2. private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();

WindowManagerGlobal类中主要有3个非常重要的变量


 
 
  1. private final ArrayList<View> mViews = new ArrayList<View>();
  2. private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
  3. private final ArrayList<WindowManager.LayoutParams> mParams =
  4. new ArrayList<WindowManager.LayoutParams>();

mViews保存的是View对象,DecorView

mRoots保存和顶层View关联的ViewRootImpl对象

mParams保存的是创建顶层View的layout参数。

而WindowManagerGlobal类也负责和WMS通信。


2. PhoneWindow

在上一节Activity中,我们在Activity的attach函数中新建了PhoneWindow对象

2.1 创建DecorView

在PhoneWindow的setContentView函数中会调用installDector来创建DecorView对象


 
 
  1. @ Override
  2. public void setContentView (View view, ViewGroup.LayoutParams params) {
  3. // Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
  4. // decor, when theme attributes and the like are crystalized. Do not check the feature
  5. // before this happens.
  6. if (mContentParent == null) {
  7. installDecor();
  8. } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
  9. mContentParent.removeAllViews();
  10. }
  11. if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
  12. view.setLayoutParams(params);
  13. final Scene newScene = new Scene(mContentParent, view);
  14. transitionTo(newScene);
  15. } else {
  16. mContentParent.addView(view, params);
  17. }
  18. mContentParent.requestApplyInsets();
  19. final Callback cb = getCallback();
  20. if (cb != null && !isDestroyed()) {
  21. cb.onContentChanged();
  22. }
  23. }

在installDecor函数中调用了generateDecor函数来创建按DecorView对象。


 
 
  1. private void installDecor() {
  2. if (mDecor == null) {
  3. mDecor = generateDecor();
  4. mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
  5. mDecor.setIsRootNamespace( true);
  6. if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
  7. mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
  8. }
  9. }
  10. ......

 
 
  1. protected DecorView generateDecor() {
  2. return new DecorView(getContext(), -1);
  3. }


2.2 DecorView的按键处理

而DecorView是PhoneWindow类的一个嵌入类。我们来看下按键事件在DecorView的处理过程,先是在ViewRootImpl的processKeyEvent函数


 
 
  1. final class ViewPostImeInputStage extends InputStage {
  2. public ViewPostImeInputStage(InputStage next) {
  3. super(next);
  4. }
  5. @ Override
  6. protected int onProcess (QueuedInputEvent q) {
  7. if (q.mEvent instanceof KeyEvent) {
  8. return processKeyEvent(q);
  9. } else {
  10. // If delivering a new non-key event, make sure the window is
  11. // now allowed to start updating.
  12. handleDispatchWindowAnimationStopped();
  13. final int source = q.mEvent.getSource();
  14. if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
  15. return processPointerEvent(q);
  16. } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
  17. return processTrackballEvent(q);
  18. } else {
  19. return processGenericMotionEvent(q);
  20. }
  21. }
  22. }

然后在processKeyEvent调用了了mView的dispatchKeyEvent函数,就是调用了DecorView的dispatchKeyEvent函数


 
 
  1. private int processKeyEvent(QueuedInputEvent q) {
  2. final KeyEvent event = (KeyEvent)q.mEvent;
  3. if (event.getAction() != KeyEvent.ACTION_UP) {
  4. // If delivering a new key event, make sure the window is
  5. // now allowed to start updating.
  6. handleDispatchWindowAnimationStopped();
  7. }
  8. // Deliver the key to the view hierarchy.
  9. if (mView.dispatchKeyEvent(event)) {
  10. return FINISH_HANDLED;
  11. }

然后在DecorView的dispatchKeyEvent中,最后调用getCallBack,如果有回调,就调用回调的dispatchKeyEvent,这个就是在Activity的attach中将Activity设置为PhoneWindow的回调,因此最后dispatchKeyEvent就会到Activity的dispatchKeyEvent函数中


 
 
  1. public boolean dispatchKeyEvent(KeyEvent event) {
  2. final int keyCode = event.getKeyCode();
  3. final int action = event.getAction();
  4. final boolean isDown = action == KeyEvent.ACTION_DOWN;
  5. if (isDown && (event.getRepeatCount() == 0)) {
  6. // First handle chording of panel key: if a panel key is held
  7. // but not released, try to execute a shortcut in it.
  8. if ((mPanelChordingKey > 0) && (mPanelChordingKey != keyCode)) {
  9. boolean handled = dispatchKeyShortcutEvent(event);
  10. if (handled) {
  11. return true;
  12. }
  13. }
  14. // If a panel is open, perform a shortcut on it without the
  15. // chorded panel key
  16. if ((mPreparedPanel != null) && mPreparedPanel.isOpen) {
  17. if (performPanelShortcut(mPreparedPanel, keyCode, event, 0)) {
  18. return true;
  19. }
  20. }
  21. }
  22. if (!isDestroyed()) {
  23. final Callback cb = getCallback();
  24. final boolean handled = cb != null && mFeatureId < 0 ? cb.dispatchKeyEvent(event)
  25. : super.dispatchKeyEvent(event);
  26. if (handled) {
  27. return true;
  28. }
  29. }
  30. return isDown ? PhoneWindow. this.onKeyDown(mFeatureId, event.getKeyCode(), event)
  31. : PhoneWindow. this.onKeyUp(mFeatureId, event.getKeyCode(), event);
  32. }
Activity的attach中调用了PhoneWindow的setCallBack函数将回调设置成Activity,最后dispatchKeyEvent就会调用到Activity的dispatchKeyEvent中。

 
 
  1. final void attach(Context context, ActivityThread aThread,
  2. Instrumentation instr, IBinder token, int ident,
  3. Application application, Intent intent, ActivityInfo info,
  4. CharSequence title, Activity parent, String id,
  5. NonConfigurationInstances lastNonConfigurationInstances,
  6. Configuration config, String referrer, IVoiceInteractor voiceInteractor) {
  7. attachBaseContext(context);
  8. mFragments.attachHost(null /*parent*/);
  9. mWindow = new PhoneWindow( this);
  10. mWindow.setCallback( this);


3. ViewRootImpl

在ActivityThread的handleResumeActivity函数中会调用WindowManager的addView函数,而这个WindowManager就是Activity的mWindowManager。


 
 
  1. if (r.window == null && !a.mFinished && willBeVisible) {
  2. r.window = r.activity.getWindow();
  3. View decor = r.window.getDecorView();
  4. decor.setVisibility(View.INVISIBLE);
  5. ViewManager wm = a.getWindowManager();
  6. WindowManager.LayoutParams l = r.window.getAttributes();
  7. a.mDecor = decor;
  8. l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
  9. l.softInputMode |= forwardBit;
  10. if (a.mVisibleFromClient) {
  11. a.mWindowAdded = true;
  12. wm.addView(decor, l);
  13. }
  14. // If the window has already been added, but during resume
  15. // we started another activity, then don't yet make the
  16. // window visible.
  17. }

这个我们在Activity一节中分析过了,因此最后addView函数在WindowManagerImpl中实现,我们再来看看WindowManagerImpl的addView函数。


 
 
  1. @ Override
  2. public void addView (@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
  3. applyDefaultToken(params);
  4. mGlobal.addView(view, params, mDisplay, mParentWindow);
  5. }

而在WindowManagerImpl最后是调用了WindowManagerGlobal的addView函数, 在这个函数中我们新建ViewRootImpl对象,然后调用了ViewRootImpl的setView函数,这里的view就是Activity的Window对象的DecorView。并且在这个函数中,把view root param这3个保存在mViews mRoots mParams这3个成员变量中了。


 
 
  1. ......
  2. ViewRootImpl root;
  3. View panelParentView = null;
  4. synchronized (mLock) {
  5. // Start watching for system property changes.
  6. if (mSystemPropertyUpdater == null) {
  7. mSystemPropertyUpdater = new Runnable() {
  8. @Override public void run() {
  9. synchronized (mLock) {
  10. for ( int i = mRoots.size() - 1; i >= 0; --i) {
  11. mRoots.get(i).loadSystemProperties();
  12. }
  13. }
  14. }
  15. };
  16. SystemProperties.addChangeCallback(mSystemPropertyUpdater);
  17. }
  18. int index = findViewLocked(view, false);
  19. if (index >= 0) {
  20. if (mDyingViews.contains(view)) {
  21. // Don't wait for MSG_DIE to make it's way through root's queue.
  22. mRoots.get(index).doDie();
  23. } else {
  24. throw new IllegalStateException( "View " + view
  25. + " has already been added to the window manager.");
  26. }
  27. // The previous removeView() had not completed executing. Now it has.
  28. }
  29. // If this is a panel window, then find the window it is being
  30. // attached to for future reference.
  31. if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
  32. wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
  33. final int count = mViews.size();
  34. for ( int i = 0; i < count; i++) {
  35. if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
  36. panelParentView = mViews.get(i);
  37. }
  38. }
  39. }
  40. root = new ViewRootImpl(view.getContext(), display); //创建ViewRootImpl
  41. view.setLayoutParams(wparams);
  42. mViews.add(view);
  43. mRoots.add(root); //保存各个成员变量
  44. mParams.add(wparams);
  45. }
  46. // do this last because it fires off messages to start doing things
  47. try {
  48. root.setView(view, wparams, panelParentView); //调用ViewRootImpl的setView函数
  49. } catch (RuntimeException e) {
  50. // BadTokenException or InvalidDisplayException, clean up.
  51. synchronized (mLock) {
  52. final int index = findViewLocked(view, false);
  53. if (index >= 0) {
  54. removeViewLocked(index, true);
  55. }
  56. }
  57. throw e;
  58. }
  59. }

在ViewRootImpl的setView函数中,先调用了requestLayout来绘制view,然后调用了mWindowSession的addToDisplay函数和WMS通信


 
 
  1. ......
  2. requestLayout(); //绘制View
  3. if ((mWindowAttributes.inputFeatures
  4. & WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
  5. mInputChannel = new InputChannel();
  6. }
  7. try {
  8. mOrigWindowType = mWindowAttributes.type;
  9. mAttachInfo.mRecomputeGlobalAttributes = true;
  10. collectViewAttributes();
  11. res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
  12. getHostVisibility(), mDisplay.getDisplayId(),
  13. mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
  14. mAttachInfo.mOutsets, mInputChannel);
  15. }

我们先来看下mWindowSession,就是通过WMS获取到WindowSession的Binder,并且自己设置了一个回调在WMS中


 
 
  1. public static IWindowSession getWindowSession() {
  2. synchronized (WindowManagerGlobal.class) {
  3. if (sWindowSession == null) {
  4. try {
  5. InputMethodManager imm = InputMethodManager.getInstance();
  6. IWindowManager windowManager = getWindowManagerService();
  7. sWindowSession = windowManager.openSession(
  8. new IWindowSessionCallback.Stub() {
  9. @Override
  10. public void onAnimatorScaleChanged( float scale) {
  11. ValueAnimator.setDurationScale(scale);
  12. }
  13. },
  14. imm.getClient(), imm.getInputContext());
  15. } catch (RemoteException e) {
  16. Log.e(TAG, "Failed to open window session", e);
  17. }
  18. }
  19. return sWindowSession;
  20. }
  21. }

最后BInder调用到Session的addToDisplay,也会调用WMS的addWindow函数,这个我们稍后在WMS中分析。


 
 
  1. public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs,
  2. int viewVisibility, int displayId, Rect outContentInsets, Rect outStableInsets,
  3. Rect outOutsets, InputChannel outInputChannel) {
  4. return mService.addWindow( this, window, seq, attrs, viewVisibility, displayId,
  5. outContentInsets, outStableInsets, outOutsets, outInputChannel);
  6. }

我们再来看ViewRootImpl中调用mWindowSession.addToDisplay函数传入的一个参数mWindow,

        mWindow = new W(this);
 
 

W是ViewRootImpl的一个嵌入类,也是一个Binder服务。通过mWindowSession.addToDisplay函数传入WMS,用来在WMS中通过Binder回调。


 
 
  1. static class W extends IWindow.Stub {
  2. private final WeakReference<ViewRootImpl> mViewAncestor;
  3. private final IWindowSession mWindowSession;
  4. W(ViewRootImpl viewAncestor) {
  5. mViewAncestor = new WeakReference<ViewRootImpl>(viewAncestor);
  6. mWindowSession = viewAncestor.mWindowSession;
  7. }
  8. @ Override
  9. public void resized (Rect frame, Rect overscanInsets, Rect contentInsets,
  10. Rect visibleInsets, Rect stableInsets, Rect outsets, boolean reportDraw,
  11. Configuration newConfig) {
  12. final ViewRootImpl viewAncestor = mViewAncestor.get();
  13. if (viewAncestor != null) {
  14. viewAncestor.dispatchResized(frame, overscanInsets, contentInsets,
  15. visibleInsets, stableInsets, outsets, reportDraw, newConfig);
  16. }
  17. }
  18. ......


4. WMS

WMS指的是WindowManagerService,下面我们来看下WMS


 
 
  1. public class WindowManagerService extends IWindowManager.Stub
  2. implements Watchdog. Monitor, WindowManagerPolicy. WindowManagerFuncs {
  3. ......
  4. final WindowManagerPolicy mPolicy = new PhoneWindowManager();
  5. ......
  6. /**
  7. * All currently active sessions with clients.
  8. */
  9. final ArraySet<Session> mSessions = new ArraySet<>();
  10. /**
  11. * Mapping from an IWindow IBinder to the server's Window object.
  12. * This is also used as the lock for all of our state.
  13. * NOTE: Never call into methods that lock ActivityManagerService while holding this object.
  14. */
  15. final HashMap<IBinder, WindowState> mWindowMap = new HashMap<>();
  16. /**
  17. * Mapping from a token IBinder to a WindowToken object.
  18. */
  19. final HashMap<IBinder, WindowToken> mTokenMap = new HashMap<>();

mPolicy是WMS所执行的窗口管理策略类,现在android只有PhoneWindowManager一个策略类。

mSessions存储的是Session服务类,每个应用都在WMS中有一个对应的Session对象保存在mSessions中。

mTokenMap保存的是所有窗口的WindowToken对象。

mWindowMap保存的是所有窗口的WindowState对象。

4.1 WindowToken对象

在WMS中有两种常见的Token,WindowToken和AppWindowToken。先来看WindowToken的定义:


 
 
  1. class WindowToken {
  2. // The window manager!
  3. final WindowManagerService service;
  4. // The actual token.
  5. final IBinder token;
  6. // The type of window this token is for, as per WindowManager.LayoutParams.
  7. final int windowType;
  8. // Set if this token was explicitly added by a client, so should
  9. // not be removed when all windows are removed.
  10. final boolean explicit;
  11. // For printing.
  12. String stringName;
  13. // If this is an AppWindowToken, this is non-null.
  14. AppWindowToken appWindowToken;
  15. // All of the windows associated with this token.
  16. final WindowList windows = new WindowList();

WindowToken的成员变量token是IBinder对象,具有系统唯一性。因此,向WMS的mWindowMap或者mTokenMap插入对象时都是使用token值作为索引。

WindowToken用来表示一组窗口对象,windowType表示窗口类型。

explicit为false表示这个Token是在WMS中创建的,true表示在其他模块通过调用addAppToken或者addWindowToken方法显示创建的。

windows类型是WindowList,包含一个WindowState对象的列表,所有拥有相同WindowToken的窗口都在这个列表中。一般而言窗口和依附于它的子窗口拥有相同的WindowToken对象。

APPWindowToken从WindowToken类派生,是一种比较特殊的WindowToken,代表应用窗口,主要是Activity中创建的顶层窗口。一个WindowToken对象的成员变量APPWindowToken如果为NULL,那么它就不是APPWindowToken,否则就是APPWindowToken对象。

APPWindowToken中增加了几个特有的成员变量,如下:


 
 
  1. class AppWindowToken extends WindowToken {
  2. // Non-null only for application tokens.
  3. final IApplicationToken appToken;
  4. // All of the windows and child windows that are included in this
  5. // application token. Note this list is NOT sorted!
  6. final WindowList allAppWindows = new WindowList();
  7. final AppWindowAnimator mAppAnimator;
  8. final WindowAnimator mAnimator;

其中appToken用来表示应用的Token,它是在AMS中创建的,代表一个应用。

allAppWindows也是一个WindowList,保存了所有相同APPWindowToken的应用窗口及其子窗口。

在WMS中定义了addAppToken用来向WMS添加一个APPWindowToken类型的Token。

4.2 窗口类型

在WMS中定义了3中类型的窗口:应用窗口,子窗口,系统窗口。

应用窗口:

应用窗口是Activity使用的全屏窗口。

1.TYPE_BASE_APPLICATION 应用中所有窗口都位于其上层

2.TYPE_APPLICATION Activity的顶层窗口,也是最常见的窗口

3.TYPE_APPLICATION_STARTING 应用启动时系统创建的窗口,显示一些信息,直到应用窗口显示出来

子窗口:

子窗口是依附于应用窗口或者系统窗口的窗口类型,它们一般随所依附的窗口一起出现或者切到后台。

TYPE_APPLICATION_PANEL:应用面板窗口,显示于所依附窗口上层

TYPE_APPLICATION_MEDIA:应用Media窗口,通常独立的Surface,用于播放Video或显示照相机预览画面。显示于所依附窗口的下层

TYPE_APPLICATION_SUB_PANEL:子面板窗口,显示在所有子窗口的上层

TYPE_APPLICATION_ATTACHED_DIALOG:和面板窗口类似的窗口,但是layout的方式和Activity的顶层窗口类似

TYPE_APPLICATION_MEDIA_OVERLAY:一种显示在Media窗口和所依附窗口之间的半透明窗口,用于显示Video字幕或者相机操作提示界面

系统窗口:

系统窗口大都用于系统生成的UI中,比较独立,种类较多。

状态条,搜索,通话,输入法等。


 
 
  1. import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW; //第一个应用窗口
  2. import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW; //第一个子窗口
  3. import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
  4. import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
  5. import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
  6. import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
  7. import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
  8. import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
  9. import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW; //最后一个应用窗口
  10. import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW; //最后一个子窗口
  11. import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
  12. import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
  13. import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION;
  14. import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
  15. import static android.view.WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
  16. import static android.view.WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
  17. import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION; //Activity顶层窗口,应用中最常见窗口
  18. import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING; //应用启动时由系统创建的窗口,显示一些信息,直到应用创建的窗口显示出来
  19. import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION; //基础应用窗口,应用中所有窗口都位于其上层。只能又系统创建
  20. import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
  21. import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
  22. import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
  23. import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
  24. import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
  25. import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_SCRIM;
  26. import static android.view.WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION;
  27. import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR; //状态条窗口
  28. import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG; //显示状态条弹出对话框
  29. import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR; //系统内部错误窗口
  30. import static android.view.WindowManager.LayoutParams.TYPE_VOICE_INTERACTION;
  31. import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
  32. import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
  33. import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;

还有一些在WindowManager中的LayoutParams中


 
 
  1. public static final int TYPE_APPLICATION_PANEL = FIRST_SUB_WINDOW;
  2. /**
  3. * Window type: window for showing media (such as video). These windows
  4. * are displayed behind their attached window.
  5. */
  6. public static final int TYPE_APPLICATION_MEDIA = FIRST_SUB_WINDOW + 1;
  7. /**
  8. * Window type: a sub-panel on top of an application window. These
  9. * windows are displayed on top their attached window and any
  10. * {@link #TYPE_APPLICATION_PANEL} panels.
  11. */
  12. public static final int TYPE_APPLICATION_SUB_PANEL = FIRST_SUB_WINDOW + 2;
  13. /** Window type: like {@link #TYPE_APPLICATION_PANEL}, but layout
  14. * of the window happens as that of a top-level window, <em>not</em>
  15. * as a child of its container.
  16. */
  17. public static final int TYPE_APPLICATION_ATTACHED_DIALOG = FIRST_SUB_WINDOW + 3;
  18. /**
  19. * Window type: window for showing overlays on top of media windows.
  20. * These windows are displayed between TYPE_APPLICATION_MEDIA and the
  21. * application window. They should be translucent to be useful. This
  22. * is a big ugly hack so:
  23. * @hide
  24. */
  25. public static final int TYPE_APPLICATION_MEDIA_OVERLAY = FIRST_SUB_WINDOW + 4;


4.3 新建窗口的过程

每次新建Activity会增加窗口,弹出菜单同样会在WMS增加窗口。而最终都是通过Session的addToDisplay向WMS增加一个窗口。WMS最终会调用addWindow来实现这个功能。

我们来回顾下之前Activity是如何调用增加一个窗口的。

新建Activity的时候先调用handleLaunchActivity,然后在这个函数中会调用handleResumeActivity函数然后在这个函数中会调用WindowManagerGlobal的addView函数,而在WindowManagerGlobal的addView函数中会新加ViewRootImpl对象,然后调用其setView函数,在setView中会调用mWindowSession.addToDisplay函数,最后到WMS的addWindow函数。


4.3.1 参数检查

 
 
  1. public int addWindow(Session session, IWindow client, int seq,
  2. WindowManager.LayoutParams attrs, int viewVisibility, int displayId,
  3. Rect outContentInsets, Rect outStableInsets, Rect outOutsets,
  4. InputChannel outInputChannel) {
  5. ......
  6. //检查mWindowMap是否有该窗口,如果有重复了,直接退出
  7. if (mWindowMap.containsKey(client.asBinder())) {
  8. Slog.w(TAG, "Window " + client + " is already added");
  9. return WindowManagerGlobal.ADD_DUPLICATE_ADD;
  10. }
  11. //子窗口类型,父窗口类型必须存在且不能是子窗口类型,否则退出
  12. if (type >= FIRST_SUB_WINDOW && type <= LAST_SUB_WINDOW) {
  13. attachedWindow = windowForClientLocked(null, attrs.token, false);
  14. if (attachedWindow == null) {
  15. Slog.w(TAG, "Attempted to add window with token that is not a window: "
  16. + attrs.token + ". Aborting.");
  17. return WindowManagerGlobal.ADD_BAD_SUBWINDOW_TOKEN;
  18. }
  19. if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
  20. && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
  21. Slog.w(TAG, "Attempted to add window with token that is a sub-window: "
  22. + attrs.token + ". Aborting.");
  23. return WindowManagerGlobal.ADD_BAD_SUBWINDOW_TOKEN;
  24. }
  25. }
  26. //私有窗口类型,显示设备不是私有类型,退出
  27. if (type == TYPE_PRIVATE_PRESENTATION && !displayContent.isPrivate()) {
  28. Slog.w(TAG, "Attempted to add private presentation window to a non-private display. Aborting.");
  29. return WindowManagerGlobal.ADD_PERMISSION_DENIED;
  30. }
  31. boolean addToken = false;
  32. WindowToken token = mTokenMap.get(attrs.token);
  33. //如果mTokenMap中没有,但是窗口类型是应用窗口,输入法窗口,壁纸,Dream则退出
  34. if (token == null) {
  35. if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {
  36. Slog.w(TAG, "Attempted to add application window with unknown token "
  37. + attrs.token + ". Aborting.");
  38. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  39. }
  40. if (type == TYPE_INPUT_METHOD) {
  41. Slog.w(TAG, "Attempted to add input method window with unknown token "
  42. + attrs.token + ". Aborting.");
  43. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  44. }
  45. if (type == TYPE_VOICE_INTERACTION) {
  46. Slog.w(TAG, "Attempted to add voice interaction window with unknown token "
  47. + attrs.token + ". Aborting.");
  48. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  49. }
  50. if (type == TYPE_WALLPAPER) {
  51. Slog.w(TAG, "Attempted to add wallpaper window with unknown token "
  52. + attrs.token + ". Aborting.");
  53. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  54. }
  55. if (type == TYPE_DREAM) {
  56. Slog.w(TAG, "Attempted to add Dream window with unknown token "
  57. + attrs.token + ". Aborting.");
  58. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  59. }
  60. if (type == TYPE_ACCESSIBILITY_OVERLAY) {
  61. Slog.w(TAG, "Attempted to add Accessibility overlay window with unknown token "
  62. + attrs.token + ". Aborting.");
  63. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  64. }
  65. //其他类型的窗口新建token
  66. token = new WindowToken( this, attrs.token, -1, false);
  67. addToken = true;
  68. } else if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {
  69. //应用类型窗口
  70. AppWindowToken atoken = token.appWindowToken;
  71. if (atoken == null) { //应用类型窗口必须有appWindowToken
  72. Slog.w(TAG, "Attempted to add window with non-application token "
  73. + token + ". Aborting.");
  74. return WindowManagerGlobal.ADD_NOT_APP_TOKEN;
  75. } else if (atoken.removed) {
  76. Slog.w(TAG, "Attempted to add window with exiting application token "
  77. + token + ". Aborting.");
  78. return WindowManagerGlobal.ADD_APP_EXITING;
  79. }
  80. if (type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
  81. //还在启动中,不能添加窗口
  82. // No need for this guy!
  83. if (localLOGV) Slog.v(
  84. TAG, "**** NO NEED TO START: " + attrs.getTitle());
  85. return WindowManagerGlobal.ADD_STARTING_NOT_NEEDED;
  86. }
  87. } else if (type == TYPE_INPUT_METHOD) { //输入法
  88. if (token.windowType != TYPE_INPUT_METHOD) {
  89. Slog.w(TAG, "Attempted to add input method window with bad token "
  90. + attrs.token + ". Aborting.");
  91. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  92. }
  93. } else if (type == TYPE_VOICE_INTERACTION) { //
  94. if (token.windowType != TYPE_VOICE_INTERACTION) {
  95. Slog.w(TAG, "Attempted to add voice interaction window with bad token "
  96. + attrs.token + ". Aborting.");
  97. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  98. }
  99. } else if (type == TYPE_WALLPAPER) { //壁纸
  100. if (token.windowType != TYPE_WALLPAPER) {
  101. Slog.w(TAG, "Attempted to add wallpaper window with bad token "
  102. + attrs.token + ". Aborting.");
  103. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  104. }
  105. } else if (type == TYPE_DREAM) { //Dream
  106. if (token.windowType != TYPE_DREAM) {
  107. Slog.w(TAG, "Attempted to add Dream window with bad token "
  108. + attrs.token + ". Aborting.");
  109. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  110. }
  111. } else if (type == TYPE_ACCESSIBILITY_OVERLAY) {
  112. if (token.windowType != TYPE_ACCESSIBILITY_OVERLAY) {
  113. Slog.w(TAG, "Attempted to add Accessibility overlay window with bad token "
  114. + attrs.token + ". Aborting.");
  115. return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
  116. }
  117. } else if (token.appWindowToken != null) {
  118. Slog.w(TAG, "Non-null appWindowToken for system window of type=" + type);
  119. // It is not valid to use an app token with other system types; we will
  120. // instead make a new token for it (as if null had been passed in for the token).
  121. attrs.token = null;
  122. token = new WindowToken( this, null, -1, false);
  123. addToken = true;
  124. }

这段代码主要是进行参数检查,大部分参数从客户进程传过来的。为了保证数据的一致性,重点检查窗口的WindowToken对象中的窗口类型和之前在WMS的WindowToken的windowType是否一致。这种检查主要针对应用窗口、输入法、壁纸、dream、TYPE_VOICE_INTERACTION、TYPE_ACCESSIBILITY_OVERLAY,因为这几种窗口的WindowToken对象在addWindow前已经创建好并加入WMS的mTokenMap中,这里需要检查是否一致。

一些特殊的窗口,入输入法窗口,必须由InputManagerService允许后才能创建。这些服务会预先调用WMS的addWindowToken方法插入WindowToken到WMS的mTokenMap中。如果应用不通过服务获取相应的WindowToken将无法创建这些特殊窗口,方便控制。

对于其他类型的窗口,addWindow方法会为它们创建WindowToken对象。


4.3.2 创建窗口对象

 
 
  1. //创建WindowState对象
  2. WindowState win = new WindowState( this, session, client, token,
  3. attachedWindow, appOp[ 0], seq, attrs, viewVisibility, displayContent);
  4. if (win.mDeathRecipient == null) {
  5. // Client has apparently died, so there is no reason to
  6. // continue.
  7. Slog.w(TAG, "Adding window client " + client.asBinder()
  8. + " that is dead, aborting.");
  9. return WindowManagerGlobal.ADD_APP_EXITING;
  10. }
  11. if (win.getDisplayContent() == null) {
  12. Slog.w(TAG, "Adding window to Display that has been removed.");
  13. return WindowManagerGlobal.ADD_INVALID_DISPLAY;
  14. }
  15. mPolicy.adjustWindowParamsLw(win.mAttrs);
  16. //检查和设置查看是否能够让其他用户看见
  17. win.setShowToOwnerOnlyLocked(mPolicy.checkShowToOwnerOnly(attrs));
  18. res = mPolicy.prepareAddWindowLw(win, attrs);
  19. if (res != WindowManagerGlobal.ADD_OKAY) {
  20. return res;
  21. }
  22. //为按键创建和应用的通信通道
  23. if (outInputChannel != null && (attrs.inputFeatures
  24. & WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
  25. String name = win.makeInputChannelName();
  26. InputChannel[] inputChannels = InputChannel.openInputChannelPair(name);
  27. win.setInputChannel(inputChannels[ 0]);
  28. inputChannels[ 1].transferTo(outInputChannel);
  29. mInputManager.registerInputChannel(win.mInputChannel, win.mInputWindowHandle);
  30. }
  31. // From now on, no exceptions or errors allowed!
  32. res = WindowManagerGlobal.ADD_OKAY;
  33. origId = Binder.clearCallingIdentity();
  34. if (addToken) { //如果是新的Token,加入mTokenMap
  35. mTokenMap.put(attrs.token, token);
  36. }
  37. win.attach();
  38. mWindowMap.put(client.asBinder(), win); //加入WindowState对象列表中
  39. ......

这段代码主要是保存WindowState对象,放入mWindowMap中。

mPolicy的adjustWindowParamsLw方法检查窗口类型是否是TYPE_SYSTEM_OVERLAY或者TYPE_SERCURE_SYSTEM_OVERLAY,如果是将窗口标记上FLAG_NOT_FOCUSABLE FLAG_NOT_TOUCHABLE FLAG_WATCH_OUTSIDE_TOUCH.这些窗口不能获取焦点。


4.3.3 将窗口加入Display列表

 
 
  1. boolean imMayMove = true;
  2. if (type == TYPE_INPUT_METHOD) { //如果窗口类是输入法窗口
  3. win.mGivenInsetsPending = true;
  4. mInputMethodWindow = win;
  5. addInputMethodWindowToListLocked(win); //插入输入法窗口到应用窗口上层
  6. imMayMove = false;
  7. } else if (type == TYPE_INPUT_METHOD_DIALOG) { //如果窗口是输入法对话框
  8. mInputMethodDialogs.add(win);
  9. addWindowToListInOrderLocked(win, true); //插入到正常位置
  10. moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked( true)); //调整对话框位置
  11. imMayMove = false;
  12. } else {
  13. addWindowToListInOrderLocked(win, true);
  14. if (type == TYPE_WALLPAPER) {
  15. mLastWallpaperTimeoutTime = 0;
  16. displayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
  17. } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
  18. displayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
  19. } else if (mWallpaperTarget != null
  20. && mWallpaperTarget.mLayer >= win.mBaseLayer) {
  21. // If there is currently a wallpaper being shown, and
  22. // the base layer of the new window is below the current
  23. // layer of the target window, then adjust the wallpaper.
  24. // This is to avoid a new window being placed between the
  25. // wallpaper and its target.
  26. displayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
  27. }
  28. }

DisplayContent类的mWindows列表按Z序保存了每个窗口,这段代码就是在根据窗口类型把窗口加入到DisplayContent合适位置。

addInputMethodWindowToListLocked方法作用就是一个输入法窗口放子啊需要显示输入法窗口的上面。

addWindowToListInOrderLocked将一个窗口插入到窗口堆栈的当前位置。


4.3.4 调整窗口的次序


 
 
  1. winAnimator.mEnterAnimationPending = true;
  2. winAnimator.mEnteringAnimation = true;
  3. if (displayContent.isDefaultDisplay) {
  4. mPolicy.getInsetHintLw(win.mAttrs, mRotation, outContentInsets, outStableInsets, //计算窗口大小
  5. outOutsets);
  6. } else {
  7. outContentInsets.setEmpty();
  8. outStableInsets.setEmpty();
  9. }
  10. if (mInTouchMode) {
  11. res |= WindowManagerGlobal.ADD_FLAG_IN_TOUCH_MODE;
  12. }
  13. if (win.mAppToken == null || !win.mAppToken.clientHidden) {
  14. res |= WindowManagerGlobal.ADD_FLAG_APP_VISIBLE;
  15. }
  16. mInputMonitor.setUpdateInputWindowsNeededLw();
  17. boolean focusChanged = false;
  18. if (win.canReceiveKeys()) { //如果窗口能接受输入,计算是否引起焦点变化
  19. focusChanged = updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS,
  20. false /*updateInputWindows*/);
  21. if (focusChanged) {
  22. imMayMove = false;
  23. }
  24. }
  25. if (imMayMove) {
  26. moveInputMethodWindowsIfNeededLocked( false);
  27. }
  28. assignLayersLocked(displayContent.getWindowList());
  29. // Don't do layout here, the window must call
  30. // relayout to be displayed, so we'll do it there.
  31. if (focusChanged) {
  32. mInputMonitor.setInputFocusLw(mCurrentFocus, false /*updateInputWindows*/);
  33. }
  34. mInputMonitor.updateInputWindowsLw( false /*force*/);
  35. if (localLOGV || DEBUG_ADD_REMOVE) Slog.v(TAG, "addWindow: New client "
  36. + client.asBinder() + ": window=" + win + " Callers=" + Debug.getCallers( 5));
  37. if (win.isVisibleOrAdding() && updateOrientationFromAppTokensLocked( false)) {
  38. reportNewConfig = true; //如果窗口的配置发生变化
  39. }
  40. }
  41. if (reportNewConfig) {
  42. sendNewConfiguration(); //发生新的配置
  43. }
  44. Binder.restoreCallingIdentity(origId);
  45. return res;

后续还有确定窗口Z轴位置,窗口尺寸,窗口动画后续接触到再分析吧。




猜你喜欢

转载自blog.csdn.net/jiangnan2222/article/details/82555524