1 /*
2  * 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.telephony.AccessNetworkConstants
21 import android.telephony.NetworkRegistrationInfo
22 import android.telephony.TelephonyManager
23 import androidx.lifecycle.Lifecycle
24 import androidx.lifecycle.LifecycleOwner
25 import androidx.lifecycle.lifecycleScope
26 import androidx.lifecycle.repeatOnLifecycle
27 import kotlinx.coroutines.Dispatchers
28 import kotlinx.coroutines.launch
29 import kotlinx.coroutines.withContext
30 
31 class NetworkSelectRepository(context: Context, subId: Int) {
32     private val telephonyManager =
33         context.getSystemService(TelephonyManager::class.java)!!.createForSubscriptionId(subId)
34 
35     data class NetworkRegistrationAndForbiddenInfo(
36         val networkList: List<NetworkRegistrationInfo>,
37         val forbiddenPlmns: List<String>,
38     )
39 
40     /** TODO: Move this to UI layer, when UI layer migrated to Kotlin. */
launchUpdateNetworkRegistrationInfonull41     fun launchUpdateNetworkRegistrationInfo(
42         lifecycleOwner: LifecycleOwner,
43         action: (NetworkRegistrationAndForbiddenInfo) -> Unit,
44     ) {
45         lifecycleOwner.lifecycleScope.launch {
46             lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
47                 withContext(Dispatchers.Default) {
48                     getNetworkRegistrationInfo()
49                 }?.let(action)
50             }
51         }
52     }
53 
getNetworkRegistrationInfonull54     fun getNetworkRegistrationInfo(): NetworkRegistrationAndForbiddenInfo? {
55         if (telephonyManager.dataState != TelephonyManager.DATA_CONNECTED) return null
56         // Try to get the network registration states
57         val serviceState = telephonyManager.serviceState ?: return null
58         val networkList = serviceState.getNetworkRegistrationInfoListForTransportType(
59             AccessNetworkConstants.TRANSPORT_TYPE_WWAN
60         )
61         if (networkList.isEmpty()) return null
62         // Due to the aggregation of cell between carriers, it's possible to get CellIdentity
63         // containing forbidden PLMN.
64         // Getting current network from ServiceState is no longer a good idea.
65         // Add an additional rule to avoid from showing forbidden PLMN to the user.
66         return NetworkRegistrationAndForbiddenInfo(networkList, getForbiddenPlmns())
67     }
68 
69     /**
70      * Update forbidden PLMNs from the USIM App
71      */
getForbiddenPlmnsnull72     private fun getForbiddenPlmns(): List<String> {
73         return telephonyManager.forbiddenPlmns?.toList() ?: emptyList()
74     }
75 }
76