Merge "Add debug logs for why taskbar might not be destroyed" into tm-qpr-dev
diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java
index c537106..3d8bf9e 100644
--- a/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java
+++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java
@@ -22,6 +22,7 @@
 
 import static com.android.launcher3.util.DisplayController.CHANGE_DENSITY;
 import static com.android.launcher3.util.DisplayController.CHANGE_NAVIGATION_MODE;
+import static com.android.launcher3.util.DisplayController.TASKBAR_NOT_DESTROYED_TAG;
 import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
 import static com.android.launcher3.util.FlagDebugUtils.formatFlagChange;
 
@@ -63,6 +64,7 @@
 import com.android.systemui.unfold.util.ScopedUnfoldTransitionProgressProvider;
 
 import java.io.PrintWriter;
+import java.util.StringJoiner;
 
 /**
  * Class to manage taskbar lifecycle
@@ -147,6 +149,8 @@
 
             @Override
             public void onConfigurationChanged(Configuration newConfig) {
+                debugWhyTaskbarNotDestroyed(
+                        "TaskbarManager#mComponentCallbacks.onConfigurationChanged: " + newConfig);
                 DeviceProfile dp = mUserUnlocked
                         ? LauncherAppState.getIDP(mContext).getDeviceProfile(mContext)
                         : null;
@@ -179,6 +183,9 @@
                     }
                 }
 
+                debugWhyTaskbarNotDestroyed("ComponentCallbacks#onConfigurationChanged() "
+                        + "configDiffForRecreate="
+                        + Configuration.configurationDiffToString(configDiffForRecreate));
                 if ((configDiffForRecreate & configsRequiringRecreate) != 0) {
                     recreateTaskbar();
                 } else {
@@ -207,6 +214,8 @@
                 mNavMode = info.navigationMode;
                 recreateTaskbar();
             }
+            debugWhyTaskbarNotDestroyed("DisplayInfoChangeListener#"
+                    + mDisplayController.getChangeFlagsString(flags));
         };
         mNavMode = mDisplayController.getInfo().navigationMode;
         mDisplayController.addChangeListener(mDispInfoChangeListener);
@@ -227,10 +236,13 @@
                     new IntentFilter(ACTION_SHOW_TASKBAR),
                     RECEIVER_NOT_EXPORTED);
         });
+
+        debugWhyTaskbarNotDestroyed("TaskbarManager created");
         recreateTaskbar();
     }
 
     private void destroyExistingTaskbar() {
+        debugWhyTaskbarNotDestroyed("destroyExistingTaskbar: " + mTaskbarActivityContext);
         if (mTaskbarActivityContext != null) {
             mTaskbarActivityContext.onDestroy();
             if (!FLAG_HIDE_NAVBAR_WINDOW) {
@@ -274,7 +286,12 @@
         if (mActivity == activity) {
             return;
         }
+        if (mActivity != null) {
+            mActivity.removeOnDeviceProfileChangeListener(mDebugActivityDeviceProfileChanged);
+        }
         mActivity = activity;
+        debugWhyTaskbarNotDestroyed("Set mActivity=" + mActivity);
+        mActivity.addOnDeviceProfileChangeListener(mDebugActivityDeviceProfileChanged);
         UnfoldTransitionProgressProvider unfoldTransitionProgressProvider =
                 getUnfoldTransitionProgressProviderForActivity(activity);
         mUnfoldProgressProvider.setSourceProvider(unfoldTransitionProgressProvider);
@@ -318,7 +335,9 @@
      */
     public void clearActivity(@NonNull StatefulActivity activity) {
         if (mActivity == activity) {
+            mActivity.removeOnDeviceProfileChangeListener(mDebugActivityDeviceProfileChanged);
             mActivity = null;
+            debugWhyTaskbarNotDestroyed("clearActivity");
             if (mTaskbarActivityContext != null) {
                 mTaskbarActivityContext.setUIController(TaskbarUIController.DEFAULT);
             }
@@ -338,8 +357,12 @@
 
         destroyExistingTaskbar();
 
-        boolean isTaskBarEnabled = dp != null && isTaskbarPresent(dp);
-        if (!isTaskBarEnabled) {
+        boolean isTaskbarEnabled = dp != null && isTaskbarPresent(dp);
+        debugWhyTaskbarNotDestroyed("recreateTaskbar: isTaskbarEnabled=" + isTaskbarEnabled
+                + " [dp != null (i.e. mUserUnlocked)]=" + (dp != null)
+                + " FLAG_HIDE_NAVBAR_WINDOW=" + FLAG_HIDE_NAVBAR_WINDOW
+                + " dp.isTaskbarPresent=" + (dp == null ? "null" : dp.isTaskbarPresent));
+        if (!isTaskbarEnabled) {
             SystemUiProxy.INSTANCE.get(mContext)
                     .notifyTaskbarStatus(/* visible */ false, /* stashed */ false);
             return;
@@ -440,6 +463,11 @@
      * Called when the manager is no longer needed
      */
     public void destroy() {
+        debugWhyTaskbarNotDestroyed("TaskbarManager#destroy()");
+        if (mActivity != null) {
+            mActivity.removeOnDeviceProfileChangeListener(mDebugActivityDeviceProfileChanged);
+        }
+
         UI_HELPER_EXECUTOR.execute(
                 () -> mTaskbarBroadcastReceiver.unregisterReceiverSafely(mContext));
         destroyExistingTaskbar();
@@ -464,4 +492,46 @@
             mTaskbarActivityContext.dumpLogs(prefix + "\t", pw);
         }
     }
+
+    /** Temp logs for b/254119092. */
+    public void debugWhyTaskbarNotDestroyed(String debugReason) {
+        StringJoiner log = new StringJoiner("\n");
+        log.add(debugReason);
+
+        boolean activityTaskbarPresent = mActivity != null
+                && mActivity.getDeviceProfile().isTaskbarPresent;
+        boolean contextTaskbarPresent = mUserUnlocked
+                && LauncherAppState.getIDP(mContext).getDeviceProfile(mContext).isTaskbarPresent;
+        if (activityTaskbarPresent == contextTaskbarPresent) {
+            log.add("mActivity and mContext agree taskbarIsPresent=" + contextTaskbarPresent);
+            Log.d(TASKBAR_NOT_DESTROYED_TAG, log.toString());
+            return;
+        }
+
+        log.add("mActivity and mContext device profiles have different values, add more logs.");
+
+        log.add("\tmActivity logs:");
+        log.add("\t\tmActivity=" + mActivity);
+        if (mActivity != null) {
+            log.add("\t\tmActivity.getResources().getConfiguration()="
+                    + mActivity.getResources().getConfiguration());
+            log.add("\t\tmActivity.getDeviceProfile().isTaskbarPresent="
+                    + activityTaskbarPresent);
+        }
+        log.add("\tmContext logs:");
+        log.add("\t\tmContext=" + mContext);
+        log.add("\t\tmContext.getResources().getConfiguration()="
+                + mContext.getResources().getConfiguration());
+        if (mUserUnlocked) {
+            log.add("\t\tLauncherAppState.getIDP().getDeviceProfile(mContext).isTaskbarPresent="
+                    + contextTaskbarPresent);
+        } else {
+            log.add("\t\tCouldn't get DeviceProfile because !mUserUnlocked");
+        }
+
+        Log.d(TASKBAR_NOT_DESTROYED_TAG, log.toString());
+    }
+
+    private final DeviceProfile.OnDeviceProfileChangeListener mDebugActivityDeviceProfileChanged =
+            dp -> debugWhyTaskbarNotDestroyed("mActivity onDeviceProfileChanged");
 }
diff --git a/quickstep/src/com/android/launcher3/uioverrides/QuickstepLauncher.java b/quickstep/src/com/android/launcher3/uioverrides/QuickstepLauncher.java
index 7318298..243ed5c 100644
--- a/quickstep/src/com/android/launcher3/uioverrides/QuickstepLauncher.java
+++ b/quickstep/src/com/android/launcher3/uioverrides/QuickstepLauncher.java
@@ -1237,6 +1237,9 @@
         Trace.instantForTrack(TRACE_TAG_APP, "QuickstepLauncher#DeviceProfileChanged",
                 getDeviceProfile().toSmallString());
         SystemUiProxy.INSTANCE.get(this).setLauncherAppIconSize(mDeviceProfile.iconSizePx);
+        if (mTaskbarManager != null) {
+            mTaskbarManager.debugWhyTaskbarNotDestroyed("QuickstepLauncher#onDeviceProfileChanged");
+        }
     }
 
     /**
diff --git a/src/com/android/launcher3/util/DisplayController.java b/src/com/android/launcher3/util/DisplayController.java
index 3d455d8..a3261b2 100644
--- a/src/com/android/launcher3/util/DisplayController.java
+++ b/src/com/android/launcher3/util/DisplayController.java
@@ -23,6 +23,7 @@
 import static com.android.launcher3.config.FeatureFlags.ENABLE_TRANSIENT_TASKBAR;
 import static com.android.launcher3.config.FeatureFlags.FORCE_PERSISTENT_TASKBAR;
 import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
+import static com.android.launcher3.util.FlagDebugUtils.appendFlag;
 import static com.android.launcher3.util.window.WindowManagerProxy.MIN_TABLET_WIDTH;
 
 import android.annotation.SuppressLint;
@@ -55,6 +56,7 @@
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
+import java.util.StringJoiner;
 
 /**
  * Utility class to cache properties of default display to avoid a system RPC on every call.
@@ -66,6 +68,9 @@
     private static final boolean DEBUG = false;
     private static boolean sTransientTaskbarStatusForTests;
 
+    // TODO(b/254119092) remove all logs with this tag
+    public static final String TASKBAR_NOT_DESTROYED_TAG = "b/254119092";
+
     public static final MainThreadInitializedObject<DisplayController> INSTANCE =
             new MainThreadInitializedObject<>(DisplayController::new);
 
@@ -206,6 +211,7 @@
     @Override
     @TargetApi(Build.VERSION_CODES.S)
     public final void onConfigurationChanged(Configuration config) {
+        Log.d(TASKBAR_NOT_DESTROYED_TAG, "DisplayController#onConfigurationChanged: " + config);
         Display display = mWindowContext.getDisplay();
         if (config.densityDpi != mInfo.densityDpi
                 || config.fontScale != mInfo.fontScale
@@ -272,7 +278,7 @@
             change |= CHANGE_SUPPORTED_BOUNDS;
         }
         if (DEBUG) {
-            Log.d(TAG, "handleInfoChange - change: 0b" + Integer.toBinaryString(change));
+            Log.d(TAG, "handleInfoChange - change: " + getChangeFlagsString(change));
         }
 
         if (change != 0) {
@@ -392,6 +398,20 @@
     }
 
     /**
+     * Returns the given binary flags as a human-readable string.
+     * @see #CHANGE_ALL
+     */
+    public String getChangeFlagsString(int change) {
+        StringJoiner result = new StringJoiner("|");
+        appendFlag(result, change, CHANGE_ACTIVE_SCREEN, "CHANGE_ACTIVE_SCREEN");
+        appendFlag(result, change, CHANGE_ROTATION, "CHANGE_ROTATION");
+        appendFlag(result, change, CHANGE_DENSITY, "CHANGE_DENSITY");
+        appendFlag(result, change, CHANGE_SUPPORTED_BOUNDS, "CHANGE_SUPPORTED_BOUNDS");
+        appendFlag(result, change, CHANGE_NAVIGATION_MODE, "CHANGE_NAVIGATION_MODE");
+        return result.toString();
+    }
+
+    /**
      * Dumps the current state information
      */
     public void dump(PrintWriter pw) {