Home | History | Annotate | Download | only in datausage
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
      5  * except in compliance with the License. You may obtain a copy of the License at
      6  *
      7  *      http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the
     10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     11  * KIND, either express or implied. See the License for the specific language governing
     12  * permissions and limitations under the License.
     13  */
     14 
     15 package com.android.settings.datausage;
     16 
     17 import static android.net.ConnectivityManager.TYPE_WIFI;
     18 
     19 import android.content.Context;
     20 import android.net.ConnectivityManager;
     21 import android.net.NetworkTemplate;
     22 import android.os.SystemProperties;
     23 import android.telephony.SubscriptionInfo;
     24 import android.telephony.SubscriptionManager;
     25 import android.telephony.TelephonyManager;
     26 import java.util.List;
     27 
     28 /**
     29  * Utility methods for data usage classes.
     30  */
     31 public final class DataUsageUtils {
     32     static final boolean TEST_RADIOS = false;
     33     static final String TEST_RADIOS_PROP = "test.radios";
     34 
     35     private DataUsageUtils() {
     36     }
     37 
     38     /**
     39      * Returns whether device has mobile data.
     40      * TODO: This is the opposite to Utils.isWifiOnly(), it should be refactored into 1 method.
     41      */
     42     public static boolean hasMobileData(Context context) {
     43         ConnectivityManager connectivityManager = ConnectivityManager.from(context);
     44         return connectivityManager != null && connectivityManager
     45                 .isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
     46     }
     47 
     48     /**
     49      * Whether device has a Wi-Fi data radio.
     50      */
     51     public static boolean hasWifiRadio(Context context) {
     52         if (TEST_RADIOS) {
     53             return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
     54         }
     55 
     56         ConnectivityManager connectivityManager = ConnectivityManager.from(context);
     57         return connectivityManager != null && connectivityManager.isNetworkSupported(TYPE_WIFI);
     58     }
     59 
     60     /**
     61      * Returns the default subscription if available else returns
     62      * SubscriptionManager#INVALID_SUBSCRIPTION_ID
     63      */
     64     public static int getDefaultSubscriptionId(Context context) {
     65         SubscriptionManager subManager = SubscriptionManager.from(context);
     66         if (subManager == null) {
     67             return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
     68         }
     69         SubscriptionInfo subscriptionInfo = subManager.getDefaultDataSubscriptionInfo();
     70         if (subscriptionInfo == null) {
     71             List<SubscriptionInfo> list = subManager.getAllSubscriptionInfoList();
     72             if (list.size() == 0) {
     73                 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
     74             }
     75             subscriptionInfo = list.get(0);
     76         }
     77         return subscriptionInfo.getSubscriptionId();
     78     }
     79 
     80     /**
     81      * Returns the default network template based on the availability of mobile data, Wifi. Returns
     82      * ethernet template if both mobile data and Wifi are not available.
     83      */
     84     static NetworkTemplate getDefaultTemplate(Context context, int defaultSubId) {
     85         if (hasMobileData(context) && defaultSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
     86             TelephonyManager telephonyManager = TelephonyManager.from(context);
     87             NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll(
     88                     telephonyManager.getSubscriberId(defaultSubId));
     89             return NetworkTemplate.normalize(mobileAll,
     90                     telephonyManager.getMergedSubscriberIds());
     91         } else if (hasWifiRadio(context)) {
     92             return NetworkTemplate.buildTemplateWifiWildcard();
     93         } else {
     94             return NetworkTemplate.buildTemplateEthernet();
     95         }
     96     }
     97 }
     98