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