Merge "Not defer network disconnect event in Probing state"

This commit is contained in:
Chiachang Wang
2018-11-12 05:30:46 +00:00
committed by Gerrit Code Review

View File

@@ -313,6 +313,7 @@ public class NetworkMonitor extends StateMachine {
private int mReevaluateDelayMs = INITIAL_REEVALUATE_DELAY_MS;
private int mEvaluateAttempts = 0;
private volatile int mProbeToken = 0;
public NetworkMonitor(Context context, Handler handler, NetworkAgentInfo networkAgentInfo,
NetworkRequest defaultRequest) {
@@ -838,7 +839,8 @@ public class NetworkMonitor extends StateMachine {
@Override
public void enter() {
mThread = new Thread(() -> sendMessage(obtainMessage(CMD_PROBE_COMPLETE,
final int token = ++mProbeToken;
mThread = new Thread(() -> sendMessage(obtainMessage(CMD_PROBE_COMPLETE, token, 0,
isCaptivePortal())));
mThread.start();
}
@@ -847,16 +849,13 @@ public class NetworkMonitor extends StateMachine {
public boolean processMessage(Message message) {
switch (message.what) {
case CMD_PROBE_COMPLETE:
// Currently, it's not possible to exit this state without mThread having
// terminated. Therefore, this state can never get CMD_PROBE_COMPLETE from a
// stale thread that is not mThread.
// TODO: As soon as it's possible to exit this state without mThread having
// terminated, ensure that CMD_PROBE_COMPLETE from stale threads are ignored.
// This could be done via a sequence number, or by changing mThread to a class
// that has a stopped volatile boolean or AtomicBoolean.
// Ensure that CMD_PROBE_COMPLETE from stale threads are ignored.
if (message.arg1 != mProbeToken) {
return HANDLED;
}
final CaptivePortalProbeResult probeResult =
(CaptivePortalProbeResult) message.obj;
if (probeResult.isSuccessful()) {
// Transit EvaluatingPrivateDnsState to get to Validated
// state (even if no Private DNS validation required).
@@ -883,6 +882,7 @@ public class NetworkMonitor extends StateMachine {
case CMD_REEVALUATE:
// Leave the event to EvaluatingState. Defer this message will result in reset
// of mReevaluateDelayMs and mEvaluateAttempts.
case CMD_NETWORK_DISCONNECTED:
return NOT_HANDLED;
default:
// TODO: Some events may able to handle in this state, instead of deferring to
@@ -894,11 +894,9 @@ public class NetworkMonitor extends StateMachine {
@Override
public void exit() {
// If StateMachine get here, the probe started in enter() is guaranteed to have
// completed, because in this state, all messages except CMD_PROBE_COMPLETE and
// CMD_REEVALUATE are deferred. CMD_REEVALUATE cannot be in the queue, because it is
// only ever sent in EvaluatingState#enter, and the StateMachine reach this state by
// processing it. Therefore, there is no need to stop the thread.
if (mThread.isAlive()) {
mThread.interrupt();
}
mThread = null;
}
}