一、优先级基本知识介绍
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");
}
}
直接返回true
private 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();
}