Activity和Window关联源码分析

2017-05-22  本文已影响40人  PhoenixGG

在Activity中,存在一个Window对象,现针对这个存在的window对象和activity之间的关系,进行分析:

在Activity的attach()方法中,进行成员变量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,
            Object lastNonConfigurationInstance,
            HashMap<String,Object> lastNonConfigurationChildInstances,
            Configuration config) {
        ......
        mWindow = PolicyManager.makeNewWindow(this);
        mWindow.setCallback(this);
        if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
            mWindow.setSoftInputMode(info.softInputMode);
        }
        ......
        mWindow.setWindowManager(null, mToken, mComponent.flattenToString());
        ......
        mWindowManager = mWindow.getWindowManager();
        ......
    }

其中mWindow对象调用PolicyManager的静态方法makeNewWindow(this)

public final class PolicyManager {
    private static final String POLICY_IMPL_CLASS_NAME =
        "com.android.internal.policy.impl.Policy";
    private static final IPolicy sPolicy;
    static {
        // Pull in the actual implementation of the policy at run-time
        try {
            Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME);
            sPolicy = (IPolicy)policyClass.newInstance();
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(
                    POLICY_IMPL_CLASS_NAME + " could not be loaded", ex);
        } catch (InstantiationException ex) {
            throw new RuntimeException(
                    POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(
                    POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);
        }
    }
    ......
    // The static methods to spawn new policy-specific objects
    public static Window makeNewWindow(Context context) {
        return sPolicy.makeNewWindow(context);
    }
    ......
}

在程序运行的时候,会调用静态代码块,生成一个IPolicy实例,实际上是通过类名反射出来的一个Policy对象,而PolicyManager.makeNewWindow返回的就是Policy的makeNewWindow,即是一个PhoneWindow。

public class Policy implements IPolicy {  
    ......  
    public PhoneWindow makeNewWindow(Context context) {  
        return new PhoneWindow(context);  
    }  
    ......  
}  

而PhoneWindow的创建,就是一个利用构造方法进行构造和初始化的过程:

public class PhoneWindow extends Window implements MenuBuilder.Callback {
    ......
    public PhoneWindow(Context context) {
        super(context);
        mLayoutInflater = LayoutInflater.from(context);
    }
    ......
}

在初始化之后,获取LayoutInflater对象,放在成员变量mLayoutInflater中,方便后面的mDecorView里面添加布局和子View。
在activity的attach()方法中,获取了mWindow对象之后,调用mWindow.setCallback()设置window的回调事件,而回调接口为在抽象类Window的内部:CallBack

public abstract class Window {
    ......
    public interface Callback {
        public boolean dispatchKeyEvent(KeyEvent event);
        public boolean dispatchTouchEvent(MotionEvent event);
        public boolean dispatchTrackballEvent(MotionEvent event);
        public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event);
        public View onCreatePanelView(int featureId);
        public boolean onCreatePanelMenu(int featureId, Menu menu);
        public boolean onPreparePanel(int featureId, View view, Menu menu);
        public boolean onMenuOpened(int featureId, Menu menu);
        public boolean onMenuItemSelected(int featureId, MenuItem item);
        public void onWindowAttributesChanged(WindowManager.LayoutParams attrs);
        public void onContentChanged();
        public void onWindowFocusChanged(boolean hasFocus);
        public void onAttachedToWindow();
        public void onDetachedFromWindow();
        public void onPanelClosed(int featureId, Menu menu);
        public boolean onSearchRequested();
    }
    ......
}

而Activity又实现了此接口,自此,当PhoneWindow接受到系统的一系列事件,可以通过回调方法,在activity中进行接受和处理。
接着往下看,设置回调方法之后是判断软键盘的输入法显示模式(mWindow.setSoftInputMode(info.softInputMode)),再之后是设置mWindow的windowManager,这个方法在Window类中

public void setWindowManager(WindowManager wm, IBinder appToken, String appName) {
        mAppToken = appToken;
        mAppName = appName;
        if (wm == null) {
            wm = WindowManagerImpl.getDefault();
        }
        mWindowManager = new LocalWindowManager(wm);
    }

因为外面传进来的WindowManager是null,会进if判断,调用windowManagerImpl的静态方法getDefault():

public class WindowManagerImpl implements WindowManager {
    ......
    public static WindowManagerImpl getDefault(){
        return mWindowManager;
    }
    ......
    private static WindowManagerImpl mWindowManager = new WindowManagerImpl();
}

将获取的windowManager对象,作为一个参数,传递至LocalWindowManager构造方法中,此时生成mWindowManager对象,LocalWindowManager对mDefaultDisplay进行了包裹,而这个mWindowManager可通过activity的getWindowManager()或者是getWindow().getWindowManager()获得。

     private class LocalWindowManager implements WindowManager {
        LocalWindowManager(WindowManager wm) {
            mWindowManager = wm;
            mDefaultDisplay = mContext.getResources().getDefaultDisplay(
                    mWindowManager.getDefaultDisplay());
        }
        ......
        public Display getDefaultDisplay() {
            return mDefaultDisplay;
        }
        private final WindowManager mWindowManager;
        private final Display mDefaultDisplay;
    }

至此,activity和window的关联源码告一段落,总结一下:

1、Activity内部的Window对象,实际是PhoneWindow的实例
2、Activity的Window实例化过程,是在attach()方法中进行的,分别进行window构建和初始化,包括设置回调(setCallBack)、设置软键盘输入模式(setSoftInputMode)、和设置窗口管理器(setWindowManager)
3、Activity的getWindowManager()方法返回的,实际上是一个LocalWindowManager。
4、对于Activity内部的视图view操作,发生在PhoneWindow内部,即Activity的方法调用,最终会调取PhoneWindow内部方法,包括findViewById,setContentView等等

上一篇下一篇

猜你喜欢

热点阅读