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

Commit 0034e2fd authored by Malcolm Chen's avatar Malcolm Chen Committed by Xiangyu/Malcolm Chen
Browse files

Use carrier config to define network validation cache ttl.

Also add a flag to control when to report validation pass upon
validated network cache hit (immediately upon request or wait until
network becomes available).

Bug: 140070796 148611362
Test: unittest
Change-Id: I1377f2d7b249b62d6aac6b581dc3ba4908527fc9
Merged-In: I1377f2d7b249b62d6aac6b581dc3ba4908527fc9
parent a9cbbc96
Loading
Loading
Loading
Loading
+32 −6
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.internal.telephony;

import static android.telephony.AccessNetworkConstants.TRANSPORT_TYPE_WWAN;
import static android.telephony.CarrierConfigManager.KEY_DATA_SWITCH_VALIDATION_MIN_GAP_LONG;
import static android.telephony.NetworkRegistrationInfo.DOMAIN_PS;

import android.content.Context;
@@ -25,6 +26,8 @@ import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.Handler;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.CellIdentity;
import android.telephony.CellIdentityLte;
import android.telephony.CellInfo;
@@ -49,6 +52,9 @@ import java.util.concurrent.TimeUnit;
 */
public class CellularNetworkValidator {
    private static final String LOG_TAG = "NetworkValidator";
    // If true, upon validated network cache hit, we report validationDone only when
    // network becomes available. Otherwise, we report validationDone immediately.
    private static boolean sWaitForNetworkAvailableWhenCacheHit = false;

    // States of validator. Only one validation can happen at once.
    // IDLE: no validation going on.
@@ -63,7 +69,7 @@ public class CellularNetworkValidator {
    // Singleton instance.
    private static CellularNetworkValidator sInstance;
    @VisibleForTesting
    public static long mValidationCacheTtl = TimeUnit.DAYS.toMillis(1);
    public static final long MAX_VALIDATION_CACHE_TTL = TimeUnit.DAYS.toMillis(1);

    private int mState = STATE_IDLE;
    private int mSubId;
@@ -82,7 +88,7 @@ public class CellularNetworkValidator {
    public Runnable mTimeoutCallback;
    private final ValidatedNetworkCache mValidatedNetworkCache = new ValidatedNetworkCache();

    private static class ValidatedNetworkCache {
    private class ValidatedNetworkCache {
        // A cache with fixed size. It remembers 10 most recently successfully validated networks.
        private static final int VALIDATED_NETWORK_CACHE_SIZE = 10;
        private final PriorityQueue<ValidatedNetwork> mValidatedNetworkPQ =
@@ -97,7 +103,7 @@ public class CellularNetworkValidator {
                });
        private final Map<String, ValidatedNetwork> mValidatedNetworkMap = new HashMap();

        private static final class ValidatedNetwork {
        private final class ValidatedNetwork {
            ValidatedNetwork(String identity, long timeStamp) {
                mValidationIdentity = identity;
                mValidationTimeStamp = timeStamp;
@@ -110,7 +116,9 @@ public class CellularNetworkValidator {
        }

        boolean isRecentlyValidated(int subId) {
            long cacheTtl = getValidationCacheTtl();
            long cacheTtl = getValidationCacheTtl(subId);
            if (cacheTtl == 0) return false;

            String networkIdentity = getValidationNetworkIdentity(subId);
            if (networkIdentity == null || !mValidatedNetworkMap.containsKey(networkIdentity)) {
                return false;
@@ -122,6 +130,8 @@ public class CellularNetworkValidator {
        }

        void storeLastValidationResult(int subId, boolean validated) {
            if (getValidationCacheTtl(subId) == 0) return;

            String networkIdentity = getValidationNetworkIdentity(subId);
            logd("storeLastValidationResult for subId " + subId
                    + (validated ? " validated." : " not validated."));
@@ -177,8 +187,18 @@ public class CellularNetworkValidator {
                    + ((CellIdentityLte) cellIdentity).getTac() + "_" + subId;
        }

        private long getValidationCacheTtl() {
            return mValidationCacheTtl;
        private long getValidationCacheTtl(int subId) {
            long ttl = 0;
            CarrierConfigManager configManager = (CarrierConfigManager)
                    mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
            if (configManager != null) {
                PersistableBundle b = configManager.getConfigForSubId(subId);
                if (b != null) {
                    ttl = b.getLong(KEY_DATA_SWITCH_VALIDATION_MIN_GAP_LONG);
                }
            }
            // Ttl can't be bigger than one day for now.
            return Math.min(ttl, MAX_VALIDATION_CACHE_TTL);
        }
    }

@@ -245,6 +265,12 @@ public class CellularNetworkValidator {
            stopValidation();
        }

        if (!sWaitForNetworkAvailableWhenCacheHit && mValidatedNetworkCache
                .isRecentlyValidated(subId)) {
            callback.onValidationResult(true, subId);
            return;
        }

        mState = STATE_VALIDATING;
        mSubId = subId;
        mTimeoutInMs = timeoutInMs;