Lorenzo Colitti | ae5cb71 | 2020-01-08 00:04:09 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.annotation.RequiresPermission; |
| 22 | import android.annotation.SystemApi; |
| 23 | import android.content.Context; |
| 24 | import android.os.Handler; |
| 25 | import android.os.Looper; |
| 26 | import android.os.Message; |
| 27 | import android.os.Messenger; |
| 28 | import android.util.Log; |
| 29 | |
| 30 | /** |
| 31 | * Base class for network providers such as telephony or Wi-Fi. NetworkProviders connect the device |
| 32 | * to networks and makes them available to to the core network stack by creating |
| 33 | * {@link NetworkAgent}s. The networks can then provide connectivity to apps and can be interacted |
| 34 | * with via networking APIs such as {@link ConnectivityManager}. |
| 35 | * |
| 36 | * Subclasses should implement {@link #onNetworkRequested} and {@link #onRequestWithdrawn} to |
| 37 | * receive {@link NetworkRequest}s sent by the system and by apps. A network that is not the |
| 38 | * best (highest-scoring) network for any request is generally not used by the system, and torn |
| 39 | * down. |
| 40 | * |
| 41 | * @hide |
| 42 | */ |
| 43 | @SystemApi |
| 44 | public class NetworkProvider { |
| 45 | /** |
| 46 | * {@code providerId} value that indicates the absence of a provider. It is the providerId of |
| 47 | * any NetworkProvider that is not currently registered, and of any NetworkRequest that is not |
| 48 | * currently being satisfied by a network. |
| 49 | */ |
| 50 | public static final int ID_NONE = -1; |
| 51 | |
| 52 | /** |
| 53 | * A hardcoded ID for NetworkAgents representing VPNs. These agents are not created by any |
| 54 | * provider, so they use this constant for clarity instead of NONE. |
| 55 | * @hide only used by ConnectivityService. |
| 56 | */ |
| 57 | public static final int ID_VPN = -2; |
| 58 | |
| 59 | /** |
| 60 | * The first providerId value that will be allocated. |
| 61 | * @hide only used by ConnectivityService. |
| 62 | */ |
| 63 | public static final int FIRST_PROVIDER_ID = 1; |
| 64 | |
| 65 | /** @hide only used by ConnectivityService */ |
| 66 | public static final int CMD_REQUEST_NETWORK = 1; |
| 67 | /** @hide only used by ConnectivityService */ |
| 68 | public static final int CMD_CANCEL_REQUEST = 2; |
| 69 | |
| 70 | private final Messenger mMessenger; |
| 71 | private final String mName; |
| 72 | private final ConnectivityManager mCm; |
| 73 | |
| 74 | private int mProviderId = ID_NONE; |
| 75 | |
| 76 | /** |
| 77 | * Constructs a new NetworkProvider. |
| 78 | * |
| 79 | * @param looper the Looper on which to run {@link #onNetworkRequested} and |
| 80 | * {@link #onRequestWithdrawn}. |
| 81 | * @param name the name of the listener, used only for debugging. |
| 82 | * |
| 83 | * @hide |
| 84 | */ |
| 85 | @SystemApi |
| 86 | public NetworkProvider(@NonNull Context context, @NonNull Looper looper, @NonNull String name) { |
| 87 | mCm = ConnectivityManager.from(context); |
| 88 | |
| 89 | Handler handler = new Handler(looper) { |
| 90 | @Override |
| 91 | public void handleMessage(Message m) { |
| 92 | switch (m.what) { |
| 93 | case CMD_REQUEST_NETWORK: |
| 94 | onNetworkRequested((NetworkRequest) m.obj, m.arg1, m.arg2); |
| 95 | break; |
| 96 | case CMD_CANCEL_REQUEST: |
| 97 | onRequestWithdrawn((NetworkRequest) m.obj); |
| 98 | break; |
| 99 | default: |
| 100 | Log.e(mName, "Unhandled message: " + m.what); |
| 101 | } |
| 102 | } |
| 103 | }; |
| 104 | mMessenger = new Messenger(handler); |
| 105 | mName = name; |
| 106 | } |
| 107 | |
| 108 | // TODO: consider adding a register() method so ConnectivityManager does not need to call this. |
| 109 | public @Nullable Messenger getMessenger() { |
| 110 | return mMessenger; |
| 111 | } |
| 112 | |
| 113 | public @NonNull String getName() { |
| 114 | return mName; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns the ID of this provider. This is known only once the provider is registered via |
| 119 | * {@link ConnectivityManager#registerNetworkProvider()}, otherwise the ID is {@link #ID_NONE}. |
| 120 | * This ID must be used when registering any {@link NetworkAgent}s. |
| 121 | */ |
| 122 | public int getProviderId() { |
| 123 | return mProviderId; |
| 124 | } |
| 125 | |
| 126 | /** @hide */ |
| 127 | public void setProviderId(int providerId) { |
| 128 | mProviderId = providerId; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Called when a NetworkRequest is received. The request may be a new request or an existing |
| 133 | * request with a different score. |
| 134 | * |
| 135 | * @param request the NetworkRequest being received |
| 136 | * @param score the score of the network currently satisfying the request, or 0 if none. |
| 137 | * @param providerId the ID of the provider that created the network currently satisfying this |
| 138 | * request, or {@link #ID_NONE} if none. |
| 139 | * |
| 140 | * @hide |
| 141 | */ |
| 142 | @SystemApi |
| 143 | public void onNetworkRequested(@NonNull NetworkRequest request, int score, int providerId) {} |
| 144 | |
| 145 | /** |
| 146 | * Called when a NetworkRequest is withdrawn. |
| 147 | * @hide |
| 148 | */ |
| 149 | @SystemApi |
| 150 | public void onRequestWithdrawn(@NonNull NetworkRequest request) {} |
| 151 | |
| 152 | /** |
| 153 | * Asserts that no provider will ever be able to satisfy the specified request. The provider |
| 154 | * must only call this method if it knows that it is the only provider on the system capable of |
| 155 | * satisfying this request, and that the request cannot be satisfied. The application filing the |
| 156 | * request will receive an {@link NetworkCallback#onUnavailable()} callback. |
| 157 | * |
| 158 | * @param request the request that cannot be fulfilled |
| 159 | * @hide |
| 160 | */ |
| 161 | @SystemApi |
| 162 | @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY) |
| 163 | public void declareNetworkRequestUnfulfillable(@NonNull NetworkRequest request) { |
| 164 | mCm.declareNetworkRequestUnfulfillable(request); |
| 165 | } |
| 166 | } |