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

Commit b8b5e58c authored by Hall Liu's avatar Hall Liu Committed by Android (Google) Code Review
Browse files

Merge "Refactor SubscriptionManager caching code"

parents 68b652f2 69e3f354
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -1138,6 +1138,7 @@ filegroup {
        "core/java/com/android/internal/os/SomeArgs.java",
        "core/java/com/android/internal/util/BitwiseInputStream.java",
        "core/java/com/android/internal/util/BitwiseOutputStream.java",
        "core/java/com/android/internal/util/FunctionalUtils.java",
        "core/java/com/android/internal/util/HexDump.java",
        "core/java/com/android/internal/util/IndentingPrintWriter.java",
        "core/java/com/android/internal/util/Preconditions.java",
+1 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ rule com.android.i18n.phonenumbers.** com.android.telephony.framework.phonenumbe
rule com.android.internal.os.SomeArgs* android.telephony.SomeArgs@1
rule com.android.internal.util.BitwiseInputStream* android.telephony.BitwiseInputStream@1
rule com.android.internal.util.BitwiseOutputStream* android.telephony.BitwiseOutputStream@1
rule com.android.internal.util.FunctionalUtils* android.telephony.FunctionalUtils@1
rule com.android.internal.util.Preconditions* android.telephony.Preconditions@1
rule com.android.internal.util.IndentingPrintWriter* android.telephony.IndentingPrintWriter@1
rule com.android.internal.util.HexDump* android.telephony.HexDump@1
+43 −46
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ import com.android.internal.telephony.ISetOpportunisticDataCallback;
import com.android.internal.telephony.ISub;
import com.android.internal.telephony.PhoneConstants;
import com.android.internal.telephony.util.HandlerExecutor;
import com.android.internal.util.FunctionalUtils;
import com.android.internal.util.Preconditions;
import com.android.telephony.Rlog;

@@ -145,21 +146,49 @@ public class SubscriptionManager {

    private static final int MAX_CACHE_SIZE = 4;

    private static PropertyInvalidatedCache<Void, Integer> sDefaultSubIdCache =
            new PropertyInvalidatedCache<Void, Integer>(
                    MAX_CACHE_SIZE, CACHE_KEY_DEFAULT_SUB_ID_PROPERTY) {
            @Override
            protected Integer recompute(Void query) {
                return getDefaultSubscriptionIdInternal();
            }};
    private static class SubscriptionPropertyInvalidatedCache<T>
            extends PropertyInvalidatedCache<Void, T> {
        private final FunctionalUtils.ThrowingFunction<ISub, T> mSubscriptionInterfaceMethod;
        private final String mCacheKeyProperty;
        private final T mDefaultValue;

        SubscriptionPropertyInvalidatedCache(
                FunctionalUtils.ThrowingFunction<ISub, T> subscriptionInterfaceMethod,
                String cacheKeyProperty,
                T defaultValue) {
            super(MAX_CACHE_SIZE, cacheKeyProperty);
            mSubscriptionInterfaceMethod = subscriptionInterfaceMethod;
            mCacheKeyProperty = cacheKeyProperty;
            mDefaultValue = defaultValue;
        }

    private static PropertyInvalidatedCache<Void, Integer> sDefaultDataSubIdCache =
            new PropertyInvalidatedCache<Void, Integer>(
                    MAX_CACHE_SIZE, CACHE_KEY_DEFAULT_DATA_SUB_ID_PROPERTY) {
        @Override
            protected Integer recompute(Void query) {
                return getDefaultDataSubscriptionIdInternal();
            }};
        protected T recompute(Void aVoid) {
            T result = mDefaultValue;

            try {
                ISub iSub = TelephonyManager.getSubscriptionService();
                if (iSub != null) {
                    result = mSubscriptionInterfaceMethod.applyOrThrow(iSub);
                }
            } catch (Exception ex) {
                Rlog.w(LOG_TAG, "Failed to recompute cache key for " + mCacheKeyProperty);
            }

            if (VDBG) logd("recomputing " + mCacheKeyProperty + ", result = " + result);
            return result;
        }
    }

    private static SubscriptionPropertyInvalidatedCache<Integer> sDefaultSubIdCache =
            new SubscriptionPropertyInvalidatedCache<>(ISub::getDefaultSubId,
                    CACHE_KEY_DEFAULT_SUB_ID_PROPERTY,
                    INVALID_SUBSCRIPTION_ID);

    private static SubscriptionPropertyInvalidatedCache<Integer> sDefaultDataSubIdCache =
            new SubscriptionPropertyInvalidatedCache<>(ISub::getDefaultDataSubId,
                    CACHE_KEY_DEFAULT_DATA_SUB_ID_PROPERTY,
                    INVALID_SUBSCRIPTION_ID);

    private static PropertyInvalidatedCache<Void, Integer> sActiveDataSubIdCache =
            new PropertyInvalidatedCache<Void, Integer>(
@@ -1886,22 +1915,6 @@ public class SubscriptionManager {
        return sDefaultSubIdCache.query(null);
    }

    private static int getDefaultSubscriptionIdInternal() {
        int subId = INVALID_SUBSCRIPTION_ID;

        try {
            ISub iSub = TelephonyManager.getSubscriptionService();
            if (iSub != null) {
                subId = iSub.getDefaultSubId();
            }
        } catch (RemoteException ex) {
            // ignore it
        }

        if (VDBG) logd("getDefaultSubId=" + subId);
        return subId;
    }

    /**
     * Returns the system's default voice subscription id.
     *
@@ -2055,22 +2068,6 @@ public class SubscriptionManager {
        return sDefaultDataSubIdCache.query(null);
    }

    private static int getDefaultDataSubscriptionIdInternal() {
        int subId = INVALID_SUBSCRIPTION_ID;

        try {
            ISub iSub = TelephonyManager.getSubscriptionService();
            if (iSub != null) {
                subId = iSub.getDefaultDataSubId();
            }
        } catch (RemoteException ex) {
            // ignore it
        }

        if (VDBG) logd("getDefaultDataSubscriptionId, sub id = " + subId);
        return subId;
    }

    /**
     * Set the subscription which will be used by default for data, with the subscription which
     * the supplied subscription ID corresponds to; or throw a RuntimeException if the supplied