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.NetworkStats;
21 import android.net.NetworkTemplate;
22 import android.telephony.SubscriptionInfo;
23 import android.telephony.SubscriptionManager;
24 import android.telephony.TelephonyManager;
25 import android.util.Log;
26 
27 import com.android.internal.util.ArrayUtils;
28 
29 import java.util.List;
30 import java.util.Set;
31 
32 /**
33  * Utils class for data usage
34  */
35 public class DataUsageUtils {
36     private static final String TAG = "DataUsageUtils";
37 
38     /**
39      * Return mobile NetworkTemplate based on {@code subId}
40      */
getMobileTemplate(Context context, int subId)41     public static NetworkTemplate getMobileTemplate(Context context, int subId) {
42         final TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class);
43         final int mobileDefaultSubId = telephonyManager.getSubscriptionId();
44 
45         final SubscriptionManager subscriptionManager =
46                 context.getSystemService(SubscriptionManager.class);
47         final List<SubscriptionInfo> subInfoList =
48                 subscriptionManager.getAvailableSubscriptionInfoList();
49         if (subInfoList == null) {
50             Log.i(TAG, "Subscription is not inited: " + subId);
51             return getMobileTemplateForSubId(telephonyManager, mobileDefaultSubId);
52         }
53 
54         for (SubscriptionInfo subInfo : subInfoList) {
55             if ((subInfo != null) && (subInfo.getSubscriptionId() == subId)) {
56                 return getNormalizedMobileTemplate(telephonyManager, subId);
57             }
58         }
59         Log.i(TAG, "Subscription is not active: " + subId);
60         return getMobileTemplateForSubId(telephonyManager, mobileDefaultSubId);
61     }
62 
getNormalizedMobileTemplate( TelephonyManager telephonyManager, int subId)63     private static NetworkTemplate getNormalizedMobileTemplate(
64             TelephonyManager telephonyManager, int subId) {
65         final NetworkTemplate mobileTemplate = getMobileTemplateForSubId(telephonyManager, subId);
66         final Set<String> mergedSubscriberIds = Set.of(telephonyManager
67                 .createForSubscriptionId(subId).getMergedImsisFromGroup());
68         if (ArrayUtils.isEmpty(mergedSubscriberIds)) {
69             Log.i(TAG, "mergedSubscriberIds is null.");
70             return mobileTemplate;
71         }
72 
73         return normalizeMobileTemplate(mobileTemplate, mergedSubscriberIds);
74     }
75 
normalizeMobileTemplate( NetworkTemplate template, Set<String> mergedSet)76     private static NetworkTemplate normalizeMobileTemplate(
77             NetworkTemplate template, Set<String> mergedSet) {
78         if (template.getSubscriberIds().isEmpty()) return template;
79         // The input template should have at most 1 subscriberId.
80         final String subscriberId = template.getSubscriberIds().iterator().next();
81 
82         if (mergedSet.contains(subscriberId)) {
83             // Requested template subscriber is part of the merge group; return
84             // a template that matches all merged subscribers.
85             return new NetworkTemplate.Builder(template.getMatchRule())
86                     .setSubscriberIds(mergedSet)
87                     .setWifiNetworkKeys(template.getWifiNetworkKeys())
88                     .setMeteredness(NetworkStats.METERED_YES).build();
89         }
90 
91         return template;
92     }
93 
getMobileTemplateForSubId( TelephonyManager telephonyManager, int subId)94     private static NetworkTemplate getMobileTemplateForSubId(
95             TelephonyManager telephonyManager, int subId) {
96         // Create template that matches any mobile network when the subscriberId is null.
97         String subscriberId = telephonyManager.getSubscriberId(subId);
98         return subscriberId != null
99                 ? new NetworkTemplate.Builder(NetworkTemplate.MATCH_CARRIER)
100                 .setSubscriberIds(Set.of(subscriberId))
101                 .setMeteredness(NetworkStats.METERED_YES)
102                 .build()
103                 : new NetworkTemplate.Builder(NetworkTemplate.MATCH_MOBILE)
104                         .setMeteredness(NetworkStats.METERED_YES)
105                         .build();
106     }
107 }
108