ConnectivityManager.java revision c849cdf5cfde007ce591838c4e2b777fb4843c8d
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 */ 16 17package android.net; 18 19import android.annotation.SdkConstant; 20import android.annotation.SdkConstant.SdkConstantType; 21import android.os.Binder; 22import android.os.RemoteException; 23 24/** 25 * Class that answers queries about the state of network connectivity. It also 26 * notifies applications when network connectivity changes. Get an instance 27 * of this class by calling 28 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}. 29 * <p> 30 * The primary responsibilities of this class are to: 31 * <ol> 32 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li> 33 * <li>Send broadcast intents when network connectivity changes</li> 34 * <li>Attempt to "fail over" to another network when connectivity to a network 35 * is lost</li> 36 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained 37 * state of the available networks</li> 38 * </ol> 39 */ 40public class ConnectivityManager 41{ 42 /** 43 * A change in network connectivity has occurred. A connection has either 44 * been established or lost. The NetworkInfo for the affected network is 45 * sent as an extra; it should be consulted to see what kind of 46 * connectivity event occurred. 47 * <p/> 48 * If this is a connection that was the result of failing over from a 49 * disconnected network, then the FAILOVER_CONNECTION boolean extra is 50 * set to true. 51 * <p/> 52 * For a loss of connectivity, if the connectivity manager is attempting 53 * to connect (or has already connected) to another network, the 54 * NetworkInfo for the new network is also passed as an extra. This lets 55 * any receivers of the broadcast know that they should not necessarily 56 * tell the user that no data traffic will be possible. Instead, the 57 * reciever should expect another broadcast soon, indicating either that 58 * the failover attempt succeeded (and so there is still overall data 59 * connectivity), or that the failover attempt failed, meaning that all 60 * connectivity has been lost. 61 * <p/> 62 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY 63 * is set to {@code true} if there are no connected networks at all. 64 */ 65 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; 66 /** 67 * The lookup key for a {@link NetworkInfo} object. Retrieve with 68 * {@link android.content.Intent#getParcelableExtra(String)}. 69 */ 70 public static final String EXTRA_NETWORK_INFO = "networkInfo"; 71 /** 72 * The lookup key for a boolean that indicates whether a connect event 73 * is for a network to which the connectivity manager was failing over 74 * following a disconnect on another network. 75 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. 76 */ 77 public static final String EXTRA_IS_FAILOVER = "isFailover"; 78 /** 79 * The lookup key for a {@link NetworkInfo} object. This is supplied when 80 * there is another network that it may be possible to connect to. Retrieve with 81 * {@link android.content.Intent#getParcelableExtra(String)}. 82 */ 83 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork"; 84 /** 85 * The lookup key for a boolean that indicates whether there is a 86 * complete lack of connectivity, i.e., no network is available. 87 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. 88 */ 89 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity"; 90 /** 91 * The lookup key for a string that indicates why an attempt to connect 92 * to a network failed. The string has no particular structure. It is 93 * intended to be used in notifications presented to users. Retrieve 94 * it with {@link android.content.Intent#getStringExtra(String)}. 95 */ 96 public static final String EXTRA_REASON = "reason"; 97 /** 98 * The lookup key for a string that provides optionally supplied 99 * extra information about the network state. The information 100 * may be passed up from the lower networking layers, and its 101 * meaning may be specific to a particular network type. Retrieve 102 * it with {@link android.content.Intent#getStringExtra(String)}. 103 */ 104 public static final String EXTRA_EXTRA_INFO = "extraInfo"; 105 106 /** 107 * Broadcast Action: The setting for background data usage has changed 108 * values. Use {@link #getBackgroundDataSetting()} to get the current value. 109 * <p> 110 * If an application uses the network in the background, it should listen 111 * for this broadcast and stop using the background data if the value is 112 * false. 113 */ 114 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 115 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED = 116 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"; 117 118 /** 119 * The Default Mobile data connection. When active, all data traffic 120 * will use this connection by default. Should not coexist with other 121 * default connections. 122 */ 123 public static final int TYPE_MOBILE = 0; 124 /** 125 * The Default WIFI data connection. When active, all data traffic 126 * will use this connection by default. Should not coexist with other 127 * default connections. 128 */ 129 public static final int TYPE_WIFI = 1; 130 /** 131 * An MMS-specific Mobile data connection. This connection may be the 132 * same as {@link #TYPEMOBILE} but it may be different. This is used 133 * by applications needing to talk to the carrier's Multimedia Messaging 134 * Service servers. It may coexist with default data connections. 135 */ 136 public static final int TYPE_MOBILE_MMS = 2; 137 /** 138 * A SUPL-specific Mobile data connection. This connection may be the 139 * same as {@link #TYPEMOBILE} but it may be different. This is used 140 * by applications needing to talk to the carrier's Secure User Plane 141 * Location servers for help locating the device. It may coexist with 142 * default data connections. 143 */ 144 public static final int TYPE_MOBILE_SUPL = 3; 145 /** 146 * A DUN-specific Mobile data connection. This connection may be the 147 * same as {@link #TYPEMOBILE} but it may be different. This is used 148 * by applicaitons performing a Dial Up Networking bridge so that 149 * the carrier is aware of DUN traffic. It may coexist with default data 150 * connections. 151 */ 152 public static final int TYPE_MOBILE_DUN = 4; 153 /** 154 * A High Priority Mobile data connection. This connection is typically 155 * the same as {@link #TYPEMOBILE} but the routing setup is different. 156 * Only requesting processes will have access to the Mobile DNS servers 157 * and only IP's explicitly requested via {@link #requestRouteToHost} 158 * will route over this interface if a default route exists. 159 */ 160 public static final int TYPE_MOBILE_HIPRI = 5; 161 /** {@hide} */ 162 public static final int MAX_RADIO_TYPE = TYPE_WIFI; 163 /** {@hide} */ 164 public static final int MAX_NETWORK_TYPE = TYPE_MOBILE_HIPRI; 165 166 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI; 167 168 private IConnectivityManager mService; 169 170 static public boolean isNetworkTypeValid(int networkType) { 171 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE; 172 } 173 174 public void setNetworkPreference(int preference) { 175 try { 176 mService.setNetworkPreference(preference); 177 } catch (RemoteException e) { 178 } 179 } 180 181 public int getNetworkPreference() { 182 try { 183 return mService.getNetworkPreference(); 184 } catch (RemoteException e) { 185 return -1; 186 } 187 } 188 189 public NetworkInfo getActiveNetworkInfo() { 190 try { 191 return mService.getActiveNetworkInfo(); 192 } catch (RemoteException e) { 193 return null; 194 } 195 } 196 197 public NetworkInfo getNetworkInfo(int networkType) { 198 try { 199 return mService.getNetworkInfo(networkType); 200 } catch (RemoteException e) { 201 return null; 202 } 203 } 204 205 public NetworkInfo[] getAllNetworkInfo() { 206 try { 207 return mService.getAllNetworkInfo(); 208 } catch (RemoteException e) { 209 return null; 210 } 211 } 212 213 /** {@hide} */ 214 public boolean setRadios(boolean turnOn) { 215 try { 216 return mService.setRadios(turnOn); 217 } catch (RemoteException e) { 218 return false; 219 } 220 } 221 222 /** {@hide} */ 223 public boolean setRadio(int networkType, boolean turnOn) { 224 try { 225 return mService.setRadio(networkType, turnOn); 226 } catch (RemoteException e) { 227 return false; 228 } 229 } 230 231 /** 232 * Tells the underlying networking system that the caller wants to 233 * begin using the named feature. The interpretation of {@code feature} 234 * is completely up to each networking implementation. 235 * @param networkType specifies which network the request pertains to 236 * @param feature the name of the feature to be used 237 * @return an integer value representing the outcome of the request. 238 * The interpretation of this value is specific to each networking 239 * implementation+feature combination, except that the value {@code -1} 240 * always indicates failure. 241 */ 242 public int startUsingNetworkFeature(int networkType, String feature) { 243 try { 244 return mService.startUsingNetworkFeature(networkType, feature, 245 new Binder()); 246 } catch (RemoteException e) { 247 return -1; 248 } 249 } 250 251 /** 252 * Tells the underlying networking system that the caller is finished 253 * using the named feature. The interpretation of {@code feature} 254 * is completely up to each networking implementation. 255 * @param networkType specifies which network the request pertains to 256 * @param feature the name of the feature that is no longer needed 257 * @return an integer value representing the outcome of the request. 258 * The interpretation of this value is specific to each networking 259 * implementation+feature combination, except that the value {@code -1} 260 * always indicates failure. 261 */ 262 public int stopUsingNetworkFeature(int networkType, String feature) { 263 try { 264 return mService.stopUsingNetworkFeature(networkType, feature); 265 } catch (RemoteException e) { 266 return -1; 267 } 268 } 269 270 /** 271 * Ensure that a network route exists to deliver traffic to the specified 272 * host via the specified network interface. An attempt to add a route that 273 * already exists is ignored, but treated as successful. 274 * @param networkType the type of the network over which traffic to the specified 275 * host is to be routed 276 * @param hostAddress the IP address of the host to which the route is desired 277 * @return {@code true} on success, {@code false} on failure 278 */ 279 public boolean requestRouteToHost(int networkType, int hostAddress) { 280 try { 281 return mService.requestRouteToHost(networkType, hostAddress); 282 } catch (RemoteException e) { 283 return false; 284 } 285 } 286 287 /** 288 * Returns the value of the setting for background data usage. If false, 289 * applications should not use the network if the application is not in the 290 * foreground. Developers should respect this setting, and check the value 291 * of this before performing any background data operations. 292 * <p> 293 * All applications that have background services that use the network 294 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}. 295 * 296 * @return Whether background data usage is allowed. 297 */ 298 public boolean getBackgroundDataSetting() { 299 try { 300 return mService.getBackgroundDataSetting(); 301 } catch (RemoteException e) { 302 // Err on the side of safety 303 return false; 304 } 305 } 306 307 /** 308 * Sets the value of the setting for background data usage. 309 * 310 * @param allowBackgroundData Whether an application should use data while 311 * it is in the background. 312 * 313 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING 314 * @see #getBackgroundDataSetting() 315 * @hide 316 */ 317 public void setBackgroundDataSetting(boolean allowBackgroundData) { 318 try { 319 mService.setBackgroundDataSetting(allowBackgroundData); 320 } catch (RemoteException e) { 321 } 322 } 323 324 /** 325 * Don't allow use of default constructor. 326 */ 327 @SuppressWarnings({"UnusedDeclaration"}) 328 private ConnectivityManager() { 329 } 330 331 /** 332 * {@hide} 333 */ 334 public ConnectivityManager(IConnectivityManager service) { 335 if (service == null) { 336 throw new IllegalArgumentException( 337 "ConnectivityManager() cannot be constructed with null service"); 338 } 339 mService = service; 340 } 341} 342