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

Commit 28276184 authored by Jack Yu's avatar Jack Yu
Browse files

Subscription Manager public method clean up

1. Preliminary step of subscription manager API surface clean up.
2. Simpler way to solve unit test issue.
3. Fixed incorrect string comparison.
4. Moved some static variables into non-static.

Bug: 239607619
Test: atest TelephonyFrameworksTests
Merged-In: I50c110f53caec4b73708dcf1cb8d52708242d7ea
Change-Id: I50c110f53caec4b73708dcf1cb8d52708242d7ea
parent ed5d3f90
Loading
Loading
Loading
Loading
+40 −96
Original line number Diff line number Diff line
@@ -164,15 +164,10 @@ public class SubscriptionController extends ISub.Stub {

    private AppOpsManager mAppOps;

    // Allows test mocks to avoid SELinux failures on invalidate calls.
    private static boolean sCachingEnabled = true;

    // Each slot can have multiple subs.
    private static class WatchedSlotIndexToSubIds {
        private Map<Integer, ArrayList<Integer>> mSlotIndexToSubIds = new ConcurrentHashMap<>();

        WatchedSlotIndexToSubIds() {
        }
        private final Map<Integer, ArrayList<Integer>> mSlotIndexToSubIds =
                new ConcurrentHashMap<>();

        public void clear() {
            mSlotIndexToSubIds.clear();
@@ -191,7 +186,7 @@ public class SubscriptionController extends ISub.Stub {
                return null;
            }

            return new ArrayList<Integer>(subIdList);
            return new ArrayList<>(subIdList);
        }

        public void put(int slotIndex, ArrayList<Integer> value) {
@@ -273,9 +268,9 @@ public class SubscriptionController extends ISub.Stub {
        }
    }

    private static WatchedSlotIndexToSubIds sSlotIndexToSubIds = new WatchedSlotIndexToSubIds();
    private final WatchedSlotIndexToSubIds mSlotIndexToSubIds = new WatchedSlotIndexToSubIds();

    protected static WatchedInt sDefaultFallbackSubId =
    private final WatchedInt mDefaultFallbackSubId =
            new WatchedInt(SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
        @Override
        public void set(int newValue) {
@@ -1479,7 +1474,7 @@ public class SubscriptionController extends ISub.Stub {
                            int defaultSubId = getDefaultSubId();
                            if (DBG) {
                                logdl("[addSubInfoRecord]"
                                        + " sSlotIndexToSubIds.size=" + sSlotIndexToSubIds.size()
                                        + " mSlotIndexToSubIds.size=" + mSlotIndexToSubIds.size()
                                        + " slotIndex=" + slotIndex + " subId=" + subId
                                        + " defaultSubId=" + defaultSubId
                                        + " simCount=" + subIdCountMax);
@@ -1559,7 +1554,7 @@ public class SubscriptionController extends ISub.Stub {
                    if (DBG) logdl("[addSubInfoRecord] sim name = " + nameToSet);
                }

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

        } finally {
@@ -1663,7 +1658,7 @@ public class SubscriptionController extends ISub.Stub {
                return -1;
            }
            refreshCachedActiveSubscriptionInfoList();
            result = sSlotIndexToSubIds.removeFromSubIdList(slotIndex, subId);
            result = mSlotIndexToSubIds.removeFromSubIdList(slotIndex, subId);
            if (result == NO_ENTRY_FOR_SLOT_INDEX) {
                loge("sSlotIndexToSubIds has no entry for slotIndex = " + slotIndex);
            } else if (result == SUB_ID_NOT_IN_SLOT) {
@@ -1707,7 +1702,7 @@ public class SubscriptionController extends ISub.Stub {
        // Refresh the Cache of Active Subscription Info List
        refreshCachedActiveSubscriptionInfoList();

        sSlotIndexToSubIds.remove(slotIndex);
        mSlotIndexToSubIds.remove(slotIndex);
    }

    /**
@@ -2594,14 +2589,14 @@ public class SubscriptionController extends ISub.Stub {
            return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
        }

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

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

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

@@ -2644,7 +2639,7 @@ public class SubscriptionController extends ISub.Stub {
        }

        // Check if we've got any SubscriptionInfo records using slotIndexToSubId as a surrogate.
        int size = sSlotIndexToSubIds.size();
        int size = mSlotIndexToSubIds.size();
        if (size == 0) {
            if (VDBG) {
                logd("[getSubId]- sSlotIndexToSubIds.size == 0, return null slotIndex="
@@ -2654,7 +2649,7 @@ public class SubscriptionController extends ISub.Stub {
        }

        // Convert ArrayList to array
        ArrayList<Integer> subIds = sSlotIndexToSubIds.getCopy(slotIndex);
        ArrayList<Integer> subIds = mSlotIndexToSubIds.getCopy(slotIndex);
        if (subIds != null && subIds.size() > 0) {
            int[] subIdArr = new int[subIds.size()];
            for (int i = 0; i < subIds.size(); i++) {
@@ -2687,7 +2682,7 @@ public class SubscriptionController extends ISub.Stub {
            return SubscriptionManager.INVALID_PHONE_INDEX;
        }

        int size = sSlotIndexToSubIds.size();
        int size = mSlotIndexToSubIds.size();
        if (size == 0) {
            phoneId = mDefaultPhoneId;
            if (VDBG) logdl("[getPhoneId]- no sims, returning default phoneId=" + phoneId);
@@ -2695,7 +2690,7 @@ public class SubscriptionController extends ISub.Stub {
        }

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

@@ -2723,14 +2718,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 = sSlotIndexToSubIds.size();
            int size = mSlotIndexToSubIds.size();

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

            sSlotIndexToSubIds.clear();
            mSlotIndexToSubIds.clear();
            if (DBG) logdl("[clearSubInfo]- clear size=" + size);
            return size;
        } finally {
@@ -2781,7 +2776,7 @@ public class SubscriptionController extends ISub.Stub {
            if (VDBG) logdl("[getDefaultSubId] NOT VoiceCapable subId=" + subId);
        }
        if (!isActiveSubId(subId)) {
            subId = sDefaultFallbackSubId.get();
            subId = mDefaultFallbackSubId.get();
            if (VDBG) logdl("[getDefaultSubId] NOT active use fall back subId=" + subId);
        }
        if (VDBG) logv("[getDefaultSubId]- value = " + subId);
@@ -2974,7 +2969,7 @@ public class SubscriptionController extends ISub.Stub {
        }
        int previousDefaultSub = getDefaultSubId();
        if (isSubscriptionForRemoteSim(subscriptionType)) {
            sDefaultFallbackSubId.set(subId);
            mDefaultFallbackSubId.set(subId);
            return;
        }
        if (SubscriptionManager.isValidSubscriptionId(subId)) {
@@ -2982,7 +2977,7 @@ public class SubscriptionController extends ISub.Stub {
            if (phoneId >= 0 && (phoneId < mTelephonyManager.getPhoneCount()
                    || mTelephonyManager.getSimCount() == 1)) {
                if (DBG) logdl("[setDefaultFallbackSubId] set sDefaultFallbackSubId=" + subId);
                sDefaultFallbackSubId.set(subId);
                mDefaultFallbackSubId.set(subId);
                // Update MCC MNC device configuration information
                String defaultMccMnc = mTelephonyManager.getSimOperatorNumericForPhone(phoneId);
                MccTable.updateMccMncConfiguration(mContext, defaultMccMnc);
@@ -3082,7 +3077,7 @@ public class SubscriptionController extends ISub.Stub {
    private synchronized ArrayList<Integer> getActiveSubIdArrayList() {
        // Clone the sub id list so it can't change out from under us while iterating
        List<Entry<Integer, ArrayList<Integer>>> simInfoList =
                new ArrayList<>(sSlotIndexToSubIds.entrySet());
                new ArrayList<>(mSlotIndexToSubIds.entrySet());

        // Put the set of sub ids in slot index order
        Collections.sort(simInfoList, (x, y) -> x.getKey().compareTo(y.getKey()));
@@ -3412,7 +3407,7 @@ public class SubscriptionController extends ISub.Stub {
                    .from(mContext).getDefaultSmsPhoneId());
            pw.flush();

            for (Entry<Integer, ArrayList<Integer>> entry : sSlotIndexToSubIds.entrySet()) {
            for (Entry<Integer, ArrayList<Integer>> entry : mSlotIndexToSubIds.entrySet()) {
                pw.println(" sSlotIndexToSubId[" + entry.getKey() + "]: subIds=" + entry);
            }
            pw.flush();
@@ -4397,10 +4392,10 @@ public class SubscriptionController extends ISub.Stub {
    }

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

        // add the given subId unless it already exists
@@ -4410,20 +4405,20 @@ public class SubscriptionController extends ISub.Stub {
        }
        if (isSubscriptionForRemoteSim(subscriptionType)) {
            // For Remote SIM subscriptions, a slot can have multiple subscriptions.
            sSlotIndexToSubIds.addToSubIdList(slotIndex, subId);
            mSlotIndexToSubIds.addToSubIdList(slotIndex, subId);
        } else {
            // for all other types of subscriptions, a slot can have only one subscription at a time
            sSlotIndexToSubIds.clearSubIdList(slotIndex);
            sSlotIndexToSubIds.addToSubIdList(slotIndex, subId);
            mSlotIndexToSubIds.clearSubIdList(slotIndex);
            mSlotIndexToSubIds.addToSubIdList(slotIndex, subId);
        }


        // Remove the slot from sSlotIndexToSubIds if it has the same sub id with the added slot
        for (Entry<Integer, ArrayList<Integer>> entry : sSlotIndexToSubIds.entrySet()) {
        for (Entry<Integer, ArrayList<Integer>> entry : mSlotIndexToSubIds.entrySet()) {
            if (entry.getKey() != slotIndex && entry.getValue() != null
                    && entry.getValue().contains(subId)) {
                logdl("addToSubIdList - remove " + entry.getKey());
                sSlotIndexToSubIds.remove(entry.getKey());
                mSlotIndexToSubIds.remove(entry.getKey());
            }
        }

@@ -4441,17 +4436,7 @@ public class SubscriptionController extends ISub.Stub {
     */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
    public Map<Integer, ArrayList<Integer>> getSlotIndexToSubIdsMap() {
        return sSlotIndexToSubIds.getMap();
    }

    /**
     * This is only for testing
     * @hide
     */
    @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
    public void resetStaticMembers() {
        sDefaultFallbackSubId.set(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
        mDefaultPhoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
        return mSlotIndexToSubIds.getMap();
    }

    private void notifyOpportunisticSubscriptionInfoChanged() {
@@ -4743,77 +4728,36 @@ public class SubscriptionController extends ISub.Stub {
     */
    private void setGlobalSetting(String name, int value) {
        Settings.Global.putInt(mContext.getContentResolver(), name, value);
        if (name == Settings.Global.MULTI_SIM_DATA_CALL_SUBSCRIPTION) {
        if (TextUtils.equals(name, Settings.Global.MULTI_SIM_DATA_CALL_SUBSCRIPTION)) {
            invalidateDefaultDataSubIdCaches();
            invalidateActiveDataSubIdCaches();
            invalidateDefaultSubIdCaches();
            invalidateSlotIndexCaches();
        } else if (name == Settings.Global.MULTI_SIM_VOICE_CALL_SUBSCRIPTION) {
        } else if (TextUtils.equals(name, Settings.Global.MULTI_SIM_VOICE_CALL_SUBSCRIPTION)) {
            invalidateDefaultSubIdCaches();
            invalidateSlotIndexCaches();
        } else if (name == Settings.Global.MULTI_SIM_SMS_SUBSCRIPTION) {
        } else if (TextUtils.equals(name, Settings.Global.MULTI_SIM_SMS_SUBSCRIPTION)) {
            invalidateDefaultSmsSubIdCaches();
        }
    }

    /**
     * @hide
     */
    private static void invalidateDefaultSubIdCaches() {
        if (sCachingEnabled) {
        SubscriptionManager.invalidateDefaultSubIdCaches();
    }
    }

    /**
     * @hide
     */
    private static void invalidateDefaultDataSubIdCaches() {
        if (sCachingEnabled) {
        SubscriptionManager.invalidateDefaultDataSubIdCaches();
    }
    }

    /**
     * @hide
     */
    private static void invalidateDefaultSmsSubIdCaches() {
        if (sCachingEnabled) {
        SubscriptionManager.invalidateDefaultSmsSubIdCaches();
    }
    }

    /**
     * @hide
     */
    public static void invalidateActiveDataSubIdCaches() {
        if (sCachingEnabled) {
    private static void invalidateActiveDataSubIdCaches() {
        SubscriptionManager.invalidateActiveDataSubIdCaches();
    }
    }

    /**
     * @hide
     */
    protected static void invalidateSlotIndexCaches() {
        if (sCachingEnabled) {
    private static void invalidateSlotIndexCaches() {
        SubscriptionManager.invalidateSlotIndexCaches();
    }
}

    /**
     * @hide
     */
    @VisibleForTesting
    public static void disableCaching() {
        sCachingEnabled = false;
    }

    /**
     * @hide
     */
    @VisibleForTesting
    public static void enableCaching() {
        sCachingEnabled = true;
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -249,7 +249,7 @@ public class PhoneSwitcher extends Handler {
        @Override
        public void set(int newValue) {
            super.set(newValue);
            SubscriptionController.invalidateActiveDataSubIdCaches();
            SubscriptionManager.invalidateActiveDataSubIdCaches();
        }
    };

@@ -395,7 +395,7 @@ public class PhoneSwitcher extends Handler {
    public static PhoneSwitcher make(int maxDataAttachModemCount, Context context, Looper looper) {
        if (sPhoneSwitcher == null) {
            sPhoneSwitcher = new PhoneSwitcher(maxDataAttachModemCount, context, looper);
            SubscriptionController.invalidateActiveDataSubIdCaches();
            SubscriptionManager.invalidateActiveDataSubIdCaches();
        }

        return sPhoneSwitcher;
+0 −1
Original line number Diff line number Diff line
@@ -154,7 +154,6 @@ public class SubscriptionControllerTest extends TelephonyTest {
         * between each test case. */
        if (mSubscriptionControllerUT != null) {
            mSubscriptionControllerUT.clearSubInfo();
            mSubscriptionControllerUT.resetStaticMembers();
            mSubscriptionControllerUT = null;
        }

+2 −2
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.app.ActivityManager;
import android.app.AppOpsManager;
import android.app.IActivityManager;
import android.app.KeyguardManager;
import android.app.PropertyInvalidatedCache;
import android.app.usage.NetworkStatsManager;
import android.content.ContentProvider;
import android.content.ContentResolver;
@@ -501,7 +502,7 @@ public abstract class TelephonyTest {
        mMockedWlanDataServiceManager = Mockito.mock(DataServiceManager.class);

        TelephonyManager.disableServiceHandleCaching();
        SubscriptionController.disableCaching();
        PropertyInvalidatedCache.disableForTestMode();
        // For testing do not allow Log.WTF as it can cause test process to crash
        Log.setWtfHandler((tagString, what, system) -> Log.d(TAG, "WTF captured, ignoring. Tag: "
                + tagString + ", exception: " + what));
@@ -898,7 +899,6 @@ public abstract class TelephonyTest {
        }
        restoreInstances();
        TelephonyManager.enableServiceHandleCaching();
        SubscriptionController.enableCaching();

        mNetworkRegistrationInfo = null;
        mActivityManager = null;