From dc02b5ea593eb7ec25f7d244fc87676bf96d4a83 Mon Sep 17 00:00:00 2001 From: Chaohui Wang Date: Mon, 17 Jun 2024 14:00:03 +0800 Subject: [PATCH] Reduce api calls when getDataSummary When defaultSubId == activeSubId, return display name directly. Bug: 339884322 Flag: com.android.settings.flags.internet_preference_controller_v2 Test: manual - on Internet Change-Id: I8422c709483069a6894b99d5cc2cc652459ab5e0 --- .../telephony/DataSubscriptionRepository.kt | 45 +++++++++---------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/src/com/android/settings/network/telephony/DataSubscriptionRepository.kt b/src/com/android/settings/network/telephony/DataSubscriptionRepository.kt index 99f639b77c9..62e7e98df3c 100644 --- a/src/com/android/settings/network/telephony/DataSubscriptionRepository.kt +++ b/src/com/android/settings/network/telephony/DataSubscriptionRepository.kt @@ -51,46 +51,41 @@ class DataSubscriptionRepository( ) .map { it.getIntExtra(SUBSCRIPTION_KEY, SubscriptionManager.INVALID_SUBSCRIPTION_ID) } .onStart { emit(SubscriptionManager.getDefaultDataSubscriptionId()) } + .distinctUntilChanged() .conflate() .flowOn(Dispatchers.Default) fun activeDataSubscriptionIdFlow(): Flow = - telephonyManager.telephonyCallbackFlow { - object : TelephonyCallback(), TelephonyCallback.ActiveDataSubscriptionIdListener { - override fun onActiveDataSubscriptionIdChanged(subId: Int) { - trySend(subId) - Log.d(TAG, "activeDataSubscriptionIdFlow: $subId") + telephonyManager + .telephonyCallbackFlow { + object : TelephonyCallback(), TelephonyCallback.ActiveDataSubscriptionIdListener { + override fun onActiveDataSubscriptionIdChanged(subId: Int) { + trySend(subId) + Log.d(TAG, "activeDataSubscriptionIdFlow: $subId") + } } } - } + .distinctUntilChanged() fun dataSummaryFlow(): Flow = 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 {