• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.network.telephony
18 
19 import android.content.Context
20 import android.content.IntentFilter
21 import android.telephony.SubscriptionManager
22 import android.telephony.TelephonyCallback
23 import android.telephony.TelephonyManager
24 import android.util.Log
25 import androidx.annotation.VisibleForTesting
26 import com.android.settings.R
27 import com.android.settings.network.SubscriptionUtil
28 import com.android.settingslib.spaprivileged.framework.common.broadcastReceiverFlow
29 import kotlinx.coroutines.Dispatchers
30 import kotlinx.coroutines.flow.Flow
31 import kotlinx.coroutines.flow.combine
32 import kotlinx.coroutines.flow.conflate
33 import kotlinx.coroutines.flow.distinctUntilChanged
34 import kotlinx.coroutines.flow.flowOn
35 import kotlinx.coroutines.flow.map
36 import kotlinx.coroutines.flow.onEach
37 import kotlinx.coroutines.flow.onStart
38 
39 class DataSubscriptionRepository(
40     private val context: Context,
41     private val getDisplayName: (subId: Int) -> String = { subId ->
42         SubscriptionUtil.getUniqueSubscriptionDisplayName(subId, context).toString()
43     },
44 ) {
45     private val telephonyManager = context.getSystemService(TelephonyManager::class.java)!!
46     private val subscriptionManager = context.requireSubscriptionManager()
47 
defaultDataSubscriptionIdFlownull48     fun defaultDataSubscriptionIdFlow(): Flow<Int> =
49         context
50             .broadcastReceiverFlow(
51                 IntentFilter(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED)
52             )
53             .map { it.getIntExtra(SUBSCRIPTION_KEY, SubscriptionManager.INVALID_SUBSCRIPTION_ID) }
<lambda>null54             .onStart { emit(SubscriptionManager.getDefaultDataSubscriptionId()) }
55             .distinctUntilChanged()
56             .conflate()
<lambda>null57             .onEach { Log.d(TAG, "defaultDataSubscriptionIdFlow: $it") }
58             .flowOn(Dispatchers.Default)
59 
activeDataSubscriptionIdFlownull60     fun activeDataSubscriptionIdFlow(): Flow<Int> =
61         telephonyManager
62             .telephonyCallbackFlow {
63                 object : TelephonyCallback(), TelephonyCallback.ActiveDataSubscriptionIdListener {
64                     override fun onActiveDataSubscriptionIdChanged(subId: Int) {
65                         trySend(subId)
66                         Log.d(TAG, "activeDataSubscriptionIdFlow: $subId")
67                     }
68                 }
69             }
70             .distinctUntilChanged()
71 
dataSummaryFlownull72     fun dataSummaryFlow(): Flow<String> =
73         combine(defaultDataSubscriptionIdFlow(), activeDataSubscriptionIdFlow()) {
74                 defaultDataSubId,
75                 activeDataSubId ->
76                 getDataSummary(defaultDataSubId, activeDataSubId)
77             }
78             .conflate()
79             .flowOn(Dispatchers.Default)
80 
getDataSummarynull81     private fun getDataSummary(defaultDataSubId: Int, activeDataSubId: Int): String {
82         if (defaultDataSubId == activeDataSubId) return getDisplayName(defaultDataSubId)
83         val activeSubInfo =
84             subscriptionManager.getActiveSubscriptionInfo(activeDataSubId)
85                 ?: return getDisplayName(defaultDataSubId)
86         if (!SubscriptionUtil.isSubscriptionVisible(subscriptionManager, context, activeSubInfo)) {
87             return getDisplayName(defaultDataSubId)
88         }
89         // non-DDS is active
90         return context.getString(R.string.mobile_data_temp_using, getDisplayName(activeDataSubId))
91     }
92 
93     companion object {
94         private const val TAG = "DataSubscriptionRepo"
95 
96         @VisibleForTesting const val SUBSCRIPTION_KEY = "subscription"
97     }
98 }
99