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

Commit 906eb70a authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Avoid unnecessary ArrayList copy." into rvc-qpr-dev

parents 7f50d1a0 d9cb2cd6
Loading
Loading
Loading
Loading
+18 −16
Original line number Diff line number Diff line
@@ -3910,8 +3910,14 @@ public class SubscriptionController extends ISub.Stub {
        }
    }

    // Helper function of getOpportunisticSubscriptions and getActiveSubscriptionInfoList.
    // They are doing similar things except operating on different cache.
    /**
     * Helper function of getOpportunisticSubscriptions and getActiveSubscriptionInfoList.
     * They are doing similar things except operating on different cache.
     *
     * NOTE: the cacheSubList passed in is a *copy* of mCacheActiveSubInfoList or
     * mCacheOpportunisticSubInfoList, so mSubInfoListLock is not required to access it. Also, this
     * method may modify cacheSubList depending on the permissions the caller has.
     */
    private List<SubscriptionInfo> getSubscriptionInfoListFromCacheHelper(
            String callingPackage, String callingFeatureId, List<SubscriptionInfo> cacheSubList) {
        boolean canReadPhoneState = false;
@@ -3944,30 +3950,26 @@ public class SubscriptionController extends ISub.Stub {
            return cacheSubList;
        }
        // Filter the list to only include subscriptions which the caller can manage.
        List<SubscriptionInfo> subscriptions = new ArrayList<>(cacheSubList.size());
        for (SubscriptionInfo subscriptionInfo : cacheSubList) {
        for (int subIndex = cacheSubList.size() - 1; subIndex >= 0; subIndex--) {
            SubscriptionInfo subscriptionInfo = cacheSubList.get(subIndex);

            int subId = subscriptionInfo.getSubscriptionId();
            boolean hasCarrierPrivileges = TelephonyPermissions.checkCarrierPrivilegeForSubId(
                    mContext, subId);
            // If the caller does not have the READ_PHONE_STATE permission nor carrier
            // privileges then they cannot access the current subscription.
            if (!canReadPhoneState && !hasCarrierPrivileges) {
                continue;
            }
            // If the caller has carrier privileges then they are granted access to all
            // identifiers for their subscription.
            if (hasCarrierPrivileges) {
                subscriptions.add(subscriptionInfo);
            } else {
            if (hasCarrierPrivileges) continue;

            cacheSubList.remove(subIndex);
            if (canReadPhoneState) {
                // The caller does not have carrier privileges for this subId, filter the
                // identifiers in the subscription based on the results of the initial
                // permission checks.
                subscriptions.add(
                        conditionallyRemoveIdentifiers(subscriptionInfo, canReadIdentifiers,
                                canReadPhoneNumber));
                cacheSubList.add(subIndex, conditionallyRemoveIdentifiers(
                        subscriptionInfo, canReadIdentifiers, canReadPhoneNumber));
            }
        }
        return subscriptions;
        return cacheSubList;
    }

    /**
+23 −0
Original line number Diff line number Diff line
@@ -1300,6 +1300,29 @@ public class SubscriptionControllerTest extends TelephonyTest {
        assertEquals(DISPLAY_NUMBER, subInfo.getNumber());
    }

    @Test
    public void testGetActiveSubscriptionInfoListCheckOrder()
            throws Exception {
        // If an app does not have the READ_PHONE_STATE permission but has carrier privileges on one
        // out of multiple sub IDs then the SubscriptionInfo for that subId should be returned with
        // the ICC ID and phone number.
        testInsertSim();
        doReturn(2).when(mTelephonyManager).getPhoneCount();
        mSubscriptionControllerUT.addSubInfoRecord("test2", 1);
        int firstSubId = getFirstSubId();
        int secondSubId = getSubIdAtIndex(1);
        setupIdentifierCarrierPrivilegesTest();
        setCarrierPrivilegesForSubId(false, firstSubId);
        setCarrierPrivilegesForSubId(true, secondSubId);

        List<SubscriptionInfo> subInfoList =
                mSubscriptionControllerUT.getActiveSubscriptionInfoList(mCallingPackage,
                        mCallingFeature);

        assertEquals(2, subInfoList.size());
        assertTrue(subInfoList.get(0).getSubscriptionId() < subInfoList.get(1).getSubscriptionId());
    }

    @Test
    public void testGetActiveSubscriptionInfoListWithIdentifierAccessWithoutNumberAccess()
            throws Exception {