Merge cherrypicks of ['googleplex-android-review.googlesource.com/25702389'] into 24Q1-release.

Change-Id: Ib844542afa77aad9c3211651d223112e5a15f8b7
diff --git a/src/com/android/wallpaper/picker/customization/ui/binder/ScreenPreviewBinder.kt b/src/com/android/wallpaper/picker/customization/ui/binder/ScreenPreviewBinder.kt
index 46b2c1a..34f0770 100644
--- a/src/com/android/wallpaper/picker/customization/ui/binder/ScreenPreviewBinder.kt
+++ b/src/com/android/wallpaper/picker/customization/ui/binder/ScreenPreviewBinder.kt
@@ -163,6 +163,19 @@
 
         var disposableHandle: DisposableHandle? = null
 
+        val cleanupWallpaperConnectionRunnable = Runnable {
+            disposableHandle?.dispose()
+            wallpaperConnection?.destroy()
+            wallpaperConnection = null
+        }
+
+        fun cleanupWallpaperConnection() {
+            // If existing, remove any scheduled cleanups...
+            previewView.removeCallbacks(cleanupWallpaperConnectionRunnable)
+            // ...and cleanup immediately
+            cleanupWallpaperConnectionRunnable.run()
+        }
+
         val job =
             lifecycleOwner.lifecycleScope.launch {
                 launch {
@@ -206,9 +219,7 @@
                             override fun onDestroy(owner: LifecycleOwner) {
                                 super.onDestroy(owner)
                                 if (isPageTransitionsFeatureEnabled) {
-                                    disposableHandle?.dispose()
-                                    wallpaperConnection?.destroy()
-                                    wallpaperConnection = null
+                                    cleanupWallpaperConnection()
                                 }
                             }
 
@@ -246,10 +257,12 @@
                                     } else null
                                 )
                                 wallpaperIsReadyForReveal = false
-                                if (!isPageTransitionsFeatureEnabled) {
-                                    disposableHandle?.dispose()
-                                    wallpaperConnection?.destroy()
-                                    wallpaperConnection = null
+                                if (isPageTransitionsFeatureEnabled) {
+                                    // delay cleanup to prevent flicker between onStop and page
+                                    // transition animation start
+                                    previewView.postDelayed(cleanupWallpaperConnectionRunnable, 100)
+                                } else {
+                                    cleanupWallpaperConnectionRunnable.run()
                                 }
                             }
 
@@ -463,9 +476,7 @@
                             }
                             (wallpaperInfo as? LiveWallpaperInfo)?.let { liveWallpaperInfo ->
                                 if (isPageTransitionsFeatureEnabled) {
-                                    disposableHandle?.dispose()
-                                    wallpaperConnection?.destroy()
-                                    wallpaperConnection = null
+                                    cleanupWallpaperConnection()
                                 }
                                 val connection =
                                     wallpaperConnection