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