(一百九十七)Android Q 学习WiFi的评分机制(四)

1.NetworkAgentInfo对评分的再处理

    private int getCurrentScore(boolean pretendValidated) {
        // TODO: We may want to refactor this into a NetworkScore class that takes a base score from
        // the NetworkAgent and signals from the NetworkAgent and uses those signals to modify the
        // score.  The NetworkScore class would provide a nice place to centralize score constants
        // so they are not scattered about the transports.

        // If this network is explicitly selected and the user has decided to use it even if it's
        // unvalidated, give it the maximum score. Also give it the maximum score if it's explicitly
        // selected and we're trying to see what its score could be. This ensures that we don't tear
        // down an explicitly selected network before the user gets a chance to prefer it when
        // a higher-scoring network (e.g., Ethernet) is available.
        if (networkMisc.explicitlySelected && (networkMisc.acceptUnvalidated || pretendValidated)) {
            return ConnectivityConstants.EXPLICITLY_SELECTED_NETWORK_SCORE;
        }

        int score = currentScore;
        if (!lastValidated && !pretendValidated && !ignoreWifiUnvalidationPenalty() && !isVPN()) {
            score -= ConnectivityConstants.UNVALIDATED_SCORE_PENALTY;
        }
        if (score < 0) score = 0;
        return score;
    }

简单来说

1)当一个网络是用户手动选择的并且可接收不能上网的话,那就给这个网络最高分,100分

2)如果未校验通过,并且不忽略网络可达性,并且也不是vpn,那么就减去40分

 

贴一下评分的定义

/**
 * A class encapsulating various constants used by Connectivity.
 * @hide
 */
public class ConnectivityConstants {

    // Penalty applied to scores of Networks that have not been validated.
    public static final int UNVALIDATED_SCORE_PENALTY = 40;

    // Score for explicitly connected network.
    //
    // This ensures that a) the explicitly selected network is never trumped by anything else, and
    // b) the explicitly selected network is never torn down.
    public static final int EXPLICITLY_SELECTED_NETWORK_SCORE = 100;
    // VPNs typically have priority over other networks. Give them a score that will
    // let them win every single time.
    public static final int VPN_DEFAULT_SCORE = 101;
}

 

2.总结

评分机制梳理的差不多了,大概是WiFi状态机那边会对评分作一个计算,移动网络估计也有类似的计算,计算完了统一传到CS这边进行所有网络的比较,如果是用户手动选择并且可忽略网络可达性,那么直接给最高分100,如果不能上网就减去40,以此计算结果去进行得分比较,得出最符合网络请求的网络。

 

相关推荐
©️2020 CSDN 皮肤主题: 鲸 设计师:meimeiellie 返回首页