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

Commit e8495069 authored by Sungcheol Ahn's avatar Sungcheol Ahn
Browse files

[satellite] Retrigger provisioning when phone number changes

Test: atest SatelliteManagerTest
Bug: 349674502
Flag: com.android.internal.telephony.flags.carrier_roaming_nb_iot_ntn
Change-Id: Iffacbb5b561200b7681d50924c9a1374458b8cee
parent 2c68d050
Loading
Loading
Loading
Loading
+51 −25
Original line number Diff line number Diff line
@@ -480,14 +480,17 @@ public class SatelliteController extends Handler {
    private Map<String, Boolean> mProvisionedSubscriberId = new HashMap<>();
    // key : subscriberId, value : subId
    @GuardedBy("mSatelliteTokenProvisionedLock")
    private Map<String, Integer> mSubscriberIdPerSub = new HashMap<>();
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
    protected Map<String, Integer> mSubscriberIdPerSub = new HashMap<>();
    // key : priority, low value is high, value : List<SubscriptionInfo>
    @GuardedBy("mSatelliteTokenProvisionedLock")
    private Map<Integer, List<SubscriptionInfo>> mSubsInfoListPerPriority = new HashMap<>();
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
    protected Map<Integer, List<SubscriptionInfo>> mSubsInfoListPerPriority = new HashMap<>();
    // The last ICC ID that framework configured to modem.
    @GuardedBy("mSatelliteTokenProvisionedLock")
    private String mLastConfiguredIccId;
    @NonNull private final Object mSatelliteTokenProvisionedLock = new Object();
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
    @NonNull protected final Object mSatelliteTokenProvisionedLock = new Object();
    private long mWaitTimeForSatelliteEnablingResponse;
    private long mDemoPointingAlignedDurationMillis;
    private long mDemoPointingNotAlignedDurationMillis;
@@ -523,6 +526,7 @@ public class SatelliteController extends Handler {
    private final Object mIsWifiConnectedLock = new Object();
    @GuardedBy("mIsWifiConnectedLock")
    private boolean mIsWifiConnected = false;
    private boolean mHasSentBroadcast = false;
    private BroadcastReceiver
            mDefaultSmsSubscriptionChangedBroadcastReceiver = new BroadcastReceiver() {
                @Override
@@ -5643,19 +5647,23 @@ public class SatelliteController extends Handler {
     * 3. Among active carrier eSOS profiles user selected(default SMS SIM) eSOS profile will be
     * the highest priority.
     */
    private void evaluateESOSProfilesPrioritization() {
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    protected void evaluateESOSProfilesPrioritization() {
        if (!mFeatureFlags.carrierRoamingNbIotNtn()) {
            plogd("evaluateESOSProfilesPrioritization: Flag CarrierRoamingNbIotNtn is disabled");
            return;
        }
        boolean isChanged = false;
        List<SubscriptionInfo> allSubInfos = mSubscriptionManagerService.getAllSubInfoList(
                mContext.getOpPackageName(), mContext.getAttributionTag());
        // Key : priority - lower value has higher priority; Value : List<SubscriptionInfo>
        Map<Integer, List<SubscriptionInfo>> newSubsInfoListPerPriority = new HashMap<>();
        synchronized (mSatelliteTokenProvisionedLock) {
            for (SubscriptionInfo info : allSubInfos) {
                int subId = info.getSubscriptionId();
                boolean isActive = info.isActive();
            boolean isDefaultSmsSubId = mSubscriptionManagerService.getDefaultSmsSubId() == subId;
                boolean isDefaultSmsSubId =
                        mSubscriptionManagerService.getDefaultSmsSubId() == subId;
                boolean isNtnOnly = info.isOnlyNonTerrestrialNetwork();
                boolean isESOSSupported = info.isSatelliteESOSSupported();
                if (!isNtnOnly && !isESOSSupported) {
@@ -5672,18 +5680,35 @@ public class SatelliteController extends Handler {
                    plogw("evaluateESOSProfilesPrioritization: Got -1 keyPriority for subId="
                            + info.getSubscriptionId());
                }

                Pair<String, Integer> subscriberIdPair = getSubscriberIdAndType(info);
                String newSubscriberId = subscriberIdPair.first;
                Optional<String> oldSubscriberId = mSubscriberIdPerSub.entrySet().stream()
                        .filter(entry -> entry.getValue().equals(subId))
                        .map(Map.Entry::getKey).findFirst();

                if (oldSubscriberId.isPresent()
                        && !newSubscriberId.equals(oldSubscriberId.get())) {
                    mSubscriberIdPerSub.remove(oldSubscriberId.get());
                    mProvisionedSubscriberId.remove(oldSubscriberId.get());
                    logd("Old phone number is removed: id = " + subId);
                    isChanged = true;
                }
            }
        }

        if (newSubsInfoListPerPriority.size() == 0) {
        if (!mHasSentBroadcast && newSubsInfoListPerPriority.size() == 0) {
            logd("evaluateESOSProfilesPrioritization: no satellite subscription available");
            return;
        }

        // If priority has changed, send broadcast for provisioned ESOS subs IDs
        synchronized (mSatelliteTokenProvisionedLock) {
            if (isPriorityChanged(mSubsInfoListPerPriority, newSubsInfoListPerPriority)) {
            if (isPriorityChanged(mSubsInfoListPerPriority, newSubsInfoListPerPriority)
                    || isChanged) {
                mSubsInfoListPerPriority = newSubsInfoListPerPriority;
                sendBroadCastForProvisionedESOSSubs();
                mHasSentBroadcast = true;
            }
        }
    }
@@ -5765,7 +5790,8 @@ public class SatelliteController extends Handler {
        logd("sendBroadCaseToProvisionedESOSSubs" + intent);
    }

    private String getStringFromOverlayConfig(int resourceId) {
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
    protected String getStringFromOverlayConfig(int resourceId) {
        String name;
        try {
            name = mContext.getResources().getString(resourceId);
+147 −0
Original line number Diff line number Diff line
@@ -72,6 +72,7 @@ import static com.android.internal.telephony.satellite.SatelliteController.SATEL
import static com.android.internal.telephony.satellite.SatelliteController.SATELLITE_MODE_ENABLED_TRUE;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@@ -101,6 +102,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.AsyncResult;
@@ -174,6 +176,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Semaphore;
@@ -4385,6 +4388,130 @@ public class SatelliteControllerTest extends TelephonyTest {
        return list;
    }

    @Test
    public void testCheckForSubscriberIdChange_noChanged() {
        when(mFeatureFlags.carrierRoamingNbIotNtn()).thenReturn(true);

        String imsi = "012345";
        String oldMsisdn = "1234567890";
        String newMsisdn = "1234567890";
        List<SubscriptionInfo> allSubInfos = new ArrayList<>();
        Optional<String> getSubscriberId;
        SubscriptionInfoInternal subInfoInternal =
                new SubscriptionInfoInternal.Builder().setCarrierId(0)
                        .setImsi(imsi).build();

        when(mSubscriptionInfo.getSubscriptionId()).thenReturn(SUB_ID);
        allSubInfos.add(mSubscriptionInfo);
        doReturn(" ").when(mContext).getOpPackageName();
        doReturn(" ").when(mContext).getAttributionTag();
        when(mMockSubscriptionManagerService.getAllSubInfoList(anyString(), anyString()))
                .thenReturn(allSubInfos);
        when(mSubscriptionInfo.isSatelliteESOSSupported()).thenReturn(true);
        when(mMockSubscriptionManagerService.getSubscriptionInfoInternal(SUB_ID))
                .thenReturn(subInfoInternal);

        try {
            Field field = SatelliteController.class.getDeclaredField("mInjectSubscriptionManager");
            field.setAccessible(true);
            field.set(mSatelliteControllerUT, mSubscriptionManager);
        } catch (Exception e) {
            loge("Exception InjectSubscriptionManager e: " + e);
        }
        when(mSubscriptionManager.getPhoneNumber(SUB_ID)).thenReturn(newMsisdn);
        when(mSubscriptionInfo.isOnlyNonTerrestrialNetwork()).thenReturn(false);
        mSatelliteControllerUT.subscriberIdPerSub().put(imsi + oldMsisdn, SUB_ID);

        getSubscriberId = mSatelliteControllerUT.subscriberIdPerSub().entrySet().stream()
                .filter(entry -> entry.getValue().equals(SUB_ID))
                .map(Map.Entry::getKey).findFirst();
        assertEquals(imsi + newMsisdn, getSubscriberId.get());

        setComponentName();
        mSatelliteControllerUT.subsInfoListPerPriority().computeIfAbsent(
                        getKeyPriority(mSubscriptionInfo), k -> new ArrayList<>())
                .add(mSubscriptionInfo);
        mSatelliteControllerUT.evaluateESOSProfilesPrioritizationTest();
        // Verify that broadcast has not been sent.
        verify(mContext, times(0)).sendBroadcast(any(Intent.class));
    }

    @Test
    public void testCheckForSubscriberIdChange_changed() {
        when(mFeatureFlags.carrierRoamingNbIotNtn()).thenReturn(true);
        List<SubscriptionInfo> allSubInfos = new ArrayList<>();

        String imsi = "012345";
        String oldMsisdn = "1234567890";
        String newMsisdn = "4567891234";

        Optional<String> getSubscriberId;
        SubscriptionInfoInternal subInfoInternal =
                new SubscriptionInfoInternal.Builder().setCarrierId(0).setImsi(imsi).build();

        when(mSubscriptionInfo.getSubscriptionId()).thenReturn(SUB_ID);
        allSubInfos.add(mSubscriptionInfo);
        doReturn(" ").when(mContext).getOpPackageName();
        doReturn(" ").when(mContext).getAttributionTag();
        when(mMockSubscriptionManagerService.getAllSubInfoList(anyString(), anyString()))
                .thenReturn(allSubInfos);

        when(mSubscriptionInfo.isSatelliteESOSSupported()).thenReturn(true);
        when(mMockSubscriptionManagerService.getSubscriptionInfoInternal(SUB_ID))
                .thenReturn(subInfoInternal);

        try {
            Field field = SatelliteController.class.getDeclaredField("mInjectSubscriptionManager");
            field.setAccessible(true);
            field.set(mSatelliteControllerUT, mSubscriptionManager);
        } catch (Exception e) {
            loge("Exception InjectSubscriptionManager e: " + e);
        }
        when(mSubscriptionManager.getPhoneNumber(SUB_ID)).thenReturn(newMsisdn);
        when(mSubscriptionInfo.isOnlyNonTerrestrialNetwork()).thenReturn(false);
        mSatelliteControllerUT.subscriberIdPerSub().put(imsi + oldMsisdn, SUB_ID);

        getSubscriberId = mSatelliteControllerUT.subscriberIdPerSub().entrySet().stream()
                .filter(entry -> entry.getValue().equals(SUB_ID))
                .map(Map.Entry::getKey).findFirst();
        assertNotEquals(imsi + newMsisdn, getSubscriberId.get());

        setComponentName();
        mSatelliteControllerUT.subsInfoListPerPriority().computeIfAbsent(
                        getKeyPriority(mSubscriptionInfo), k -> new ArrayList<>())
                .add(mSubscriptionInfo);
        mSatelliteControllerUT.evaluateESOSProfilesPrioritizationTest();
        // Verify that broadcast has been sent.
        verify(mContext, times(1)).sendBroadcast(any(Intent.class));
    }

    private void setComponentName() {
        when(mSatelliteControllerUT.getStringFromOverlayConfigTest(
                R.string.config_satellite_gateway_service_package))
                .thenReturn("com.example.package");
        when(mSatelliteControllerUT.getStringFromOverlayConfigTest(
                R.string.config_satellite_carrier_roaming_esos_provisioned_class))
                .thenReturn("com.example.class");
    }

    private int getKeyPriority(SubscriptionInfo subscriptionInfo) {
        boolean isActive = subscriptionInfo.isActive();
        boolean isNtnOnly = subscriptionInfo.isOnlyNonTerrestrialNetwork();
        boolean isESOSSupported = subscriptionInfo.isSatelliteESOSSupported();

        int keyPriority;
        if (isESOSSupported && isActive) {
            keyPriority = 1;
        } else if (isNtnOnly) {
            keyPriority = 2;
        } else if (isESOSSupported) {
            keyPriority = 3;
        } else {
            keyPriority = -1;
        }
        return keyPriority;
    }

    private void resetSatelliteControllerUTEnabledState() {
        logd("resetSatelliteControllerUTEnabledState");
        setUpResponseForRequestIsSatelliteSupported(false, SATELLITE_RESULT_RADIO_NOT_AVAILABLE);
@@ -5168,5 +5295,25 @@ public class SatelliteControllerTest extends TelephonyTest {
        public boolean isWaitForCellularModemOffTimerStarted() {
            return hasMessages(EVENT_WAIT_FOR_CELLULAR_MODEM_OFF_TIMED_OUT);
        }

        public Map<String, Integer> subscriberIdPerSub() {
            synchronized (mSatelliteTokenProvisionedLock) {
                return mSubscriberIdPerSub;
            }
        }

        public Map<Integer, List<SubscriptionInfo>> subsInfoListPerPriority() {
            synchronized (mSatelliteTokenProvisionedLock) {
                return mSubsInfoListPerPriority;
            }
        }

        public void evaluateESOSProfilesPrioritizationTest() {
            evaluateESOSProfilesPrioritization();
        }

        public String getStringFromOverlayConfigTest(int resourceId) {
            return getStringFromOverlayConfig(resourceId);
        }
    }
}