Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ac56a425 authored by Malcolm Chen's avatar Malcolm Chen
Browse files

Provide @hide API to return merged IMSIs from grouping.

Bug: 135105735
Test: test app. Apply grouping and call the new API, to make sure
it returns correct values.

Change-Id: I99586976d3421d4b35be465201ee33cbb2dfd1d9
parent 00b4e26e
Loading
Loading
Loading
Loading
+12 −6
Original line number Diff line number Diff line
@@ -18,11 +18,11 @@ package com.android.settingslib.net;

import android.content.Context;
import android.net.NetworkTemplate;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.android.internal.util.ArrayUtils;
/**
 * Utils class for data usage
 */
@@ -34,19 +34,25 @@ public class DataUsageUtils {
     */
    public static NetworkTemplate getMobileTemplate(Context context, int subId) {
        final TelephonyManager telephonyManager = context.getSystemService(
                TelephonyManager.class).createForSubscriptionId(subId);
                TelephonyManager.class);
        final SubscriptionManager subscriptionManager = context.getSystemService(
                SubscriptionManager.class);
        final SubscriptionInfo info = subscriptionManager.getActiveSubscriptionInfo(subId);
        final NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll(
                telephonyManager.getSubscriberId(subId));

        if (info == null) {
        if (!subscriptionManager.isActiveSubId(subId)) {
            Log.i(TAG, "Subscription is not active: " + subId);
            return mobileAll;
        }

        // Use old API to build networkTemplate
        return NetworkTemplate.normalize(mobileAll, telephonyManager.getMergedSubscriberIds());
        final String[] mergedSubscriberIds = telephonyManager.createForSubscriptionId(subId)
                .getMergedSubscriberIdsFromGroup();

        if (ArrayUtils.isEmpty(mergedSubscriberIds)) {
            Log.i(TAG, "mergedSubscriberIds is null.");
            return mobileAll;
        }

        return NetworkTemplate.normalize(mobileAll, mergedSubscriberIds);
    }
}
+21 −11
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settingslib.net;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

@@ -37,13 +38,13 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

import java.util.ArrayList;
import java.util.List;

@RunWith(RobolectricTestRunner.class)
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";

@@ -66,21 +67,28 @@ public class DataUsageUtilsTest {

        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.getMergedSubscriberIds()).thenReturn(
                new String[]{SUBSCRIBER_ID, SUBSCRIBER_ID_2});

        mInfos = new ArrayList<>();
        mInfos.add(mInfo1);
        mInfos.add(mInfo2);
        when(mSubscriptionManager.getSubscriptionsInGroup(mParcelUuid)).thenReturn(mInfos);
        when(mTelephonyManager.getSubscriberId(SUB_ID_2)).thenReturn(SUBSCRIBER_ID_2);
        when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mTelephonyManager);
        when(mSubscriptionManager.isActiveSubId(anyInt())).thenReturn(true);
    }

    @Test
    public void getMobileTemplate_infoNull_returnMobileAll() {
        when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(null);
        when(mSubscriptionManager.isActiveSubId(SUB_ID)).thenReturn(false);

        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_groupUuidNull_returnMobileAll() {
        when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(mInfo1);
        when(mInfo1.getGroupUuid()).thenReturn(null);
        when(mTelephonyManager.getMergedSubscriberIdsFromGroup())
                .thenReturn(new String[] {SUBSCRIBER_ID});

        final NetworkTemplate networkTemplate = DataUsageUtils.getMobileTemplate(mContext, SUB_ID);
        assertThat(networkTemplate.matchesSubscriberId(SUBSCRIBER_ID)).isTrue();
@@ -88,9 +96,11 @@ public class DataUsageUtilsTest {
    }

    @Test
    public void getMobileTemplate_infoExisted_returnMobileMerged() {
    public void getMobileTemplate_groupUuidExist_returnMobileMerged() {
        when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(mInfo1);
        when(mInfo1.getGroupUuid()).thenReturn(mParcelUuid);
        when(mTelephonyManager.getMergedSubscriberIdsFromGroup())
                .thenReturn(new String[] {SUBSCRIBER_ID, SUBSCRIBER_ID_2});

        final NetworkTemplate networkTemplate = DataUsageUtils.getMobileTemplate(mContext, SUB_ID);
        assertThat(networkTemplate.matchesSubscriberId(SUBSCRIBER_ID)).isTrue();
+27 −0
Original line number Diff line number Diff line
@@ -3972,9 +3972,14 @@ public class TelephonyManager {
     * The returned set of subscriber IDs will include the subscriber ID corresponding to this
     * TelephonyManager's subId.
     *
     * This is deprecated and {@link #getMergedSubscriberIdsFromGroup()} should be used for data
     * usage merging purpose.
     * TODO: remove this API.
     *
     * @hide
     */
    @UnsupportedAppUsage
    @Deprecated
    public @Nullable String[] getMergedSubscriberIds() {
        try {
            ITelephony telephony = getITelephony();
@@ -3986,6 +3991,28 @@ public class TelephonyManager {
        return null;
    }

    /**
     * Return the set of subscriber IDs that should be considered "merged together" for data usage
     * purposes. Unlike {@link #getMergedSubscriberIds()} this API merge subscriberIds based on
     * subscription grouping: subscriberId of those in the same group will all be returned.
     *
     * <p>Requires the calling app to have READ_PRIVILEGED_PHONE_STATE permission.
     *
     * @hide
     */
    @RequiresPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
    public @Nullable String[] getMergedSubscriberIdsFromGroup() {
        try {
            ITelephony telephony = getITelephony();
            if (telephony != null) {
                return telephony.getMergedSubscriberIdsFromGroup(getSubId(), getOpPackageName());
            }
        } catch (RemoteException ex) {
        } catch (NullPointerException ex) {
        }
        return null;
    }

    /**
     * Returns the MSISDN string.
     * for a GSM phone. Return null if it is unavailable.
+5 −0
Original line number Diff line number Diff line
@@ -1058,6 +1058,11 @@ interface ITelephony {
     */
    String[] getMergedSubscriberIds(int subId, String callingPackage);

    /**
     * @hide
     */
    String[] getMergedSubscriberIdsFromGroup(int subId, String callingPackage);

    /**
     * Override the operator branding for the current ICCID.
     *