Fix click on TileService bug when closing shade

If an active (unbound) tile is clicked and then the shade is closed
immediately, the click would never be sent to the tile. This was because
a tile that has `onStopListening` called will immediately stopListening
(even if not bound) and reject the click when bound finally happens.

Instead, queue the stopListening to happen right after the click is
dispatched once the tile is bound.

Also, fix when we unbind from active tiles (as together with this it was
causing multiple calls to `onStopListening`). Now:
* If an active tile requests listening, it will be unbound right after
  they send a status update.
* If an active tile is bound because of a click, it will stop listening
  and be unbound as if it was not active.

Test: atest com.android.systemui.qs
Test: atest CtsTileServiceTestCases CtsSystemUiHostTestCases
Flag: ACONFIG com.android.systemui.qs_custom_tile_click_guaranteed_bug_fix DISABLED
Fixes: 339290820

Change-Id: I0d0a87304e252ad68c48145819098115b00399a1
diff --git a/packages/SystemUI/aconfig/systemui.aconfig b/packages/SystemUI/aconfig/systemui.aconfig
index e69ac0a..626e219 100644
--- a/packages/SystemUI/aconfig/systemui.aconfig
+++ b/packages/SystemUI/aconfig/systemui.aconfig
@@ -882,3 +882,13 @@
   description: "Enables Backlinks improvement feature in App Clips"
   bug: "300307759"
 }
+
+flag {
+  name: "qs_custom_tile_click_guaranteed_bug_fix"
+  namespace: "systemui"
+  description: "Guarantee that clicks on a tile always happen by postponing onStopListening until after the click."
+  bug: "339290820"
+  metadata {
+    purpose: PURPOSE_BUGFIX
+  }
+}
\ No newline at end of file