Merge changes Ia930b3d3,If614da81,I975a9439

* changes:
  [NS04.8] Address comments from NS04 and NS04.5
  [NS04.7] Reinstate a necessary change
  [NS04.5] Have NetworkOffer embark a provider ID
This commit is contained in:
Junyu Lai
2021-06-04 20:52:38 +00:00
committed by Gerrit Code Review
6 changed files with 70 additions and 57 deletions

View File

@@ -3341,9 +3341,10 @@ public class ConnectivityManager {
* Register or update a network offer with ConnectivityService. * Register or update a network offer with ConnectivityService.
* *
* ConnectivityService keeps track of offers made by the various providers and matches * 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 * them to networking requests made by apps or the system. A callback identifies an offer
* and the capabilities of the network it might be able to bring up ; these act as filters * uniquely, and later calls with the same callback update the offer. The provider supplies a
* used by ConnectivityService to only send those requests that can be fulfilled by the * 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. * provider.
* *
* The provider is under no obligation to be able to bring up the network it offers at any * 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 = { @RequiresPermission(anyOf = {
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
android.Manifest.permission.NETWORK_FACTORY}) 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 NetworkScore score, @NonNull final NetworkCapabilities caps,
@NonNull final INetworkOfferCallback callback) { @NonNull final INetworkOfferCallback callback) {
try { try {
mService.offerNetwork(Objects.requireNonNull(provider.getMessenger(), "null messenger"), mService.offerNetwork(providerId,
Objects.requireNonNull(score, "null score"), Objects.requireNonNull(score, "null score"),
Objects.requireNonNull(caps, "null caps"), Objects.requireNonNull(caps, "null caps"),
Objects.requireNonNull(callback, "null callback")); Objects.requireNonNull(callback, "null callback"));

View File

@@ -223,7 +223,7 @@ interface IConnectivityManager
int getRestrictBackgroundStatusByCaller(); int getRestrictBackgroundStatusByCaller();
void offerNetwork(in Messenger messenger, in NetworkScore score, void offerNetwork(int providerId, in NetworkScore score,
in NetworkCapabilities caps, in INetworkOfferCallback callback); in NetworkCapabilities caps, in INetworkOfferCallback callback);
void unofferNetwork(in INetworkOfferCallback callback); void unofferNetwork(in INetworkOfferCallback callback);
} }

View File

@@ -22,15 +22,16 @@ import android.net.NetworkRequest;
* A callback registered with connectivity by network providers together with * A callback registered with connectivity by network providers together with
* a NetworkOffer. * a NetworkOffer.
* *
* When the offer is needed to satisfy some application or system component, * When the network for this offer is needed to satisfy some application or
* connectivity will call onOfferNeeded on this callback. When this happens, * system component, connectivity will call onNetworkNeeded on this callback.
* the provider should try and bring up the network. * 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 * When the network for this offer is no longer needed, for example because
* withdrawn the request or if the request is being satisfied by a network * the application has withdrawn the request or if the request is being
* that this offer will never be able to beat, connectivity calls * satisfied by a network that this offer will never be able to beat,
* onOfferUnneeded. When this happens, the provider should stop trying to * connectivity calls onNetworkUnneeded. When this happens, the provider
* bring up the network, or tear it down if it has already been brought up. * 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 * When NetworkProvider#offerNetwork is called, the provider can expect to
* immediately receive all requests that can be fulfilled by that offer and * 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 * request is currently outstanding, because no requests have been made that
* can be satisfied by this offer, or because all such requests are already * can be satisfied by this offer, or because all such requests are already
* satisfied by a better network. * 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 * offer is withdrawn with NetworkProvider#unofferNetwork is called. This
* typically happens when a new network request is filed by an application, * typically happens when a new network request is filed by an application,
* or when the network satisfying a request disconnects and this offer now * 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 * @hide
*/ */
oneway interface INetworkOfferCallback { 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 networkRequest the request to satisfy
* @param providerId the ID of the provider currently satisfying * @param providerId the ID of the provider currently satisfying
* this request, or NetworkProvider.ID_NONE if none. * 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);
} }

View File

