Merge "Fix NetworkStatsAccessTest"
diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java
index c96d19d..9e8acd0 100644
--- a/core/java/android/net/ConnectivityManager.java
+++ b/core/java/android/net/ConnectivityManager.java
@@ -1466,9 +1466,7 @@
// Map from type to transports.
final int NOT_FOUND = -1;
final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
- if (transport == NOT_FOUND) {
- throw new IllegalArgumentException("unknown legacy type: " + type);
- }
+ Preconditions.checkArgument(transport != NOT_FOUND, "unknown legacy type: " + type);
nc.addTransportType(transport);
// Map from type to capabilities.
@@ -1814,9 +1812,7 @@
*/
public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
INetworkActivityListener rl = mNetworkActivityListeners.get(l);
- if (rl == null) {
- throw new IllegalArgumentException("Listener not registered: " + l);
- }
+ Preconditions.checkArgument(rl != null, "Listener was not registered.");
try {
getNetworkManagementService().unregisterNetworkActivityListener(rl);
} catch (RemoteException e) {
@@ -1873,9 +1869,8 @@
/** {@hide} */
public static final void enforceTetherChangePermission(Context context, String callingPkg) {
- if (null == context || null == callingPkg) {
- throw new IllegalArgumentException("arguments should not be null");
- }
+ Preconditions.checkNotNull(context, "Context cannot be null");
+ Preconditions.checkNotNull(callingPkg, "callingPkg cannot be null");
if (context.getResources().getStringArray(
com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
@@ -2773,84 +2768,67 @@
}
CallbackHandler(Handler handler) {
- this(handler.getLooper());
+ this(Preconditions.checkNotNull(handler, "Handler cannot be null.").getLooper());
}
@Override
public void handleMessage(Message message) {
- NetworkRequest request = getObject(message, NetworkRequest.class);
- Network network = getObject(message, Network.class);
+ if (message.what == EXPIRE_LEGACY_REQUEST) {
+ expireRequest((NetworkCapabilities) message.obj, message.arg1);
+ return;
+ }
+
+ final NetworkRequest request = getObject(message, NetworkRequest.class);
+ final Network network = getObject(message, Network.class);
+ final NetworkCallback callback;
+ synchronized (sCallbacks) {
+ callback = sCallbacks.get(request);
+ }
if (DBG) {
Log.d(TAG, getCallbackName(message.what) + " for network " + network);
}
+ if (callback == null) {
+ Log.w(TAG, "callback not found for " + getCallbackName(message.what) + " message");
+ return;
+ }
+
switch (message.what) {
case CALLBACK_PRECHECK: {
- NetworkCallback callback = getCallback(message);
- if (callback != null) {
- callback.onPreCheck(network);
- }
+ callback.onPreCheck(network);
break;
}
case CALLBACK_AVAILABLE: {
- NetworkCallback callback = getCallback(message);
- if (callback != null) {
- callback.onAvailable(network);
- }
+ callback.onAvailable(network);
break;
}
case CALLBACK_LOSING: {
- NetworkCallback callback = getCallback(message);
- if (callback != null) {
- callback.onLosing(network, message.arg1);
- }
+ callback.onLosing(network, message.arg1);
break;
}
case CALLBACK_LOST: {
- NetworkCallback callback = getCallback(message);
- if (callback != null) {
- callback.onLost(network);
- }
+ callback.onLost(network);
break;
}
case CALLBACK_UNAVAIL: {
- NetworkCallback callback = getCallback(message);
- if (callback != null) {
- callback.onUnavailable();
- }
+ callback.onUnavailable();
break;
}
case CALLBACK_CAP_CHANGED: {
- NetworkCallback callback = getCallback(message);
- if (callback != null) {
- NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
- callback.onCapabilitiesChanged(network, cap);
- }
+ NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
+ callback.onCapabilitiesChanged(network, cap);
break;
}
case CALLBACK_IP_CHANGED: {
- NetworkCallback callback = getCallback(message);
- if (callback != null) {
- LinkProperties lp = getObject(message, LinkProperties.class);
- callback.onLinkPropertiesChanged(network, lp);
- }
+ LinkProperties lp = getObject(message, LinkProperties.class);
+ callback.onLinkPropertiesChanged(network, lp);
break;
}
case CALLBACK_SUSPENDED: {
- NetworkCallback callback = getCallback(message);
- if (callback != null) {
- callback.onNetworkSuspended(network);
- }
+ callback.onNetworkSuspended(network);
break;
}
case CALLBACK_RESUMED: {
- NetworkCallback callback = getCallback(message);
- if (callback != null) {
- callback.onNetworkResumed(network);
- }
- break;
- }
- case EXPIRE_LEGACY_REQUEST: {
- expireRequest((NetworkCapabilities)message.obj, message.arg1);
+ callback.onNetworkResumed(network);
break;
}
}
@@ -2859,18 +2837,6 @@
private <T> T getObject(Message msg, Class<T> c) {
return (T) msg.getData().getParcelable(c.getSimpleName());
}
-
- private NetworkCallback getCallback(Message msg) {
- final NetworkRequest req = getObject(msg, NetworkRequest.class);
- final NetworkCallback callback;
- synchronized(sCallbacks) {
- callback = sCallbacks.get(req);
- }
- if (callback == null) {
- Log.w(TAG, "callback not found for " + getCallbackName(msg.what) + " message");
- }
- return callback;
- }
}
private CallbackHandler getDefaultHandler() {
@@ -2890,7 +2856,7 @@
private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
int timeoutMs, int action, int legacyType, CallbackHandler handler) {
- Preconditions.checkArgument(callback != null, "null NetworkCallback");
+ checkCallbackNotNull(callback);
Preconditions.checkArgument(action == REQUEST || need != null, "null NetworkCapabilities");
final NetworkRequest request;
try {
@@ -3042,14 +3008,11 @@
*/
public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
int timeoutMs) {
- if (timeoutMs <= 0) {
- throw new IllegalArgumentException("Non-positive timeoutMs: " + timeoutMs);
- }
+ checkTimeout(timeoutMs);
int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler());
}
-
/**
* Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
* by a timeout.
@@ -3079,9 +3042,7 @@
*/
public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Handler handler, int timeoutMs) {
- if (timeoutMs <= 0) {
- throw new IllegalArgumentException("Non-positive timeoutMs");
- }
+ checkTimeout(timeoutMs);
int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
CallbackHandler cbHandler = new CallbackHandler(handler);
requestNetwork(request, networkCallback, timeoutMs, legacyType, cbHandler);
@@ -3153,7 +3114,7 @@
* {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
*/
public void requestNetwork(NetworkRequest request, PendingIntent operation) {
- checkPendingIntent(operation);
+ checkPendingIntentNotNull(operation);
try {
mService.pendingRequestForNetwork(request.networkCapabilities, operation);
} catch (RemoteException e) {
@@ -3176,7 +3137,7 @@
* corresponding NetworkRequest you'd like to remove. Cannot be null.
*/
public void releaseNetworkRequest(PendingIntent operation) {
- checkPendingIntent(operation);
+ checkPendingIntentNotNull(operation);
try {
mService.releasePendingNetworkRequest(operation);
} catch (RemoteException e) {
@@ -3184,10 +3145,16 @@
}
}
- private void checkPendingIntent(PendingIntent intent) {
- if (intent == null) {
- throw new IllegalArgumentException("PendingIntent cannot be null.");
- }
+ private static void checkPendingIntentNotNull(PendingIntent intent) {
+ Preconditions.checkNotNull(intent, "PendingIntent cannot be null.");
+ }
+
+ private static void checkCallbackNotNull(NetworkCallback callback) {
+ Preconditions.checkNotNull(callback, "null NetworkCallback");
+ }
+
+ private static void checkTimeout(int timeoutMs) {
+ Preconditions.checkArgumentPositive(timeoutMs, "timeoutMs must be strictly positive.");
}
/**
@@ -3257,7 +3224,7 @@
* comes from {@link PendingIntent#getBroadcast}. Cannot be null.
*/
public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
- checkPendingIntent(operation);
+ checkPendingIntentNotNull(operation);
try {
mService.pendingListenForNetwork(request.networkCapabilities, operation);
} catch (RemoteException e) {
@@ -3301,8 +3268,9 @@
// capabilities, this request is guaranteed, at all times, to be
// satisfied by the same network, if any, that satisfies the default
// request, i.e., the system default network.
+ NetworkCapabilities nullCapabilities = null;
CallbackHandler cbHandler = new CallbackHandler(handler);
- sendRequestForNetwork(null, networkCallback, 0, REQUEST, TYPE_NONE, cbHandler);
+ sendRequestForNetwork(nullCapabilities, networkCallback, 0, REQUEST, TYPE_NONE, cbHandler);
}
/**
@@ -3339,7 +3307,7 @@
* @param networkCallback The {@link NetworkCallback} used when making the request.
*/
public void unregisterNetworkCallback(NetworkCallback networkCallback) {
- Preconditions.checkArgument(networkCallback != null, "null NetworkCallback");
+ checkCallbackNotNull(networkCallback);
final List<NetworkRequest> reqs = new ArrayList<>();
// Find all requests associated to this callback and stop callback triggers immediately.
// Callback is reusable immediately. http://b/20701525, http://b/35921499.
@@ -3375,6 +3343,7 @@
* Cannot be null.
*/
public void unregisterNetworkCallback(PendingIntent operation) {
+ checkPendingIntentNotNull(operation);
releaseNetworkRequest(operation);
}
diff --git a/services/core/java/com/android/server/connectivity/Tethering.java b/services/core/java/com/android/server/connectivity/Tethering.java
index 2ec9f23..ee89d57 100644
--- a/services/core/java/com/android/server/connectivity/Tethering.java
+++ b/services/core/java/com/android/server/connectivity/Tethering.java
@@ -241,21 +241,11 @@
// See NetlinkHandler.cpp:71.
if (VDBG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up);
synchronized (mPublicSync) {
- int interfaceType = ifaceNameToType(iface);
- if (interfaceType == ConnectivityManager.TETHERING_INVALID) {
- return;
- }
-
- TetherState tetherState = mTetherStates.get(iface);
if (up) {
- if (tetherState == null) {
- trackNewTetherableInterface(iface, interfaceType);
- }
+ maybeTrackNewInterfaceLocked(iface);
} else {
- if (interfaceType == ConnectivityManager.TETHERING_BLUETOOTH) {
- tetherState.stateMachine.sendMessage(
- TetherInterfaceStateMachine.CMD_INTERFACE_DOWN);
- mTetherStates.remove(iface);
+ if (ifaceNameToType(iface) == ConnectivityManager.TETHERING_BLUETOOTH) {
+ stopTrackingInterfaceLocked(iface);
} else {
// Ignore usb0 down after enabling RNDIS.
// We will handle disconnect in interfaceRemoved.
@@ -289,18 +279,7 @@
public void interfaceAdded(String iface) {
if (VDBG) Log.d(TAG, "interfaceAdded " + iface);
synchronized (mPublicSync) {
- int interfaceType = ifaceNameToType(iface);
- if (interfaceType == ConnectivityManager.TETHERING_INVALID) {
- if (VDBG) Log.d(TAG, iface + " is not a tetherable iface, ignoring");
- return;
- }
-
- TetherState tetherState = mTetherStates.get(iface);
- if (tetherState == null) {
- trackNewTetherableInterface(iface, interfaceType);
- } else {
- if (VDBG) Log.d(TAG, "active iface (" + iface + ") reported as added, ignoring");
- }
+ maybeTrackNewInterfaceLocked(iface);
}
}
@@ -308,15 +287,7 @@
public void interfaceRemoved(String iface) {
if (VDBG) Log.d(TAG, "interfaceRemoved " + iface);
synchronized (mPublicSync) {
- TetherState tetherState = mTetherStates.get(iface);
- if (tetherState == null) {
- if (VDBG) {
- Log.e(TAG, "attempting to remove unknown iface (" + iface + "), ignoring");
- }
- return;
- }
- tetherState.stateMachine.sendMessage(TetherInterfaceStateMachine.CMD_INTERFACE_DOWN);
- mTetherStates.remove(iface);
+ stopTrackingInterfaceLocked(iface);
}
}
@@ -1774,15 +1745,40 @@
sendTetherStateChangedBroadcast();
}
- private void trackNewTetherableInterface(String iface, int interfaceType) {
- TetherState tetherState;
- tetherState = new TetherState(new TetherInterfaceStateMachine(iface, mLooper,
- interfaceType, mLog, mNMService, mStatsService, this,
- new IPv6TetheringInterfaceServices(iface, mNMService, mLog)));
+ private void maybeTrackNewInterfaceLocked(final String iface) {
+ // If we don't care about this type of interface, ignore.
+ final int interfaceType = ifaceNameToType(iface);
+ if (interfaceType == ConnectivityManager.TETHERING_INVALID) {
+ mLog.log(iface + " is not a tetherable iface, ignoring");
+ return;
+ }
+
+ // If we have already started a TISM for this interface, skip.
+ if (mTetherStates.containsKey(iface)) {
+ mLog.log("active iface (" + iface + ") reported as added, ignoring");
+ return;
+ }
+
+ mLog.log("adding TetheringInterfaceStateMachine for: " + iface);
+ final TetherState tetherState = new TetherState(
+ new TetherInterfaceStateMachine(
+ iface, mLooper, interfaceType, mLog, mNMService, mStatsService, this,
+ new IPv6TetheringInterfaceServices(iface, mNMService, mLog)));
mTetherStates.put(iface, tetherState);
tetherState.stateMachine.start();
}
+ private void stopTrackingInterfaceLocked(final String iface) {
+ final TetherState tetherState = mTetherStates.get(iface);
+ if (tetherState == null) {
+ mLog.log("attempting to remove unknown iface (" + iface + "), ignoring");
+ return;
+ }
+ tetherState.stateMachine.sendMessage(TetherInterfaceStateMachine.CMD_INTERFACE_DOWN);
+ mLog.log("removing TetheringInterfaceStateMachine for: " + iface);
+ mTetherStates.remove(iface);
+ }
+
private static String[] copy(String[] strarray) {
return Arrays.copyOf(strarray, strarray.length);
}