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

Commit 13463578 authored by Priyanka Advani's avatar Priyanka Advani Committed by Android (Google) Code Review
Browse files

Merge "Revert "Create CallStateRepository.isInCallFlow"" into 24D1-dev

parents 9d689ab2 93e4c65d
Loading
Loading
Loading
Loading
+10 −13
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -17,19 +17,16 @@
package com.android.settings.network.telephony

import android.content.Context
import com.android.settings.network.SatelliteRepository
import android.telephony.TelephonyCallback
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine

class SubscriptionActivationRepository(
    private val context: Context,
    private val callStateRepository: CallStateRepository = CallStateRepository(context),
    private val satelliteRepository: SatelliteRepository = SatelliteRepository(context),
) {
    fun isActivationChangeableFlow(): Flow<Boolean> = combine(
        callStateRepository.isInCallFlow(),
        satelliteRepository.getIsSessionStartedFlow()
    ) { isInCall, isSatelliteModemEnabled ->
        !isInCall && !isSatelliteModemEnabled
/**
 * Flow for call state.
 */
fun Context.callStateFlow(subId: Int): Flow<Int> = telephonyCallbackFlow(subId) {
    object : TelephonyCallback(), TelephonyCallback.CallStateListener {
        override fun onCallStateChanged(state: Int) {
            trySend(state)
        }
    }
}
+0 −64
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.network.telephony

import android.content.Context
import android.telephony.TelephonyCallback
import android.telephony.TelephonyManager
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onEach

@OptIn(ExperimentalCoroutinesApi::class)
class CallStateRepository(private val context: Context) {
    private val subscriptionManager = context.requireSubscriptionManager()

    /** Flow for call state of given [subId]. */
    fun callStateFlow(subId: Int): Flow<Int> = context.telephonyCallbackFlow(subId) {
        object : TelephonyCallback(), TelephonyCallback.CallStateListener {
            override fun onCallStateChanged(state: Int) {
                trySend(state)
            }
        }
    }

    /**
     * Flow for in call state.
     *
     * @return true if any active subscription's call state is not idle.
     */
    fun isInCallFlow(): Flow<Boolean> = context.subscriptionsChangedFlow()
        .flatMapLatest {
            val subIds = subscriptionManager.activeSubscriptionIdList
            combine(subIds.map(::callStateFlow)) { states ->
                states.any { it != TelephonyManager.CALL_STATE_IDLE }
            }
        }
        .conflate()
        .flowOn(Dispatchers.Default)
        .onEach { Log.d(TAG, "isInCallFlow: $it") }

    private companion object {
        private const val TAG = "CallStateRepository"
    }
}
+5 −4
Original line number Diff line number Diff line
@@ -23,8 +23,10 @@ import android.telephony.TelephonyManager
import androidx.lifecycle.LifecycleOwner
import androidx.preference.Preference
import androidx.preference.PreferenceScreen
import com.android.settings.R
import com.android.settings.core.BasePreferenceController
import com.android.settings.network.SubscriptionUtil
import com.android.settings.network.telephony.MobileNetworkUtils
import com.android.settingslib.spa.framework.util.collectLatestWithLifecycle

/** This controls a preference allowing the user to delete the profile for an eSIM.  */
@@ -55,8 +57,7 @@ class DeleteSimProfilePreferenceController(context: Context, preferenceKey: Stri
    }

    override fun onViewCreated(viewLifecycleOwner: LifecycleOwner) {
        CallStateRepository(mContext).callStateFlow(subscriptionId)
            .collectLatestWithLifecycle(viewLifecycleOwner) {
        mContext.callStateFlow(subscriptionId).collectLatestWithLifecycle(viewLifecycleOwner) {
            preference.isEnabled = (it == TelephonyManager.CALL_STATE_IDLE)
        }
    }
+11 −3
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settings.network.telephony

import android.content.Context
import android.telephony.SubscriptionManager
import android.telephony.TelephonyManager
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
@@ -25,17 +26,19 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.android.settings.R
import com.android.settings.network.SatelliteRepository
import com.android.settings.network.SubscriptionUtil
import com.android.settings.spa.preference.ComposePreferenceController
import com.android.settingslib.spa.widget.preference.MainSwitchPreference
import com.android.settingslib.spa.widget.preference.SwitchPreferenceModel
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map

class MobileNetworkSwitchController @JvmOverloads constructor(
    context: Context,
    preferenceKey: String,
    private val subscriptionRepository: SubscriptionRepository = SubscriptionRepository(context),
    private val subscriptionActivationRepository: SubscriptionActivationRepository =
        SubscriptionActivationRepository(context),
    private val satelliteRepository: SatelliteRepository = SatelliteRepository(context)
) : ComposePreferenceController(context, preferenceKey) {

    private var subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID
@@ -54,7 +57,12 @@ class MobileNetworkSwitchController @JvmOverloads constructor(
            subscriptionRepository.isSubscriptionEnabledFlow(subId)
        }.collectAsStateWithLifecycle(initialValue = null)
        val changeable by remember {
            subscriptionActivationRepository.isActivationChangeableFlow()
            combine(
                context.callStateFlow(subId).map { it == TelephonyManager.CALL_STATE_IDLE },
                satelliteRepository.getIsSessionStartedFlow()
            ) { isCallStateIdle, isSatelliteModemEnabled ->
                isCallStateIdle && !isSatelliteModemEnabled
            }
        }.collectAsStateWithLifecycle(initialValue = true)
        MainSwitchPreference(model = object : SwitchPreferenceModel {
            override val title = stringResource(R.string.mobile_network_use_sim_on)
+3 −2
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import com.android.settings.R
import com.android.settings.network.telephony.wificalling.WifiCallingRepository
import com.android.settingslib.spa.framework.util.collectLatestWithLifecycle
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext

/**
@@ -40,7 +41,7 @@ import kotlinx.coroutines.withContext
open class WifiCallingPreferenceController @JvmOverloads constructor(
    context: Context,
    key: String,
    private val callStateRepository: CallStateRepository = CallStateRepository(context),
    private val callStateFlowFactory: (subId: Int) -> Flow<Int> = context::callStateFlow,
    private val wifiCallingRepositoryFactory: (subId: Int) -> WifiCallingRepository = { subId ->
        WifiCallingRepository(context, subId)
    },
@@ -90,7 +91,7 @@ open class WifiCallingPreferenceController @JvmOverloads constructor(
                if (isReady) update()
            }

        callStateRepository.callStateFlow(mSubId).collectLatestWithLifecycle(viewLifecycleOwner) {
        callStateFlowFactory(mSubId).collectLatestWithLifecycle(viewLifecycleOwner) {
            preference.isEnabled = (it == TelephonyManager.CALL_STATE_IDLE)
        }
    }
Loading