1 /* 2 * Copyright (C) 2020 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; 23 24 import androidx.preference.Preference; 25 26 import com.android.settings.R; 27 import com.android.settings.core.BasePreferenceController; 28 import com.android.settings.utils.AnnotationSpan; 29 import com.android.settingslib.HelpUtils; 30 31 /** 32 * Class to show the footer that can't connect to 5G when device is in DSDS mode. 33 */ 34 public class NrDisabledInDsdsFooterPreferenceController extends BasePreferenceController { 35 private int mSubId; 36 37 /** 38 * Constructor. 39 */ NrDisabledInDsdsFooterPreferenceController(Context context, String preferenceKey)40 public NrDisabledInDsdsFooterPreferenceController(Context context, String preferenceKey) { 41 super(context, preferenceKey); 42 mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; 43 } 44 45 /** 46 * Init and specify a subId. 47 */ init(int subId)48 public void init(int subId) { 49 mSubId = subId; 50 } 51 52 @Override updateState(Preference preference)53 public void updateState(Preference preference) { 54 super.updateState(preference); 55 56 if (preference != null) { 57 // This is necessary to ensure that setting the title to the spannable string returned 58 // by getFooterText will be accepted. Internally, setTitle does an equality check on 59 // the spannable string being set to the text already set on the preference. That 60 // equality check apparently only takes into account the raw text and not and spannables 61 // that are part of the text. So we clear the title before applying the spannable 62 // footer to ensure it is accepted. 63 preference.setTitle(""); 64 preference.setTitle(getFooterText()); 65 } 66 } 67 getFooterText()68 private CharSequence getFooterText() { 69 final Intent helpIntent = HelpUtils.getHelpIntent(mContext, 70 mContext.getString(R.string.help_uri_5g_dsds), 71 mContext.getClass().getName()); 72 final AnnotationSpan.LinkInfo linkInfo = new AnnotationSpan.LinkInfo(mContext, 73 "url", helpIntent); 74 75 if (linkInfo.isActionable()) { 76 return AnnotationSpan.linkify(mContext.getText(R.string.no_5g_in_dsds_text), linkInfo); 77 } else { 78 return AnnotationSpan.textWithoutLink(mContext.getText(R.string.no_5g_in_dsds_text)); 79 } 80 } 81 82 @Override getAvailabilityStatus()83 public int getAvailabilityStatus() { 84 if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { 85 return CONDITIONALLY_UNAVAILABLE; 86 } 87 88 final TelephonyManager teleManager = ((TelephonyManager) 89 mContext.getSystemService(Context.TELEPHONY_SERVICE)) 90 .createForSubscriptionId(mSubId); 91 final SubscriptionManager subManager = ((SubscriptionManager) 92 mContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE)); 93 final int[] activeSubIdList = subManager.getActiveSubscriptionIdList(); 94 final int activeSubCount = activeSubIdList == null ? 0 : activeSubIdList.length; 95 // Show the footer only when DSDS is enabled, and mobile data is enabled on this SIM, and 96 // 5G is supported on this device. 97 if (teleManager.isDataEnabled() && activeSubCount >= 2 && is5GSupportedByRadio(teleManager) 98 && !teleManager.canConnectTo5GInDsdsMode()) { 99 return AVAILABLE; 100 } else { 101 return CONDITIONALLY_UNAVAILABLE; 102 } 103 } 104 is5GSupportedByRadio(TelephonyManager tm)105 private boolean is5GSupportedByRadio(TelephonyManager tm) { 106 return (tm.getSupportedRadioAccessFamily() & TelephonyManager.NETWORK_TYPE_BITMASK_NR) > 0; 107 } 108 } 109