一、優先級基本知識介紹
Android6.0之后系統中優先級設置都是根據Score分值來設置優先級,分值0-100,數值越高,越優先。
SIM卡網絡 50
wifi網絡 60
有線網絡 70
手機網絡設置都有自己的Factory設置類,都繼承自NetworkFactory.java
wifi網絡設置類:WifiNetworkFactory.java packages/modules/Wifi/service/java/com/android/server/wifi/WifiNetworkFactory.java
有線網絡設置類:EthernetNetworkFactory.java frameworks\opt\net\ethernet\java\com\android\server\ethernet\EthernetNetworkFactory.java
移動網絡設置類:TelephonyNetworkFactory.java
frameworks\opt\telephony\src\java\com\android\internal\telephony\dataconnection\TelephonyNetworkFactory.java
NetworkFactory的子類都有NETWORK_SCORE常量,表示該網絡的分值。
二、有線網絡優先級設置
1、在Android9.0設置有線網絡優先級直接修改EthernetNetworkFactory.java的 NETWORK_SCORE 值就行
private final static int NETWORK_SCORE = 55; //change score from 70
但是我Android11 的代碼修改后發現并不能生效,還是有線網優先。
研究了一下EthernetNetworkFactory.java和ConnectivityService.java發現里面的邏輯有很大的修改。
2、在Android11 修改有線網絡優先級
找到EthernetNetworkFactory.java的getNetworkScore()方法,這里面返回的score才是有線網的有效分值;
這個getNetworkScore()方法是在Android11 新增的。
在該方法返回NETWORK_SCORE值即可。里面很多判斷是沒啥用的。
如果要wifi優先級高于有線,一定要設置有線網絡的分值比wifi小,在后期測試過程中發現在某些情況,wifi的分值會變成20,把有線網絡分值設置成15才生效。
frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetNetworkFactory.java
private int getNetworkScore() {return 10;// never set the network score below 0.// if (!mLinkUp) {// return 0;// }// int[] transportTypes = mCapabilities.getTransportTypes();// if (transportTypes.length < 1) {// Log.w(TAG, "Network interface '" + mLinkProperties.getInterfaceName() + "' has no "// + "transport type associated with it. Score set to zero");// return 0;// }// TransportInfo transportInfo = sTransports.get(transportTypes[0], /* if dne */ null);// if (transportInfo != null) {// return transportInfo.mScore;// }// return 0;}
修改共存代碼 支持三網共存
frameworks/libs/net/common/src_servicescommon/android/net/NetworkFactory.java
private void evalRequest(NetworkRequestInfo n) {if (VDBG) {log("evalRequest");log(" n.requests = " + n.requested);log(" n.score = " + n.score);log(" mScore = " + mScore);log(" request.providerId = " + n.providerId);log(" mProvider.id = " + mProvider.getProviderId());}if (shouldNeedNetworkFor(n)) {if (VDBG) log(" needNetworkFor");needNetworkFor(n.request, n.score);n.requested = true;} else if (shouldReleaseNetworkFor(n)) {if (VDBG) log(" releaseNetworkFor");隱藏下面兩塊// releaseNetworkFor(n.request);// n.requested = false;} else {if (VDBG) log(" done");}}直接返回trueprivate boolean shouldNeedNetworkFor(NetworkRequestInfo n) {return true;// // If this request is already tracked, it doesn't qualify for need// return !n.requested// // If the score of this request is higher or equal to that of this factory and some// // other factory is responsible for it, then this factory should not track the request// // because it has no hope of satisfying it.// && (n.score < mScore || n.providerId == mProvider.getProviderId())// // If this factory can't satisfy the capability needs of this request, then it// // should not be tracked.// && n.request.canBeSatisfiedBy(mCapabilityFilter)// // Finally if the concrete implementation of the factory rejects the request, then// // don't track it.// && acceptRequest(n.request, n.score);}
frameworks/base/services/core/java/com/android/server/ConnectivityService.java
隱藏兩處nai.asyncChannel.disconnect();
private boolean maybeHandleAsyncChannelMessage(Message msg) {switch (msg.what) {default:return false;case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: {handleAsyncChannelHalfConnect(msg);break;}case AsyncChannel.CMD_CHANNEL_DISCONNECT: {NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo);// if (nai != null) nai.asyncChannel.disconnect();break;}case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {handleAsyncChannelDisconnected(msg);break;}}return true;}private void teardownUnneededNetwork(NetworkAgentInfo nai) {if (nai.numRequestNetworkRequests() != 0) {for (int i = 0; i < nai.numNetworkRequests(); i++) {NetworkRequest nr = nai.requestAt(i);// Ignore listening requests.if (nr.isListen()) continue;loge("Dead network still had at least " + nr);break;}}// nai.asyncChannel.disconnect();}