From 4a2a45dde3659418be419ee5773ec8ac7f6b08b1 Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 27 Apr 2020 16:03:37 -0700 Subject: Updated Android native app to better handle orientation changes --- NativeApp/include/Android/AndroidAppBase.hpp | 23 +++--------------- NativeApp/src/Android/AndroidAppBase.cpp | 35 +++++++++++++++++++++------- 2 files changed, 29 insertions(+), 29 deletions(-) (limited to 'NativeApp') diff --git a/NativeApp/include/Android/AndroidAppBase.hpp b/NativeApp/include/Android/AndroidAppBase.hpp index 21cd937..403c62d 100644 --- a/NativeApp/include/Android/AndroidAppBase.hpp +++ b/NativeApp/include/Android/AndroidAppBase.hpp @@ -42,31 +42,16 @@ public: void SetState(android_app* state, const char* native_activity_class_name); void InitSensors(); void ProcessSensors(int32_t id); - void DrawFrame(); + virtual void DrawFrame(); bool IsReady(); virtual void TrimMemory() = 0; virtual void TermDisplay() = 0; static int32_t HandleInput(android_app* app, AInputEvent* event); static void HandleCmd(android_app* app, int32_t cmd); - bool CheckWindowSizeChanged() - { - auto new_window_width = ANativeWindow_getWidth(app_->window); - auto new_window_height = ANativeWindow_getHeight(app_->window); - if (new_window_width != window_width_ || new_window_height != window_height_) - { - window_width_ = new_window_width; - window_height_ = new_window_height; - return true; - } - else - return false; - } - protected: virtual void Initialize() { - CheckWindowSizeChanged(); } virtual int Resume(ANativeWindow* window) = 0; @@ -100,10 +85,8 @@ private: void ShowUI(); void UpdateFPS(float fFPS); - bool initialized_resources_ = false; - bool has_focus_ = false; - int32_t window_width_ = 0; - int32_t window_height_ = 0; + bool initialized_resources_ = false; + bool has_focus_ = false; ASensorManager* sensor_manager_ = nullptr; const ASensor* accelerometer_sensor_ = nullptr; diff --git a/NativeApp/src/Android/AndroidAppBase.cpp b/NativeApp/src/Android/AndroidAppBase.cpp index bc79470..3af8116 100644 --- a/NativeApp/src/Android/AndroidAppBase.cpp +++ b/NativeApp/src/Android/AndroidAppBase.cpp @@ -72,12 +72,6 @@ void AndroidAppBase::InitSensors() // void AndroidAppBase::DrawFrame() { - // APP_CMD_CONFIG_CHANGED event is generated seveal frames - // before the screen is actually resized. The only robust way - // to detect window resize is to check it very frame - if (CheckWindowSizeChanged()) - WindowResize(window_width_, window_height_); - float fFPS; if (monitor_.Update(fFPS)) { @@ -135,11 +129,34 @@ void AndroidAppBase::HandleCmd(struct android_app* app, int32_t cmd) break; case APP_CMD_CONFIG_CHANGED: + { + // This callback is not reliable for handling orientation changes. Depending on the + // device, it may be called before or after the surface has been actually resized. + break; + } + + // Note that as of NDK r21b (21.1.6352462), APP_CMD_WINDOW_RESIZED event is never generated + // by android_native_app_glue. + // Also note that modifying android_native_app_glue to handle onNativeWindowResized + // callback (as suggested in https://android-developers.googleblog.com/2020/02/handling-device-orientation-efficiently.html) + // does not work either - the callback is only called once after the window has been created. case APP_CMD_WINDOW_RESIZED: - // This does not work as the screen resizes few frames - // after the event has been received - // eng->WindowResize(0,0); + { + auto new_window_width = ANativeWindow_getWidth(app->window); + auto new_window_height = ANativeWindow_getHeight(app->window); + eng->WindowResize(new_window_width, new_window_height); break; + } + + // Note that as of NDK r21b (21.1.6352462), APP_CMD_CONTENT_RECT_CHANGED event is never + // generated by android_native_app_glue + case APP_CMD_CONTENT_RECT_CHANGED: + { + auto new_window_width = app->contentRect.right - app->contentRect.left; + auto new_window_height = app->contentRect.bottom - app->contentRect.top; + eng->WindowResize(new_window_width, new_window_height); + break; + } case APP_CMD_TERM_WINDOW: // The window is being hidden or closed, clean it up. -- cgit v1.2.3