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

Commit 44bc04a1 authored by Bonian Chen's avatar Bonian Chen
Browse files

[Settings] Update EID in background

Access EID in background thread to avoid blocking UI.

Bug: 153407357
Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=SimStatusDialogControllerTest
Merged-In: Id30851bbd0b0fe080ed703586f9718be8b97abdd
Change-Id: I23fc6168d939bb5cb5e281413462176e7221819c
parent 7df1fb2a
Loading
Loading
Loading
Loading
+27 −10
Original line number Diff line number Diff line
@@ -60,9 +60,11 @@ import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume;
import com.android.settingslib.utils.ThreadUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

public class SimStatusDialogController implements LifecycleObserver, OnResume, OnPause {

@@ -220,7 +222,7 @@ public class SimStatusDialogController implements LifecycleObserver, OnResume, O
    }

    public void initialize() {
        updateEid();
        requestForUpdateEid();

        if (mSubscriptionInfo == null) {
            return;
@@ -539,25 +541,33 @@ public class SimStatusDialogController implements LifecycleObserver, OnResume, O
        }
    }

    private void updateEid() {
    @VisibleForTesting
    void requestForUpdateEid() {
        ThreadUtils.postOnBackgroundThread(() -> {
            final AtomicReference<String> eid = getEid(mSlotIndex);
            ThreadUtils.postOnMainThread(() -> updateEid(eid));
        });
    }

    @VisibleForTesting
    AtomicReference<String> getEid(int slotIndex) {
        boolean shouldHaveEid = false;
        String eid = null;

        if (mTelephonyManager.getActiveModemCount() > MAX_PHONE_COUNT_SINGLE_SIM) {
            // Get EID per-SIM in multi-SIM mode
            Map<Integer, Integer> mapping = mTelephonyManager.getLogicalToPhysicalSlotMapping();
            int pSlotId = mapping.getOrDefault(mSlotIndex,
            final Map<Integer, Integer> mapping = mTelephonyManager
                    .getLogicalToPhysicalSlotMapping();
            final int pSlotId = mapping.getOrDefault(slotIndex,
                    SubscriptionManager.INVALID_SIM_SLOT_INDEX);

            if (pSlotId != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
                List<UiccCardInfo> infos = mTelephonyManager.getUiccCardsInfo();
                final List<UiccCardInfo> infos = mTelephonyManager.getUiccCardsInfo();

                for (UiccCardInfo info : infos) {
                    if (info.getSlotIndex() == pSlotId) {
                        if (info.isEuicc()) {
                            shouldHaveEid = true;
                            eid = info.getEid();

                            if (TextUtils.isEmpty(eid)) {
                                eid = mEuiccManager.createForCardId(info.getCardId()).getEid();
                            }
@@ -571,12 +581,19 @@ public class SimStatusDialogController implements LifecycleObserver, OnResume, O
            shouldHaveEid = true;
            eid = mEuiccManager.getEid();
        }
        if ((!shouldHaveEid) && (eid == null)) {
            return null;
        }
        return new AtomicReference<String>(eid);
    }

        if (!shouldHaveEid) {
    @VisibleForTesting
    void updateEid(AtomicReference<String> eid) {
        if (eid == null) {
            mDialog.removeSettingFromScreen(EID_INFO_LABEL_ID);
            mDialog.removeSettingFromScreen(EID_INFO_VALUE_ID);
        } else if (!TextUtils.isEmpty(eid)) {
            mDialog.setText(EID_INFO_VALUE_ID, eid);
        } else if (eid.get() != null) {
            mDialog.setText(EID_INFO_VALUE_ID, eid.get());
        }
    }

+18 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import static com.android.settings.deviceinfo.simstatus.SimStatusDialogControlle
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
@@ -452,6 +453,8 @@ public class SimStatusDialogControllerTest {
        when(mEuiccManager.isEnabled()).thenReturn(true);
        when(mEuiccManager.getEid()).thenReturn(null);

        doNothing().when(mController).requestForUpdateEid();
        mController.updateEid(mController.getEid(0));
        mController.initialize();

        // Keep 'Not available' if neither the card nor the associated manager can provide EID.
@@ -489,7 +492,10 @@ public class SimStatusDialogControllerTest {

        when(mEuiccManager.isEnabled()).thenReturn(true);
        when(mEuiccManager.getEid()).thenReturn(TEST_EID_FROM_MANAGER);
        when(mEuiccManager.createForCardId(0)).thenReturn(mEuiccManager);

        doNothing().when(mController).requestForUpdateEid();
        mController.updateEid(mController.getEid(0));
        mController.initialize();

        // Set EID retrieved from the card.
@@ -531,6 +537,8 @@ public class SimStatusDialogControllerTest {
                new RuntimeException("Unexpected card ID was specified"));
        when(mEuiccManager.createForCardId(1)).thenReturn(mEuiccManager);

        doNothing().when(mController).requestForUpdateEid();
        mController.updateEid(mController.getEid(0));
        mController.initialize();

        // Set EID retrieved from the manager associated with the card which cannot provide EID.
@@ -569,6 +577,8 @@ public class SimStatusDialogControllerTest {
        when(mEuiccManager.isEnabled()).thenReturn(true);
        when(mEuiccManager.getEid()).thenReturn(TEST_EID_FROM_MANAGER);

        doNothing().when(mController).requestForUpdateEid();
        mController.updateEid(mController.getEid(0));
        mController.initialize();

        // Remove EID if the card is not eUICC.
@@ -599,6 +609,8 @@ public class SimStatusDialogControllerTest {
        when(mEuiccManager.isEnabled()).thenReturn(true);
        when(mEuiccManager.getEid()).thenReturn(null);

        doNothing().when(mController).requestForUpdateEid();
        mController.updateEid(mController.getEid(0));
        mController.initialize();

        // Keep 'Not available' if the default eUICC manager cannot provide EID in Single SIM mode.
@@ -630,6 +642,8 @@ public class SimStatusDialogControllerTest {
        when(mEuiccManager.createForCardId(anyInt())).thenThrow(
                new RuntimeException("EID shall be retrieved from the default eUICC manager"));

        doNothing().when(mController).requestForUpdateEid();
        mController.updateEid(mController.getEid(0));
        mController.initialize();

        // Set EID retrieved from the default eUICC manager in Single SIM mode.
@@ -661,6 +675,8 @@ public class SimStatusDialogControllerTest {
        when(mEuiccManager.createForCardId(anyInt())).thenThrow(
                new RuntimeException("EID shall be retrieved from the default eUICC manager"));

        doNothing().when(mController).requestForUpdateEid();
        mController.updateEid(mController.getEid(0));
        mController.initialize();

        // Set EID retrieved from the default eUICC manager in Single SIM mode.
@@ -690,6 +706,8 @@ public class SimStatusDialogControllerTest {
        when(mEuiccManager.isEnabled()).thenReturn(false);
        when(mEuiccManager.getEid()).thenReturn(null);

        doNothing().when(mController).requestForUpdateEid();
        mController.updateEid(mController.getEid(0));
        mController.initialize();

        // Remove EID if the default eUICC manager indicates that eSIM is not enabled.