Use ComponentCallbacks to recreate on fold/unfold

* DisplayController callbacks were sometimes being
called too soon and providing stale window bounds,
which led to the incorrect DeviceProfile being used.
Switched to ComponentCallbacks on the window to get
the most up-to-date callbacks.
* Sadly, for navigation mode changes, ComponentCallbacks
get fired too soon and have a stale navigation mode
so we continue to rely on DisplayController for that
change for now.

Fixes: 219958843
Test: Tested folding/unfolding, no taskbar appears when
it shouldn't.
Tested with changing display/font size as well as
nav mode changes

Change-Id: I1b398934c0ea49f69bad19b13977b7b0906f3190
diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java
index 5ce4fa7..494d21e 100644
--- a/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java
+++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java
@@ -89,8 +89,13 @@
      */
     private final TaskbarSharedState mSharedState = new TaskbarSharedState();
 
-    private static final int CHANGE_FLAGS = CHANGE_ACTIVE_SCREEN | CHANGE_DENSITY
-            | CHANGE_SUPPORTED_BOUNDS | CHANGE_NAVIGATION_MODE;
+    /**
+     * We use WindowManager's ComponentCallbacks() for most of the config changes, however for
+     * navigation mode, that callback gets called too soon, before it's internal navigation mode
+     * reflects the current one.
+     * DisplayController's callback is delayed enough to get the correct nav mode value
+     */
+    private static final int CHANGE_FLAGS = CHANGE_NAVIGATION_MODE;
 
     private boolean mUserUnlocked = false;
 
@@ -108,19 +113,29 @@
 
             @Override
             public void onConfigurationChanged(Configuration newConfig) {
+                DeviceProfile dp = mUserUnlocked
+                        ? LauncherAppState.getIDP(mContext).getDeviceProfile(mContext)
+                        : null;
                 int configDiff = mOldConfig.diff(newConfig);
                 int configsRequiringRecreate = ActivityInfo.CONFIG_ASSETS_PATHS
-                        | ActivityInfo.CONFIG_LAYOUT_DIRECTION | ActivityInfo.CONFIG_UI_MODE;
+                        | ActivityInfo.CONFIG_LAYOUT_DIRECTION | ActivityInfo.CONFIG_UI_MODE
+                        | ActivityInfo.CONFIG_DENSITY | ActivityInfo.CONFIG_SCREEN_SIZE;
                 if ((configDiff & configsRequiringRecreate) != 0) {
-                    // Color has changed, recreate taskbar to reload background color & icons.
-                    recreateTaskbar();
+                    if ((configDiff & ActivityInfo.CONFIG_SCREEN_SIZE) != 0 &&
+                            mTaskbarActivityContext != null && dp != null) {
+                        DeviceProfile oldDp = mTaskbarActivityContext.getDeviceProfile();
+                        // Additional check since this callback gets fired multiple times w/o
+                        // screen size changing
+                        if (dp.widthPx != oldDp.widthPx || dp.heightPx != oldDp.heightPx) {
+                            recreateTaskbar();
+                        }
+                    } else {
+                        // Color has changed, recreate taskbar to reload background color & icons.
+                        recreateTaskbar();
+                    }
                 } else {
                     // Config change might be handled without re-creating the taskbar
                     if (mTaskbarActivityContext != null) {
-                        DeviceProfile dp = mUserUnlocked
-                                ? LauncherAppState.getIDP(mContext).getDeviceProfile(mContext)
-                                : null;
-
                         if (dp != null && dp.isTaskbarPresent) {
                             mTaskbarActivityContext.updateDeviceProfile(dp.copy(mContext));
                         }