diff --git a/framework/src/android/net/ConnectivityManager.java b/framework/src/android/net/ConnectivityManager.java index 2af5b35218..a64a9e6e7d 100644 --- a/framework/src/android/net/ConnectivityManager.java +++ b/framework/src/android/net/ConnectivityManager.java @@ -3341,9 +3341,10 @@ public class ConnectivityManager { * Register or update a network offer with ConnectivityService. * * ConnectivityService keeps track of offers made by the various providers and matches - * them to networking requests made by apps or the system. The provider supplies a score - * and the capabilities of the network it might be able to bring up ; these act as filters - * used by ConnectivityService to only send those requests that can be fulfilled by the + * them to networking requests made by apps or the system. A callback identifies an offer + * uniquely, and later calls with the same callback update the offer. The provider supplies a + * score and the capabilities of the network it might be able to bring up ; these act as + * filters used by ConnectivityService to only send those requests that can be fulfilled by the * provider. * * The provider is under no obligation to be able to bring up the network it offers at any @@ -3364,11 +3365,11 @@ public class ConnectivityManager { @RequiresPermission(anyOf = { NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, android.Manifest.permission.NETWORK_FACTORY}) - public void offerNetwork(@NonNull final NetworkProvider provider, + public void offerNetwork(@NonNull final int providerId, @NonNull final NetworkScore score, @NonNull final NetworkCapabilities caps, @NonNull final INetworkOfferCallback callback) { try { - mService.offerNetwork(Objects.requireNonNull(provider.getMessenger(), "null messenger"), + mService.offerNetwork(providerId, Objects.requireNonNull(score, "null score"), Objects.requireNonNull(caps, "null caps"), Objects.requireNonNull(callback, "null callback")); diff --git a/framework/src/android/net/IConnectivityManager.aidl b/framework/src/android/net/IConnectivityManager.aidl index d937c9cd78..c434bbcb0f 100644 --- a/framework/src/android/net/IConnectivityManager.aidl +++ b/framework/src/android/net/IConnectivityManager.aidl @@ -223,7 +223,7 @@ interface IConnectivityManager int getRestrictBackgroundStatusByCaller(); - void offerNetwork(in Messenger messenger, in NetworkScore score, + void offerNetwork(int providerId, in NetworkScore score, in NetworkCapabilities caps, in INetworkOfferCallback callback); void unofferNetwork(in INetworkOfferCallback callback); } diff --git a/framework/src/android/net/INetworkOfferCallback.aidl b/framework/src/android/net/INetworkOfferCallback.aidl index 67d2d405db..a6de173fa3 100644 --- a/framework/src/android/net/INetworkOfferCallback.aidl +++ b/framework/src/android/net/INetworkOfferCallback.aidl @@ -22,15 +22,16 @@ import android.net.NetworkRequest; * A callback registered with connectivity by network providers together with * a NetworkOffer. * - * When the offer is needed to satisfy some application or system component, - * connectivity will call onOfferNeeded on this callback. When this happens, - * the provider should try and bring up the network. + * When the network for this offer is needed to satisfy some application or + * system component, connectivity will call onNetworkNeeded on this callback. + * When this happens, the provider should try and bring up the network. * - * When the offer is no longer needed, for example because the application has - * withdrawn the request or if the request is being satisfied by a network - * that this offer will never be able to beat, connectivity calls - * onOfferUnneeded. When this happens, the provider should stop trying to - * bring up the network, or tear it down if it has already been brought up. + * When the network for this offer is no longer needed, for example because + * the application has withdrawn the request or if the request is being + * satisfied by a network that this offer will never be able to beat, + * connectivity calls onNetworkUnneeded. When this happens, the provider + * should stop trying to bring up the network, or tear it down if it has + * already been brought up. * * When NetworkProvider#offerNetwork is called, the provider can expect to * immediately receive all requests that can be fulfilled by that offer and @@ -38,25 +39,25 @@ import android.net.NetworkRequest; * request is currently outstanding, because no requests have been made that * can be satisfied by this offer, or because all such requests are already * satisfied by a better network. - * onOfferNeeded can be called at any time after registration and until the + * onNetworkNeeded can be called at any time after registration and until the * offer is withdrawn with NetworkProvider#unofferNetwork is called. This * typically happens when a new network request is filed by an application, * or when the network satisfying a request disconnects and this offer now - * stands a chance to be the best network for it. + * stands a chance to supply the best network for it. * * @hide */ oneway interface INetworkOfferCallback { /** - * Informs the registrant that the offer is needed to fulfill this request. + * Called when a network for this offer is needed to fulfill this request. * @param networkRequest the request to satisfy * @param providerId the ID of the provider currently satisfying * this request, or NetworkProvider.ID_NONE if none. */ - void onOfferNeeded(in NetworkRequest networkRequest, int providerId); + void onNetworkNeeded(in NetworkRequest networkRequest, int providerId); /** - * Informs the registrant that the offer is no longer needed to fulfill this request. + * Informs the registrant that the offer is no longer valuable to fulfill this request. */ - void onOfferUnneeded(in NetworkRequest networkRequest); + void onNetworkUnneeded(in NetworkRequest networkRequest); } diff --git a/framework/src/android/net/NetworkProvider.java b/framework/src/android/net/NetworkProvider.java index 8f93047cf8..d859022552 100644 --- a/framework/src/android/net/NetworkProvider.java +++ b/framework/src/android/net/NetworkProvider.java @@ -170,10 +170,11 @@ public class NetworkProvider { /** @hide */ // TODO : make @SystemApi when the impl is complete public interface NetworkOfferCallback { - /** Called by the system when this offer is needed to satisfy some networking request. */ - void onOfferNeeded(@NonNull NetworkRequest request, int providerId); - /** Called by the system when this offer is no longer needed. */ - void onOfferUnneeded(@NonNull NetworkRequest request); + /** Called by the system when a network for this offer is needed to satisfy some + * networking request. */ + void onNetworkNeeded(@NonNull NetworkRequest request, int providerId); + /** Called by the system when this offer is no longer valuable for this request. */ + void onNetworkUnneeded(@NonNull NetworkRequest request); } private class NetworkOfferCallbackProxy extends INetworkOfferCallback.Stub { @@ -187,14 +188,14 @@ public class NetworkProvider { } @Override - public void onOfferNeeded(final @NonNull NetworkRequest request, + public void onNetworkNeeded(final @NonNull NetworkRequest request, final int providerId) { - mExecutor.execute(() -> callback.onOfferNeeded(request, providerId)); + mExecutor.execute(() -> callback.onNetworkNeeded(request, providerId)); } @Override - public void onOfferUnneeded(final @NonNull NetworkRequest request) { - mExecutor.execute(() -> callback.onOfferUnneeded(request)); + public void onNetworkUnneeded(final @NonNull NetworkRequest request) { + mExecutor.execute(() -> callback.onNetworkUnneeded(request)); } } @@ -213,41 +214,41 @@ public class NetworkProvider { } /** - * Register or update an offer for network with the passed caps and score. + * Register or update an offer for network with the passed capabilities and score. * - * A NetworkProvider's job is to provide networks. This function is how a provider tells the + * A NetworkProvider's role is to provide networks. This method is how a provider tells the * connectivity stack what kind of network it may provide. The score and caps arguments act - * as filters that the connectivity stack uses to tell when the offer is necessary. When an - * offer might be advantageous over existing networks, the provider will receive a call to - * the associated callback's {@link NetworkOfferCallback#onOfferNeeded} method. The provider - * should then try to bring up this network. When an offer is no longer needed, the stack - * will inform the provider by calling {@link NetworkOfferCallback#onOfferUnneeded}. The + * as filters that the connectivity stack uses to tell when the offer is valuable. When an + * offer might be preferred over existing networks, the provider will receive a call to + * the associated callback's {@link NetworkOfferCallback#onNetworkNeeded} method. The provider + * should then try to bring up this network. When an offer is no longer useful, the stack + * will inform the provider by calling {@link NetworkOfferCallback#onNetworkUnneeded}. The * provider should stop trying to bring up such a network, or disconnect it if it already has * one. * - * The stack determines what offers are needed according to what networks are currently + * The stack determines what offers are valuable according to what networks are currently * available to the system, and what networking requests are made by applications. If an - * offer looks like it could be a better choice than any existing network for any particular - * request, that's when the stack decides the offer is needed. If the current networking - * requests are all satisfied by networks that this offer can't possibly be a better match - * for, that's when the offer is unneeded. An offer starts off as unneeded ; the provider - * should not try to bring up the network until {@link NetworkOfferCallback#onOfferNeeded} - * is called. + * offer looks like it could connect a better network than any existing network for any + * particular request, that's when the stack decides the network is needed. If the current + * networking requests are all satisfied by networks that this offer couldn't possibly be a + * better match for, that's when the offer is no longer valuable. An offer starts out as + * unneeded ; the provider should not try to bring up the network until + * {@link NetworkOfferCallback#onNetworkNeeded} is called. * * Note that the offers are non-binding to the providers, in particular because providers * often don't know if they will be able to bring up such a network at any given time. For - * example, no wireless network may be in range when the offer is needed. This is fine and - * expected ; the provider should simply continue to try to bring up the network and do so + * example, no wireless network may be in range when the offer would be valuable. This is fine + * and expected ; the provider should simply continue to try to bring up the network and do so * if/when it becomes possible. In the mean time, the stack will continue to satisfy requests * with the best network currently available, or if none, keep the apps informed that no * network can currently satisfy this request. When/if the provider can bring up the network, * the connectivity stack will match it against requests, and inform interested apps of the * availability of this network. This may, in turn, render the offer of some other provider - * unneeded if all requests it used to satisfy are now better served by this network. + * low-value if all requests it used to satisfy are now better served by this network. * * A network can become unneeded for a reason like the above : whether the provider managed * to bring up the offered network after it became needed or not, some other provider may - * bring up a better network than this one, making this offer unneeded. A network may also + * bring up a better network than this one, making this network unneeded. A network may also * become unneeded if the application making the request withdrew it (for example, after it * is done transferring data, or if the user canceled an operation). * @@ -272,6 +273,9 @@ public class NetworkProvider { public void offerNetwork(@NonNull final NetworkScore score, @NonNull final NetworkCapabilities caps, @NonNull final Executor executor, @NonNull final NetworkOfferCallback callback) { + // Can't offer a network with a provider that is not yet registered or already unregistered. + final int providerId = mProviderId; + if (providerId == ID_NONE) return; NetworkOfferCallbackProxy proxy = null; synchronized (mProxies) { for (final NetworkOfferCallbackProxy existingProxy : mProxies) { @@ -285,7 +289,8 @@ public class NetworkProvider { mProxies.add(proxy); } } - mContext.getSystemService(ConnectivityManager.class).offerNetwork(this, score, caps, proxy); + mContext.getSystemService(ConnectivityManager.class) + .offerNetwork(providerId, score, caps, proxy); } /** diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java index adfcacea1a..aa1648f9e7 100644 --- a/service/src/com/android/server/ConnectivityService.java +++ b/service/src/com/android/server/ConnectivityService.java @@ -6233,11 +6233,11 @@ public class ConnectivityService extends IConnectivityManager.Stub } @Override - public void offerNetwork(@NonNull final Messenger providerMessenger, + public void offerNetwork(final int providerId, @NonNull final NetworkScore score, @NonNull final NetworkCapabilities caps, @NonNull final INetworkOfferCallback callback) { final NetworkOffer offer = new NetworkOffer( - FullScore.makeProspectiveScore(score, caps), caps, callback, providerMessenger); + FullScore.makeProspectiveScore(score, caps), caps, callback, providerId); mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_OFFER, offer)); } @@ -6255,7 +6255,7 @@ public class ConnectivityService extends IConnectivityManager.Stub // Unregister all the offers from this provider final ArrayList toRemove = new ArrayList<>(); for (final NetworkOfferInfo noi : mNetworkOffers) { - if (noi.offer.provider == messenger) { + if (noi.offer.providerId == npi.providerId) { // Can't call handleUnregisterNetworkOffer here because iteration is in progress toRemove.add(noi); } @@ -6640,6 +6640,13 @@ public class ConnectivityService extends IConnectivityManager.Stub } } + private boolean isNetworkProviderWithIdRegistered(final int providerId) { + for (final NetworkProviderInfo npi : mNetworkProviderInfos.values()) { + if (npi.providerId == providerId) return true; + } + return false; + } + /** * Register or update a network offer. * @param newOffer The new offer. If the callback member is the same as an existing @@ -6647,7 +6654,7 @@ public class ConnectivityService extends IConnectivityManager.Stub */ private void handleRegisterNetworkOffer(@NonNull final NetworkOffer newOffer) { ensureRunningOnConnectivityServiceThread(); - if (null == mNetworkProviderInfos.get(newOffer.provider)) { + if (!isNetworkProviderWithIdRegistered(newOffer.providerId)) { // This may actually happen if a provider updates its score or registers and then // immediately unregisters. The offer would still be in the handler queue, but the // provider would have been removed. @@ -6662,7 +6669,7 @@ public class ConnectivityService extends IConnectivityManager.Stub } final NetworkOfferInfo noi = new NetworkOfferInfo(newOffer); try { - noi.offer.provider.getBinder().linkToDeath(noi, 0 /* flags */); + noi.offer.callback.asBinder().linkToDeath(noi, 0 /* flags */); } catch (RemoteException e) { noi.binderDied(); return; @@ -6674,7 +6681,7 @@ public class ConnectivityService extends IConnectivityManager.Stub private void handleUnregisterNetworkOffer(@NonNull final NetworkOfferInfo noi) { ensureRunningOnConnectivityServiceThread(); mNetworkOffers.remove(noi); - noi.offer.provider.getBinder().unlinkToDeath(noi, 0 /* flags */); + noi.offer.callback.asBinder().unlinkToDeath(noi, 0 /* flags */); } @Nullable private NetworkOfferInfo findNetworkOfferInfoByCallback( diff --git a/service/src/com/android/server/connectivity/NetworkOffer.java b/service/src/com/android/server/connectivity/NetworkOffer.java index fa2d465fff..548db6b6dd 100644 --- a/service/src/com/android/server/connectivity/NetworkOffer.java +++ b/service/src/com/android/server/connectivity/NetworkOffer.java @@ -21,7 +21,6 @@ import android.annotation.Nullable; import android.net.INetworkOfferCallback; import android.net.NetworkCapabilities; import android.net.NetworkRequest; -import android.os.Messenger; import java.util.Objects; @@ -44,7 +43,7 @@ public class NetworkOffer { @NonNull public final FullScore score; @NonNull public final NetworkCapabilities caps; @NonNull public final INetworkOfferCallback callback; - @NonNull public final Messenger provider; + @NonNull public final int providerId; private static NetworkCapabilities emptyCaps() { final NetworkCapabilities nc = new NetworkCapabilities(); @@ -56,11 +55,11 @@ public class NetworkOffer { public NetworkOffer(@NonNull final FullScore score, @Nullable final NetworkCapabilities caps, @NonNull final INetworkOfferCallback callback, - @NonNull final Messenger provider) { + @NonNull final int providerId) { this.score = Objects.requireNonNull(score); this.caps = null != caps ? caps : emptyCaps(); this.callback = Objects.requireNonNull(callback); - this.provider = Objects.requireNonNull(provider); + this.providerId = providerId; } /**