Allow connecting with no requests if we're already connected.

Currently, we drop all connection start attempts if we do not
have any NetworkRequests that we can fulfil. This can happen if
the default network has a score that's higher than our score
filter. The reason for this is that we don't want to connect a
new network if we're not already connected: if we did,
ConnectivityService would immediately request that the network be
torn down.

However, it does make sense to connect if we are already
connected, because that means that we ourselves are the
highest-scoring network - otherwise, ConnectivityService would
have torn us down. So, when deciding whether to process a
connection request, allow it if mNetworkAgent is non-null, even
if there are no NetworkRequests that we can fulfil.

In order to do this, move the check from startConnectToNetwork to
the code that processes CMD_START_CONNECT, because it's not safe
to access mNetworkAgent except from the handler thread. Moving
the check also seems more correct in general, because
the return value of hasConnectionRequests() could change between
when startConnectToNetwork is called and when the resulting
CMD_START_CONNECT is processed.

The connect requests are only allowed from the settings/sysui. We don't
allow any non-privileged apps to be able to override the user's decision
to stay connected to a network with no internet access (which causes the
mNetworkAgent.releaseNetworkFor()).

Bug: 65262556
Test: Unit tests for START_CONNECT handling for 4 scenarios:
a) START_CONNECT with the default network request.
b) START_CONNECT with no network request.
c) START_CONNECT with no network request, but already connected
d) START_CONNECT with no network request, but already connected, but
from non-privileged app.
Test: Settings can connect to a new network when wifi has a score of 100

Change-Id: Iad582b5109a81f238f335eaac3e7264c5da6e9b1
4 files changed