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

Commit 6327115d authored by Malcolm Chen's avatar Malcolm Chen Committed by Jack Yu
Browse files

Dynamically calculate network score.

To prepare for test before break for CBRS, we need to assign
different scores based on its network request it's serving.

Bug: 118348832
Test: manual
Merged-In: I5dec8d299602479d37e55a8e22344cdcd37e4341
Change-Id: I5dec8d299602479d37e55a8e22344cdcd37e4341
(cherry picked from commit 0b3da149)
parent 29da5b3c
Loading
Loading
Loading
Loading
+17 −1
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import com.android.internal.util.IndentingPrintWriter;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

@@ -146,7 +147,6 @@ public class ApnContext {
        return mDataConnection;
    }


    /**
     * Set the associated data connection.
     * @param dc data connection
@@ -422,6 +422,11 @@ public class ApnContext {
                mLocalLogs.add(log);
                mNetworkRequests.add(networkRequest);
                mDcTracker.enableApn(ApnSetting.getApnTypesBitmaskFromString(mApnType), type);
                if (mDataConnection != null) {
                    // New network request added. Should re-evaluate properties of
                    // the data connection. For example, the score may change.
                    mDataConnection.reevaluateDataConnectionProperties();
                }
            }
        }
    }
@@ -439,6 +444,11 @@ public class ApnContext {
                        + networkRequest + ")");
            } else {
                mNetworkRequests.remove(networkRequest);
                if (mDataConnection != null) {
                    // New network request added. Should re-evaluate properties of
                    // the data connection. For example, the score may change.
                    mDataConnection.reevaluateDataConnectionProperties();
                }
                log.log("ApnContext.releaseNetwork left with " + mNetworkRequests.size() +
                        " requests.");
                if (mNetworkRequests.size() == 0
@@ -630,6 +640,12 @@ public class ApnContext {
        return apnType;
    }

    public List<NetworkRequest> getNetworkRequests() {
        synchronized (mRefCountLock) {
            return new ArrayList<NetworkRequest>(mNetworkRequests);
        }
    }

    @Override
    public synchronized String toString() {
        // We don't print mDataConnection because its recursive.
+68 −2
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.net.NetworkAgent;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkMisc;
import android.net.NetworkRequest;
import android.net.NetworkUtils;
import android.net.ProxyInfo;
import android.net.RouteInfo;
@@ -108,6 +109,18 @@ public class DataConnection extends StateMachine {

    private static final String NETWORK_TYPE = "MOBILE";

    // The data connection providing default Internet connection will have a higher score of 50.
    // Other connections will have a slightly lower score of 45. The intention is other connections
    // will not cause ConnectivityService to tear down default internet connection. For example,
    // to validate Internet connection on non-default data SIM, we'll set up a temporary Internet
    // connection on that data SIM. In this case, score of 45 is assigned so ConnectivityService
    // will not replace the default Internet connection with it.
    private static final int DEFAULT_INTERNET_CONNECTION_SCORE = 50;
    private static final int OTHER_CONNECTION_SCORE = 45;

    // The score we report to connectivity service
    private int mScore;

    // The data connection controller
    private DcController mDcController;

@@ -240,8 +253,10 @@ public class DataConnection extends StateMachine {
    static final int EVENT_LINK_CAPACITY_CHANGED = BASE + 23;
    static final int EVENT_RESET = BASE + 24;
    static final int EVENT_REEVALUATE_RESTRICTED_STATE = BASE + 25;
    static final int EVENT_REEVALUATE_DATA_CONNECTION_PROPERTIES = BASE + 26;

    private static final int CMD_TO_STRING_COUNT = EVENT_REEVALUATE_RESTRICTED_STATE - BASE + 1;
    private static final int CMD_TO_STRING_COUNT =
            EVENT_REEVALUATE_DATA_CONNECTION_PROPERTIES - BASE + 1;

    private static String[] sCmdToString = new String[CMD_TO_STRING_COUNT];
    static {
@@ -275,6 +290,8 @@ public class DataConnection extends StateMachine {
        sCmdToString[EVENT_RESET - BASE] = "EVENT_RESET";
        sCmdToString[EVENT_REEVALUATE_RESTRICTED_STATE - BASE] =
                "EVENT_REEVALUATE_RESTRICTED_STATE";
        sCmdToString[EVENT_REEVALUATE_DATA_CONNECTION_PROPERTIES - BASE] =
                "EVENT_REEVALUATE_DATA_CONNECTION_PROPERTIES";
    }
    // Convert cmd to string or null if unknown
    static String cmdToString(int cmd) {
@@ -1814,9 +1831,10 @@ public class DataConnection extends StateMachine {
                    loge("Cannot find the data connection for handover.");
                }
            } else {
                mScore = calculateScore();
                mNetworkAgent = new DcNetworkAgent(getHandler().getLooper(), mPhone.getContext(),
                        "DcNetworkAgent", mNetworkInfo, getNetworkCapabilities(), mLinkProperties,
                        50, misc);
                        mScore, misc);
            }
            if (mDataServiceManager.getTransportType() == TransportType.WWAN) {
                mPhone.mCi.registerForNattKeepaliveStatus(
@@ -2123,6 +2141,12 @@ public class DataConnection extends StateMachine {
                    retVal = HANDLED;
                    break;
                }
                case EVENT_REEVALUATE_DATA_CONNECTION_PROPERTIES: {
                    // Update other properties like link properties if needed in future.
                    updateScore();
                    retVal = HANDLED;
                    break;
                }
                default:
                    if (VDBG) {
                        log("DcActiveState not handled msg.what=" + getWhatToString(msg.what));
@@ -2536,6 +2560,15 @@ public class DataConnection extends StateMachine {
        if (DBG) log("reevaluate restricted state");
    }

    /**
     * Re-evaluate the data connection properties. For example, it will recalculate data connection
     * score and update through network agent it if changed.
     */
    void reevaluateDataConnectionProperties() {
        sendMessage(EVENT_REEVALUATE_DATA_CONNECTION_PROPERTIES);
        if (DBG) log("reevaluate data connection properties");
    }

    /**
     * @return The parameters used for initiating a data connection.
     */
