blob: 9e360e11bf8b5483391f35ee5b17f3299085b9f7 [file] [log] [blame]
Robert Greenwalt7b816022014-04-18 15:25:25 -07001/*
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
17package android.net;
18
19import android.content.Context;
Erik Kline9d598e12015-07-13 16:37:51 +090020import android.os.Bundle;
Robert Greenwalt7b816022014-04-18 15:25:25 -070021import android.os.Handler;
22import android.os.Looper;
23import android.os.Message;
24import android.os.Messenger;
Robert Greenwalt7b816022014-04-18 15:25:25 -070025import android.util.Log;
Robert Greenwalt7b816022014-04-18 15:25:25 -070026
27import com.android.internal.util.AsyncChannel;
28import com.android.internal.util.Protocol;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +090029import android.net.ConnectivityManager.PacketKeepalive;
Robert Greenwalt7b816022014-04-18 15:25:25 -070030
Robert Greenwalt3192dec2014-05-27 13:20:24 -070031import java.util.ArrayList;
fenglu95ce8032015-05-01 17:04:36 -070032import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt7b816022014-04-18 15:25:25 -070033
34/**
Robert Greenwalt3192dec2014-05-27 13:20:24 -070035 * A Utility class for handling for communicating between bearer-specific
36 * code and ConnectivityService.
Robert Greenwalt7b816022014-04-18 15:25:25 -070037 *
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 Greenwalt3192dec2014-05-27 13:20:24 -070040 * perhaps connections with different SSID or P2P for Wi-Fi).
Robert Greenwalt7b816022014-04-18 15:25:25 -070041 *
Robert Greenwalt7b816022014-04-18 15:25:25 -070042 * @hide
43 */
44public abstract class NetworkAgent extends Handler {
Paul Jensen31a94f42015-02-13 14:18:39 -050045 // Guaranteed to be valid (not NETID_UNSET), otherwise registerNetworkAgent() would have thrown
46 // an exception.
47 public final int netId;
48
Robert Greenwalt3192dec2014-05-27 13:20:24 -070049 private volatile AsyncChannel mAsyncChannel;
Robert Greenwalt7b816022014-04-18 15:25:25 -070050 private final String LOG_TAG;
51 private static final boolean DBG = true;
Robert Greenwaltfc0c6892014-08-27 14:34:02 -070052 private static final boolean VDBG = false;
Robert Greenwalt7b816022014-04-18 15:25:25 -070053 private final Context mContext;
Robert Greenwalt3192dec2014-05-27 13:20:24 -070054 private final ArrayList<Message>mPreConnectedQueue = new ArrayList<Message>();
fenglub15e72b2015-03-20 11:29:56 -070055 private volatile long mLastBwRefreshTime = 0;
56 private static final long BW_REFRESH_MIN_WIN_MS = 500;
fenglu95ce8032015-05-01 17:04:36 -070057 private boolean mPollLceScheduled = false;
58 private AtomicBoolean mPollLcePending = new AtomicBoolean(false);
Robert Greenwalt7b816022014-04-18 15:25:25 -070059
60 private static final int BASE = Protocol.BASE_NETWORK_AGENT;
61
62 /**
Robert Greenwalt7b816022014-04-18 15:25:25 -070063 * 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 Greenwalt3192dec2014-05-27 13:20:24 -070067 public static final int CMD_SUSPECT_BAD = BASE;
Robert Greenwalt7b816022014-04-18 15:25:25 -070068
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 Greenwalt3192dec2014-05-27 13:20:24 -070075 public static final int EVENT_NETWORK_INFO_CHANGED = BASE + 1;
Robert Greenwalt7b816022014-04-18 15:25:25 -070076
77 /**
78 * Sent by the NetworkAgent to ConnectivityService to pass the current
79 * NetworkCapabilties.
80 * obj = NetworkCapabilities
81 */
Robert Greenwalt3192dec2014-05-27 13:20:24 -070082 public static final int EVENT_NETWORK_CAPABILITIES_CHANGED = BASE + 2;
Robert Greenwalt7b816022014-04-18 15:25:25 -070083
84 /**
85 * Sent by the NetworkAgent to ConnectivityService to pass the current
86 * NetworkProperties.
87 * obj = NetworkProperties
88 */
Robert Greenwalt3192dec2014-05-27 13:20:24 -070089 public static final int EVENT_NETWORK_PROPERTIES_CHANGED = BASE + 3;
Robert Greenwalt7b816022014-04-18 15:25:25 -070090
vandwalle2ab90892014-06-02 15:30:39 -070091 /* 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 Greenwalt7b816022014-04-18 15:25:25 -070096 /**
97 * Sent by the NetworkAgent to ConnectivityService to pass the current
98 * network score.
Robert Greenwalt3192dec2014-05-27 13:20:24 -070099 * obj = network score Integer
Robert Greenwalt7b816022014-04-18 15:25:25 -0700100 */
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700101 public static final int EVENT_NETWORK_SCORE_CHANGED = BASE + 4;
Robert Greenwalt7b816022014-04-18 15:25:25 -0700102
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400103 /**
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 Ramachandran42065ac2014-07-27 00:37:35 -0700117 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900118 * Sent by ConnectivityService to the NetworkAgent to inform the agent of the
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700119 * 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 Colitti60446162014-09-20 00:14:31 +0900124 public static final int CMD_REPORT_NETWORK_STATUS = BASE + 7;
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700125
126 public static final int VALID_NETWORK = 1;
127 public static final int INVALID_NETWORK = 2;
128
Robert Greenwalte73cc462014-09-07 16:50:01 -0700129 /**
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 Colittie03c3c72015-04-03 16:38:52 +0900133 *
134 * obj = boolean indicating whether to use this network even if unvalidated
Robert Greenwalte73cc462014-09-07 16:50:01 -0700135 */
Lorenzo Colitti60446162014-09-20 00:14:31 +0900136 public static final int EVENT_SET_EXPLICITLY_SELECTED = BASE + 8;
Robert Greenwalte73cc462014-09-07 16:50:01 -0700137
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900138 /**
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 Colitti8bf977d2015-06-15 14:29:22 +0900148 /**
149 * Sent by ConnectivityService to the NetworkAgent to inform the agent to pull
fenglub15e72b2015-03-20 11:29:56 -0700150 * the underlying network connection for updated bandwidth information.
151 */
152 public static final int CMD_REQUEST_BANDWIDTH_UPDATE = BASE + 10;
153
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +0900154 /**
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 Colittic3f21f32015-07-06 23:50:27 +0900188 /**
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 Jensenbf109912015-07-29 11:31:53 -0400196 /**
Paul Jensenf95d2202015-07-13 15:19:51 -0400197 * 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 Jensenbf109912015-07-29 11:31:53 -0400201 public static final int CMD_PREVENT_AUTOMATIC_RECONNECT = BASE + 15;
Paul Jensenf95d2202015-07-13 15:19:51 -0400202
Paul Jensen578a76e2016-01-14 14:54:39 -0500203 /**
204 * Sent by ConnectivityService to the NetworkAgent to install an APF program in the network
205 * chipset for use to filter packets.
206 *
207 * obj = byte[] containing the APF program bytecode.
208 */
209 public static final int CMD_PUSH_APF_PROGRAM = BASE + 16;
210
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700211 public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
212 NetworkCapabilities nc, LinkProperties lp, int score) {
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -0700213 this(looper, context, logTag, ni, nc, lp, score, null);
214 }
215
216 public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
217 NetworkCapabilities nc, LinkProperties lp, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -0700218 super(looper);
219 LOG_TAG = logTag;
220 mContext = context;
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700221 if (ni == null || nc == null || lp == null) {
222 throw new IllegalArgumentException();
Robert Greenwalt7b816022014-04-18 15:25:25 -0700223 }
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700224
Robert Greenwaltfc0c6892014-08-27 14:34:02 -0700225 if (VDBG) log("Registering NetworkAgent");
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700226 ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(
227 Context.CONNECTIVITY_SERVICE);
Paul Jensen31a94f42015-02-13 14:18:39 -0500228 netId = cm.registerNetworkAgent(new Messenger(this), new NetworkInfo(ni),
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -0700229 new LinkProperties(lp), new NetworkCapabilities(nc), score, misc);
Robert Greenwalt7b816022014-04-18 15:25:25 -0700230 }
231
232 @Override
233 public void handleMessage(Message msg) {
234 switch (msg.what) {
235 case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700236 if (mAsyncChannel != null) {
237 log("Received new connection while already connected!");
238 } else {
Robert Greenwaltfc0c6892014-08-27 14:34:02 -0700239 if (VDBG) log("NetworkAgent fully connected");
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700240 AsyncChannel ac = new AsyncChannel();
241 ac.connected(null, this, msg.replyTo);
242 ac.replyToMessage(msg, AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED,
243 AsyncChannel.STATUS_SUCCESSFUL);
244 synchronized (mPreConnectedQueue) {
245 mAsyncChannel = ac;
246 for (Message m : mPreConnectedQueue) {
247 ac.sendMessage(m);
248 }
249 mPreConnectedQueue.clear();
Robert Greenwalt7b816022014-04-18 15:25:25 -0700250 }
251 }
252 break;
253 }
254 case AsyncChannel.CMD_CHANNEL_DISCONNECT: {
Robert Greenwaltfc0c6892014-08-27 14:34:02 -0700255 if (VDBG) log("CMD_CHANNEL_DISCONNECT");
Robert Greenwalt7b816022014-04-18 15:25:25 -0700256 if (mAsyncChannel != null) mAsyncChannel.disconnect();
257 break;
258 }
259 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
260 if (DBG) log("NetworkAgent channel lost");
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700261 // let the client know CS is done with us.
262 unwanted();
263 synchronized (mPreConnectedQueue) {
264 mAsyncChannel = null;
265 }
Robert Greenwalt7b816022014-04-18 15:25:25 -0700266 break;
267 }
268 case CMD_SUSPECT_BAD: {
269 log("Unhandled Message " + msg);
270 break;
271 }
fenglub15e72b2015-03-20 11:29:56 -0700272 case CMD_REQUEST_BANDWIDTH_UPDATE: {
fenglu95ce8032015-05-01 17:04:36 -0700273 long currentTimeMs = System.currentTimeMillis();
fenglub15e72b2015-03-20 11:29:56 -0700274 if (VDBG) {
275 log("CMD_REQUEST_BANDWIDTH_UPDATE request received.");
276 }
fenglu95ce8032015-05-01 17:04:36 -0700277 if (currentTimeMs >= (mLastBwRefreshTime + BW_REFRESH_MIN_WIN_MS)) {
278 mPollLceScheduled = false;
279 if (mPollLcePending.getAndSet(true) == false) {
280 pollLceData();
281 }
282 } else {
283 // deliver the request at a later time rather than discard it completely.
284 if (!mPollLceScheduled) {
285 long waitTime = mLastBwRefreshTime + BW_REFRESH_MIN_WIN_MS -
286 currentTimeMs + 1;
287 mPollLceScheduled = sendEmptyMessageDelayed(
288 CMD_REQUEST_BANDWIDTH_UPDATE, waitTime);
289 }
fenglub15e72b2015-03-20 11:29:56 -0700290 }
291 break;
292 }
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700293 case CMD_REPORT_NETWORK_STATUS: {
294 if (VDBG) {
295 log("CMD_REPORT_NETWORK_STATUS(" +
296 (msg.arg1 == VALID_NETWORK ? "VALID)" : "INVALID)"));
297 }
298 networkStatus(msg.arg1);
299 break;
300 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900301 case CMD_SAVE_ACCEPT_UNVALIDATED: {
302 saveAcceptUnvalidated(msg.arg1 != 0);
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +0900303 break;
304 }
305 case CMD_START_PACKET_KEEPALIVE: {
306 startPacketKeepalive(msg);
307 break;
308 }
309 case CMD_STOP_PACKET_KEEPALIVE: {
310 stopPacketKeepalive(msg);
311 break;
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900312 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900313
314 case CMD_SET_SIGNAL_STRENGTH_THRESHOLDS: {
Erik Kline9d598e12015-07-13 16:37:51 +0900315 ArrayList<Integer> thresholds =
316 ((Bundle) msg.obj).getIntegerArrayList("thresholds");
317 // TODO: Change signal strength thresholds API to use an ArrayList<Integer>
318 // rather than convert to int[].
319 int[] intThresholds = new int[(thresholds != null) ? thresholds.size() : 0];
320 for (int i = 0; i < intThresholds.length; i++) {
321 intThresholds[i] = thresholds.get(i);
322 }
323 setSignalStrengthThresholds(intThresholds);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900324 break;
325 }
Paul Jensenf95d2202015-07-13 15:19:51 -0400326 case CMD_PREVENT_AUTOMATIC_RECONNECT: {
327 preventAutomaticReconnect();
328 break;
Robert Greenwalt7b816022014-04-18 15:25:25 -0700329 }
Paul Jensen578a76e2016-01-14 14:54:39 -0500330 case CMD_PUSH_APF_PROGRAM: {
331 installPacketFilter((byte[]) msg.obj);
332 break;
333 }
Robert Greenwalt7b816022014-04-18 15:25:25 -0700334 }
335 }
336
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700337 private void queueOrSendMessage(int what, Object obj) {
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +0900338 queueOrSendMessage(what, 0, 0, obj);
339 }
340
341 private void queueOrSendMessage(int what, int arg1, int arg2) {
342 queueOrSendMessage(what, arg1, arg2, null);
343 }
344
345 private void queueOrSendMessage(int what, int arg1, int arg2, Object obj) {
346 Message msg = Message.obtain();
347 msg.what = what;
348 msg.arg1 = arg1;
349 msg.arg2 = arg2;
350 msg.obj = obj;
351 queueOrSendMessage(msg);
352 }
353
354 private void queueOrSendMessage(Message msg) {
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700355 synchronized (mPreConnectedQueue) {
356 if (mAsyncChannel != null) {
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +0900357 mAsyncChannel.sendMessage(msg);
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700358 } else {
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700359 mPreConnectedQueue.add(msg);
Lorenzo Colitti9a6a11a2014-05-24 02:25:55 +0900360 }
Robert Greenwalt7b816022014-04-18 15:25:25 -0700361 }
362 }
363
Robert Greenwalt7b816022014-04-18 15:25:25 -0700364 /**
365 * Called by the bearer code when it has new LinkProperties data.
Robert Greenwalt7b816022014-04-18 15:25:25 -0700366 */
367 public void sendLinkProperties(LinkProperties linkProperties) {
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700368 queueOrSendMessage(EVENT_NETWORK_PROPERTIES_CHANGED, new LinkProperties(linkProperties));
Robert Greenwalt7b816022014-04-18 15:25:25 -0700369 }
370
371 /**
372 * Called by the bearer code when it has new NetworkInfo data.
Robert Greenwalt7b816022014-04-18 15:25:25 -0700373 */
374 public void sendNetworkInfo(NetworkInfo networkInfo) {
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700375 queueOrSendMessage(EVENT_NETWORK_INFO_CHANGED, new NetworkInfo(networkInfo));
Robert Greenwalt7b816022014-04-18 15:25:25 -0700376 }
377
378 /**
379 * Called by the bearer code when it has new NetworkCapabilities data.
Robert Greenwalt7b816022014-04-18 15:25:25 -0700380 */
381 public void sendNetworkCapabilities(NetworkCapabilities networkCapabilities) {
fenglu95ce8032015-05-01 17:04:36 -0700382 mPollLcePending.set(false);
fenglub15e72b2015-03-20 11:29:56 -0700383 mLastBwRefreshTime = System.currentTimeMillis();
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700384 queueOrSendMessage(EVENT_NETWORK_CAPABILITIES_CHANGED,
385 new NetworkCapabilities(networkCapabilities));
Robert Greenwalt7b816022014-04-18 15:25:25 -0700386 }
387
388 /**
389 * Called by the bearer code when it has a new score for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -0700390 */
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700391 public void sendNetworkScore(int score) {
Robert Greenwalt35f7a942014-09-09 14:46:37 -0700392 if (score < 0) {
393 throw new IllegalArgumentException("Score must be >= 0");
394 }
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700395 queueOrSendMessage(EVENT_NETWORK_SCORE_CHANGED, new Integer(score));
Robert Greenwalt7b816022014-04-18 15:25:25 -0700396 }
397
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700398 /**
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400399 * Called by the VPN code when it wants to add ranges of UIDs to be routed
400 * through the VPN network.
401 */
402 public void addUidRanges(UidRange[] ranges) {
403 queueOrSendMessage(EVENT_UID_RANGES_ADDED, ranges);
404 }
405
406 /**
407 * Called by the VPN code when it wants to remove ranges of UIDs from being routed
408 * through the VPN network.
409 */
410 public void removeUidRanges(UidRange[] ranges) {
411 queueOrSendMessage(EVENT_UID_RANGES_REMOVED, ranges);
412 }
413
414 /**
Robert Greenwalte73cc462014-09-07 16:50:01 -0700415 * Called by the bearer to indicate this network was manually selected by the user.
416 * This should be called before the NetworkInfo is marked CONNECTED so that this
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900417 * Network can be given special treatment at that time. If {@code acceptUnvalidated} is
418 * {@code true}, then the system will switch to this network. If it is {@code false} and the
419 * network cannot be validated, the system will ask the user whether to switch to this network.
420 * If the user confirms and selects "don't ask again", then the system will call
421 * {@link #saveAcceptUnvalidated} to persist the user's choice. Thus, if the transport ever
422 * calls this method with {@code acceptUnvalidated} set to {@code false}, it must also implement
423 * {@link #saveAcceptUnvalidated} to respect the user's choice.
Robert Greenwalte73cc462014-09-07 16:50:01 -0700424 */
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900425 public void explicitlySelected(boolean acceptUnvalidated) {
426 queueOrSendMessage(EVENT_SET_EXPLICITLY_SELECTED, acceptUnvalidated);
Robert Greenwalte73cc462014-09-07 16:50:01 -0700427 }
428
429 /**
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700430 * Called when ConnectivityService has indicated they no longer want this network.
431 * The parent factory should (previously) have received indication of the change
432 * as well, either canceling NetworkRequests or altering their score such that this
433 * network won't be immediately requested again.
434 */
435 abstract protected void unwanted();
Robert Greenwalt7b816022014-04-18 15:25:25 -0700436
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700437 /**
fenglub15e72b2015-03-20 11:29:56 -0700438 * Called when ConnectivityService request a bandwidth update. The parent factory
439 * shall try to overwrite this method and produce a bandwidth update if capable.
440 */
441 protected void pollLceData() {
442 }
443
444 /**
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700445 * Called when the system determines the usefulness of this network.
446 *
447 * Networks claiming internet connectivity will have their internet
448 * connectivity verified.
449 *
450 * Currently there are two possible values:
451 * {@code VALID_NETWORK} if the system is happy with the connection,
452 * {@code INVALID_NETWORK} if the system is not happy.
453 * TODO - add indications of captive portal-ness and related success/failure,
454 * ie, CAPTIVE_SUCCESS_NETWORK, CAPTIVE_NETWORK for successful login and detection
455 *
456 * This may be called multiple times as the network status changes and may
457 * generate false negatives if we lose ip connectivity before the link is torn down.
458 */
459 protected void networkStatus(int status) {
460 }
461
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900462 /**
463 * Called when the user asks to remember the choice to use this network even if unvalidated.
464 * The transport is responsible for remembering the choice, and the next time the user connects
465 * to the network, should explicitlySelected with {@code acceptUnvalidated} set to {@code true}.
466 * This method will only be called if {@link #explicitlySelected} was called with
467 * {@code acceptUnvalidated} set to {@code false}.
468 */
469 protected void saveAcceptUnvalidated(boolean accept) {
470 }
471
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +0900472 /**
473 * Requests that the network hardware send the specified packet at the specified interval.
474 */
475 protected void startPacketKeepalive(Message msg) {
476 onPacketKeepaliveEvent(msg.arg1, PacketKeepalive.ERROR_HARDWARE_UNSUPPORTED);
477 }
478
479 /**
480 * Requests that the network hardware send the specified packet at the specified interval.
481 */
482 protected void stopPacketKeepalive(Message msg) {
483 onPacketKeepaliveEvent(msg.arg1, PacketKeepalive.ERROR_HARDWARE_UNSUPPORTED);
484 }
485
486 /**
487 * Called by the network when a packet keepalive event occurs.
488 */
489 public void onPacketKeepaliveEvent(int slot, int reason) {
490 queueOrSendMessage(EVENT_PACKET_KEEPALIVE, slot, reason);
491 }
492
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900493 /**
494 * Called by ConnectivityService to inform this network transport of signal strength thresholds
495 * that when crossed should trigger a system wakeup and a NetworkCapabilities update.
496 */
497 protected void setSignalStrengthThresholds(int[] thresholds) {
498 }
499
Paul Jensenbf109912015-07-29 11:31:53 -0400500 /**
Paul Jensenf95d2202015-07-13 15:19:51 -0400501 * Called when the user asks to not stay connected to this network because it was found to not
502 * provide Internet access. Usually followed by call to {@code unwanted}. The transport is
503 * responsible for making sure the device does not automatically reconnect to the same network
504 * after the {@code unwanted} call.
505 */
506 protected void preventAutomaticReconnect() {
507 }
508
Paul Jensen578a76e2016-01-14 14:54:39 -0500509 /**
510 * Install a packet filter.
511 * @param filter an APF program to filter incoming packets.
512 * @return {@code true} if filter successfully installed, {@code false} otherwise.
513 */
514 protected boolean installPacketFilter(byte[] filter) {
515 return false;
516 }
517
Robert Greenwalt7b816022014-04-18 15:25:25 -0700518 protected void log(String s) {
519 Log.d(LOG_TAG, "NetworkAgent: " + s);
520 }
521}