diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java index 083345d25d..57e5cadb14 100644 --- a/core/java/android/net/ConnectivityManager.java +++ b/core/java/android/net/ConnectivityManager.java @@ -1465,9 +1465,7 @@ public class ConnectivityManager { // 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. @@ -1815,9 +1813,7 @@ public class ConnectivityManager { */ 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) { @@ -1874,9 +1870,8 @@ public class ConnectivityManager { /** {@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) { @@ -2766,7 +2761,7 @@ public class ConnectivityManager { } CallbackHandler(Handler handler) { - this(handler.getLooper()); + this(Preconditions.checkNotNull(handler, "Handler cannot be null.").getLooper()); } @Override @@ -2883,7 +2878,7 @@ public class ConnectivityManager { 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 { @@ -3035,14 +3030,11 @@ public class ConnectivityManager { */ 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. @@ -3072,9 +3064,7 @@ public class ConnectivityManager { */ 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); @@ -3146,7 +3136,7 @@ public class ConnectivityManager { * {@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) { @@ -3169,7 +3159,7 @@ public class ConnectivityManager { * 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) { @@ -3177,10 +3167,16 @@ public class ConnectivityManager { } } - 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."); } /** @@ -3247,7 +3243,7 @@ public class ConnectivityManager { */ @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) { - checkPendingIntent(operation); + checkPendingIntentNotNull(operation); try { mService.pendingListenForNetwork(request.networkCapabilities, operation); } catch (RemoteException e) { @@ -3289,8 +3285,9 @@ public class ConnectivityManager { // 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); } /** @@ -3327,7 +3324,7 @@ public class ConnectivityManager { * @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 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. @@ -3363,6 +3360,7 @@ public class ConnectivityManager { * Cannot be null. */ public void unregisterNetworkCallback(PendingIntent operation) { + checkPendingIntentNotNull(operation); releaseNetworkRequest(operation); }