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

Commit a2195018 authored by Shuo Qian's avatar Shuo Qian Committed by Grace Jia
Browse files

Migrate Default Phone Account Handle

Migrate the phone account handle ID from IccID (if any) to SubId when
 default phone account handle is loaded from the database in Telecom.

Test: atest PhoneAccountRegistrarTest
Bug: 185235527
Change-Id: Ifbbee5ec5e8123156d1e424ec266c0d52471eb6c
parent 6bff4281
Loading
Loading
Loading
Loading
+47 −1
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.telephony.CarrierConfigManager;
import android.telephony.PhoneNumberUtils;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
@@ -1413,7 +1414,7 @@ public class PhoneAccountRegistrar {

        public final UserHandle userHandle;

        public final PhoneAccountHandle phoneAccountHandle;
        public PhoneAccountHandle phoneAccountHandle;

        public final String groupId;

@@ -1555,6 +1556,7 @@ public class PhoneAccountRegistrar {
            XmlPullParser parser = Xml.resolvePullParser(is);
            parser.nextTag();
            mState = readFromXml(parser, mContext);
            migratePhoneAccountHandle(mState);
            versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;

        } catch (IOException | XmlPullParserException e) {
@@ -1599,6 +1601,50 @@ public class PhoneAccountRegistrar {
        return s != null ? s : new State();
    }

    /**
     * Try to migrate the ID of default phone account handle from IccId to SubId.
     */
    @VisibleForTesting
    public void migratePhoneAccountHandle(State state) {
        if (mSubscriptionManager == null) {
            return;
        }
        // Use getAllSubscirptionInfoList() to get the mapping between iccId and subId
        // from the subscription database
        List<SubscriptionInfo> subscriptionInfos = mSubscriptionManager
                .getAllSubscriptionInfoList();
        Map<UserHandle, DefaultPhoneAccountHandle> defaultPhoneAccountHandles
                = state.defaultOutgoingAccountHandles;
        for (Map.Entry<UserHandle, DefaultPhoneAccountHandle> entry
                : defaultPhoneAccountHandles.entrySet()) {
            DefaultPhoneAccountHandle defaultPhoneAccountHandle = entry.getValue();

            // Migrate Telephony PhoneAccountHandle only
            String telephonyComponentName =
                    "com.android.phone/com.android.services.telephony.TelephonyConnectionService";
            if (!defaultPhoneAccountHandle.phoneAccountHandle.getComponentName()
                    .flattenToString().equals(telephonyComponentName)) {
                continue;
            }
            // Migrate from IccId to SubId
            for (SubscriptionInfo subscriptionInfo : subscriptionInfos) {
                String phoneAccountHandleId = defaultPhoneAccountHandle.phoneAccountHandle.getId();
                // Some phone account handle would store phone account handle id with the IccId
                // string plus "F", and the getIccId() returns IccId string itself without "F",
                // so here need to use "startsWith" to match.
                if (phoneAccountHandleId != null && phoneAccountHandleId.startsWith(
                        subscriptionInfo.getIccId())) {
                    Log.i(this, "Found subscription ID to migrate: "
                            + subscriptionInfo.getSubscriptionId());
                    defaultPhoneAccountHandle.phoneAccountHandle = new PhoneAccountHandle(
                            defaultPhoneAccountHandle.phoneAccountHandle.getComponentName(),
                                    Integer.toString(subscriptionInfo.getSubscriptionId()));
                    break;
                }
            }
        }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////
    //
    // XML serialization
+5 −1
Original line number Diff line number Diff line
@@ -540,7 +540,7 @@ public class ComponentContextFixture implements TestFixture<Context> {
    private final NotificationManager mNotificationManager = mock(NotificationManager.class);
    private final UserManager mUserManager = mock(UserManager.class);
    private final StatusBarManager mStatusBarManager = mock(StatusBarManager.class);
    private final SubscriptionManager mSubscriptionManager = mock(SubscriptionManager.class);
    private SubscriptionManager mSubscriptionManager = mock(SubscriptionManager.class);
    private final CarrierConfigManager mCarrierConfigManager = mock(CarrierConfigManager.class);
    private final CountryDetector mCountryDetector = mock(CountryDetector.class);
    private final Map<String, IContentProvider> mIContentProviderByUri = new HashMap<>();
@@ -735,6 +735,10 @@ public class ComponentContextFixture implements TestFixture<Context> {
        mTelecomManager = telecomManager;
    }

    public void setSubscriptionManager(SubscriptionManager subscriptionManager) {
        mSubscriptionManager = subscriptionManager;
    }

    public TelephonyManager getTelephonyManager() {
        return mTelephonyManager;
    }
+47 −0
Original line number Diff line number Diff line
@@ -51,6 +51,8 @@ import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.test.suitebuilder.annotation.SmallTest;
import android.test.suitebuilder.annotation.MediumTest;
import android.util.Xml;
@@ -83,6 +85,7 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -98,6 +101,7 @@ public class PhoneAccountRegistrarTest extends TelecomTestCase {
    private final String COMPONENT_NAME = "com.android.server.telecom.tests.MockConnectionService";
    private final TelecomSystem.SyncRoot mLock = new TelecomSystem.SyncRoot() { };
    private PhoneAccountRegistrar mRegistrar;
    @Mock private SubscriptionManager mSubscriptionManager;
    @Mock private TelecomManager mTelecomManager;
    @Mock private DefaultDialerCache mDefaultDialerCache;
    @Mock private AppLabelProxy mAppLabelProxy;
@@ -108,6 +112,7 @@ public class PhoneAccountRegistrarTest extends TelecomTestCase {
        super.setUp();
        MockitoAnnotations.initMocks(this);
        mComponentContextFixture.setTelecomManager(mTelecomManager);
        mComponentContextFixture.setSubscriptionManager(mSubscriptionManager);
        new File(
                mComponentContextFixture.getTestDouble().getApplicationContext().getFilesDir(),
                FILE_NAME)
@@ -1226,6 +1231,29 @@ public class PhoneAccountRegistrarTest extends TelecomTestCase {
        clearInvocations(mComponentContextFixture.getTelephonyManager());
    }

    /**
     * Test PhoneAccountHandle Migration Logic.
     */
    @Test
    public void testPhoneAccountMigration() throws Exception {
        PhoneAccountRegistrar.State testState = makeQuickStateWithTelephonyPhoneAccountHandle();
        final int mTestPhoneAccountHandleSubIdInt = 123;
        // Mock SubscriptionManager
        SubscriptionInfo subscriptionInfo = new SubscriptionInfo(
                mTestPhoneAccountHandleSubIdInt, "id0", 1, "a", "b", 1, 1, "test",
                        1, null, null, null, null, false, null, null);
        List<SubscriptionInfo> subscriptionInfoList = new ArrayList<>();
        subscriptionInfoList.add(subscriptionInfo);
        when(mSubscriptionManager.getAllSubscriptionInfoList()).thenReturn(subscriptionInfoList);
        mRegistrar.migratePhoneAccountHandle(testState);
        Collection<DefaultPhoneAccountHandle> defaultPhoneAccountHandles
                = testState.defaultOutgoingAccountHandles.values();
        DefaultPhoneAccountHandle defaultPhoneAccountHandle
                = defaultPhoneAccountHandles.iterator().next();
        assertEquals(Integer.toString(mTestPhoneAccountHandleSubIdInt),
                defaultPhoneAccountHandle.phoneAccountHandle.getId());
    }

    private static ComponentName makeQuickConnectionServiceComponentName() {
        return new ComponentName(
                "com.android.server.telecom.tests",
@@ -1447,6 +1475,25 @@ public class PhoneAccountRegistrarTest extends TelecomTestCase {
        }
    }

    private PhoneAccountRegistrar.State makeQuickStateWithTelephonyPhoneAccountHandle() {
        PhoneAccountRegistrar.State s = new PhoneAccountRegistrar.State();
        s.accounts.add(makeQuickAccount("id0", 0));
        s.accounts.add(makeQuickAccount("id1", 1));
        s.accounts.add(makeQuickAccount("id2", 2));
        PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(new ComponentName(
                "com.android.phone",
                        "com.android.services.telephony.TelephonyConnectionService"), "id0");
        UserHandle userHandle = phoneAccountHandle.getUserHandle();
        when(UserManager.get(mContext).getSerialNumberForUser(userHandle))
            .thenReturn(0L);
        when(UserManager.get(mContext).getUserForSerialNumber(0L))
            .thenReturn(userHandle);
        s.defaultOutgoingAccountHandles
            .put(userHandle, new DefaultPhoneAccountHandle(userHandle, phoneAccountHandle,
                "testGroup"));
        return s;
    }

    private PhoneAccountRegistrar.State makeQuickState() {
        PhoneAccountRegistrar.State s = new PhoneAccountRegistrar.State();
        s.accounts.add(makeQuickAccount("id0", 0));