Merge "CP partial ag/26460595 for coroutine instance." into 24D1-dev
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogDelegate.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogDelegate.java
index dd40b7f..f30a838 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogDelegate.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogDelegate.java
@@ -68,13 +68,15 @@
 import com.android.systemui.statusbar.policy.KeyguardStateController;
 import com.android.wifitrackerlib.WifiEntry;
 
-import java.util.List;
-import java.util.concurrent.Executor;
-
 import dagger.assisted.Assisted;
 import dagger.assisted.AssistedFactory;
 import dagger.assisted.AssistedInject;
 
+import kotlinx.coroutines.CoroutineScope;
+
+import java.util.List;
+import java.util.concurrent.Executor;
+
 /**
  * Dialog for showing mobile network, connected Wi-Fi network and Wi-Fi networks.
  */
@@ -165,7 +167,8 @@
         InternetDialogDelegate create(
                 @Assisted(ABOVE_STATUS_BAR) boolean aboveStatusBar,
                 @Assisted(CAN_CONFIG_MOBILE_DATA) boolean canConfigMobileData,
-                @Assisted(CAN_CONFIG_WIFI) boolean canConfigWifi);
+                @Assisted(CAN_CONFIG_WIFI) boolean canConfigWifi,
+                @Assisted CoroutineScope coroutineScope);
     }
 
     @AssistedInject
@@ -176,6 +179,7 @@
             @Assisted(ABOVE_STATUS_BAR) boolean canConfigMobileData,
             @Assisted(CAN_CONFIG_MOBILE_DATA) boolean canConfigWifi,
             @Assisted(CAN_CONFIG_WIFI) boolean aboveStatusBar,
+            @Assisted CoroutineScope coroutineScope,
             UiEventLogger uiEventLogger,
             DialogTransitionAnimator dialogTransitionAnimator,
             @Main Handler handler,
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogManager.kt b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogManager.kt
index 2a177c7..5aef950 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogManager.kt
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogManager.kt
@@ -21,26 +21,35 @@
 import com.android.systemui.animation.DialogCuj
 import com.android.systemui.animation.DialogTransitionAnimator
 import com.android.systemui.dagger.SysUISingleton
+import com.android.systemui.dagger.qualifiers.Background
 import com.android.systemui.statusbar.phone.SystemUIDialog
 import javax.inject.Inject
+import kotlinx.coroutines.CoroutineDispatcher
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.cancel
 
 private const val TAG = "InternetDialogFactory"
 private val DEBUG = Log.isLoggable(TAG, Log.DEBUG)
 
-/**
- * Factory to create [InternetDialogDelegate] objects.
- */
+/** Factory to create [InternetDialogDelegate] objects. */
 @SysUISingleton
-class InternetDialogManager @Inject constructor(
+class InternetDialogManager
+@Inject
+constructor(
     private val dialogTransitionAnimator: DialogTransitionAnimator,
-    private val dialogFactory: InternetDialogDelegate.Factory
+    private val dialogFactory: InternetDialogDelegate.Factory,
+    @Background private val bgDispatcher: CoroutineDispatcher,
 ) {
+    private lateinit var coroutineScope: CoroutineScope
     companion object {
         private const val INTERACTION_JANK_TAG = "internet"
         var dialog: SystemUIDialog? = null
     }
 
-    /** Creates a [InternetDialogDelegate]. The dialog will be animated from [view] if it is not null. */
+    /**
+     * Creates a [InternetDialogDelegate]. The dialog will be animated from [view] if it is not
+     * null.
+     */
     fun create(
         aboveStatusBar: Boolean,
         canConfigMobileData: Boolean,
@@ -53,16 +62,21 @@
             }
             return
         } else {
-            dialog = dialogFactory.create(
-                    aboveStatusBar, canConfigMobileData, canConfigWifi).createDialog()
+            coroutineScope = CoroutineScope(bgDispatcher)
+            dialog =
+                dialogFactory
+                    .create(aboveStatusBar, canConfigMobileData, canConfigWifi, coroutineScope)
+                    .createDialog()
             if (view != null) {
                 dialogTransitionAnimator.showFromView(
-                        dialog!!, view,
+                    dialog!!,
+                    view,
                     animateBackgroundBoundsChange = true,
-                    cuj = DialogCuj(
-                        InteractionJankMonitor.CUJ_SHADE_DIALOG_OPEN,
-                        INTERACTION_JANK_TAG
-                    )
+                    cuj =
+                        DialogCuj(
+                            InteractionJankMonitor.CUJ_SHADE_DIALOG_OPEN,
+                            INTERACTION_JANK_TAG
+                        )
                 )
             } else {
                 dialog!!.show()
@@ -74,6 +88,9 @@
         if (DEBUG) {
             Log.d(TAG, "destroyDialog")
         }
+        if (dialog != null) {
+            coroutineScope.cancel()
+        }
         dialog = null
     }
 }