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

Commit c5fc20d8 authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "Reduce api calls when getDataSummary" into main

parents 3576f484 dc02b5ea
Loading
Loading
Loading
Loading
+20 −25
Original line number Diff line number Diff line
@@ -51,11 +51,13 @@ class DataSubscriptionRepository(
            )
            .map { it.getIntExtra(SUBSCRIPTION_KEY, SubscriptionManager.INVALID_SUBSCRIPTION_ID) }
            .onStart { emit(SubscriptionManager.getDefaultDataSubscriptionId()) }
            .distinctUntilChanged()
            .conflate()
            .flowOn(Dispatchers.Default)

    fun activeDataSubscriptionIdFlow(): Flow<Int> =
        telephonyManager.telephonyCallbackFlow {
        telephonyManager
            .telephonyCallbackFlow {
                object : TelephonyCallback(), TelephonyCallback.ActiveDataSubscriptionIdListener {
                    override fun onActiveDataSubscriptionIdChanged(subId: Int) {
                        trySend(subId)
@@ -63,34 +65,27 @@ class DataSubscriptionRepository(
                    }
                }
            }
            .distinctUntilChanged()

    fun dataSummaryFlow(): Flow<String> =
        combine(defaultDataSubscriptionIdFlow(), activeDataSubscriptionIdFlow()) {
                defaultSubId,
                activeSubId ->
                DataSubscriptionIds(defaultSubId, activeSubId)
                defaultDataSubId,
                activeDataSubId ->
                getDataSummary(defaultDataSubId, activeDataSubId)
            }
            .distinctUntilChanged()
            .map { it.getDataSummary() }
            .conflate()
            .flowOn(Dispatchers.Default)

    private data class DataSubscriptionIds(
        val defaultSubId: Int,
        val activeSubId: Int,
    )

    private fun DataSubscriptionIds.getDataSummary(): String {
        val activeSubInfo = subscriptionManager.getActiveSubscriptionInfo(activeSubId) ?: return ""
    private fun getDataSummary(defaultDataSubId: Int, activeDataSubId: Int): String {
        if (defaultDataSubId == activeDataSubId) return getDisplayName(defaultDataSubId)
        val activeSubInfo =
            subscriptionManager.getActiveSubscriptionInfo(activeDataSubId)
                ?: return getDisplayName(defaultDataSubId)
        if (!SubscriptionUtil.isSubscriptionVisible(subscriptionManager, context, activeSubInfo)) {
            return getDisplayName(defaultSubId)
        }
        val uniqueName = getDisplayName(activeSubId)
        return if (activeSubId == defaultSubId) {
            uniqueName
        } else {
            context.getString(R.string.mobile_data_temp_using, uniqueName)
            return getDisplayName(defaultDataSubId)
        }
        // non-DDS is active
        return context.getString(R.string.mobile_data_temp_using, getDisplayName(activeDataSubId))
    }

    companion object {