Merge "Re-factor state machine in NetworkMonitor"
am: 2339da24d8
Change-Id: I6d3ca73a09cdb145160729fee57ceca88493812c
This commit is contained in:
@@ -313,6 +313,7 @@ public class NetworkMonitor extends StateMachine {
|
|||||||
private final State mCaptivePortalState = new CaptivePortalState();
|
private final State mCaptivePortalState = new CaptivePortalState();
|
||||||
private final State mEvaluatingPrivateDnsState = new EvaluatingPrivateDnsState();
|
private final State mEvaluatingPrivateDnsState = new EvaluatingPrivateDnsState();
|
||||||
private final State mProbingState = new ProbingState();
|
private final State mProbingState = new ProbingState();
|
||||||
|
private final State mWaitingForNextProbeState = new WaitingForNextProbeState();
|
||||||
|
|
||||||
private CustomIntentReceiver mLaunchCaptivePortalAppBroadcastReceiver = null;
|
private CustomIntentReceiver mLaunchCaptivePortalAppBroadcastReceiver = null;
|
||||||
|
|
||||||
@@ -368,6 +369,7 @@ public class NetworkMonitor extends StateMachine {
|
|||||||
addState(mMaybeNotifyState, mDefaultState);
|
addState(mMaybeNotifyState, mDefaultState);
|
||||||
addState(mEvaluatingState, mMaybeNotifyState);
|
addState(mEvaluatingState, mMaybeNotifyState);
|
||||||
addState(mProbingState, mEvaluatingState);
|
addState(mProbingState, mEvaluatingState);
|
||||||
|
addState(mWaitingForNextProbeState, mEvaluatingState);
|
||||||
addState(mCaptivePortalState, mMaybeNotifyState);
|
addState(mCaptivePortalState, mMaybeNotifyState);
|
||||||
addState(mEvaluatingPrivateDnsState, mDefaultState);
|
addState(mEvaluatingPrivateDnsState, mDefaultState);
|
||||||
addState(mValidatedState, mDefaultState);
|
addState(mValidatedState, mDefaultState);
|
||||||
@@ -877,6 +879,11 @@ public class NetworkMonitor extends StateMachine {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void enter() {
|
public void enter() {
|
||||||
|
if (mEvaluateAttempts >= BLAME_FOR_EVALUATION_ATTEMPTS) {
|
||||||
|
//Don't continue to blame UID forever.
|
||||||
|
TrafficStats.clearThreadStatsUid();
|
||||||
|
}
|
||||||
|
|
||||||
final int token = ++mProbeToken;
|
final int token = ++mProbeToken;
|
||||||
mThread = new Thread(() -> sendMessage(obtainMessage(CMD_PROBE_COMPLETE, token, 0,
|
mThread = new Thread(() -> sendMessage(obtainMessage(CMD_PROBE_COMPLETE, token, 0,
|
||||||
isCaptivePortal())));
|
isCaptivePortal())));
|
||||||
@@ -904,29 +911,16 @@ public class NetworkMonitor extends StateMachine {
|
|||||||
mLastPortalProbeResult = probeResult;
|
mLastPortalProbeResult = probeResult;
|
||||||
transitionTo(mCaptivePortalState);
|
transitionTo(mCaptivePortalState);
|
||||||
} else {
|
} else {
|
||||||
final Message msg = obtainMessage(CMD_REEVALUATE, ++mReevaluateToken, 0);
|
|
||||||
sendMessageDelayed(msg, mReevaluateDelayMs);
|
|
||||||
logNetworkEvent(NetworkEvent.NETWORK_VALIDATION_FAILED);
|
logNetworkEvent(NetworkEvent.NETWORK_VALIDATION_FAILED);
|
||||||
notifyNetworkTestResultInvalid(probeResult.redirectUrl);
|
notifyNetworkTestResultInvalid(probeResult.redirectUrl);
|
||||||
if (mEvaluateAttempts >= BLAME_FOR_EVALUATION_ATTEMPTS) {
|
transitionTo(mWaitingForNextProbeState);
|
||||||
// Don't continue to blame UID forever.
|
|
||||||
TrafficStats.clearThreadStatsUid();
|
|
||||||
}
|
|
||||||
mReevaluateDelayMs *= 2;
|
|
||||||
if (mReevaluateDelayMs > MAX_REEVALUATE_DELAY_MS) {
|
|
||||||
mReevaluateDelayMs = MAX_REEVALUATE_DELAY_MS;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return HANDLED;
|
return HANDLED;
|
||||||
case CMD_REEVALUATE:
|
|
||||||
// Leave the event to EvaluatingState. Defer this message will result in reset
|
|
||||||
// of mReevaluateDelayMs and mEvaluateAttempts.
|
|
||||||
case CMD_NETWORK_DISCONNECTED:
|
|
||||||
case EVENT_DNS_NOTIFICATION:
|
case EVENT_DNS_NOTIFICATION:
|
||||||
|
// Leave the event to DefaultState to record correct dns timestamp.
|
||||||
return NOT_HANDLED;
|
return NOT_HANDLED;
|
||||||
default:
|
default:
|
||||||
// TODO: Some events may able to handle in this state, instead of deferring to
|
// Wait for probe result and defer events to next state by default.
|
||||||
// next state.
|
|
||||||
deferMessage(message);
|
deferMessage(message);
|
||||||
return HANDLED;
|
return HANDLED;
|
||||||
}
|
}
|
||||||
@@ -941,6 +935,29 @@ public class NetworkMonitor extends StateMachine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Being in the WaitingForNextProbeState indicates that evaluating probes failed and state is
|
||||||
|
// transited from ProbingState. This ensures that the state machine is only in ProbingState
|
||||||
|
// while a probe is in progress, not while waiting to perform the next probe. That allows
|
||||||
|
// ProbingState to defer most messages until the probe is complete, which keeps the code simple
|
||||||
|
// and matches the pre-Q behaviour where probes were a blocking operation performed on the state
|
||||||
|
// machine thread.
|
||||||
|
private class WaitingForNextProbeState extends State {
|
||||||
|
@Override
|
||||||
|
public void enter() {
|
||||||
|
final Message msg = obtainMessage(CMD_REEVALUATE, ++mReevaluateToken, 0);
|
||||||
|
sendMessageDelayed(msg, mReevaluateDelayMs);
|
||||||
|
mReevaluateDelayMs *= 2;
|
||||||
|
if (mReevaluateDelayMs > MAX_REEVALUATE_DELAY_MS) {
|
||||||
|
mReevaluateDelayMs = MAX_REEVALUATE_DELAY_MS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean processMessage(Message message) {
|
||||||
|
return NOT_HANDLED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Limits the list of IP addresses returned by getAllByName or tried by openConnection to at
|
// Limits the list of IP addresses returned by getAllByName or tried by openConnection to at
|
||||||
// most one per address family. This ensures we only wait up to 20 seconds for TCP connections
|
// most one per address family. This ensures we only wait up to 20 seconds for TCP connections
|
||||||
// to complete, regardless of how many IP addresses a host has.
|
// to complete, regardless of how many IP addresses a host has.
|
||||||
|
|||||||
Reference in New Issue
Block a user