一、NetworkMonitor
packages/modules/NetworkStack/src/com/android/server/connectivity/NetworkMonitor.java
网络上都叫网络可用性校验,在WIFI和数据(以太网切换中使用)
有多个状态机
方法:isCaptivePortal
11-03 12:01:17.885 2535 4681 D NetworkMonitor/100: isCaptivePortal: isSuccessful()=false isPortal()=false RedirectUrl=null isPartialConnectivity()=false Time=23029ms
类 EvaluationState下的reportEvaluationResult
----------------------------------------------------------------------------------------------------------------------
父类下的notifyNetworkTested,会将校验结果发送给ConnectivityService
private final INetworkMonitorCallbacks mCallback;
private void notifyNetworkTested(NetworkTestResultParcelable result) {
mCallback.notifyNetworkTestedWithExtras(result);
}
ConnectivityService.java里有个子类NetworkMonitorCallbacks 重写了notifyNetworkTestedWithExtras
NetworkMonitorCallbacks是在registerNetworkAgent里注册的
mDeps.getNetworkStack().makeNetworkMonitor(
nai.network, name, new NetworkMonitorCallbacks(nai));
-----------------------------------------------------------------------------------------------------------------------
二、NetworkMonitor与ConnectivityService的回调
class EvaluationState 应该是上报数据给ConnectivityService的
reportEvaluationResult
notifyNetworkTested
mCallback.notifyNetworkTestedWithExtras
isCaptivePortal
else if (mUseHttps && httpsUrls.length == 1 && httpUrls.length == 1)
sendHttpAndHttpsParallelWithFallbackProbes
reportHttpProbeResult(NETWORK_VALIDATION_PROBE_FALLBACK) (16)
reportHttpProbeResult(NETWORK_VALIDATION_PROBE_HTTPS) (32)
packages/modules/NetworkStack/src/com/android/server/connectivity/NetworkMonitor.java
sendDnsProbe
sendDnsProbeWithTimeout //这个是阻塞执行
DnsUtils.getAllByName
11-28 10:57:11.770 2414 4178 D NetworkMonitor/100: PROBE_DNS connectivitycheck.gstatic.com 12508ms FAIL in type ADDRCONFIG Timeout
这个日志一直没找到在哪儿打印,后来发现是在这儿
protected InetAddress[] sendDnsProbeWithTimeout(String host, int timeoutMs)
throws UnknownHostException {
return DnsUtils.getAllByName(mDependencies.getDnsResolver(), mCleartextDnsNetwork, host,
TYPE_ADDRCONFIG, FLAG_EMPTY, timeoutMs,
str -> validationLog(ValidationProbeEvent.PROBE_DNS, host, str));
}
private void validationLog(int probeType, Object url, String msg) {
String probeName = ValidationProbeEvent.getProbeName(probeType);
//自己加的日志
Log.i(TAG, "validationLog, probeName = " + probeName + " url = " + url.toString());
validationLog(String.format("%s %s %s", probeName, url, msg));
}