当前位置:网站首页>Window source code analysis (IV): window deletion mechanism
Window source code analysis (IV): window deletion mechanism
2022-07-28 09:37:00 【Yu Qirong】
notes : The source code analyzed in this article is based on API 25, Part of the content comes from 《Android Exploration of development Art 》.
Chapter one :《Window The source code parsing ( One ): And DecorView Those things 》
Second articles :《Window The source code parsing ( Two ):Window The mechanism of adding 》
Second articles :《Window The source code parsing ( 3、 ... and ):Window The update mechanism of 》
Header
This article will be Window The last part of the series , Let's mainly talk about Window Mechanism and principle of deletion .
In fact, compared with Window For the addition and update of , Deleting is also changing soup without changing medicine . through WindowSession and WindowManagerService To complete this step .
Window The deletion mechanism of
We delete Window Code for :
WindowManager.removeView
WindowManagerImpl
removeView(View view)
@Override
public void removeView(View view) {
mGlobal.removeView(view, false);
}WindowManager It's an interface , The concrete realization is WindowManagerImpl class . Needless to say ,WindowManagerImpl The interior must be WindowManagerGlobal stay “ Make trouble ” Slightly .
WindowManagerGlobal
removeView(View view, boolean immediate)
public void removeView(View view, boolean immediate) {
if (view == null) {
throw new IllegalArgumentException("view must not be null");
}
synchronized (mLock) {
// Get the present view The index of
int index = findViewLocked(view, true);
View curView = mRoots.get(index).getView();
// It mainly implements deletion view The operation of
removeViewLocked(index, immediate);
// If you want to delete view No viewrootimpl Medium view , Then an exception will be thrown
if (curView == view) {
return;
}
throw new IllegalStateException("Calling with view " + view
+ " but the ViewAncestor is attached to " + curView);
}
} stay removeView(View view, boolean immediate) Find the one you want to delete first View The index of . Then delete according to the index .
if immediate Parameter is passed in true , Then the synchronous deletion operation is performed ; Otherwise, it is asynchronous deletion . Most of them use asynchronous deletion , Avoid error , namely immediate by false;
In fact, the focus of this method is removeViewLocked(index, immediate) It's in .
removeViewLocked(int index, boolean immediate)
private void removeViewLocked(int index, boolean immediate) {
ViewRootImpl root = mRoots.get(index);
View view = root.getView();
// Turn off ime
if (view != null) {
InputMethodManager imm = InputMethodManager.getInstance();
if (imm != null) {
imm.windowDismissed(mViews.get(index).getWindowToken());
}
}
// call die Method , take immediate Pass in , That is, whether it is synchronously deleted
boolean deferred = root.die(immediate);
if (view != null) {
view.assignParent(null);
if (deferred) {
// Add to the collection to be removed immediately
mDyingViews.add(view);
}
}
} stay removeViewLocked(int index, boolean immediate) in , Called ViewRootImpl Of die Method . Most default ,immediate All for false .
And then view Add to mDyingViews in .mDyingViews All of them are about to be deleted View .
ViewRootImpl
die(boolean 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.
// In case of synchronous removal , Then execute immediately doDie
if (immediate && !mIsInTraversal) {
doDie();
return false;
}
if (!mIsDrawing) {
destroyHardwareRenderer();
} else {
Log.e(mTag, "Attempting to destroy the window while drawing!\n" +
" window=" + this + ", title=" + mWindowAttributes.getTitle());
}
// If you are asynchronous, use handler Send a messaage , Received message Later, it is also implemented doDie Method
mHandler.sendEmptyMessage(MSG_DIE);
return true;
} stay die(boolean immediate) In the method , Whether synchronous or asynchronous , It's all about execution doDie() Method . The difference is that synchronization is executed immediately , Asynchrony is the use of Handler Send a message , After receiving the message, execute .
doDie()
void doDie() {
// First check the thread , In the main thread
checkThread();
if (LOCAL_LOGV) Log.v(mTag, "DIE in " + this + " of " + mSurface);
synchronized (this) {
if (mRemoved) {
return;
}
mRemoved = true;
if (mAdded) {
// If yes, it has been added to Window Upper , Delete
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);
}
} catch (RemoteException e) {
}
}
mSurface.release();
}
}
// take mAdded Set to false
mAdded = false;
}
// From the corresponding mRoots mParams mDyingViews Remove the view References to
WindowManagerGlobal.getInstance().doRemoveView(this);
}doDie() There are two main points in the method :
- dispatchDetachedFromWindow() Is to delete window Methods ;
- WindowManagerGlobal.getInstance().doRemoveView(this) hold mRoot 、mParams and mDyingViews About the current Window All parameters of have been removed .
So we're going to , Still have to see dispatchDetachedFromWindow() Method .
void dispatchDetachedFromWindow() {
// Call it here view Of dispatchDetachedFromWindow Method
if (mView != null && mView.mAttachInfo != null) {
mAttachInfo.mTreeObserver.dispatchOnWindowAttachedChange(false);
mView.dispatchDetachedFromWindow();
}
// Auxiliary function related operations
mAccessibilityInteractionConnectionManager.ensureNoConnection();
mAccessibilityManager.removeAccessibilityStateChangeListener(
mAccessibilityInteractionConnectionManager);
mAccessibilityManager.removeHighTextContrastStateChangeListener(
mHighContrastTextManager);
removeSendWindowContentChangedCallback();
// Garbage collection work
destroyHardwareRenderer();
setAccessibilityFocus(null, null);
mView.assignParent(null);
mView = null;
mAttachInfo.mRootView = null;
mSurface.release();
if (mInputQueueCallback != null && mInputQueue != null) {
mInputQueueCallback.onInputQueueDestroyed(mInputQueue);
mInputQueue.dispose();
mInputQueueCallback = null;
mInputQueue = null;
}
if (mInputEventReceiver != null) {
mInputEventReceiver.dispose();
mInputEventReceiver = null;
}
// The key is coming. , call session To do it window Remove operation
try {
mWindowSession.remove(mWindow);
} catch (RemoteException e) {
}
// Dispose the input channel after removing the window so the Window Manager
// doesn't interpret the input channel being closed as an abnormal termination.
if (mInputChannel != null) {
mInputChannel.dispose();
mInputChannel = null;
}
mDisplayManager.unregisterDisplayListener(mDisplayListener);
// relieve view Operations such as drawing
unscheduleTraversals();
}At the beginning of the method , I'll call back first View Of dispatchDetachedFromWindow Method , This method represents View It's coming from Window It's deleted from . In this method , You can do some resource recycling .
Then I did some garbage collection , Such as clear data and information , Remove callback, etc .
Then we have to see mWindowSession.remove(mWindow) , This step is the real call Session To remove Window The operation of , yes IPC The process of . We went deep into the details .
Session
public void remove(IWindow window) {
mService.removeWindow(this, window);
} stay Session Called directly in WindowManagerService Of removeWindow(Session session, IWindow client) Method .
WindowManagerService
public void removeWindow(Session session, IWindow client) {
synchronized(mWindowMap) {
// obtain windowstate object
WindowState win = windowForClientLocked(session, client, false);
if (win == null) {
return;
}
// Remove window operation
removeWindowLocked(win);
}
}First get WindowState object , Call again removeWindowLocked To remove the WindowState . Specific removeWindowLocked We won't go deep into the code here , You can do your own research .
thus , Whole Window The removal mechanism is analyzed .
Footer
Finally, finally Window The relevant contents of have been reorganized , It also took nearly a month .
Before, some ambiguous points have also become clear , But there are still some places that have not been covered in depth . such as WindowManagerService Internal operations .
There is still a long way to go , I look forward to going deeper .
References
边栏推荐
- 什么是跨域?如何解决请跨域问题?
- Technology sharing | quick intercom integrated dispatching system
- 网络工程——软科中国大学专业排名
- JDBC连接数据库
- ARouter源码解析(三)
- 376. 摆动序列【贪心、动态规划------】
- 51 single chip microcomputer storage: EEPROM (I2C)
- 【AUTOSAR-RTE】-2-Composition,Component和VFB的介绍
- 就这么一个简单的校验,80%的程序员却做不到,更不理解!
- Activiti startup error: cannot create poolableconnectionfactory (could not create connection to database server
猜你喜欢

译文推荐 | 调试 BookKeeper 协议 - 无界 Ledger

ECCV 2022 | 无需微调即可推广!基于配准的少样本异常检测框架

Introduction to shardingsphere's concept of sub database and sub table (2)

Title and answer of work permit for safety management personnel of hazardous chemical business units in 2022

QT basic hand training applet - simple calculator design (with source code, analysis)

final关键字和枚举类型

Database core system

IJCAI 2022 | 图结构学习最新综述:研究进展与未来展望

mysql 最大建议行数2000w,靠谱吗?

《PyTorch深度学习实践》第九课多分类问题(手写数字MNIST)
随机推荐
AMQ streams (1) of openshift 4 - multiple consumers receive data from partition
MQTT. JS introductory tutorial: learning notes
Window源码解析(三):Window的更新机制
【AUTOSAR-RTE】-3-Runnable及其Task Mapping映射
负数的十六进制表示
Oracle-11gR2默认的系统JOB
JS array is de duplicated, the ID is the same, and a value is added and merged
ActivityRouter源码解析
Regular expressions are hexadecimal digits?
FPGA development learning open source website summary
Which system table is the keyword of SQL Server in?
MySQL 8.0.30 GA
Salted fish esp32 instance - mqtt lit LED
LeetCode - 哈希表专题
Face warp - hand tear code
ARouter源码解析(二)
【C语言】详解顺序表(SeqList)
网络工程——软科中国大学专业排名
Introduction to shardingsphere's concept of sub database and sub table (2)
Promise实例如何解决地狱回调