am e57064a2: Merge "Don\'t bother validating networks that don\'t satisfy the default request." into lmp-mr1-dev

* commit 'e57064a289082758fcb9bb71f987dde84931e3e6':
  Don't bother validating networks that don't satisfy the default request.
This commit is contained in:
Paul Jensen
2014-11-24 18:36:27 +00:00
committed by Android Git Automerger
3 changed files with 30 additions and 12 deletions

View File

@@ -3541,6 +3541,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
private final HashMap<Messenger, NetworkAgentInfo> mNetworkAgentInfos = private final HashMap<Messenger, NetworkAgentInfo> mNetworkAgentInfos =
new HashMap<Messenger, NetworkAgentInfo>(); new HashMap<Messenger, NetworkAgentInfo>();
// Note: if mDefaultRequest is changed, NetworkMonitor needs to be updated.
private final NetworkRequest mDefaultRequest; private final NetworkRequest mDefaultRequest;
private boolean isDefaultNetwork(NetworkAgentInfo nai) { private boolean isDefaultNetwork(NetworkAgentInfo nai) {
@@ -3552,10 +3553,12 @@ public class ConnectivityService extends IConnectivityManager.Stub
int currentScore, NetworkMisc networkMisc) { int currentScore, NetworkMisc networkMisc) {
enforceConnectivityInternalPermission(); enforceConnectivityInternalPermission();
// TODO: Instead of passing mDefaultRequest, provide an API to determine whether a Network
// satisfies mDefaultRequest.
NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(), NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(),
new NetworkInfo(networkInfo), new LinkProperties(linkProperties), new NetworkInfo(networkInfo), new LinkProperties(linkProperties),
new NetworkCapabilities(networkCapabilities), currentScore, mContext, mTrackerHandler, new NetworkCapabilities(networkCapabilities), currentScore, mContext, mTrackerHandler,
new NetworkMisc(networkMisc)); new NetworkMisc(networkMisc), mDefaultRequest);
synchronized (this) { synchronized (this) {
nai.networkMonitor.systemReady = mSystemReady; nai.networkMonitor.systemReady = mSystemReady;
} }

View File

@@ -68,7 +68,7 @@ public class NetworkAgentInfo {
public NetworkAgentInfo(Messenger messenger, AsyncChannel ac, NetworkInfo info, public NetworkAgentInfo(Messenger messenger, AsyncChannel ac, NetworkInfo info,
LinkProperties lp, NetworkCapabilities nc, int score, Context context, Handler handler, LinkProperties lp, NetworkCapabilities nc, int score, Context context, Handler handler,
NetworkMisc misc) { NetworkMisc misc, NetworkRequest defaultRequest) {
this.messenger = messenger; this.messenger = messenger;
asyncChannel = ac; asyncChannel = ac;
network = null; network = null;
@@ -76,7 +76,7 @@ public class NetworkAgentInfo {
linkProperties = lp; linkProperties = lp;
networkCapabilities = nc; networkCapabilities = nc;
currentScore = score; currentScore = score;
networkMonitor = new NetworkMonitor(context, handler, this); networkMonitor = new NetworkMonitor(context, handler, this, defaultRequest);
networkMisc = misc; networkMisc = misc;
created = false; created = false;
validated = false; validated = false;

View File

@@ -27,6 +27,7 @@ import android.net.ConnectivityManager;
import android.net.Network; import android.net.Network;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.net.TrafficStats; import android.net.TrafficStats;
import android.net.wifi.WifiInfo; import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
@@ -222,6 +223,7 @@ public class NetworkMonitor extends StateMachine {
private final TelephonyManager mTelephonyManager; private final TelephonyManager mTelephonyManager;
private final WifiManager mWifiManager; private final WifiManager mWifiManager;
private final AlarmManager mAlarmManager; private final AlarmManager mAlarmManager;
private final NetworkRequest mDefaultRequest;
private String mServer; private String mServer;
private boolean mIsCaptivePortalCheckEnabled = false; private boolean mIsCaptivePortalCheckEnabled = false;
@@ -239,7 +241,8 @@ public class NetworkMonitor extends StateMachine {
private State mCaptivePortalState = new CaptivePortalState(); private State mCaptivePortalState = new CaptivePortalState();
private State mLingeringState = new LingeringState(); private State mLingeringState = new LingeringState();
public NetworkMonitor(Context context, Handler handler, NetworkAgentInfo networkAgentInfo) { public NetworkMonitor(Context context, Handler handler, NetworkAgentInfo networkAgentInfo,
NetworkRequest defaultRequest) {
// Add suffix indicating which NetworkMonitor we're talking about. // Add suffix indicating which NetworkMonitor we're talking about.
super(TAG + networkAgentInfo.name()); super(TAG + networkAgentInfo.name());
@@ -249,6 +252,7 @@ public class NetworkMonitor extends StateMachine {
mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mDefaultRequest = defaultRequest;
addState(mDefaultState); addState(mDefaultState);
addState(mOfflineState, mDefaultState); addState(mOfflineState, mDefaultState);
@@ -369,14 +373,25 @@ public class NetworkMonitor extends StateMachine {
case CMD_REEVALUATE: case CMD_REEVALUATE:
if (message.arg1 != mReevaluateToken) if (message.arg1 != mReevaluateToken)
return HANDLED; return HANDLED;
if (mNetworkAgentInfo.isVPN()) { // Don't bother validating networks that don't satisify the default request.
transitionTo(mValidatedState); // This includes:
return HANDLED; // - VPNs which can be considered explicitly desired by the user and the
} // user's desire trumps whether the network validates.
// If network provides no internet connectivity adjust evaluation. // - Networks that don't provide internet access. It's unclear how to
if (!mNetworkAgentInfo.networkCapabilities.hasCapability( // validate such networks.
NetworkCapabilities.NET_CAPABILITY_INTERNET)) { // - Untrusted networks. It's unsafe to prompt the user to sign-in to
// TODO: Try to verify something works. Do all gateways respond to pings? // such networks and the user didn't express interest in connecting to
// such networks (an app did) so the user may be unhappily surprised when
// asked to sign-in to a network they didn't want to connect to in the
// first place. Validation could be done to adjust the network scores
// however these networks are app-requested and may not be intended for
// general usage, in which case general validation may not be an accurate
// measure of the network's quality. Only the app knows how to evaluate
// the network so don't bother validating here. Furthermore sending HTTP
// packets over the network may be undesirable, for example an extremely
// expensive metered network, or unwanted leaking of the User Agent string.
if (!mDefaultRequest.networkCapabilities.satisfiedByNetworkCapabilities(
mNetworkAgentInfo.networkCapabilities)) {
transitionTo(mValidatedState); transitionTo(mValidatedState);
return HANDLED; return HANDLED;
} }