@@ -2743,6 +2776,38 @@ public class DataConnection extends StateMachine {
        }, null);
    }

    /**
     *  Re-calculate score and update through network agent if it changes.
     */
    private void updateScore() {
        int oldScore = mScore;
        mScore = calculateScore();
        if (oldScore != mScore) {
            log("Updating score from " + oldScore + " to " + mScore);
            mNetworkAgent.sendNetworkScore(mScore);
        }
    }

    private int calculateScore() {
        int score = OTHER_CONNECTION_SCORE;

        // If it's serving a network request that asks NET_CAPABILITY_INTERNET and doesn't have
        // specify a subId, this dataConnection is considered to be default Internet data
        // connection. In this case we assign a slightly higher score of 50. The intention is
        // it will not be replaced by other data connections accidentally in DSDS usecase.
        for (ApnContext apnContext : mApnContexts.keySet()) {
            for (NetworkRequest networkRequest : apnContext.getNetworkRequests()) {
                if (networkRequest.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                        && networkRequest.networkCapabilities.getNetworkSpecifier() == null) {
                    score = DEFAULT_INTERNET_CONNECTION_SCORE;
                    break;
                }
            }
        }

        return score;
    }

    /**
     * Dump the current state.
     *
@@ -2781,6 +2846,7 @@ public class DataConnection extends StateMachine {
        pw.println("mUnmeteredUseOnly=" + mUnmeteredUseOnly);
        pw.println("mInstanceNumber=" + mInstanceNumber);
        pw.println("mAc=" + mAc);
        pw.println("mScore=" + mScore);
        pw.println("Network capabilities changed history:");
        pw.increaseIndent();
        mNetCapsLocalLog.dump(fd, pw, args);