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