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 18 19 import android.content.Context 20 import android.provider.Settings 21 import androidx.lifecycle.LifecycleOwner 22 import androidx.preference.Preference 23 import androidx.preference.PreferenceScreen 24 import com.android.settings.R 25 import com.android.settings.core.BasePreferenceController 26 import com.android.settings.dashboard.DashboardFragment 27 import com.android.settings.network.telephony.SimRepository 28 import com.android.settings.overlay.FeatureFactory.Companion.featureFactory 29 import com.android.settings.spa.network.startAddSimFlow 30 import com.android.settings.spa.network.startSatelliteWarningDialogFlow 31 import com.android.settingslib.RestrictedPreference 32 import com.android.settingslib.spa.framework.util.collectLatestWithLifecycle 33 import com.android.settingslib.spaprivileged.settingsprovider.settingsGlobalBooleanFlow 34 import kotlinx.coroutines.flow.Flow 35 36 /** 37 * This controls the summary text and click behavior of the "Mobile network" item on the Network & 38 * internet page. There are 2 separate cases depending on the number of mobile network 39 * subscriptions: 40 * - No subscription: click action begins a UI flow to add a network subscription, and the summary 41 * text indicates this 42 * - Has subscriptions: click action takes you to a page listing the subscriptions, and the summary 43 * text gives the count of SIMs 44 */ 45 // LINT.IfChange 46 class MobileNetworkSummaryController 47 @JvmOverloads 48 constructor( 49 private val context: Context, 50 preferenceKey: String, 51 private val repository: MobileNetworkSummaryRepository = 52 MobileNetworkSummaryRepository(context), 53 private val airplaneModeOnFlow: Flow<Boolean> = 54 context.settingsGlobalBooleanFlow(Settings.Global.AIRPLANE_MODE_ON), 55 private val satelliteIsStartedFlow: Flow<Boolean> = SatelliteRepository(context).getIsSessionStartedFlow() 56 ) : BasePreferenceController(context, preferenceKey) { 57 private val metricsFeatureProvider = featureFactory.metricsFeatureProvider 58 private var preference: RestrictedPreference? = null 59 60 private var isAirplaneModeOn = false 61 private var isSatelliteOn = false 62 getAvailabilityStatusnull63 override fun getAvailabilityStatus() = 64 if (SimRepository(mContext).showMobileNetworkPageEntrance()) AVAILABLE 65 else CONDITIONALLY_UNAVAILABLE 66 67 override fun displayPreference(screen: PreferenceScreen) { 68 super.displayPreference(screen) 69 preference = screen.findPreference(preferenceKey) 70 } 71 onViewCreatednull72 override fun onViewCreated(viewLifecycleOwner: LifecycleOwner) { 73 repository 74 .subscriptionsStateFlow() 75 .collectLatestWithLifecycle(viewLifecycleOwner, action = ::update) 76 airplaneModeOnFlow.collectLatestWithLifecycle(viewLifecycleOwner) { 77 isAirplaneModeOn = it 78 updateEnabled() 79 } 80 satelliteIsStartedFlow.collectLatestWithLifecycle(viewLifecycleOwner) { 81 isSatelliteOn = it 82 } 83 } 84 updatenull85 private fun update(state: MobileNetworkSummaryRepository.SubscriptionsState) { 86 val preference = preference ?: return 87 preference.onPreferenceClickListener = null 88 preference.fragment = null 89 when (state) { 90 MobileNetworkSummaryRepository.AddNetwork -> { 91 preference.summary = 92 context.getString(R.string.mobile_network_summary_add_a_network) 93 preference.onPreferenceClickListener = 94 Preference.OnPreferenceClickListener { 95 logPreferenceClick() 96 if (isSatelliteOn) 97 startSatelliteWarningDialogFlow(context) 98 else 99 startAddSimFlow(context) 100 true 101 } 102 } 103 104 MobileNetworkSummaryRepository.NoSubscriptions -> { 105 preference.summary = null 106 } 107 108 is MobileNetworkSummaryRepository.HasSubscriptions -> { 109 preference.summary = state.displayNames.joinToString(", ") 110 preference.fragment = MobileNetworkListFragment::class.java.canonicalName 111 } 112 } 113 updateEnabled() 114 } 115 updateEnablednull116 private fun updateEnabled() { 117 val preference = preference ?: return 118 if (preference.isDisabledByAdmin) return 119 preference.isEnabled = 120 (preference.onPreferenceClickListener != null || preference.fragment != null) && 121 !isAirplaneModeOn 122 } 123 logPreferenceClicknull124 private fun logPreferenceClick() { 125 val preference = preference ?: return 126 metricsFeatureProvider.logClickedPreference( 127 preference, 128 preference.extras.getInt(DashboardFragment.CATEGORY), 129 ) 130 } 131 } 132 // LINT.ThenChange(MobileNetworkListScreen.kt) 133