1 /*
2  * Copyright (C) 2023 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.SubscriptionInfo
21 import android.telephony.SubscriptionManager
22 import android.telephony.TelephonyManager
23 import androidx.lifecycle.LifecycleOwner
24 import androidx.preference.Preference
25 import androidx.preference.PreferenceScreen
26 import com.android.settings.core.BasePreferenceController
27 import com.android.settings.network.SubscriptionUtil
28 import com.android.settingslib.spa.framework.util.collectLatestWithLifecycle
29 
30 /** This controls a preference allowing the user to delete the profile for an eSIM.  */
31 class DeleteSimProfilePreferenceController(context: Context, preferenceKey: String) :
32     BasePreferenceController(context, preferenceKey) {
33     private var subscriptionId: Int = SubscriptionManager.INVALID_SUBSCRIPTION_ID
34     private var carrierId: Int = TelephonyManager.UNKNOWN_CARRIER_ID
35     private var subscriptionInfo: SubscriptionInfo? = null
36     private lateinit var preference: Preference
37 
initnull38     fun init(subscriptionId: Int) {
39         this.subscriptionId = subscriptionId
40         subscriptionInfo = SubscriptionUtil.getAvailableSubscriptions(mContext)
41             .find { it.subscriptionId == subscriptionId && it.isEmbedded }
42         subscriptionInfo?.let {
43             carrierId = it.carrierId
44         }
45     }
46 
getAvailabilityStatusnull47     override fun getAvailabilityStatus() = when (subscriptionInfo) {
48         null -> CONDITIONALLY_UNAVAILABLE
49         else -> AVAILABLE
50     }
51 
displayPreferencenull52     override fun displayPreference(screen: PreferenceScreen) {
53         super.displayPreference(screen)
54         preference = screen.findPreference(preferenceKey)!!
55     }
56 
onViewCreatednull57     override fun onViewCreated(viewLifecycleOwner: LifecycleOwner) {
58         CallStateRepository(mContext).callStateFlow(subscriptionId)
59             .collectLatestWithLifecycle(viewLifecycleOwner) {
60                 preference.isEnabled = (it == TelephonyManager.CALL_STATE_IDLE)
61             }
62     }
63 
handlePreferenceTreeClicknull64     override fun handlePreferenceTreeClick(preference: Preference): Boolean {
65         if (preference.key != preferenceKey) return false
66 
67         MobileNetworkUtils.showLockScreen(mContext) { deleteSim() }
68 
69         return true
70     }
71 
deleteSimnull72     private fun deleteSim() {
73         SubscriptionUtil.startDeleteEuiccSubscriptionDialogActivity(mContext, subscriptionId,
74                 carrierId)
75         // result handled in MobileNetworkSettings
76     }
77 }
78