blob: 204c25f01d01bf4fbce3702c2356c769c69bbd22 [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
Mathew Inwood53f089f2018-08-08 14:44:44 +010019import android.annotation.UnsupportedAppUsage;
Robert Greenwalt7b816022014-04-18 15:25:25 -070020import android.content.Context;
Chalard Jeance1a9d82018-01-23 21:25:37 +090021import android.net.ConnectivityManager.PacketKeepalive;
Mathew Inwood31755f92018-12-20 13:53:36 +000022import android.os.Build;
Erik Kline9d598e12015-07-13 16:37:51 +090023import android.os.Bundle;
Robert Greenwalt7b816022014-04-18 15:25:25 -070024import android.os.Handler;
25import android.os.Looper;
26import android.os.Message;
27import android.os.Messenger;
Robert Greenwalt7b816022014-04-18 15:25:25 -070028import android.util.Log;
Robert Greenwalt7b816022014-04-18 15:25:25 -070029
30import com.android.internal.util.AsyncChannel;
31import com.android.internal.util.Protocol;
32
Robert Greenwalt3192dec2014-05-27 13:20:24 -070033import java.util.ArrayList;
fenglu95ce8032015-05-01 17:04:36 -070034import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt7b816022014-04-18 15:25:25 -070035
36/**
Robert Greenwalt3192dec2014-05-27 13:20:24 -070037 * A Utility class for handling for communicating between bearer-specific
38 * code and ConnectivityService.
Robert Greenwalt7b816022014-04-18 15:25:25 -070039 *
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 Greenwalt3192dec2014-05-27 13:20:24 -070042 * perhaps connections with different SSID or P2P for Wi-Fi).
Robert Greenwalt7b816022014-04-18 15:25:25 -070043 *
Robert Greenwalt7b816022014-04-18 15:25:25 -070044 * @hide
45 */
46public abstract class NetworkAgent extends Handler {
Paul Jensen31a94f42015-02-13 14:18:39 -050047 // Guaranteed to be valid (not NETID_UNSET), otherwise registerNetworkAgent() would have thrown
48 // an exception.
49 public final int netId;
50
Robert Greenwalt3192dec2014-05-27 13:20:24 -070051 private volatile AsyncChannel mAsyncChannel;
Robert Greenwalt7b816022014-04-18 15:25:25 -070052 private final String LOG_TAG;
53 private static final boolean DBG = true;
Robert Greenwaltfc0c6892014-08-27 14:34:02 -070054 private static final boolean VDBG = false;
Robert Greenwalt7b816022014-04-18 15:25:25 -070055 private final Context mContext;
Robert Greenwalt3192dec2014-05-27 13:20:24 -070056 private final ArrayList<Message>mPreConnectedQueue = new ArrayList<Message>();
fenglub15e72b2015-03-20 11:29:56 -070057 private volatile long mLastBwRefreshTime = 0;
58 private static final long BW_REFRESH_MIN_WIN_MS = 500;
fenglu95ce8032015-05-01 17:04:36 -070059 private boolean mPollLceScheduled = false;
60 private AtomicBoolean mPollLcePending = new AtomicBoolean(false);
Chalard Jean08577fc2018-05-02 21:14:54 +090061 public final int mFactorySerialNumber;
Robert Greenwalt7b816022014-04-18 15:25:25 -070062
63 private static final int BASE = Protocol.BASE_NETWORK_AGENT;
64
65 /**
Robert Greenwalt7b816022014-04-18 15:25:25 -070066 * 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 Greenwalt3192dec2014-05-27 13:20:24 -070070 public static final int CMD_SUSPECT_BAD = BASE;
Robert Greenwalt7b816022014-04-18 15:25:25 -070071
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 Greenwalt3192dec2014-05-27 13:20:24 -070078 public static final int EVENT_NETWORK_INFO_CHANGED = BASE + 1;
Robert Greenwalt7b816022014-04-18 15:25:25 -070079
80 /**
81 * Sent by the NetworkAgent to ConnectivityService to pass the current
82 * NetworkCapabilties.
83 * obj = NetworkCapabilities
84 */
Robert Greenwalt3192dec2014-05-27 13:20:24 -070085 public static final int EVENT_NETWORK_CAPABILITIES_CHANGED = BASE + 2;
Robert Greenwalt7b816022014-04-18 15:25:25 -070086
87 /**
88 * Sent by the NetworkAgent to ConnectivityService to pass the current
89 * NetworkProperties.
90 * obj = NetworkProperties
91 */
Robert Greenwalt3192dec2014-05-27 13:20:24 -070092 public static final int EVENT_NETWORK_PROPERTIES_CHANGED = BASE + 3;
Robert Greenwalt7b816022014-04-18 15:25:25 -070093
vandwalle2ab90892014-06-02 15:30:39 -070094 /* 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 Greenwalt7b816022014-04-18 15:25:25 -070099 /**
100 * Sent by the NetworkAgent to ConnectivityService to pass the current
101 * network score.
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700102 * obj = network score Integer
Robert Greenwalt7b816022014-04-18 15:25:25 -0700103 */
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700104 public static final int EVENT_NETWORK_SCORE_CHANGED = BASE + 4;
Robert Greenwalt7b816022014-04-18 15:25:25 -0700105
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400106 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900107 * Sent by ConnectivityService to the NetworkAgent to inform the agent of the
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700108 * 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 Jensen232437312016-04-06 09:51:26 -0400112 * 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 Greenwalt49f63fb2014-09-13 12:04:12 -0700115 */
Lorenzo Colitti60446162014-09-20 00:14:31 +0900116 public static final int CMD_REPORT_NETWORK_STATUS = BASE + 7;
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700117
118 public static final int VALID_NETWORK = 1;
119 public static final int INVALID_NETWORK = 2;
120
Paul Jensen232437312016-04-06 09:51:26 -0400121 public static String REDIRECT_URL_KEY = "redirect URL";
122
Robert Greenwalte73cc462014-09-07 16:50:01 -0700123 /**
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 Colittie03c3c72015-04-03 16:38:52 +0900127 *
128 * obj = boolean indicating whether to use this network even if unvalidated
Robert Greenwalte73cc462014-09-07 16:50:01 -0700129 */
Lorenzo Colitti60446162014-09-20 00:14:31 +0900130 public static final int EVENT_SET_EXPLICITLY_SELECTED = BASE + 8;
Robert Greenwalte73cc462014-09-07 16:50:01 -0700131
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900132 /**
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 Colitti8bf977d2015-06-15 14:29:22 +0900142 /**
143 * Sent by ConnectivityService to the NetworkAgent to inform the agent to pull
fenglub15e72b2015-03-20 11:29:56 -0700144 * the underlying network connection for updated bandwidth information.
145 */
146 public static final int CMD_REQUEST_BANDWIDTH_UPDATE = BASE + 10;
147
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +0900148 /**
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 */
158 public static final int CMD_START_PACKET_KEEPALIVE = BASE + 11;
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 */
167 public static final int CMD_STOP_PACKET_KEEPALIVE = BASE + 12;
168
169 /**
170 * Sent by the NetworkAgent to ConnectivityService to provide status on a packet keepalive
171 * request. This may either be the reply to a CMD_START_PACKET_KEEPALIVE, or an asynchronous
172 * error notification.
173 *
174 * This is also sent by KeepaliveTracker to the app's ConnectivityManager.PacketKeepalive to
175 * so that the app's PacketKeepaliveCallback methods can be called.
176 *
177 * arg1 = slot number of the keepalive
178 * arg2 = error code
179 */
180 public static final int EVENT_PACKET_KEEPALIVE = BASE + 13;
181
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900182 /**
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
Paul Jensenbf109912015-07-29 11:31:53 -0400190 /**
Paul Jensenf95d2202015-07-13 15:19:51 -0400191 * 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 */
Paul Jensenbf109912015-07-29 11:31:53 -0400195 public static final int CMD_PREVENT_AUTOMATIC_RECONNECT = BASE + 15;
Paul Jensenf95d2202015-07-13 15:19:51 -0400196
Chalard Jean08577fc2018-05-02 21:14:54 +0900197 // TODO : remove these two constructors. They are a stopgap measure to help sheperding a number
198 // of dependent changes that would conflict throughout the automerger graph. Having these
199 // temporarily helps with the process of going through with all these dependent changes across
200 // the entire tree.
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700201 public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
202 NetworkCapabilities nc, LinkProperties lp, int score) {
Chalard Jean08577fc2018-05-02 21:14:54 +0900203 this(looper, context, logTag, ni, nc, lp, score, null, NetworkFactory.SerialNumber.NONE);
204 }
205 public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
206 NetworkCapabilities nc, LinkProperties lp, int score, NetworkMisc misc) {
207 this(looper, context, logTag, ni, nc, lp, score, misc, NetworkFactory.SerialNumber.NONE);
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -0700208 }
209
210 public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
Chalard Jean08577fc2018-05-02 21:14:54 +0900211 NetworkCapabilities nc, LinkProperties lp, int score, int factorySerialNumber) {
212 this(looper, context, logTag, ni, nc, lp, score, null, factorySerialNumber);
213 }
214
215 public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
216 NetworkCapabilities nc, LinkProperties lp, int score, NetworkMisc misc,
217 int factorySerialNumber) {
Robert Greenwalt7b816022014-04-18 15:25:25 -0700218 super(looper);
219 LOG_TAG = logTag;
220 mContext = context;
Chalard Jean08577fc2018-05-02 21:14:54 +0900221 mFactorySerialNumber = factorySerialNumber;
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700222 if (ni == null || nc == null || lp == null) {
223 throw new IllegalArgumentException();
Robert Greenwalt7b816022014-04-18 15:25:25 -0700224 }
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700225
Robert Greenwaltfc0c6892014-08-27 14:34:02 -0700226 if (VDBG) log("Registering NetworkAgent");
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700227 ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(
228 Context.CONNECTIVITY_SERVICE);
Paul Jensen31a94f42015-02-13 14:18:39 -0500229 netId = cm.registerNetworkAgent(new Messenger(this), new NetworkInfo(ni),
Chalard Jean08577fc2018-05-02 21:14:54 +0900230 new LinkProperties(lp), new NetworkCapabilities(nc), score, misc,
231 factorySerialNumber);
Robert Greenwalt7b816022014-04-18 15:25:25 -0700232 }
233
234 @Override
235 public void handleMessage(Message msg) {
236 switch (msg.what) {
237 case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700238 if (mAsyncChannel != null) {
239 log("Received new connection while already connected!");
240 } else {
Robert Greenwaltfc0c6892014-08-27 14:34:02 -0700241 if (VDBG) log("NetworkAgent fully connected");
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700242 AsyncChannel ac = new AsyncChannel();
243 ac.connected(null, this, msg.replyTo);
244 ac.replyToMessage(msg, AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED,
245 AsyncChannel.STATUS_SUCCESSFUL);
246 synchronized (mPreConnectedQueue) {
247 mAsyncChannel = ac;
248 for (Message m : mPreConnectedQueue) {
249 ac.sendMessage(m);
250 }
251 mPreConnectedQueue.clear();
Robert Greenwalt7b816022014-04-18 15:25:25 -0700252 }
253 }
254 break;
255 }
256 case AsyncChannel.CMD_CHANNEL_DISCONNECT: {
Robert Greenwaltfc0c6892014-08-27 14:34:02 -0700257 if (VDBG) log("CMD_CHANNEL_DISCONNECT");
Robert Greenwalt7b816022014-04-18 15:25:25 -0700258 if (mAsyncChannel != null) mAsyncChannel.disconnect();
259 break;
260 }
261 case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
262 if (DBG) log("NetworkAgent channel lost");
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700263 // let the client know CS is done with us.
264 unwanted();
265 synchronized (mPreConnectedQueue) {
266 mAsyncChannel = null;
267 }
Robert Greenwalt7b816022014-04-18 15:25:25 -0700268 break;
269 }
270 case CMD_SUSPECT_BAD: {
271 log("Unhandled Message " + msg);
272 break;
273 }
fenglub15e72b2015-03-20 11:29:56 -0700274 case CMD_REQUEST_BANDWIDTH_UPDATE: {
fenglu95ce8032015-05-01 17:04:36 -0700275 long currentTimeMs = System.currentTimeMillis();
fenglub15e72b2015-03-20 11:29:56 -0700276 if (VDBG) {
277 log("CMD_REQUEST_BANDWIDTH_UPDATE request received.");
278 }
fenglu95ce8032015-05-01 17:04:36 -0700279 if (currentTimeMs >= (mLastBwRefreshTime + BW_REFRESH_MIN_WIN_MS)) {
280 mPollLceScheduled = false;
281 if (mPollLcePending.getAndSet(true) == false) {
282 pollLceData();
283 }
284 } else {
285 // deliver the request at a later time rather than discard it completely.
286 if (!mPollLceScheduled) {
287 long waitTime = mLastBwRefreshTime + BW_REFRESH_MIN_WIN_MS -
288 currentTimeMs + 1;
289 mPollLceScheduled = sendEmptyMessageDelayed(
290 CMD_REQUEST_BANDWIDTH_UPDATE, waitTime);
291 }
fenglub15e72b2015-03-20 11:29:56 -0700292 }
293 break;
294 }
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700295 case CMD_REPORT_NETWORK_STATUS: {
Paul Jensen232437312016-04-06 09:51:26 -0400296 String redirectUrl = ((Bundle)msg.obj).getString(REDIRECT_URL_KEY);
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700297 if (VDBG) {
298 log("CMD_REPORT_NETWORK_STATUS(" +
Paul Jensen232437312016-04-06 09:51:26 -0400299 (msg.arg1 == VALID_NETWORK ? "VALID, " : "INVALID, ") + redirectUrl);
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700300 }
Paul Jensen232437312016-04-06 09:51:26 -0400301 networkStatus(msg.arg1, redirectUrl);
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700302 break;
303 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900304 case CMD_SAVE_ACCEPT_UNVALIDATED: {
305 saveAcceptUnvalidated(msg.arg1 != 0);
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +0900306 break;
307 }
308 case CMD_START_PACKET_KEEPALIVE: {
309 startPacketKeepalive(msg);
310 break;
311 }
312 case CMD_STOP_PACKET_KEEPALIVE: {
313 stopPacketKeepalive(msg);
314 break;
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900315 }
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900316
317 case CMD_SET_SIGNAL_STRENGTH_THRESHOLDS: {
Erik Kline9d598e12015-07-13 16:37:51 +0900318 ArrayList<Integer> thresholds =
319 ((Bundle) msg.obj).getIntegerArrayList("thresholds");
320 // TODO: Change signal strength thresholds API to use an ArrayList<Integer>
321 // rather than convert to int[].
322 int[] intThresholds = new int[(thresholds != null) ? thresholds.size() : 0];
323 for (int i = 0; i < intThresholds.length; i++) {
324 intThresholds[i] = thresholds.get(i);
325 }
326 setSignalStrengthThresholds(intThresholds);
Lorenzo Colittic3f21f32015-07-06 23:50:27 +0900327 break;
328 }
Paul Jensenf95d2202015-07-13 15:19:51 -0400329 case CMD_PREVENT_AUTOMATIC_RECONNECT: {
330 preventAutomaticReconnect();
331 break;
Robert Greenwalt7b816022014-04-18 15:25:25 -0700332 }
Robert Greenwalt7b816022014-04-18 15:25:25 -0700333 }
334 }
335
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700336 private void queueOrSendMessage(int what, Object obj) {
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +0900337 queueOrSendMessage(what, 0, 0, obj);
338 }
339
340 private void queueOrSendMessage(int what, int arg1, int arg2) {
341 queueOrSendMessage(what, arg1, arg2, null);
342 }
343
344 private void queueOrSendMessage(int what, int arg1, int arg2, Object obj) {
345 Message msg = Message.obtain();
346 msg.what = what;
347 msg.arg1 = arg1;
348 msg.arg2 = arg2;
349 msg.obj = obj;
350 queueOrSendMessage(msg);
351 }
352
353 private void queueOrSendMessage(Message msg) {
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700354 synchronized (mPreConnectedQueue) {
355 if (mAsyncChannel != null) {
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +0900356 mAsyncChannel.sendMessage(msg);
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700357 } else {
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700358 mPreConnectedQueue.add(msg);
Lorenzo Colitti9a6a11a2014-05-24 02:25:55 +0900359 }
Robert Greenwalt7b816022014-04-18 15:25:25 -0700360 }
361 }
362
Robert Greenwalt7b816022014-04-18 15:25:25 -0700363 /**
364 * Called by the bearer code when it has new LinkProperties data.
Robert Greenwalt7b816022014-04-18 15:25:25 -0700365 */
366 public void sendLinkProperties(LinkProperties linkProperties) {
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700367 queueOrSendMessage(EVENT_NETWORK_PROPERTIES_CHANGED, new LinkProperties(linkProperties));
Robert Greenwalt7b816022014-04-18 15:25:25 -0700368 }
369
370 /**
371 * Called by the bearer code when it has new NetworkInfo data.
Robert Greenwalt7b816022014-04-18 15:25:25 -0700372 */
Mathew Inwood31755f92018-12-20 13:53:36 +0000373 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Robert Greenwalt7b816022014-04-18 15:25:25 -0700374 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 }
Roshan Pius64e99ef2018-08-02 10:25:02 -0700395 queueOrSendMessage(EVENT_NETWORK_SCORE_CHANGED, score, 0);
Robert Greenwalt7b816022014-04-18 15:25:25 -0700396 }
397
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700398 /**
Robert Greenwalte73cc462014-09-07 16:50:01 -0700399 * Called by the bearer to indicate this network was manually selected by the user.
400 * This should be called before the NetworkInfo is marked CONNECTED so that this
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900401 * Network can be given special treatment at that time. If {@code acceptUnvalidated} is
402 * {@code true}, then the system will switch to this network. If it is {@code false} and the
403 * network cannot be validated, the system will ask the user whether to switch to this network.
404 * If the user confirms and selects "don't ask again", then the system will call
405 * {@link #saveAcceptUnvalidated} to persist the user's choice. Thus, if the transport ever
406 * calls this method with {@code acceptUnvalidated} set to {@code false}, it must also implement
407 * {@link #saveAcceptUnvalidated} to respect the user's choice.
Robert Greenwalte73cc462014-09-07 16:50:01 -0700408 */
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900409 public void explicitlySelected(boolean acceptUnvalidated) {
Roshan Piuseaf8dee2018-08-10 07:36:39 -0700410 queueOrSendMessage(EVENT_SET_EXPLICITLY_SELECTED, acceptUnvalidated ? 1 : 0, 0);
Robert Greenwalte73cc462014-09-07 16:50:01 -0700411 }
412
413 /**
Robert Greenwalt3192dec2014-05-27 13:20:24 -0700414 * Called when ConnectivityService has indicated they no longer want this network.
415 * The parent factory should (previously) have received indication of the change
416 * as well, either canceling NetworkRequests or altering their score such that this
417 * network won't be immediately requested again.
418 */
419 abstract protected void unwanted();
Robert Greenwalt7b816022014-04-18 15:25:25 -0700420
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700421 /**
fenglub15e72b2015-03-20 11:29:56 -0700422 * Called when ConnectivityService request a bandwidth update. The parent factory
423 * shall try to overwrite this method and produce a bandwidth update if capable.
424 */
425 protected void pollLceData() {
426 }
427
428 /**
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700429 * Called when the system determines the usefulness of this network.
430 *
431 * Networks claiming internet connectivity will have their internet
432 * connectivity verified.
433 *
434 * Currently there are two possible values:
435 * {@code VALID_NETWORK} if the system is happy with the connection,
436 * {@code INVALID_NETWORK} if the system is not happy.
437 * TODO - add indications of captive portal-ness and related success/failure,
438 * ie, CAPTIVE_SUCCESS_NETWORK, CAPTIVE_NETWORK for successful login and detection
439 *
440 * This may be called multiple times as the network status changes and may
441 * generate false negatives if we lose ip connectivity before the link is torn down.
Paul Jensen232437312016-04-06 09:51:26 -0400442 *
443 * @param status one of {@code VALID_NETWORK} or {@code INVALID_NETWORK}.
444 * @param redirectUrl If the Internet probe was redirected, this is the destination it was
445 * redirected to, otherwise {@code null}.
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700446 */
Paul Jensen232437312016-04-06 09:51:26 -0400447 protected void networkStatus(int status, String redirectUrl) {
Robert Greenwalt49f63fb2014-09-13 12:04:12 -0700448 }
449
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900450 /**
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 Colitti8bf977d2015-06-15 14:29:22 +0900460 /**
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 Colittic3f21f32015-07-06 23:50:27 +0900481 /**
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 Jensenbf109912015-07-29 11:31:53 -0400488 /**
Paul Jensenf95d2202015-07-13 15:19:51 -0400489 * 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 Greenwalt7b816022014-04-18 15:25:25 -0700497 protected void log(String s) {
498 Log.d(LOG_TAG, "NetworkAgent: " + s);
499 }
500}