先上一張uml關系圖,自己制作的哦
- WindowManager是Android中一個重要的服務(Service )。WindowManager Service 是全局的朗涩,是唯一的尸诽。它將用戶的操作甥材,翻譯成為指令,發(fā)送給呈現(xiàn)在界面上的各個Window性含。Activity會將頂級的控件注冊到 Window Manager 中.
WindowManager 繼承自ViewManager,ViewManager是一個接口,代碼如下
public interface ViewManager{
public void addView(View view, ViewGroup.LayoutParams params);
public void updateViewLayout(View view, ViewGroup.LayoutParams params);
public void removeView(View view);
}
- windowManager 里一個重要的內部類就是 LayoutParams , LayoutParams 繼承 ViewGroup.LayoutParams
實現(xiàn)了 Parcelable接口
layoutParams 里邊定義了window的層級關系.代碼如下
public static final int TYPE_BASE_APPLICATION = 1;
public static final int TYPE_APPLICATION = 2;
public static final int LAST_APPLICATION_WINDOW = 99;
public static final int LAST_SUB_WINDOW = 1999;
public static final int FIRST_SYSTEM_WINDOW = 2000;
其中.應用Window的層級范圍是1-99,子Window的層級是1000-1999,系統(tǒng)Window的層級是2000-2999.想讓window位于所有window的最頂層,采用較大的層級就可以.使用系統(tǒng)層級的window需要在權限中聲名對應的window權限如
<user-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
3.window 是一個抽象概念.每個window都對應著一個View和ViewRootImpl,Window和View通過ViewRootImpl來建立聯(lián)系.下邊分別講解
-
window的添加過程
window的add,remove,update過程都有WindowManager來實現(xiàn),WindowManager是一個接口,具體實現(xiàn)類是WindowManagerImpl類.WindowManagerImpl 代碼 private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance(); @Override public void addView(View view, ViewGroup.LayoutParams params) { mGlobal.addView(view, params, mDisplay, mParentWindow); } @Override public void updateViewLayout(View view, ViewGroup.LayoutParams params) { mGlobal.updateViewLayout(view, params); } @Override public void removeView(View view) { mGlobal.removeView(view, false); } @Override public void removeViewImmediate(View view) { mGlobal.removeView(view, true); }
1.WindowManagerImpl 通過 WindowManagerGlobal 來對View進行操作. WindowManagerGlobal通過單例模式提供他自己的對象.
//WindowManagerGlobal 代碼
public static WindowManagerGlobal getInstance() {
synchronized (WindowManagerGlobal.class) {
if (sDefaultWindowManager == null) {
sDefaultWindowManager = new WindowManagerGlobal();
}
return sDefaultWindowManager;
}
}
2.接下來講解 WindowManagerGlobal,他里邊有幾個重要參數(shù)先列出來
// WindowManagerGlobal 代碼
private static IWindowManager sWindowManagerService;
private static IWindowSession sWindowSession; //前兩個先不看
private final Object mLock = new Object(); //線程的鎖
//所有Window對應的view的集合
private final ArrayList<View> mViews = new ArrayList<View>();
//所有window對應的ViewRootImpl 的集合
private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
//所有Window對應的布局參數(shù)集合
private final ArrayList<WindowManager.LayoutParams> mParams =
new ArrayList<WindowManager.LayoutParams>();
//所有調用removeView方法但刪除操作還沒完成的View集合
private final ArraySet<View> mDyingViews = new ArraySet<View>();
3.看WindowManagerGolbal的addView方法
//WindowManagerGolbal 代碼
public void addView(View view, ViewGroup.LayoutParams params,
Display display, Window parentWindow) {
// ------- 1.先檢查參數(shù)是否合法,
if (view == null) {
throw new IllegalArgumentException("view must not be null");
}
if (display == null) {
throw new IllegalArgumentException("display must not be null");
}
if (!(params instanceof WindowManager.LayoutParams)) {
throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
}
final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params;
if (parentWindow != null) { //調整parentWindow的param參數(shù)
parentWindow.adjustLayoutParamsForSubWindow(wparams);
} else {
// If there's no parent and we're running on L or above (or in the
// system context), assume we want hardware acceleration.
final Context context = view.getContext();
if (context != null //開啟硬件加速
&& context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
}
}
ViewRootImpl root;
View panelParentView = null;
synchronized (mLock) {
// Start watching for system property changes.
if (mSystemPropertyUpdater == null) {
mSystemPropertyUpdater = new Runnable() {
@Override public void run() {
synchronized (mLock) {
for (int i = mRoots.size() - 1; i >= 0; --i) {
mRoots.get(i).loadSystemProperties();//ViewRootImpl 加載系統(tǒng)屬性
}
}
}
};
SystemProperties.addChangeCallback(mSystemPropertyUpdater);//監(jiān)聽系統(tǒng)屬性變化
}
int index = findViewLocked(view, false);//看這個view是否存在于mViews里
if (index >= 0) {
if (mDyingViews.contains(view)) { //如果已經(jīng)添加到mViews里,看看是否是在待刪的mDyingViews里,是的話直接刪除
,不是的話,就拋出異常.
// Don't wait for MSG_DIE to make it's way through root's queue.
mRoots.get(index).doDie();
} else {
throw new IllegalStateException("View " + view
+ " has already been added to the window manager.");
}
// The previous removeView() had not completed executing. Now it has.
}
// If this is a panel window, then find the window it is being
// attached to for future reference.
//如果是面板view,就找到他第一次attach的window,以備以后查詢.
if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
final int count = mViews.size();
for (int i = 0; i < count; i++) {
if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
panelParentView = mViews.get(i);
}
}
}
//------- 2.生成一個新的viewRootImpl,并且把 view. viewrootImpl, param 都保存起來.
root = new ViewRootImpl(view.getContext(), display);
view.setLayoutParams(wparams);
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
}
// do this last because it fires off messages to start doing things
try {
//------- 3.調用ViewRoomImpl來更新界面并完成Window的添加過程
root.setView(view, wparams, panelParentView);
} catch (RuntimeException e) {
// BadTokenException or InvalidDisplayException, clean up.
synchronized (mLock) {
final int index = findViewLocked(view, false);
if (index >= 0) {
removeViewLocked(index, true);
}
}
throw e;
}
}
-------4.在 ViewRootImpl的setview方法里主要是一些對LayoutParams的處理,也看不太懂.不過其中重要是調用了requestLayout();
進行重新繪制.然后調用 mWindowSession完成view的添加過程.
//ViewRoomImpl內部成員
final IWindowSession mWindowSession;
//VewRoomImpl 的 setView 方法
try {
mOrigWindowType = mWindowAttributes.type;
mAttachInfo.mRecomputeGlobalAttributes = true;
collectViewAttributes();
res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
getHostVisibility(), mDisplay.getDisplayId(),
mAttachInfo.mContentInsets, mInputChannel);
}
其實,mWindowSession的類型是IWindowSession,他是一個Binder對象,真正的實現(xiàn)類是Session.也就是Window的一次添加過程是一次IPC調用
//Session代碼
final WindowManagerService mService;
@Override
public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs,
int viewVisibility, int displayId, Rect outContentInsets,
InputChannel outInputChannel) {
return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId,
outContentInsets, outInputChannel);
}
這樣,Window的一次添加過程就由WindowManagerService去處理了,而WindowManagerService內部會為每一個應用并保留一個單獨的session
-
Window的刪除過程
從 WindowManager的實現(xiàn)類WindowManagerImpl 看起//windowManagerImpl代碼 private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance(); @Override public void removeView(View view) { mGlobal.removeView(view, false); }
接著看 WindowManagerGlobal
//WindowManagerGlobal 代碼 為了方便.把之前列出的WindowManagerGlobal 幾個成員寫下
private static IWindowManager sWindowManagerService;
private static IWindowSession sWindowSession; //前兩個先不看
private final Object mLock = new Object(); //線程的鎖
//所有Window對應的view的集合
private final ArrayList<View> mViews = new ArrayList<View>();
//所有window對應的ViewRootImpl 的集合
private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
//所有Window對應的布局參數(shù)集合
private final ArrayList<WindowManager.LayoutParams> mParams =
new ArrayList<WindowManager.LayoutParams>();
//所有調用removeView方法但刪除操作還沒完成的View集合
private final ArraySet<View> mDyingViews = new ArraySet<View>();
// WindowManagerGlobal 主要代碼
public void removeView(View view, boolean immediate) {
if (view == null) {
throw new IllegalArgumentException("view must not be null");
}
synchronized (mLock) {
int index = findViewLocked(view, true);//在 mViews集合里查找這個view的index.
View curView = mRoots.get(index).getView();//找到這個所以的ViewRootImpl對應的view
removeViewLocked(index, immediate); //這是主要的刪除辦法
if (curView == view) { //兩個view相同,代表數(shù)據(jù)是正確的.結束方法.否則就拋出異常
return;
}
throw new IllegalStateException("Calling with view " + view
+ " but the ViewAncestor is attached to " + curView);
}
}
// 接著看 removeViewLocked
//WindowManagerGlobal 方法 ,immediate 標識是否立即刪除.
private void removeViewLocked(int index, boolean immediate) {
ViewRootImpl root = mRoots.get(index);
View view = root.getView();
if (view != null) {
InputMethodManager imm = InputMethodManager.getInstance();
if (imm != null) {
imm.windowDismissed(mViews.get(index).getWindowToken()); //這是一個調用InputManagerService的ipc的過程
}
}
boolean deferred = root.die(immediate); //通過ViewRootImpl 完成view的刪除
if (view != null) {
view.assignParent(null);
if (deferred) { //如果是延時的remove view,則把view 添加到mDyingViews里
mDyingViews.add(view);
}
}
}
接下來追中到ViewRootImpl里的die方法
//ViewRootImpl代碼 immediate
boolean die(boolean immediate) {
// Make sure we do execute immediately if we are in the middle of a traversal or the damage
// done by dispatchDetachedFromWindow will cause havoc on return.
if (immediate && !mIsInTraversal) {
doDie(); //直接執(zhí)行
return false;
}
if (!mIsDrawing) {
destroyHardwareRenderer();
} else {
Log.e(TAG, "Attempting to destroy the window while drawing!\n" +
" window=" + this + ", title=" + mWindowAttributes.getTitle());
}
mHandler.sendEmptyMessage(MSG_DIE); //其實就是在handler在處理消息時會調用 dodie方法
return true;
}
//繼續(xù)看dodie方法 ViewRootImpl代碼
void doDie() {
checkThread(); //確保mThread=Thread.currentThread() //其實mThread初始化就是他
if (LOCAL_LOGV) Log.v(TAG, "DIE in " + this + " of " + mSurface);
synchronized (this) {
if (mRemoved) { //標記變量.標識view是否被刪除
return;
}
mRemoved = true;
if (mAdded) {
dispatchDetachedFromWindow();//這是主要的刪除方法
}
if (mAdded && !mFirst) {
destroyHardwareRenderer(); //銷毀硬件渲染器
if (mView != null) {
int viewVisibility = mView.getVisibility();
boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
if (mWindowAttributesChanged || viewVisibilityChanged) {
// If layout params have been changed, first give them
// to the window manager to make sure it has the correct
// animation info.
try {
if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
& WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0) {
mWindowSession.finishDrawing(mWindow); //用來更新window的layoutparams
}
} catch (RemoteException e) {
}
}
mSurface.release();
}
}
mAdded = false;
}
//這句代碼主要是通過本ViewRoomImpl在WindowManagerGlobal mRoots中的index.
//分別在mRoots,mParams,mDyingViews 把這個index對應的不同數(shù)據(jù)都刪除
WindowManagerGlobal.getInstance().doRemoveView(this);
}
最后 ViewRootImpl中注意做了以下的事
1.垃圾回收相關,清楚數(shù)據(jù),和消息,移除回調
2.通過Session的remove方法刪除Window,其實是通過IPC調用WindowManagerService的removeWindow方 法.
3.調用View的diapatchDetachedfromWindow 方法,內部會回調View的onDetachedFromWindow及onDetachedFromWindowInternal(),
到此.remove過程完畢