The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 16 | package android.net; |
| 17 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 18 | import static com.android.internal.util.Preconditions.checkNotNull; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 19 | |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 20 | import android.annotation.IntDef; |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 21 | import android.annotation.Nullable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.annotation.SdkConstant; |
| 23 | import android.annotation.SdkConstant.SdkConstantType; |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 24 | import android.annotation.SystemApi; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 25 | import android.app.PendingIntent; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 27 | import android.content.Intent; |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 29 | import android.os.Binder; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 30 | import android.os.Build.VERSION_CODES; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 31 | import android.os.Bundle; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 32 | import android.os.Handler; |
| 33 | import android.os.HandlerThread; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 34 | import android.os.IBinder; |
| 35 | import android.os.INetworkActivityListener; |
| 36 | import android.os.INetworkManagementService; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 37 | import android.os.Looper; |
| 38 | import android.os.Message; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 39 | import android.os.Messenger; |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 40 | import android.os.Process; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.os.RemoteException; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 42 | import android.os.ResultReceiver; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 43 | import android.os.ServiceManager; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 44 | import android.provider.Settings; |
Wink Saville | 36ffb04 | 2014-12-05 11:10:30 -0800 | [diff] [blame] | 45 | import android.telephony.SubscriptionManager; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 46 | import android.util.ArrayMap; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 47 | import android.util.Log; |
Lorenzo Colitti | fcfa7d9 | 2016-03-01 22:56:37 +0900 | [diff] [blame] | 48 | import android.util.SparseArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 50 | import com.android.internal.telephony.ITelephony; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 51 | import com.android.internal.telephony.PhoneConstants; |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 52 | import com.android.internal.util.Protocol; |
Lorenzo Colitti | fcfa7d9 | 2016-03-01 22:56:37 +0900 | [diff] [blame] | 53 | import com.android.internal.util.MessageUtils; |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 54 | |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 55 | import libcore.net.event.NetworkEventDispatcher; |
| 56 | |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 57 | import java.lang.annotation.Retention; |
| 58 | import java.lang.annotation.RetentionPolicy; |
Jeremy Klein | d42209d | 2015-12-28 15:11:58 -0800 | [diff] [blame] | 59 | import java.net.InetAddress; |
| 60 | import java.util.HashMap; |
| 61 | import java.util.concurrent.atomic.AtomicInteger; |
| 62 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Class that answers queries about the state of network connectivity. It also |
| 65 | * notifies applications when network connectivity changes. Get an instance |
| 66 | * of this class by calling |
| 67 | * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}. |
| 68 | * <p> |
| 69 | * The primary responsibilities of this class are to: |
| 70 | * <ol> |
| 71 | * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li> |
| 72 | * <li>Send broadcast intents when network connectivity changes</li> |
| 73 | * <li>Attempt to "fail over" to another network when connectivity to a network |
| 74 | * is lost</li> |
| 75 | * <li>Provide an API that allows applications to query the coarse-grained or fine-grained |
| 76 | * state of the available networks</li> |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 77 | * <li>Provide an API that allows applications to request and select networks for their data |
| 78 | * traffic</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | * </ol> |
| 80 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 81 | public class ConnectivityManager { |
| 82 | private static final String TAG = "ConnectivityManager"; |
| 83 | |
Lorenzo Colitti | fcfa7d9 | 2016-03-01 22:56:37 +0900 | [diff] [blame] | 84 | private static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames( |
| 85 | new Class[]{ConnectivityManager.class}, new String[]{"CALLBACK_"}); |
| 86 | |
| 87 | private static final String whatToString(int what) { |
| 88 | return sMagicDecoderRing.get(what, Integer.toString(what)); |
| 89 | } |
| 90 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 92 | * A change in network connectivity has occurred. A default connection has either |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | * been established or lost. The NetworkInfo for the affected network is |
| 94 | * sent as an extra; it should be consulted to see what kind of |
| 95 | * connectivity event occurred. |
| 96 | * <p/> |
| 97 | * If this is a connection that was the result of failing over from a |
| 98 | * disconnected network, then the FAILOVER_CONNECTION boolean extra is |
| 99 | * set to true. |
| 100 | * <p/> |
| 101 | * For a loss of connectivity, if the connectivity manager is attempting |
| 102 | * to connect (or has already connected) to another network, the |
| 103 | * NetworkInfo for the new network is also passed as an extra. This lets |
| 104 | * any receivers of the broadcast know that they should not necessarily |
| 105 | * tell the user that no data traffic will be possible. Instead, the |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 106 | * receiver should expect another broadcast soon, indicating either that |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | * the failover attempt succeeded (and so there is still overall data |
| 108 | * connectivity), or that the failover attempt failed, meaning that all |
| 109 | * connectivity has been lost. |
| 110 | * <p/> |
| 111 | * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY |
| 112 | * is set to {@code true} if there are no connected networks at all. |
| 113 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 114 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 116 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | /** |
Robert Greenwalt | e94a6ff | 2015-09-01 08:23:04 -0700 | [diff] [blame] | 118 | * A temporary hack until SUPL system can get off the legacy APIS. |
| 119 | * They do too many network requests and the long list of apps listening |
| 120 | * and waking due to the CONNECTIVITY_ACTION bcast makes it expensive. |
| 121 | * Use this bcast intent instead for SUPL requests. |
| 122 | * @hide |
| 123 | */ |
| 124 | public static final String CONNECTIVITY_ACTION_SUPL = |
| 125 | "android.net.conn.CONNECTIVITY_CHANGE_SUPL"; |
| 126 | |
| 127 | /** |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 128 | * The device has connected to a network that has presented a captive |
| 129 | * portal, which is blocking Internet connectivity. The user was presented |
| 130 | * with a notification that network sign in is required, |
| 131 | * and the user invoked the notification's action indicating they |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 132 | * desire to sign in to the network. Apps handling this activity should |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 133 | * facilitate signing in to the network. This action includes a |
| 134 | * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents |
| 135 | * the network presenting the captive portal; all communication with the |
| 136 | * captive portal must be done using this {@code Network} object. |
| 137 | * <p/> |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 138 | * This activity includes a {@link CaptivePortal} extra named |
| 139 | * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different |
| 140 | * outcomes of the captive portal sign in to the system: |
| 141 | * <ul> |
| 142 | * <li> When the app handling this action believes the user has signed in to |
| 143 | * the network and the captive portal has been dismissed, the app should |
| 144 | * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can |
| 145 | * reevaluate the network. If reevaluation finds the network no longer |
| 146 | * subject to a captive portal, the network may become the default active |
| 147 | * data network. </li> |
| 148 | * <li> When the app handling this action believes the user explicitly wants |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 149 | * to ignore the captive portal and the network, the app should call |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 150 | * {@link CaptivePortal#ignoreNetwork}. </li> |
| 151 | * </ul> |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 152 | */ |
| 153 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 154 | public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL"; |
| 155 | |
| 156 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | * The lookup key for a {@link NetworkInfo} object. Retrieve with |
| 158 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 159 | * |
| 160 | * @deprecated Since {@link NetworkInfo} can vary based on UID, applications |
| 161 | * should always obtain network information through |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 162 | * {@link #getActiveNetworkInfo()}. |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 163 | * @see #EXTRA_NETWORK_TYPE |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | */ |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 165 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | public static final String EXTRA_NETWORK_INFO = "networkInfo"; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 167 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | /** |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 169 | * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast. |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 170 | * |
| 171 | * @see android.content.Intent#getIntExtra(String, int) |
| 172 | */ |
| 173 | public static final String EXTRA_NETWORK_TYPE = "networkType"; |
| 174 | |
| 175 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | * The lookup key for a boolean that indicates whether a connect event |
| 177 | * is for a network to which the connectivity manager was failing over |
| 178 | * following a disconnect on another network. |
| 179 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 180 | */ |
| 181 | public static final String EXTRA_IS_FAILOVER = "isFailover"; |
| 182 | /** |
| 183 | * The lookup key for a {@link NetworkInfo} object. This is supplied when |
| 184 | * there is another network that it may be possible to connect to. Retrieve with |
| 185 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 186 | */ |
| 187 | public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork"; |
| 188 | /** |
| 189 | * The lookup key for a boolean that indicates whether there is a |
| 190 | * complete lack of connectivity, i.e., no network is available. |
| 191 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 192 | */ |
| 193 | public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity"; |
| 194 | /** |
| 195 | * The lookup key for a string that indicates why an attempt to connect |
| 196 | * to a network failed. The string has no particular structure. It is |
| 197 | * intended to be used in notifications presented to users. Retrieve |
| 198 | * it with {@link android.content.Intent#getStringExtra(String)}. |
| 199 | */ |
| 200 | public static final String EXTRA_REASON = "reason"; |
| 201 | /** |
| 202 | * The lookup key for a string that provides optionally supplied |
| 203 | * extra information about the network state. The information |
| 204 | * may be passed up from the lower networking layers, and its |
| 205 | * meaning may be specific to a particular network type. Retrieve |
| 206 | * it with {@link android.content.Intent#getStringExtra(String)}. |
| 207 | */ |
| 208 | public static final String EXTRA_EXTRA_INFO = "extraInfo"; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 209 | /** |
| 210 | * The lookup key for an int that provides information about |
| 211 | * our connection to the internet at large. 0 indicates no connection, |
| 212 | * 100 indicates a great connection. Retrieve it with |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 213 | * {@link android.content.Intent#getIntExtra(String, int)}. |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 214 | * {@hide} |
| 215 | */ |
| 216 | public static final String EXTRA_INET_CONDITION = "inetCondition"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | /** |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 218 | * The lookup key for a {@link CaptivePortal} object included with the |
| 219 | * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal} |
| 220 | * object can be used to either indicate to the system that the captive |
| 221 | * portal has been dismissed or that the user does not want to pursue |
| 222 | * signing in to captive portal. Retrieve it with |
| 223 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 224 | */ |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 225 | public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL"; |
Jan Nordqvist | 52eb29f | 2015-09-22 15:54:32 -0700 | [diff] [blame] | 226 | |
| 227 | /** |
| 228 | * Key for passing a URL to the captive portal login activity. |
| 229 | */ |
| 230 | public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL"; |
| 231 | |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 232 | /** |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 233 | * Broadcast action to indicate the change of data activity status |
| 234 | * (idle or active) on a network in a recent period. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 235 | * The network becomes active when data transmission is started, or |
| 236 | * idle if there is no data transmission for a period of time. |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 237 | * {@hide} |
| 238 | */ |
| 239 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 240 | public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE"; |
| 241 | /** |
| 242 | * The lookup key for an enum that indicates the network device type on which this data activity |
| 243 | * change happens. |
| 244 | * {@hide} |
| 245 | */ |
| 246 | public static final String EXTRA_DEVICE_TYPE = "deviceType"; |
| 247 | /** |
| 248 | * The lookup key for a boolean that indicates the device is active or not. {@code true} means |
| 249 | * it is actively sending or receiving data and {@code false} means it is idle. |
| 250 | * {@hide} |
| 251 | */ |
| 252 | public static final String EXTRA_IS_ACTIVE = "isActive"; |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 253 | /** |
| 254 | * The lookup key for a long that contains the timestamp (nanos) of the radio state change. |
| 255 | * {@hide} |
| 256 | */ |
| 257 | public static final String EXTRA_REALTIME_NS = "tsNanos"; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 258 | |
| 259 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | * Broadcast Action: The setting for background data usage has changed |
| 261 | * values. Use {@link #getBackgroundDataSetting()} to get the current value. |
| 262 | * <p> |
| 263 | * If an application uses the network in the background, it should listen |
| 264 | * for this broadcast and stop using the background data if the value is |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 265 | * {@code false}. |
Jeff Sharkey | 54ee2ad | 2012-01-30 16:29:24 -0800 | [diff] [blame] | 266 | * <p> |
| 267 | * |
| 268 | * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability |
| 269 | * of background data depends on several combined factors, and |
| 270 | * this broadcast is no longer sent. Instead, when background |
| 271 | * data is unavailable, {@link #getActiveNetworkInfo()} will now |
| 272 | * appear disconnected. During first boot after a platform |
| 273 | * upgrade, this broadcast will be sent once if |
| 274 | * {@link #getBackgroundDataSetting()} was {@code false} before |
| 275 | * the upgrade. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | */ |
| 277 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Jeff Sharkey | 54ee2ad | 2012-01-30 16:29:24 -0800 | [diff] [blame] | 278 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED = |
| 280 | "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"; |
| 281 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 282 | /** |
| 283 | * Broadcast Action: The network connection may not be good |
| 284 | * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and |
| 285 | * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify |
| 286 | * the network and it's condition. |
| 287 | * @hide |
| 288 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 289 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 290 | public static final String INET_CONDITION_ACTION = |
| 291 | "android.net.conn.INET_CONDITION_ACTION"; |
| 292 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 293 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 294 | * Broadcast Action: A tetherable connection has come or gone. |
| 295 | * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER}, |
| 296 | * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and |
| 297 | * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate |
| 298 | * the current state of tethering. Each include a list of |
| 299 | * interface names in that state (may be empty). |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 300 | * @hide |
| 301 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 302 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 303 | public static final String ACTION_TETHER_STATE_CHANGED = |
| 304 | "android.net.conn.TETHER_STATE_CHANGED"; |
| 305 | |
| 306 | /** |
| 307 | * @hide |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 308 | * gives a String[] listing all the interfaces configured for |
| 309 | * tethering and currently available for tethering. |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 310 | */ |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 311 | public static final String EXTRA_AVAILABLE_TETHER = "availableArray"; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 312 | |
| 313 | /** |
| 314 | * @hide |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 315 | * gives a String[] listing all the interfaces currently tethered |
| 316 | * (ie, has dhcp support and packets potentially forwarded/NATed) |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 317 | */ |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 318 | public static final String EXTRA_ACTIVE_TETHER = "activeArray"; |
| 319 | |
| 320 | /** |
| 321 | * @hide |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 322 | * gives a String[] listing all the interfaces we tried to tether and |
| 323 | * failed. Use {@link #getLastTetherError} to find the error code |
| 324 | * for any interfaces listed here. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 325 | */ |
| 326 | public static final String EXTRA_ERRORED_TETHER = "erroredArray"; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 327 | |
| 328 | /** |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 329 | * Broadcast Action: The captive portal tracker has finished its test. |
| 330 | * Sent only while running Setup Wizard, in lieu of showing a user |
| 331 | * notification. |
| 332 | * @hide |
| 333 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 334 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 335 | public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED = |
| 336 | "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED"; |
| 337 | /** |
| 338 | * The lookup key for a boolean that indicates whether a captive portal was detected. |
| 339 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 340 | * @hide |
| 341 | */ |
| 342 | public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal"; |
| 343 | |
| 344 | /** |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 345 | * Action used to display a dialog that asks the user whether to connect to a network that is |
| 346 | * not validated. This intent is used to start the dialog in settings via startActivity. |
| 347 | * |
| 348 | * @hide |
| 349 | */ |
| 350 | public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED"; |
| 351 | |
| 352 | /** |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 353 | * Invalid tethering type. |
| 354 | * @see #startTethering(int, OnStartTetheringCallback, boolean) |
| 355 | * @hide |
| 356 | */ |
| 357 | public static final int TETHERING_INVALID = -1; |
| 358 | |
| 359 | /** |
| 360 | * Wifi tethering type. |
| 361 | * @see #startTethering(int, OnStartTetheringCallback, boolean) |
| 362 | * @hide |
| 363 | */ |
| 364 | @SystemApi |
| 365 | public static final int TETHERING_WIFI = 0; |
| 366 | |
| 367 | /** |
| 368 | * USB tethering type. |
| 369 | * @see #startTethering(int, OnStartTetheringCallback, boolean) |
| 370 | * @hide |
| 371 | */ |
| 372 | @SystemApi |
| 373 | public static final int TETHERING_USB = 1; |
| 374 | |
| 375 | /** |
| 376 | * Bluetooth tethering type. |
| 377 | * @see #startTethering(int, OnStartTetheringCallback, boolean) |
| 378 | * @hide |
| 379 | */ |
| 380 | @SystemApi |
| 381 | public static final int TETHERING_BLUETOOTH = 2; |
| 382 | |
| 383 | /** |
| 384 | * Extra used for communicating with the TetherService. Includes the type of tethering to |
| 385 | * enable if any. |
| 386 | * @hide |
| 387 | */ |
| 388 | public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType"; |
| 389 | |
| 390 | /** |
| 391 | * Extra used for communicating with the TetherService. Includes the type of tethering for |
| 392 | * which to cancel provisioning. |
| 393 | * @hide |
| 394 | */ |
| 395 | public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType"; |
| 396 | |
| 397 | /** |
| 398 | * Extra used for communicating with the TetherService. True to schedule a recheck of tether |
| 399 | * provisioning. |
| 400 | * @hide |
| 401 | */ |
| 402 | public static final String EXTRA_SET_ALARM = "extraSetAlarm"; |
| 403 | |
| 404 | /** |
| 405 | * Tells the TetherService to run a provision check now. |
| 406 | * @hide |
| 407 | */ |
| 408 | public static final String EXTRA_RUN_PROVISION = "extraRunProvision"; |
| 409 | |
| 410 | /** |
| 411 | * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver} |
| 412 | * which will receive provisioning results. Can be left empty. |
| 413 | * @hide |
| 414 | */ |
| 415 | public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback"; |
| 416 | |
| 417 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 418 | * The absence of a connection type. |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 419 | * @hide |
| 420 | */ |
| 421 | public static final int TYPE_NONE = -1; |
| 422 | |
| 423 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 424 | * The Mobile data connection. When active, all data traffic |
| 425 | * will use this network type's interface by default |
| 426 | * (it has a default route) |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 427 | */ |
| 428 | public static final int TYPE_MOBILE = 0; |
| 429 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 430 | * The WIFI data connection. When active, all data traffic |
| 431 | * will use this network type's interface by default |
| 432 | * (it has a default route). |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 433 | */ |
| 434 | public static final int TYPE_WIFI = 1; |
| 435 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 436 | * An MMS-specific Mobile data connection. This network type may use the |
| 437 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 438 | * one. This is used by applications needing to talk to the carrier's |
| 439 | * Multimedia Messaging Service servers. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 440 | * |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 441 | * @deprecated Applications should instead use |
| 442 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 443 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 444 | */ |
| 445 | public static final int TYPE_MOBILE_MMS = 2; |
| 446 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 447 | * A SUPL-specific Mobile data connection. This network type may use the |
| 448 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 449 | * one. This is used by applications needing to talk to the carrier's |
| 450 | * Secure User Plane Location servers for help locating the device. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 451 | * |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 452 | * @deprecated Applications should instead use |
| 453 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 454 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 455 | */ |
| 456 | public static final int TYPE_MOBILE_SUPL = 3; |
| 457 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 458 | * A DUN-specific Mobile data connection. This network type may use the |
| 459 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 460 | * one. This is sometimes by the system when setting up an upstream connection |
| 461 | * for tethering so that the carrier is aware of DUN traffic. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 462 | */ |
| 463 | public static final int TYPE_MOBILE_DUN = 4; |
| 464 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 465 | * A High Priority Mobile data connection. This network type uses the |
| 466 | * same network interface as {@link #TYPE_MOBILE} but the routing setup |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 467 | * is different. |
| 468 | * |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 469 | * @deprecated Applications should instead use |
| 470 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 471 | * uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 472 | */ |
| 473 | public static final int TYPE_MOBILE_HIPRI = 5; |
jsh | 8214deb | 2010-03-11 15:04:43 -0800 | [diff] [blame] | 474 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 475 | * The WiMAX data connection. When active, all data traffic |
| 476 | * will use this network type's interface by default |
| 477 | * (it has a default route). |
jsh | 8214deb | 2010-03-11 15:04:43 -0800 | [diff] [blame] | 478 | */ |
| 479 | public static final int TYPE_WIMAX = 6; |
Robert Greenwalt | da3d5e6 | 2010-12-06 13:56:24 -0800 | [diff] [blame] | 480 | |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 481 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 482 | * The Bluetooth data connection. When active, all data traffic |
| 483 | * will use this network type's interface by default |
| 484 | * (it has a default route). |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 485 | */ |
| 486 | public static final int TYPE_BLUETOOTH = 7; |
| 487 | |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 488 | /** |
| 489 | * Dummy data connection. This should not be used on shipping devices. |
| 490 | */ |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 491 | public static final int TYPE_DUMMY = 8; |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 492 | |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 493 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 494 | * The Ethernet data connection. When active, all data traffic |
| 495 | * will use this network type's interface by default |
| 496 | * (it has a default route). |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 497 | */ |
Robert Greenwalt | e12aec9 | 2011-01-28 14:48:37 -0800 | [diff] [blame] | 498 | public static final int TYPE_ETHERNET = 9; |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 499 | |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 500 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 501 | * Over the air Administration. |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 502 | * {@hide} |
| 503 | */ |
| 504 | public static final int TYPE_MOBILE_FOTA = 10; |
| 505 | |
| 506 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 507 | * IP Multimedia Subsystem. |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 508 | * {@hide} |
| 509 | */ |
| 510 | public static final int TYPE_MOBILE_IMS = 11; |
| 511 | |
| 512 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 513 | * Carrier Branded Services. |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 514 | * {@hide} |
| 515 | */ |
| 516 | public static final int TYPE_MOBILE_CBS = 12; |
| 517 | |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 518 | /** |
| 519 | * A Wi-Fi p2p connection. Only requesting processes will have access to |
| 520 | * the peers connected. |
| 521 | * {@hide} |
| 522 | */ |
| 523 | public static final int TYPE_WIFI_P2P = 13; |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 524 | |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 525 | /** |
| 526 | * The network to use for initially attaching to the network |
| 527 | * {@hide} |
| 528 | */ |
| 529 | public static final int TYPE_MOBILE_IA = 14; |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 530 | |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 531 | /** |
Robert Greenwalt | 4bd4389 | 2015-07-09 14:49:35 -0700 | [diff] [blame] | 532 | * Emergency PDN connection for emergency services. This |
| 533 | * may include IMS and MMS in emergency situations. |
Ram | 3e0e3bc | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 534 | * {@hide} |
| 535 | */ |
| 536 | public static final int TYPE_MOBILE_EMERGENCY = 15; |
| 537 | |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 538 | /** |
| 539 | * The network that uses proxy to achieve connectivity. |
| 540 | * {@hide} |
| 541 | */ |
| 542 | public static final int TYPE_PROXY = 16; |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 543 | |
Robert Greenwalt | 8283f88 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 544 | /** |
| 545 | * A virtual network using one or more native bearers. |
| 546 | * It may or may not be providing security services. |
| 547 | */ |
| 548 | public static final int TYPE_VPN = 17; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 549 | |
| 550 | /** {@hide} */ |
Robert Greenwalt | 8283f88 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 551 | public static final int MAX_RADIO_TYPE = TYPE_VPN; |
| 552 | |
| 553 | /** {@hide} */ |
| 554 | public static final int MAX_NETWORK_TYPE = TYPE_VPN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 556 | /** |
| 557 | * If you want to set the default network preference,you can directly |
| 558 | * change the networkAttributes array in framework's config.xml. |
| 559 | * |
| 560 | * @deprecated Since we support so many more networks now, the single |
| 561 | * network default network preference can't really express |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 562 | * the hierarchy. Instead, the default is defined by the |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 563 | * networkAttributes in config.xml. You can determine |
Robert Greenwalt | 4c8b748 | 2012-12-07 09:56:50 -0800 | [diff] [blame] | 564 | * the current value by calling {@link #getNetworkPreference()} |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 565 | * from an App. |
| 566 | */ |
| 567 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI; |
| 569 | |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 570 | /** |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 571 | * @hide |
| 572 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 573 | public final static int REQUEST_ID_UNSET = 0; |
| 574 | |
Paul Jensen | 5d59e78 | 2014-07-11 12:28:19 -0400 | [diff] [blame] | 575 | /** |
| 576 | * A NetID indicating no Network is selected. |
| 577 | * Keep in sync with bionic/libc/dns/include/resolv_netid.h |
| 578 | * @hide |
| 579 | */ |
| 580 | public static final int NETID_UNSET = 0; |
| 581 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 582 | private final IConnectivityManager mService; |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 583 | /** |
| 584 | * A kludge to facilitate static access where a Context pointer isn't available, like in the |
| 585 | * case of the static set/getProcessDefaultNetwork methods and from the Network class. |
| 586 | * TODO: Remove this after deprecating the static methods in favor of non-static methods or |
| 587 | * methods that take a Context argument. |
| 588 | */ |
| 589 | private static ConnectivityManager sInstance; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 591 | private final Context mContext; |
| 592 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 593 | private INetworkManagementService mNMService; |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 594 | private INetworkPolicyManager mNPManager; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 595 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 596 | /** |
| 597 | * Tests if a given integer represents a valid network type. |
| 598 | * @param networkType the type to be tested |
| 599 | * @return a boolean. {@code true} if the type is valid, else {@code false} |
Paul Jensen | 9e59e12 | 2015-05-06 10:42:25 -0400 | [diff] [blame] | 600 | * @deprecated All APIs accepting a network type are deprecated. There should be no need to |
| 601 | * validate a network type. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 602 | */ |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 603 | public static boolean isNetworkTypeValid(int networkType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 604 | return networkType >= 0 && networkType <= MAX_NETWORK_TYPE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | } |
| 606 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 607 | /** |
| 608 | * Returns a non-localized string representing a given network type. |
| 609 | * ONLY used for debugging output. |
| 610 | * @param type the type needing naming |
| 611 | * @return a String for the given type, or a string version of the type ("87") |
| 612 | * if no name is known. |
| 613 | * {@hide} |
| 614 | */ |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 615 | public static String getNetworkTypeName(int type) { |
| 616 | switch (type) { |
| 617 | case TYPE_MOBILE: |
| 618 | return "MOBILE"; |
| 619 | case TYPE_WIFI: |
| 620 | return "WIFI"; |
| 621 | case TYPE_MOBILE_MMS: |
| 622 | return "MOBILE_MMS"; |
| 623 | case TYPE_MOBILE_SUPL: |
| 624 | return "MOBILE_SUPL"; |
| 625 | case TYPE_MOBILE_DUN: |
| 626 | return "MOBILE_DUN"; |
| 627 | case TYPE_MOBILE_HIPRI: |
| 628 | return "MOBILE_HIPRI"; |
| 629 | case TYPE_WIMAX: |
| 630 | return "WIMAX"; |
| 631 | case TYPE_BLUETOOTH: |
| 632 | return "BLUETOOTH"; |
| 633 | case TYPE_DUMMY: |
| 634 | return "DUMMY"; |
| 635 | case TYPE_ETHERNET: |
| 636 | return "ETHERNET"; |
| 637 | case TYPE_MOBILE_FOTA: |
| 638 | return "MOBILE_FOTA"; |
| 639 | case TYPE_MOBILE_IMS: |
| 640 | return "MOBILE_IMS"; |
| 641 | case TYPE_MOBILE_CBS: |
| 642 | return "MOBILE_CBS"; |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 643 | case TYPE_WIFI_P2P: |
| 644 | return "WIFI_P2P"; |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 645 | case TYPE_MOBILE_IA: |
| 646 | return "MOBILE_IA"; |
Ram | 3e0e3bc | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 647 | case TYPE_MOBILE_EMERGENCY: |
| 648 | return "MOBILE_EMERGENCY"; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 649 | case TYPE_PROXY: |
| 650 | return "PROXY"; |
Erik Kline | 37fbfa1 | 2014-11-19 17:23:41 +0900 | [diff] [blame] | 651 | case TYPE_VPN: |
| 652 | return "VPN"; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 653 | default: |
| 654 | return Integer.toString(type); |
| 655 | } |
| 656 | } |
| 657 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 658 | /** |
| 659 | * Checks if a given type uses the cellular data connection. |
| 660 | * This should be replaced in the future by a network property. |
| 661 | * @param networkType the type to check |
| 662 | * @return a boolean - {@code true} if uses cellular network, else {@code false} |
| 663 | * {@hide} |
| 664 | */ |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 665 | public static boolean isNetworkTypeMobile(int networkType) { |
| 666 | switch (networkType) { |
| 667 | case TYPE_MOBILE: |
| 668 | case TYPE_MOBILE_MMS: |
| 669 | case TYPE_MOBILE_SUPL: |
| 670 | case TYPE_MOBILE_DUN: |
| 671 | case TYPE_MOBILE_HIPRI: |
| 672 | case TYPE_MOBILE_FOTA: |
| 673 | case TYPE_MOBILE_IMS: |
| 674 | case TYPE_MOBILE_CBS: |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 675 | case TYPE_MOBILE_IA: |
Ram | 3e0e3bc | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 676 | case TYPE_MOBILE_EMERGENCY: |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 677 | return true; |
| 678 | default: |
| 679 | return false; |
| 680 | } |
| 681 | } |
| 682 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 683 | /** |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 684 | * Checks if the given network type is backed by a Wi-Fi radio. |
| 685 | * |
| 686 | * @hide |
| 687 | */ |
| 688 | public static boolean isNetworkTypeWifi(int networkType) { |
| 689 | switch (networkType) { |
| 690 | case TYPE_WIFI: |
| 691 | case TYPE_WIFI_P2P: |
| 692 | return true; |
| 693 | default: |
| 694 | return false; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 699 | * Specifies the preferred network type. When the device has more |
| 700 | * than one type available the preferred network type will be used. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 701 | * |
| 702 | * @param preference the network type to prefer over all others. It is |
| 703 | * unspecified what happens to the old preferred network in the |
| 704 | * overall ordering. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 705 | * @deprecated Functionality has been removed as it no longer makes sense, |
| 706 | * with many more than two networks - we'd need an array to express |
| 707 | * preference. Instead we use dynamic network properties of |
| 708 | * the networks to describe their precedence. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 709 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | public void setNetworkPreference(int preference) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | } |
| 712 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 713 | /** |
| 714 | * Retrieves the current preferred network type. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 715 | * <p>This method requires the caller to hold the permission |
| 716 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 717 | * |
| 718 | * @return an integer representing the preferred network type |
| 719 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 720 | * @deprecated Functionality has been removed as it no longer makes sense, |
| 721 | * with many more than two networks - we'd need an array to express |
| 722 | * preference. Instead we use dynamic network properties of |
| 723 | * the networks to describe their precedence. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 724 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 725 | public int getNetworkPreference() { |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 726 | return TYPE_NONE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | } |
| 728 | |
Scott Main | 671644c | 2011-10-06 19:02:28 -0700 | [diff] [blame] | 729 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 730 | * Returns details about the currently active default data network. When |
| 731 | * connected, this network is the default route for outgoing connections. |
| 732 | * You should always check {@link NetworkInfo#isConnected()} before initiating |
| 733 | * network traffic. This may return {@code null} when there is no default |
| 734 | * network. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 735 | * <p>This method requires the caller to hold the permission |
| 736 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 737 | * |
| 738 | * @return a {@link NetworkInfo} object for the current default network |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 739 | * or {@code null} if no default network is currently active |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 740 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | public NetworkInfo getActiveNetworkInfo() { |
| 742 | try { |
| 743 | return mService.getActiveNetworkInfo(); |
| 744 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 745 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 749 | /** |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 750 | * Returns a {@link Network} object corresponding to the currently active |
| 751 | * default data network. In the event that the current active default data |
| 752 | * network disconnects, the returned {@code Network} object will no longer |
| 753 | * be usable. This will return {@code null} when there is no default |
| 754 | * network. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 755 | * <p>This method requires the caller to hold the permission |
| 756 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 757 | * |
| 758 | * @return a {@link Network} object for the current default network or |
| 759 | * {@code null} if no default network is currently active |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 760 | */ |
| 761 | public Network getActiveNetwork() { |
| 762 | try { |
| 763 | return mService.getActiveNetwork(); |
| 764 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 765 | throw e.rethrowFromSystemServer(); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
| 769 | /** |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 770 | * Configures an always-on VPN connection through a specific application. |
| 771 | * This connection is automatically granted and persisted after a reboot. |
| 772 | * |
| 773 | * <p>The designated package should declare a {@link VpnService} in its |
| 774 | * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE}, |
| 775 | * otherwise the call will fail. |
| 776 | * |
| 777 | * @param userId The identifier of the user to set an always-on VPN for. |
| 778 | * @param vpnPackage The package name for an installed VPN app on the device, or {@code null} |
| 779 | * to remove an existing always-on VPN configuration. |
| 780 | |
| 781 | * @return {@code true} if the package is set as always-on VPN controller; |
| 782 | * {@code false} otherwise. |
| 783 | * @hide |
| 784 | */ |
| 785 | public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage) { |
| 786 | try { |
| 787 | return mService.setAlwaysOnVpnPackage(userId, vpnPackage); |
| 788 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 789 | throw e.rethrowFromSystemServer(); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * Returns the package name of the currently set always-on VPN application. |
| 795 | * If there is no always-on VPN set, or the VPN is provided by the system instead |
| 796 | * of by an app, {@code null} will be returned. |
| 797 | * |
| 798 | * @return Package name of VPN controller responsible for always-on VPN, |
| 799 | * or {@code null} if none is set. |
| 800 | * @hide |
| 801 | */ |
| 802 | public String getAlwaysOnVpnPackageForUser(int userId) { |
| 803 | try { |
| 804 | return mService.getAlwaysOnVpnPackage(userId); |
| 805 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 806 | throw e.rethrowFromSystemServer(); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | |
| 810 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 811 | * Returns details about the currently active default data network |
| 812 | * for a given uid. This is for internal use only to avoid spying |
| 813 | * other apps. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 814 | * <p>This method requires the caller to hold the permission |
| 815 | * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL} |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 816 | * |
| 817 | * @return a {@link NetworkInfo} object for the current default network |
| 818 | * for the given uid or {@code null} if no default network is |
| 819 | * available for the specified uid. |
| 820 | * |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 821 | * {@hide} |
| 822 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 823 | public NetworkInfo getActiveNetworkInfoForUid(int uid) { |
| 824 | try { |
| 825 | return mService.getActiveNetworkInfoForUid(uid); |
| 826 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 827 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 831 | /** |
| 832 | * Returns connection status information about a particular |
| 833 | * network type. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 834 | * <p>This method requires the caller to hold the permission |
| 835 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 836 | * |
| 837 | * @param networkType integer specifying which networkType in |
| 838 | * which you're interested. |
| 839 | * @return a {@link NetworkInfo} object for the requested |
| 840 | * network type or {@code null} if the type is not |
| 841 | * supported by the device. |
| 842 | * |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 843 | * @deprecated This method does not support multiple connected networks |
| 844 | * of the same type. Use {@link #getAllNetworks} and |
| 845 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 846 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | public NetworkInfo getNetworkInfo(int networkType) { |
| 848 | try { |
| 849 | return mService.getNetworkInfo(networkType); |
| 850 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 851 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 855 | /** |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 856 | * Returns connection status information about a particular |
| 857 | * Network. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 858 | * <p>This method requires the caller to hold the permission |
| 859 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 860 | * |
| 861 | * @param network {@link Network} specifying which network |
| 862 | * in which you're interested. |
| 863 | * @return a {@link NetworkInfo} object for the requested |
| 864 | * network or {@code null} if the {@code Network} |
| 865 | * is not valid. |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 866 | */ |
| 867 | public NetworkInfo getNetworkInfo(Network network) { |
| 868 | try { |
| 869 | return mService.getNetworkInfoForNetwork(network); |
| 870 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 871 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
| 875 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 876 | * Returns connection status information about all network |
| 877 | * types supported by the device. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 878 | * <p>This method requires the caller to hold the permission |
| 879 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 880 | * |
| 881 | * @return an array of {@link NetworkInfo} objects. Check each |
| 882 | * {@link NetworkInfo#getType} for which type each applies. |
| 883 | * |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 884 | * @deprecated This method does not support multiple connected networks |
| 885 | * of the same type. Use {@link #getAllNetworks} and |
| 886 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 887 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | public NetworkInfo[] getAllNetworkInfo() { |
| 889 | try { |
| 890 | return mService.getAllNetworkInfo(); |
| 891 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 892 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | } |
| 894 | } |
| 895 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 896 | /** |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 897 | * Returns the {@link Network} object currently serving a given type, or |
| 898 | * null if the given type is not connected. |
| 899 | * |
| 900 | * <p>This method requires the caller to hold the permission |
| 901 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
| 902 | * |
| 903 | * @hide |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 904 | * @deprecated This method does not support multiple connected networks |
| 905 | * of the same type. Use {@link #getAllNetworks} and |
| 906 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 907 | */ |
| 908 | public Network getNetworkForType(int networkType) { |
| 909 | try { |
| 910 | return mService.getNetworkForType(networkType); |
| 911 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 912 | throw e.rethrowFromSystemServer(); |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
| 916 | /** |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 917 | * Returns an array of all {@link Network} currently tracked by the |
| 918 | * framework. |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 919 | * <p>This method requires the caller to hold the permission |
| 920 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 921 | * |
| 922 | * @return an array of {@link Network} objects. |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 923 | */ |
| 924 | public Network[] getAllNetworks() { |
| 925 | try { |
| 926 | return mService.getAllNetworks(); |
| 927 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 928 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | |
| 932 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 933 | * Returns an array of {@link android.net.NetworkCapabilities} objects, representing |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 934 | * the Networks that applications run by the given user will use by default. |
| 935 | * @hide |
| 936 | */ |
| 937 | public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) { |
| 938 | try { |
| 939 | return mService.getDefaultNetworkCapabilitiesForUser(userId); |
| 940 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 941 | throw e.rethrowFromSystemServer(); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | |
| 945 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 946 | * Returns the IP information for the current default network. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 947 | * <p>This method requires the caller to hold the permission |
| 948 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 949 | * |
| 950 | * @return a {@link LinkProperties} object describing the IP info |
| 951 | * for the current default network, or {@code null} if there |
| 952 | * is no current default network. |
| 953 | * |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 954 | * {@hide} |
| 955 | */ |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 956 | public LinkProperties getActiveLinkProperties() { |
| 957 | try { |
| 958 | return mService.getActiveLinkProperties(); |
| 959 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 960 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 964 | /** |
| 965 | * Returns the IP information for a given network type. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 966 | * <p>This method requires the caller to hold the permission |
| 967 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 968 | * |
| 969 | * @param networkType the network type of interest. |
| 970 | * @return a {@link LinkProperties} object describing the IP info |
| 971 | * for the given networkType, or {@code null} if there is |
| 972 | * no current default network. |
| 973 | * |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 974 | * {@hide} |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 975 | * @deprecated This method does not support multiple connected networks |
| 976 | * of the same type. Use {@link #getAllNetworks}, |
| 977 | * {@link #getNetworkInfo(android.net.Network)}, and |
| 978 | * {@link #getLinkProperties(android.net.Network)} instead. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 979 | */ |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 980 | public LinkProperties getLinkProperties(int networkType) { |
| 981 | try { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 982 | return mService.getLinkPropertiesForType(networkType); |
| 983 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 984 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 988 | /** |
| 989 | * Get the {@link LinkProperties} for the given {@link Network}. This |
| 990 | * will return {@code null} if the network is unknown. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 991 | * <p>This method requires the caller to hold the permission |
| 992 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 993 | * |
| 994 | * @param network The {@link Network} object identifying the network in question. |
| 995 | * @return The {@link LinkProperties} for the network, or {@code null}. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 996 | */ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 997 | public LinkProperties getLinkProperties(Network network) { |
| 998 | try { |
| 999 | return mService.getLinkProperties(network); |
| 1000 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1001 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1005 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 1006 | * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1007 | * will return {@code null} if the network is unknown. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1008 | * <p>This method requires the caller to hold the permission |
| 1009 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1010 | * |
| 1011 | * @param network The {@link Network} object identifying the network in question. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 1012 | * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1013 | */ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1014 | public NetworkCapabilities getNetworkCapabilities(Network network) { |
| 1015 | try { |
| 1016 | return mService.getNetworkCapabilities(network); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1017 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1018 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1022 | /** |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1023 | * Gets the URL that should be used for resolving whether a captive portal is present. |
| 1024 | * 1. This URL should respond with a 204 response to a GET request to indicate no captive |
| 1025 | * portal is present. |
| 1026 | * 2. This URL must be HTTP as redirect responses are used to find captive portal |
| 1027 | * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects. |
| 1028 | * |
| 1029 | * @hide |
| 1030 | */ |
| 1031 | @SystemApi |
| 1032 | public String getCaptivePortalServerUrl() { |
| 1033 | try { |
| 1034 | return mService.getCaptivePortalServerUrl(); |
| 1035 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1036 | throw e.rethrowFromSystemServer(); |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | * Tells the underlying networking system that the caller wants to |
| 1042 | * begin using the named feature. The interpretation of {@code feature} |
| 1043 | * is completely up to each networking implementation. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1044 | * |
| 1045 | * <p>This method requires the caller to hold either the |
| 1046 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 1047 | * or the ability to modify system settings as determined by |
| 1048 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 1049 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1050 | * @param networkType specifies which network the request pertains to |
| 1051 | * @param feature the name of the feature to be used |
| 1052 | * @return an integer value representing the outcome of the request. |
| 1053 | * The interpretation of this value is specific to each networking |
| 1054 | * implementation+feature combination, except that the value {@code -1} |
| 1055 | * always indicates failure. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1056 | * |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 1057 | * @deprecated Deprecated in favor of the cleaner |
| 1058 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API. |
Dianne Hackborn | 692a244 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 1059 | * In {@link VERSION_CODES#M}, and above, this method is unsupported and will |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1060 | * throw {@code UnsupportedOperationException} if called. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1061 | */ |
| 1062 | public int startUsingNetworkFeature(int networkType, String feature) { |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1063 | checkLegacyRoutingApiAccess(); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1064 | NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); |
| 1065 | if (netCap == null) { |
| 1066 | Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " + |
| 1067 | feature); |
| 1068 | return PhoneConstants.APN_REQUEST_FAILED; |
| 1069 | } |
| 1070 | |
| 1071 | NetworkRequest request = null; |
| 1072 | synchronized (sLegacyRequests) { |
| 1073 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1074 | if (l != null) { |
| 1075 | Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest); |
| 1076 | renewRequestLocked(l); |
| 1077 | if (l.currentNetwork != null) { |
| 1078 | return PhoneConstants.APN_ALREADY_ACTIVE; |
| 1079 | } else { |
| 1080 | return PhoneConstants.APN_REQUEST_STARTED; |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | request = requestNetworkForFeatureLocked(netCap); |
| 1085 | } |
| 1086 | if (request != null) { |
Robert Greenwalt | 257ee5f | 2014-06-20 10:58:45 -0700 | [diff] [blame] | 1087 | Log.d(TAG, "starting startUsingNetworkFeature for request " + request); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1088 | return PhoneConstants.APN_REQUEST_STARTED; |
| 1089 | } else { |
| 1090 | Log.d(TAG, " request Failed"); |
| 1091 | return PhoneConstants.APN_REQUEST_FAILED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | /** |
| 1096 | * Tells the underlying networking system that the caller is finished |
| 1097 | * using the named feature. The interpretation of {@code feature} |
| 1098 | * is completely up to each networking implementation. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1099 | * |
| 1100 | * <p>This method requires the caller to hold either the |
| 1101 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 1102 | * or the ability to modify system settings as determined by |
| 1103 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 1104 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | * @param networkType specifies which network the request pertains to |
| 1106 | * @param feature the name of the feature that is no longer needed |
| 1107 | * @return an integer value representing the outcome of the request. |
| 1108 | * The interpretation of this value is specific to each networking |
| 1109 | * implementation+feature combination, except that the value {@code -1} |
| 1110 | * always indicates failure. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1111 | * |
Robert Greenwalt | a36c074 | 2015-07-28 11:41:31 -0700 | [diff] [blame] | 1112 | * @deprecated Deprecated in favor of the cleaner {@link #unregisterNetworkCallback} API. |
Dianne Hackborn | 692a244 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 1113 | * In {@link VERSION_CODES#M}, and above, this method is unsupported and will |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1114 | * throw {@code UnsupportedOperationException} if called. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1115 | */ |
| 1116 | public int stopUsingNetworkFeature(int networkType, String feature) { |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1117 | checkLegacyRoutingApiAccess(); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1118 | NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); |
| 1119 | if (netCap == null) { |
| 1120 | Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " + |
| 1121 | feature); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1122 | return -1; |
| 1123 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1124 | |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1125 | if (removeRequestForFeature(netCap)) { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1126 | Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1127 | } |
| 1128 | return 1; |
| 1129 | } |
| 1130 | |
| 1131 | private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) { |
| 1132 | if (networkType == TYPE_MOBILE) { |
| 1133 | int cap = -1; |
| 1134 | if ("enableMMS".equals(feature)) { |
| 1135 | cap = NetworkCapabilities.NET_CAPABILITY_MMS; |
| 1136 | } else if ("enableSUPL".equals(feature)) { |
| 1137 | cap = NetworkCapabilities.NET_CAPABILITY_SUPL; |
| 1138 | } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) { |
| 1139 | cap = NetworkCapabilities.NET_CAPABILITY_DUN; |
| 1140 | } else if ("enableHIPRI".equals(feature)) { |
| 1141 | cap = NetworkCapabilities.NET_CAPABILITY_INTERNET; |
| 1142 | } else if ("enableFOTA".equals(feature)) { |
| 1143 | cap = NetworkCapabilities.NET_CAPABILITY_FOTA; |
| 1144 | } else if ("enableIMS".equals(feature)) { |
| 1145 | cap = NetworkCapabilities.NET_CAPABILITY_IMS; |
| 1146 | } else if ("enableCBS".equals(feature)) { |
| 1147 | cap = NetworkCapabilities.NET_CAPABILITY_CBS; |
| 1148 | } else { |
| 1149 | return null; |
| 1150 | } |
| 1151 | NetworkCapabilities netCap = new NetworkCapabilities(); |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 1152 | netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap); |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 1153 | netCap.maybeMarkCapabilitiesRestricted(); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1154 | return netCap; |
| 1155 | } else if (networkType == TYPE_WIFI) { |
| 1156 | if ("p2p".equals(feature)) { |
| 1157 | NetworkCapabilities netCap = new NetworkCapabilities(); |
| 1158 | netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 1159 | netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P); |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 1160 | netCap.maybeMarkCapabilitiesRestricted(); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1161 | return netCap; |
| 1162 | } |
| 1163 | } |
| 1164 | return null; |
| 1165 | } |
| 1166 | |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1167 | /** |
| 1168 | * Guess what the network request was trying to say so that the resulting |
| 1169 | * network is accessible via the legacy (deprecated) API such as |
| 1170 | * requestRouteToHost. |
Lorenzo Colitti | def4cb0 | 2015-11-25 20:28:50 +0900 | [diff] [blame] | 1171 | * |
| 1172 | * This means we should try to be fairly precise about transport and |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1173 | * capability but ignore things such as networkSpecifier. |
| 1174 | * If the request has more than one transport or capability it doesn't |
| 1175 | * match the old legacy requests (they selected only single transport/capability) |
| 1176 | * so this function cannot map the request to a single legacy type and |
| 1177 | * the resulting network will not be available to the legacy APIs. |
| 1178 | * |
Lorenzo Colitti | def4cb0 | 2015-11-25 20:28:50 +0900 | [diff] [blame] | 1179 | * This code is only called from the requestNetwork API (L and above). |
| 1180 | * |
| 1181 | * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive |
| 1182 | * because they wake up lots of apps - see http://b/23350688 . So we currently only |
| 1183 | * do this for SUPL requests, which are the only ones that we know need it. If |
| 1184 | * omitting these broadcasts causes unacceptable app breakage, then for backwards |
| 1185 | * compatibility we can send them: |
| 1186 | * |
| 1187 | * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M |
| 1188 | * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L |
| 1189 | * |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1190 | * TODO - This should be removed when the legacy APIs are removed. |
| 1191 | */ |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1192 | private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) { |
| 1193 | if (netCap == null) { |
| 1194 | return TYPE_NONE; |
| 1195 | } |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1196 | |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1197 | if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { |
| 1198 | return TYPE_NONE; |
| 1199 | } |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1200 | |
Lifu Tang | 30f95a7 | 2016-01-07 23:20:38 -0800 | [diff] [blame] | 1201 | // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 . |
Lorenzo Colitti | def4cb0 | 2015-11-25 20:28:50 +0900 | [diff] [blame] | 1202 | if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) { |
| 1203 | // NOTE: if this causes app breakage, we should not just comment out this early return; |
| 1204 | // instead, we should make this early return conditional on the requesting app's target |
| 1205 | // SDK version, as described in the comment above. |
| 1206 | return TYPE_NONE; |
| 1207 | } |
| 1208 | |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1209 | String type = null; |
| 1210 | int result = TYPE_NONE; |
| 1211 | |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1212 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) { |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1213 | type = "enableCBS"; |
| 1214 | result = TYPE_MOBILE_CBS; |
| 1215 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) { |
| 1216 | type = "enableIMS"; |
| 1217 | result = TYPE_MOBILE_IMS; |
| 1218 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) { |
| 1219 | type = "enableFOTA"; |
| 1220 | result = TYPE_MOBILE_FOTA; |
| 1221 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) { |
| 1222 | type = "enableDUN"; |
| 1223 | result = TYPE_MOBILE_DUN; |
| 1224 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) { |
Lorenzo Colitti | def4cb0 | 2015-11-25 20:28:50 +0900 | [diff] [blame] | 1225 | type = "enableSUPL"; |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1226 | result = TYPE_MOBILE_SUPL; |
Robert Greenwalt | 74ab4fa | 2015-08-28 12:37:54 -0700 | [diff] [blame] | 1227 | // back out this hack for mms as they no longer need this and it's causing |
| 1228 | // device slowdowns - b/23350688 (note, supl still needs this) |
| 1229 | //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) { |
| 1230 | // type = "enableMMS"; |
| 1231 | // result = TYPE_MOBILE_MMS; |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1232 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { |
| 1233 | type = "enableHIPRI"; |
| 1234 | result = TYPE_MOBILE_HIPRI; |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1235 | } |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1236 | if (type != null) { |
| 1237 | NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type); |
| 1238 | if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) { |
| 1239 | return result; |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1240 | } |
| 1241 | } |
| 1242 | return TYPE_NONE; |
| 1243 | } |
| 1244 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1245 | private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1246 | if (netCap == null) return TYPE_NONE; |
| 1247 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) { |
| 1248 | return TYPE_MOBILE_CBS; |
| 1249 | } |
| 1250 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) { |
| 1251 | return TYPE_MOBILE_IMS; |
| 1252 | } |
| 1253 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) { |
| 1254 | return TYPE_MOBILE_FOTA; |
| 1255 | } |
| 1256 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) { |
| 1257 | return TYPE_MOBILE_DUN; |
| 1258 | } |
| 1259 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) { |
| 1260 | return TYPE_MOBILE_SUPL; |
| 1261 | } |
| 1262 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) { |
| 1263 | return TYPE_MOBILE_MMS; |
| 1264 | } |
| 1265 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { |
| 1266 | return TYPE_MOBILE_HIPRI; |
| 1267 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1268 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) { |
| 1269 | return TYPE_WIFI_P2P; |
| 1270 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1271 | return TYPE_NONE; |
| 1272 | } |
| 1273 | |
| 1274 | private static class LegacyRequest { |
| 1275 | NetworkCapabilities networkCapabilities; |
| 1276 | NetworkRequest networkRequest; |
| 1277 | int expireSequenceNumber; |
| 1278 | Network currentNetwork; |
| 1279 | int delay = -1; |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1280 | |
| 1281 | private void clearDnsBinding() { |
| 1282 | if (currentNetwork != null) { |
| 1283 | currentNetwork = null; |
| 1284 | setProcessDefaultNetworkForHostResolution(null); |
| 1285 | } |
| 1286 | } |
| 1287 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1288 | NetworkCallback networkCallback = new NetworkCallback() { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1289 | @Override |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1290 | public void onAvailable(Network network) { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1291 | currentNetwork = network; |
| 1292 | Log.d(TAG, "startUsingNetworkFeature got Network:" + network); |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 1293 | setProcessDefaultNetworkForHostResolution(network); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1294 | } |
| 1295 | @Override |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1296 | public void onLost(Network network) { |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1297 | if (network.equals(currentNetwork)) clearDnsBinding(); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1298 | Log.d(TAG, "startUsingNetworkFeature lost Network:" + network); |
| 1299 | } |
| 1300 | }; |
| 1301 | } |
| 1302 | |
Robert Greenwalt | fab50167 | 2014-07-23 11:44:01 -0700 | [diff] [blame] | 1303 | private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests = |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1304 | new HashMap<NetworkCapabilities, LegacyRequest>(); |
| 1305 | |
| 1306 | private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) { |
| 1307 | synchronized (sLegacyRequests) { |
| 1308 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1309 | if (l != null) return l.networkRequest; |
| 1310 | } |
| 1311 | return null; |
| 1312 | } |
| 1313 | |
| 1314 | private void renewRequestLocked(LegacyRequest l) { |
| 1315 | l.expireSequenceNumber++; |
| 1316 | Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber); |
| 1317 | sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay); |
| 1318 | } |
| 1319 | |
| 1320 | private void expireRequest(NetworkCapabilities netCap, int sequenceNum) { |
| 1321 | int ourSeqNum = -1; |
| 1322 | synchronized (sLegacyRequests) { |
| 1323 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1324 | if (l == null) return; |
| 1325 | ourSeqNum = l.expireSequenceNumber; |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1326 | if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1327 | } |
| 1328 | Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum); |
| 1329 | } |
| 1330 | |
| 1331 | private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) { |
| 1332 | int delay = -1; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1333 | int type = legacyTypeForNetworkCapabilities(netCap); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1334 | try { |
| 1335 | delay = mService.getRestoreDefaultNetworkDelay(type); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1336 | } catch (RemoteException e) { |
| 1337 | throw e.rethrowFromSystemServer(); |
| 1338 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1339 | LegacyRequest l = new LegacyRequest(); |
| 1340 | l.networkCapabilities = netCap; |
| 1341 | l.delay = delay; |
| 1342 | l.expireSequenceNumber = 0; |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1343 | l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0, |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1344 | REQUEST, type); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1345 | if (l.networkRequest == null) return null; |
| 1346 | sLegacyRequests.put(netCap, l); |
| 1347 | sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay); |
| 1348 | return l.networkRequest; |
| 1349 | } |
| 1350 | |
| 1351 | private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) { |
| 1352 | if (delay >= 0) { |
| 1353 | Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay); |
| 1354 | Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap); |
| 1355 | sCallbackHandler.sendMessageDelayed(msg, delay); |
| 1356 | } |
| 1357 | } |
| 1358 | |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1359 | private boolean removeRequestForFeature(NetworkCapabilities netCap) { |
| 1360 | final LegacyRequest l; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1361 | synchronized (sLegacyRequests) { |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1362 | l = sLegacyRequests.remove(netCap); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1363 | } |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1364 | if (l == null) return false; |
| 1365 | unregisterNetworkCallback(l.networkCallback); |
| 1366 | l.clearDnsBinding(); |
| 1367 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1368 | } |
| 1369 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1370 | /** @hide */ |
| 1371 | public static class PacketKeepaliveCallback { |
| 1372 | /** The requested keepalive was successfully started. */ |
| 1373 | public void onStarted() {} |
| 1374 | /** The keepalive was successfully stopped. */ |
| 1375 | public void onStopped() {} |
| 1376 | /** An error occurred. */ |
| 1377 | public void onError(int error) {} |
| 1378 | } |
| 1379 | |
| 1380 | /** |
| 1381 | * Allows applications to request that the system periodically send specific packets on their |
| 1382 | * behalf, using hardware offload to save battery power. |
| 1383 | * |
| 1384 | * To request that the system send keepalives, call one of the methods that return a |
| 1385 | * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive}, |
| 1386 | * passing in a non-null callback. If the callback is successfully started, the callback's |
| 1387 | * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called, |
| 1388 | * specifying one of the {@code ERROR_*} constants in this class. |
| 1389 | * |
| 1390 | * To stop an existing keepalive, call {@link stop}. The system will call {@code onStopped} if |
| 1391 | * the operation was successfull or {@code onError} if an error occurred. |
| 1392 | * |
| 1393 | * @hide |
| 1394 | */ |
| 1395 | public class PacketKeepalive { |
| 1396 | |
| 1397 | private static final String TAG = "PacketKeepalive"; |
| 1398 | |
| 1399 | /** @hide */ |
| 1400 | public static final int SUCCESS = 0; |
| 1401 | |
| 1402 | /** @hide */ |
| 1403 | public static final int NO_KEEPALIVE = -1; |
| 1404 | |
| 1405 | /** @hide */ |
| 1406 | public static final int BINDER_DIED = -10; |
| 1407 | |
| 1408 | /** The specified {@code Network} is not connected. */ |
| 1409 | public static final int ERROR_INVALID_NETWORK = -20; |
| 1410 | /** The specified IP addresses are invalid. For example, the specified source IP address is |
| 1411 | * not configured on the specified {@code Network}. */ |
| 1412 | public static final int ERROR_INVALID_IP_ADDRESS = -21; |
| 1413 | /** The requested port is invalid. */ |
| 1414 | public static final int ERROR_INVALID_PORT = -22; |
| 1415 | /** The packet length is invalid (e.g., too long). */ |
| 1416 | public static final int ERROR_INVALID_LENGTH = -23; |
| 1417 | /** The packet transmission interval is invalid (e.g., too short). */ |
| 1418 | public static final int ERROR_INVALID_INTERVAL = -24; |
| 1419 | |
| 1420 | /** The hardware does not support this request. */ |
| 1421 | public static final int ERROR_HARDWARE_UNSUPPORTED = -30; |
Lorenzo Colitti | 9d1284e | 2015-09-08 16:46:36 +0900 | [diff] [blame] | 1422 | /** The hardware returned an error. */ |
| 1423 | public static final int ERROR_HARDWARE_ERROR = -31; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1424 | |
| 1425 | public static final int NATT_PORT = 4500; |
| 1426 | |
| 1427 | private final Network mNetwork; |
| 1428 | private final PacketKeepaliveCallback mCallback; |
| 1429 | private final Looper mLooper; |
| 1430 | private final Messenger mMessenger; |
| 1431 | |
| 1432 | private volatile Integer mSlot; |
| 1433 | |
| 1434 | void stopLooper() { |
| 1435 | mLooper.quit(); |
| 1436 | } |
| 1437 | |
| 1438 | public void stop() { |
| 1439 | try { |
| 1440 | mService.stopKeepalive(mNetwork, mSlot); |
| 1441 | } catch (RemoteException e) { |
| 1442 | Log.e(TAG, "Error stopping packet keepalive: ", e); |
| 1443 | stopLooper(); |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | private PacketKeepalive(Network network, PacketKeepaliveCallback callback) { |
| 1448 | checkNotNull(network, "network cannot be null"); |
| 1449 | checkNotNull(callback, "callback cannot be null"); |
| 1450 | mNetwork = network; |
| 1451 | mCallback = callback; |
| 1452 | HandlerThread thread = new HandlerThread(TAG); |
| 1453 | thread.start(); |
| 1454 | mLooper = thread.getLooper(); |
| 1455 | mMessenger = new Messenger(new Handler(mLooper) { |
| 1456 | @Override |
| 1457 | public void handleMessage(Message message) { |
| 1458 | switch (message.what) { |
| 1459 | case NetworkAgent.EVENT_PACKET_KEEPALIVE: |
| 1460 | int error = message.arg2; |
| 1461 | try { |
| 1462 | if (error == SUCCESS) { |
| 1463 | if (mSlot == null) { |
| 1464 | mSlot = message.arg1; |
| 1465 | mCallback.onStarted(); |
| 1466 | } else { |
| 1467 | mSlot = null; |
| 1468 | stopLooper(); |
| 1469 | mCallback.onStopped(); |
| 1470 | } |
| 1471 | } else { |
| 1472 | stopLooper(); |
| 1473 | mCallback.onError(error); |
| 1474 | } |
| 1475 | } catch (Exception e) { |
| 1476 | Log.e(TAG, "Exception in keepalive callback(" + error + ")", e); |
| 1477 | } |
| 1478 | break; |
| 1479 | default: |
| 1480 | Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what)); |
| 1481 | break; |
| 1482 | } |
| 1483 | } |
| 1484 | }); |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | /** |
| 1489 | * Starts an IPsec NAT-T keepalive packet with the specified parameters. |
| 1490 | * |
| 1491 | * @hide |
| 1492 | */ |
| 1493 | public PacketKeepalive startNattKeepalive( |
| 1494 | Network network, int intervalSeconds, PacketKeepaliveCallback callback, |
| 1495 | InetAddress srcAddr, int srcPort, InetAddress dstAddr) { |
| 1496 | final PacketKeepalive k = new PacketKeepalive(network, callback); |
| 1497 | try { |
| 1498 | mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(), |
| 1499 | srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress()); |
| 1500 | } catch (RemoteException e) { |
| 1501 | Log.e(TAG, "Error starting packet keepalive: ", e); |
| 1502 | k.stopLooper(); |
| 1503 | return null; |
| 1504 | } |
| 1505 | return k; |
| 1506 | } |
| 1507 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1508 | /** |
| 1509 | * Ensure that a network route exists to deliver traffic to the specified |
| 1510 | * host via the specified network interface. An attempt to add a route that |
| 1511 | * already exists is ignored, but treated as successful. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1512 | * |
| 1513 | * <p>This method requires the caller to hold either the |
| 1514 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 1515 | * or the ability to modify system settings as determined by |
| 1516 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 1517 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1518 | * @param networkType the type of the network over which traffic to the specified |
| 1519 | * host is to be routed |
| 1520 | * @param hostAddress the IP address of the host to which the route is desired |
| 1521 | * @return {@code true} on success, {@code false} on failure |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1522 | * |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 1523 | * @deprecated Deprecated in favor of the |
| 1524 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, |
| 1525 | * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API. |
Dianne Hackborn | 692a244 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 1526 | * In {@link VERSION_CODES#M}, and above, this method is unsupported and will |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1527 | * throw {@code UnsupportedOperationException} if called. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1528 | */ |
| 1529 | public boolean requestRouteToHost(int networkType, int hostAddress) { |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1530 | return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress)); |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | /** |
| 1534 | * Ensure that a network route exists to deliver traffic to the specified |
| 1535 | * host via the specified network interface. An attempt to add a route that |
| 1536 | * already exists is ignored, but treated as successful. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1537 | * |
| 1538 | * <p>This method requires the caller to hold either the |
| 1539 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 1540 | * or the ability to modify system settings as determined by |
| 1541 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 1542 | * |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1543 | * @param networkType the type of the network over which traffic to the specified |
| 1544 | * host is to be routed |
| 1545 | * @param hostAddress the IP address of the host to which the route is desired |
| 1546 | * @return {@code true} on success, {@code false} on failure |
| 1547 | * @hide |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1548 | * @deprecated Deprecated in favor of the {@link #requestNetwork} and |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 1549 | * {@link #bindProcessToNetwork} API. |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1550 | */ |
| 1551 | public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) { |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1552 | checkLegacyRoutingApiAccess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1553 | try { |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1554 | return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1555 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1556 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | /** |
| 1561 | * Returns the value of the setting for background data usage. If false, |
| 1562 | * applications should not use the network if the application is not in the |
| 1563 | * foreground. Developers should respect this setting, and check the value |
| 1564 | * of this before performing any background data operations. |
| 1565 | * <p> |
| 1566 | * All applications that have background services that use the network |
| 1567 | * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}. |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1568 | * <p> |
Scott Main | 4cc5333 | 2011-10-06 18:32:43 -0700 | [diff] [blame] | 1569 | * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1570 | * background data depends on several combined factors, and this method will |
| 1571 | * always return {@code true}. Instead, when background data is unavailable, |
| 1572 | * {@link #getActiveNetworkInfo()} will now appear disconnected. |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 1573 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1574 | * @return Whether background data usage is allowed. |
| 1575 | */ |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1576 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1577 | public boolean getBackgroundDataSetting() { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1578 | // assume that background data is allowed; final authority is |
| 1579 | // NetworkInfo which may be blocked. |
| 1580 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | /** |
| 1584 | * Sets the value of the setting for background data usage. |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1585 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1586 | * @param allowBackgroundData Whether an application should use data while |
| 1587 | * it is in the background. |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1588 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1589 | * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING |
| 1590 | * @see #getBackgroundDataSetting() |
| 1591 | * @hide |
| 1592 | */ |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1593 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1594 | public void setBackgroundDataSetting(boolean allowBackgroundData) { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1595 | // ignored |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1596 | } |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1597 | |
| 1598 | /** |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1599 | * Return quota status for the current active network, or {@code null} if no |
| 1600 | * network is active. Quota status can change rapidly, so these values |
| 1601 | * shouldn't be cached. |
Jeff Sharkey | 44a3e0d | 2011-10-06 10:50:09 -0700 | [diff] [blame] | 1602 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1603 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1604 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
| 1605 | * |
Jeff Sharkey | 44a3e0d | 2011-10-06 10:50:09 -0700 | [diff] [blame] | 1606 | * @hide |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1607 | */ |
| 1608 | public NetworkQuotaInfo getActiveNetworkQuotaInfo() { |
| 1609 | try { |
| 1610 | return mService.getActiveNetworkQuotaInfo(); |
| 1611 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1612 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | /** |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1617 | * @hide |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 1618 | * @deprecated Talk to TelephonyManager directly |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1619 | */ |
| 1620 | public boolean getMobileDataEnabled() { |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 1621 | IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE); |
| 1622 | if (b != null) { |
| 1623 | try { |
| 1624 | ITelephony it = ITelephony.Stub.asInterface(b); |
Shishir Agrawal | 7ea3e8b | 2016-01-25 13:03:07 -0800 | [diff] [blame] | 1625 | int subId = SubscriptionManager.getDefaultDataSubscriptionId(); |
Wink Saville | 36ffb04 | 2014-12-05 11:10:30 -0800 | [diff] [blame] | 1626 | Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId); |
| 1627 | boolean retVal = it.getDataEnabled(subId); |
| 1628 | Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId |
| 1629 | + " retVal=" + retVal); |
| 1630 | return retVal; |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1631 | } catch (RemoteException e) { |
| 1632 | throw e.rethrowFromSystemServer(); |
| 1633 | } |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1634 | } |
Wink Saville | 36ffb04 | 2014-12-05 11:10:30 -0800 | [diff] [blame] | 1635 | Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false"); |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 1636 | return false; |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1637 | } |
| 1638 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1639 | /** |
Robert Greenwalt | b248987 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 1640 | * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener} |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1641 | * to find out when the system default network has gone in to a high power state. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1642 | */ |
| 1643 | public interface OnNetworkActiveListener { |
| 1644 | /** |
| 1645 | * Called on the main thread of the process to report that the current data network |
| 1646 | * has become active, and it is now a good time to perform any pending network |
| 1647 | * operations. Note that this listener only tells you when the network becomes |
| 1648 | * active; if at any other time you want to know whether it is active (and thus okay |
| 1649 | * to initiate network traffic), you can retrieve its instantaneous state with |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1650 | * {@link ConnectivityManager#isDefaultNetworkActive}. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1651 | */ |
| 1652 | public void onNetworkActive(); |
| 1653 | } |
| 1654 | |
| 1655 | private INetworkManagementService getNetworkManagementService() { |
| 1656 | synchronized (this) { |
| 1657 | if (mNMService != null) { |
| 1658 | return mNMService; |
| 1659 | } |
| 1660 | IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); |
| 1661 | mNMService = INetworkManagementService.Stub.asInterface(b); |
| 1662 | return mNMService; |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener> |
| 1667 | mNetworkActivityListeners |
| 1668 | = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>(); |
| 1669 | |
| 1670 | /** |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1671 | * Start listening to reports when the system's default data network is active, meaning it is |
| 1672 | * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()} |
| 1673 | * to determine the current state of the system's default network after registering the |
| 1674 | * listener. |
| 1675 | * <p> |
| 1676 | * If the process default network has been set with |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 1677 | * {@link ConnectivityManager#bindProcessToNetwork} this function will not |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1678 | * reflect the process's default, but the system default. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1679 | * |
| 1680 | * @param l The listener to be told when the network is active. |
| 1681 | */ |
Robert Greenwalt | b248987 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 1682 | public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1683 | INetworkActivityListener rl = new INetworkActivityListener.Stub() { |
| 1684 | @Override |
| 1685 | public void onNetworkActive() throws RemoteException { |
| 1686 | l.onNetworkActive(); |
| 1687 | } |
| 1688 | }; |
| 1689 | |
| 1690 | try { |
| 1691 | getNetworkManagementService().registerNetworkActivityListener(rl); |
| 1692 | mNetworkActivityListeners.put(l, rl); |
| 1693 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1694 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | /** |
| 1699 | * Remove network active listener previously registered with |
Robert Greenwalt | b248987 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 1700 | * {@link #addDefaultNetworkActiveListener}. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1701 | * |
| 1702 | * @param l Previously registered listener. |
| 1703 | */ |
Robert Greenwalt | b248987 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 1704 | public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1705 | INetworkActivityListener rl = mNetworkActivityListeners.get(l); |
| 1706 | if (rl == null) { |
| 1707 | throw new IllegalArgumentException("Listener not registered: " + l); |
| 1708 | } |
| 1709 | try { |
| 1710 | getNetworkManagementService().unregisterNetworkActivityListener(rl); |
| 1711 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1712 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | /** |
| 1717 | * Return whether the data network is currently active. An active network means that |
| 1718 | * it is currently in a high power state for performing data transmission. On some |
| 1719 | * types of networks, it may be expensive to move and stay in such a state, so it is |
| 1720 | * more power efficient to batch network traffic together when the radio is already in |
| 1721 | * this state. This method tells you whether right now is currently a good time to |
| 1722 | * initiate network traffic, as the network is already active. |
| 1723 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1724 | public boolean isDefaultNetworkActive() { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1725 | try { |
| 1726 | return getNetworkManagementService().isNetworkActive(); |
| 1727 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1728 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1729 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1733 | * {@hide} |
| 1734 | */ |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1735 | public ConnectivityManager(Context context, IConnectivityManager service) { |
| 1736 | mContext = checkNotNull(context, "missing context"); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1737 | mService = checkNotNull(service, "missing IConnectivityManager"); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1738 | sInstance = this; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1739 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1740 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1741 | /** {@hide} */ |
| 1742 | public static ConnectivityManager from(Context context) { |
| 1743 | return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 1744 | } |
| 1745 | |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1746 | /** {@hide} */ |
| 1747 | public static final void enforceChangePermission(Context context) { |
| 1748 | int uid = Binder.getCallingUid(); |
| 1749 | Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings |
| 1750 | .getPackageNameForUid(context, uid), true /* throwException */); |
| 1751 | } |
| 1752 | |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 1753 | /** {@hide */ |
| 1754 | public static final void enforceTetherChangePermission(Context context) { |
| 1755 | if (context.getResources().getStringArray( |
| 1756 | com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) { |
| 1757 | // Have a provisioning app - must only let system apps (which check this app) |
| 1758 | // turn on tethering |
| 1759 | context.enforceCallingOrSelfPermission( |
Jeremy Klein | d42209d | 2015-12-28 15:11:58 -0800 | [diff] [blame] | 1760 | android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService"); |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 1761 | } else { |
Billy Lau | a7238a3 | 2015-08-01 12:45:02 +0100 | [diff] [blame] | 1762 | int uid = Binder.getCallingUid(); |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1763 | Settings.checkAndNoteWriteSettingsOperation(context, uid, Settings |
| 1764 | .getPackageNameForUid(context, uid), true /* throwException */); |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 1765 | } |
| 1766 | } |
| 1767 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1768 | /** |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1769 | * @deprecated - use getSystemService. This is a kludge to support static access in certain |
| 1770 | * situations where a Context pointer is unavailable. |
| 1771 | * @hide |
| 1772 | */ |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 1773 | static ConnectivityManager getInstanceOrNull() { |
| 1774 | return sInstance; |
| 1775 | } |
| 1776 | |
| 1777 | /** |
| 1778 | * @deprecated - use getSystemService. This is a kludge to support static access in certain |
| 1779 | * situations where a Context pointer is unavailable. |
| 1780 | * @hide |
| 1781 | */ |
| 1782 | private static ConnectivityManager getInstance() { |
| 1783 | if (getInstanceOrNull() == null) { |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1784 | throw new IllegalStateException("No ConnectivityManager yet constructed"); |
| 1785 | } |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 1786 | return getInstanceOrNull(); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1790 | * Get the set of tetherable, available interfaces. This list is limited by |
| 1791 | * device configuration and current interface existence. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1792 | * <p>This method requires the caller to hold the permission |
| 1793 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1794 | * |
| 1795 | * @return an array of 0 or more Strings of tetherable interface names. |
| 1796 | * |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1797 | * {@hide} |
| 1798 | */ |
| 1799 | public String[] getTetherableIfaces() { |
| 1800 | try { |
| 1801 | return mService.getTetherableIfaces(); |
| 1802 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1803 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1808 | * Get the set of tethered interfaces. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1809 | * <p>This method requires the caller to hold the permission |
| 1810 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1811 | * |
| 1812 | * @return an array of 0 or more String of currently tethered interface names. |
| 1813 | * |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1814 | * {@hide} |
| 1815 | */ |
| 1816 | public String[] getTetheredIfaces() { |
| 1817 | try { |
| 1818 | return mService.getTetheredIfaces(); |
| 1819 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1820 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1821 | } |
| 1822 | } |
| 1823 | |
| 1824 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1825 | * Get the set of interface names which attempted to tether but |
| 1826 | * failed. Re-attempting to tether may cause them to reset to the Tethered |
| 1827 | * state. Alternatively, causing the interface to be destroyed and recreated |
| 1828 | * may cause them to reset to the available state. |
| 1829 | * {@link ConnectivityManager#getLastTetherError} can be used to get more |
| 1830 | * information on the cause of the errors. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1831 | * <p>This method requires the caller to hold the permission |
| 1832 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1833 | * |
| 1834 | * @return an array of 0 or more String indicating the interface names |
| 1835 | * which failed to tether. |
| 1836 | * |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1837 | * {@hide} |
| 1838 | */ |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1839 | public String[] getTetheringErroredIfaces() { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1840 | try { |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1841 | return mService.getTetheringErroredIfaces(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1842 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1843 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | /** |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 1848 | * Get the set of tethered dhcp ranges. |
| 1849 | * |
| 1850 | * @return an array of 0 or more {@code String} of tethered dhcp ranges. |
| 1851 | * {@hide} |
| 1852 | */ |
| 1853 | public String[] getTetheredDhcpRanges() { |
| 1854 | try { |
| 1855 | return mService.getTetheredDhcpRanges(); |
| 1856 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1857 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1862 | * Attempt to tether the named interface. This will setup a dhcp server |
| 1863 | * on the interface, forward and NAT IP packets and forward DNS requests |
| 1864 | * to the best active upstream network interface. Note that if no upstream |
| 1865 | * IP network interface is available, dhcp will still run and traffic will be |
| 1866 | * allowed between the tethered devices and this device, though upstream net |
| 1867 | * access will of course fail until an upstream network interface becomes |
| 1868 | * active. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1869 | * |
| 1870 | * <p>This method requires the caller to hold either the |
| 1871 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 1872 | * or the ability to modify system settings as determined by |
| 1873 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1874 | * |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 1875 | * <p>WARNING: New clients should not use this function. The only usages should be in PanService |
| 1876 | * and WifiStateMachine which need direct access. All other clients should use |
| 1877 | * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning |
| 1878 | * logic.</p> |
| 1879 | * |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1880 | * @param iface the interface name to tether. |
| 1881 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
| 1882 | * |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1883 | * {@hide} |
| 1884 | */ |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1885 | public int tether(String iface) { |
| 1886 | try { |
| 1887 | return mService.tether(iface); |
| 1888 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1889 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1894 | * Stop tethering the named interface. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1895 | * |
| 1896 | * <p>This method requires the caller to hold either the |
| 1897 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 1898 | * or the ability to modify system settings as determined by |
| 1899 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1900 | * |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 1901 | * <p>WARNING: New clients should not use this function. The only usages should be in PanService |
| 1902 | * and WifiStateMachine which need direct access. All other clients should use |
| 1903 | * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning |
| 1904 | * logic.</p> |
| 1905 | * |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1906 | * @param iface the interface name to untether. |
| 1907 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
| 1908 | * |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1909 | * {@hide} |
| 1910 | */ |
| 1911 | public int untether(String iface) { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1912 | try { |
| 1913 | return mService.untether(iface); |
| 1914 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1915 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1916 | } |
| 1917 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1918 | |
| 1919 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1920 | * Check if the device allows for tethering. It may be disabled via |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1921 | * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1922 | * due to device configuration. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1923 | * <p>This method requires the caller to hold the permission |
| 1924 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1925 | * |
| 1926 | * @return a boolean - {@code true} indicating Tethering is supported. |
| 1927 | * |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1928 | * {@hide} |
| 1929 | */ |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 1930 | @SystemApi |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1931 | public boolean isTetheringSupported() { |
| 1932 | try { |
| 1933 | return mService.isTetheringSupported(); |
| 1934 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1935 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1936 | } |
| 1937 | } |
| 1938 | |
| 1939 | /** |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 1940 | * Callback for use with {@link #startTethering} to find out whether tethering succeeded. |
| 1941 | * @hide |
| 1942 | */ |
| 1943 | @SystemApi |
| 1944 | public static abstract class OnStartTetheringCallback { |
| 1945 | /** |
| 1946 | * Called when tethering has been successfully started. |
| 1947 | */ |
| 1948 | public void onTetheringStarted() {}; |
| 1949 | |
| 1950 | /** |
| 1951 | * Called when starting tethering failed. |
| 1952 | */ |
| 1953 | public void onTetheringFailed() {}; |
| 1954 | } |
| 1955 | |
| 1956 | /** |
| 1957 | * Convenient overload for |
| 1958 | * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null |
| 1959 | * handler to run on the current thread's {@link Looper}. |
| 1960 | * @hide |
| 1961 | */ |
| 1962 | @SystemApi |
| 1963 | public void startTethering(int type, boolean showProvisioningUi, |
| 1964 | final OnStartTetheringCallback callback) { |
| 1965 | startTethering(type, showProvisioningUi, callback, null); |
| 1966 | } |
| 1967 | |
| 1968 | /** |
| 1969 | * Runs tether provisioning for the given type if needed and then starts tethering if |
| 1970 | * the check succeeds. If no carrier provisioning is required for tethering, tethering is |
| 1971 | * enabled immediately. If provisioning fails, tethering will not be enabled. It also |
| 1972 | * schedules tether provisioning re-checks if appropriate. |
| 1973 | * |
| 1974 | * @param type The type of tethering to start. Must be one of |
| 1975 | * {@link ConnectivityManager.TETHERING_WIFI}, |
| 1976 | * {@link ConnectivityManager.TETHERING_USB}, or |
| 1977 | * {@link ConnectivityManager.TETHERING_BLUETOOTH}. |
| 1978 | * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there |
| 1979 | * is one. This should be true the first time this function is called and also any time |
| 1980 | * the user can see this UI. It gives users information from their carrier about the |
| 1981 | * check failing and how they can sign up for tethering if possible. |
| 1982 | * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller |
| 1983 | * of the result of trying to tether. |
| 1984 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 1985 | * @hide |
| 1986 | */ |
| 1987 | @SystemApi |
| 1988 | public void startTethering(int type, boolean showProvisioningUi, |
| 1989 | final OnStartTetheringCallback callback, Handler handler) { |
| 1990 | ResultReceiver wrappedCallback = new ResultReceiver(handler) { |
| 1991 | @Override |
| 1992 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
| 1993 | if (resultCode == TETHER_ERROR_NO_ERROR) { |
| 1994 | callback.onTetheringStarted(); |
| 1995 | } else { |
| 1996 | callback.onTetheringFailed(); |
| 1997 | } |
| 1998 | } |
| 1999 | }; |
| 2000 | try { |
| 2001 | mService.startTethering(type, wrappedCallback, showProvisioningUi); |
| 2002 | } catch (RemoteException e) { |
| 2003 | Log.e(TAG, "Exception trying to start tethering.", e); |
| 2004 | wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null); |
| 2005 | } |
| 2006 | } |
| 2007 | |
| 2008 | /** |
| 2009 | * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if |
| 2010 | * applicable. |
| 2011 | * |
| 2012 | * @param type The type of tethering to stop. Must be one of |
| 2013 | * {@link ConnectivityManager.TETHERING_WIFI}, |
| 2014 | * {@link ConnectivityManager.TETHERING_USB}, or |
| 2015 | * {@link ConnectivityManager.TETHERING_BLUETOOTH}. |
| 2016 | * @hide |
| 2017 | */ |
| 2018 | @SystemApi |
| 2019 | public void stopTethering(int type) { |
| 2020 | try { |
| 2021 | mService.stopTethering(type); |
| 2022 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2023 | throw e.rethrowFromSystemServer(); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2028 | * Get the list of regular expressions that define any tetherable |
| 2029 | * USB network interfaces. If USB tethering is not supported by the |
| 2030 | * device, this list should be empty. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2031 | * <p>This method requires the caller to hold the permission |
| 2032 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2033 | * |
| 2034 | * @return an array of 0 or more regular expression Strings defining |
| 2035 | * what interfaces are considered tetherable usb interfaces. |
| 2036 | * |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2037 | * {@hide} |
| 2038 | */ |
| 2039 | public String[] getTetherableUsbRegexs() { |
| 2040 | try { |
| 2041 | return mService.getTetherableUsbRegexs(); |
| 2042 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2043 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2048 | * Get the list of regular expressions that define any tetherable |
| 2049 | * Wifi network interfaces. If Wifi tethering is not supported by the |
| 2050 | * device, this list should be empty. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2051 | * <p>This method requires the caller to hold the permission |
| 2052 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2053 | * |
| 2054 | * @return an array of 0 or more regular expression Strings defining |
| 2055 | * what interfaces are considered tetherable wifi interfaces. |
| 2056 | * |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2057 | * {@hide} |
| 2058 | */ |
| 2059 | public String[] getTetherableWifiRegexs() { |
| 2060 | try { |
| 2061 | return mService.getTetherableWifiRegexs(); |
| 2062 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2063 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2064 | } |
| 2065 | } |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2066 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2067 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2068 | * Get the list of regular expressions that define any tetherable |
| 2069 | * Bluetooth network interfaces. If Bluetooth tethering is not supported by the |
| 2070 | * device, this list should be empty. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2071 | * <p>This method requires the caller to hold the permission |
| 2072 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2073 | * |
| 2074 | * @return an array of 0 or more regular expression Strings defining |
| 2075 | * what interfaces are considered tetherable bluetooth interfaces. |
| 2076 | * |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2077 | * {@hide} |
| 2078 | */ |
| 2079 | public String[] getTetherableBluetoothRegexs() { |
| 2080 | try { |
| 2081 | return mService.getTetherableBluetoothRegexs(); |
| 2082 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2083 | throw e.rethrowFromSystemServer(); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2084 | } |
| 2085 | } |
| 2086 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2087 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2088 | * Attempt to both alter the mode of USB and Tethering of USB. A |
| 2089 | * utility method to deal with some of the complexity of USB - will |
| 2090 | * attempt to switch to Rndis and subsequently tether the resulting |
| 2091 | * interface on {@code true} or turn off tethering and switch off |
| 2092 | * Rndis on {@code false}. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2093 | * |
| 2094 | * <p>This method requires the caller to hold either the |
| 2095 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2096 | * or the ability to modify system settings as determined by |
| 2097 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2098 | * |
| 2099 | * @param enable a boolean - {@code true} to enable tethering |
| 2100 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
| 2101 | * |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2102 | * {@hide} |
| 2103 | */ |
| 2104 | public int setUsbTethering(boolean enable) { |
| 2105 | try { |
| 2106 | return mService.setUsbTethering(enable); |
| 2107 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2108 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2109 | } |
| 2110 | } |
| 2111 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2112 | /** {@hide} */ |
| 2113 | public static final int TETHER_ERROR_NO_ERROR = 0; |
| 2114 | /** {@hide} */ |
| 2115 | public static final int TETHER_ERROR_UNKNOWN_IFACE = 1; |
| 2116 | /** {@hide} */ |
| 2117 | public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2; |
| 2118 | /** {@hide} */ |
| 2119 | public static final int TETHER_ERROR_UNSUPPORTED = 3; |
| 2120 | /** {@hide} */ |
| 2121 | public static final int TETHER_ERROR_UNAVAIL_IFACE = 4; |
| 2122 | /** {@hide} */ |
| 2123 | public static final int TETHER_ERROR_MASTER_ERROR = 5; |
| 2124 | /** {@hide} */ |
| 2125 | public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6; |
| 2126 | /** {@hide} */ |
| 2127 | public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7; |
| 2128 | /** {@hide} */ |
| 2129 | public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8; |
| 2130 | /** {@hide} */ |
| 2131 | public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9; |
| 2132 | /** {@hide} */ |
| 2133 | public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2134 | /** {@hide} */ |
| 2135 | public static final int TETHER_ERROR_PROVISION_FAILED = 11; |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2136 | |
| 2137 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2138 | * Get a more detailed error code after a Tethering or Untethering |
| 2139 | * request asynchronously failed. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2140 | * <p>This method requires the caller to hold the permission |
| 2141 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2142 | * |
| 2143 | * @param iface The name of the interface of interest |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2144 | * @return error The error code of the last error tethering or untethering the named |
| 2145 | * interface |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2146 | * |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2147 | * {@hide} |
| 2148 | */ |
| 2149 | public int getLastTetherError(String iface) { |
| 2150 | try { |
| 2151 | return mService.getLastTetherError(iface); |
| 2152 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2153 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2154 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2158 | * Report network connectivity status. This is currently used only |
| 2159 | * to alter status bar UI. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2160 | * <p>This method requires the caller to hold the permission |
| 2161 | * {@link android.Manifest.permission#STATUS_BAR}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2162 | * |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2163 | * @param networkType The type of network you want to report on |
| 2164 | * @param percentage The quality of the connection 0 is bad, 100 is good |
| 2165 | * {@hide} |
| 2166 | */ |
| 2167 | public void reportInetCondition(int networkType, int percentage) { |
| 2168 | try { |
| 2169 | mService.reportInetCondition(networkType, percentage); |
| 2170 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2171 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2172 | } |
| 2173 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2174 | |
| 2175 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2176 | * Report a problem network to the framework. This provides a hint to the system |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 2177 | * that there might be connectivity problems on this network and may cause |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2178 | * the framework to re-evaluate network connectivity and/or switch to another |
| 2179 | * network. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2180 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2181 | * @param network The {@link Network} the application was attempting to use |
| 2182 | * or {@code null} to indicate the current default network. |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2183 | * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both |
| 2184 | * working and non-working connectivity. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2185 | */ |
| 2186 | public void reportBadNetwork(Network network) { |
| 2187 | try { |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2188 | // One of these will be ignored because it matches system's current state. |
| 2189 | // The other will trigger the necessary reevaluation. |
| 2190 | mService.reportNetworkConnectivity(network, true); |
| 2191 | mService.reportNetworkConnectivity(network, false); |
| 2192 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2193 | throw e.rethrowFromSystemServer(); |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | /** |
| 2198 | * Report to the framework whether a network has working connectivity. |
| 2199 | * This provides a hint to the system that a particular network is providing |
| 2200 | * working connectivity or not. In response the framework may re-evaluate |
| 2201 | * the network's connectivity and might take further action thereafter. |
| 2202 | * |
| 2203 | * @param network The {@link Network} the application was attempting to use |
| 2204 | * or {@code null} to indicate the current default network. |
| 2205 | * @param hasConnectivity {@code true} if the application was able to successfully access the |
| 2206 | * Internet using {@code network} or {@code false} if not. |
| 2207 | */ |
| 2208 | public void reportNetworkConnectivity(Network network, boolean hasConnectivity) { |
| 2209 | try { |
| 2210 | mService.reportNetworkConnectivity(network, hasConnectivity); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2211 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2212 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2213 | } |
| 2214 | } |
| 2215 | |
| 2216 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2217 | * Set a network-independent global http proxy. This is not normally what you want |
| 2218 | * for typical HTTP proxies - they are general network dependent. However if you're |
| 2219 | * doing something unusual like general internal filtering this may be useful. On |
| 2220 | * a private network where the proxy is not accessible, you may break HTTP using this. |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 2221 | * <p>This method requires the caller to hold the permission |
Jason Monk | faf3fd5 | 2014-05-07 18:41:13 -0400 | [diff] [blame] | 2222 | * android.Manifest.permission#CONNECTIVITY_INTERNAL. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2223 | * |
| 2224 | * @param p A {@link ProxyInfo} object defining the new global |
| 2225 | * HTTP proxy. A {@code null} value will clear the global HTTP proxy. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2226 | * @hide |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2227 | */ |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2228 | public void setGlobalProxy(ProxyInfo p) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2229 | try { |
| 2230 | mService.setGlobalProxy(p); |
| 2231 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2232 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2233 | } |
| 2234 | } |
| 2235 | |
| 2236 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2237 | * Retrieve any network-independent global HTTP proxy. |
| 2238 | * |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2239 | * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null} |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2240 | * if no global HTTP proxy is set. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2241 | * @hide |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2242 | */ |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2243 | public ProxyInfo getGlobalProxy() { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2244 | try { |
| 2245 | return mService.getGlobalProxy(); |
| 2246 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2247 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | /** |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 2252 | * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a |
| 2253 | * network-specific HTTP proxy. If {@code network} is null, the |
| 2254 | * network-specific proxy returned is the proxy of the default active |
| 2255 | * network. |
| 2256 | * |
| 2257 | * @return {@link ProxyInfo} for the current global HTTP proxy, or if no |
| 2258 | * global HTTP proxy is set, {@code ProxyInfo} for {@code network}, |
| 2259 | * or when {@code network} is {@code null}, |
| 2260 | * the {@code ProxyInfo} for the default active network. Returns |
| 2261 | * {@code null} when no proxy applies or the caller doesn't have |
| 2262 | * permission to use {@code network}. |
| 2263 | * @hide |
| 2264 | */ |
| 2265 | public ProxyInfo getProxyForNetwork(Network network) { |
| 2266 | try { |
| 2267 | return mService.getProxyForNetwork(network); |
| 2268 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2269 | throw e.rethrowFromSystemServer(); |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 2270 | } |
| 2271 | } |
| 2272 | |
| 2273 | /** |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2274 | * Get the current default HTTP proxy settings. If a global proxy is set it will be returned, |
| 2275 | * otherwise if this process is bound to a {@link Network} using |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2276 | * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2277 | * the default network's proxy is returned. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2278 | * |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2279 | * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2280 | * HTTP proxy is active. |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2281 | */ |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2282 | public ProxyInfo getDefaultProxy() { |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 2283 | return getProxyForNetwork(getBoundNetworkForProcess()); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2284 | } |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2285 | |
| 2286 | /** |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 2287 | * Returns true if the hardware supports the given network type |
| 2288 | * else it returns false. This doesn't indicate we have coverage |
| 2289 | * or are authorized onto a network, just whether or not the |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2290 | * hardware supports it. For example a GSM phone without a SIM |
| 2291 | * should still return {@code true} for mobile data, but a wifi only |
| 2292 | * tablet would return {@code false}. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2293 | * <p>This method requires the caller to hold the permission |
| 2294 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2295 | * |
| 2296 | * @param networkType The network type we'd like to check |
| 2297 | * @return {@code true} if supported, else {@code false} |
| 2298 | * |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 2299 | * @hide |
| 2300 | */ |
| 2301 | public boolean isNetworkSupported(int networkType) { |
| 2302 | try { |
| 2303 | return mService.isNetworkSupported(networkType); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2304 | } catch (RemoteException e) { |
| 2305 | throw e.rethrowFromSystemServer(); |
| 2306 | } |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 2307 | } |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 2308 | |
| 2309 | /** |
| 2310 | * Returns if the currently active data network is metered. A network is |
| 2311 | * classified as metered when the user is sensitive to heavy data usage on |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2312 | * that connection due to monetary costs, data limitations or |
| 2313 | * battery/performance issues. You should check this before doing large |
| 2314 | * data transfers, and warn the user or delay the operation until another |
| 2315 | * network is available. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2316 | * <p>This method requires the caller to hold the permission |
| 2317 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2318 | * |
| 2319 | * @return {@code true} if large transfers should be avoided, otherwise |
| 2320 | * {@code false}. |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 2321 | */ |
| 2322 | public boolean isActiveNetworkMetered() { |
| 2323 | try { |
| 2324 | return mService.isActiveNetworkMetered(); |
| 2325 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2326 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 2327 | } |
| 2328 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2329 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2330 | /** |
| 2331 | * If the LockdownVpn mechanism is enabled, updates the vpn |
| 2332 | * with a reload of its profile. |
| 2333 | * |
| 2334 | * @return a boolean with {@code} indicating success |
| 2335 | * |
| 2336 | * <p>This method can only be called by the system UID |
| 2337 | * {@hide} |
| 2338 | */ |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2339 | public boolean updateLockdownVpn() { |
| 2340 | try { |
| 2341 | return mService.updateLockdownVpn(); |
| 2342 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2343 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2344 | } |
| 2345 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2346 | |
| 2347 | /** |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2348 | * Check mobile provisioning. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2349 | * |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2350 | * @param suggestedTimeOutMs, timeout in milliseconds |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2351 | * |
| 2352 | * @return time out that will be used, maybe less that suggestedTimeOutMs |
| 2353 | * -1 if an error. |
| 2354 | * |
| 2355 | * {@hide} |
| 2356 | */ |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2357 | public int checkMobileProvisioning(int suggestedTimeOutMs) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2358 | int timeOutMs = -1; |
| 2359 | try { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2360 | timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2361 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2362 | throw e.rethrowFromSystemServer(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2363 | } |
| 2364 | return timeOutMs; |
| 2365 | } |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 2366 | |
| 2367 | /** |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 2368 | * Get the mobile provisioning url. |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 2369 | * {@hide} |
| 2370 | */ |
| 2371 | public String getMobileProvisioningUrl() { |
| 2372 | try { |
| 2373 | return mService.getMobileProvisioningUrl(); |
| 2374 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2375 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 2376 | } |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 2377 | } |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 2378 | |
| 2379 | /** |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2380 | * Set sign in error notification to visible or in visible |
| 2381 | * |
| 2382 | * @param visible |
| 2383 | * @param networkType |
| 2384 | * |
| 2385 | * {@hide} |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 2386 | * @deprecated Doesn't properly deal with multiple connected networks of the same type. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2387 | */ |
| 2388 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 2389 | String action) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2390 | try { |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 2391 | mService.setProvisioningNotificationVisible(visible, networkType, action); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2392 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2393 | throw e.rethrowFromSystemServer(); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2394 | } |
| 2395 | } |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 2396 | |
| 2397 | /** |
| 2398 | * Set the value for enabling/disabling airplane mode |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2399 | * <p>This method requires the caller to hold the permission |
| 2400 | * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}. |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 2401 | * |
| 2402 | * @param enable whether to enable airplane mode or not |
| 2403 | * |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 2404 | * @hide |
| 2405 | */ |
| 2406 | public void setAirplaneMode(boolean enable) { |
| 2407 | try { |
| 2408 | mService.setAirplaneMode(enable); |
| 2409 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2410 | throw e.rethrowFromSystemServer(); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 2411 | } |
| 2412 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2413 | |
| 2414 | /** {@hide} */ |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2415 | public void registerNetworkFactory(Messenger messenger, String name) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2416 | try { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2417 | mService.registerNetworkFactory(messenger, name); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2418 | } catch (RemoteException e) { |
| 2419 | throw e.rethrowFromSystemServer(); |
| 2420 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2421 | } |
| 2422 | |
| 2423 | /** {@hide} */ |
| 2424 | public void unregisterNetworkFactory(Messenger messenger) { |
| 2425 | try { |
| 2426 | mService.unregisterNetworkFactory(messenger); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2427 | } catch (RemoteException e) { |
| 2428 | throw e.rethrowFromSystemServer(); |
| 2429 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2430 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2431 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 2432 | /** |
| 2433 | * @hide |
| 2434 | * Register a NetworkAgent with ConnectivityService. |
| 2435 | * @return NetID corresponding to NetworkAgent. |
| 2436 | */ |
| 2437 | public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 2438 | NetworkCapabilities nc, int score, NetworkMisc misc) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2439 | try { |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 2440 | return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc); |
| 2441 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2442 | throw e.rethrowFromSystemServer(); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 2443 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2444 | } |
| 2445 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2446 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2447 | * Base class for NetworkRequest callbacks. Used for notifications about network |
| 2448 | * changes. Should be extended by applications wanting notifications. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2449 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2450 | public static class NetworkCallback { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2451 | /** |
Lorenzo Colitti | 0708693 | 2015-04-24 12:23:24 +0900 | [diff] [blame] | 2452 | * Called when the framework connects to a new network to evaluate whether it satisfies this |
| 2453 | * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable} |
| 2454 | * callback. There is no guarantee that this new network will satisfy any requests, or that |
| 2455 | * the network will stay connected for longer than the time necessary to evaluate it. |
| 2456 | * <p> |
| 2457 | * Most applications <b>should not</b> act on this callback, and should instead use |
| 2458 | * {@link #onAvailable}. This callback is intended for use by applications that can assist |
| 2459 | * the framework in properly evaluating the network — for example, an application that |
| 2460 | * can automatically log in to a captive portal without user intervention. |
| 2461 | * |
| 2462 | * @param network The {@link Network} of the network that is being evaluated. |
Lorenzo Colitti | 6627612 | 2015-06-11 14:27:17 +0900 | [diff] [blame] | 2463 | * |
| 2464 | * @hide |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2465 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2466 | public void onPreCheck(Network network) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2467 | |
| 2468 | /** |
Lorenzo Colitti | 0708693 | 2015-04-24 12:23:24 +0900 | [diff] [blame] | 2469 | * Called when the framework connects and has declared a new network ready for use. |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2470 | * This callback may be called more than once if the {@link Network} that is |
| 2471 | * satisfying the request changes. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2472 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2473 | * @param network The {@link Network} of the satisfying network. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2474 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2475 | public void onAvailable(Network network) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2476 | |
| 2477 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2478 | * Called when the network is about to be disconnected. Often paired with an |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2479 | * {@link NetworkCallback#onAvailable} call with the new replacement network |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2480 | * for graceful handover. This may not be called if we have a hard loss |
| 2481 | * (loss without warning). This may be followed by either a |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2482 | * {@link NetworkCallback#onLost} call or a |
| 2483 | * {@link NetworkCallback#onAvailable} call for this network depending |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2484 | * on whether we lose or regain it. |
| 2485 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2486 | * @param network The {@link Network} that is about to be disconnected. |
| 2487 | * @param maxMsToLive The time in ms the framework will attempt to keep the |
| 2488 | * network connected. Note that the network may suffer a |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2489 | * hard loss at any time. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2490 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2491 | public void onLosing(Network network, int maxMsToLive) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2492 | |
| 2493 | /** |
| 2494 | * Called when the framework has a hard loss of the network or when the |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2495 | * graceful failure ends. |
| 2496 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2497 | * @param network The {@link Network} lost. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2498 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2499 | public void onLost(Network network) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2500 | |
| 2501 | /** |
| 2502 | * Called if no network is found in the given timeout time. If no timeout is given, |
| 2503 | * this will not be called. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2504 | * @hide |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2505 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2506 | public void onUnavailable() {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2507 | |
| 2508 | /** |
| 2509 | * Called when the network the framework connected to for this request |
| 2510 | * changes capabilities but still satisfies the stated need. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2511 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2512 | * @param network The {@link Network} whose capabilities have changed. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 2513 | * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2514 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2515 | public void onCapabilitiesChanged(Network network, |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2516 | NetworkCapabilities networkCapabilities) {} |
| 2517 | |
| 2518 | /** |
| 2519 | * Called when the network the framework connected to for this request |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2520 | * changes {@link LinkProperties}. |
| 2521 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2522 | * @param network The {@link Network} whose link properties have changed. |
| 2523 | * @param linkProperties The new {@link LinkProperties} for this network. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2524 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2525 | public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2526 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 2527 | /** |
| 2528 | * Called when the network the framework connected to for this request |
| 2529 | * goes into {@link NetworkInfo.DetailedState.SUSPENDED}. |
| 2530 | * This generally means that while the TCP connections are still live, |
| 2531 | * temporarily network data fails to transfer. Specifically this is used |
| 2532 | * on cellular networks to mask temporary outages when driving through |
| 2533 | * a tunnel, etc. |
| 2534 | * @hide |
| 2535 | */ |
| 2536 | public void onNetworkSuspended(Network network) {} |
| 2537 | |
| 2538 | /** |
| 2539 | * Called when the network the framework connected to for this request |
| 2540 | * returns from a {@link NetworkInfo.DetailedState.SUSPENDED} state. |
| 2541 | * This should always be preceeded by a matching {@code onNetworkSuspended} |
| 2542 | * call. |
| 2543 | * @hide |
| 2544 | */ |
| 2545 | public void onNetworkResumed(Network network) {} |
| 2546 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2547 | private NetworkRequest networkRequest; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2548 | } |
| 2549 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2550 | private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2551 | /** @hide */ |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 2552 | public static final int CALLBACK_PRECHECK = BASE + 1; |
| 2553 | /** @hide */ |
| 2554 | public static final int CALLBACK_AVAILABLE = BASE + 2; |
| 2555 | /** @hide arg1 = TTL */ |
| 2556 | public static final int CALLBACK_LOSING = BASE + 3; |
| 2557 | /** @hide */ |
| 2558 | public static final int CALLBACK_LOST = BASE + 4; |
| 2559 | /** @hide */ |
| 2560 | public static final int CALLBACK_UNAVAIL = BASE + 5; |
| 2561 | /** @hide */ |
| 2562 | public static final int CALLBACK_CAP_CHANGED = BASE + 6; |
| 2563 | /** @hide */ |
| 2564 | public static final int CALLBACK_IP_CHANGED = BASE + 7; |
| 2565 | /** @hide */ |
| 2566 | public static final int CALLBACK_RELEASED = BASE + 8; |
| 2567 | /** @hide */ |
| 2568 | public static final int CALLBACK_EXIT = BASE + 9; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2569 | /** @hide obj = NetworkCapabilities, arg1 = seq number */ |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 2570 | private static final int EXPIRE_LEGACY_REQUEST = BASE + 10; |
| 2571 | /** @hide */ |
| 2572 | public static final int CALLBACK_SUSPENDED = BASE + 11; |
| 2573 | /** @hide */ |
| 2574 | public static final int CALLBACK_RESUMED = BASE + 12; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2575 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2576 | private class CallbackHandler extends Handler { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2577 | private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2578 | private final AtomicInteger mRefCount; |
| 2579 | private static final String TAG = "ConnectivityManager.CallbackHandler"; |
| 2580 | private final ConnectivityManager mCm; |
Robert Greenwalt | a9ebeef | 2015-09-03 16:41:45 -0700 | [diff] [blame] | 2581 | private static final boolean DBG = false; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2582 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2583 | CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap, |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2584 | AtomicInteger refCount, ConnectivityManager cm) { |
| 2585 | super(looper); |
| 2586 | mCallbackMap = callbackMap; |
| 2587 | mRefCount = refCount; |
| 2588 | mCm = cm; |
| 2589 | } |
| 2590 | |
| 2591 | @Override |
| 2592 | public void handleMessage(Message message) { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2593 | NetworkRequest request = (NetworkRequest) getObject(message, NetworkRequest.class); |
| 2594 | Network network = (Network) getObject(message, Network.class); |
Lorenzo Colitti | fcfa7d9 | 2016-03-01 22:56:37 +0900 | [diff] [blame] | 2595 | if (DBG) { |
| 2596 | Log.d(TAG, whatToString(message.what) + " for network " + network); |
| 2597 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2598 | switch (message.what) { |
| 2599 | case CALLBACK_PRECHECK: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2600 | NetworkCallback callback = getCallback(request, "PRECHECK"); |
| 2601 | if (callback != null) { |
| 2602 | callback.onPreCheck(network); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2603 | } |
| 2604 | break; |
| 2605 | } |
| 2606 | case CALLBACK_AVAILABLE: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2607 | NetworkCallback callback = getCallback(request, "AVAILABLE"); |
| 2608 | if (callback != null) { |
| 2609 | callback.onAvailable(network); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2610 | } |
| 2611 | break; |
| 2612 | } |
| 2613 | case CALLBACK_LOSING: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2614 | NetworkCallback callback = getCallback(request, "LOSING"); |
| 2615 | if (callback != null) { |
| 2616 | callback.onLosing(network, message.arg1); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2617 | } |
| 2618 | break; |
| 2619 | } |
| 2620 | case CALLBACK_LOST: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2621 | NetworkCallback callback = getCallback(request, "LOST"); |
| 2622 | if (callback != null) { |
| 2623 | callback.onLost(network); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2624 | } |
| 2625 | break; |
| 2626 | } |
| 2627 | case CALLBACK_UNAVAIL: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2628 | NetworkCallback callback = getCallback(request, "UNAVAIL"); |
| 2629 | if (callback != null) { |
| 2630 | callback.onUnavailable(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2631 | } |
| 2632 | break; |
| 2633 | } |
| 2634 | case CALLBACK_CAP_CHANGED: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2635 | NetworkCallback callback = getCallback(request, "CAP_CHANGED"); |
| 2636 | if (callback != null) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2637 | NetworkCapabilities cap = (NetworkCapabilities)getObject(message, |
| 2638 | NetworkCapabilities.class); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2639 | |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2640 | callback.onCapabilitiesChanged(network, cap); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2641 | } |
| 2642 | break; |
| 2643 | } |
| 2644 | case CALLBACK_IP_CHANGED: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2645 | NetworkCallback callback = getCallback(request, "IP_CHANGED"); |
| 2646 | if (callback != null) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2647 | LinkProperties lp = (LinkProperties)getObject(message, |
| 2648 | LinkProperties.class); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2649 | |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2650 | callback.onLinkPropertiesChanged(network, lp); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2651 | } |
| 2652 | break; |
| 2653 | } |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 2654 | case CALLBACK_SUSPENDED: { |
| 2655 | NetworkCallback callback = getCallback(request, "SUSPENDED"); |
| 2656 | if (callback != null) { |
| 2657 | callback.onNetworkSuspended(network); |
| 2658 | } |
| 2659 | break; |
| 2660 | } |
| 2661 | case CALLBACK_RESUMED: { |
| 2662 | NetworkCallback callback = getCallback(request, "RESUMED"); |
| 2663 | if (callback != null) { |
| 2664 | callback.onNetworkResumed(network); |
| 2665 | } |
| 2666 | break; |
| 2667 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2668 | case CALLBACK_RELEASED: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2669 | NetworkCallback callback = null; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2670 | synchronized(mCallbackMap) { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2671 | callback = mCallbackMap.remove(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2672 | } |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2673 | if (callback != null) { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2674 | synchronized(mRefCount) { |
| 2675 | if (mRefCount.decrementAndGet() == 0) { |
| 2676 | getLooper().quit(); |
| 2677 | } |
| 2678 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2679 | } else { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2680 | Log.e(TAG, "callback not found for RELEASED message"); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2681 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2682 | break; |
| 2683 | } |
| 2684 | case CALLBACK_EXIT: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2685 | Log.d(TAG, "Listener quitting"); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2686 | getLooper().quit(); |
| 2687 | break; |
| 2688 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2689 | case EXPIRE_LEGACY_REQUEST: { |
| 2690 | expireRequest((NetworkCapabilities)message.obj, message.arg1); |
| 2691 | break; |
| 2692 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2693 | } |
| 2694 | } |
| 2695 | |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2696 | private Object getObject(Message msg, Class c) { |
| 2697 | return msg.getData().getParcelable(c.getSimpleName()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2698 | } |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2699 | |
| 2700 | private NetworkCallback getCallback(NetworkRequest req, String name) { |
| 2701 | NetworkCallback callback; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2702 | synchronized(mCallbackMap) { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2703 | callback = mCallbackMap.get(req); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2704 | } |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2705 | if (callback == null) { |
| 2706 | Log.e(TAG, "callback not found for " + name + " message"); |
| 2707 | } |
| 2708 | return callback; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2709 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2710 | } |
| 2711 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2712 | private void incCallbackHandlerRefCount() { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2713 | synchronized(sCallbackRefCount) { |
| 2714 | if (sCallbackRefCount.incrementAndGet() == 1) { |
| 2715 | // TODO - switch this over to a ManagerThread or expire it when done |
| 2716 | HandlerThread callbackThread = new HandlerThread("ConnectivityManager"); |
| 2717 | callbackThread.start(); |
| 2718 | sCallbackHandler = new CallbackHandler(callbackThread.getLooper(), |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2719 | sNetworkCallback, sCallbackRefCount, this); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2720 | } |
| 2721 | } |
| 2722 | } |
| 2723 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2724 | private void decCallbackHandlerRefCount() { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2725 | synchronized(sCallbackRefCount) { |
| 2726 | if (sCallbackRefCount.decrementAndGet() == 0) { |
| 2727 | sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget(); |
| 2728 | sCallbackHandler = null; |
| 2729 | } |
| 2730 | } |
| 2731 | } |
| 2732 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2733 | static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback = |
| 2734 | new HashMap<NetworkRequest, NetworkCallback>(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2735 | static final AtomicInteger sCallbackRefCount = new AtomicInteger(0); |
| 2736 | static CallbackHandler sCallbackHandler = null; |
| 2737 | |
| 2738 | private final static int LISTEN = 1; |
| 2739 | private final static int REQUEST = 2; |
| 2740 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2741 | private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2742 | NetworkCallback networkCallback, int timeoutSec, int action, |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 2743 | int legacyType) { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2744 | if (networkCallback == null) { |
| 2745 | throw new IllegalArgumentException("null NetworkCallback"); |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2746 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2747 | if (need == null) throw new IllegalArgumentException("null NetworkCapabilities"); |
| 2748 | try { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2749 | incCallbackHandlerRefCount(); |
Paul Jensen | 7221cc3 | 2014-06-27 11:05:32 -0400 | [diff] [blame] | 2750 | synchronized(sNetworkCallback) { |
| 2751 | if (action == LISTEN) { |
| 2752 | networkCallback.networkRequest = mService.listenForNetwork(need, |
| 2753 | new Messenger(sCallbackHandler), new Binder()); |
| 2754 | } else { |
| 2755 | networkCallback.networkRequest = mService.requestNetwork(need, |
| 2756 | new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType); |
| 2757 | } |
| 2758 | if (networkCallback.networkRequest != null) { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2759 | sNetworkCallback.put(networkCallback.networkRequest, networkCallback); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2760 | } |
| 2761 | } |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2762 | } catch (RemoteException e) { |
| 2763 | throw e.rethrowFromSystemServer(); |
| 2764 | } |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2765 | if (networkCallback.networkRequest == null) decCallbackHandlerRefCount(); |
| 2766 | return networkCallback.networkRequest; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | /** |
Lorenzo Colitti | 7de289f | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 2770 | * Helper function to requests a network with a particular legacy type. |
| 2771 | * |
| 2772 | * This is temporarily public @hide so it can be called by system code that uses the |
| 2773 | * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for |
| 2774 | * instead network notifications. |
| 2775 | * |
| 2776 | * TODO: update said system code to rely on NetworkCallbacks and make this method private. |
| 2777 | * |
| 2778 | * @hide |
| 2779 | */ |
Lorenzo Colitti | d117946 | 2015-11-25 15:47:14 +0900 | [diff] [blame] | 2780 | public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback, |
Lorenzo Colitti | 7de289f | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 2781 | int timeoutMs, int legacyType) { |
Lorenzo Colitti | d117946 | 2015-11-25 15:47:14 +0900 | [diff] [blame] | 2782 | sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs, REQUEST, |
| 2783 | legacyType); |
Lorenzo Colitti | 7de289f | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 2784 | } |
| 2785 | |
| 2786 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 2787 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2788 | * |
| 2789 | * This {@link NetworkRequest} will live until released via |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2790 | * {@link #unregisterNetworkCallback} or the calling application exits. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2791 | * Status of the request can be followed by listening to the various |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2792 | * callbacks described in {@link NetworkCallback}. The {@link Network} |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2793 | * can be used to direct traffic to the network. |
Paul Jensen | bb2e0e9 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 2794 | * <p>It is presently unsupported to request a network with mutable |
| 2795 | * {@link NetworkCapabilities} such as |
| 2796 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 2797 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} |
| 2798 | * as these {@code NetworkCapabilities} represent states that a particular |
| 2799 | * network may never attain, and whether a network will attain these states |
| 2800 | * is unknown prior to bringing up the network so the framework does not |
| 2801 | * know how to go about satisfing a request with these capabilities. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2802 | * |
| 2803 | * <p>This method requires the caller to hold either the |
| 2804 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2805 | * or the ability to modify system settings as determined by |
| 2806 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2807 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2808 | * @param request {@link NetworkRequest} describing this request. |
| 2809 | * @param networkCallback The {@link NetworkCallback} to be utilized for this |
| 2810 | * request. Note the callback must not be shared - they |
| 2811 | * uniquely specify this request. |
Paul Jensen | bb2e0e9 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 2812 | * @throws IllegalArgumentException if {@code request} specifies any mutable |
| 2813 | * {@code NetworkCapabilities}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2814 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2815 | public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) { |
Lorenzo Colitti | d117946 | 2015-11-25 15:47:14 +0900 | [diff] [blame] | 2816 | requestNetwork(request, networkCallback, 0, |
Lorenzo Colitti | 7de289f | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 2817 | inferLegacyTypeForNetworkCapabilities(request.networkCapabilities)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2818 | } |
| 2819 | |
| 2820 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 2821 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2822 | * by a timeout. |
| 2823 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2824 | * This function behaves identically to the non-timedout version, but if a suitable |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2825 | * network is not found within the given time (in milliseconds) the |
| 2826 | * {@link NetworkCallback#unavailable} callback is called. The request must |
Lorenzo Colitti | 36728a9 | 2015-11-26 11:03:17 +0900 | [diff] [blame] | 2827 | * still be released normally by calling {@link unregisterNetworkCallback}. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2828 | * |
| 2829 | * <p>This method requires the caller to hold either the |
| 2830 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2831 | * or the ability to modify system settings as determined by |
| 2832 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 2833 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2834 | * @param request {@link NetworkRequest} describing this request. |
| 2835 | * @param networkCallback The callbacks to be utilized for this request. Note |
| 2836 | * the callbacks must not be shared - they uniquely specify |
| 2837 | * this request. |
| 2838 | * @param timeoutMs The time in milliseconds to attempt looking for a suitable network |
| 2839 | * before {@link NetworkCallback#unavailable} is called. |
Lorenzo Colitti | 7de289f | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 2840 | * |
| 2841 | * TODO: Make timeouts work and then unhide this method. |
| 2842 | * |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2843 | * @hide |
| 2844 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2845 | public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback, |
| 2846 | int timeoutMs) { |
Lorenzo Colitti | d117946 | 2015-11-25 15:47:14 +0900 | [diff] [blame] | 2847 | requestNetwork(request, networkCallback, timeoutMs, |
Lorenzo Colitti | 7de289f | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 2848 | inferLegacyTypeForNetworkCapabilities(request.networkCapabilities)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2849 | } |
| 2850 | |
| 2851 | /** |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2852 | * The maximum number of milliseconds the framework will look for a suitable network |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2853 | * during a timeout-equiped call to {@link requestNetwork}. |
| 2854 | * {@hide} |
| 2855 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2856 | public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2857 | |
| 2858 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2859 | * The lookup key for a {@link Network} object included with the intent after |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2860 | * successfully finding a network for the applications request. Retrieve it with |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2861 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Jeremy Joslin | fcde58f | 2015-02-11 16:51:13 -0800 | [diff] [blame] | 2862 | * <p> |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2863 | * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)} |
| 2864 | * then you must get a ConnectivityManager instance before doing so. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2865 | */ |
Erik Kline | 90e9307 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 2866 | public static final String EXTRA_NETWORK = "android.net.extra.NETWORK"; |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2867 | |
| 2868 | /** |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2869 | * The lookup key for a {@link NetworkRequest} object included with the intent after |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2870 | * successfully finding a network for the applications request. Retrieve it with |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2871 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 2872 | */ |
Erik Kline | 90e9307 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 2873 | public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST"; |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2874 | |
| 2875 | |
| 2876 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 2877 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2878 | * |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2879 | * This function behaves identically to the version that takes a NetworkCallback, but instead |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2880 | * of {@link NetworkCallback} a {@link PendingIntent} is used. This means |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2881 | * the request may outlive the calling application and get called back when a suitable |
| 2882 | * network is found. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2883 | * <p> |
| 2884 | * The operation is an Intent broadcast that goes to a broadcast receiver that |
| 2885 | * you registered with {@link Context#registerReceiver} or through the |
| 2886 | * <receiver> tag in an AndroidManifest.xml file |
| 2887 | * <p> |
| 2888 | * The operation Intent is delivered with two extras, a {@link Network} typed |
Erik Kline | 90e9307 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 2889 | * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest} |
| 2890 | * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2891 | * the original requests parameters. It is important to create a new, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2892 | * {@link NetworkCallback} based request before completing the processing of the |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2893 | * Intent to reserve the network or it will be released shortly after the Intent |
| 2894 | * is processed. |
| 2895 | * <p> |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 2896 | * If there is already a request for this Intent registered (with the equality of |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2897 | * two Intents defined by {@link Intent#filterEquals}), then it will be removed and |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2898 | * replaced by this one, effectively releasing the previous {@link NetworkRequest}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2899 | * <p> |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2900 | * The request may be released normally by calling |
| 2901 | * {@link #releaseNetworkRequest(android.app.PendingIntent)}. |
Paul Jensen | bb2e0e9 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 2902 | * <p>It is presently unsupported to request a network with either |
| 2903 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 2904 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} |
| 2905 | * as these {@code NetworkCapabilities} represent states that a particular |
| 2906 | * network may never attain, and whether a network will attain these states |
| 2907 | * is unknown prior to bringing up the network so the framework does not |
| 2908 | * know how to go about satisfing a request with these capabilities. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2909 | * |
| 2910 | * <p>This method requires the caller to hold either the |
| 2911 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2912 | * or the ability to modify system settings as determined by |
| 2913 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 2914 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2915 | * @param request {@link NetworkRequest} describing this request. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2916 | * @param operation Action to perform when the network is available (corresponds |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2917 | * to the {@link NetworkCallback#onAvailable} call. Typically |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2918 | * comes from {@link PendingIntent#getBroadcast}. Cannot be null. |
Paul Jensen | bb2e0e9 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 2919 | * @throws IllegalArgumentException if {@code request} contains either |
| 2920 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 2921 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2922 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2923 | public void requestNetwork(NetworkRequest request, PendingIntent operation) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2924 | checkPendingIntent(operation); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2925 | try { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2926 | mService.pendingRequestForNetwork(request.networkCapabilities, operation); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2927 | } catch (RemoteException e) { |
| 2928 | throw e.rethrowFromSystemServer(); |
| 2929 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | /** |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2933 | * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} |
| 2934 | * <p> |
| 2935 | * This method has the same behavior as {@link #unregisterNetworkCallback} with respect to |
| 2936 | * releasing network resources and disconnecting. |
| 2937 | * |
| 2938 | * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the |
| 2939 | * PendingIntent passed to |
| 2940 | * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the |
| 2941 | * corresponding NetworkRequest you'd like to remove. Cannot be null. |
| 2942 | */ |
| 2943 | public void releaseNetworkRequest(PendingIntent operation) { |
| 2944 | checkPendingIntent(operation); |
| 2945 | try { |
| 2946 | mService.releasePendingNetworkRequest(operation); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2947 | } catch (RemoteException e) { |
| 2948 | throw e.rethrowFromSystemServer(); |
| 2949 | } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | private void checkPendingIntent(PendingIntent intent) { |
| 2953 | if (intent == null) { |
| 2954 | throw new IllegalArgumentException("PendingIntent cannot be null."); |
| 2955 | } |
| 2956 | } |
| 2957 | |
| 2958 | /** |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2959 | * Registers to receive notifications about all networks which satisfy the given |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2960 | * {@link NetworkRequest}. The callbacks will continue to be called until |
| 2961 | * either the application exits or {@link #unregisterNetworkCallback} is called |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2962 | * <p>This method requires the caller to hold the permission |
| 2963 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2964 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2965 | * @param request {@link NetworkRequest} describing this request. |
| 2966 | * @param networkCallback The {@link NetworkCallback} that the system will call as suitable |
| 2967 | * networks change state. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2968 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2969 | public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) { |
| 2970 | sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | /** |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 2974 | * Registers a PendingIntent to be sent when a network is available which satisfies the given |
| 2975 | * {@link NetworkRequest}. |
| 2976 | * |
| 2977 | * This function behaves identically to the version that takes a NetworkCallback, but instead |
| 2978 | * of {@link NetworkCallback} a {@link PendingIntent} is used. This means |
| 2979 | * the request may outlive the calling application and get called back when a suitable |
| 2980 | * network is found. |
| 2981 | * <p> |
| 2982 | * The operation is an Intent broadcast that goes to a broadcast receiver that |
| 2983 | * you registered with {@link Context#registerReceiver} or through the |
| 2984 | * <receiver> tag in an AndroidManifest.xml file |
| 2985 | * <p> |
| 2986 | * The operation Intent is delivered with two extras, a {@link Network} typed |
| 2987 | * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest} |
| 2988 | * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing |
| 2989 | * the original requests parameters. |
| 2990 | * <p> |
| 2991 | * If there is already a request for this Intent registered (with the equality of |
| 2992 | * two Intents defined by {@link Intent#filterEquals}), then it will be removed and |
| 2993 | * replaced by this one, effectively releasing the previous {@link NetworkRequest}. |
| 2994 | * <p> |
| 2995 | * The request may be released normally by calling |
Paul Jensen | f2c1cfe | 2015-06-30 14:29:18 -0400 | [diff] [blame] | 2996 | * {@link #unregisterNetworkCallback(android.app.PendingIntent)}. |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 2997 | * <p>This method requires the caller to hold the permission |
| 2998 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
| 2999 | * @param request {@link NetworkRequest} describing this request. |
| 3000 | * @param operation Action to perform when the network is available (corresponds |
| 3001 | * to the {@link NetworkCallback#onAvailable} call. Typically |
| 3002 | * comes from {@link PendingIntent#getBroadcast}. Cannot be null. |
| 3003 | */ |
| 3004 | public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) { |
| 3005 | checkPendingIntent(operation); |
| 3006 | try { |
| 3007 | mService.pendingListenForNetwork(request.networkCapabilities, operation); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3008 | } catch (RemoteException e) { |
| 3009 | throw e.rethrowFromSystemServer(); |
| 3010 | } |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | /** |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 3014 | * Requests bandwidth update for a given {@link Network} and returns whether the update request |
| 3015 | * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying |
| 3016 | * network connection for updated bandwidth information. The caller will be notified via |
| 3017 | * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this |
| 3018 | * method assumes that the caller has previously called {@link #registerNetworkCallback} to |
| 3019 | * listen for network changes. |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 3020 | * |
fenglu | ae51919 | 2015-04-27 14:28:04 -0700 | [diff] [blame] | 3021 | * @param network {@link Network} specifying which network you're interested. |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 3022 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 3023 | */ |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 3024 | public boolean requestBandwidthUpdate(Network network) { |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 3025 | try { |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 3026 | return mService.requestBandwidthUpdate(network); |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 3027 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3028 | throw e.rethrowFromSystemServer(); |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 3029 | } |
| 3030 | } |
| 3031 | |
| 3032 | /** |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3033 | * Unregisters callbacks about and possibly releases networks originating from |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 3034 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and {@link #registerNetworkCallback} |
| 3035 | * calls. If the given {@code NetworkCallback} had previously been used with |
| 3036 | * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request |
| 3037 | * will be disconnected. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3038 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3039 | * @param networkCallback The {@link NetworkCallback} used when making the request. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3040 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3041 | public void unregisterNetworkCallback(NetworkCallback networkCallback) { |
| 3042 | if (networkCallback == null || networkCallback.networkRequest == null || |
| 3043 | networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) { |
| 3044 | throw new IllegalArgumentException("Invalid NetworkCallback"); |
| 3045 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3046 | try { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3047 | mService.releaseNetworkRequest(networkCallback.networkRequest); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3048 | } catch (RemoteException e) { |
| 3049 | throw e.rethrowFromSystemServer(); |
| 3050 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3051 | } |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 3052 | |
| 3053 | /** |
Paul Jensen | f2c1cfe | 2015-06-30 14:29:18 -0400 | [diff] [blame] | 3054 | * Unregisters a callback previously registered via |
| 3055 | * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}. |
| 3056 | * |
| 3057 | * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the |
| 3058 | * PendingIntent passed to |
| 3059 | * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}. |
| 3060 | * Cannot be null. |
| 3061 | */ |
| 3062 | public void unregisterNetworkCallback(PendingIntent operation) { |
| 3063 | releaseNetworkRequest(operation); |
| 3064 | } |
| 3065 | |
| 3066 | /** |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3067 | * Informs the system whether it should switch to {@code network} regardless of whether it is |
| 3068 | * validated or not. If {@code accept} is true, and the network was explicitly selected by the |
| 3069 | * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become |
| 3070 | * the system default network regardless of any other network that's currently connected. If |
| 3071 | * {@code always} is true, then the choice is remembered, so that the next time the user |
| 3072 | * connects to this network, the system will switch to it. |
| 3073 | * |
| 3074 | * <p>This method requires the caller to hold the permission |
| 3075 | * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL} |
| 3076 | * |
| 3077 | * @param network The network to accept. |
| 3078 | * @param accept Whether to accept the network even if unvalidated. |
| 3079 | * @param always Whether to remember this choice in the future. |
| 3080 | * |
| 3081 | * @hide |
| 3082 | */ |
| 3083 | public void setAcceptUnvalidated(Network network, boolean accept, boolean always) { |
| 3084 | try { |
| 3085 | mService.setAcceptUnvalidated(network, accept, always); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3086 | } catch (RemoteException e) { |
| 3087 | throw e.rethrowFromSystemServer(); |
| 3088 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | /** |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 3092 | * Resets all connectivity manager settings back to factory defaults. |
| 3093 | * @hide |
| 3094 | */ |
| 3095 | public void factoryReset() { |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 3096 | try { |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 3097 | mService.factoryReset(); |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 3098 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3099 | throw e.rethrowFromSystemServer(); |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | /** |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 3104 | * Binds the current process to {@code network}. All Sockets created in the future |
| 3105 | * (and not explicitly bound via a bound SocketFactory from |
| 3106 | * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to |
| 3107 | * {@code network}. All host name resolutions will be limited to {@code network} as well. |
| 3108 | * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to |
| 3109 | * work and all host name resolutions will fail. This is by design so an application doesn't |
| 3110 | * accidentally use Sockets it thinks are still bound to a particular {@link Network}. |
| 3111 | * To clear binding pass {@code null} for {@code network}. Using individually bound |
| 3112 | * Sockets created by Network.getSocketFactory().createSocket() and |
| 3113 | * performing network-specific host name resolutions via |
| 3114 | * {@link Network#getAllByName Network.getAllByName} is preferred to calling |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 3115 | * {@code bindProcessToNetwork}. |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 3116 | * |
| 3117 | * @param network The {@link Network} to bind the current process to, or {@code null} to clear |
| 3118 | * the current binding. |
| 3119 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 3120 | */ |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 3121 | public boolean bindProcessToNetwork(Network network) { |
| 3122 | // Forcing callers to call thru non-static function ensures ConnectivityManager |
| 3123 | // instantiated. |
| 3124 | return setProcessDefaultNetwork(network); |
| 3125 | } |
| 3126 | |
| 3127 | /** |
| 3128 | * Binds the current process to {@code network}. All Sockets created in the future |
| 3129 | * (and not explicitly bound via a bound SocketFactory from |
| 3130 | * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to |
| 3131 | * {@code network}. All host name resolutions will be limited to {@code network} as well. |
| 3132 | * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to |
| 3133 | * work and all host name resolutions will fail. This is by design so an application doesn't |
| 3134 | * accidentally use Sockets it thinks are still bound to a particular {@link Network}. |
| 3135 | * To clear binding pass {@code null} for {@code network}. Using individually bound |
| 3136 | * Sockets created by Network.getSocketFactory().createSocket() and |
| 3137 | * performing network-specific host name resolutions via |
| 3138 | * {@link Network#getAllByName Network.getAllByName} is preferred to calling |
| 3139 | * {@code setProcessDefaultNetwork}. |
| 3140 | * |
| 3141 | * @param network The {@link Network} to bind the current process to, or {@code null} to clear |
| 3142 | * the current binding. |
| 3143 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 3144 | * @deprecated This function can throw {@link IllegalStateException}. Use |
| 3145 | * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork} |
| 3146 | * is a direct replacement. |
| 3147 | */ |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 3148 | public static boolean setProcessDefaultNetwork(Network network) { |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 3149 | int netId = (network == null) ? NETID_UNSET : network.netId; |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 3150 | if (netId == NetworkUtils.getBoundNetworkForProcess()) { |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 3151 | return true; |
| 3152 | } |
| 3153 | if (NetworkUtils.bindProcessToNetwork(netId)) { |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 3154 | // Set HTTP proxy system properties to match network. |
| 3155 | // TODO: Deprecate this static method and replace it with a non-static version. |
Lorenzo Colitti | ec4c555 | 2015-04-22 11:52:48 +0900 | [diff] [blame] | 3156 | try { |
| 3157 | Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy()); |
| 3158 | } catch (SecurityException e) { |
| 3159 | // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy. |
| 3160 | Log.e(TAG, "Can't set proxy properties", e); |
| 3161 | } |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 3162 | // Must flush DNS cache as new network may have different DNS resolutions. |
| 3163 | InetAddress.clearDnsCache(); |
| 3164 | // Must flush socket pool as idle sockets will be bound to previous network and may |
| 3165 | // cause subsequent fetches to be performed on old network. |
| 3166 | NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged(); |
| 3167 | return true; |
| 3168 | } else { |
| 3169 | return false; |
| 3170 | } |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | /** |
| 3174 | * Returns the {@link Network} currently bound to this process via |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 3175 | * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 3176 | * |
| 3177 | * @return {@code Network} to which this process is bound, or {@code null}. |
| 3178 | */ |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 3179 | public Network getBoundNetworkForProcess() { |
| 3180 | // Forcing callers to call thru non-static function ensures ConnectivityManager |
| 3181 | // instantiated. |
| 3182 | return getProcessDefaultNetwork(); |
| 3183 | } |
| 3184 | |
| 3185 | /** |
| 3186 | * Returns the {@link Network} currently bound to this process via |
| 3187 | * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. |
| 3188 | * |
| 3189 | * @return {@code Network} to which this process is bound, or {@code null}. |
| 3190 | * @deprecated Using this function can lead to other functions throwing |
| 3191 | * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead. |
| 3192 | * {@code getBoundNetworkForProcess} is a direct replacement. |
| 3193 | */ |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 3194 | public static Network getProcessDefaultNetwork() { |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 3195 | int netId = NetworkUtils.getBoundNetworkForProcess(); |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 3196 | if (netId == NETID_UNSET) return null; |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 3197 | return new Network(netId); |
| 3198 | } |
| 3199 | |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 3200 | private void unsupportedStartingFrom(int version) { |
| 3201 | if (Process.myUid() == Process.SYSTEM_UID) { |
| 3202 | // The getApplicationInfo() call we make below is not supported in system context, and |
| 3203 | // we want to allow the system to use these APIs anyway. |
| 3204 | return; |
| 3205 | } |
| 3206 | |
| 3207 | if (mContext.getApplicationInfo().targetSdkVersion >= version) { |
| 3208 | throw new UnsupportedOperationException( |
| 3209 | "This method is not supported in target SDK version " + version + " and above"); |
| 3210 | } |
| 3211 | } |
| 3212 | |
| 3213 | // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature, |
| 3214 | // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException. |
Lifu Tang | 30f95a7 | 2016-01-07 23:20:38 -0800 | [diff] [blame] | 3215 | // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 3216 | // remove these exemptions. Note that this check is not secure, and apps can still access these |
| 3217 | // functions by accessing ConnectivityService directly. However, it should be clear that doing |
| 3218 | // so is unsupported and may break in the future. http://b/22728205 |
| 3219 | private void checkLegacyRoutingApiAccess() { |
| 3220 | if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS") |
| 3221 | == PackageManager.PERMISSION_GRANTED) { |
| 3222 | return; |
| 3223 | } |
| 3224 | |
Dianne Hackborn | 692a244 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 3225 | unsupportedStartingFrom(VERSION_CODES.M); |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 3226 | } |
| 3227 | |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 3228 | /** |
| 3229 | * Binds host resolutions performed by this process to {@code network}. |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 3230 | * {@link #bindProcessToNetwork} takes precedence over this setting. |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 3231 | * |
| 3232 | * @param network The {@link Network} to bind host resolutions from the current process to, or |
| 3233 | * {@code null} to clear the current binding. |
| 3234 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 3235 | * @hide |
| 3236 | * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}. |
| 3237 | */ |
| 3238 | public static boolean setProcessDefaultNetworkForHostResolution(Network network) { |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 3239 | return NetworkUtils.bindProcessToNetworkForHostResolution( |
| 3240 | network == null ? NETID_UNSET : network.netId); |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 3241 | } |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 3242 | |
| 3243 | /** |
| 3244 | * Device is not restricting metered network activity while application is running on |
| 3245 | * background. |
| 3246 | */ |
| 3247 | public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1; |
| 3248 | |
| 3249 | /** |
| 3250 | * Device is restricting metered network activity while application is running on background, |
| 3251 | * but application is allowed to bypass it. |
| 3252 | * <p> |
| 3253 | * In this state, application should take action to mitigate metered network access. |
| 3254 | * For example, a music streaming application should switch to a low-bandwidth bitrate. |
| 3255 | */ |
| 3256 | public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2; |
| 3257 | |
| 3258 | /** |
| 3259 | * Device is restricting metered network activity while application is running on background. |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 3260 | * <p> |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 3261 | * In this state, application should not try to use the network while running on background, |
| 3262 | * because it would be denied. |
| 3263 | */ |
| 3264 | public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3; |
| 3265 | |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 3266 | /** |
| 3267 | * A change in the background metered network activity restriction has occurred. |
| 3268 | * <p> |
| 3269 | * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction |
| 3270 | * applies to them. |
| 3271 | * <p> |
| 3272 | * This is only sent to registered receivers, not manifest receivers. |
| 3273 | */ |
| 3274 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 3275 | public static final String ACTION_RESTRICT_BACKGROUND_CHANGED = |
| 3276 | "android.net.conn.RESTRICT_BACKGROUND_CHANGED"; |
| 3277 | |
Felipe Leme | ecfccea | 2016-01-25 11:48:04 -0800 | [diff] [blame] | 3278 | /** @hide */ |
| 3279 | @Retention(RetentionPolicy.SOURCE) |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 3280 | @IntDef(flag = false, value = { |
| 3281 | RESTRICT_BACKGROUND_STATUS_DISABLED, |
| 3282 | RESTRICT_BACKGROUND_STATUS_WHITELISTED, |
| 3283 | RESTRICT_BACKGROUND_STATUS_ENABLED, |
| 3284 | }) |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 3285 | public @interface RestrictBackgroundStatus { |
| 3286 | } |
| 3287 | |
| 3288 | private INetworkPolicyManager getNetworkPolicyManager() { |
| 3289 | synchronized (this) { |
| 3290 | if (mNPManager != null) { |
| 3291 | return mNPManager; |
| 3292 | } |
| 3293 | mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager |
| 3294 | .getService(Context.NETWORK_POLICY_SERVICE)); |
| 3295 | return mNPManager; |
| 3296 | } |
| 3297 | } |
| 3298 | |
| 3299 | /** |
| 3300 | * Determines if the calling application is subject to metered network restrictions while |
| 3301 | * running on background. |
| 3302 | */ |
| 3303 | public @RestrictBackgroundStatus int getRestrictBackgroundStatus() { |
| 3304 | try { |
| 3305 | return getNetworkPolicyManager().getRestrictBackgroundByCaller(); |
| 3306 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3307 | throw e.rethrowFromSystemServer(); |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 3308 | } |
| 3309 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3310 | } |