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

Commit 541be1e9 authored by Vasu Nori's avatar Vasu Nori
Browse files

Changes to SubscriptionController to allow multiple sims per slot-index.

Allowing multiple subscriptions per slot-index may need changes
elsewhere in the code. But this CL only deals with a local data struct
in SubscriptionController.java.
This CL's immediate beneficiary is work associated with b/110267643

Bug: 112321331
Test: tested w/ unittests
Change-Id: Icae42dce43c4557a656f8ab6635bed5ebcc44730
parent 877bf6fd
Loading
Loading
Loading
Loading
+51 −48
Original line number Diff line number Diff line
@@ -55,14 +55,12 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@@ -155,9 +153,8 @@ public class SubscriptionController extends ISub.Stub {

    private AppOpsManager mAppOps;

    // FIXME: Does not allow for multiple subs in a slot and change to SparseArray
    private static Map<Integer, Integer> sSlotIndexToSubId =
            new ConcurrentHashMap<Integer, Integer>();
    // Each slot can have multiple subs.
    private static Map<Integer, ArrayList<Integer>> sSlotIndexToSubIds = new ConcurrentHashMap<>();
    private static int mDefaultFallbackSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
    private static int mDefaultPhoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;

@@ -216,7 +213,7 @@ public class SubscriptionController extends ISub.Stub {
    }

    private boolean isSubInfoReady() {
        return sSlotIndexToSubId.size() > 0;
        return sSlotIndexToSubIds.size() > 0;
    }

    private SubscriptionController(Phone phone) {
@@ -982,24 +979,20 @@ public class SubscriptionController extends ISub.Stub {
                    do {
                        int subId = cursor.getInt(cursor.getColumnIndexOrThrow(
                                SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID));
                        // If sSlotIndexToSubId already has the same subId for a slotIndex/phoneId,
                        // If sSlotIndexToSubIds already has the same subId for a slotIndex/phoneId,
                        // do not add it.
                        Integer currentSubId = sSlotIndexToSubId.get(slotIndex);
                        if (currentSubId == null
                                || currentSubId != subId
                                || !SubscriptionManager.isValidSubscriptionId(currentSubId)) {
                        if (addToSubIdList(slotIndex, subId)) {
                            // TODO While two subs active, if user deactivats first
                            // one, need to update the default subId with second one.

                            // FIXME: Currently we assume phoneId == slotIndex which in the future
                            // may not be true, for instance with multiple subs per slot.
                            // But is true at the moment.
                            sSlotIndexToSubId.put(slotIndex, subId);
                            int subIdCountMax = getActiveSubInfoCountMax();
                            int defaultSubId = getDefaultSubId();
                            if (DBG) {
                                logdl("[addSubInfoRecord]"
                                        + " sSlotIndexToSubId.size=" + sSlotIndexToSubId.size()
                                        + " sSlotIndexToSubIds.size=" + sSlotIndexToSubIds.size()
                                        + " slotIndex=" + slotIndex + " subId=" + subId
                                        + " defaultSubId=" + defaultSubId + " simCount=" + subIdCountMax);
                            }
@@ -1066,7 +1059,7 @@ public class SubscriptionController extends ISub.Stub {
            // Once the records are loaded, notify DcTracker
            sPhones[slotIndex].updateDataConnectionTracker();

            if (DBG) logdl("[addSubInfoRecord]- info size=" + sSlotIndexToSubId.size());
            if (DBG) logdl("[addSubInfoRecord]- info size=" + sSlotIndexToSubIds.size());

        } finally {
            Binder.restoreCallingIdentity(identity);
@@ -1419,20 +1412,18 @@ public class SubscriptionController extends ISub.Stub {
            return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
        }

        int size = sSlotIndexToSubId.size();
        int size = sSlotIndexToSubIds.size();

        if (size == 0)
        {
        if (size == 0) {
            if (DBG) logd("[getSlotIndex]- size == 0, return SIM_NOT_INSERTED instead");
            return SubscriptionManager.SIM_NOT_INSERTED;
        }

        for (Entry<Integer, Integer> entry: sSlotIndexToSubId.entrySet()) {
        for (Entry<Integer, ArrayList<Integer>> entry : sSlotIndexToSubIds.entrySet()) {
            int sim = entry.getKey();
            int sub = entry.getValue();
            ArrayList<Integer> subs = entry.getValue();

            if (subId == sub)
            {
            if (subs.contains(subId)) {
                if (VDBG) logv("[getSlotIndex]- return = " + sim);
                return sim;
            }
@@ -1468,26 +1459,17 @@ public class SubscriptionController extends ISub.Stub {
        }

        // Check if we've got any SubscriptionInfo records using slotIndexToSubId as a surrogate.
        int size = sSlotIndexToSubId.size();
        int size = sSlotIndexToSubIds.size();
        if (size == 0) {
            if (VDBG) {
                logd("[getSubId]- sSlotIndexToSubId.size == 0, return DummySubIds slotIndex="
                logd("[getSubId]- sSlotIndexToSubIds.size == 0, return DummySubIds slotIndex="
                        + slotIndex);
            }
            return getDummySubIds(slotIndex);
        }

        // Create an array of subIds that are in this slot?
        ArrayList<Integer> subIds = new ArrayList<Integer>();
        for (Entry<Integer, Integer> entry: sSlotIndexToSubId.entrySet()) {
            int slot = entry.getKey();
            int sub = entry.getValue();
            if (slotIndex == slot) {
                subIds.add(sub);
            }
        }

        // Convert ArrayList to array
        ArrayList<Integer> subIds = sSlotIndexToSubIds.get(slotIndex);
        int numSubIds = subIds.size();
        if (numSubIds > 0) {
            int[] subIdArr = new int[numSubIds];
@@ -1520,7 +1502,7 @@ public class SubscriptionController extends ISub.Stub {
            return SubscriptionManager.INVALID_PHONE_INDEX;
        }

        int size = sSlotIndexToSubId.size();
        int size = sSlotIndexToSubIds.size();
        if (size == 0) {
            phoneId = mDefaultPhoneId;
            if (DBG) logdl("[getPhoneId]- no sims, returning default phoneId=" + phoneId);
@@ -1528,11 +1510,11 @@ public class SubscriptionController extends ISub.Stub {
        }

        // FIXME: Assumes phoneId == slotIndex
        for (Entry<Integer, Integer> entry: sSlotIndexToSubId.entrySet()) {
        for (Entry<Integer, ArrayList<Integer>> entry: sSlotIndexToSubIds.entrySet()) {
            int sim = entry.getKey();
            int sub = entry.getValue();
            ArrayList<Integer> subs = entry.getValue();

            if (subId == sub) {
            if (subs.contains(subId)) {
                if (VDBG) logdl("[getPhoneId]- found subId=" + subId + " phoneId=" + sim);
                return sim;
            }
@@ -1577,14 +1559,14 @@ public class SubscriptionController extends ISub.Stub {
        // Now that all security checks passes, perform the operation as ourselves.
        final long identity = Binder.clearCallingIdentity();
        try {
            int size = sSlotIndexToSubId.size();
            int size = sSlotIndexToSubIds.size();

            if (size == 0) {
                if (DBG) logdl("[clearSubInfo]- no simInfo size=" + size);
                return 0;
            }

            sSlotIndexToSubId.clear();
            sSlotIndexToSubIds.clear();
            if (DBG) logdl("[clearSubInfo]- clear size=" + size);
            return size;
        } finally {
@@ -1943,23 +1925,29 @@ public class SubscriptionController extends ISub.Stub {
        sPhones = phones;
    }

    private synchronized ArrayList<Integer> getActiveSubIdArrayList() {
        ArrayList<Integer> allSubs = new ArrayList<>();
        for (int i : sSlotIndexToSubIds.keySet()) {
            allSubs.addAll(sSlotIndexToSubIds.get(i));
        }
        return allSubs;
    }

    /**
     * @return the list of subId's that are active, is never null but the length maybe 0.
     */
    @Override
    public int[] getActiveSubIdList() {
        Set<Entry<Integer, Integer>> simInfoSet = new HashSet<>(sSlotIndexToSubId.entrySet());

        int[] subIdArr = new int[simInfoSet.size()];
        ArrayList<Integer> allSubs = getActiveSubIdArrayList();
        int[] subIdArr = new int[allSubs.size()];
        int i = 0;
        for (Entry<Integer, Integer> entry: simInfoSet) {
            int sub = entry.getValue();
        for (int sub : allSubs) {
            subIdArr[i] = sub;
            i++;
        }

        if (VDBG) {
            logdl("[getActiveSubIdList] simInfoSet=" + simInfoSet + " subIdArr.length="
            logdl("[getActiveSubIdList] allSubs=" + allSubs + " subIdArr.length="
                    + subIdArr.length);
        }
        return subIdArr;
@@ -1968,7 +1956,7 @@ public class SubscriptionController extends ISub.Stub {
    @Override
    public boolean isActiveSubId(int subId) {
        boolean retVal = SubscriptionManager.isValidSubscriptionId(subId)
                && sSlotIndexToSubId.containsValue(subId);
                && getActiveSubIdArrayList().contains(subId);

        if (VDBG) logdl("[isActiveSubId]- " + retVal);
        return retVal;
@@ -2171,8 +2159,8 @@ public class SubscriptionController extends ISub.Stub {
                    .from(mContext).getDefaultSmsPhoneId());
            pw.flush();

            for (Entry<Integer, Integer> entry : sSlotIndexToSubId.entrySet()) {
                pw.println(" sSlotIndexToSubId[" + entry.getKey() + "]: subId=" + entry.getValue());
            for (Entry<Integer, ArrayList<Integer>> entry : sSlotIndexToSubIds.entrySet()) {
                pw.println(" sSlotIndexToSubId[" + entry.getKey() + "]: subIds=" + entry);
            }
            pw.flush();
            pw.println("++++++++++++++++++++++++++++++++");
@@ -2334,4 +2322,19 @@ public class SubscriptionController extends ISub.Stub {
            Binder.restoreCallingIdentity(token);
        }
    }

    private synchronized boolean addToSubIdList(int slotIndex, int subId) {
        ArrayList<Integer> subIdsList = sSlotIndexToSubIds.get(slotIndex);
        if (subIdsList == null) {
            subIdsList = new ArrayList<>();
            sSlotIndexToSubIds.put(slotIndex, subIdsList);
        }

        // add the given subId unless it already exists
        if (!subIdsList.contains(subId)) {
            subIdsList.add(subId);
            return true;
        }
        return false;
    }
}