ConnectivityManager.java revision e285b434f4cc0f719e189e472bb3f83865b58b7b
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 */
16package android.net;
17
18import static com.android.internal.util.Preconditions.checkNotNull;
19
20import android.annotation.SdkConstant;
21import android.annotation.SdkConstant.SdkConstantType;
22import android.app.PendingIntent;
23import android.content.Context;
24import android.content.Intent;
25import android.net.NetworkUtils;
26import android.os.Binder;
27import android.os.Build.VERSION_CODES;
28import android.os.Handler;
29import android.os.HandlerThread;
30import android.os.IBinder;
31import android.os.INetworkActivityListener;
32import android.os.INetworkManagementService;
33import android.os.Looper;
34import android.os.Message;
35import android.os.Messenger;
36import android.os.RemoteException;
37import android.os.ServiceManager;
38import android.provider.Settings;
39import android.telephony.SubscriptionManager;
40import android.util.ArrayMap;
41import android.util.Log;
42
43import com.android.internal.telephony.ITelephony;
44import com.android.internal.telephony.PhoneConstants;
45import com.android.internal.util.Protocol;
46
47import java.net.InetAddress;
48import java.util.concurrent.atomic.AtomicInteger;
49import java.util.HashMap;
50
51import libcore.net.event.NetworkEventDispatcher;
52
53/**
54 * Class that answers queries about the state of network connectivity. It also
55 * notifies applications when network connectivity changes. Get an instance
56 * of this class by calling
57 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
58 * <p>
59 * The primary responsibilities of this class are to:
60 * <ol>
61 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
62 * <li>Send broadcast intents when network connectivity changes</li>
63 * <li>Attempt to "fail over" to another network when connectivity to a network
64 * is lost</li>
65 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
66 * state of the available networks</li>
67 * <li>Provide an API that allows applications to request and select networks for their data
68 * traffic</li>
69 * </ol>
70 */
71public class ConnectivityManager {
72    private static final String TAG = "ConnectivityManager";
73
74    /**
75     * A change in network connectivity has occurred. A default connection has either
76     * been established or lost. The NetworkInfo for the affected network is
77     * sent as an extra; it should be consulted to see what kind of
78     * connectivity event occurred.
79     * <p/>
80     * If this is a connection that was the result of failing over from a
81     * disconnected network, then the FAILOVER_CONNECTION boolean extra is
82     * set to true.
83     * <p/>
84     * For a loss of connectivity, if the connectivity manager is attempting
85     * to connect (or has already connected) to another network, the
86     * NetworkInfo for the new network is also passed as an extra. This lets
87     * any receivers of the broadcast know that they should not necessarily
88     * tell the user that no data traffic will be possible. Instead, the
89     * receiver should expect another broadcast soon, indicating either that
90     * the failover attempt succeeded (and so there is still overall data
91     * connectivity), or that the failover attempt failed, meaning that all
92     * connectivity has been lost.
93     * <p/>
94     * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
95     * is set to {@code true} if there are no connected networks at all.
96     */
97    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
98    public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
99
100    /**
101     * The lookup key for a {@link NetworkInfo} object. Retrieve with
102     * {@link android.content.Intent#getParcelableExtra(String)}.
103     *
104     * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
105     *             should always obtain network information through
106     *             {@link #getActiveNetworkInfo()} or
107     *             {@link #getAllNetworkInfo()}.
108     * @see #EXTRA_NETWORK_TYPE
109     */
110    @Deprecated
111    public static final String EXTRA_NETWORK_INFO = "networkInfo";
112
113    /**
114     * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
115     * Can be used with {@link #getNetworkInfo(int)} to get {@link NetworkInfo}
116     * state based on the calling application.
117     *
118     * @see android.content.Intent#getIntExtra(String, int)
119     */
120    public static final String EXTRA_NETWORK_TYPE = "networkType";
121
122    /**
123     * The lookup key for a boolean that indicates whether a connect event
124     * is for a network to which the connectivity manager was failing over
125     * following a disconnect on another network.
126     * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
127     */
128    public static final String EXTRA_IS_FAILOVER = "isFailover";
129    /**
130     * The lookup key for a {@link NetworkInfo} object. This is supplied when
131     * there is another network that it may be possible to connect to. Retrieve with
132     * {@link android.content.Intent#getParcelableExtra(String)}.
133     */
134    public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
135    /**
136     * The lookup key for a boolean that indicates whether there is a
137     * complete lack of connectivity, i.e., no network is available.
138     * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
139     */
140    public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
141    /**
142     * The lookup key for a string that indicates why an attempt to connect
143     * to a network failed. The string has no particular structure. It is
144     * intended to be used in notifications presented to users. Retrieve
145     * it with {@link android.content.Intent#getStringExtra(String)}.
146     */
147    public static final String EXTRA_REASON = "reason";
148    /**
149     * The lookup key for a string that provides optionally supplied
150     * extra information about the network state. The information
151     * may be passed up from the lower networking layers, and its
152     * meaning may be specific to a particular network type. Retrieve
153     * it with {@link android.content.Intent#getStringExtra(String)}.
154     */
155    public static final String EXTRA_EXTRA_INFO = "extraInfo";
156    /**
157     * The lookup key for an int that provides information about
158     * our connection to the internet at large.  0 indicates no connection,
159     * 100 indicates a great connection.  Retrieve it with
160     * {@link android.content.Intent#getIntExtra(String, int)}.
161     * {@hide}
162     */
163    public static final String EXTRA_INET_CONDITION = "inetCondition";
164
165    /**
166     * Broadcast action to indicate the change of data activity status
167     * (idle or active) on a network in a recent period.
168     * The network becomes active when data transmission is started, or
169     * idle if there is no data transmission for a period of time.
170     * {@hide}
171     */
172    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
173    public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
174    /**
175     * The lookup key for an enum that indicates the network device type on which this data activity
176     * change happens.
177     * {@hide}
178     */
179    public static final String EXTRA_DEVICE_TYPE = "deviceType";
180    /**
181     * The lookup key for a boolean that indicates the device is active or not. {@code true} means
182     * it is actively sending or receiving data and {@code false} means it is idle.
183     * {@hide}
184     */
185    public static final String EXTRA_IS_ACTIVE = "isActive";
186    /**
187     * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
188     * {@hide}
189     */
190    public static final String EXTRA_REALTIME_NS = "tsNanos";
191
192    /**
193     * Broadcast Action: The setting for background data usage has changed
194     * values. Use {@link #getBackgroundDataSetting()} to get the current value.
195     * <p>
196     * If an application uses the network in the background, it should listen
197     * for this broadcast and stop using the background data if the value is
198     * {@code false}.
199     * <p>
200     *
201     * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
202     *             of background data depends on several combined factors, and
203     *             this broadcast is no longer sent. Instead, when background
204     *             data is unavailable, {@link #getActiveNetworkInfo()} will now
205     *             appear disconnected. During first boot after a platform
206     *             upgrade, this broadcast will be sent once if
207     *             {@link #getBackgroundDataSetting()} was {@code false} before
208     *             the upgrade.
209     */
210    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
211    @Deprecated
212    public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
213            "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
214
215    /**
216     * Broadcast Action: The network connection may not be good
217     * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
218     * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
219     * the network and it's condition.
220     * @hide
221     */
222    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
223    public static final String INET_CONDITION_ACTION =
224            "android.net.conn.INET_CONDITION_ACTION";
225
226    /**
227     * Broadcast Action: A tetherable connection has come or gone.
228     * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
229     * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
230     * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
231     * the current state of tethering.  Each include a list of
232     * interface names in that state (may be empty).
233     * @hide
234     */
235    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
236    public static final String ACTION_TETHER_STATE_CHANGED =
237            "android.net.conn.TETHER_STATE_CHANGED";
238
239    /**
240     * @hide
241     * gives a String[] listing all the interfaces configured for
242     * tethering and currently available for tethering.
243     */
244    public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
245
246    /**
247     * @hide
248     * gives a String[] listing all the interfaces currently tethered
249     * (ie, has dhcp support and packets potentially forwarded/NATed)
250     */
251    public static final String EXTRA_ACTIVE_TETHER = "activeArray";
252
253    /**
254     * @hide
255     * gives a String[] listing all the interfaces we tried to tether and
256     * failed.  Use {@link #getLastTetherError} to find the error code
257     * for any interfaces listed here.
258     */
259    public static final String EXTRA_ERRORED_TETHER = "erroredArray";
260
261    /**
262     * Broadcast Action: The captive portal tracker has finished its test.
263     * Sent only while running Setup Wizard, in lieu of showing a user
264     * notification.
265     * @hide
266     */
267    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
268    public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
269            "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
270    /**
271     * The lookup key for a boolean that indicates whether a captive portal was detected.
272     * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
273     * @hide
274     */
275    public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
276
277    /**
278     * Action used to display a dialog that asks the user whether to connect to a network that is
279     * not validated. This intent is used to start the dialog in settings via startActivity.
280     *
281     * @hide
282     */
283    public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
284
285    /**
286     * The absence of a connection type.
287     * @hide
288     */
289    public static final int TYPE_NONE        = -1;
290
291    /**
292     * The Mobile data connection.  When active, all data traffic
293     * will use this network type's interface by default
294     * (it has a default route)
295     */
296    public static final int TYPE_MOBILE      = 0;
297    /**
298     * The WIFI data connection.  When active, all data traffic
299     * will use this network type's interface by default
300     * (it has a default route).
301     */
302    public static final int TYPE_WIFI        = 1;
303    /**
304     * An MMS-specific Mobile data connection.  This network type may use the
305     * same network interface as {@link #TYPE_MOBILE} or it may use a different
306     * one.  This is used by applications needing to talk to the carrier's
307     * Multimedia Messaging Service servers.
308     *
309     * @deprecated Applications should instead use {@link #requestNetwork} to request a network that
310     *         provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
311     */
312    public static final int TYPE_MOBILE_MMS  = 2;
313    /**
314     * A SUPL-specific Mobile data connection.  This network type may use the
315     * same network interface as {@link #TYPE_MOBILE} or it may use a different
316     * one.  This is used by applications needing to talk to the carrier's
317     * Secure User Plane Location servers for help locating the device.
318     *
319     * @deprecated Applications should instead use {@link #requestNetwork} to request a network that
320     *         provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
321     */
322    public static final int TYPE_MOBILE_SUPL = 3;
323    /**
324     * A DUN-specific Mobile data connection.  This network type may use the
325     * same network interface as {@link #TYPE_MOBILE} or it may use a different
326     * one.  This is sometimes by the system when setting up an upstream connection
327     * for tethering so that the carrier is aware of DUN traffic.
328     */
329    public static final int TYPE_MOBILE_DUN  = 4;
330    /**
331     * A High Priority Mobile data connection.  This network type uses the
332     * same network interface as {@link #TYPE_MOBILE} but the routing setup
333     * is different.
334     *
335     * @deprecated Applications should instead use {@link #requestNetwork} to request a network that
336     *         uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport.
337     */
338    public static final int TYPE_MOBILE_HIPRI = 5;
339    /**
340     * The WiMAX data connection.  When active, all data traffic
341     * will use this network type's interface by default
342     * (it has a default route).
343     */
344    public static final int TYPE_WIMAX       = 6;
345
346    /**
347     * The Bluetooth data connection.  When active, all data traffic
348     * will use this network type's interface by default
349     * (it has a default route).
350     */
351    public static final int TYPE_BLUETOOTH   = 7;
352
353    /**
354     * Dummy data connection.  This should not be used on shipping devices.
355     */
356    public static final int TYPE_DUMMY       = 8;
357
358    /**
359     * The Ethernet data connection.  When active, all data traffic
360     * will use this network type's interface by default
361     * (it has a default route).
362     */
363    public static final int TYPE_ETHERNET    = 9;
364
365    /**
366     * Over the air Administration.
367     * {@hide}
368     */
369    public static final int TYPE_MOBILE_FOTA = 10;
370
371    /**
372     * IP Multimedia Subsystem.
373     * {@hide}
374     */
375    public static final int TYPE_MOBILE_IMS  = 11;
376
377    /**
378     * Carrier Branded Services.
379     * {@hide}
380     */
381    public static final int TYPE_MOBILE_CBS  = 12;
382
383    /**
384     * A Wi-Fi p2p connection. Only requesting processes will have access to
385     * the peers connected.
386     * {@hide}
387     */
388    public static final int TYPE_WIFI_P2P    = 13;
389
390    /**
391     * The network to use for initially attaching to the network
392     * {@hide}
393     */
394    public static final int TYPE_MOBILE_IA = 14;
395
396    /**
397     * Emergency PDN connection for emergency calls
398     * {@hide}
399     */
400    public static final int TYPE_MOBILE_EMERGENCY = 15;
401
402    /**
403     * The network that uses proxy to achieve connectivity.
404     * {@hide}
405     */
406    public static final int TYPE_PROXY = 16;
407
408    /**
409     * A virtual network using one or more native bearers.
410     * It may or may not be providing security services.
411     */
412    public static final int TYPE_VPN = 17;
413
414    /** {@hide} */
415    public static final int MAX_RADIO_TYPE   = TYPE_VPN;
416
417    /** {@hide} */
418    public static final int MAX_NETWORK_TYPE = TYPE_VPN;
419
420    /**
421     * If you want to set the default network preference,you can directly
422     * change the networkAttributes array in framework's config.xml.
423     *
424     * @deprecated Since we support so many more networks now, the single
425     *             network default network preference can't really express
426     *             the hierarchy.  Instead, the default is defined by the
427     *             networkAttributes in config.xml.  You can determine
428     *             the current value by calling {@link #getNetworkPreference()}
429     *             from an App.
430     */
431    @Deprecated
432    public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
433
434    /**
435     * @hide
436     */
437    public final static int REQUEST_ID_UNSET = 0;
438
439    /**
440     * A NetID indicating no Network is selected.
441     * Keep in sync with bionic/libc/dns/include/resolv_netid.h
442     * @hide
443     */
444    public static final int NETID_UNSET = 0;
445
446    private final IConnectivityManager mService;
447    /**
448     * A kludge to facilitate static access where a Context pointer isn't available, like in the
449     * case of the static set/getProcessDefaultNetwork methods and from the Network class.
450     * TODO: Remove this after deprecating the static methods in favor of non-static methods or
451     * methods that take a Context argument.
452     */
453    private static ConnectivityManager sInstance;
454
455    private INetworkManagementService mNMService;
456
457    /**
458     * Tests if a given integer represents a valid network type.
459     * @param networkType the type to be tested
460     * @return a boolean.  {@code true} if the type is valid, else {@code false}
461     */
462    public static boolean isNetworkTypeValid(int networkType) {
463        return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
464    }
465
466    /**
467     * Returns a non-localized string representing a given network type.
468     * ONLY used for debugging output.
469     * @param type the type needing naming
470     * @return a String for the given type, or a string version of the type ("87")
471     * if no name is known.
472     * {@hide}
473     */
474    public static String getNetworkTypeName(int type) {
475        switch (type) {
476            case TYPE_MOBILE:
477                return "MOBILE";
478            case TYPE_WIFI:
479                return "WIFI";
480            case TYPE_MOBILE_MMS:
481                return "MOBILE_MMS";
482            case TYPE_MOBILE_SUPL:
483                return "MOBILE_SUPL";
484            case TYPE_MOBILE_DUN:
485                return "MOBILE_DUN";
486            case TYPE_MOBILE_HIPRI:
487                return "MOBILE_HIPRI";
488            case TYPE_WIMAX:
489                return "WIMAX";
490            case TYPE_BLUETOOTH:
491                return "BLUETOOTH";
492            case TYPE_DUMMY:
493                return "DUMMY";
494            case TYPE_ETHERNET:
495                return "ETHERNET";
496            case TYPE_MOBILE_FOTA:
497                return "MOBILE_FOTA";
498            case TYPE_MOBILE_IMS:
499                return "MOBILE_IMS";
500            case TYPE_MOBILE_CBS:
501                return "MOBILE_CBS";
502            case TYPE_WIFI_P2P:
503                return "WIFI_P2P";
504            case TYPE_MOBILE_IA:
505                return "MOBILE_IA";
506            case TYPE_MOBILE_EMERGENCY:
507                return "MOBILE_EMERGENCY";
508            case TYPE_PROXY:
509                return "PROXY";
510            case TYPE_VPN:
511                return "VPN";
512            default:
513                return Integer.toString(type);
514        }
515    }
516
517    /**
518     * Checks if a given type uses the cellular data connection.
519     * This should be replaced in the future by a network property.
520     * @param networkType the type to check
521     * @return a boolean - {@code true} if uses cellular network, else {@code false}
522     * {@hide}
523     */
524    public static boolean isNetworkTypeMobile(int networkType) {
525        switch (networkType) {
526            case TYPE_MOBILE:
527            case TYPE_MOBILE_MMS:
528            case TYPE_MOBILE_SUPL:
529            case TYPE_MOBILE_DUN:
530            case TYPE_MOBILE_HIPRI:
531            case TYPE_MOBILE_FOTA:
532            case TYPE_MOBILE_IMS:
533            case TYPE_MOBILE_CBS:
534            case TYPE_MOBILE_IA:
535            case TYPE_MOBILE_EMERGENCY:
536                return true;
537            default:
538                return false;
539        }
540    }
541
542    /**
543     * Checks if the given network type is backed by a Wi-Fi radio.
544     *
545     * @hide
546     */
547    public static boolean isNetworkTypeWifi(int networkType) {
548        switch (networkType) {
549            case TYPE_WIFI:
550            case TYPE_WIFI_P2P:
551                return true;
552            default:
553                return false;
554        }
555    }
556
557    /**
558     * Specifies the preferred network type.  When the device has more
559     * than one type available the preferred network type will be used.
560     *
561     * @param preference the network type to prefer over all others.  It is
562     *         unspecified what happens to the old preferred network in the
563     *         overall ordering.
564     * @deprecated Functionality has been removed as it no longer makes sense,
565     *             with many more than two networks - we'd need an array to express
566     *             preference.  Instead we use dynamic network properties of
567     *             the networks to describe their precedence.
568     */
569    public void setNetworkPreference(int preference) {
570    }
571
572    /**
573     * Retrieves the current preferred network type.
574     *
575     * @return an integer representing the preferred network type
576     *
577     * <p>This method requires the caller to hold the permission
578     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
579     * @deprecated Functionality has been removed as it no longer makes sense,
580     *             with many more than two networks - we'd need an array to express
581     *             preference.  Instead we use dynamic network properties of
582     *             the networks to describe their precedence.
583     */
584    public int getNetworkPreference() {
585        return TYPE_NONE;
586    }
587
588    /**
589     * Returns details about the currently active default data network. When
590     * connected, this network is the default route for outgoing connections.
591     * You should always check {@link NetworkInfo#isConnected()} before initiating
592     * network traffic. This may return {@code null} when there is no default
593     * network.
594     *
595     * @return a {@link NetworkInfo} object for the current default network
596     *        or {@code null} if no default network is currently active
597     *
598     * <p>This method requires the caller to hold the permission
599     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
600     */
601    public NetworkInfo getActiveNetworkInfo() {
602        try {
603            return mService.getActiveNetworkInfo();
604        } catch (RemoteException e) {
605            return null;
606        }
607    }
608
609    /**
610     * Returns a {@link Network} object corresponding to the currently active
611     * default data network.  In the event that the current active default data
612     * network disconnects, the returned {@code Network} object will no longer
613     * be usable.  This will return {@code null} when there is no default
614     * network.
615     *
616     * @return a {@link Network} object for the current default network or
617     *        {@code null} if no default network is currently active
618     *
619     * <p>This method requires the caller to hold the permission
620     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
621     */
622    public Network getActiveNetwork() {
623        try {
624            return mService.getActiveNetwork();
625        } catch (RemoteException e) {
626            return null;
627        }
628    }
629
630    /**
631     * Returns details about the currently active default data network
632     * for a given uid.  This is for internal use only to avoid spying
633     * other apps.
634     *
635     * @return a {@link NetworkInfo} object for the current default network
636     *        for the given uid or {@code null} if no default network is
637     *        available for the specified uid.
638     *
639     * <p>This method requires the caller to hold the permission
640     * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
641     * {@hide}
642     */
643    public NetworkInfo getActiveNetworkInfoForUid(int uid) {
644        try {
645            return mService.getActiveNetworkInfoForUid(uid);
646        } catch (RemoteException e) {
647            return null;
648        }
649    }
650
651    /**
652     * Returns connection status information about a particular
653     * network type.
654     *
655     * @param networkType integer specifying which networkType in
656     *        which you're interested.
657     * @return a {@link NetworkInfo} object for the requested
658     *        network type or {@code null} if the type is not
659     *        supported by the device.
660     *
661     * <p>This method requires the caller to hold the permission
662     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
663     */
664    public NetworkInfo getNetworkInfo(int networkType) {
665        try {
666            return mService.getNetworkInfo(networkType);
667        } catch (RemoteException e) {
668            return null;
669        }
670    }
671
672    /**
673     * Returns connection status information about a particular
674     * Network.
675     *
676     * @param network {@link Network} specifying which network
677     *        in which you're interested.
678     * @return a {@link NetworkInfo} object for the requested
679     *        network or {@code null} if the {@code Network}
680     *        is not valid.
681     *
682     * <p>This method requires the caller to hold the permission
683     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
684     */
685    public NetworkInfo getNetworkInfo(Network network) {
686        try {
687            return mService.getNetworkInfoForNetwork(network);
688        } catch (RemoteException e) {
689            return null;
690        }
691    }
692
693    /**
694     * Returns connection status information about all network
695     * types supported by the device.
696     *
697     * @return an array of {@link NetworkInfo} objects.  Check each
698     * {@link NetworkInfo#getType} for which type each applies.
699     *
700     * <p>This method requires the caller to hold the permission
701     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
702     */
703    public NetworkInfo[] getAllNetworkInfo() {
704        try {
705            return mService.getAllNetworkInfo();
706        } catch (RemoteException e) {
707            return null;
708        }
709    }
710
711    /**
712     * Returns the {@link Network} object currently serving a given type, or
713     * null if the given type is not connected.
714     *
715     * <p>This method requires the caller to hold the permission
716     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
717     *
718     * @hide
719     */
720    public Network getNetworkForType(int networkType) {
721        try {
722            return mService.getNetworkForType(networkType);
723        } catch (RemoteException e) {
724            return null;
725        }
726    }
727
728    /**
729     * Returns an array of all {@link Network} currently tracked by the
730     * framework.
731     *
732     * @return an array of {@link Network} objects.
733     *
734     * <p>This method requires the caller to hold the permission
735     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
736     */
737    public Network[] getAllNetworks() {
738        try {
739            return mService.getAllNetworks();
740        } catch (RemoteException e) {
741            return null;
742        }
743    }
744
745    /**
746     * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
747     * the Networks that applications run by the given user will use by default.
748     * @hide
749     */
750    public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
751        try {
752            return mService.getDefaultNetworkCapabilitiesForUser(userId);
753        } catch (RemoteException e) {
754            return null;
755        }
756    }
757
758    /**
759     * Returns details about the Provisioning or currently active default data network. When
760     * connected, this network is the default route for outgoing connections.
761     * You should always check {@link NetworkInfo#isConnected()} before initiating
762     * network traffic. This may return {@code null} when there is no default
763     * network.
764     *
765     * @return a {@link NetworkInfo} object for the current default network
766     *        or {@code null} if no default network is currently active
767     *
768     * <p>This method requires the caller to hold the permission
769     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
770     *
771     * {@hide}
772     */
773    public NetworkInfo getProvisioningOrActiveNetworkInfo() {
774        try {
775            return mService.getProvisioningOrActiveNetworkInfo();
776        } catch (RemoteException e) {
777            return null;
778        }
779    }
780
781    /**
782     * Returns the IP information for the current default network.
783     *
784     * @return a {@link LinkProperties} object describing the IP info
785     *        for the current default network, or {@code null} if there
786     *        is no current default network.
787     *
788     * <p>This method requires the caller to hold the permission
789     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
790     * {@hide}
791     */
792    public LinkProperties getActiveLinkProperties() {
793        try {
794            return mService.getActiveLinkProperties();
795        } catch (RemoteException e) {
796            return null;
797        }
798    }
799
800    /**
801     * Returns the IP information for a given network type.
802     *
803     * @param networkType the network type of interest.
804     * @return a {@link LinkProperties} object describing the IP info
805     *        for the given networkType, or {@code null} if there is
806     *        no current default network.
807     *
808     * <p>This method requires the caller to hold the permission
809     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
810     * {@hide}
811     */
812    public LinkProperties getLinkProperties(int networkType) {
813        try {
814            return mService.getLinkPropertiesForType(networkType);
815        } catch (RemoteException e) {
816            return null;
817        }
818    }
819
820    /**
821     * Get the {@link LinkProperties} for the given {@link Network}.  This
822     * will return {@code null} if the network is unknown.
823     *
824     * @param network The {@link Network} object identifying the network in question.
825     * @return The {@link LinkProperties} for the network, or {@code null}.
826     **/
827    public LinkProperties getLinkProperties(Network network) {
828        try {
829            return mService.getLinkProperties(network);
830        } catch (RemoteException e) {
831            return null;
832        }
833    }
834
835    /**
836     * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}.  This
837     * will return {@code null} if the network is unknown.
838     *
839     * @param network The {@link Network} object identifying the network in question.
840     * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
841     */
842    public NetworkCapabilities getNetworkCapabilities(Network network) {
843        try {
844            return mService.getNetworkCapabilities(network);
845        } catch (RemoteException e) {
846            return null;
847        }
848    }
849
850    /**
851     * Tells the underlying networking system that the caller wants to
852     * begin using the named feature. The interpretation of {@code feature}
853     * is completely up to each networking implementation.
854     * <p>This method requires the caller to hold the permission
855     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
856     * @param networkType specifies which network the request pertains to
857     * @param feature the name of the feature to be used
858     * @return an integer value representing the outcome of the request.
859     * The interpretation of this value is specific to each networking
860     * implementation+feature combination, except that the value {@code -1}
861     * always indicates failure.
862     *
863     * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
864     * @removed
865     */
866    public int startUsingNetworkFeature(int networkType, String feature) {
867        NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
868        if (netCap == null) {
869            Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
870                    feature);
871            return PhoneConstants.APN_REQUEST_FAILED;
872        }
873
874        NetworkRequest request = null;
875        synchronized (sLegacyRequests) {
876            LegacyRequest l = sLegacyRequests.get(netCap);
877            if (l != null) {
878                Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
879                renewRequestLocked(l);
880                if (l.currentNetwork != null) {
881                    return PhoneConstants.APN_ALREADY_ACTIVE;
882                } else {
883                    return PhoneConstants.APN_REQUEST_STARTED;
884                }
885            }
886
887            request = requestNetworkForFeatureLocked(netCap);
888        }
889        if (request != null) {
890            Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
891            return PhoneConstants.APN_REQUEST_STARTED;
892        } else {
893            Log.d(TAG, " request Failed");
894            return PhoneConstants.APN_REQUEST_FAILED;
895        }
896    }
897
898    /**
899     * Tells the underlying networking system that the caller is finished
900     * using the named feature. The interpretation of {@code feature}
901     * is completely up to each networking implementation.
902     * <p>This method requires the caller to hold the permission
903     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
904     * @param networkType specifies which network the request pertains to
905     * @param feature the name of the feature that is no longer needed
906     * @return an integer value representing the outcome of the request.
907     * The interpretation of this value is specific to each networking
908     * implementation+feature combination, except that the value {@code -1}
909     * always indicates failure.
910     *
911     * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
912     * @removed
913     */
914    public int stopUsingNetworkFeature(int networkType, String feature) {
915        NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
916        if (netCap == null) {
917            Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
918                    feature);
919            return -1;
920        }
921
922        if (removeRequestForFeature(netCap)) {
923            Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
924        }
925        return 1;
926    }
927
928    /**
929     * Removes the NET_CAPABILITY_NOT_RESTRICTED capability from the given
930     * NetworkCapabilities object if all the capabilities it provides are
931     * typically provided by restricted networks.
932     *
933     * TODO: consider:
934     * - Moving to NetworkCapabilities
935     * - Renaming it to guessRestrictedCapability and make it set the
936     *   restricted capability bit in addition to clearing it.
937     * @hide
938     */
939    public static void maybeMarkCapabilitiesRestricted(NetworkCapabilities nc) {
940        for (int capability : nc.getCapabilities()) {
941            switch (capability) {
942                case NetworkCapabilities.NET_CAPABILITY_CBS:
943                case NetworkCapabilities.NET_CAPABILITY_DUN:
944                case NetworkCapabilities.NET_CAPABILITY_EIMS:
945                case NetworkCapabilities.NET_CAPABILITY_FOTA:
946                case NetworkCapabilities.NET_CAPABILITY_IA:
947                case NetworkCapabilities.NET_CAPABILITY_IMS:
948                case NetworkCapabilities.NET_CAPABILITY_RCS:
949                case NetworkCapabilities.NET_CAPABILITY_XCAP:
950                case NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED: //there by default
951                    continue;
952                default:
953                    // At least one capability usually provided by unrestricted
954                    // networks. Conclude that this network is unrestricted.
955                    return;
956            }
957        }
958        // All the capabilities are typically provided by restricted networks.
959        // Conclude that this network is restricted.
960        nc.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
961    }
962
963    private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
964        if (networkType == TYPE_MOBILE) {
965            int cap = -1;
966            if ("enableMMS".equals(feature)) {
967                cap = NetworkCapabilities.NET_CAPABILITY_MMS;
968            } else if ("enableSUPL".equals(feature)) {
969                cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
970            } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
971                cap = NetworkCapabilities.NET_CAPABILITY_DUN;
972            } else if ("enableHIPRI".equals(feature)) {
973                cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
974            } else if ("enableFOTA".equals(feature)) {
975                cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
976            } else if ("enableIMS".equals(feature)) {
977                cap = NetworkCapabilities.NET_CAPABILITY_IMS;
978            } else if ("enableCBS".equals(feature)) {
979                cap = NetworkCapabilities.NET_CAPABILITY_CBS;
980            } else {
981                return null;
982            }
983            NetworkCapabilities netCap = new NetworkCapabilities();
984            netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
985            maybeMarkCapabilitiesRestricted(netCap);
986            return netCap;
987        } else if (networkType == TYPE_WIFI) {
988            if ("p2p".equals(feature)) {
989                NetworkCapabilities netCap = new NetworkCapabilities();
990                netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
991                netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
992                maybeMarkCapabilitiesRestricted(netCap);
993                return netCap;
994            }
995        }
996        return null;
997    }
998
999    /**
1000     * Guess what the network request was trying to say so that the resulting
1001     * network is accessible via the legacy (deprecated) API such as
1002     * requestRouteToHost.
1003     * This means we should try to be fairly preceise about transport and
1004     * capability but ignore things such as networkSpecifier.
1005     * If the request has more than one transport or capability it doesn't
1006     * match the old legacy requests (they selected only single transport/capability)
1007     * so this function cannot map the request to a single legacy type and
1008     * the resulting network will not be available to the legacy APIs.
1009     *
1010     * TODO - This should be removed when the legacy APIs are removed.
1011     */
1012    private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1013        if (netCap == null) {
1014            return TYPE_NONE;
1015        }
1016
1017        if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1018            return TYPE_NONE;
1019        }
1020
1021        String type = null;
1022        int result = TYPE_NONE;
1023
1024        if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1025            type = "enableCBS";
1026            result = TYPE_MOBILE_CBS;
1027        } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1028            type = "enableIMS";
1029            result = TYPE_MOBILE_IMS;
1030        } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1031            type = "enableFOTA";
1032            result = TYPE_MOBILE_FOTA;
1033        } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1034            type = "enableDUN";
1035            result = TYPE_MOBILE_DUN;
1036        } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1037            type = "enableSUPL";
1038            result = TYPE_MOBILE_SUPL;
1039        } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1040            type = "enableMMS";
1041            result = TYPE_MOBILE_MMS;
1042        } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1043            type = "enableHIPRI";
1044            result = TYPE_MOBILE_HIPRI;
1045        }
1046        if (type != null) {
1047            NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1048            if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1049                return result;
1050            }
1051        }
1052        return TYPE_NONE;
1053    }
1054
1055    private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1056        if (netCap == null) return TYPE_NONE;
1057        if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1058            return TYPE_MOBILE_CBS;
1059        }
1060        if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1061            return TYPE_MOBILE_IMS;
1062        }
1063        if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1064            return TYPE_MOBILE_FOTA;
1065        }
1066        if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1067            return TYPE_MOBILE_DUN;
1068        }
1069        if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1070            return TYPE_MOBILE_SUPL;
1071        }
1072        if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1073            return TYPE_MOBILE_MMS;
1074        }
1075        if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1076            return TYPE_MOBILE_HIPRI;
1077        }
1078        if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1079            return TYPE_WIFI_P2P;
1080        }
1081        return TYPE_NONE;
1082    }
1083
1084    private static class LegacyRequest {
1085        NetworkCapabilities networkCapabilities;
1086        NetworkRequest networkRequest;
1087        int expireSequenceNumber;
1088        Network currentNetwork;
1089        int delay = -1;
1090
1091        private void clearDnsBinding() {
1092            if (currentNetwork != null) {
1093                currentNetwork = null;
1094                setProcessDefaultNetworkForHostResolution(null);
1095            }
1096        }
1097
1098        NetworkCallback networkCallback = new NetworkCallback() {
1099            @Override
1100            public void onAvailable(Network network) {
1101                currentNetwork = network;
1102                Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
1103                setProcessDefaultNetworkForHostResolution(network);
1104            }
1105            @Override
1106            public void onLost(Network network) {
1107                if (network.equals(currentNetwork)) clearDnsBinding();
1108                Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1109            }
1110        };
1111    }
1112
1113    private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
1114            new HashMap<NetworkCapabilities, LegacyRequest>();
1115
1116    private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1117        synchronized (sLegacyRequests) {
1118            LegacyRequest l = sLegacyRequests.get(netCap);
1119            if (l != null) return l.networkRequest;
1120        }
1121        return null;
1122    }
1123
1124    private void renewRequestLocked(LegacyRequest l) {
1125        l.expireSequenceNumber++;
1126        Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1127        sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1128    }
1129
1130    private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1131        int ourSeqNum = -1;
1132        synchronized (sLegacyRequests) {
1133            LegacyRequest l = sLegacyRequests.get(netCap);
1134            if (l == null) return;
1135            ourSeqNum = l.expireSequenceNumber;
1136            if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
1137        }
1138        Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1139    }
1140
1141    private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1142        int delay = -1;
1143        int type = legacyTypeForNetworkCapabilities(netCap);
1144        try {
1145            delay = mService.getRestoreDefaultNetworkDelay(type);
1146        } catch (RemoteException e) {}
1147        LegacyRequest l = new LegacyRequest();
1148        l.networkCapabilities = netCap;
1149        l.delay = delay;
1150        l.expireSequenceNumber = 0;
1151        l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0,
1152                REQUEST, type);
1153        if (l.networkRequest == null) return null;
1154        sLegacyRequests.put(netCap, l);
1155        sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1156        return l.networkRequest;
1157    }
1158
1159    private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1160        if (delay >= 0) {
1161            Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1162            Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1163            sCallbackHandler.sendMessageDelayed(msg, delay);
1164        }
1165    }
1166
1167    private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1168        final LegacyRequest l;
1169        synchronized (sLegacyRequests) {
1170            l = sLegacyRequests.remove(netCap);
1171        }
1172        if (l == null) return false;
1173        unregisterNetworkCallback(l.networkCallback);
1174        l.clearDnsBinding();
1175        return true;
1176    }
1177
1178    /**
1179     * Ensure that a network route exists to deliver traffic to the specified
1180     * host via the specified network interface. An attempt to add a route that
1181     * already exists is ignored, but treated as successful.
1182     * <p>This method requires the caller to hold the permission
1183     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
1184     * @param networkType the type of the network over which traffic to the specified
1185     * host is to be routed
1186     * @param hostAddress the IP address of the host to which the route is desired
1187     * @return {@code true} on success, {@code false} on failure
1188     *
1189     * @deprecated Deprecated in favor of the {@link #requestNetwork},
1190     *             {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} api.
1191     * @removed
1192     */
1193    public boolean requestRouteToHost(int networkType, int hostAddress) {
1194        return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
1195    }
1196
1197    /**
1198     * Ensure that a network route exists to deliver traffic to the specified
1199     * host via the specified network interface. An attempt to add a route that
1200     * already exists is ignored, but treated as successful.
1201     * <p>This method requires the caller to hold the permission
1202     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
1203     * @param networkType the type of the network over which traffic to the specified
1204     * host is to be routed
1205     * @param hostAddress the IP address of the host to which the route is desired
1206     * @return {@code true} on success, {@code false} on failure
1207     * @hide
1208     * @deprecated Deprecated in favor of the {@link #requestNetwork} and
1209     *             {@link #bindProcessToNetwork} api.
1210     * @removed
1211     */
1212    public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
1213        try {
1214            return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
1215        } catch (RemoteException e) {
1216            return false;
1217        }
1218    }
1219
1220    /**
1221     * Returns the value of the setting for background data usage. If false,
1222     * applications should not use the network if the application is not in the
1223     * foreground. Developers should respect this setting, and check the value
1224     * of this before performing any background data operations.
1225     * <p>
1226     * All applications that have background services that use the network
1227     * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
1228     * <p>
1229     * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
1230     * background data depends on several combined factors, and this method will
1231     * always return {@code true}. Instead, when background data is unavailable,
1232     * {@link #getActiveNetworkInfo()} will now appear disconnected.
1233     *
1234     * @return Whether background data usage is allowed.
1235     */
1236    @Deprecated
1237    public boolean getBackgroundDataSetting() {
1238        // assume that background data is allowed; final authority is
1239        // NetworkInfo which may be blocked.
1240        return true;
1241    }
1242
1243    /**
1244     * Sets the value of the setting for background data usage.
1245     *
1246     * @param allowBackgroundData Whether an application should use data while
1247     *            it is in the background.
1248     *
1249     * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1250     * @see #getBackgroundDataSetting()
1251     * @hide
1252     */
1253    @Deprecated
1254    public void setBackgroundDataSetting(boolean allowBackgroundData) {
1255        // ignored
1256    }
1257
1258    /**
1259     * Return quota status for the current active network, or {@code null} if no
1260     * network is active. Quota status can change rapidly, so these values
1261     * shouldn't be cached.
1262     *
1263     * <p>This method requires the caller to hold the permission
1264     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1265     *
1266     * @hide
1267     */
1268    public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1269        try {
1270            return mService.getActiveNetworkQuotaInfo();
1271        } catch (RemoteException e) {
1272            return null;
1273        }
1274    }
1275
1276    /**
1277     * @hide
1278     * @deprecated Talk to TelephonyManager directly
1279     */
1280    public boolean getMobileDataEnabled() {
1281        IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1282        if (b != null) {
1283            try {
1284                ITelephony it = ITelephony.Stub.asInterface(b);
1285                int subId = SubscriptionManager.getDefaultDataSubId();
1286                Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
1287                boolean retVal = it.getDataEnabled(subId);
1288                Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1289                        + " retVal=" + retVal);
1290                return retVal;
1291            } catch (RemoteException e) { }
1292        }
1293        Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
1294        return false;
1295    }
1296
1297    /**
1298     * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
1299     * to find out when the system default network has gone in to a high power state.
1300     */
1301    public interface OnNetworkActiveListener {
1302        /**
1303         * Called on the main thread of the process to report that the current data network
1304         * has become active, and it is now a good time to perform any pending network
1305         * operations.  Note that this listener only tells you when the network becomes
1306         * active; if at any other time you want to know whether it is active (and thus okay
1307         * to initiate network traffic), you can retrieve its instantaneous state with
1308         * {@link ConnectivityManager#isDefaultNetworkActive}.
1309         */
1310        public void onNetworkActive();
1311    }
1312
1313    private INetworkManagementService getNetworkManagementService() {
1314        synchronized (this) {
1315            if (mNMService != null) {
1316                return mNMService;
1317            }
1318            IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1319            mNMService = INetworkManagementService.Stub.asInterface(b);
1320            return mNMService;
1321        }
1322    }
1323
1324    private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1325            mNetworkActivityListeners
1326                    = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1327
1328    /**
1329     * Start listening to reports when the system's default data network is active, meaning it is
1330     * a good time to perform network traffic.  Use {@link #isDefaultNetworkActive()}
1331     * to determine the current state of the system's default network after registering the
1332     * listener.
1333     * <p>
1334     * If the process default network has been set with
1335     * {@link ConnectivityManager#bindProcessToNetwork} this function will not
1336     * reflect the process's default, but the system default.
1337     *
1338     * @param l The listener to be told when the network is active.
1339     */
1340    public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
1341        INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1342            @Override
1343            public void onNetworkActive() throws RemoteException {
1344                l.onNetworkActive();
1345            }
1346        };
1347
1348        try {
1349            getNetworkManagementService().registerNetworkActivityListener(rl);
1350            mNetworkActivityListeners.put(l, rl);
1351        } catch (RemoteException e) {
1352        }
1353    }
1354
1355    /**
1356     * Remove network active listener previously registered with
1357     * {@link #addDefaultNetworkActiveListener}.
1358     *
1359     * @param l Previously registered listener.
1360     */
1361    public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
1362        INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1363        if (rl == null) {
1364            throw new IllegalArgumentException("Listener not registered: " + l);
1365        }
1366        try {
1367            getNetworkManagementService().unregisterNetworkActivityListener(rl);
1368        } catch (RemoteException e) {
1369        }
1370    }
1371
1372    /**
1373     * Return whether the data network is currently active.  An active network means that
1374     * it is currently in a high power state for performing data transmission.  On some
1375     * types of networks, it may be expensive to move and stay in such a state, so it is
1376     * more power efficient to batch network traffic together when the radio is already in
1377     * this state.  This method tells you whether right now is currently a good time to
1378     * initiate network traffic, as the network is already active.
1379     */
1380    public boolean isDefaultNetworkActive() {
1381        try {
1382            return getNetworkManagementService().isNetworkActive();
1383        } catch (RemoteException e) {
1384        }
1385        return false;
1386    }
1387
1388    /**
1389     * {@hide}
1390     */
1391    public ConnectivityManager(IConnectivityManager service) {
1392        mService = checkNotNull(service, "missing IConnectivityManager");
1393        sInstance = this;
1394    }
1395
1396    /** {@hide} */
1397    public static ConnectivityManager from(Context context) {
1398        return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1399    }
1400
1401    /** {@hide */
1402    public static final void enforceTetherChangePermission(Context context) {
1403        if (context.getResources().getStringArray(
1404                com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
1405            // Have a provisioning app - must only let system apps (which check this app)
1406            // turn on tethering
1407            context.enforceCallingOrSelfPermission(
1408                    android.Manifest.permission.CONNECTIVITY_INTERNAL, "ConnectivityService");
1409        } else {
1410            context.enforceCallingOrSelfPermission(
1411                    android.Manifest.permission.CHANGE_NETWORK_STATE, "ConnectivityService");
1412        }
1413    }
1414
1415    /**
1416     * @deprecated - use getSystemService. This is a kludge to support static access in certain
1417     *               situations where a Context pointer is unavailable.
1418     * @hide
1419     */
1420    static ConnectivityManager getInstanceOrNull() {
1421        return sInstance;
1422    }
1423
1424    /**
1425     * @deprecated - use getSystemService. This is a kludge to support static access in certain
1426     *               situations where a Context pointer is unavailable.
1427     * @hide
1428     */
1429    private static ConnectivityManager getInstance() {
1430        if (getInstanceOrNull() == null) {
1431            throw new IllegalStateException("No ConnectivityManager yet constructed");
1432        }
1433        return getInstanceOrNull();
1434    }
1435
1436    /**
1437     * Get the set of tetherable, available interfaces.  This list is limited by
1438     * device configuration and current interface existence.
1439     *
1440     * @return an array of 0 or more Strings of tetherable interface names.
1441     *
1442     * <p>This method requires the caller to hold the permission
1443     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1444     * {@hide}
1445     */
1446    public String[] getTetherableIfaces() {
1447        try {
1448            return mService.getTetherableIfaces();
1449        } catch (RemoteException e) {
1450            return new String[0];
1451        }
1452    }
1453
1454    /**
1455     * Get the set of tethered interfaces.
1456     *
1457     * @return an array of 0 or more String of currently tethered interface names.
1458     *
1459     * <p>This method requires the caller to hold the permission
1460     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1461     * {@hide}
1462     */
1463    public String[] getTetheredIfaces() {
1464        try {
1465            return mService.getTetheredIfaces();
1466        } catch (RemoteException e) {
1467            return new String[0];
1468        }
1469    }
1470
1471    /**
1472     * Get the set of interface names which attempted to tether but
1473     * failed.  Re-attempting to tether may cause them to reset to the Tethered
1474     * state.  Alternatively, causing the interface to be destroyed and recreated
1475     * may cause them to reset to the available state.
1476     * {@link ConnectivityManager#getLastTetherError} can be used to get more
1477     * information on the cause of the errors.
1478     *
1479     * @return an array of 0 or more String indicating the interface names
1480     *        which failed to tether.
1481     *
1482     * <p>This method requires the caller to hold the permission
1483     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1484     * {@hide}
1485     */
1486    public String[] getTetheringErroredIfaces() {
1487        try {
1488            return mService.getTetheringErroredIfaces();
1489        } catch (RemoteException e) {
1490            return new String[0];
1491        }
1492    }
1493
1494    /**
1495     * Get the set of tethered dhcp ranges.
1496     *
1497     * @return an array of 0 or more {@code String} of tethered dhcp ranges.
1498     * {@hide}
1499     */
1500    public String[] getTetheredDhcpRanges() {
1501        try {
1502            return mService.getTetheredDhcpRanges();
1503        } catch (RemoteException e) {
1504            return new String[0];
1505        }
1506    }
1507
1508    /**
1509     * Attempt to tether the named interface.  This will setup a dhcp server
1510     * on the interface, forward and NAT IP packets and forward DNS requests
1511     * to the best active upstream network interface.  Note that if no upstream
1512     * IP network interface is available, dhcp will still run and traffic will be
1513     * allowed between the tethered devices and this device, though upstream net
1514     * access will of course fail until an upstream network interface becomes
1515     * active.
1516     *
1517     * @param iface the interface name to tether.
1518     * @return error a {@code TETHER_ERROR} value indicating success or failure type
1519     *
1520     * <p>This method requires the caller to hold the permission
1521     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
1522     * {@hide}
1523     */
1524    public int tether(String iface) {
1525        try {
1526            return mService.tether(iface);
1527        } catch (RemoteException e) {
1528            return TETHER_ERROR_SERVICE_UNAVAIL;
1529        }
1530    }
1531
1532    /**
1533     * Stop tethering the named interface.
1534     *
1535     * @param iface the interface name to untether.
1536     * @return error a {@code TETHER_ERROR} value indicating success or failure type
1537     *
1538     * <p>This method requires the caller to hold the permission
1539     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
1540     * {@hide}
1541     */
1542    public int untether(String iface) {
1543        try {
1544            return mService.untether(iface);
1545        } catch (RemoteException e) {
1546            return TETHER_ERROR_SERVICE_UNAVAIL;
1547        }
1548    }
1549
1550    /**
1551     * Check if the device allows for tethering.  It may be disabled via
1552     * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
1553     * due to device configuration.
1554     *
1555     * @return a boolean - {@code true} indicating Tethering is supported.
1556     *
1557     * <p>This method requires the caller to hold the permission
1558     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1559     * {@hide}
1560     */
1561    public boolean isTetheringSupported() {
1562        try {
1563            return mService.isTetheringSupported();
1564        } catch (RemoteException e) {
1565            return false;
1566        }
1567    }
1568
1569    /**
1570     * Get the list of regular expressions that define any tetherable
1571     * USB network interfaces.  If USB tethering is not supported by the
1572     * device, this list should be empty.
1573     *
1574     * @return an array of 0 or more regular expression Strings defining
1575     *        what interfaces are considered tetherable usb interfaces.
1576     *
1577     * <p>This method requires the caller to hold the permission
1578     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1579     * {@hide}
1580     */
1581    public String[] getTetherableUsbRegexs() {
1582        try {
1583            return mService.getTetherableUsbRegexs();
1584        } catch (RemoteException e) {
1585            return new String[0];
1586        }
1587    }
1588
1589    /**
1590     * Get the list of regular expressions that define any tetherable
1591     * Wifi network interfaces.  If Wifi tethering is not supported by the
1592     * device, this list should be empty.
1593     *
1594     * @return an array of 0 or more regular expression Strings defining
1595     *        what interfaces are considered tetherable wifi interfaces.
1596     *
1597     * <p>This method requires the caller to hold the permission
1598     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1599     * {@hide}
1600     */
1601    public String[] getTetherableWifiRegexs() {
1602        try {
1603            return mService.getTetherableWifiRegexs();
1604        } catch (RemoteException e) {
1605            return new String[0];
1606        }
1607    }
1608
1609    /**
1610     * Get the list of regular expressions that define any tetherable
1611     * Bluetooth network interfaces.  If Bluetooth tethering is not supported by the
1612     * device, this list should be empty.
1613     *
1614     * @return an array of 0 or more regular expression Strings defining
1615     *        what interfaces are considered tetherable bluetooth interfaces.
1616     *
1617     * <p>This method requires the caller to hold the permission
1618     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1619     * {@hide}
1620     */
1621    public String[] getTetherableBluetoothRegexs() {
1622        try {
1623            return mService.getTetherableBluetoothRegexs();
1624        } catch (RemoteException e) {
1625            return new String[0];
1626        }
1627    }
1628
1629    /**
1630     * Attempt to both alter the mode of USB and Tethering of USB.  A
1631     * utility method to deal with some of the complexity of USB - will
1632     * attempt to switch to Rndis and subsequently tether the resulting
1633     * interface on {@code true} or turn off tethering and switch off
1634     * Rndis on {@code false}.
1635     *
1636     * @param enable a boolean - {@code true} to enable tethering
1637     * @return error a {@code TETHER_ERROR} value indicating success or failure type
1638     *
1639     * <p>This method requires the caller to hold the permission
1640     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
1641     * {@hide}
1642     */
1643    public int setUsbTethering(boolean enable) {
1644        try {
1645            return mService.setUsbTethering(enable);
1646        } catch (RemoteException e) {
1647            return TETHER_ERROR_SERVICE_UNAVAIL;
1648        }
1649    }
1650
1651    /** {@hide} */
1652    public static final int TETHER_ERROR_NO_ERROR           = 0;
1653    /** {@hide} */
1654    public static final int TETHER_ERROR_UNKNOWN_IFACE      = 1;
1655    /** {@hide} */
1656    public static final int TETHER_ERROR_SERVICE_UNAVAIL    = 2;
1657    /** {@hide} */
1658    public static final int TETHER_ERROR_UNSUPPORTED        = 3;
1659    /** {@hide} */
1660    public static final int TETHER_ERROR_UNAVAIL_IFACE      = 4;
1661    /** {@hide} */
1662    public static final int TETHER_ERROR_MASTER_ERROR       = 5;
1663    /** {@hide} */
1664    public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
1665    /** {@hide} */
1666    public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
1667    /** {@hide} */
1668    public static final int TETHER_ERROR_ENABLE_NAT_ERROR     = 8;
1669    /** {@hide} */
1670    public static final int TETHER_ERROR_DISABLE_NAT_ERROR    = 9;
1671    /** {@hide} */
1672    public static final int TETHER_ERROR_IFACE_CFG_ERROR      = 10;
1673
1674    /**
1675     * Get a more detailed error code after a Tethering or Untethering
1676     * request asynchronously failed.
1677     *
1678     * @param iface The name of the interface of interest
1679     * @return error The error code of the last error tethering or untethering the named
1680     *               interface
1681     *
1682     * <p>This method requires the caller to hold the permission
1683     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1684     * {@hide}
1685     */
1686    public int getLastTetherError(String iface) {
1687        try {
1688            return mService.getLastTetherError(iface);
1689        } catch (RemoteException e) {
1690            return TETHER_ERROR_SERVICE_UNAVAIL;
1691        }
1692    }
1693
1694    /**
1695     * Report network connectivity status.  This is currently used only
1696     * to alter status bar UI.
1697     *
1698     * @param networkType The type of network you want to report on
1699     * @param percentage The quality of the connection 0 is bad, 100 is good
1700     *
1701     * <p>This method requires the caller to hold the permission
1702     * {@link android.Manifest.permission#STATUS_BAR}.
1703     * {@hide}
1704     */
1705    public void reportInetCondition(int networkType, int percentage) {
1706        try {
1707            mService.reportInetCondition(networkType, percentage);
1708        } catch (RemoteException e) {
1709        }
1710    }
1711
1712    /**
1713     * Report a problem network to the framework.  This provides a hint to the system
1714     * that there might be connectivity problems on this network and may cause
1715     * the framework to re-evaluate network connectivity and/or switch to another
1716     * network.
1717     *
1718     * @param network The {@link Network} the application was attempting to use
1719     *                or {@code null} to indicate the current default network.
1720     * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
1721     *             working and non-working connectivity.
1722     */
1723    public void reportBadNetwork(Network network) {
1724        try {
1725            // One of these will be ignored because it matches system's current state.
1726            // The other will trigger the necessary reevaluation.
1727            mService.reportNetworkConnectivity(network, true);
1728            mService.reportNetworkConnectivity(network, false);
1729        } catch (RemoteException e) {
1730        }
1731    }
1732
1733    /**
1734     * Report to the framework whether a network has working connectivity.
1735     * This provides a hint to the system that a particular network is providing
1736     * working connectivity or not.  In response the framework may re-evaluate
1737     * the network's connectivity and might take further action thereafter.
1738     *
1739     * @param network The {@link Network} the application was attempting to use
1740     *                or {@code null} to indicate the current default network.
1741     * @param hasConnectivity {@code true} if the application was able to successfully access the
1742     *                        Internet using {@code network} or {@code false} if not.
1743     */
1744    public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
1745        try {
1746            mService.reportNetworkConnectivity(network, hasConnectivity);
1747        } catch (RemoteException e) {
1748        }
1749    }
1750
1751    /**
1752     * Set a network-independent global http proxy.  This is not normally what you want
1753     * for typical HTTP proxies - they are general network dependent.  However if you're
1754     * doing something unusual like general internal filtering this may be useful.  On
1755     * a private network where the proxy is not accessible, you may break HTTP using this.
1756     *
1757     * @param p The a {@link ProxyInfo} object defining the new global
1758     *        HTTP proxy.  A {@code null} value will clear the global HTTP proxy.
1759     *
1760     * <p>This method requires the caller to hold the permission
1761     * android.Manifest.permission#CONNECTIVITY_INTERNAL.
1762     * @hide
1763     */
1764    public void setGlobalProxy(ProxyInfo p) {
1765        try {
1766            mService.setGlobalProxy(p);
1767        } catch (RemoteException e) {
1768        }
1769    }
1770
1771    /**
1772     * Retrieve any network-independent global HTTP proxy.
1773     *
1774     * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
1775     *        if no global HTTP proxy is set.
1776     *
1777     * <p>This method requires the caller to hold the permission
1778     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1779     * @hide
1780     */
1781    public ProxyInfo getGlobalProxy() {
1782        try {
1783            return mService.getGlobalProxy();
1784        } catch (RemoteException e) {
1785            return null;
1786        }
1787    }
1788
1789    /**
1790     * Get the current default HTTP proxy settings.  If a global proxy is set it will be returned,
1791     * otherwise if this process is bound to a {@link Network} using
1792     * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
1793     * the default network's proxy is returned.
1794     *
1795     * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
1796     *        HTTP proxy is active.
1797     */
1798    public ProxyInfo getDefaultProxy() {
1799        final Network network = getBoundNetworkForProcess();
1800        if (network != null) {
1801            final ProxyInfo globalProxy = getGlobalProxy();
1802            if (globalProxy != null) return globalProxy;
1803            final LinkProperties lp = getLinkProperties(network);
1804            if (lp != null) return lp.getHttpProxy();
1805            return null;
1806        }
1807        try {
1808            return mService.getDefaultProxy();
1809        } catch (RemoteException e) {
1810            return null;
1811        }
1812    }
1813
1814    /**
1815     * Returns true if the hardware supports the given network type
1816     * else it returns false.  This doesn't indicate we have coverage
1817     * or are authorized onto a network, just whether or not the
1818     * hardware supports it.  For example a GSM phone without a SIM
1819     * should still return {@code true} for mobile data, but a wifi only
1820     * tablet would return {@code false}.
1821     *
1822     * @param networkType The network type we'd like to check
1823     * @return {@code true} if supported, else {@code false}
1824     *
1825     * <p>This method requires the caller to hold the permission
1826     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1827     * @hide
1828     */
1829    public boolean isNetworkSupported(int networkType) {
1830        try {
1831            return mService.isNetworkSupported(networkType);
1832        } catch (RemoteException e) {}
1833        return false;
1834    }
1835
1836    /**
1837     * Returns if the currently active data network is metered. A network is
1838     * classified as metered when the user is sensitive to heavy data usage on
1839     * that connection due to monetary costs, data limitations or
1840     * battery/performance issues. You should check this before doing large
1841     * data transfers, and warn the user or delay the operation until another
1842     * network is available.
1843     *
1844     * @return {@code true} if large transfers should be avoided, otherwise
1845     *        {@code false}.
1846     *
1847     * <p>This method requires the caller to hold the permission
1848     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1849     */
1850    public boolean isActiveNetworkMetered() {
1851        try {
1852            return mService.isActiveNetworkMetered();
1853        } catch (RemoteException e) {
1854            return false;
1855        }
1856    }
1857
1858    /**
1859     * If the LockdownVpn mechanism is enabled, updates the vpn
1860     * with a reload of its profile.
1861     *
1862     * @return a boolean with {@code} indicating success
1863     *
1864     * <p>This method can only be called by the system UID
1865     * {@hide}
1866     */
1867    public boolean updateLockdownVpn() {
1868        try {
1869            return mService.updateLockdownVpn();
1870        } catch (RemoteException e) {
1871            return false;
1872        }
1873    }
1874
1875    /**
1876     * Signal that the captive portal check on the indicated network
1877     * is complete and whether its a captive portal or not.
1878     *
1879     * @param info the {@link NetworkInfo} object for the networkType
1880     *        in question.
1881     * @param isCaptivePortal true/false.
1882     *
1883     * <p>This method requires the caller to hold the permission
1884     * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1885     * {@hide}
1886     */
1887    public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) {
1888        try {
1889            mService.captivePortalCheckCompleted(info, isCaptivePortal);
1890        } catch (RemoteException e) {
1891        }
1892    }
1893
1894    /**
1895     * Check mobile provisioning.
1896     *
1897     * @param suggestedTimeOutMs, timeout in milliseconds
1898     *
1899     * @return time out that will be used, maybe less that suggestedTimeOutMs
1900     * -1 if an error.
1901     *
1902     * {@hide}
1903     */
1904    public int checkMobileProvisioning(int suggestedTimeOutMs) {
1905        int timeOutMs = -1;
1906        try {
1907            timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
1908        } catch (RemoteException e) {
1909        }
1910        return timeOutMs;
1911    }
1912
1913    /**
1914     * Get the mobile provisioning url.
1915     * {@hide}
1916     */
1917    public String getMobileProvisioningUrl() {
1918        try {
1919            return mService.getMobileProvisioningUrl();
1920        } catch (RemoteException e) {
1921        }
1922        return null;
1923    }
1924
1925    /**
1926     * Get the mobile redirected provisioning url.
1927     * {@hide}
1928     */
1929    public String getMobileRedirectedProvisioningUrl() {
1930        try {
1931            return mService.getMobileRedirectedProvisioningUrl();
1932        } catch (RemoteException e) {
1933        }
1934        return null;
1935    }
1936
1937    /**
1938     * Set sign in error notification to visible or in visible
1939     *
1940     * @param visible
1941     * @param networkType
1942     *
1943     * {@hide}
1944     */
1945    public void setProvisioningNotificationVisible(boolean visible, int networkType,
1946            String action) {
1947        try {
1948            mService.setProvisioningNotificationVisible(visible, networkType, action);
1949        } catch (RemoteException e) {
1950        }
1951    }
1952
1953    /**
1954     * Set the value for enabling/disabling airplane mode
1955     *
1956     * @param enable whether to enable airplane mode or not
1957     *
1958     * <p>This method requires the caller to hold the permission
1959     * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1960     * @hide
1961     */
1962    public void setAirplaneMode(boolean enable) {
1963        try {
1964            mService.setAirplaneMode(enable);
1965        } catch (RemoteException e) {
1966        }
1967    }
1968
1969    /** {@hide} */
1970    public void registerNetworkFactory(Messenger messenger, String name) {
1971        try {
1972            mService.registerNetworkFactory(messenger, name);
1973        } catch (RemoteException e) { }
1974    }
1975
1976    /** {@hide} */
1977    public void unregisterNetworkFactory(Messenger messenger) {
1978        try {
1979            mService.unregisterNetworkFactory(messenger);
1980        } catch (RemoteException e) { }
1981    }
1982
1983    /**
1984     * @hide
1985     * Register a NetworkAgent with ConnectivityService.
1986     * @return NetID corresponding to NetworkAgent.
1987     */
1988    public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
1989            NetworkCapabilities nc, int score, NetworkMisc misc) {
1990        try {
1991            return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
1992        } catch (RemoteException e) {
1993            return NETID_UNSET;
1994        }
1995    }
1996
1997    /**
1998     * Base class for NetworkRequest callbacks.  Used for notifications about network
1999     * changes.  Should be extended by applications wanting notifications.
2000     */
2001    public static class NetworkCallback {
2002        /** @hide */
2003        public static final int PRECHECK     = 1;
2004        /** @hide */
2005        public static final int AVAILABLE    = 2;
2006        /** @hide */
2007        public static final int LOSING       = 3;
2008        /** @hide */
2009        public static final int LOST         = 4;
2010        /** @hide */
2011        public static final int UNAVAIL      = 5;
2012        /** @hide */
2013        public static final int CAP_CHANGED  = 6;
2014        /** @hide */
2015        public static final int PROP_CHANGED = 7;
2016        /** @hide */
2017        public static final int CANCELED     = 8;
2018
2019        /**
2020         * @hide
2021         * Called whenever the framework connects to a network that it may use to
2022         * satisfy this request
2023         */
2024        public void onPreCheck(Network network) {}
2025
2026        /**
2027         * Called when the framework connects and has declared new network ready for use.
2028         * This callback may be called more than once if the {@link Network} that is
2029         * satisfying the request changes.
2030         *
2031         * @param network The {@link Network} of the satisfying network.
2032         */
2033        public void onAvailable(Network network) {}
2034
2035        /**
2036         * Called when the network is about to be disconnected.  Often paired with an
2037         * {@link NetworkCallback#onAvailable} call with the new replacement network
2038         * for graceful handover.  This may not be called if we have a hard loss
2039         * (loss without warning).  This may be followed by either a
2040         * {@link NetworkCallback#onLost} call or a
2041         * {@link NetworkCallback#onAvailable} call for this network depending
2042         * on whether we lose or regain it.
2043         *
2044         * @param network The {@link Network} that is about to be disconnected.
2045         * @param maxMsToLive The time in ms the framework will attempt to keep the
2046         *                     network connected.  Note that the network may suffer a
2047         *                     hard loss at any time.
2048         */
2049        public void onLosing(Network network, int maxMsToLive) {}
2050
2051        /**
2052         * Called when the framework has a hard loss of the network or when the
2053         * graceful failure ends.
2054         *
2055         * @param network The {@link Network} lost.
2056         */
2057        public void onLost(Network network) {}
2058
2059        /**
2060         * Called if no network is found in the given timeout time.  If no timeout is given,
2061         * this will not be called.
2062         * @hide
2063         */
2064        public void onUnavailable() {}
2065
2066        /**
2067         * Called when the network the framework connected to for this request
2068         * changes capabilities but still satisfies the stated need.
2069         *
2070         * @param network The {@link Network} whose capabilities have changed.
2071         * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network.
2072         */
2073        public void onCapabilitiesChanged(Network network,
2074                NetworkCapabilities networkCapabilities) {}
2075
2076        /**
2077         * Called when the network the framework connected to for this request
2078         * changes {@link LinkProperties}.
2079         *
2080         * @param network The {@link Network} whose link properties have changed.
2081         * @param linkProperties The new {@link LinkProperties} for this network.
2082         */
2083        public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
2084
2085        private NetworkRequest networkRequest;
2086    }
2087
2088    private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
2089    /** @hide obj = pair(NetworkRequest, Network) */
2090    public static final int CALLBACK_PRECHECK           = BASE + 1;
2091    /** @hide obj = pair(NetworkRequest, Network) */
2092    public static final int CALLBACK_AVAILABLE          = BASE + 2;
2093    /** @hide obj = pair(NetworkRequest, Network), arg1 = ttl */
2094    public static final int CALLBACK_LOSING             = BASE + 3;
2095    /** @hide obj = pair(NetworkRequest, Network) */
2096    public static final int CALLBACK_LOST               = BASE + 4;
2097    /** @hide obj = NetworkRequest */
2098    public static final int CALLBACK_UNAVAIL            = BASE + 5;
2099    /** @hide obj = pair(NetworkRequest, Network) */
2100    public static final int CALLBACK_CAP_CHANGED        = BASE + 6;
2101    /** @hide obj = pair(NetworkRequest, Network) */
2102    public static final int CALLBACK_IP_CHANGED         = BASE + 7;
2103    /** @hide obj = NetworkRequest */
2104    public static final int CALLBACK_RELEASED           = BASE + 8;
2105    /** @hide */
2106    public static final int CALLBACK_EXIT               = BASE + 9;
2107    /** @hide obj = NetworkCapabilities, arg1 = seq number */
2108    private static final int EXPIRE_LEGACY_REQUEST      = BASE + 10;
2109
2110    private class CallbackHandler extends Handler {
2111        private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap;
2112        private final AtomicInteger mRefCount;
2113        private static final String TAG = "ConnectivityManager.CallbackHandler";
2114        private final ConnectivityManager mCm;
2115
2116        CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap,
2117                AtomicInteger refCount, ConnectivityManager cm) {
2118            super(looper);
2119            mCallbackMap = callbackMap;
2120            mRefCount = refCount;
2121            mCm = cm;
2122        }
2123
2124        @Override
2125        public void handleMessage(Message message) {
2126            Log.d(TAG, "CM callback handler got msg " + message.what);
2127            switch (message.what) {
2128                case CALLBACK_PRECHECK: {
2129                    NetworkRequest request = (NetworkRequest)getObject(message,
2130                            NetworkRequest.class);
2131                    NetworkCallback callbacks = getCallbacks(request);
2132                    if (callbacks != null) {
2133                        callbacks.onPreCheck((Network)getObject(message, Network.class));
2134                    } else {
2135                        Log.e(TAG, "callback not found for PRECHECK message");
2136                    }
2137                    break;
2138                }
2139                case CALLBACK_AVAILABLE: {
2140                    NetworkRequest request = (NetworkRequest)getObject(message,
2141                            NetworkRequest.class);
2142                    NetworkCallback callbacks = getCallbacks(request);
2143                    if (callbacks != null) {
2144                        callbacks.onAvailable((Network)getObject(message, Network.class));
2145                    } else {
2146                        Log.e(TAG, "callback not found for AVAILABLE message");
2147                    }
2148                    break;
2149                }
2150                case CALLBACK_LOSING: {
2151                    NetworkRequest request = (NetworkRequest)getObject(message,
2152                            NetworkRequest.class);
2153                    NetworkCallback callbacks = getCallbacks(request);
2154                    if (callbacks != null) {
2155                        callbacks.onLosing((Network)getObject(message, Network.class),
2156                                message.arg1);
2157                    } else {
2158                        Log.e(TAG, "callback not found for LOSING message");
2159                    }
2160                    break;
2161                }
2162                case CALLBACK_LOST: {
2163                    NetworkRequest request = (NetworkRequest)getObject(message,
2164                            NetworkRequest.class);
2165
2166                    NetworkCallback callbacks = getCallbacks(request);
2167                    if (callbacks != null) {
2168                        callbacks.onLost((Network)getObject(message, Network.class));
2169                    } else {
2170                        Log.e(TAG, "callback not found for LOST message");
2171                    }
2172                    break;
2173                }
2174                case CALLBACK_UNAVAIL: {
2175                    NetworkRequest request = (NetworkRequest)getObject(message,
2176                            NetworkRequest.class);
2177                    NetworkCallback callbacks = null;
2178                    synchronized(mCallbackMap) {
2179                        callbacks = mCallbackMap.get(request);
2180                    }
2181                    if (callbacks != null) {
2182                        callbacks.onUnavailable();
2183                    } else {
2184                        Log.e(TAG, "callback not found for UNAVAIL message");
2185                    }
2186                    break;
2187                }
2188                case CALLBACK_CAP_CHANGED: {
2189                    NetworkRequest request = (NetworkRequest)getObject(message,
2190                            NetworkRequest.class);
2191                    NetworkCallback callbacks = getCallbacks(request);
2192                    if (callbacks != null) {
2193                        Network network = (Network)getObject(message, Network.class);
2194                        NetworkCapabilities cap = (NetworkCapabilities)getObject(message,
2195                                NetworkCapabilities.class);
2196
2197                        callbacks.onCapabilitiesChanged(network, cap);
2198                    } else {
2199                        Log.e(TAG, "callback not found for CAP_CHANGED message");
2200                    }
2201                    break;
2202                }
2203                case CALLBACK_IP_CHANGED: {
2204                    NetworkRequest request = (NetworkRequest)getObject(message,
2205                            NetworkRequest.class);
2206                    NetworkCallback callbacks = getCallbacks(request);
2207                    if (callbacks != null) {
2208                        Network network = (Network)getObject(message, Network.class);
2209                        LinkProperties lp = (LinkProperties)getObject(message,
2210                                LinkProperties.class);
2211
2212                        callbacks.onLinkPropertiesChanged(network, lp);
2213                    } else {
2214                        Log.e(TAG, "callback not found for IP_CHANGED message");
2215                    }
2216                    break;
2217                }
2218                case CALLBACK_RELEASED: {
2219                    NetworkRequest req = (NetworkRequest)getObject(message, NetworkRequest.class);
2220                    NetworkCallback callbacks = null;
2221                    synchronized(mCallbackMap) {
2222                        callbacks = mCallbackMap.remove(req);
2223                    }
2224                    if (callbacks != null) {
2225                        synchronized(mRefCount) {
2226                            if (mRefCount.decrementAndGet() == 0) {
2227                                getLooper().quit();
2228                            }
2229                        }
2230                    } else {
2231                        Log.e(TAG, "callback not found for CANCELED message");
2232                    }
2233                    break;
2234                }
2235                case CALLBACK_EXIT: {
2236                    Log.d(TAG, "Listener quiting");
2237                    getLooper().quit();
2238                    break;
2239                }
2240                case EXPIRE_LEGACY_REQUEST: {
2241                    expireRequest((NetworkCapabilities)message.obj, message.arg1);
2242                    break;
2243                }
2244            }
2245        }
2246
2247        private Object getObject(Message msg, Class c) {
2248            return msg.getData().getParcelable(c.getSimpleName());
2249        }
2250        private NetworkCallback getCallbacks(NetworkRequest req) {
2251            synchronized(mCallbackMap) {
2252                return mCallbackMap.get(req);
2253            }
2254        }
2255    }
2256
2257    private void incCallbackHandlerRefCount() {
2258        synchronized(sCallbackRefCount) {
2259            if (sCallbackRefCount.incrementAndGet() == 1) {
2260                // TODO - switch this over to a ManagerThread or expire it when done
2261                HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
2262                callbackThread.start();
2263                sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
2264                        sNetworkCallback, sCallbackRefCount, this);
2265            }
2266        }
2267    }
2268
2269    private void decCallbackHandlerRefCount() {
2270        synchronized(sCallbackRefCount) {
2271            if (sCallbackRefCount.decrementAndGet() == 0) {
2272                sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
2273                sCallbackHandler = null;
2274            }
2275        }
2276    }
2277
2278    static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback =
2279            new HashMap<NetworkRequest, NetworkCallback>();
2280    static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
2281    static CallbackHandler sCallbackHandler = null;
2282
2283    private final static int LISTEN  = 1;
2284    private final static int REQUEST = 2;
2285
2286    private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
2287            NetworkCallback networkCallback, int timeoutSec, int action,
2288            int legacyType) {
2289        if (networkCallback == null) {
2290            throw new IllegalArgumentException("null NetworkCallback");
2291        }
2292        if (need == null) throw new IllegalArgumentException("null NetworkCapabilities");
2293        try {
2294            incCallbackHandlerRefCount();
2295            synchronized(sNetworkCallback) {
2296                if (action == LISTEN) {
2297                    networkCallback.networkRequest = mService.listenForNetwork(need,
2298                            new Messenger(sCallbackHandler), new Binder());
2299                } else {
2300                    networkCallback.networkRequest = mService.requestNetwork(need,
2301                            new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType);
2302                }
2303                if (networkCallback.networkRequest != null) {
2304                    sNetworkCallback.put(networkCallback.networkRequest, networkCallback);
2305                }
2306            }
2307        } catch (RemoteException e) {}
2308        if (networkCallback.networkRequest == null) decCallbackHandlerRefCount();
2309        return networkCallback.networkRequest;
2310    }
2311
2312    /**
2313     * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
2314     *
2315     * This {@link NetworkRequest} will live until released via
2316     * {@link #unregisterNetworkCallback} or the calling application exits.
2317     * Status of the request can be followed by listening to the various
2318     * callbacks described in {@link NetworkCallback}.  The {@link Network}
2319     * can be used to direct traffic to the network.
2320     *
2321     * @param request {@link NetworkRequest} describing this request.
2322     * @param networkCallback The {@link NetworkCallback} to be utilized for this
2323     *                        request.  Note the callback must not be shared - they
2324     *                        uniquely specify this request.
2325     */
2326    public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
2327        sendRequestForNetwork(request.networkCapabilities, networkCallback, 0,
2328                REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
2329    }
2330
2331    /**
2332     * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
2333     * by a timeout.
2334     *
2335     * This function behaves identically to the non-timedout version, but if a suitable
2336     * network is not found within the given time (in milliseconds) the
2337     * {@link NetworkCallback#unavailable} callback is called.  The request must
2338     * still be released normally by calling {@link releaseNetworkRequest}.
2339     * @param request {@link NetworkRequest} describing this request.
2340     * @param networkCallback The callbacks to be utilized for this request.  Note
2341     *                        the callbacks must not be shared - they uniquely specify
2342     *                        this request.
2343     * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2344     *                  before {@link NetworkCallback#unavailable} is called.
2345     * @hide
2346     */
2347    public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2348            int timeoutMs) {
2349        sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs,
2350                REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
2351    }
2352
2353    /**
2354     * The maximum number of milliseconds the framework will look for a suitable network
2355     * during a timeout-equiped call to {@link requestNetwork}.
2356     * {@hide}
2357     */
2358    public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
2359
2360    /**
2361     * The lookup key for a {@link Network} object included with the intent after
2362     * successfully finding a network for the applications request.  Retrieve it with
2363     * {@link android.content.Intent#getParcelableExtra(String)}.
2364     * <p>
2365     * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
2366     * then you must get a ConnectivityManager instance before doing so.
2367     */
2368    public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
2369
2370    /**
2371     * The lookup key for a {@link NetworkRequest} object included with the intent after
2372     * successfully finding a network for the applications request.  Retrieve it with
2373     * {@link android.content.Intent#getParcelableExtra(String)}.
2374     */
2375    public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
2376
2377
2378    /**
2379     * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
2380     *
2381     * This function behaves identically to the version that takes a NetworkCallback, but instead
2382     * of {@link NetworkCallback} a {@link PendingIntent} is used.  This means
2383     * the request may outlive the calling application and get called back when a suitable
2384     * network is found.
2385     * <p>
2386     * The operation is an Intent broadcast that goes to a broadcast receiver that
2387     * you registered with {@link Context#registerReceiver} or through the
2388     * &lt;receiver&gt; tag in an AndroidManifest.xml file
2389     * <p>
2390     * The operation Intent is delivered with two extras, a {@link Network} typed
2391     * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2392     * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
2393     * the original requests parameters.  It is important to create a new,
2394     * {@link NetworkCallback} based request before completing the processing of the
2395     * Intent to reserve the network or it will be released shortly after the Intent
2396     * is processed.
2397     * <p>
2398     * If there is already an request for this Intent registered (with the equality of
2399     * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
2400     * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
2401     * <p>
2402     * The request may be released normally by calling
2403     * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
2404     *
2405     * @param request {@link NetworkRequest} describing this request.
2406     * @param operation Action to perform when the network is available (corresponds
2407     *                  to the {@link NetworkCallback#onAvailable} call.  Typically
2408     *                  comes from {@link PendingIntent#getBroadcast}. Cannot be null.
2409     */
2410    public void requestNetwork(NetworkRequest request, PendingIntent operation) {
2411        checkPendingIntent(operation);
2412        try {
2413            mService.pendingRequestForNetwork(request.networkCapabilities, operation);
2414        } catch (RemoteException e) {}
2415    }
2416
2417    /**
2418     * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
2419     * <p>
2420     * This method has the same behavior as {@link #unregisterNetworkCallback} with respect to
2421     * releasing network resources and disconnecting.
2422     *
2423     * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
2424     *                  PendingIntent passed to
2425     *                  {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
2426     *                  corresponding NetworkRequest you'd like to remove. Cannot be null.
2427     */
2428    public void releaseNetworkRequest(PendingIntent operation) {
2429        checkPendingIntent(operation);
2430        try {
2431            mService.releasePendingNetworkRequest(operation);
2432        } catch (RemoteException e) {}
2433    }
2434
2435    private void checkPendingIntent(PendingIntent intent) {
2436        if (intent == null) {
2437            throw new IllegalArgumentException("PendingIntent cannot be null.");
2438        }
2439    }
2440
2441    /**
2442     * Registers to receive notifications about all networks which satisfy the given
2443     * {@link NetworkRequest}.  The callbacks will continue to be called until
2444     * either the application exits or {@link #unregisterNetworkCallback} is called
2445     *
2446     * @param request {@link NetworkRequest} describing this request.
2447     * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
2448     *                        networks change state.
2449     */
2450    public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
2451        sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
2452    }
2453
2454    /**
2455     * Requests bandwidth update for a given {@link Network} and returns whether the update request
2456     * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
2457     * network connection for updated bandwidth information. The caller will be notified via
2458     * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
2459     * method assumes that the caller has previously called {@link #registerNetworkCallback} to
2460     * listen for network changes.
2461     *
2462     * @param network{@link Network} specifying which network you're interested.
2463     * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2464     *
2465     * @hide
2466     */
2467    public boolean requestBandwidthUpdate(Network network) {
2468        try {
2469            return mService.requestBandwidthUpdate(network);
2470        } catch (RemoteException e) {
2471            return false;
2472        }
2473    }
2474
2475    /**
2476     * Unregisters callbacks about and possibly releases networks originating from
2477     * {@link #requestNetwork} and {@link #registerNetworkCallback} calls.  If the
2478     * given {@code NetworkCallback} had previously been used with {@code #requestNetwork},
2479     * any networks that had been connected to only to satisfy that request will be
2480     * disconnected.
2481     *
2482     * @param networkCallback The {@link NetworkCallback} used when making the request.
2483     */
2484    public void unregisterNetworkCallback(NetworkCallback networkCallback) {
2485        if (networkCallback == null || networkCallback.networkRequest == null ||
2486                networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
2487            throw new IllegalArgumentException("Invalid NetworkCallback");
2488        }
2489        try {
2490            mService.releaseNetworkRequest(networkCallback.networkRequest);
2491        } catch (RemoteException e) {}
2492    }
2493
2494    /**
2495     * Informs the system whether it should switch to {@code network} regardless of whether it is
2496     * validated or not. If {@code accept} is true, and the network was explicitly selected by the
2497     * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
2498     * the system default network regardless of any other network that's currently connected. If
2499     * {@code always} is true, then the choice is remembered, so that the next time the user
2500     * connects to this network, the system will switch to it.
2501     *
2502     * <p>This method requires the caller to hold the permission
2503     * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
2504     *
2505     * @param network The network to accept.
2506     * @param accept Whether to accept the network even if unvalidated.
2507     * @param always Whether to remember this choice in the future.
2508     *
2509     * @hide
2510     */
2511    public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
2512        try {
2513            mService.setAcceptUnvalidated(network, accept, always);
2514        } catch (RemoteException e) {}
2515    }
2516
2517    /**
2518     * Resets all connectivity manager settings back to factory defaults.
2519     * @hide
2520     */
2521    public void factoryReset() {
2522        try {
2523            mService.factoryReset();
2524        } catch (RemoteException e) {
2525        }
2526    }
2527
2528    /**
2529     * Binds the current process to {@code network}.  All Sockets created in the future
2530     * (and not explicitly bound via a bound SocketFactory from
2531     * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2532     * {@code network}.  All host name resolutions will be limited to {@code network} as well.
2533     * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2534     * work and all host name resolutions will fail.  This is by design so an application doesn't
2535     * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2536     * To clear binding pass {@code null} for {@code network}.  Using individually bound
2537     * Sockets created by Network.getSocketFactory().createSocket() and
2538     * performing network-specific host name resolutions via
2539     * {@link Network#getAllByName Network.getAllByName} is preferred to calling
2540     * {@code bindProcessToNetwork}.
2541     *
2542     * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2543     *                the current binding.
2544     * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2545     */
2546    public boolean bindProcessToNetwork(Network network) {
2547        // Forcing callers to call thru non-static function ensures ConnectivityManager
2548        // instantiated.
2549        return setProcessDefaultNetwork(network);
2550    }
2551
2552    /**
2553     * Binds the current process to {@code network}.  All Sockets created in the future
2554     * (and not explicitly bound via a bound SocketFactory from
2555     * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2556     * {@code network}.  All host name resolutions will be limited to {@code network} as well.
2557     * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2558     * work and all host name resolutions will fail.  This is by design so an application doesn't
2559     * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2560     * To clear binding pass {@code null} for {@code network}.  Using individually bound
2561     * Sockets created by Network.getSocketFactory().createSocket() and
2562     * performing network-specific host name resolutions via
2563     * {@link Network#getAllByName Network.getAllByName} is preferred to calling
2564     * {@code setProcessDefaultNetwork}.
2565     *
2566     * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2567     *                the current binding.
2568     * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2569     * @deprecated This function can throw {@link IllegalStateException}.  Use
2570     *             {@link #bindProcessToNetwork} instead.  {@code bindProcessToNetwork}
2571     *             is a direct replacement.
2572     */
2573    public static boolean setProcessDefaultNetwork(Network network) {
2574        int netId = (network == null) ? NETID_UNSET : network.netId;
2575        if (netId == NetworkUtils.getBoundNetworkForProcess()) {
2576            return true;
2577        }
2578        if (NetworkUtils.bindProcessToNetwork(netId)) {
2579            // Set HTTP proxy system properties to match network.
2580            // TODO: Deprecate this static method and replace it with a non-static version.
2581            try {
2582                Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
2583            } catch (SecurityException e) {
2584                // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
2585                Log.e(TAG, "Can't set proxy properties", e);
2586            }
2587            // Must flush DNS cache as new network may have different DNS resolutions.
2588            InetAddress.clearDnsCache();
2589            // Must flush socket pool as idle sockets will be bound to previous network and may
2590            // cause subsequent fetches to be performed on old network.
2591            NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
2592            return true;
2593        } else {
2594            return false;
2595        }
2596    }
2597
2598    /**
2599     * Returns the {@link Network} currently bound to this process via
2600     * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
2601     *
2602     * @return {@code Network} to which this process is bound, or {@code null}.
2603     */
2604    public Network getBoundNetworkForProcess() {
2605        // Forcing callers to call thru non-static function ensures ConnectivityManager
2606        // instantiated.
2607        return getProcessDefaultNetwork();
2608    }
2609
2610    /**
2611     * Returns the {@link Network} currently bound to this process via
2612     * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
2613     *
2614     * @return {@code Network} to which this process is bound, or {@code null}.
2615     * @deprecated Using this function can lead to other functions throwing
2616     *             {@link IllegalStateException}.  Use {@link #getBoundNetworkForProcess} instead.
2617     *             {@code getBoundNetworkForProcess} is a direct replacement.
2618     */
2619    public static Network getProcessDefaultNetwork() {
2620        int netId = NetworkUtils.getBoundNetworkForProcess();
2621        if (netId == NETID_UNSET) return null;
2622        return new Network(netId);
2623    }
2624
2625    /**
2626     * Binds host resolutions performed by this process to {@code network}.
2627     * {@link #bindProcessToNetwork} takes precedence over this setting.
2628     *
2629     * @param network The {@link Network} to bind host resolutions from the current process to, or
2630     *                {@code null} to clear the current binding.
2631     * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2632     * @hide
2633     * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
2634     */
2635    public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
2636        return NetworkUtils.bindProcessToNetworkForHostResolution(
2637                network == null ? NETID_UNSET : network.netId);
2638    }
2639}
2640