in NearbyConnectionsWalkieTalkie/app/src/manual/java/com/google/location/nearby/apps/walkietalkie/MainActivity.java [309:391]
private void onStateChanged(State oldState, State newState) {
if (mCurrentAnimator != null && mCurrentAnimator.isRunning()) {
mCurrentAnimator.cancel();
}
// Update Nearby Connections to the new state.
switch (newState) {
case DISCOVERING:
if (isAdvertising()) {
stopAdvertising();
}
disconnectFromAllEndpoints();
startDiscovering();
break;
case ADVERTISING:
if (isDiscovering()) {
stopDiscovering();
}
disconnectFromAllEndpoints();
startAdvertising();
break;
case CONNECTED:
if (isDiscovering()) {
stopDiscovering();
} else if (isAdvertising()) {
// Continue to advertise, so others can still connect,
// but clear the discover runnable.
removeCallbacks(mDiscoverRunnable);
}
break;
case UNKNOWN:
stopAllEndpoints();
break;
default:
// no-op
break;
}
// Update the UI.
switch (oldState) {
case UNKNOWN:
// Unknown is our initial state. Whatever state we move to,
// we're transitioning forwards.
transitionForward(oldState, newState);
break;
case DISCOVERING:
switch (newState) {
case UNKNOWN:
transitionBackward(oldState, newState);
break;
case ADVERTISING:
case CONNECTED:
transitionForward(oldState, newState);
break;
default:
// no-op
break;
}
break;
case ADVERTISING:
switch (newState) {
case UNKNOWN:
case DISCOVERING:
transitionBackward(oldState, newState);
break;
case CONNECTED:
transitionForward(oldState, newState);
break;
default:
// no-op
break;
}
break;
case CONNECTED:
// Connected is our final state. Whatever new state we move to,
// we're transitioning backwards.
transitionBackward(oldState, newState);
break;
default:
// no-op
break;
}
}