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.deviceinfo.simstatus
18 
19 import android.content.Context
20 import android.telephony.ServiceState
21 import android.telephony.SignalStrength
22 import android.telephony.TelephonyCallback
23 import android.util.Log
24 import com.android.settings.R
25 import com.android.settings.network.telephony.serviceStateFlow
26 import com.android.settings.network.telephony.telephonyCallbackFlow
27 import com.android.settingslib.Utils
28 import kotlinx.coroutines.Dispatchers
29 import kotlinx.coroutines.ExperimentalCoroutinesApi
30 import kotlinx.coroutines.flow.Flow
31 import kotlinx.coroutines.flow.conflate
32 import kotlinx.coroutines.flow.flatMapLatest
33 import kotlinx.coroutines.flow.flowOf
34 import kotlinx.coroutines.flow.flowOn
35 import kotlinx.coroutines.flow.map
36 
37 @OptIn(ExperimentalCoroutinesApi::class)
38 class SignalStrengthRepository(
39     private val context: Context,
40     private val serviceStateFlowFactory: (subId: Int) -> Flow<ServiceState> = {
41         context.serviceStateFlow(it)
42     },
43 ) {
signalStrengthDisplayFlownull44     fun signalStrengthDisplayFlow(subId: Int): Flow<String> =
45         serviceStateFlowFactory(subId).flatMapLatest { serviceState ->
46             if (Utils.isInService(serviceState)) {
47                 signalStrengthFlow(subId).map { it.displayString() }
48             } else {
49                 flowOf("0")
50             }
51         }.conflate().flowOn(Dispatchers.Default)
52 
53     /** Creates an instance of a cold Flow for [SignalStrength] of given [subId]. */
signalStrengthFlownull54     private fun signalStrengthFlow(subId: Int): Flow<SignalStrength> =
55         context.telephonyCallbackFlow(subId) {
56             object : TelephonyCallback(), TelephonyCallback.SignalStrengthsListener {
57                 override fun onSignalStrengthsChanged(signalStrength: SignalStrength) {
58                     trySend(signalStrength)
59                     val cellSignalStrengths = signalStrength.cellSignalStrengths
60                     Log.d(TAG, "[$subId] onSignalStrengthsChanged: $cellSignalStrengths")
61                 }
62             }
63         }
64 
displayStringnull65     private fun SignalStrength.displayString() =
66         context.getString(R.string.sim_signal_strength, signalDbm(), signalAsu())
67 
68     private companion object {
69         private const val TAG = "SignalStrengthRepo"
70 
71 
72         private fun SignalStrength.signalDbm(): Int =
73             cellSignalStrengths.firstOrNull { it.dbm != -1 }?.dbm ?: 0
74 
75         private fun SignalStrength.signalAsu(): Int =
76             cellSignalStrengths.firstOrNull { it.asuLevel != -1 }?.asuLevel ?: 0
77     }
78 }
79