Merge "Ignore package sessions with empty package names." into main
diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/common/data/repository/PackageInstallerMonitorTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/common/data/repository/PackageInstallerMonitorTest.kt
index 781e416..ede29d8 100644
--- a/packages/SystemUI/multivalentTests/src/com/android/systemui/common/data/repository/PackageInstallerMonitorTest.kt
+++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/common/data/repository/PackageInstallerMonitorTest.kt
@@ -26,6 +26,9 @@
 import com.android.systemui.common.shared.model.PackageInstallSession
 import com.android.systemui.coroutines.collectLastValue
 import com.android.systemui.kosmos.applicationCoroutineScope
+import com.android.systemui.kosmos.backgroundScope
+import com.android.systemui.kosmos.collectLastValue
+import com.android.systemui.kosmos.runTest
 import com.android.systemui.kosmos.testScope
 import com.android.systemui.log.logcatLogBuffer
 import com.android.systemui.testKosmos
@@ -173,6 +176,58 @@
         }
 
     @Test
+    fun onCreateUpdatedSession_ignoreNullPackageNameSessions() =
+        kosmos.runTest {
+            val nullPackageSession =
+                SessionInfo().apply {
+                    sessionId = 1
+                    appPackageName = null
+                    appIcon = icon1
+                }
+
+            val wellFormedSession =
+                SessionInfo().apply {
+                    sessionId = 2
+                    appPackageName = "pkg_name"
+                    appIcon = icon2
+                }
+
+            defaultSessions = listOf(wellFormedSession)
+
+            whenever(packageInstaller.allSessions).thenReturn(defaultSessions)
+            whenever(packageInstaller.getSessionInfo(1)).thenReturn(nullPackageSession)
+            whenever(packageInstaller.getSessionInfo(2)).thenReturn(wellFormedSession)
+
+            val packageInstallerMonitor =
+                PackageInstallerMonitor(
+                    handler,
+                    backgroundScope,
+                    logcatLogBuffer("PackageInstallerRepositoryImplTest"),
+                    packageInstaller,
+                )
+
+            val sessions by collectLastValue(packageInstallerMonitor.installSessionsForPrimaryUser)
+
+            // Verify flow updated with the new session
+            assertThat(sessions)
+                .comparingElementsUsing(represents)
+                .containsExactlyElementsIn(defaultSessions)
+
+            val callback =
+                withArgCaptor<PackageInstaller.SessionCallback> {
+                    verify(packageInstaller).registerSessionCallback(capture(), eq(handler))
+                }
+
+            // New session added
+            callback.onCreated(nullPackageSession.sessionId)
+
+            // Verify flow updated with the new session
+            assertThat(sessions)
+                .comparingElementsUsing(represents)
+                .containsExactlyElementsIn(defaultSessions)
+        }
+
+    @Test
     fun installSessions_newSessionsAreAdded() =
         testScope.runTest {
             val installSessions by collectLastValue(underTest.installSessionsForPrimaryUser)
diff --git a/packages/SystemUI/src/com/android/systemui/common/data/repository/PackageInstallerMonitor.kt b/packages/SystemUI/src/com/android/systemui/common/data/repository/PackageInstallerMonitor.kt
index 208adc2..5f7dca8 100644
--- a/packages/SystemUI/src/com/android/systemui/common/data/repository/PackageInstallerMonitor.kt
+++ b/packages/SystemUI/src/com/android/systemui/common/data/repository/PackageInstallerMonitor.kt
@@ -64,15 +64,14 @@
                         synchronized(sessions) {
                             sessions.putAll(
                                 packageInstaller.allSessions
-                                    .filter { !TextUtils.isEmpty(it.appPackageName) }
-                                    .map { session -> session.toModel() }
+                                    .mapNotNull { session -> session.toModel() }
                                     .associateBy { it.sessionId }
                             )
                             updateInstallerSessionsFlow()
                         }
                         packageInstaller.registerSessionCallback(
                             this@PackageInstallerMonitor,
-                            bgHandler
+                            bgHandler,
                         )
                     } else {
                         synchronized(sessions) {
@@ -130,7 +129,7 @@
             if (session == null) {
                 sessions.remove(sessionId)
             } else {
-                sessions[sessionId] = session.toModel()
+                session.toModel()?.apply { sessions[sessionId] = this }
             }
             updateInstallerSessionsFlow()
         }
@@ -144,7 +143,11 @@
     companion object {
         const val TAG = "PackageInstallerMonitor"
 
-        private fun PackageInstaller.SessionInfo.toModel(): PackageInstallSession {
+        private fun PackageInstaller.SessionInfo.toModel(): PackageInstallSession? {
+            if (TextUtils.isEmpty(this.appPackageName)) {
+                return null
+            }
+
             return PackageInstallSession(
                 sessionId = this.sessionId,
                 packageName = this.appPackageName,