@@ -170,10 +170,11 @@ public class NetworkProvider {
/** @hide */ /** @hide */
// TODO : make @SystemApi when the impl is complete // TODO : make @SystemApi when the impl is complete
public interface NetworkOfferCallback { public interface NetworkOfferCallback {
/** Called by the system when this offer is needed to satisfy some networking request. */ /** Called by the system when a network for this offer is needed to satisfy some
void onOfferNeeded(@NonNull NetworkRequest request, int providerId); * networking request. */
/** Called by the system when this offer is no longer needed. */ void onNetworkNeeded(@NonNull NetworkRequest request, int providerId);
void onOfferUnneeded(@NonNull NetworkRequest request); /** 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 { private class NetworkOfferCallbackProxy extends INetworkOfferCallback.Stub {
@@ -187,14 +188,14 @@ public class NetworkProvider {
} }
@Override @Override
public void onOfferNeeded(final @NonNull NetworkRequest request, public void onNetworkNeeded(final @NonNull NetworkRequest request,
final int providerId) { final int providerId) {
mExecutor.execute(() -> callback.onOfferNeeded(request, providerId)); mExecutor.execute(() -> callback.onNetworkNeeded(request, providerId));
} }
@Override @Override
public void onOfferUnneeded(final @NonNull NetworkRequest request) { public void onNetworkUnneeded(final @NonNull NetworkRequest request) {
mExecutor.execute(() -> callback.onOfferUnneeded(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 * 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 * as filters that the connectivity stack uses to tell when the offer is valuable. When an
* offer might be advantageous over existing networks, the provider will receive a call to * offer might be preferred over existing networks, the provider will receive a call to
* the associated callback's {@link NetworkOfferCallback#onOfferNeeded} method. The provider * the associated callback's {@link NetworkOfferCallback#onNetworkNeeded} method. The provider
* should then try to bring up this network. When an offer is no longer needed, the stack * 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#onOfferUnneeded}. The * 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 * provider should stop trying to bring up such a network, or disconnect it if it already has
* one. * 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 * 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 * offer looks like it could connect a better network than any existing network for any
* request, that's when the stack decides the offer is needed. If the current networking * particular request, that's when the stack decides the network is needed. If the current
* requests are all satisfied by networks that this offer can't possibly be a better match * networking requests are all satisfied by networks that this offer couldn't possibly be a
* for, that's when the offer is unneeded. An offer starts off as unneeded ; the provider * better match for, that's when the offer is no longer valuable. An offer starts out as
* should not try to bring up the network until {@link NetworkOfferCallback#onOfferNeeded} * unneeded ; the provider should not try to bring up the network until
* is called. * {@link NetworkOfferCallback#onNetworkNeeded} is called.
* *
* Note that the offers are non-binding to the providers, in particular because providers * 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 * 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 * example, no wireless network may be in range when the offer would be valuable. This is fine
* expected ; the provider should simply continue to try to bring up the network and do so * 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 * 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 * 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, * 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 * 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 * 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 * 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 * 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 * 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). * 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, public void offerNetwork(@NonNull final NetworkScore score,
@NonNull final NetworkCapabilities caps, @NonNull final Executor executor, @NonNull final NetworkCapabilities caps, @NonNull final Executor executor,
@NonNull final NetworkOfferCallback callback) { @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; NetworkOfferCallbackProxy proxy = null;
synchronized (mProxies) { synchronized (mProxies) {
for (final NetworkOfferCallbackProxy existingProxy : mProxies) { for (final NetworkOfferCallbackProxy existingProxy : mProxies) {
@@ -285,7 +289,8 @@ public class NetworkProvider {
mProxies.add(proxy); mProxies.add(proxy);
} }
} }
mContext.getSystemService(ConnectivityManager.class).offerNetwork(this, score, caps, proxy); mContext.getSystemService(ConnectivityManager.class)
.offerNetwork(providerId, score, caps, proxy);
} }
/** /**

View File

@@ -6233,11 +6233,11 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
@Override @Override
public void offerNetwork(@NonNull final Messenger providerMessenger, public void offerNetwork(final int providerId,
@NonNull final NetworkScore score, @NonNull final NetworkCapabilities caps, @NonNull final NetworkScore score, @NonNull final NetworkCapabilities caps,
@NonNull final INetworkOfferCallback callback) { @NonNull final INetworkOfferCallback callback) {
final NetworkOffer offer = new NetworkOffer( 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)); 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 // Unregister all the offers from this provider
final ArrayList<NetworkOfferInfo> toRemove = new ArrayList<>(); final ArrayList<NetworkOfferInfo> toRemove = new ArrayList<>();
for (final NetworkOfferInfo noi : mNetworkOffers) { 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 // Can't call handleUnregisterNetworkOffer here because iteration is in progress
toRemove.add(noi); 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. * Register or update a network offer.
* @param newOffer The new offer. If the callback member is the same as an existing * @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) { private void handleRegisterNetworkOffer(@NonNull final NetworkOffer newOffer) {
ensureRunningOnConnectivityServiceThread(); 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 // 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 // immediately unregisters. The offer would still be in the handler queue, but the
// provider would have been removed. // provider would have been removed.
@@ -6662,7 +6669,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
final NetworkOfferInfo noi = new NetworkOfferInfo(newOffer); final NetworkOfferInfo noi = new NetworkOfferInfo(newOffer);
try { try {
noi.offer.provider.getBinder().linkToDeath(noi, 0 /* flags */); noi.offer.callback.asBinder().linkToDeath(noi, 0 /* flags */);
} catch (RemoteException e) { } catch (RemoteException e) {
noi.binderDied(); noi.binderDied();
return; return;
@@ -6674,7 +6681,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
private void handleUnregisterNetworkOffer(@NonNull final NetworkOfferInfo noi) { private void handleUnregisterNetworkOffer(@NonNull final NetworkOfferInfo noi) {
ensureRunningOnConnectivityServiceThread(); ensureRunningOnConnectivityServiceThread();
mNetworkOffers.remove(noi); mNetworkOffers.remove(noi);
noi.offer.provider.getBinder().unlinkToDeath(noi, 0 /* flags */); noi.offer.callback.asBinder().unlinkToDeath(noi, 0 /* flags */);
} }
@Nullable private NetworkOfferInfo findNetworkOfferInfoByCallback( @Nullable private NetworkOfferInfo findNetworkOfferInfoByCallback(

View File

@@ -21,7 +21,6 @@ import android.annotation.Nullable;
import android.net.INetworkOfferCallback; import android.net.INetworkOfferCallback;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
import android.net.NetworkRequest; import android.net.NetworkRequest;
import android.os.Messenger;
import java.util.Objects; import java.util.Objects;
@@ -44,7 +43,7 @@ public class NetworkOffer {
@NonNull public final FullScore score; @NonNull public final FullScore score;
@NonNull public final NetworkCapabilities caps; @NonNull public final NetworkCapabilities caps;
@NonNull public final INetworkOfferCallback callback; @NonNull public final INetworkOfferCallback callback;
@NonNull public final Messenger provider; @NonNull public final int providerId;
private static NetworkCapabilities emptyCaps() { private static NetworkCapabilities emptyCaps() {
final NetworkCapabilities nc = new NetworkCapabilities(); final NetworkCapabilities nc = new NetworkCapabilities();
@@ -56,11 +55,11 @@ public class NetworkOffer {
public NetworkOffer(@NonNull final FullScore score, public NetworkOffer(@NonNull final FullScore score,
@Nullable final NetworkCapabilities caps, @Nullable final NetworkCapabilities caps,
@NonNull final INetworkOfferCallback callback, @NonNull final INetworkOfferCallback callback,
@NonNull final Messenger provider) { @NonNull final int providerId) {
this.score = Objects.requireNonNull(score); this.score = Objects.requireNonNull(score);
this.caps = null != caps ? caps : emptyCaps(); this.caps = null != caps ? caps : emptyCaps();
this.callback = Objects.requireNonNull(callback); this.callback = Objects.requireNonNull(callback);
this.provider = Objects.requireNonNull(provider); this.providerId = providerId;
} }
/** /**