1 /* <lambda>null2 * 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.SubscriptionManager 21 import android.telephony.TelephonyManager 22 import android.telephony.data.ApnSetting 23 import androidx.lifecycle.LifecycleOwner 24 import androidx.preference.PreferenceScreen 25 import com.android.settings.network.mobileDataEnabledFlow 26 import com.android.settingslib.spa.framework.util.collectLatestWithLifecycle 27 import kotlinx.coroutines.flow.combine 28 29 /** 30 * Preference controller for "MMS messages" 31 */ 32 class MmsMessagePreferenceController @JvmOverloads constructor( 33 context: Context, 34 key: String, 35 private val getDefaultDataSubId: () -> Int = { 36 SubscriptionManager.getDefaultDataSubscriptionId() 37 }, 38 ) : TelephonyTogglePreferenceController(context, key) { 39 40 private lateinit var telephonyManager: TelephonyManager 41 42 private var preferenceScreen: PreferenceScreen? = null 43 initnull44 fun init(subId: Int) { 45 mSubId = subId 46 telephonyManager = mContext.getSystemService(TelephonyManager::class.java)!! 47 .createForSubscriptionId(subId) 48 } 49 getAvailabilityStatusnull50 override fun getAvailabilityStatus(subId: Int) = 51 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && 52 this::telephonyManager.isInitialized && 53 !telephonyManager.isDataEnabled && 54 telephonyManager.isApnMetered(ApnSetting.TYPE_MMS) && 55 !isFallbackDataEnabled() 56 ) AVAILABLE else CONDITIONALLY_UNAVAILABLE 57 58 private fun isFallbackDataEnabled(): Boolean { 59 val defaultDataSubId = getDefaultDataSubId() 60 return defaultDataSubId != mSubId && 61 telephonyManager.createForSubscriptionId(defaultDataSubId).isDataEnabled && 62 telephonyManager.isMobileDataPolicyEnabled( 63 TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH 64 ) 65 } 66 displayPreferencenull67 override fun displayPreference(screen: PreferenceScreen) { 68 super.displayPreference(screen) 69 preferenceScreen = screen 70 } 71 onViewCreatednull72 override fun onViewCreated(viewLifecycleOwner: LifecycleOwner) { 73 combine( 74 mContext.mobileDataEnabledFlow(mSubId), 75 mContext.subscriptionsChangedFlow(), // Capture isMobileDataPolicyEnabled() changes 76 ) { _, _ -> }.collectLatestWithLifecycle(viewLifecycleOwner) { 77 preferenceScreen?.let { super.displayPreference(it) } 78 } 79 } 80 isCheckednull81 override fun isChecked(): Boolean = telephonyManager.isMobileDataPolicyEnabled( 82 TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED 83 ) 84 85 override fun setChecked(isChecked: Boolean): Boolean { 86 telephonyManager.setMobileDataPolicyEnabled( 87 TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED, 88 isChecked, 89 ) 90 return true 91 } 92 } 93