1 /* 2 * Copyright (C) 2019 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.car.settings.network; 18 19 import android.content.Context; 20 import android.net.ConnectivityManager; 21 import android.net.Network; 22 import android.net.NetworkCapabilities; 23 import android.telephony.SubscriptionInfo; 24 import android.telephony.SubscriptionManager; 25 import android.telephony.TelephonyManager; 26 27 import com.android.internal.telephony.flags.Flags; 28 29 import java.util.List; 30 31 /** Provides helpful utilities surrounding network related tasks. */ 32 public final class NetworkUtils { 33 NetworkUtils()34 private NetworkUtils() { 35 } 36 37 /** Returns {@code true} if device has a mobile network. */ hasMobileNetwork(ConnectivityManager connectivityManager)38 public static boolean hasMobileNetwork(ConnectivityManager connectivityManager) { 39 Network[] networks = connectivityManager.getAllNetworks(); 40 for (Network network : networks) { 41 NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network); 42 if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { 43 return true; 44 } 45 } 46 return false; 47 } 48 49 /** Returns {@code true} if device has a sim card. */ hasSim(TelephonyManager telephonyManager)50 public static boolean hasSim(TelephonyManager telephonyManager) { 51 int simState = telephonyManager.getSimState(); 52 53 // Note that pulling out the SIM card returns UNKNOWN, not ABSENT. 54 return simState != TelephonyManager.SIM_STATE_ABSENT 55 && simState != TelephonyManager.SIM_STATE_UNKNOWN; 56 } 57 58 /** 59 * Sets the mobile data enabled state based on {@code enabled} for the subscription defined by 60 * {@code subId}. If {@code disableOtherSubscriptions} is set, other subscriptions will be 61 * disabled unless they are opportunistic. 62 */ setMobileDataEnabled(Context context, int subId, boolean enabled, boolean disableOtherSubscriptions)63 public static void setMobileDataEnabled(Context context, int subId, boolean enabled, 64 boolean disableOtherSubscriptions) { 65 TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class) 66 .createForSubscriptionId(subId); 67 SubscriptionManager subscriptionManager = context.getSystemService( 68 SubscriptionManager.class); 69 if (Flags.workProfileApiSplit()) { 70 subscriptionManager = subscriptionManager.createForAllUserProfiles(); 71 } 72 telephonyManager.setDataEnabled(enabled); 73 74 if (disableOtherSubscriptions) { 75 List<SubscriptionInfo> subInfoList = 76 subscriptionManager.getActiveSubscriptionInfoList(); 77 if (subInfoList != null) { 78 for (SubscriptionInfo subInfo : subInfoList) { 79 // We never disable mobile data for opportunistic subscriptions. 80 if (subInfo.getSubscriptionId() != subId && !subInfo.isOpportunistic()) { 81 context.getSystemService(TelephonyManager.class).createForSubscriptionId( 82 subInfo.getSubscriptionId()).setDataEnabled(false); 83 } 84 } 85 } 86 } 87 } 88 } 89