Further status logic tweaks

Change the SettingsUI logic slightly: All other things being equal, pick
the "worst" location dependency status to show. Some renaming and
annotations for clarity.

Also fixes the mocking behavior in the tests around resources, which
were accidentally using the real strings and therefore dependent on real
resource string values. Tests are made more explicit.

Bug: 266921482
Test: atest tests/robotests/src/com/android/settings/datetime/LocationProviderStatusPreferenceControllerTest.java
Change-Id: Ifd96c543dad692884be23bf94e3f1294eed291d5
diff --git a/src/com/android/settings/datetime/LocationProviderStatusPreferenceController.java b/src/com/android/settings/datetime/LocationProviderStatusPreferenceController.java
index eeece12..f45fe88 100644
--- a/src/com/android/settings/datetime/LocationProviderStatusPreferenceController.java
+++ b/src/com/android/settings/datetime/LocationProviderStatusPreferenceController.java
@@ -91,17 +91,14 @@
     // only one LTZP on a device, the primary. The UI here only reports status for one
     // LTZP. This UI logic prioritizes the primary if there is a "bad" status for both.
     @Nullable
-    private TimeZoneProviderStatus getLtzpStatus() {
+    private TimeZoneProviderStatus getLtzpStatusToReport() {
         LocationTimeZoneAlgorithmStatus status =
                 mTimeManager.getTimeZoneCapabilitiesAndConfig().getDetectorStatus()
                         .getLocationTimeZoneAlgorithmStatus();
-        TimeZoneProviderStatus primary = status.getPrimaryProviderReportedStatus();
-        TimeZoneProviderStatus secondary = status.getSecondaryProviderReportedStatus();
+        @Nullable TimeZoneProviderStatus primary = status.getPrimaryProviderReportedStatus();
+        @Nullable TimeZoneProviderStatus secondary = status.getSecondaryProviderReportedStatus();
         if (primary != null && secondary != null) {
-            if (primary.getLocationDetectionDependencyStatus() == DEPENDENCY_STATUS_OK) {
-                return secondary;
-            }
-            return primary;
+            return pickWorstLtzpStatus(primary, secondary);
         } else if (primary != null) {
             return primary;
         } else {
@@ -109,6 +106,23 @@
         }
     }
 
+    private static TimeZoneProviderStatus pickWorstLtzpStatus(
+            TimeZoneProviderStatus primary, TimeZoneProviderStatus secondary) {
+        int primaryScore = scoreLtzpStatus(primary);
+        int secondaryScore = scoreLtzpStatus(secondary);
+        return primaryScore >= secondaryScore ? primary : secondary;
+    }
+
+    private static int scoreLtzpStatus(TimeZoneProviderStatus providerStatus) {
+        @DependencyStatus int locationStatus =
+                providerStatus.getLocationDetectionDependencyStatus();
+        if (locationStatus <= DEPENDENCY_STATUS_OK) {
+            return 0;
+        }
+        // The enum values currently correspond well to severity.
+        return providerStatus.getLocationDetectionDependencyStatus();
+    }
+
     @Override
     public void onChange() {
         if (mPreference != null) {
@@ -128,33 +142,36 @@
 
         if (!timeZoneCapabilities.isUseLocationEnabled()
                 && hasLocationTimeZoneNoTelephonyFallback(detectorStatus)) {
-            return mContext.getResources().getString(
+            return mContext.getString(
                     R.string.location_time_zone_detection_status_summary_blocked_by_settings);
         }
 
-        TimeZoneProviderStatus ltzpStatus = getLtzpStatus();
+        TimeZoneProviderStatus ltzpStatus = getLtzpStatusToReport();
         if (ltzpStatus == null) {
             return "";
         }
 
-        @DependencyStatus int status = ltzpStatus.getLocationDetectionDependencyStatus();
+        @DependencyStatus int locationStatus = ltzpStatus.getLocationDetectionDependencyStatus();
 
-        if (status == TimeZoneProviderStatus.DEPENDENCY_STATUS_BLOCKED_BY_ENVIRONMENT) {
-            return mContext.getResources().getString(
-                    R.string.location_time_zone_detection_status_summary_blocked_by_environment);
-        }
-        if (status == TimeZoneProviderStatus.DEPENDENCY_STATUS_DEGRADED_BY_SETTINGS) {
-            return mContext.getResources().getString(
-                    R.string.location_time_zone_detection_status_summary_degraded_by_settings);
-        }
-        if (status == TimeZoneProviderStatus.DEPENDENCY_STATUS_TEMPORARILY_UNAVAILABLE) {
-            return mContext.getResources().getString(
-                    R.string.location_time_zone_detection_status_summary_temporarily_unavailable);
-        }
-        if (status == TimeZoneProviderStatus.DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS) {
-            return mContext.getResources().getString(
+        if (locationStatus == TimeZoneProviderStatus.DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS) {
+            return mContext.getString(
                     R.string.location_time_zone_detection_status_summary_blocked_by_settings);
         }
+        if (locationStatus == TimeZoneProviderStatus.DEPENDENCY_STATUS_DEGRADED_BY_SETTINGS) {
+            return mContext.getString(
+                    R.string.location_time_zone_detection_status_summary_degraded_by_settings);
+        }
+        if (locationStatus == TimeZoneProviderStatus.DEPENDENCY_STATUS_BLOCKED_BY_ENVIRONMENT) {
+            return mContext.getString(
+                    R.string.location_time_zone_detection_status_summary_blocked_by_environment);
+        }
+        if (locationStatus == TimeZoneProviderStatus.DEPENDENCY_STATUS_TEMPORARILY_UNAVAILABLE) {
+            return mContext.getString(
+                    R.string.location_time_zone_detection_status_summary_temporarily_unavailable);
+        }
+
+        // LTZP-reported network connectivity and time zone resolution statuses are currently
+        // ignored. Partners can tweak this logic if they also want to report these to users.
 
         return "";
     }
diff --git a/tests/robotests/src/com/android/settings/datetime/LocationProviderStatusPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/datetime/LocationProviderStatusPreferenceControllerTest.java
index 1da5e7c..9ace563 100644
--- a/tests/robotests/src/com/android/settings/datetime/LocationProviderStatusPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/datetime/LocationProviderStatusPreferenceControllerTest.java
@@ -21,6 +21,7 @@
 import static android.app.time.LocationTimeZoneAlgorithmStatus.PROVIDER_STATUS_IS_CERTAIN;
 import static android.app.time.LocationTimeZoneAlgorithmStatus.PROVIDER_STATUS_IS_UNCERTAIN;
 import static android.service.timezone.TimeZoneProviderStatus.DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS;
+import static android.service.timezone.TimeZoneProviderStatus.DEPENDENCY_STATUS_DEGRADED_BY_SETTINGS;
 import static android.service.timezone.TimeZoneProviderStatus.DEPENDENCY_STATUS_OK;
 
 import static com.google.common.truth.Truth.assertThat;
@@ -72,44 +73,9 @@
         when(mContext.getString(
                 R.string.location_time_zone_detection_status_summary_blocked_by_settings))
                 .thenReturn("BBS");
-    }
-
-    @Test
-    public void testCapabilityStatus() {
-        LocationProviderStatusPreferenceController controller =
-                new LocationProviderStatusPreferenceController(mContext, "LPSPC");
-
-        TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(false,
-                PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK,
-                PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK);
-        when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
-
-        assertThat(controller.getAvailabilityStatus()).isEqualTo(
-                BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
-
-        capabilitiesAndConfig = createCapabilitiesAndConfig(true,
-                PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK,
-                PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK);
-        when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
-
-        assertThat(controller.getAvailabilityStatus()).isEqualTo(
-                BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
-
-        capabilitiesAndConfig = createCapabilitiesAndConfig(false,
-                PROVIDER_STATUS_IS_UNCERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS,
-                PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK);
-        when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
-
-        assertThat(controller.getAvailabilityStatus()).isEqualTo(
-                BasePreferenceController.AVAILABLE_UNSEARCHABLE);
-
-        capabilitiesAndConfig = createCapabilitiesAndConfig(true,
-                PROVIDER_STATUS_IS_UNCERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS,
-                PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK);
-        when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
-
-        assertThat(controller.getAvailabilityStatus()).isEqualTo(
-                BasePreferenceController.AVAILABLE_UNSEARCHABLE);
+        when(mContext.getString(
+                R.string.location_time_zone_detection_status_summary_degraded_by_settings))
+                .thenReturn("DBS");
     }
 
     @Test
@@ -140,6 +106,41 @@
 
         assertThat(controller.getAvailabilityStatus()).isEqualTo(
                 BasePreferenceController.AVAILABLE_UNSEARCHABLE);
+
+        // Test whether reportable statuses that can still result in the LTZP being "certain" are
+        // reported.
+
+        capabilitiesAndConfig = createCapabilitiesAndConfig(false,
+                PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_DEGRADED_BY_SETTINGS,
+                PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK);
+        when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
+
+        assertThat(controller.getAvailabilityStatus()).isEqualTo(
+                BasePreferenceController.AVAILABLE_UNSEARCHABLE);
+
+        capabilitiesAndConfig = createCapabilitiesAndConfig(false,
+                PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_DEGRADED_BY_SETTINGS,
+                PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_DEGRADED_BY_SETTINGS);
+        when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
+
+        assertThat(controller.getAvailabilityStatus()).isEqualTo(
+                BasePreferenceController.AVAILABLE_UNSEARCHABLE);
+
+        capabilitiesAndConfig = createCapabilitiesAndConfig(false,
+                PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_DEGRADED_BY_SETTINGS,
+                PROVIDER_STATUS_IS_UNCERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS);
+        when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
+
+        assertThat(controller.getAvailabilityStatus()).isEqualTo(
+                BasePreferenceController.AVAILABLE_UNSEARCHABLE);
+
+        capabilitiesAndConfig = createCapabilitiesAndConfig(false,
+                PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK,
+                PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_DEGRADED_BY_SETTINGS);
+        when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
+
+        assertThat(controller.getAvailabilityStatus()).isEqualTo(
+                BasePreferenceController.AVAILABLE_UNSEARCHABLE);
     }
 
     @Test