mirror of
https://gitee.com/houstudio/Cdroid.git
synced 2024-12-12 12:05:08 +08:00
modify looper& uieventsource,make self destroy more safely
This commit is contained in:
parent
4a69c3a3ef
commit
082339e621
@ -29,6 +29,7 @@ void AnimationHandler::MyFrameCallbackProvider::setFrameDelay(long delay) {
|
|||||||
AnimationHandler::AnimationHandler(){
|
AnimationHandler::AnimationHandler(){
|
||||||
mProvider = nullptr;
|
mProvider = nullptr;
|
||||||
mListDirty = false;
|
mListDirty = false;
|
||||||
|
mInLooper = false;
|
||||||
mFrameCallback=std::bind(&AnimationHandler::doFrame,this,std::placeholders::_1);
|
mFrameCallback=std::bind(&AnimationHandler::doFrame,this,std::placeholders::_1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,8 +72,8 @@ void AnimationHandler::doAnimationFrame(long frameTime){
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool AnimationHandler::isCallbackDue(AnimationFrameCallback* callback, long currentTime){
|
bool AnimationHandler::isCallbackDue(AnimationFrameCallback* callback, long currentTime){
|
||||||
auto it=mDelayedCallbackStartTime.find(callback);
|
auto it = mDelayedCallbackStartTime.find(callback);
|
||||||
if(it==mDelayedCallbackStartTime.end())return true;
|
if(it == mDelayedCallbackStartTime.end())return true;
|
||||||
if (it->second < currentTime) {
|
if (it->second < currentTime) {
|
||||||
mDelayedCallbackStartTime.erase(it);
|
mDelayedCallbackStartTime.erase(it);
|
||||||
return true;
|
return true;
|
||||||
@ -81,9 +82,9 @@ bool AnimationHandler::isCallbackDue(AnimationFrameCallback* callback, long curr
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AnimationHandler::commitAnimationFrame(AnimationFrameCallback* callback, long frameTime){
|
void AnimationHandler::commitAnimationFrame(AnimationFrameCallback* callback, long frameTime){
|
||||||
auto it=mDelayedCallbackStartTime.find(callback);
|
auto it = mDelayedCallbackStartTime.find(callback);
|
||||||
auto itc=std::find(mCommitCallbacks.begin(),mCommitCallbacks.end(),callback);
|
auto itc = std::find(mCommitCallbacks.begin(),mCommitCallbacks.end(),callback);
|
||||||
if (it==mDelayedCallbackStartTime.end() && itc!=mCommitCallbacks.end()) {
|
if (it == mDelayedCallbackStartTime.end() && itc!=mCommitCallbacks.end()) {
|
||||||
callback->commitAnimationFrame(frameTime);/*commitAnimationFrame mybe call removeCallback!!!*/
|
callback->commitAnimationFrame(frameTime);/*commitAnimationFrame mybe call removeCallback!!!*/
|
||||||
mCommitCallbacks.erase(itc);
|
mCommitCallbacks.erase(itc);
|
||||||
}
|
}
|
||||||
@ -102,6 +103,10 @@ void AnimationHandler::cleanUpList(){
|
|||||||
|
|
||||||
static NeverDestroyed<AnimationHandler>mInst;
|
static NeverDestroyed<AnimationHandler>mInst;
|
||||||
AnimationHandler&AnimationHandler::getInstance(){
|
AnimationHandler&AnimationHandler::getInstance(){
|
||||||
|
if(!mInst->mInLooper){
|
||||||
|
Looper::getDefault()->addEventHandler(mInst.get());
|
||||||
|
mInst->mInLooper = true;
|
||||||
|
}
|
||||||
return *mInst;
|
return *mInst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,6 +32,7 @@ private:
|
|||||||
void setFrameDelay(long delay)override;
|
void setFrameDelay(long delay)override;
|
||||||
};
|
};
|
||||||
bool mListDirty;
|
bool mListDirty;
|
||||||
|
bool mInLooper;
|
||||||
AnimationFrameCallbackProvider* mProvider;
|
AnimationFrameCallbackProvider* mProvider;
|
||||||
Choreographer::FrameCallback mFrameCallback;
|
Choreographer::FrameCallback mFrameCallback;
|
||||||
std::list<AnimationFrameCallback*> mAnimationCallbacks;
|
std::list<AnimationFrameCallback*> mAnimationCallbacks;
|
||||||
|
@ -320,11 +320,19 @@ int Looper::pollInner(int timeoutMillis) {
|
|||||||
EventHandler*es=(*it);
|
EventHandler*es=(*it);
|
||||||
if(es&&((es->mFlags&1)==0)&&(es->checkEvents()>0)){
|
if(es&&((es->mFlags&1)==0)&&(es->checkEvents()>0)){
|
||||||
es->handleEvents();
|
es->handleEvents();
|
||||||
if(es->mFlags&1) it = mEventHandlers.erase(it);
|
//may be EventHandler::handleEvents will destroy itself,recheck the flags
|
||||||
if((es->mFlags&3)==3) delete es;//EventHandler owned by looper must be freed here
|
if((es->mFlags&1)==1) it = mEventHandlers.erase(it);
|
||||||
|
if((es->mFlags&3)==3) delete es;//make EventHandler::handleEvents can destroy itself
|
||||||
|
//EventHandler owned by looper must be freed here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(auto it=mEventHandlers.begin();it!=mEventHandlers.end();it++){
|
||||||
|
EventHandler*es=(*it);
|
||||||
|
if((es->mFlags&3)==3){
|
||||||
|
it = mEventHandlers.erase(it);
|
||||||
|
delete es;//EventHandler owned by looper must be freed here
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//removeEventHandlers();
|
|
||||||
long elapsedMillis = SystemClock::uptimeMillis()-t1;
|
long elapsedMillis = SystemClock::uptimeMillis()-t1;
|
||||||
|
|
||||||
// Acquire lock.
|
// Acquire lock.
|
||||||
@ -380,7 +388,6 @@ int Looper::pollAll(int timeoutMillis, int* outFd, int* outEvents, void** outDat
|
|||||||
return result;
|
return result;
|
||||||
} else {
|
} else {
|
||||||
nsecs_t endTime = SystemClock::uptimeMillis() + timeoutMillis;
|
nsecs_t endTime = SystemClock::uptimeMillis() + timeoutMillis;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int result = pollOnce(timeoutMillis, outFd, outEvents, outData);
|
int result = pollOnce(timeoutMillis, outFd, outEvents, outData);
|
||||||
if (result != POLL_CALLBACK) {
|
if (result != POLL_CALLBACK) {
|
||||||
@ -621,7 +628,7 @@ void Looper::removeHandler(MessageHandler*handler){
|
|||||||
for(auto it=mHandlers.begin();it!=mHandlers.end();it++){
|
for(auto it=mHandlers.begin();it!=mHandlers.end();it++){
|
||||||
if((*it)==handler){
|
if((*it)==handler){
|
||||||
handler->mFlags|=1;
|
handler->mFlags|=1;
|
||||||
if((handler->mFlags&2)==0)//handler is not owned by looper,erase at once
|
if((handler->mFlags&2)==0)//handler is not owned by looper,erase at once
|
||||||
mHandlers.erase(it);
|
mHandlers.erase(it);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -630,7 +637,6 @@ void Looper::removeHandler(MessageHandler*handler){
|
|||||||
|
|
||||||
void Looper::addEventHandler(const EventHandler*handler){
|
void Looper::addEventHandler(const EventHandler*handler){
|
||||||
mEventHandlers.insert(mEventHandlers.begin(),(EventHandler*)handler);
|
mEventHandlers.insert(mEventHandlers.begin(),(EventHandler*)handler);
|
||||||
LOGE_IF(mEventHandlers.size()>32,"Too many eventhandlers %d",mEventHandlers.size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Looper::removeEventHandler(const EventHandler*handler){
|
void Looper::removeEventHandler(const EventHandler*handler){
|
||||||
|
@ -49,7 +49,7 @@ int UIEventSource::handleRunnables(){
|
|||||||
RUNNER runner = mRunnables.front();
|
RUNNER runner = mRunnables.front();
|
||||||
if(runner.time > nowms)break;
|
if(runner.time > nowms)break;
|
||||||
mRunnables.pop_front();
|
mRunnables.pop_front();
|
||||||
if(runner.run)runner.run();
|
if(runner.run&&(runner.removed==false))runner.run();
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
if(((mFlags&1)==0) && mAttachedView->isDirty() && mAttachedView->getVisibility()==View::VISIBLE){
|
if(((mFlags&1)==0) && mAttachedView->isDirty() && mAttachedView->getVisibility()==View::VISIBLE){
|
||||||
|
@ -381,11 +381,6 @@ View::~View(){
|
|||||||
mViewCount --;
|
mViewCount --;
|
||||||
LOGD_IF(View::VIEW_DEBUG||(mViewCount>1000),"%p:%d mViewCount=%d",this,mID,mViewCount);
|
LOGD_IF(View::VIEW_DEBUG||(mViewCount>1000),"%p:%d mViewCount=%d",this,mID,mViewCount);
|
||||||
|
|
||||||
removeTapCallback();
|
|
||||||
removeLongPressCallback();
|
|
||||||
removePerformClickCallback();
|
|
||||||
removeUnsetPressCallback();
|
|
||||||
|
|
||||||
delete mScrollCache;
|
delete mScrollCache;
|
||||||
mScrollCache = nullptr;
|
mScrollCache = nullptr;
|
||||||
if(mParent)
|
if(mParent)
|
||||||
@ -1411,6 +1406,7 @@ void View::onDetachedFromWindowInternal() {
|
|||||||
mPrivateFlags3 &= ~PFLAG3_IS_LAID_OUT;
|
mPrivateFlags3 &= ~PFLAG3_IS_LAID_OUT;
|
||||||
mPrivateFlags3 &= ~PFLAG3_TEMPORARY_DETACH;
|
mPrivateFlags3 &= ~PFLAG3_TEMPORARY_DETACH;
|
||||||
|
|
||||||
|
removeTapCallback();
|
||||||
removeUnsetPressCallback();
|
removeUnsetPressCallback();
|
||||||
removeLongPressCallback();
|
removeLongPressCallback();
|
||||||
removePerformClickCallback();
|
removePerformClickCallback();
|
||||||
|
@ -118,12 +118,6 @@ AbsListView::~AbsListView() {
|
|||||||
mVelocityTracker->recycle();
|
mVelocityTracker->recycle();
|
||||||
mVelocityTracker = nullptr;
|
mVelocityTracker = nullptr;
|
||||||
}
|
}
|
||||||
if(mPerformClick)mPerformClick->removeCallbacks();
|
|
||||||
if(mPendingCheckForLongPress)mPendingCheckForLongPress->removeCallbacks();
|
|
||||||
if(mPendingCheckForTap)mPendingCheckForTap->removeCallbacks();
|
|
||||||
if(mPendingCheckForKeyLongPress)mPendingCheckForKeyLongPress->removeCallbacks();
|
|
||||||
if(mFlingRunnable)mFlingRunnable->removeCallbacks();
|
|
||||||
|
|
||||||
delete mSelector;
|
delete mSelector;
|
||||||
delete mFastScroll;
|
delete mFastScroll;
|
||||||
delete mRecycler;
|
delete mRecycler;
|
||||||
@ -1942,6 +1936,11 @@ void AbsListView::onAttachedToWindow() {
|
|||||||
void AbsListView::onDetachedFromWindow() {
|
void AbsListView::onDetachedFromWindow() {
|
||||||
AdapterView::onDetachedFromWindow();
|
AdapterView::onDetachedFromWindow();
|
||||||
|
|
||||||
|
if(mPerformClick)mPerformClick->removeCallbacks();
|
||||||
|
if(mPendingCheckForLongPress)mPendingCheckForLongPress->removeCallbacks();
|
||||||
|
if(mPendingCheckForTap)mPendingCheckForTap->removeCallbacks();
|
||||||
|
if(mPendingCheckForKeyLongPress)mPendingCheckForKeyLongPress->removeCallbacks();
|
||||||
|
if(mFlingRunnable)mFlingRunnable->removeCallbacks();
|
||||||
mIsDetaching = true;
|
mIsDetaching = true;
|
||||||
|
|
||||||
// Dismiss the popup in case onSaveInstanceState() was not invoked
|
// Dismiss the popup in case onSaveInstanceState() was not invoked
|
||||||
|
Loading…
Reference in New Issue
Block a user