Fix external source check after ag/21912387.

We should not allow the real source to continue providing data while
it's being overridden in tests.

Bug: 272333629
Test: atest CtsSafetyCenterTestCases
Change-Id: Ifec2d034799db3132bbabb8a8db0c297200fc63f
diff --git a/service/java/com/android/safetycenter/SafetyCenterConfigReader.java b/service/java/com/android/safetycenter/SafetyCenterConfigReader.java
index 35c72b1..c97fb54 100644
--- a/service/java/com/android/safetycenter/SafetyCenterConfigReader.java
+++ b/service/java/com/android/safetycenter/SafetyCenterConfigReader.java
@@ -124,8 +124,8 @@
      * Returns the {@link ExternalSafetySource} associated with the {@code safetySourceId}, if any.
      *
      * <p>The returned {@link SafetySource} can either be associated with the XML or overridden
-     * {@link SafetyCenterConfig}; {@link #isExternalSafetySourceActive(String)} can be used to
-     * check if it is associated with the current {@link SafetyCenterConfig}. This is to continue
+     * {@link SafetyCenterConfig}; {@link #isExternalSafetySourceActive(String, String)} can be used
+     * to check if it is associated with the current {@link SafetyCenterConfig}. This is to continue
      * allowing sources from the XML config to interact with SafetCenter during tests (but their
      * calls will be no-oped).
      *
@@ -177,9 +177,30 @@
      * currently expected to interact with our API and may affect Safety Center status. An inactive
      * source is expected to interact with Safety Center, but is currently being silenced / no-ops
      * while an override for tests is in place.
+     *
+     * <p>The {@code callingPackageName} is used to differentiate a real source being overridden. It
+     * could be that a test is overriding a real source and as such the real source should not be
+     * able to provide data while its override is in place.
      */
-    public boolean isExternalSafetySourceActive(String safetySourceId) {
-        return getCurrentConfigInternal().getExternalSafetySources().containsKey(safetySourceId);
+    public boolean isExternalSafetySourceActive(String safetySourceId, String callingPackageName) {
+        ExternalSafetySource externalSafetySourceInCurrentConfig =
+                getCurrentConfigInternal().getExternalSafetySources().get(safetySourceId);
+        if (externalSafetySourceInCurrentConfig == null) {
+            return false;
+        }
+        return Objects.equals(
+                externalSafetySourceInCurrentConfig.getSafetySource().getPackageName(),
+                callingPackageName);
+    }
+
+    /**
+     * Returns whether the {@code safetySourceId} is associated with an {@link ExternalSafetySource}
+     * that is in the real config XML file (i.e. not being overridden).
+     */
+    public boolean isExternalSafetySourceFromRealConfig(String safetySourceId) {
+        return requireNonNull(mConfigInternalFromXml)
+                .getExternalSafetySources()
+                .containsKey(safetySourceId);
     }
 
     /**
diff --git a/service/java/com/android/safetycenter/data/SafetyCenterIssueDismissalRepository.java b/service/java/com/android/safetycenter/data/SafetyCenterIssueDismissalRepository.java
index 3fe1db1..bc9bfb2 100644
--- a/service/java/com/android/safetycenter/data/SafetyCenterIssueDismissalRepository.java
+++ b/service/java/com/android/safetycenter/data/SafetyCenterIssueDismissalRepository.java
@@ -374,10 +374,13 @@
             PersistedSafetyCenterIssue persistedIssue = persistedSafetyCenterIssues.get(i);
             SafetyCenterIssueKey key = SafetyCenterIds.issueKeyFromString(persistedIssue.getKey());
 
-            // Check the source associated with this issue still exists, it might have been removed
-            // from the Safety Center config or the device might have rebooted with data persisted
-            // from a temporary Safety Center config.
-            if (!mSafetyCenterConfigReader.isExternalSafetySourceActive(key.getSafetySourceId())) {
+            // Only load the issues associated with the "real" config. We do not want to keep on
+            // persisting potentially stray issues from tests (they should supposedly be cleared,
+            // but may stick around if the data is not cleared after a test run).
+            // There is a caveat that if a real source was overridden in tests and the override
+            // provided data without clearing it, we will associate this issue with the real source.
+            if (!mSafetyCenterConfigReader.isExternalSafetySourceFromRealConfig(
+                    key.getSafetySourceId())) {
                 someDataChanged = true;
                 continue;
             }
diff --git a/service/java/com/android/safetycenter/data/SafetySourceDataRepository.java b/service/java/com/android/safetycenter/data/SafetySourceDataRepository.java
index c8dd3d4..c0be280 100644
--- a/service/java/com/android/safetycenter/data/SafetySourceDataRepository.java
+++ b/service/java/com/android/safetycenter/data/SafetySourceDataRepository.java
@@ -371,7 +371,8 @@
 
         boolean retrievingOrClearingData = safetySourceData == null;
         if (retrievingOrClearingData) {
-            return mSafetyCenterConfigReader.isExternalSafetySourceActive(safetySourceId);
+            return mSafetyCenterConfigReader.isExternalSafetySourceActive(
+                    safetySourceId, packageName);
         }
 
         SafetySourceStatus safetySourceStatus = safetySourceData.getStatus();
@@ -437,7 +438,7 @@
             }
         }
 
-        return mSafetyCenterConfigReader.isExternalSafetySourceActive(safetySourceId);
+        return mSafetyCenterConfigReader.isExternalSafetySourceActive(safetySourceId, packageName);
     }
 
     private void validateCallingPackage(