Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 758e3828 authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Disable app data usage if all time usage is 0

Otherwise user can open an empty App data usage page.

Bug: 292346951
Test: manual - on Mobile Settings
Test: unit test
Change-Id: I63c3e06af2606c3e2017f2440addb2ac2d9d775b
parent 429291b7
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -11467,8 +11467,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] -->
+1 −1
Original line number Diff line number Diff line
@@ -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
+11 −13
Original line number Diff line number Diff line
@@ -81,17 +81,13 @@ class DataUsagePreferenceController(context: Context, key: String) :
    }

    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.isEnabled = enabled
        preference.summary = summary
    }
    }

    private fun getNetworkTemplate(): NetworkTemplate? = when {
        SubscriptionManager.isValidSubscriptionId(mSubId) -> {
@@ -105,17 +101,19 @@ class DataUsagePreferenceController(context: Context, key: String) :
    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
    }
}
+21 −1
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE
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 @@ class DataUsagePreferenceControllerTest {
    }

    @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())

@@ -152,6 +156,22 @@ class DataUsagePreferenceControllerTest {
        waitUntil { preference.summary?.contains("0 B used") == true }
    }

    @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))