Merge "Handle listing of private space apps in data usage settings" into main
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 2b78458..0f87d87 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -11483,8 +11483,6 @@
     <!-- Mobile network settings screen, message asking the user to check their pricing with their Carrier, when enabling Data roaming. [CHAR LIMIT=NONE] -->
     <string name="roaming_check_price_warning">Check with your network provider for pricing.</string>
 
-    <!-- Title for mobile data preference, to display the mobile data usage for each app. [CHAR LIMIT=NONE]-->
-    <string name="mobile_data_usage_title">App data usage</string>
     <!-- Summary to show the current network mode is invalid. [CHAR LIMIT=NONE]-->
     <string name="mobile_network_mode_error">Invalid Network Mode <xliff:g id="networkModeId" example="0">%1$d</xliff:g>. Ignore.</string>
     <!-- Title for _satellite_setting_preference_layout in mobile network settings [CHAR LIMIT=60] -->
diff --git a/res/xml/mobile_network_settings.xml b/res/xml/mobile_network_settings.xml
index a29e123..1e43ef0 100644
--- a/res/xml/mobile_network_settings.xml
+++ b/res/xml/mobile_network_settings.xml
@@ -95,7 +95,7 @@
 
         <Preference
             android:key="data_usage_summary"
-            android:title="@string/mobile_data_usage_title"
+            android:title="@string/app_cellular_data_usage"
             settings:controller="com.android.settings.network.telephony.DataUsagePreferenceController"/>
 
         <com.android.settings.datausage.BillingCyclePreference
diff --git a/src/com/android/settings/network/telephony/DataUsagePreferenceController.kt b/src/com/android/settings/network/telephony/DataUsagePreferenceController.kt
index 6ee6909..240843d 100644
--- a/src/com/android/settings/network/telephony/DataUsagePreferenceController.kt
+++ b/src/com/android/settings/network/telephony/DataUsagePreferenceController.kt
@@ -81,16 +81,12 @@
     }
 
     private suspend fun update() {
-        val summary = withContext(Dispatchers.Default) {
+        val (summary, enabled) = withContext(Dispatchers.Default) {
             networkTemplate = getNetworkTemplate()
-            getDataUsageSummary()
+            getDataUsageSummaryAndEnabled()
         }
-        if (summary == null) {
-            preference.isEnabled = false
-        } else {
-            preference.isEnabled = true
-            preference.summary = summary
-        }
+        preference.isEnabled = enabled
+        preference.summary = summary
     }
 
     private fun getNetworkTemplate(): NetworkTemplate? = when {
@@ -105,17 +101,19 @@
     fun createNetworkCycleDataRepository(): NetworkCycleDataRepository? =
         networkTemplate?.let { NetworkCycleDataRepository(mContext, it) }
 
-    private fun getDataUsageSummary(): String? {
-        val repository = createNetworkCycleDataRepository() ?: return null
+    private fun getDataUsageSummaryAndEnabled(): Pair<String?, Boolean> {
+        val repository = createNetworkCycleDataRepository() ?: return null to false
+
         repository.loadFirstCycle()?.let { usageData ->
             return mContext.getString(
                 R.string.data_usage_template,
                 usageData.formatUsage(mContext),
                 usageData.formatDateRange(mContext),
-            )
+            ) to (usageData.usage > 0 || repository.queryUsage(AllTimeRange).usage > 0)
         }
 
-        return repository.queryUsage(AllTimeRange).takeIf { it.usage > 0 }
-            ?.getDataUsedString(mContext)
+        val allTimeUsage = repository.queryUsage(AllTimeRange)
+        if (allTimeUsage.usage > 0) return allTimeUsage.getDataUsedString(mContext) to true
+        return null to false
     }
 }
diff --git a/tests/spa_unit/src/com/android/settings/network/telephony/DataUsagePreferenceControllerTest.kt b/tests/spa_unit/src/com/android/settings/network/telephony/DataUsagePreferenceControllerTest.kt
index 6cd9151..5f80855 100644
--- a/tests/spa_unit/src/com/android/settings/network/telephony/DataUsagePreferenceControllerTest.kt
+++ b/tests/spa_unit/src/com/android/settings/network/telephony/DataUsagePreferenceControllerTest.kt
@@ -33,6 +33,7 @@
 import com.android.settings.datausage.DataUsageUtils
 import com.android.settings.datausage.lib.DataUsageLib
 import com.android.settings.datausage.lib.NetworkCycleDataRepository
+import com.android.settings.datausage.lib.NetworkStatsRepository.Companion.AllTimeRange
 import com.android.settings.datausage.lib.NetworkUsageData
 import com.android.settingslib.spa.testutils.waitUntil
 import com.google.common.truth.Truth.assertThat
@@ -140,11 +141,14 @@
     }
 
     @Test
-    fun updateState_noUsageData_shouldEnablePreference() = runBlocking {
+    fun updateState_noFistCycleUsageButOtherUsage_shouldEnablePreference() = runBlocking {
         val usageData = NetworkUsageData(START_TIME, END_TIME, 0L)
         repository.stub {
             on { loadFirstCycle() } doReturn usageData
+            on { queryUsage(AllTimeRange) } doReturn
+                NetworkUsageData(AllTimeRange.lower, AllTimeRange.upper, 1L)
         }
+        preference.isEnabled = false
 
         controller.onViewCreated(TestLifecycleOwner())
 
@@ -153,6 +157,22 @@
     }
 
     @Test
+    fun updateState_noDataUsage_shouldDisablePreference() = runBlocking {
+        val usageData = NetworkUsageData(START_TIME, END_TIME, 0L)
+        repository.stub {
+            on { loadFirstCycle() } doReturn usageData
+            on { queryUsage(AllTimeRange) } doReturn
+                NetworkUsageData(AllTimeRange.lower, AllTimeRange.upper, 0L)
+        }
+        preference.isEnabled = true
+
+        controller.onViewCreated(TestLifecycleOwner())
+
+        waitUntil { !preference.isEnabled }
+        waitUntil { preference.summary?.contains("0 B used") == true }
+    }
+
+    @Test
     fun updateState_shouldUseIecUnit() = runBlocking {
         val usageData = NetworkUsageData(START_TIME, END_TIME, DataUnit.MEBIBYTES.toBytes(1))
         repository.stub {