Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
lucaslin | c2bac51 | 2019-11-07 16:47:56 +0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 20 | import android.annotation.Nullable; |
| 21 | import android.annotation.SystemApi; |
Artur Satayev | 33f9217 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 22 | import android.compat.annotation.UnsupportedAppUsage; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 24 | import android.os.Build; |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 25 | import android.os.Bundle; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 26 | import android.os.Handler; |
| 27 | import android.os.Looper; |
| 28 | import android.os.Message; |
| 29 | import android.os.Messenger; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 30 | import android.util.Log; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 31 | |
| 32 | import com.android.internal.util.AsyncChannel; |
| 33 | import com.android.internal.util.Protocol; |
| 34 | |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 35 | import java.util.ArrayList; |
fenglu | 95ce803 | 2015-05-01 17:04:36 -0700 | [diff] [blame] | 36 | import java.util.concurrent.atomic.AtomicBoolean; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 39 | * A Utility class for handling for communicating between bearer-specific |
| 40 | * code and ConnectivityService. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 41 | * |
| 42 | * A bearer may have more than one NetworkAgent if it can simultaneously |
| 43 | * support separate networks (IMS / Internet / MMS Apns on cellular, or |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 44 | * perhaps connections with different SSID or P2P for Wi-Fi). |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 45 | * |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 46 | * @hide |
| 47 | */ |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 48 | @SystemApi |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 49 | public abstract class NetworkAgent { |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 50 | /** |
| 51 | * The {@link Network} corresponding to this object. |
| 52 | */ |
Chalard Jean | a0e2aa12 | 2019-12-13 19:47:12 +0900 | [diff] [blame] | 53 | @NonNull |
| 54 | public final Network network; |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 55 | |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 56 | private final Handler mHandler; |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 57 | private volatile AsyncChannel mAsyncChannel; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 58 | private final String LOG_TAG; |
| 59 | private static final boolean DBG = true; |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 60 | private static final boolean VDBG = false; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 61 | private final Context mContext; |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 62 | private final ArrayList<Message>mPreConnectedQueue = new ArrayList<Message>(); |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 63 | private volatile long mLastBwRefreshTime = 0; |
| 64 | private static final long BW_REFRESH_MIN_WIN_MS = 500; |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 65 | private boolean mBandwidthUpdateScheduled = false; |
| 66 | private AtomicBoolean mBandwidthUpdatePending = new AtomicBoolean(false); |
| 67 | |
| 68 | /** |
| 69 | * The ID of the {@link NetworkProvider} that created this object, or |
| 70 | * {@link NetworkProvider#ID_NONE} if unknown. |
| 71 | */ |
| 72 | public final int providerId; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 73 | |
| 74 | private static final int BASE = Protocol.BASE_NETWORK_AGENT; |
| 75 | |
| 76 | /** |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 77 | * Sent by ConnectivityService to the NetworkAgent to inform it of |
| 78 | * suspected connectivity problems on its network. The NetworkAgent |
| 79 | * should take steps to verify and correct connectivity. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 80 | * @hide |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 81 | */ |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 82 | public static final int CMD_SUSPECT_BAD = BASE; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Sent by the NetworkAgent (note the EVENT vs CMD prefix) to |
| 86 | * ConnectivityService to pass the current NetworkInfo (connection state). |
| 87 | * Sent when the NetworkInfo changes, mainly due to change of state. |
| 88 | * obj = NetworkInfo |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 89 | * @hide |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 90 | */ |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 91 | public static final int EVENT_NETWORK_INFO_CHANGED = BASE + 1; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Sent by the NetworkAgent to ConnectivityService to pass the current |
| 95 | * NetworkCapabilties. |
| 96 | * obj = NetworkCapabilities |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 97 | * @hide |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 98 | */ |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 99 | public static final int EVENT_NETWORK_CAPABILITIES_CHANGED = BASE + 2; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Sent by the NetworkAgent to ConnectivityService to pass the current |
| 103 | * NetworkProperties. |
| 104 | * obj = NetworkProperties |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 105 | * @hide |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 106 | */ |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 107 | public static final int EVENT_NETWORK_PROPERTIES_CHANGED = BASE + 3; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 108 | |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 109 | /** |
| 110 | * Centralize the place where base network score, and network score scaling, will be |
vandwalle | 2ab9089 | 2014-06-02 15:30:39 -0700 | [diff] [blame] | 111 | * stored, so as we can consistently compare apple and oranges, or wifi, ethernet and LTE |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 112 | * @hide |
vandwalle | 2ab9089 | 2014-06-02 15:30:39 -0700 | [diff] [blame] | 113 | */ |
| 114 | public static final int WIFI_BASE_SCORE = 60; |
| 115 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 116 | /** |
| 117 | * Sent by the NetworkAgent to ConnectivityService to pass the current |
| 118 | * network score. |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 119 | * obj = network score Integer |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 120 | * @hide |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 121 | */ |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 122 | public static final int EVENT_NETWORK_SCORE_CHANGED = BASE + 4; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 123 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 124 | /** |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 125 | * Sent by ConnectivityService to the NetworkAgent to inform the agent of the |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 126 | * networks status - whether we could use the network or could not, due to |
| 127 | * either a bad network configuration (no internet link) or captive portal. |
| 128 | * |
| 129 | * arg1 = either {@code VALID_NETWORK} or {@code INVALID_NETWORK} |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 130 | * obj = Bundle containing map from {@code REDIRECT_URL_KEY} to {@code String} |
| 131 | * representing URL that Internet probe was redirect to, if it was redirected, |
| 132 | * or mapping to {@code null} otherwise. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 133 | * @hide |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 134 | */ |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 135 | public static final int CMD_REPORT_NETWORK_STATUS = BASE + 7; |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 136 | |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * Network validation suceeded. |
| 140 | * Corresponds to {@link NetworkCapabilities.NET_CAPABILITY_VALIDATED}. |
| 141 | */ |
| 142 | public static final int VALIDATION_STATUS_VALID = 1; |
| 143 | |
| 144 | /** |
| 145 | * Network validation was attempted and failed. This may be received more than once as |
| 146 | * subsequent validation attempts are made. |
| 147 | */ |
| 148 | public static final int VALIDATION_STATUS_NOT_VALID = 2; |
| 149 | |
| 150 | // TODO: remove. |
| 151 | /** @hide */ |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 152 | public static final int VALID_NETWORK = 1; |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 153 | /** @hide */ |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 154 | public static final int INVALID_NETWORK = 2; |
| 155 | |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 156 | /** |
| 157 | * The key for the redirect URL in the Bundle argument of {@code CMD_REPORT_NETWORK_STATUS}. |
| 158 | * @hide |
| 159 | */ |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 160 | public static String REDIRECT_URL_KEY = "redirect URL"; |
| 161 | |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 162 | /** |
| 163 | * Sent by the NetworkAgent to ConnectivityService to indicate this network was |
| 164 | * explicitly selected. This should be sent before the NetworkInfo is marked |
| 165 | * CONNECTED so it can be given special treatment at that time. |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 166 | * |
| 167 | * obj = boolean indicating whether to use this network even if unvalidated |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 168 | * @hide |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 169 | */ |
Lorenzo Colitti | 6044616 | 2014-09-20 00:14:31 +0900 | [diff] [blame] | 170 | public static final int EVENT_SET_EXPLICITLY_SELECTED = BASE + 8; |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 171 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 172 | /** |
| 173 | * Sent by ConnectivityService to the NetworkAgent to inform the agent of |
| 174 | * whether the network should in the future be used even if not validated. |
| 175 | * This decision is made by the user, but it is the network transport's |
| 176 | * responsibility to remember it. |
| 177 | * |
| 178 | * arg1 = 1 if true, 0 if false |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 179 | * @hide |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 180 | */ |
| 181 | public static final int CMD_SAVE_ACCEPT_UNVALIDATED = BASE + 9; |
| 182 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 183 | /** |
| 184 | * Sent by ConnectivityService to the NetworkAgent to inform the agent to pull |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 185 | * the underlying network connection for updated bandwidth information. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 186 | * @hide |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 187 | */ |
| 188 | public static final int CMD_REQUEST_BANDWIDTH_UPDATE = BASE + 10; |
| 189 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 190 | /** |
| 191 | * Sent by ConnectivityService to the NetworkAgent to request that the specified packet be sent |
| 192 | * periodically on the given interval. |
| 193 | * |
| 194 | * arg1 = the slot number of the keepalive to start |
| 195 | * arg2 = interval in seconds |
| 196 | * obj = KeepalivePacketData object describing the data to be sent |
| 197 | * |
| 198 | * Also used internally by ConnectivityService / KeepaliveTracker, with different semantics. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 199 | * @hide |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 200 | */ |
junyulai | e413528 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 201 | public static final int CMD_START_SOCKET_KEEPALIVE = BASE + 11; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * Requests that the specified keepalive packet be stopped. |
| 205 | * |
| 206 | * arg1 = slot number of the keepalive to stop. |
| 207 | * |
| 208 | * Also used internally by ConnectivityService / KeepaliveTracker, with different semantics. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 209 | * @hide |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 210 | */ |
junyulai | e413528 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 211 | public static final int CMD_STOP_SOCKET_KEEPALIVE = BASE + 12; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 212 | |
| 213 | /** |
junyulai | e413528 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 214 | * Sent by the NetworkAgent to ConnectivityService to provide status on a socket keepalive |
| 215 | * request. This may either be the reply to a CMD_START_SOCKET_KEEPALIVE, or an asynchronous |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 216 | * error notification. |
| 217 | * |
junyulai | e413528 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 218 | * This is also sent by KeepaliveTracker to the app's {@link SocketKeepalive}, |
| 219 | * so that the app's {@link SocketKeepalive.Callback} methods can be called. |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 220 | * |
| 221 | * arg1 = slot number of the keepalive |
| 222 | * arg2 = error code |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 223 | * @hide |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 224 | */ |
junyulai | e413528 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 225 | public static final int EVENT_SOCKET_KEEPALIVE = BASE + 13; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 226 | |
junyulai | 76baf5f | 2019-05-17 22:13:02 +0800 | [diff] [blame] | 227 | /** |
| 228 | * Sent by ConnectivityService to inform this network transport of signal strength thresholds |
| 229 | * that when crossed should trigger a system wakeup and a NetworkCapabilities update. |
| 230 | * |
| 231 | * obj = int[] describing signal strength thresholds. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 232 | * @hide |
junyulai | 76baf5f | 2019-05-17 22:13:02 +0800 | [diff] [blame] | 233 | */ |
| 234 | public static final int CMD_SET_SIGNAL_STRENGTH_THRESHOLDS = BASE + 14; |
| 235 | |
| 236 | /** |
| 237 | * Sent by ConnectivityService to the NeworkAgent to inform the agent to avoid |
| 238 | * automatically reconnecting to this network (e.g. via autojoin). Happens |
| 239 | * when user selects "No" option on the "Stay connected?" dialog box. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 240 | * @hide |
junyulai | 76baf5f | 2019-05-17 22:13:02 +0800 | [diff] [blame] | 241 | */ |
| 242 | public static final int CMD_PREVENT_AUTOMATIC_RECONNECT = BASE + 15; |
| 243 | |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 244 | /** |
| 245 | * Sent by the KeepaliveTracker to NetworkAgent to add a packet filter. |
| 246 | * |
| 247 | * For TCP keepalive offloads, keepalive packets are sent by the firmware. However, because the |
| 248 | * remote site will send ACK packets in response to the keepalive packets, the firmware also |
| 249 | * needs to be configured to properly filter the ACKs to prevent the system from waking up. |
| 250 | * This does not happen with UDP, so this message is TCP-specific. |
| 251 | * arg1 = slot number of the keepalive to filter for. |
| 252 | * obj = the keepalive packet to send repeatedly. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 253 | * @hide |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 254 | */ |
| 255 | public static final int CMD_ADD_KEEPALIVE_PACKET_FILTER = BASE + 16; |
| 256 | |
| 257 | /** |
| 258 | * Sent by the KeepaliveTracker to NetworkAgent to remove a packet filter. See |
| 259 | * {@link #CMD_ADD_KEEPALIVE_PACKET_FILTER}. |
| 260 | * arg1 = slot number of the keepalive packet filter to remove. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 261 | * @hide |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 262 | */ |
| 263 | public static final int CMD_REMOVE_KEEPALIVE_PACKET_FILTER = BASE + 17; |
| 264 | |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 265 | // TODO : remove these two constructors. They are a stopgap measure to help sheperding a number |
| 266 | // of dependent changes that would conflict throughout the automerger graph. Having these |
| 267 | // temporarily helps with the process of going through with all these dependent changes across |
| 268 | // the entire tree. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 269 | /** @hide TODO: decide which of these to expose. */ |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 270 | public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni, |
| 271 | NetworkCapabilities nc, LinkProperties lp, int score) { |
Lorenzo Colitti | 6654b08 | 2020-01-10 00:40:28 +0900 | [diff] [blame] | 272 | this(looper, context, logTag, ni, nc, lp, score, null, NetworkProvider.ID_NONE); |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 273 | } |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 274 | |
| 275 | /** @hide TODO: decide which of these to expose. */ |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 276 | public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni, |
Lorenzo Colitti | d969656 | 2020-01-12 22:28:37 +0900 | [diff] [blame] | 277 | NetworkCapabilities nc, LinkProperties lp, int score, NetworkAgentConfig config) { |
| 278 | this(looper, context, logTag, ni, nc, lp, score, config, NetworkProvider.ID_NONE); |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 281 | /** @hide TODO: decide which of these to expose. */ |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 282 | public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni, |
Lorenzo Colitti | 6654b08 | 2020-01-10 00:40:28 +0900 | [diff] [blame] | 283 | NetworkCapabilities nc, LinkProperties lp, int score, int providerId) { |
| 284 | this(looper, context, logTag, ni, nc, lp, score, null, providerId); |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 285 | } |
| 286 | |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 287 | /** @hide TODO: decide which of these to expose. */ |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 288 | public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni, |
Lorenzo Colitti | d969656 | 2020-01-12 22:28:37 +0900 | [diff] [blame] | 289 | NetworkCapabilities nc, LinkProperties lp, int score, NetworkAgentConfig config, |
Lorenzo Colitti | 6654b08 | 2020-01-10 00:40:28 +0900 | [diff] [blame] | 290 | int providerId) { |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 291 | mHandler = new NetworkAgentHandler(looper); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 292 | LOG_TAG = logTag; |
| 293 | mContext = context; |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 294 | this.providerId = providerId; |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 295 | if (ni == null || nc == null || lp == null) { |
| 296 | throw new IllegalArgumentException(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 297 | } |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 298 | |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 299 | if (VDBG) log("Registering NetworkAgent"); |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 300 | ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService( |
| 301 | Context.CONNECTIVITY_SERVICE); |
Chalard Jean | a0e2aa12 | 2019-12-13 19:47:12 +0900 | [diff] [blame] | 302 | network = cm.registerNetworkAgent(new Messenger(mHandler), new NetworkInfo(ni), |
Lorenzo Colitti | d969656 | 2020-01-12 22:28:37 +0900 | [diff] [blame] | 303 | new LinkProperties(lp), new NetworkCapabilities(nc), score, config, |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 304 | providerId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 307 | private class NetworkAgentHandler extends Handler { |
| 308 | NetworkAgentHandler(Looper looper) { |
| 309 | super(looper); |
| 310 | } |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 311 | |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 312 | @Override |
| 313 | public void handleMessage(Message msg) { |
| 314 | switch (msg.what) { |
| 315 | case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: { |
| 316 | if (mAsyncChannel != null) { |
| 317 | log("Received new connection while already connected!"); |
| 318 | } else { |
| 319 | if (VDBG) log("NetworkAgent fully connected"); |
| 320 | AsyncChannel ac = new AsyncChannel(); |
| 321 | ac.connected(null, this, msg.replyTo); |
| 322 | ac.replyToMessage(msg, AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED, |
| 323 | AsyncChannel.STATUS_SUCCESSFUL); |
| 324 | synchronized (mPreConnectedQueue) { |
| 325 | mAsyncChannel = ac; |
| 326 | for (Message m : mPreConnectedQueue) { |
| 327 | ac.sendMessage(m); |
| 328 | } |
| 329 | mPreConnectedQueue.clear(); |
| 330 | } |
| 331 | } |
| 332 | break; |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 333 | } |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 334 | case AsyncChannel.CMD_CHANNEL_DISCONNECT: { |
| 335 | if (VDBG) log("CMD_CHANNEL_DISCONNECT"); |
| 336 | if (mAsyncChannel != null) mAsyncChannel.disconnect(); |
| 337 | break; |
| 338 | } |
| 339 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { |
| 340 | if (DBG) log("NetworkAgent channel lost"); |
| 341 | // let the client know CS is done with us. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 342 | onNetworkUnwanted(); |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 343 | synchronized (mPreConnectedQueue) { |
| 344 | mAsyncChannel = null; |
| 345 | } |
| 346 | break; |
| 347 | } |
| 348 | case CMD_SUSPECT_BAD: { |
| 349 | log("Unhandled Message " + msg); |
| 350 | break; |
| 351 | } |
| 352 | case CMD_REQUEST_BANDWIDTH_UPDATE: { |
| 353 | long currentTimeMs = System.currentTimeMillis(); |
| 354 | if (VDBG) { |
| 355 | log("CMD_REQUEST_BANDWIDTH_UPDATE request received."); |
| 356 | } |
| 357 | if (currentTimeMs >= (mLastBwRefreshTime + BW_REFRESH_MIN_WIN_MS)) { |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 358 | mBandwidthUpdateScheduled = false; |
| 359 | if (!mBandwidthUpdatePending.getAndSet(true)) { |
| 360 | onBandwidthUpdateRequested(); |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 361 | } |
| 362 | } else { |
| 363 | // deliver the request at a later time rather than discard it completely. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 364 | if (!mBandwidthUpdateScheduled) { |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 365 | long waitTime = mLastBwRefreshTime + BW_REFRESH_MIN_WIN_MS |
| 366 | - currentTimeMs + 1; |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 367 | mBandwidthUpdateScheduled = sendEmptyMessageDelayed( |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 368 | CMD_REQUEST_BANDWIDTH_UPDATE, waitTime); |
| 369 | } |
| 370 | } |
| 371 | break; |
| 372 | } |
| 373 | case CMD_REPORT_NETWORK_STATUS: { |
| 374 | String redirectUrl = ((Bundle) msg.obj).getString(REDIRECT_URL_KEY); |
| 375 | if (VDBG) { |
| 376 | log("CMD_REPORT_NETWORK_STATUS(" |
| 377 | + (msg.arg1 == VALID_NETWORK ? "VALID, " : "INVALID, ") |
| 378 | + redirectUrl); |
| 379 | } |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 380 | onValidationStatus(msg.arg1 /* status */, redirectUrl); |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 381 | break; |
| 382 | } |
| 383 | case CMD_SAVE_ACCEPT_UNVALIDATED: { |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 384 | onSaveAcceptUnvalidated(msg.arg1 != 0); |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 385 | break; |
| 386 | } |
| 387 | case CMD_START_SOCKET_KEEPALIVE: { |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 388 | onStartSocketKeepalive(msg.arg1 /* slot */, msg.arg2 /* interval */, |
| 389 | (KeepalivePacketData) msg.obj /* packet */); |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 390 | break; |
| 391 | } |
| 392 | case CMD_STOP_SOCKET_KEEPALIVE: { |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 393 | onStopSocketKeepalive(msg.arg1 /* slot */); |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 394 | break; |
| 395 | } |
| 396 | |
| 397 | case CMD_SET_SIGNAL_STRENGTH_THRESHOLDS: { |
| 398 | ArrayList<Integer> thresholds = |
| 399 | ((Bundle) msg.obj).getIntegerArrayList("thresholds"); |
| 400 | // TODO: Change signal strength thresholds API to use an ArrayList<Integer> |
| 401 | // rather than convert to int[]. |
| 402 | int[] intThresholds = new int[(thresholds != null) ? thresholds.size() : 0]; |
| 403 | for (int i = 0; i < intThresholds.length; i++) { |
| 404 | intThresholds[i] = thresholds.get(i); |
| 405 | } |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 406 | onSignalStrengthThresholdsUpdated(intThresholds); |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 407 | break; |
| 408 | } |
| 409 | case CMD_PREVENT_AUTOMATIC_RECONNECT: { |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 410 | onAutomaticReconnectDisabled(); |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | case CMD_ADD_KEEPALIVE_PACKET_FILTER: { |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 414 | onAddKeepalivePacketFilter(msg.arg1 /* slot */, |
| 415 | (KeepalivePacketData) msg.obj /* packet */); |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 416 | break; |
| 417 | } |
| 418 | case CMD_REMOVE_KEEPALIVE_PACKET_FILTER: { |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 419 | onRemoveKeepalivePacketFilter(msg.arg1 /* slot */); |
Lorenzo Colitti | 782b48d5 | 2020-01-12 20:27:32 +0900 | [diff] [blame] | 420 | break; |
| 421 | } |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 422 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 426 | private void queueOrSendMessage(int what, Object obj) { |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 427 | queueOrSendMessage(what, 0, 0, obj); |
| 428 | } |
| 429 | |
| 430 | private void queueOrSendMessage(int what, int arg1, int arg2) { |
| 431 | queueOrSendMessage(what, arg1, arg2, null); |
| 432 | } |
| 433 | |
| 434 | private void queueOrSendMessage(int what, int arg1, int arg2, Object obj) { |
| 435 | Message msg = Message.obtain(); |
| 436 | msg.what = what; |
| 437 | msg.arg1 = arg1; |
| 438 | msg.arg2 = arg2; |
| 439 | msg.obj = obj; |
| 440 | queueOrSendMessage(msg); |
| 441 | } |
| 442 | |
| 443 | private void queueOrSendMessage(Message msg) { |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 444 | synchronized (mPreConnectedQueue) { |
| 445 | if (mAsyncChannel != null) { |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 446 | mAsyncChannel.sendMessage(msg); |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 447 | } else { |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 448 | mPreConnectedQueue.add(msg); |
Lorenzo Colitti | 9a6a11a | 2014-05-24 02:25:55 +0900 | [diff] [blame] | 449 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 453 | /** |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 454 | * Must be called by the agent when the network's {@link LinkProperties} change. |
| 455 | * @param linkProperties the new LinkProperties. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 456 | */ |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 457 | public void sendLinkProperties(@NonNull LinkProperties linkProperties) { |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 458 | queueOrSendMessage(EVENT_NETWORK_PROPERTIES_CHANGED, new LinkProperties(linkProperties)); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /** |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 462 | * Must be called by the agent when it has a new NetworkInfo object. |
| 463 | * @hide TODO: expose something better. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 464 | */ |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 465 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 466 | public void sendNetworkInfo(NetworkInfo networkInfo) { |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 467 | queueOrSendMessage(EVENT_NETWORK_INFO_CHANGED, new NetworkInfo(networkInfo)); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /** |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 471 | * Must be called by the agent when the network's {@link NetworkCapabilities} change. |
| 472 | * @param networkCapabilities the new NetworkCapabilities. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 473 | */ |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 474 | public void sendNetworkCapabilities(@NonNull NetworkCapabilities networkCapabilities) { |
| 475 | mBandwidthUpdatePending.set(false); |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 476 | mLastBwRefreshTime = System.currentTimeMillis(); |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 477 | queueOrSendMessage(EVENT_NETWORK_CAPABILITIES_CHANGED, |
| 478 | new NetworkCapabilities(networkCapabilities)); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /** |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 482 | * Must be called by the agent to update the score of this network. |
| 483 | * @param score the new score. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 484 | */ |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 485 | public void sendNetworkScore(int score) { |
Robert Greenwalt | 35f7a94 | 2014-09-09 14:46:37 -0700 | [diff] [blame] | 486 | if (score < 0) { |
| 487 | throw new IllegalArgumentException("Score must be >= 0"); |
| 488 | } |
lucaslin | c2bac51 | 2019-11-07 16:47:56 +0800 | [diff] [blame] | 489 | final NetworkScore ns = new NetworkScore(); |
| 490 | ns.putIntExtension(NetworkScore.LEGACY_SCORE, score); |
| 491 | updateScore(ns); |
| 492 | } |
| 493 | |
| 494 | /** |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 495 | * Must be called by the agent when it has a new {@link NetworkScore} for this network. |
| 496 | * @param ns the new score. |
| 497 | * @hide TODO: unhide the NetworkScore class, and rename to sendNetworkScore. |
lucaslin | c2bac51 | 2019-11-07 16:47:56 +0800 | [diff] [blame] | 498 | */ |
| 499 | public void updateScore(@NonNull NetworkScore ns) { |
| 500 | queueOrSendMessage(EVENT_NETWORK_SCORE_CHANGED, new NetworkScore(ns)); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 503 | /** |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 504 | * Must be called by the agent to indicate this network was manually selected by the user. |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 505 | * This should be called before the NetworkInfo is marked CONNECTED so that this |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 506 | * Network can be given special treatment at that time. If {@code acceptUnvalidated} is |
| 507 | * {@code true}, then the system will switch to this network. If it is {@code false} and the |
| 508 | * network cannot be validated, the system will ask the user whether to switch to this network. |
| 509 | * If the user confirms and selects "don't ask again", then the system will call |
| 510 | * {@link #saveAcceptUnvalidated} to persist the user's choice. Thus, if the transport ever |
| 511 | * calls this method with {@code acceptUnvalidated} set to {@code false}, it must also implement |
| 512 | * {@link #saveAcceptUnvalidated} to respect the user's choice. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 513 | * @hide should move to NetworkAgentConfig. |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 514 | */ |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 515 | public void explicitlySelected(boolean acceptUnvalidated) { |
Lorenzo Colitti | 0e33bd1 | 2019-06-04 14:37:26 +0900 | [diff] [blame] | 516 | explicitlySelected(true /* explicitlySelected */, acceptUnvalidated); |
| 517 | } |
| 518 | |
| 519 | /** |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 520 | * Must be called by the agent to indicate whether the network was manually selected by the |
| 521 | * user. This should be called before the network becomes connected, so it can be given |
| 522 | * special treatment when it does. |
Lorenzo Colitti | b5ad613 | 2019-06-04 22:15:33 +0900 | [diff] [blame] | 523 | * |
| 524 | * If {@code explicitlySelected} is {@code true}, and {@code acceptUnvalidated} is {@code true}, |
| 525 | * then the system will switch to this network. If {@code explicitlySelected} is {@code true} |
| 526 | * and {@code acceptUnvalidated} is {@code false}, and the network cannot be validated, the |
| 527 | * system will ask the user whether to switch to this network. If the user confirms and selects |
| 528 | * "don't ask again", then the system will call {@link #saveAcceptUnvalidated} to persist the |
| 529 | * user's choice. Thus, if the transport ever calls this method with {@code explicitlySelected} |
| 530 | * set to {@code true} and {@code acceptUnvalidated} set to {@code false}, it must also |
| 531 | * implement {@link #saveAcceptUnvalidated} to respect the user's choice. |
| 532 | * |
| 533 | * If {@code explicitlySelected} is {@code false} and {@code acceptUnvalidated} is |
| 534 | * {@code true}, the system will interpret this as the user having accepted partial connectivity |
| 535 | * on this network. Thus, the system will switch to the network and consider it validated even |
| 536 | * if it only provides partial connectivity, but the network is not otherwise treated specially. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 537 | * @hide should move to NetworkAgentConfig. |
Lorenzo Colitti | 0e33bd1 | 2019-06-04 14:37:26 +0900 | [diff] [blame] | 538 | */ |
| 539 | public void explicitlySelected(boolean explicitlySelected, boolean acceptUnvalidated) { |
| 540 | queueOrSendMessage(EVENT_SET_EXPLICITLY_SELECTED, |
| 541 | explicitlySelected ? 1 : 0, |
| 542 | acceptUnvalidated ? 1 : 0); |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /** |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 546 | * Called when ConnectivityService has indicated they no longer want this network. |
| 547 | * The parent factory should (previously) have received indication of the change |
| 548 | * as well, either canceling NetworkRequests or altering their score such that this |
| 549 | * network won't be immediately requested again. |
| 550 | */ |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 551 | public void onNetworkUnwanted() { |
| 552 | unwanted(); |
| 553 | } |
| 554 | /** @hide TODO delete once subclasses have moved to onNetworkUnwanted. */ |
| 555 | protected void unwanted() { |
| 556 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 557 | |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 558 | /** |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 559 | * Called when ConnectivityService request a bandwidth update. The parent factory |
| 560 | * shall try to overwrite this method and produce a bandwidth update if capable. |
| 561 | */ |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 562 | public void onBandwidthUpdateRequested() { |
| 563 | pollLceData(); |
| 564 | } |
| 565 | /** @hide TODO delete once subclasses have moved to onBandwidthUpdateRequested. */ |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 566 | protected void pollLceData() { |
| 567 | } |
| 568 | |
| 569 | /** |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 570 | * Called when the system determines the usefulness of this network. |
| 571 | * |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 572 | * The system attempts to validate Internet connectivity on networks that provide the |
markchien | 06b9dfb | 2020-01-15 20:41:31 +0800 | [diff] [blame] | 573 | * {@link NetworkCapabilities#NET_CAPABILITY_INTERNET} capability. |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 574 | * |
| 575 | * Currently there are two possible values: |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 576 | * {@code VALIDATION_STATUS_VALID} if Internet connectivity was validated, |
| 577 | * {@code VALIDATION_STATUS_NOT_VALID} if Internet connectivity was not validated. |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 578 | * |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 579 | * This may be called multiple times as network status changes, or if there are multiple |
| 580 | * subsequent attempts to validate connectivity that fail. |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 581 | * |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 582 | * @param status one of {@code VALIDATION_STATUS_VALID} or {@code VALIDATION_STATUS_NOT_VALID}. |
| 583 | * @param redirectUrl If Internet connectivity is being redirected (e.g., on a captive portal), |
| 584 | * this is the destination the probes are being redirected to, otherwise {@code null}. |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 585 | */ |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 586 | public void onValidationStatus(int status, @Nullable String redirectUrl) { |
| 587 | networkStatus(status, redirectUrl); |
| 588 | } |
| 589 | /** @hide TODO delete once subclasses have moved to onValidationStatus */ |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 590 | protected void networkStatus(int status, String redirectUrl) { |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 593 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 594 | /** |
| 595 | * Called when the user asks to remember the choice to use this network even if unvalidated. |
| 596 | * The transport is responsible for remembering the choice, and the next time the user connects |
| 597 | * to the network, should explicitlySelected with {@code acceptUnvalidated} set to {@code true}. |
| 598 | * This method will only be called if {@link #explicitlySelected} was called with |
| 599 | * {@code acceptUnvalidated} set to {@code false}. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 600 | * @param accept whether the user wants to use the network even if unvalidated. |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 601 | */ |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 602 | public void onSaveAcceptUnvalidated(boolean accept) { |
| 603 | saveAcceptUnvalidated(accept); |
| 604 | } |
| 605 | /** @hide TODO delete once subclasses have moved to onSaveAcceptUnvalidated */ |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 606 | protected void saveAcceptUnvalidated(boolean accept) { |
| 607 | } |
| 608 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 609 | /** |
| 610 | * Requests that the network hardware send the specified packet at the specified interval. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 611 | * |
| 612 | * @param slot the hardware slot on which to start the keepalive. |
| 613 | * @param intervalSeconds the interval between packets |
| 614 | * @param packet the packet to send. |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 615 | */ |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 616 | public void onStartSocketKeepalive(int slot, int intervalSeconds, |
| 617 | @NonNull KeepalivePacketData packet) { |
| 618 | Message msg = mHandler.obtainMessage(CMD_START_SOCKET_KEEPALIVE, slot, intervalSeconds, |
| 619 | packet); |
| 620 | startSocketKeepalive(msg); |
| 621 | msg.recycle(); |
| 622 | } |
| 623 | /** @hide TODO delete once subclasses have moved to onStartSocketKeepalive */ |
junyulai | e413528 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 624 | protected void startSocketKeepalive(Message msg) { |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 625 | onSocketKeepaliveEvent(msg.arg1, SocketKeepalive.ERROR_UNSUPPORTED); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /** |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 629 | * Requests that the network hardware stop a previously-started keepalive. |
| 630 | * |
| 631 | * @param slot the hardware slot on which to stop the keepalive. |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 632 | */ |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 633 | public void onStopSocketKeepalive(int slot) { |
| 634 | Message msg = mHandler.obtainMessage(CMD_STOP_SOCKET_KEEPALIVE, slot, 0, null); |
| 635 | stopSocketKeepalive(msg); |
| 636 | msg.recycle(); |
| 637 | } |
| 638 | /** @hide TODO delete once subclasses have moved to onStopSocketKeepalive */ |
junyulai | e413528 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 639 | protected void stopSocketKeepalive(Message msg) { |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 640 | onSocketKeepaliveEvent(msg.arg1, SocketKeepalive.ERROR_UNSUPPORTED); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /** |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 644 | * Must be called by the agent when a socket keepalive event occurs. |
| 645 | * |
| 646 | * @param slot the hardware slot on which the event occurred. |
| 647 | * @param event the event that occurred. |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 648 | */ |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 649 | public void sendSocketKeepaliveEvent(int slot, int event) { |
| 650 | queueOrSendMessage(EVENT_SOCKET_KEEPALIVE, slot, event); |
| 651 | } |
| 652 | /** @hide TODO delete once callers have moved to sendSocketKeepaliveEvent */ |
junyulai | e413528 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 653 | public void onSocketKeepaliveEvent(int slot, int reason) { |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 654 | sendSocketKeepaliveEvent(slot, reason); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 655 | } |
| 656 | |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 657 | /** |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 658 | * Called by ConnectivityService to add specific packet filter to network hardware to block |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 659 | * replies (e.g., TCP ACKs) matching the sent keepalive packets. Implementations that support |
| 660 | * this feature must override this method. |
| 661 | * |
| 662 | * @param slot the hardware slot on which the keepalive should be sent. |
| 663 | * @param packet the packet that is being sent. |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 664 | */ |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 665 | public void onAddKeepalivePacketFilter(int slot, @NonNull KeepalivePacketData packet) { |
| 666 | Message msg = mHandler.obtainMessage(CMD_ADD_KEEPALIVE_PACKET_FILTER, slot, 0, packet); |
| 667 | addKeepalivePacketFilter(msg); |
| 668 | msg.recycle(); |
| 669 | } |
| 670 | /** @hide TODO delete once subclasses have moved to onAddKeepalivePacketFilter */ |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 671 | protected void addKeepalivePacketFilter(Message msg) { |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /** |
| 675 | * Called by ConnectivityService to remove a packet filter installed with |
| 676 | * {@link #addKeepalivePacketFilter(Message)}. Implementations that support this feature |
| 677 | * must override this method. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 678 | * |
| 679 | * @param slot the hardware slot on which the keepalive is being sent. |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 680 | */ |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 681 | public void onRemoveKeepalivePacketFilter(int slot) { |
| 682 | Message msg = mHandler.obtainMessage(CMD_REMOVE_KEEPALIVE_PACKET_FILTER, slot, 0, null); |
| 683 | removeKeepalivePacketFilter(msg); |
| 684 | msg.recycle(); |
| 685 | } |
| 686 | /** @hide TODO delete once subclasses have moved to onRemoveKeepalivePacketFilter */ |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 687 | protected void removeKeepalivePacketFilter(Message msg) { |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | /** |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 691 | * Called by ConnectivityService to inform this network transport of signal strength thresholds |
| 692 | * that when crossed should trigger a system wakeup and a NetworkCapabilities update. |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 693 | * |
| 694 | * @param thresholds the array of thresholds that should trigger wakeups. |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 695 | */ |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 696 | public void onSignalStrengthThresholdsUpdated(@NonNull int[] thresholds) { |
| 697 | setSignalStrengthThresholds(thresholds); |
| 698 | } |
| 699 | /** @hide TODO delete once subclasses have moved to onSetSignalStrengthThresholds */ |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 700 | protected void setSignalStrengthThresholds(int[] thresholds) { |
| 701 | } |
| 702 | |
Paul Jensen | bf10991 | 2015-07-29 11:31:53 -0400 | [diff] [blame] | 703 | /** |
Paul Jensen | f95d220 | 2015-07-13 15:19:51 -0400 | [diff] [blame] | 704 | * Called when the user asks to not stay connected to this network because it was found to not |
| 705 | * provide Internet access. Usually followed by call to {@code unwanted}. The transport is |
| 706 | * responsible for making sure the device does not automatically reconnect to the same network |
| 707 | * after the {@code unwanted} call. |
| 708 | */ |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 709 | public void onAutomaticReconnectDisabled() { |
| 710 | preventAutomaticReconnect(); |
| 711 | } |
| 712 | /** @hide TODO delete once subclasses have moved to onAutomaticReconnectDisabled */ |
Paul Jensen | f95d220 | 2015-07-13 15:19:51 -0400 | [diff] [blame] | 713 | protected void preventAutomaticReconnect() { |
| 714 | } |
| 715 | |
Lorenzo Colitti | 631f33a | 2020-01-15 00:13:37 +0900 | [diff] [blame] | 716 | /** @hide */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 717 | protected void log(String s) { |
| 718 | Log.d(LOG_TAG, "NetworkAgent: " + s); |
| 719 | } |
| 720 | } |