• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settingslib.net;
18 
19 import android.content.Context;
20 import android.net.NetworkTemplate;
21 import android.telephony.SubscriptionInfo;
22 import android.telephony.SubscriptionManager;
23 import android.telephony.TelephonyManager;
24 import android.util.Log;
25 
26 import com.android.internal.util.ArrayUtils;
27 
28 import java.util.List;
29 
30 /**
31  * Utils class for data usage
32  */
33 public class DataUsageUtils {
34     private static final String TAG = "DataUsageUtils";
35 
36     /**
37      * Return mobile NetworkTemplate based on {@code subId}
38      */
getMobileTemplate(Context context, int subId)39     public static NetworkTemplate getMobileTemplate(Context context, int subId) {
40         final TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class);
41         final int mobileDefaultSubId = telephonyManager.getSubscriptionId();
42 
43         final SubscriptionManager subscriptionManager =
44                 context.getSystemService(SubscriptionManager.class);
45         final List<SubscriptionInfo> subInfoList =
46                 subscriptionManager.getAvailableSubscriptionInfoList();
47         if (subInfoList == null) {
48             Log.i(TAG, "Subscription is not inited: " + subId);
49             return getMobileTemplateForSubId(telephonyManager, mobileDefaultSubId);
50         }
51 
52         for (SubscriptionInfo subInfo : subInfoList) {
53             if ((subInfo != null) && (subInfo.getSubscriptionId() == subId)) {
54                 return getNormalizedMobileTemplate(telephonyManager, subId);
55             }
56         }
57         Log.i(TAG, "Subscription is not active: " + subId);
58         return getMobileTemplateForSubId(telephonyManager, mobileDefaultSubId);
59     }
60 
getNormalizedMobileTemplate( TelephonyManager telephonyManager, int subId)61     private static NetworkTemplate getNormalizedMobileTemplate(
62             TelephonyManager telephonyManager, int subId) {
63         final NetworkTemplate mobileTemplate = getMobileTemplateForSubId(telephonyManager, subId);
64         final String[] mergedSubscriberIds = telephonyManager
65                 .createForSubscriptionId(subId).getMergedImsisFromGroup();
66         if (ArrayUtils.isEmpty(mergedSubscriberIds)) {
67             Log.i(TAG, "mergedSubscriberIds is null.");
68             return mobileTemplate;
69         }
70 
71         return NetworkTemplate.normalize(mobileTemplate, mergedSubscriberIds);
72     }
73 
getMobileTemplateForSubId( TelephonyManager telephonyManager, int subId)74     private static NetworkTemplate getMobileTemplateForSubId(
75             TelephonyManager telephonyManager, int subId) {
76         // The null subscriberId means that no any mobile/carrier network will be matched.
77         // Using old API: buildTemplateMobileAll for the null subscriberId to avoid NPE.
78         String subscriberId = telephonyManager.getSubscriberId(subId);
79         return subscriberId != null ? NetworkTemplate.buildTemplateCarrierMetered(subscriberId)
80                 : NetworkTemplate.buildTemplateMobileAll(subscriberId);
81     }
82 }
83