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

Commit 7a6ecced authored by Jack Yu's avatar Jack Yu Committed by Gerrit Code Review
Browse files

Merge changes I8752aea9,I89d1d6a1 into main

* changes:
  Added unit tests
  Moved UICC application enabled callback
parents 0b84cee5 7fda1971
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -347,7 +347,7 @@ public class GsmCdmaPhone extends Phone {
            mSubscriptionManagerService.registerCallback(new SubscriptionManagerServiceCallback(
                    this::post) {
                @Override
                public void onUiccApplicationsEnabled(int subId) {
                public void onUiccApplicationsEnabledChanged(int subId) {
                    reapplyUiccAppsEnablementIfNeeded(ENABLE_UICC_APPS_MAX_RETRIES);
                }
            });
@@ -4254,6 +4254,7 @@ public class GsmCdmaPhone extends Phone {
                " mTelecomVoiceServiceStateOverride=" + mTelecomVoiceServiceStateOverride + "("
                        + ServiceState.rilServiceStateToString(mTelecomVoiceServiceStateOverride)
                        + ")");
        pw.println(" mUiccApplicationsEnabled=" + mUiccApplicationsEnabled);
        pw.flush();
    }

@@ -4639,6 +4640,8 @@ public class GsmCdmaPhone extends Phone {
        // If no card is present or we don't have mUiccApplicationsEnabled yet, do nothing.
        if (slot == null || slot.getCardState() != IccCardStatus.CardState.CARDSTATE_PRESENT
                || mUiccApplicationsEnabled == null) {
            loge("reapplyUiccAppsEnablementIfNeeded: slot state="
                    + (slot != null ? slot.getCardState() : null));
            return;
        }

+0 −16
Original line number Diff line number Diff line
@@ -553,14 +553,6 @@ public class SubscriptionDatabaseManager extends Handler {
         * @param subId The subscription id.
         */
        public abstract void onSubscriptionChanged(int subId);

        /**
         * Called when {@link SubscriptionInfoInternal#areUiccApplicationsEnabled()}
         * changed.
         *
         * @param subId The subscription id.
         */
        public abstract void onUiccApplicationsEnabled(int subId);
    }

    /**
@@ -918,10 +910,6 @@ public class SubscriptionDatabaseManager extends Handler {
                            mAllSubscriptionInfoInternalCache.put(id, builder.build());
                            mCallback.invokeFromExecutor(()
                                    -> mCallback.onSubscriptionChanged(subId));
                            if (columnName.equals(SimInfo.COLUMN_UICC_APPLICATIONS_ENABLED)) {
                                mCallback.invokeFromExecutor(()
                                        -> mCallback.onUiccApplicationsEnabled(subId));
                            }
                        }
                    }
                }
@@ -956,10 +944,6 @@ public class SubscriptionDatabaseManager extends Handler {
            if (updateDatabase(subId, createDeltaContentValues(oldSubInfo, newSubInfo)) > 0) {
                mAllSubscriptionInfoInternalCache.put(subId, newSubInfo);
                mCallback.invokeFromExecutor(() -> mCallback.onSubscriptionChanged(subId));
                if (oldSubInfo.areUiccApplicationsEnabled()
                        != newSubInfo.areUiccApplicationsEnabled()) {
                    mCallback.invokeFromExecutor(() -> mCallback.onUiccApplicationsEnabled(subId));
                }
            }
        } finally {
            mReadWriteLock.writeLock().unlock();
+21 −30
Original line number Diff line number Diff line
@@ -266,8 +266,12 @@ public class SubscriptionManagerService extends ISub.Stub {
    /**
     * Slot index/subscription map that automatically invalidate cache in
     * {@link SubscriptionManager}.
     *
     * @param <K> The type of the key.
     * @param <V> The type of the value.
     */
    private static class SubscriptionMap<K, V> extends ConcurrentHashMap<K, V> {
    @VisibleForTesting
    public static class SubscriptionMap<K, V> extends ConcurrentHashMap<K, V> {
        @Override
        public void clear() {
            super.clear();
@@ -379,7 +383,7 @@ public class SubscriptionManagerService extends ISub.Stub {
         *
         * @param subId The subscription id.
         */
        public void onUiccApplicationsEnabled(int subId) {}
        public void onUiccApplicationsEnabledChanged(int subId) {}
    }

    /**
@@ -501,23 +505,6 @@ public class SubscriptionManagerService extends ISub.Stub {
                                && telephonyRegistryManager != null) {
                            telephonyRegistryManager.notifyOpportunisticSubscriptionInfoChanged();
                        }

                        // TODO: Call TelephonyMetrics.updateActiveSubscriptionInfoList when active
                        //  subscription changes.
                    }

                    /**
                     * Called when {@link SubscriptionInfoInternal#areUiccApplicationsEnabled()}
                     * changed.
                     *
                     * @param subId The subscription id.
                     */
                    @Override
                    public void onUiccApplicationsEnabled(int subId) {
                        log("onUiccApplicationsEnabled: subId=" + subId);
                        mSubscriptionManagerServiceCallbacks.forEach(
                                callback -> callback.invokeFromExecutor(
                                        () -> callback.onUiccApplicationsEnabled(subId)));
                    }
                });

@@ -3000,7 +2987,7 @@ public class SubscriptionManagerService extends ISub.Stub {
                        + columnName);
            }
        } catch (IllegalArgumentException e) {
            logv("getSubscriptionProperty: Invalid subId " + subId + ", columnName=" + columnName);
            loge("getSubscriptionProperty: Invalid subId " + subId + ", columnName=" + columnName);
            return null;
        } finally {
            Binder.restoreCallingIdentity(token);
@@ -3193,7 +3180,20 @@ public class SubscriptionManagerService extends ISub.Stub {

        final long identity = Binder.clearCallingIdentity();
        try {

            SubscriptionInfoInternal subInfo = mSubscriptionDatabaseManager
                    .getSubscriptionInfoInternal(subId);
            if (subInfo == null) {
                throw new IllegalArgumentException("setUiccApplicationsEnabled: Subscription "
                        + "doesn't exist. subId=" + subId);
            }

            if (subInfo.areUiccApplicationsEnabled() != enabled) {
                mSubscriptionDatabaseManager.setUiccApplicationsEnabled(subId, enabled);
                mSubscriptionManagerServiceCallbacks.forEach(
                        callback -> callback.invokeFromExecutor(
                                () -> callback.onUiccApplicationsEnabledChanged(subId)));
            }
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
@@ -3874,15 +3874,6 @@ public class SubscriptionManagerService extends ISub.Stub {
        Rlog.e(LOG_TAG, s);
    }

    /**
     * Log verbose messages.
     *
     * @param s debug messages.
     */
    private void logv(@NonNull String s) {
        if (VDBG) Rlog.v(LOG_TAG, s);
    }

    /**
     * Log debug messages and also log into the local log.
     *
+36 −0
Original line number Diff line number Diff line
@@ -63,6 +63,8 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;

@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper
@@ -1673,4 +1675,38 @@ public class SubscriptionDatabaseManagerTest extends TelephonyTest {
        assertThat(mDatabaseManagerUT.getSubscriptionInfoInternal(3)).isNull();
        verify(mSubscriptionDatabaseManagerCallback).onSubscriptionChanged(eq(3));
    }

    @Test
    public void testCallback() {
        CountDownLatch latch = new CountDownLatch(2);
        Executor executor = Runnable::run;
        SubscriptionDatabaseManagerCallback callback =
                new SubscriptionDatabaseManagerCallback(executor) {
                    @Override
                    public void onInitialized() {
                        latch.countDown();
                        logd("onInitialized");
                    }

                    @Override
                    public void onSubscriptionChanged(int subId) {
                        latch.countDown();
                        logd("onSubscriptionChanged");
                    }
                };
        assertThat(callback.getExecutor()).isEqualTo(executor);
        mDatabaseManagerUT = new SubscriptionDatabaseManager(mContext, Looper.myLooper(), callback);
        processAllMessages();

        assertThat(latch.getCount()).isEqualTo(1);

        mDatabaseManagerUT.insertSubscriptionInfo(
                new SubscriptionInfoInternal.Builder()
                        .setId(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
                        .setIccId(FAKE_ICCID1)
                        .setSimSlotIndex(0)
                        .build());
        processAllMessages();
        assertThat(latch.getCount()).isEqualTo(0);
    }
}
+97 −12
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@@ -103,6 +104,7 @@ import com.android.internal.telephony.TelephonyTest;
import com.android.internal.telephony.euicc.EuiccController;
import com.android.internal.telephony.subscription.SubscriptionDatabaseManagerTest.SubscriptionProvider;
import com.android.internal.telephony.subscription.SubscriptionManagerService.SubscriptionManagerServiceCallback;
import com.android.internal.telephony.subscription.SubscriptionManagerService.SubscriptionMap;
import com.android.internal.telephony.uicc.UiccSlot;

import libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges;
@@ -121,11 +123,11 @@ import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;

@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper
@@ -135,6 +137,8 @@ public class SubscriptionManagerServiceTest extends TelephonyTest {

    private static final String CALLING_FEATURE = "calling_feature";

    private static final String GROUP_UUID = "6adbc864-691c-45dc-b698-8fc9a2176fae";

    private SubscriptionManagerService mSubscriptionManagerServiceUT;

    private final SubscriptionProvider mSubscriptionProvider = new SubscriptionProvider();
@@ -252,21 +256,17 @@ public class SubscriptionManagerServiceTest extends TelephonyTest {
            // Insertion is sync, but the onSubscriptionChanged callback is handled by the handler.
            processAllMessages();

            Class<?> SubscriptionMapClass = Class.forName(
                    "com.android.internal.telephony.subscription"
                            + ".SubscriptionManagerService$SubscriptionMap");
            Field field = SubscriptionManagerService.class.getDeclaredField("mSlotIndexToSubId");
            field.setAccessible(true);
            Object map = field.get(mSubscriptionManagerServiceUT);
            SubscriptionMap<Integer, Integer> map = (SubscriptionMap<Integer, Integer>)
                    field.get(mSubscriptionManagerServiceUT);
            Class[] cArgs = new Class[2];
            cArgs[0] = Object.class;
            cArgs[1] = Object.class;

            if (subInfo.getSimSlotIndex() >= 0) {
                // Change the slot -> subId mapping
                Method method = SubscriptionMapClass.getDeclaredMethod("put", cArgs);
                method.setAccessible(true);
                method.invoke(map, subInfo.getSimSlotIndex(), subId);
                map.put(subInfo.getSimSlotIndex(), subId);
            }

            mContextFixture.removeCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
@@ -1179,7 +1179,7 @@ public class SubscriptionManagerServiceTest extends TelephonyTest {
        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
        insertSubscription(FAKE_SUBSCRIPTION_INFO2);

        ParcelUuid newUuid = ParcelUuid.fromString("6adbc864-691c-45dc-b698-8fc9a2176fae");
        ParcelUuid newUuid = ParcelUuid.fromString(GROUP_UUID);
        String newOwner = "new owner";
        // Should fail without MODIFY_PHONE_STATE
        assertThrows(SecurityException.class, () -> mSubscriptionManagerServiceUT
@@ -1268,11 +1268,20 @@ public class SubscriptionManagerServiceTest extends TelephonyTest {
        mSubscriptionManagerServiceUT.setUiccApplicationsEnabled(false, 1);
        processAllMessages();
        verify(mMockedSubscriptionManagerServiceCallback).onSubscriptionChanged(eq(1));
        verify(mMockedSubscriptionManagerServiceCallback).onUiccApplicationsEnabledChanged(eq(1));

        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
                .getSubscriptionInfoInternal(1);
        assertThat(subInfo).isNotNull();
        assertThat(subInfo.areUiccApplicationsEnabled()).isFalse();

        Mockito.clearInvocations(mMockedSubscriptionManagerServiceCallback);
        mSubscriptionManagerServiceUT.setUiccApplicationsEnabled(false, 1);
        processAllMessages();

        verify(mMockedSubscriptionManagerServiceCallback, never()).onSubscriptionChanged(eq(1));
        verify(mMockedSubscriptionManagerServiceCallback, never())
                .onUiccApplicationsEnabledChanged(eq(1));
    }

    @Test
@@ -2026,14 +2035,16 @@ public class SubscriptionManagerServiceTest extends TelephonyTest {
    @Test
    public void testOnUiccApplicationsEnabled() {
        CountDownLatch latch = new CountDownLatch(1);
        Executor executor = Runnable::run;
        SubscriptionManagerServiceCallback callback =
                new SubscriptionManagerServiceCallback(Runnable::run) {
                new SubscriptionManagerServiceCallback(executor) {
                    @Override
                    public void onUiccApplicationsEnabled(int subId) {
                    public void onUiccApplicationsEnabledChanged(int subId) {
                        latch.countDown();
                        logd("testOnSubscriptionChanged: onUiccApplicationsEnabled");
                        logd("testOnSubscriptionChanged: onUiccApplicationsEnabledChanged");
                    }
                };
        assertThat(callback.getExecutor()).isEqualTo(executor);
        mSubscriptionManagerServiceUT.registerCallback(callback);
        int subId = insertSubscription(FAKE_SUBSCRIPTION_INFO1);

@@ -2041,6 +2052,13 @@ public class SubscriptionManagerServiceTest extends TelephonyTest {
        mSubscriptionManagerServiceUT.setUiccApplicationsEnabled(false, subId);
        processAllMessages();
        assertThat(latch.getCount()).isEqualTo(0);

        mSubscriptionManagerServiceUT.unregisterCallback(callback);
        // without override. Nothing should happen.
        callback = new SubscriptionManagerServiceCallback(Runnable::run);
        mSubscriptionManagerServiceUT.registerCallback(callback);
        mSubscriptionManagerServiceUT.setUiccApplicationsEnabled(true, subId);
        processAllMessages();
    }

    @Test
@@ -2097,4 +2115,71 @@ public class SubscriptionManagerServiceTest extends TelephonyTest {
        assertThat(mSubscriptionManagerServiceUT.getActiveSubscriptionInfoList(
                CALLING_PACKAGE, CALLING_FEATURE).get(0).getIccId()).isEqualTo(FAKE_MAC_ADDRESS2);
    }

    @Test
    public void testRemoveSubscriptionsFromGroup() {
        testAddSubscriptionsIntoGroup();

        mContextFixture.removeCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
        assertThrows(SecurityException.class, ()
                -> mSubscriptionManagerServiceUT.removeSubscriptionsFromGroup(new int[]{2},
                ParcelUuid.fromString(GROUP_UUID), CALLING_PACKAGE));

        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);

        assertThrows(IllegalArgumentException.class, ()
                -> mSubscriptionManagerServiceUT.removeSubscriptionsFromGroup(new int[]{3},
                ParcelUuid.fromString(GROUP_UUID), CALLING_PACKAGE));

        assertThrows(IllegalArgumentException.class, ()
                -> mSubscriptionManagerServiceUT.removeSubscriptionsFromGroup(new int[]{2},
                ParcelUuid.fromString("55911c5b-83ed-419d-8f9b-4e027cf09305"), CALLING_PACKAGE));

        mSubscriptionManagerServiceUT.removeSubscriptionsFromGroup(new int[]{2},
                ParcelUuid.fromString(GROUP_UUID), CALLING_PACKAGE);

        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
                .getSubscriptionInfoInternal(2);
        assertThat(subInfo.getGroupUuid()).isEmpty();
        assertThat(subInfo.getGroupOwner()).isEmpty();
    }

    @Test
    public void testUpdateSimStateForInactivePort() {
        testSetUiccApplicationsEnabled();

        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
        mSubscriptionManagerServiceUT.updateSimStateForInactivePort(0);
        processAllMessages();

        SubscriptionInfoInternal subInfo = mSubscriptionManagerServiceUT
                .getSubscriptionInfoInternal(1);
        assertThat(subInfo.areUiccApplicationsEnabled()).isTrue();
    }

    @Test
    public void testRestoreAllSimSpecificSettingsFromBackup() {
        assertThrows(SecurityException.class, ()
                -> mSubscriptionManagerServiceUT.restoreAllSimSpecificSettingsFromBackup(
                        new byte[0]));
        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);

        // TODO: Briefly copy the logic from TelephonyProvider to
        //  SubscriptionDatabaseManagerTest.SubscriptionProvider
        mSubscriptionManagerServiceUT.restoreAllSimSpecificSettingsFromBackup(
                new byte[0]);
    }

    @Test
    public void testSubscriptionMap() {
        SubscriptionMap<Integer, Integer> map = new SubscriptionMap<>();
        map.put(1, 1);
        assertThat(map.get(1)).isEqualTo(1);
        map.put(0, 2);
        assertThat(map.get(0)).isEqualTo(2);
        map.remove(1);
        assertThat(map.get(1)).isNull();
        map.clear();
        assertThat(map).hasSize(0);
    }
}