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