Skip to content

Commit

Permalink
Fix non-foreground state processing time-consuming tasks, resulting i…
Browse files Browse the repository at this point in the history
…n anr (cocos#11406)

* Fix non-foreground state processing time-consuming tasks, resulting in anr
  • Loading branch information
bofeng-song authored Jun 9, 2022
1 parent c61dbc2 commit f7c129c
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions native/cocos/platform/android/AndroidPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,19 @@ class GameInputProxy {
break;
}
case APP_CMD_GAINED_FOCUS:
_isActive = true;
CC_LOG_INFO("AndroidPlatform: APP_CMD_GAINED_FOCUS");
break;
case APP_CMD_LOST_FOCUS:
_isActive = false;
CC_LOG_INFO("AndroidPlatform: APP_CMD_LOST_FOCUS");
break;
case APP_CMD_PAUSE:
_isActive = false;
CC_LOG_INFO("AndroidPlatform: APP_CMD_PAUSE");
break;
case APP_CMD_RESUME: {
_isActive = true;
CC_LOG_INFO("AndroidPlatform: APP_CMD_RESUME");
break;
}
Expand Down Expand Up @@ -394,10 +398,14 @@ class GameInputProxy {
_eventCallback = std::move(callback);
}

bool isAnimating() const {
inline bool isAnimating() const {
return _isVisible && _hasWindow;
}

inline bool isActive() const {
return _isActive;
}

private:
AppEventCallback _eventCallback{nullptr};
AndroidPlatform *_androidPlatform{nullptr};
Expand All @@ -406,6 +414,7 @@ class GameInputProxy {
bool _launched{false};
bool _isVisible{false};
bool _hasWindow{false};
bool _isActive{false};
};

static void handleCmdProxy(struct android_app *app, int32_t cmd) {
Expand Down Expand Up @@ -512,9 +521,11 @@ int32_t AndroidPlatform::loop() {
}
}
_inputProxy->handleInput();
if (_inputProxy->isAnimating() ) {
if (_inputProxy->isAnimating()) {
runTask();
flushTasksOnGameThreadAtForegroundJNI();
if (_inputProxy->isActive()) {
flushTasksOnGameThreadAtForegroundJNI();
}
}
flushTasksOnGameThreadJNI();

Expand Down

0 comments on commit f7c129c

Please sign in to comment.