import android.content.Context;
import android.net.NetworkTemplate;
-import android.os.ParcelUuid;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;
-import java.util.ArrayList;
-import java.util.List;
-
/**
* Utils class for data usage
*/
*/
public static NetworkTemplate getMobileTemplate(Context context, int subId) {
final TelephonyManager telephonyManager = context.getSystemService(
- TelephonyManager.class);
+ TelephonyManager.class).createForSubscriptionId(subId);
final SubscriptionManager subscriptionManager = context.getSystemService(
SubscriptionManager.class);
final SubscriptionInfo info = subscriptionManager.getActiveSubscriptionInfo(subId);
Log.i(TAG, "Subscription is not active: " + subId);
return mobileAll;
}
- final ParcelUuid groupUuid = info.getGroupUuid();
- if (groupUuid == null) {
- Log.i(TAG, "Subscription doesn't have valid group uuid: " + subId);
- return mobileAll;
- }
- // Otherwise merge other subscriberId to create new NetworkTemplate
- final List<SubscriptionInfo> groupInfos = subscriptionManager.getSubscriptionsInGroup(
- groupUuid);
- final List<String> mergedSubscriberIds = new ArrayList<>();
- for (SubscriptionInfo subInfo : groupInfos) {
- final String subscriberId = telephonyManager.getSubscriberId(
- subInfo.getSubscriptionId());
- if (subscriberId != null) {
- mergedSubscriberIds.add(subscriberId);
- }
- }
- return NetworkTemplate.normalize(mobileAll, mergedSubscriberIds.toArray(new String[0]));
+ // Use old API to build networkTemplate
+ return NetworkTemplate.normalize(mobileAll, telephonyManager.getMergedSubscriberIds());
}
}
public class DataUsageUtilsTest {
private static final int SUB_ID = 1;
- private static final int SUB_ID_2 = 2;
private static final String SUBSCRIBER_ID = "Test Subscriber";
private static final String SUBSCRIBER_ID_2 = "Test Subscriber 2";
mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
+ when(mTelephonyManager.createForSubscriptionId(SUB_ID)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
when(mTelephonyManager.getSubscriberId(SUB_ID)).thenReturn(SUBSCRIBER_ID);
- when(mTelephonyManager.getSubscriberId(SUB_ID_2)).thenReturn(SUBSCRIBER_ID_2);
- when(mInfo1.getSubscriptionId()).thenReturn(SUB_ID);
- when(mInfo2.getSubscriptionId()).thenReturn(SUB_ID_2);
+ when(mTelephonyManager.getMergedSubscriberIds()).thenReturn(
+ new String[]{SUBSCRIBER_ID, SUBSCRIBER_ID_2});
mInfos = new ArrayList<>();
mInfos.add(mInfo1);
}
@Test
- public void getMobileTemplate_groupUuidNull_returnMobileAll() {
- when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(mInfo1);
- when(mInfo1.getGroupUuid()).thenReturn(null);
-
- final NetworkTemplate networkTemplate = DataUsageUtils.getMobileTemplate(mContext, SUB_ID);
- assertThat(networkTemplate.matchesSubscriberId(SUBSCRIBER_ID)).isTrue();
- assertThat(networkTemplate.matchesSubscriberId(SUBSCRIBER_ID_2)).isFalse();
- }
-
- @Test
- public void getMobileTemplate_groupUuidExist_returnMobileMerged() {
+ public void getMobileTemplate_infoExisted_returnMobileMerged() {
when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(mInfo1);
when(mInfo1.getGroupUuid()).thenReturn(mParcelUuid);