Merge "[NS04] Introduce Network Offers and their callbacks"
This commit is contained in:
@@ -3337,7 +3337,60 @@ public class ConnectivityManager {
|
|||||||
provider.setProviderId(NetworkProvider.ID_NONE);
|
provider.setProviderId(NetworkProvider.ID_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
* provider.
|
||||||
|
*
|
||||||
|
* The provider is under no obligation to be able to bring up the network it offers at any
|
||||||
|
* given time. Instead, this mechanism is meant to limit requests received by providers
|
||||||
|
* to those they actually have a chance to fulfill, as providers don't have a way to compare
|
||||||
|
* the quality of the network satisfying a given request to their own offer.
|
||||||
|
*
|
||||||
|
* An offer can be updated by calling this again with the same callback object. This is
|
||||||
|
* similar to calling unofferNetwork and offerNetwork again, but will only update the
|
||||||
|
* provider with the changes caused by the changes in the offer.
|
||||||
|
*
|
||||||
|
* @param provider The provider making this offer.
|
||||||
|
* @param score The prospective score of the network.
|
||||||
|
* @param caps The prospective capabilities of the network.
|
||||||
|
* @param callback The callback to call when this offer is needed or unneeded.
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
@RequiresPermission(anyOf = {
|
||||||
|
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
|
||||||
|
android.Manifest.permission.NETWORK_FACTORY})
|
||||||
|
public void offerNetwork(@NonNull final NetworkProvider provider,
|
||||||
|
@NonNull final NetworkScore score, @NonNull final NetworkCapabilities caps,
|
||||||
|
@NonNull final INetworkOfferCallback callback) {
|
||||||
|
try {
|
||||||
|
mService.offerNetwork(Objects.requireNonNull(provider.getMessenger(), "null messenger"),
|
||||||
|
Objects.requireNonNull(score, "null score"),
|
||||||
|
Objects.requireNonNull(caps, "null caps"),
|
||||||
|
Objects.requireNonNull(callback, "null callback"));
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
throw e.rethrowFromSystemServer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Withdraw a network offer made with {@link #offerNetwork}.
|
||||||
|
*
|
||||||
|
* @param callback The callback passed at registration time. This must be the same object
|
||||||
|
* that was passed to {@link #offerNetwork}
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
public void unofferNetwork(@NonNull final INetworkOfferCallback callback) {
|
||||||
|
try {
|
||||||
|
mService.unofferNetwork(Objects.requireNonNull(callback));
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
throw e.rethrowFromSystemServer();
|
||||||
|
}
|
||||||
|
}
|
||||||
/** @hide exposed via the NetworkProvider class. */
|
/** @hide exposed via the NetworkProvider class. */
|
||||||
@RequiresPermission(anyOf = {
|
@RequiresPermission(anyOf = {
|
||||||
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
|
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import android.net.IConnectivityDiagnosticsCallback;
|
|||||||
import android.net.INetworkAgent;
|
import android.net.INetworkAgent;
|
||||||
import android.net.IOnCompleteListener;
|
import android.net.IOnCompleteListener;
|
||||||
import android.net.INetworkActivityListener;
|
import android.net.INetworkActivityListener;
|
||||||
|
import android.net.INetworkOfferCallback;
|
||||||
import android.net.IQosCallback;
|
import android.net.IQosCallback;
|
||||||
import android.net.ISocketKeepaliveCallback;
|
import android.net.ISocketKeepaliveCallback;
|
||||||
import android.net.LinkProperties;
|
import android.net.LinkProperties;
|
||||||
@@ -221,4 +222,8 @@ interface IConnectivityManager
|
|||||||
in IOnCompleteListener listener);
|
in IOnCompleteListener listener);
|
||||||
|
|
||||||
int getRestrictBackgroundStatusByCaller();
|
int getRestrictBackgroundStatusByCaller();
|
||||||
|
|
||||||
|
void offerNetwork(in Messenger messenger, in NetworkScore score,
|
||||||
|
in NetworkCapabilities caps, in INetworkOfferCallback callback);
|
||||||
|
void unofferNetwork(in INetworkOfferCallback callback);
|
||||||
}
|
}
|
||||||
|
|||||||
62
framework/src/android/net/INetworkOfferCallback.aidl
Normal file
62
framework/src/android/net/INetworkOfferCallback.aidl
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2021 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package android.net;
|
||||||
|
|
||||||
|
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 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 NetworkProvider#offerNetwork is called, the provider can expect to
|
||||||
|
* immediately receive all requests that can be fulfilled by that offer and
|
||||||
|
* are not already satisfied by a better network. It is possible no such
|
||||||
|
* 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
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
oneway interface INetworkOfferCallback {
|
||||||
|
/**
|
||||||
|
* Informs the registrant that the 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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Informs the registrant that the offer is no longer needed to fulfill this request.
|
||||||
|
*/
|
||||||
|
void onOfferUnneeded(in NetworkRequest networkRequest);
|
||||||
|
}
|
||||||
@@ -28,6 +28,11 @@ import android.os.Message;
|
|||||||
import android.os.Messenger;
|
import android.os.Messenger;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.android.internal.annotations.GuardedBy;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for network providers such as telephony or Wi-Fi. NetworkProviders connect the device
|
* Base class for network providers such as telephony or Wi-Fi. NetworkProviders connect the device
|
||||||
* to networks and makes them available to the core network stack by creating
|
* to networks and makes them available to the core network stack by creating
|
||||||
@@ -78,7 +83,9 @@ public class NetworkProvider {
|
|||||||
*/
|
*/
|
||||||
@SystemApi
|
@SystemApi
|
||||||
public NetworkProvider(@NonNull Context context, @NonNull Looper looper, @NonNull String name) {
|
public NetworkProvider(@NonNull Context context, @NonNull Looper looper, @NonNull String name) {
|
||||||
Handler handler = new Handler(looper) {
|
// TODO (b/174636568) : this class should be able to cache an instance of
|
||||||
|
// ConnectivityManager so it doesn't have to fetch it again every time.
|
||||||
|
final Handler handler = new Handler(looper) {
|
||||||
@Override
|
@Override
|
||||||
public void handleMessage(Message m) {
|
public void handleMessage(Message m) {
|
||||||
switch (m.what) {
|
switch (m.what) {
|
||||||
@@ -159,4 +166,148 @@ public class NetworkProvider {
|
|||||||
public void declareNetworkRequestUnfulfillable(@NonNull NetworkRequest request) {
|
public void declareNetworkRequestUnfulfillable(@NonNull NetworkRequest request) {
|
||||||
ConnectivityManager.from(mContext).declareNetworkRequestUnfulfillable(request);
|
ConnectivityManager.from(mContext).declareNetworkRequestUnfulfillable(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @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);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class NetworkOfferCallbackProxy extends INetworkOfferCallback.Stub {
|
||||||
|
@NonNull public final NetworkOfferCallback callback;
|
||||||
|
@NonNull private final Executor mExecutor;
|
||||||
|
|
||||||
|
NetworkOfferCallbackProxy(@NonNull final NetworkOfferCallback callback,
|
||||||
|
@NonNull final Executor executor) {
|
||||||
|
this.callback = callback;
|
||||||
|
this.mExecutor = executor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onOfferNeeded(final @NonNull NetworkRequest request,
|
||||||
|
final int providerId) {
|
||||||
|
mExecutor.execute(() -> callback.onOfferNeeded(request, providerId));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onOfferUnneeded(final @NonNull NetworkRequest request) {
|
||||||
|
mExecutor.execute(() -> callback.onOfferUnneeded(request));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@GuardedBy("mProxies")
|
||||||
|
@NonNull private final ArrayList<NetworkOfferCallbackProxy> mProxies = new ArrayList<>();
|
||||||
|
|
||||||
|
// Returns the proxy associated with this callback, or null if none.
|
||||||
|
@Nullable
|
||||||
|
private NetworkOfferCallbackProxy findProxyForCallback(@NonNull final NetworkOfferCallback cb) {
|
||||||
|
synchronized (mProxies) {
|
||||||
|
for (final NetworkOfferCallbackProxy p : mProxies) {
|
||||||
|
if (p.callback == cb) return p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register or update an offer for network with the passed caps and score.
|
||||||
|
*
|
||||||
|
* A NetworkProvider's job is to provide networks. This function 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
|
||||||
|
* 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
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
* 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).
|
||||||
|
*
|
||||||
|
* The capabilities and score act as filters as to what requests the provider will see.
|
||||||
|
* They are not promises, but for best performance, the providers should strive to put
|
||||||
|
* as much known information as possible in the offer. For capabilities in particular, it
|
||||||
|
* should put all NetworkAgent-managed capabilities a network may have, even if it doesn't
|
||||||
|
* have them at first. This applies to INTERNET, for example ; if a provider thinks the
|
||||||
|
* network it can bring up for this offer may offer Internet access it should include the
|
||||||
|
* INTERNET bit. It's fine if the brought up network ends up not actually having INTERNET.
|
||||||
|
*
|
||||||
|
* TODO : in the future, to avoid possible infinite loops, there should be constraints on
|
||||||
|
* what can be put in capabilities of networks brought up for an offer. If a provider might
|
||||||
|
* bring up a network with or without INTERNET, then it should file two offers : this will
|
||||||
|
* let it know precisely what networks are needed, so it can avoid bringing up networks that
|
||||||
|
* won't actually satisfy requests and remove the risk for bring-up-bring-down loops.
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
// TODO : make @SystemApi when the impl is complete
|
||||||
|
@RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
|
||||||
|
public void offerNetwork(@NonNull final NetworkScore score,
|
||||||
|
@NonNull final NetworkCapabilities caps, @NonNull final Executor executor,
|
||||||
|
@NonNull final NetworkOfferCallback callback) {
|
||||||
|
NetworkOfferCallbackProxy proxy = null;
|
||||||
|
synchronized (mProxies) {
|
||||||
|
for (final NetworkOfferCallbackProxy existingProxy : mProxies) {
|
||||||
|
if (existingProxy.callback == callback) {
|
||||||
|
proxy = existingProxy;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (null == proxy) {
|
||||||
|
proxy = new NetworkOfferCallbackProxy(callback, executor);
|
||||||
|
mProxies.add(proxy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mContext.getSystemService(ConnectivityManager.class).offerNetwork(this, score, caps, proxy);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Withdraw a network offer previously made to the networking stack.
|
||||||
|
*
|
||||||
|
* If a provider can no longer provide a network they offered, it should call this method.
|
||||||
|
* An example of usage could be if the hardware necessary to bring up the network was turned
|
||||||
|
* off in UI by the user. Note that because offers are never binding, the provider might
|
||||||
|
* alternatively decide not to withdraw this offer and simply refuse to bring up the network
|
||||||
|
* even when it's needed. However, withdrawing the request is slightly more resource-efficient
|
||||||
|
* because the networking stack won't have to compare this offer to exiting networks to see
|
||||||
|
* if it could beat any of them, and may be advantageous to the provider's implementation that
|
||||||
|
* can rely on no longer receiving callbacks for a network that they can't bring up anyways.
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
// TODO : make @SystemApi when the impl is complete
|
||||||
|
@RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
|
||||||
|
public void unofferNetwork(final @NonNull NetworkOfferCallback callback) {
|
||||||
|
final NetworkOfferCallbackProxy proxy = findProxyForCallback(callback);
|
||||||
|
if (null == proxy) return;
|
||||||
|
mProxies.remove(proxy);
|
||||||
|
mContext.getSystemService(ConnectivityManager.class).unofferNetwork(proxy);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
package com.android.server;
|
package com.android.server;
|
||||||
|
|
||||||
import static android.Manifest.permission.RECEIVE_DATA_ACTIVITY_CHANGE;
|
import static android.Manifest.permission.RECEIVE_DATA_ACTIVITY_CHANGE;
|
||||||
import static android.content.pm.PackageManager.FEATURE_BLUETOOTH;
|
import static android.content.pm.PackageManager.FEATURE_BLUETOOTH;
|
||||||
import static android.content.pm.PackageManager.FEATURE_WATCH;
|
import static android.content.pm.PackageManager.FEATURE_WATCH;
|
||||||
@@ -125,6 +124,7 @@ import android.net.INetworkActivityListener;
|
|||||||
import android.net.INetworkAgent;
|
import android.net.INetworkAgent;
|
||||||
import android.net.INetworkMonitor;
|
import android.net.INetworkMonitor;
|
||||||
import android.net.INetworkMonitorCallbacks;
|
import android.net.INetworkMonitorCallbacks;
|
||||||
|
import android.net.INetworkOfferCallback;
|
||||||
import android.net.IOnCompleteListener;
|
import android.net.IOnCompleteListener;
|
||||||
import android.net.IQosCallback;
|
import android.net.IQosCallback;
|
||||||
import android.net.ISocketKeepaliveCallback;
|
import android.net.ISocketKeepaliveCallback;
|
||||||
@@ -234,6 +234,7 @@ import com.android.net.module.util.PermissionUtils;
|
|||||||
import com.android.server.connectivity.AutodestructReference;
|
import com.android.server.connectivity.AutodestructReference;
|
||||||
import com.android.server.connectivity.DnsManager;
|
import com.android.server.connectivity.DnsManager;
|
||||||
import com.android.server.connectivity.DnsManager.PrivateDnsValidationUpdate;
|
import com.android.server.connectivity.DnsManager.PrivateDnsValidationUpdate;
|
||||||
|
import com.android.server.connectivity.FullScore;
|
||||||
import com.android.server.connectivity.KeepaliveTracker;
|
import com.android.server.connectivity.KeepaliveTracker;
|
||||||
import com.android.server.connectivity.LingerMonitor;
|
import com.android.server.connectivity.LingerMonitor;
|
||||||
import com.android.server.connectivity.MockableSystemProperties;
|
import com.android.server.connectivity.MockableSystemProperties;
|
||||||
@@ -241,6 +242,7 @@ import com.android.server.connectivity.NetworkAgentInfo;
|
|||||||
import com.android.server.connectivity.NetworkDiagnostics;
|
import com.android.server.connectivity.NetworkDiagnostics;
|
||||||
import com.android.server.connectivity.NetworkNotificationManager;
|
import com.android.server.connectivity.NetworkNotificationManager;
|
||||||
import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
|
import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
|
||||||
|
import com.android.server.connectivity.NetworkOffer;
|
||||||
import com.android.server.connectivity.NetworkRanker;
|
import com.android.server.connectivity.NetworkRanker;
|
||||||
import com.android.server.connectivity.PermissionMonitor;
|
import com.android.server.connectivity.PermissionMonitor;
|
||||||
import com.android.server.connectivity.ProfileNetworkPreferences;
|
import com.android.server.connectivity.ProfileNetworkPreferences;
|
||||||
@@ -603,6 +605,18 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
*/
|
*/
|
||||||
private static final int EVENT_UID_BLOCKED_REASON_CHANGED = 51;
|
private static final int EVENT_UID_BLOCKED_REASON_CHANGED = 51;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event to register a new network offer
|
||||||
|
* obj = NetworkOffer
|
||||||
|
*/
|
||||||
|
private static final int EVENT_REGISTER_NETWORK_OFFER = 52;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event to unregister an existing network offer
|
||||||
|
* obj = INetworkOfferCallback
|
||||||
|
*/
|
||||||
|
private static final int EVENT_UNREGISTER_NETWORK_OFFER = 53;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Argument for {@link #EVENT_PROVISIONING_NOTIFICATION} to indicate that the notification
|
* Argument for {@link #EVENT_PROVISIONING_NOTIFICATION} to indicate that the notification
|
||||||
* should be shown.
|
* should be shown.
|
||||||
@@ -4684,6 +4698,18 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
handleUnregisterNetworkProvider((Messenger) msg.obj);
|
handleUnregisterNetworkProvider((Messenger) msg.obj);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case EVENT_REGISTER_NETWORK_OFFER: {
|
||||||
|
handleRegisterNetworkOffer((NetworkOffer) msg.obj);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case EVENT_UNREGISTER_NETWORK_OFFER: {
|
||||||
|
final NetworkOfferInfo offer =
|
||||||
|
findNetworkOfferInfoByCallback((INetworkOfferCallback) msg.obj);
|
||||||
|
if (null != offer) {
|
||||||
|
handleUnregisterNetworkOffer(offer);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
case EVENT_REGISTER_NETWORK_AGENT: {
|
case EVENT_REGISTER_NETWORK_AGENT: {
|
||||||
final Pair<NetworkAgentInfo, INetworkMonitor> arg =
|
final Pair<NetworkAgentInfo, INetworkMonitor> arg =
|
||||||
(Pair<NetworkAgentInfo, INetworkMonitor>) msg.obj;
|
(Pair<NetworkAgentInfo, INetworkMonitor>) msg.obj;
|
||||||
@@ -6214,12 +6240,37 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
mHandler.sendMessage(mHandler.obtainMessage(EVENT_UNREGISTER_NETWORK_PROVIDER, messenger));
|
mHandler.sendMessage(mHandler.obtainMessage(EVENT_UNREGISTER_NETWORK_PROVIDER, messenger));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void offerNetwork(@NonNull final Messenger providerMessenger,
|
||||||
|
@NonNull final NetworkScore score, @NonNull final NetworkCapabilities caps,
|
||||||
|
@NonNull final INetworkOfferCallback callback) {
|
||||||
|
final NetworkOffer offer = new NetworkOffer(
|
||||||
|
FullScore.makeProspectiveScore(score, caps), caps, callback, providerMessenger);
|
||||||
|
mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_OFFER, offer));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void unofferNetwork(@NonNull final INetworkOfferCallback callback) {
|
||||||
|
mHandler.sendMessage(mHandler.obtainMessage(EVENT_UNREGISTER_NETWORK_OFFER, callback));
|
||||||
|
}
|
||||||
|
|
||||||
private void handleUnregisterNetworkProvider(Messenger messenger) {
|
private void handleUnregisterNetworkProvider(Messenger messenger) {
|
||||||
NetworkProviderInfo npi = mNetworkProviderInfos.remove(messenger);
|
NetworkProviderInfo npi = mNetworkProviderInfos.remove(messenger);
|
||||||
if (npi == null) {
|
if (npi == null) {
|
||||||
loge("Failed to find Messenger in unregisterNetworkProvider");
|
loge("Failed to find Messenger in unregisterNetworkProvider");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Unregister all the offers from this provider
|
||||||
|
final ArrayList<NetworkOfferInfo> toRemove = new ArrayList<>();
|
||||||
|
for (final NetworkOfferInfo noi : mNetworkOffers) {
|
||||||
|
if (noi.offer.provider == messenger) {
|
||||||
|
// Can't call handleUnregisterNetworkOffer here because iteration is in progress
|
||||||
|
toRemove.add(noi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (NetworkOfferInfo noi : toRemove) {
|
||||||
|
handleUnregisterNetworkOffer(noi);
|
||||||
|
}
|
||||||
if (DBG) log("unregisterNetworkProvider for " + npi.name);
|
if (DBG) log("unregisterNetworkProvider for " + npi.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6258,6 +6309,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
// (on the handler thread).
|
// (on the handler thread).
|
||||||
private volatile List<UidRange> mVpnBlockedUidRanges = new ArrayList<>();
|
private volatile List<UidRange> mVpnBlockedUidRanges = new ArrayList<>();
|
||||||
|
|
||||||
|
// Must only be accessed on the handler thread
|
||||||
|
@NonNull
|
||||||
|
private final ArrayList<NetworkOfferInfo> mNetworkOffers = new ArrayList<>();
|
||||||
|
|
||||||
@GuardedBy("mBlockedAppUids")
|
@GuardedBy("mBlockedAppUids")
|
||||||
private final HashSet<Integer> mBlockedAppUids = new HashSet<>();
|
private final HashSet<Integer> mBlockedAppUids = new HashSet<>();
|
||||||
|
|
||||||
@@ -6582,6 +6637,65 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
updateUids(nai, null, nai.networkCapabilities);
|
updateUids(nai, null, nai.networkCapabilities);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class NetworkOfferInfo implements IBinder.DeathRecipient {
|
||||||
|
@NonNull public final NetworkOffer offer;
|
||||||
|
|
||||||
|
NetworkOfferInfo(@NonNull final NetworkOffer offer) {
|
||||||
|
this.offer = offer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void binderDied() {
|
||||||
|
mHandler.post(() -> handleUnregisterNetworkOffer(this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register or update a network offer.
|
||||||
|
* @param newOffer The new offer. If the callback member is the same as an existing
|
||||||
|
* offer, it is an update of that offer.
|
||||||
|
*/
|
||||||
|
private void handleRegisterNetworkOffer(@NonNull final NetworkOffer newOffer) {
|
||||||
|
ensureRunningOnConnectivityServiceThread();
|
||||||
|
if (null == mNetworkProviderInfos.get(newOffer.provider)) {
|
||||||
|
// 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.
|
||||||
|
if (DBG) log("Received offer from an unregistered provider");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final NetworkOfferInfo existingOffer = findNetworkOfferInfoByCallback(newOffer.callback);
|
||||||
|
if (null != existingOffer) {
|
||||||
|
handleUnregisterNetworkOffer(existingOffer);
|
||||||
|
newOffer.migrateFrom(existingOffer.offer);
|
||||||
|
}
|
||||||
|
final NetworkOfferInfo noi = new NetworkOfferInfo(newOffer);
|
||||||
|
try {
|
||||||
|
noi.offer.provider.getBinder().linkToDeath(noi, 0 /* flags */);
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
noi.binderDied();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mNetworkOffers.add(noi);
|
||||||
|
// TODO : send requests to the provider.
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleUnregisterNetworkOffer(@NonNull final NetworkOfferInfo noi) {
|
||||||
|
ensureRunningOnConnectivityServiceThread();
|
||||||
|
mNetworkOffers.remove(noi);
|
||||||
|
noi.offer.provider.getBinder().unlinkToDeath(noi, 0 /* flags */);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable private NetworkOfferInfo findNetworkOfferInfoByCallback(
|
||||||
|
@NonNull final INetworkOfferCallback callback) {
|
||||||
|
ensureRunningOnConnectivityServiceThread();
|
||||||
|
for (final NetworkOfferInfo noi : mNetworkOffers) {
|
||||||
|
if (noi.offer.callback.equals(callback)) return noi;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when receiving LinkProperties directly from a NetworkAgent.
|
* Called when receiving LinkProperties directly from a NetworkAgent.
|
||||||
* Stores into |nai| any data coming from the agent that might also be written to the network's
|
* Stores into |nai| any data coming from the agent that might also be written to the network's
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package com.android.server.connectivity;
|
package com.android.server.connectivity;
|
||||||
|
|
||||||
|
import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
|
||||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
|
import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
|
||||||
import static android.net.NetworkCapabilities.TRANSPORT_VPN;
|
import static android.net.NetworkCapabilities.TRANSPORT_VPN;
|
||||||
|
|
||||||
@@ -115,6 +116,33 @@ public class FullScore {
|
|||||||
config.acceptUnvalidated);
|
config.acceptUnvalidated);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given a score supplied by the NetworkAgent, produce a prospective score for an offer.
|
||||||
|
*
|
||||||
|
* NetworkOffers have score filters that are compared to the scores of actual networks
|
||||||
|
* to see if they could possibly beat the current satisfier. Some things the agent can't
|
||||||
|
* know in advance ; a good example is the validation bit – some networks will validate,
|
||||||
|
* others won't. For comparison purposes, assume the best, so all possibly beneficial
|
||||||
|
* networks will be brought up.
|
||||||
|
*
|
||||||
|
* @param score the score supplied by the agent for this offer
|
||||||
|
* @param caps the capabilities supplied by the agent for this offer
|
||||||
|
* @return a FullScore appropriate for comparing to actual network's scores.
|
||||||
|
*/
|
||||||
|
public static FullScore makeProspectiveScore(@NonNull final NetworkScore score,
|
||||||
|
@NonNull final NetworkCapabilities caps) {
|
||||||
|
// If the network offers Internet access, it may validate.
|
||||||
|
final boolean mayValidate = caps.hasCapability(NET_CAPABILITY_INTERNET);
|
||||||
|
// VPN transports are known in advance.
|
||||||
|
final boolean vpn = caps.hasTransport(TRANSPORT_VPN);
|
||||||
|
// The network hasn't been chosen by the user (yet, at least).
|
||||||
|
final boolean everUserSelected = false;
|
||||||
|
// Don't assume the user will accept unvalidated connectivity.
|
||||||
|
final boolean acceptUnvalidated = false;
|
||||||
|
return withPolicies(score.getLegacyInt(), mayValidate, vpn, everUserSelected,
|
||||||
|
acceptUnvalidated);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a new score given updated caps and config.
|
* Return a new score given updated caps and config.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2021 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.server.connectivity;
|
||||||
|
|
||||||
|
import android.annotation.NonNull;
|
||||||
|
import android.annotation.Nullable;
|
||||||
|
import android.net.INetworkOfferCallback;
|
||||||
|
import android.net.NetworkCapabilities;
|
||||||
|
import android.net.NetworkRequest;
|
||||||
|
import android.os.Messenger;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an offer made by a NetworkProvider to create a network if a need arises.
|
||||||
|
*
|
||||||
|
* This class contains the prospective score and capabilities of the network. The provider
|
||||||
|
* is not obligated to caps able to create a network satisfying this, nor to build a network
|
||||||
|
* with the exact score and/or capabilities passed ; after all, not all providers know in
|
||||||
|
* advance what a network will look like after it's connected. Instead, this is meant as a
|
||||||
|
* filter to limit requests sent to the provider by connectivity to those that this offer stands
|
||||||
|
* a chance to fulfill.
|
||||||
|
*
|
||||||
|
* @see NetworkProvider#offerNetwork.
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
public class NetworkOffer {
|
||||||
|
@NonNull public final FullScore score;
|
||||||
|
@NonNull public final NetworkCapabilities caps;
|
||||||
|
@NonNull public final INetworkOfferCallback callback;
|
||||||
|
@NonNull public final Messenger provider;
|
||||||
|
|
||||||
|
private static NetworkCapabilities emptyCaps() {
|
||||||
|
final NetworkCapabilities nc = new NetworkCapabilities();
|
||||||
|
return nc;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ideally the caps argument would be non-null, but null has historically meant no filter
|
||||||
|
// and telephony passes null. Keep backward compatibility.
|
||||||
|
public NetworkOffer(@NonNull final FullScore score,
|
||||||
|
@Nullable final NetworkCapabilities caps,
|
||||||
|
@NonNull final INetworkOfferCallback callback,
|
||||||
|
@NonNull final Messenger provider) {
|
||||||
|
this.score = Objects.requireNonNull(score);
|
||||||
|
this.caps = null != caps ? caps : emptyCaps();
|
||||||
|
this.callback = Objects.requireNonNull(callback);
|
||||||
|
this.provider = Objects.requireNonNull(provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Migrate from, and take over, a previous offer.
|
||||||
|
*
|
||||||
|
* When an updated offer is sent from a provider, call this method on the new offer, passing
|
||||||
|
* the old one, to take over the state.
|
||||||
|
*
|
||||||
|
* @param previousOffer
|
||||||
|
*/
|
||||||
|
public void migrateFrom(@NonNull final NetworkOffer previousOffer) {
|
||||||
|
if (!callback.equals(previousOffer.callback)) {
|
||||||
|
throw new IllegalArgumentException("Can only migrate from a previous version of"
|
||||||
|
+ " the same offer");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether an offer can satisfy a NetworkRequest, according to its capabilities.
|
||||||
|
* @param request The request to test against.
|
||||||
|
* @return Whether this offer can satisfy the request.
|
||||||
|
*/
|
||||||
|
public final boolean canSatisfy(@NonNull final NetworkRequest request) {
|
||||||
|
return request.networkCapabilities.satisfiedByNetworkCapabilities(caps);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "NetworkOffer [ Score " + score + " ]";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user