• 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.Intent
21 import android.telephony.SubscriptionManager
22 import android.telephony.TelephonyManager.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS
23 import android.util.Log
24 import com.android.settings.Utils
25 import com.android.settings.flags.Flags
26 import com.android.settings.network.SatelliteRepository
27 import com.android.settings.network.SimOnboardingActivity.Companion.startSimOnboardingActivity
28 import kotlinx.coroutines.Dispatchers
29 import kotlinx.coroutines.flow.Flow
30 import kotlinx.coroutines.flow.combine
31 import kotlinx.coroutines.withContext
32 
33 class SubscriptionActivationRepository(
34     private val context: Context,
35     private val callStateRepository: CallStateRepository = CallStateRepository(context),
36     private val satelliteRepository: SatelliteRepository = SatelliteRepository(context),
37 ) {
38     fun isActivationChangeableFlow(): Flow<Boolean> = combine(
39         callStateRepository.isInCallFlow(),
40         satelliteRepository.getIsSessionStartedFlow()
41     ) { isInCall, isSatelliteModemEnabled ->
42         !isInCall && !isSatelliteModemEnabled
43     }
44 
45     /**
46      * Starts a dialog activity to handle SIM enabling / disabling.
47      * @param subId The id of subscription need to be enabled or disabled.
48      * @param active Whether the subscription with [subId] should be enabled or disabled.
49      */
50     suspend fun setActive(subId: Int, active: Boolean) {
51         if (!SubscriptionManager.isUsableSubscriptionId(subId)) {
52             Log.i(TAG, "Unable to toggle subscription due to unusable subscription ID.")
53             return
54         }
55         if (isEmergencyCallbackMode(subId)) {
56             val intent = Intent(ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS).apply {
57                 setPackage(Utils.PHONE_PACKAGE_NAME)
58             }
59             context.startActivity(intent)
60             return
61         }
62         if (active && Flags.isDualSimOnboardingEnabled()) {
63             startSimOnboardingActivity(context, subId)
64             return
65         }
66         context.startActivity(ToggleSubscriptionDialogActivity.getIntent(context, subId, active))
67     }
68 
69     private suspend fun isEmergencyCallbackMode(subId: Int) = withContext(Dispatchers.Default) {
70         context.telephonyManager(subId).emergencyCallbackMode
71     }
72 
73     private companion object {
74         private const val TAG = "SubscriptionActivationR"
75     }
76 }
77