ConnectivityManager.java revision 0535a9f7aa5d0f875b06845e9af5f6dbfc5c8a19
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
19import static com.android.internal.util.Preconditions.checkNotNull;
20
21import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
23import android.content.Context;
24import android.os.Binder;
25import android.os.Build.VERSION_CODES;
26import android.os.IBinder;
27import android.os.INetworkActivityListener;
28import android.os.INetworkManagementService;
29import android.os.Messenger;
30import android.os.RemoteException;
31import android.os.ServiceManager;
32import android.provider.Settings;
33import android.util.ArrayMap;
34
35import java.net.InetAddress;
36
37/**
38 * Class that answers queries about the state of network connectivity. It also
39 * notifies applications when network connectivity changes. Get an instance
40 * of this class by calling
41 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
42 * <p>
43 * The primary responsibilities of this class are to:
44 * <ol>
45 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
46 * <li>Send broadcast intents when network connectivity changes</li>
47 * <li>Attempt to "fail over" to another network when connectivity to a network
48 * is lost</li>
49 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
50 * state of the available networks</li>
51 * </ol>
52 */
53public class ConnectivityManager {
54    private static final String TAG = "ConnectivityManager";
55
56    /**
57     * A change in network connectivity has occurred. A connection has either
58     * been established or lost. The NetworkInfo for the affected network is
59     * sent as an extra; it should be consulted to see what kind of
60     * connectivity event occurred.
61     * <p/>
62     * If this is a connection that was the result of failing over from a
63     * disconnected network, then the FAILOVER_CONNECTION boolean extra is
64     * set to true.
65     * <p/>
66     * For a loss of connectivity, if the connectivity manager is attempting
67     * to connect (or has already connected) to another network, the
68     * NetworkInfo for the new network is also passed as an extra. This lets
69     * any receivers of the broadcast know that they should not necessarily
70     * tell the user that no data traffic will be possible. Instead, the
71     * receiver should expect another broadcast soon, indicating either that
72     * the failover attempt succeeded (and so there is still overall data
73     * connectivity), or that the failover attempt failed, meaning that all
74     * connectivity has been lost.
75     * <p/>
76     * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
77     * is set to {@code true} if there are no connected networks at all.
78     */
79    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
80    public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
81
82    /**
83     * Identical to {@link #CONNECTIVITY_ACTION} broadcast, but sent without any
84     * applicable {@link Settings.Global#CONNECTIVITY_CHANGE_DELAY}.
85     *
86     * @hide
87     */
88    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
89    public static final String CONNECTIVITY_ACTION_IMMEDIATE =
90            "android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE";
91
92    /**
93     * The lookup key for a {@link NetworkInfo} object. Retrieve with
94     * {@link android.content.Intent#getParcelableExtra(String)}.
95     *
96     * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
97     *             should always obtain network information through
98     *             {@link #getActiveNetworkInfo()} or
99     *             {@link #getAllNetworkInfo()}.
100     * @see #EXTRA_NETWORK_TYPE
101     */
102    @Deprecated
103    public static final String EXTRA_NETWORK_INFO = "networkInfo";
104
105    /**
106     * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
107     * Can be used with {@link #getNetworkInfo(int)} to get {@link NetworkInfo}
108     * state based on the calling application.
109     *
110     * @see android.content.Intent#getIntExtra(String, int)
111     */
112    public static final String EXTRA_NETWORK_TYPE = "networkType";
113
114    /**
115     * The lookup key for a boolean that indicates whether a connect event
116     * is for a network to which the connectivity manager was failing over
117     * following a disconnect on another network.
118     * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
119     */
120    public static final String EXTRA_IS_FAILOVER = "isFailover";
121    /**
122     * The lookup key for a {@link NetworkInfo} object. This is supplied when
123     * there is another network that it may be possible to connect to. Retrieve with
124     * {@link android.content.Intent#getParcelableExtra(String)}.
125     */
126    public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
127    /**
128     * The lookup key for a boolean that indicates whether there is a
129     * complete lack of connectivity, i.e., no network is available.
130     * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
131     */
132    public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
133    /**
134     * The lookup key for a string that indicates why an attempt to connect
135     * to a network failed. The string has no particular structure. It is
136     * intended to be used in notifications presented to users. Retrieve
137     * it with {@link android.content.Intent#getStringExtra(String)}.
138     */
139    public static final String EXTRA_REASON = "reason";
140    /**
141     * The lookup key for a string that provides optionally supplied
142     * extra information about the network state. The information
143     * may be passed up from the lower networking layers, and its
144     * meaning may be specific to a particular network type. Retrieve
145     * it with {@link android.content.Intent#getStringExtra(String)}.
146     */
147    public static final String EXTRA_EXTRA_INFO = "extraInfo";
148    /**
149     * The lookup key for an int that provides information about
150     * our connection to the internet at large.  0 indicates no connection,
151     * 100 indicates a great connection.  Retrieve it with
152     * {@link android.content.Intent#getIntExtra(String, int)}.
153     * {@hide}
154     */
155    public static final String EXTRA_INET_CONDITION = "inetCondition";
156
157    /**
158     * Broadcast action to indicate the change of data activity status
159     * (idle or active) on a network in a recent period.
160     * The network becomes active when data transmission is started, or
161     * idle if there is no data transmission for a period of time.
162     * {@hide}
163     */
164    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
165    public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
166    /**
167     * The lookup key for an enum that indicates the network device type on which this data activity
168     * change happens.
169     * {@hide}
170     */
171    public static final String EXTRA_DEVICE_TYPE = "deviceType";
172    /**
173     * The lookup key for a boolean that indicates the device is active or not. {@code true} means
174     * it is actively sending or receiving data and {@code false} means it is idle.
175     * {@hide}
176     */
177    public static final String EXTRA_IS_ACTIVE = "isActive";
178    /**
179     * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
180     * {@hide}
181     */
182    public static final String EXTRA_REALTIME_NS = "tsNanos";
183
184    /**
185     * Broadcast Action: The setting for background data usage has changed
186     * values. Use {@link #getBackgroundDataSetting()} to get the current value.
187     * <p>
188     * If an application uses the network in the background, it should listen
189     * for this broadcast and stop using the background data if the value is
190     * {@code false}.
191     * <p>
192     *
193     * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
194     *             of background data depends on several combined factors, and
195     *             this broadcast is no longer sent. Instead, when background
196     *             data is unavailable, {@link #getActiveNetworkInfo()} will now
197     *             appear disconnected. During first boot after a platform
198     *             upgrade, this broadcast will be sent once if
199     *             {@link #getBackgroundDataSetting()} was {@code false} before
200     *             the upgrade.
201     */
202    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
203    @Deprecated
204    public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
205            "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
206
207    /**
208     * Broadcast Action: The network connection may not be good
209     * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
210     * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
211     * the network and it's condition.
212     * @hide
213     */
214    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
215    public static final String INET_CONDITION_ACTION =
216            "android.net.conn.INET_CONDITION_ACTION";
217
218    /**
219     * Broadcast Action: A tetherable connection has come or gone.
220     * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
221     * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
222     * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
223     * the current state of tethering.  Each include a list of
224     * interface names in that state (may be empty).
225     * @hide
226     */
227    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
228    public static final String ACTION_TETHER_STATE_CHANGED =
229            "android.net.conn.TETHER_STATE_CHANGED";
230
231    /**
232     * @hide
233     * gives a String[] listing all the interfaces configured for
234     * tethering and currently available for tethering.
235     */
236    public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
237
238    /**
239     * @hide
240     * gives a String[] listing all the interfaces currently tethered
241     * (ie, has dhcp support and packets potentially forwarded/NATed)
242     */
243    public static final String EXTRA_ACTIVE_TETHER = "activeArray";
244
245    /**
246     * @hide
247     * gives a String[] listing all the interfaces we tried to tether and
248     * failed.  Use {@link #getLastTetherError} to find the error code
249     * for any interfaces listed here.
250     */
251    public static final String EXTRA_ERRORED_TETHER = "erroredArray";
252
253    /**
254     * Broadcast Action: The captive portal tracker has finished its test.
255     * Sent only while running Setup Wizard, in lieu of showing a user
256     * notification.
257     * @hide
258     */
259    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
260    public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
261            "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
262    /**
263     * The lookup key for a boolean that indicates whether a captive portal was detected.
264     * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
265     * @hide
266     */
267    public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
268
269    /**
270     * The absence of a connection type.
271     * @hide
272     */
273    public static final int TYPE_NONE        = -1;
274
275    /**
276     * The Mobile data connection.  When active, all data traffic
277     * will use this network type's interface by default
278     * (it has a default route)
279     */
280    public static final int TYPE_MOBILE      = 0;
281    /**
282     * The WIFI data connection.  When active, all data traffic
283     * will use this network type's interface by default
284     * (it has a default route).
285     */
286    public static final int TYPE_WIFI        = 1;
287    /**
288     * An MMS-specific Mobile data connection.  This network type may use the
289     * same network interface as {@link #TYPE_MOBILE} or it may use a different
290     * one.  This is used by applications needing to talk to the carrier's
291     * Multimedia Messaging Service servers.
292     */
293    public static final int TYPE_MOBILE_MMS  = 2;
294    /**
295     * A SUPL-specific Mobile data connection.  This network type may use the
296     * same network interface as {@link #TYPE_MOBILE} or it may use a different
297     * one.  This is used by applications needing to talk to the carrier's
298     * Secure User Plane Location servers for help locating the device.
299     */
300    public static final int TYPE_MOBILE_SUPL = 3;
301    /**
302     * A DUN-specific Mobile data connection.  This network type may use the
303     * same network interface as {@link #TYPE_MOBILE} or it may use a different
304     * one.  This is sometimes by the system when setting up an upstream connection
305     * for tethering so that the carrier is aware of DUN traffic.
306     */
307    public static final int TYPE_MOBILE_DUN  = 4;
308    /**
309     * A High Priority Mobile data connection.  This network type uses the
310     * same network interface as {@link #TYPE_MOBILE} but the routing setup
311     * is different.  Only requesting processes will have access to the
312     * Mobile DNS servers and only IP's explicitly requested via {@link #requestRouteToHost}
313     * will route over this interface if no default route exists.
314     */
315    public static final int TYPE_MOBILE_HIPRI = 5;
316    /**
317     * The WiMAX data connection.  When active, all data traffic
318     * will use this network type's interface by default
319     * (it has a default route).
320     */
321    public static final int TYPE_WIMAX       = 6;
322
323    /**
324     * The Bluetooth data connection.  When active, all data traffic
325     * will use this network type's interface by default
326     * (it has a default route).
327     */
328    public static final int TYPE_BLUETOOTH   = 7;
329
330    /**
331     * Dummy data connection.  This should not be used on shipping devices.
332     */
333    public static final int TYPE_DUMMY       = 8;
334
335    /**
336     * The Ethernet data connection.  When active, all data traffic
337     * will use this network type's interface by default
338     * (it has a default route).
339     */
340    public static final int TYPE_ETHERNET    = 9;
341
342    /**
343     * Over the air Administration.
344     * {@hide}
345     */
346    public static final int TYPE_MOBILE_FOTA = 10;
347
348    /**
349     * IP Multimedia Subsystem.
350     * {@hide}
351     */
352    public static final int TYPE_MOBILE_IMS  = 11;
353
354    /**
355     * Carrier Branded Services.
356     * {@hide}
357     */
358    public static final int TYPE_MOBILE_CBS  = 12;
359
360    /**
361     * A Wi-Fi p2p connection. Only requesting processes will have access to
362     * the peers connected.
363     * {@hide}
364     */
365    public static final int TYPE_WIFI_P2P    = 13;
366
367    /**
368     * The network to use for initially attaching to the network
369     * {@hide}
370     */
371    public static final int TYPE_MOBILE_IA = 14;
372
373    /**
374     * The network that uses proxy to achieve connectivity.
375     * {@hide}
376     */
377    public static final int TYPE_PROXY = 16;
378
379    /** {@hide} */
380    public static final int MAX_RADIO_TYPE   = TYPE_PROXY;
381
382    /** {@hide} */
383    public static final int MAX_NETWORK_TYPE = TYPE_PROXY;
384
385    /**
386     * If you want to set the default network preference,you can directly
387     * change the networkAttributes array in framework's config.xml.
388     *
389     * @deprecated Since we support so many more networks now, the single
390     *             network default network preference can't really express
391     *             the hierarchy.  Instead, the default is defined by the
392     *             networkAttributes in config.xml.  You can determine
393     *             the current value by calling {@link #getNetworkPreference()}
394     *             from an App.
395     */
396    @Deprecated
397    public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
398
399    /**
400     * Default value for {@link Settings.Global#CONNECTIVITY_CHANGE_DELAY} in
401     * milliseconds.  This was introduced because IPv6 routes seem to take a
402     * moment to settle - trying network activity before the routes are adjusted
403     * can lead to packets using the wrong interface or having the wrong IP address.
404     * This delay is a bit crude, but in the future hopefully we will have kernel
405     * notifications letting us know when it's safe to use the new network.
406     *
407     * @hide
408     */
409    public static final int CONNECTIVITY_CHANGE_DELAY_DEFAULT = 3000;
410
411    private final IConnectivityManager mService;
412
413    private final String mPackageName;
414
415    private INetworkManagementService mNMService;
416
417    /**
418     * Tests if a given integer represents a valid network type.
419     * @param networkType the type to be tested
420     * @return a boolean.  {@code true} if the type is valid, else {@code false}
421     */
422    public static boolean isNetworkTypeValid(int networkType) {
423        return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
424    }
425
426    /**
427     * Returns a non-localized string representing a given network type.
428     * ONLY used for debugging output.
429     * @param type the type needing naming
430     * @return a String for the given type, or a string version of the type ("87")
431     * if no name is known.
432     * {@hide}
433     */
434    public static String getNetworkTypeName(int type) {
435        switch (type) {
436            case TYPE_MOBILE:
437                return "MOBILE";
438            case TYPE_WIFI:
439                return "WIFI";
440            case TYPE_MOBILE_MMS:
441                return "MOBILE_MMS";
442            case TYPE_MOBILE_SUPL:
443                return "MOBILE_SUPL";
444            case TYPE_MOBILE_DUN:
445                return "MOBILE_DUN";
446            case TYPE_MOBILE_HIPRI:
447                return "MOBILE_HIPRI";
448            case TYPE_WIMAX:
449                return "WIMAX";
450            case TYPE_BLUETOOTH:
451                return "BLUETOOTH";
452            case TYPE_DUMMY:
453                return "DUMMY";
454            case TYPE_ETHERNET:
455                return "ETHERNET";
456            case TYPE_MOBILE_FOTA:
457                return "MOBILE_FOTA";
458            case TYPE_MOBILE_IMS:
459                return "MOBILE_IMS";
460            case TYPE_MOBILE_CBS:
461                return "MOBILE_CBS";
462            case TYPE_WIFI_P2P:
463                return "WIFI_P2P";
464            case TYPE_MOBILE_IA:
465                return "MOBILE_IA";
466            case TYPE_PROXY:
467                return "PROXY";
468            default:
469                return Integer.toString(type);
470        }
471    }
472
473    /**
474     * Checks if a given type uses the cellular data connection.
475     * This should be replaced in the future by a network property.
476     * @param networkType the type to check
477     * @return a boolean - {@code true} if uses cellular network, else {@code false}
478     * {@hide}
479     */
480    public static boolean isNetworkTypeMobile(int networkType) {
481        switch (networkType) {
482            case TYPE_MOBILE:
483            case TYPE_MOBILE_MMS:
484            case TYPE_MOBILE_SUPL:
485            case TYPE_MOBILE_DUN:
486            case TYPE_MOBILE_HIPRI:
487            case TYPE_MOBILE_FOTA:
488            case TYPE_MOBILE_IMS:
489            case TYPE_MOBILE_CBS:
490            case TYPE_MOBILE_IA:
491                return true;
492            default:
493                return false;
494        }
495    }
496
497    /**
498     * Checks if the given network type is backed by a Wi-Fi radio.
499     *
500     * @hide
501     */
502    public static boolean isNetworkTypeWifi(int networkType) {
503        switch (networkType) {
504            case TYPE_WIFI:
505            case TYPE_WIFI_P2P:
506                return true;
507            default:
508                return false;
509        }
510    }
511
512    /**
513     * Checks if the given network type should be exempt from VPN routing rules
514     *
515     * @hide
516     */
517    public static boolean isNetworkTypeExempt(int networkType) {
518        switch (networkType) {
519            case TYPE_MOBILE_MMS:
520            case TYPE_MOBILE_SUPL:
521            case TYPE_MOBILE_HIPRI:
522            case TYPE_MOBILE_IA:
523                return true;
524            default:
525                return false;
526        }
527    }
528
529    /**
530     * Specifies the preferred network type.  When the device has more
531     * than one type available the preferred network type will be used.
532     * Note that this made sense when we only had 2 network types,
533     * but with more and more default networks we need an array to list
534     * their ordering.  This will be deprecated soon.
535     *
536     * @param preference the network type to prefer over all others.  It is
537     *         unspecified what happens to the old preferred network in the
538     *         overall ordering.
539     */
540    public void setNetworkPreference(int preference) {
541        try {
542            mService.setNetworkPreference(preference);
543        } catch (RemoteException e) {
544        }
545    }
546
547    /**
548     * Retrieves the current preferred network type.
549     * Note that this made sense when we only had 2 network types,
550     * but with more and more default networks we need an array to list
551     * their ordering.  This will be deprecated soon.
552     *
553     * @return an integer representing the preferred network type
554     *
555     * <p>This method requires the caller to hold the permission
556     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
557     */
558    public int getNetworkPreference() {
559        try {
560            return mService.getNetworkPreference();
561        } catch (RemoteException e) {
562            return -1;
563        }
564    }
565
566    /**
567     * Returns details about the currently active default data network. When
568     * connected, this network is the default route for outgoing connections.
569     * You should always check {@link NetworkInfo#isConnected()} before initiating
570     * network traffic. This may return {@code null} when there is no default
571     * network.
572     *
573     * @return a {@link NetworkInfo} object for the current default network
574     *        or {@code null} if no network default network is currently active
575     *
576     * <p>This method requires the call to hold the permission
577     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
578     */
579    public NetworkInfo getActiveNetworkInfo() {
580        try {
581            return mService.getActiveNetworkInfo();
582        } catch (RemoteException e) {
583            return null;
584        }
585    }
586
587    /**
588     * Returns details about the currently active default data network
589     * for a given uid.  This is for internal use only to avoid spying
590     * other apps.
591     *
592     * @return a {@link NetworkInfo} object for the current default network
593     *        for the given uid or {@code null} if no default network is
594     *        available for the specified uid.
595     *
596     * <p>This method requires the caller to hold the permission
597     * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
598     * {@hide}
599     */
600    public NetworkInfo getActiveNetworkInfoForUid(int uid) {
601        try {
602            return mService.getActiveNetworkInfoForUid(uid);
603        } catch (RemoteException e) {
604            return null;
605        }
606    }
607
608    /**
609     * Returns connection status information about a particular
610     * network type.
611     *
612     * @param networkType integer specifying which networkType in
613     *        which you're interested.
614     * @return a {@link NetworkInfo} object for the requested
615     *        network type or {@code null} if the type is not
616     *        supported by the device.
617     *
618     * <p>This method requires the call to hold the permission
619     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
620     */
621    public NetworkInfo getNetworkInfo(int networkType) {
622        try {
623            return mService.getNetworkInfo(networkType);
624        } catch (RemoteException e) {
625            return null;
626        }
627    }
628
629    /**
630     * Returns connection status information about all network
631     * types supported by the device.
632     *
633     * @return an array of {@link NetworkInfo} objects.  Check each
634     * {@link NetworkInfo#getType} for which type each applies.
635     *
636     * <p>This method requires the call to hold the permission
637     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
638     */
639    public NetworkInfo[] getAllNetworkInfo() {
640        try {
641            return mService.getAllNetworkInfo();
642        } catch (RemoteException e) {
643            return null;
644        }
645    }
646
647    /**
648     * Returns details about the Provisioning or currently active default data network. When
649     * connected, this network is the default route for outgoing connections.
650     * You should always check {@link NetworkInfo#isConnected()} before initiating
651     * network traffic. This may return {@code null} when there is no default
652     * network.
653     *
654     * @return a {@link NetworkInfo} object for the current default network
655     *        or {@code null} if no network default network is currently active
656     *
657     * <p>This method requires the call to hold the permission
658     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
659     *
660     * {@hide}
661     */
662    public NetworkInfo getProvisioningOrActiveNetworkInfo() {
663        try {
664            return mService.getProvisioningOrActiveNetworkInfo();
665        } catch (RemoteException e) {
666            return null;
667        }
668    }
669
670    /**
671     * Returns the IP information for the current default network.
672     *
673     * @return a {@link LinkProperties} object describing the IP info
674     *        for the current default network, or {@code null} if there
675     *        is no current default network.
676     *
677     * <p>This method requires the call to hold the permission
678     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
679     * {@hide}
680     */
681    public LinkProperties getActiveLinkProperties() {
682        try {
683            return mService.getActiveLinkProperties();
684        } catch (RemoteException e) {
685            return null;
686        }
687    }
688
689    /**
690     * Returns the IP information for a given network type.
691     *
692     * @param networkType the network type of interest.
693     * @return a {@link LinkProperties} object describing the IP info
694     *        for the given networkType, or {@code null} if there is
695     *        no current default network.
696     *
697     * <p>This method requires the call to hold the permission
698     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
699     * {@hide}
700     */
701    public LinkProperties getLinkProperties(int networkType) {
702        try {
703            return mService.getLinkProperties(networkType);
704        } catch (RemoteException e) {
705            return null;
706        }
707    }
708
709    /**
710     * Tells each network type to set its radio power state as directed.
711     *
712     * @param turnOn a boolean, {@code true} to turn the radios on,
713     *        {@code false} to turn them off.
714     * @return a boolean, {@code true} indicating success.  All network types
715     *        will be tried, even if some fail.
716     *
717     * <p>This method requires the call to hold the permission
718     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
719     * {@hide}
720     */
721    public boolean setRadios(boolean turnOn) {
722        try {
723            return mService.setRadios(turnOn);
724        } catch (RemoteException e) {
725            return false;
726        }
727    }
728
729    /**
730     * Tells a given networkType to set its radio power state as directed.
731     *
732     * @param networkType the int networkType of interest.
733     * @param turnOn a boolean, {@code true} to turn the radio on,
734     *        {@code} false to turn it off.
735     * @return a boolean, {@code true} indicating success.
736     *
737     * <p>This method requires the call to hold the permission
738     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
739     * {@hide}
740     */
741    public boolean setRadio(int networkType, boolean turnOn) {
742        try {
743            return mService.setRadio(networkType, turnOn);
744        } catch (RemoteException e) {
745            return false;
746        }
747    }
748
749    /**
750     * Tells the underlying networking system that the caller wants to
751     * begin using the named feature. The interpretation of {@code feature}
752     * is completely up to each networking implementation.
753     * <p>This method requires the caller to hold the permission
754     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
755     * @param networkType specifies which network the request pertains to
756     * @param feature the name of the feature to be used
757     * @return an integer value representing the outcome of the request.
758     * The interpretation of this value is specific to each networking
759     * implementation+feature combination, except that the value {@code -1}
760     * always indicates failure.
761     */
762    public int startUsingNetworkFeature(int networkType, String feature) {
763        try {
764            return mService.startUsingNetworkFeature(networkType, feature,
765                    new Binder());
766        } catch (RemoteException e) {
767            return -1;
768        }
769    }
770
771    /**
772     * Tells the underlying networking system that the caller is finished
773     * using the named feature. The interpretation of {@code feature}
774     * is completely up to each networking implementation.
775     * <p>This method requires the caller to hold the permission
776     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
777     * @param networkType specifies which network the request pertains to
778     * @param feature the name of the feature that is no longer needed
779     * @return an integer value representing the outcome of the request.
780     * The interpretation of this value is specific to each networking
781     * implementation+feature combination, except that the value {@code -1}
782     * always indicates failure.
783     */
784    public int stopUsingNetworkFeature(int networkType, String feature) {
785        try {
786            return mService.stopUsingNetworkFeature(networkType, feature);
787        } catch (RemoteException e) {
788            return -1;
789        }
790    }
791
792    /**
793     * Ensure that a network route exists to deliver traffic to the specified
794     * host via the specified network interface. An attempt to add a route that
795     * already exists is ignored, but treated as successful.
796     * <p>This method requires the caller to hold the permission
797     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
798     * @param networkType the type of the network over which traffic to the specified
799     * host is to be routed
800     * @param hostAddress the IP address of the host to which the route is desired
801     * @return {@code true} on success, {@code false} on failure
802     */
803    public boolean requestRouteToHost(int networkType, int hostAddress) {
804        InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
805
806        if (inetAddress == null) {
807            return false;
808        }
809
810        return requestRouteToHostAddress(networkType, inetAddress);
811    }
812
813    /**
814     * Ensure that a network route exists to deliver traffic to the specified
815     * host via the specified network interface. An attempt to add a route that
816     * already exists is ignored, but treated as successful.
817     * <p>This method requires the caller to hold the permission
818     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
819     * @param networkType the type of the network over which traffic to the specified
820     * host is to be routed
821     * @param hostAddress the IP address of the host to which the route is desired
822     * @return {@code true} on success, {@code false} on failure
823     * @hide
824     */
825    public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
826        byte[] address = hostAddress.getAddress();
827        try {
828            return mService.requestRouteToHostAddress(networkType, address, mPackageName);
829        } catch (RemoteException e) {
830            return false;
831        }
832    }
833
834    /**
835     * Returns the value of the setting for background data usage. If false,
836     * applications should not use the network if the application is not in the
837     * foreground. Developers should respect this setting, and check the value
838     * of this before performing any background data operations.
839     * <p>
840     * All applications that have background services that use the network
841     * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
842     * <p>
843     * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
844     * background data depends on several combined factors, and this method will
845     * always return {@code true}. Instead, when background data is unavailable,
846     * {@link #getActiveNetworkInfo()} will now appear disconnected.
847     *
848     * @return Whether background data usage is allowed.
849     */
850    @Deprecated
851    public boolean getBackgroundDataSetting() {
852        // assume that background data is allowed; final authority is
853        // NetworkInfo which may be blocked.
854        return true;
855    }
856
857    /**
858     * Sets the value of the setting for background data usage.
859     *
860     * @param allowBackgroundData Whether an application should use data while
861     *            it is in the background.
862     *
863     * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
864     * @see #getBackgroundDataSetting()
865     * @hide
866     */
867    @Deprecated
868    public void setBackgroundDataSetting(boolean allowBackgroundData) {
869        // ignored
870    }
871
872    /**
873     * Return quota status for the current active network, or {@code null} if no
874     * network is active. Quota status can change rapidly, so these values
875     * shouldn't be cached.
876     *
877     * <p>This method requires the call to hold the permission
878     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
879     *
880     * @hide
881     */
882    public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
883        try {
884            return mService.getActiveNetworkQuotaInfo();
885        } catch (RemoteException e) {
886            return null;
887        }
888    }
889
890    /**
891     * Gets the value of the setting for enabling Mobile data.
892     *
893     * @return Whether mobile data is enabled.
894     *
895     * <p>This method requires the call to hold the permission
896     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
897     * @hide
898     */
899    public boolean getMobileDataEnabled() {
900        try {
901            return mService.getMobileDataEnabled();
902        } catch (RemoteException e) {
903            return true;
904        }
905    }
906
907    /**
908     * Sets the persisted value for enabling/disabling Mobile data.
909     *
910     * @param enabled Whether the user wants the mobile data connection used
911     *            or not.
912     * @hide
913     */
914    public void setMobileDataEnabled(boolean enabled) {
915        try {
916            mService.setMobileDataEnabled(enabled);
917        } catch (RemoteException e) {
918        }
919    }
920
921    /**
922     * Callback for use with {@link ConnectivityManager#registerNetworkActiveListener} to
923     * find out when the current network has gone in to a high power state.
924     */
925    public interface OnNetworkActiveListener {
926        /**
927         * Called on the main thread of the process to report that the current data network
928         * has become active, and it is now a good time to perform any pending network
929         * operations.  Note that this listener only tells you when the network becomes
930         * active; if at any other time you want to know whether it is active (and thus okay
931         * to initiate network traffic), you can retrieve its instantaneous state with
932         * {@link ConnectivityManager#isNetworkActive}.
933         */
934        public void onNetworkActive();
935    }
936
937    private INetworkManagementService getNetworkManagementService() {
938        synchronized (this) {
939            if (mNMService != null) {
940                return mNMService;
941            }
942            IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
943            mNMService = INetworkManagementService.Stub.asInterface(b);
944            return mNMService;
945        }
946    }
947
948    private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
949            mNetworkActivityListeners
950                    = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
951
952    /**
953     * Start listening to reports when the data network is active, meaning it is
954     * a good time to perform network traffic.  Use {@link #isNetworkActive()}
955     * to determine the current state of the network after registering the listener.
956     *
957     * @param l The listener to be told when the network is active.
958     */
959    public void registerNetworkActiveListener(final OnNetworkActiveListener l) {
960        INetworkActivityListener rl = new INetworkActivityListener.Stub() {
961            @Override
962            public void onNetworkActive() throws RemoteException {
963                l.onNetworkActive();
964            }
965        };
966
967        try {
968            getNetworkManagementService().registerNetworkActivityListener(rl);
969            mNetworkActivityListeners.put(l, rl);
970        } catch (RemoteException e) {
971        }
972    }
973
974    /**
975     * Remove network active listener previously registered with
976     * {@link #registerNetworkActiveListener}.
977     *
978     * @param l Previously registered listener.
979     */
980    public void unregisterNetworkActiveListener(OnNetworkActiveListener l) {
981        INetworkActivityListener rl = mNetworkActivityListeners.get(l);
982        if (rl == null) {
983            throw new IllegalArgumentException("Listener not registered: " + l);
984        }
985        try {
986            getNetworkManagementService().unregisterNetworkActivityListener(rl);
987        } catch (RemoteException e) {
988        }
989    }
990
991    /**
992     * Return whether the data network is currently active.  An active network means that
993     * it is currently in a high power state for performing data transmission.  On some
994     * types of networks, it may be expensive to move and stay in such a state, so it is
995     * more power efficient to batch network traffic together when the radio is already in
996     * this state.  This method tells you whether right now is currently a good time to
997     * initiate network traffic, as the network is already active.
998     */
999    public boolean isNetworkActive() {
1000        try {
1001            return getNetworkManagementService().isNetworkActive();
1002        } catch (RemoteException e) {
1003        }
1004        return false;
1005    }
1006
1007    /**
1008     * {@hide}
1009     */
1010    public ConnectivityManager(IConnectivityManager service, String packageName) {
1011        mService = checkNotNull(service, "missing IConnectivityManager");
1012        mPackageName = checkNotNull(packageName, "missing package name");
1013    }
1014
1015    /** {@hide} */
1016    public static ConnectivityManager from(Context context) {
1017        return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1018    }
1019
1020    /**
1021     * Get the set of tetherable, available interfaces.  This list is limited by
1022     * device configuration and current interface existence.
1023     *
1024     * @return an array of 0 or more Strings of tetherable interface names.
1025     *
1026     * <p>This method requires the call to hold the permission
1027     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1028     * {@hide}
1029     */
1030    public String[] getTetherableIfaces() {
1031        try {
1032            return mService.getTetherableIfaces();
1033        } catch (RemoteException e) {
1034            return new String[0];
1035        }
1036    }
1037
1038    /**
1039     * Get the set of tethered interfaces.
1040     *
1041     * @return an array of 0 or more String of currently tethered interface names.
1042     *
1043     * <p>This method requires the call to hold the permission
1044     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1045     * {@hide}
1046     */
1047    public String[] getTetheredIfaces() {
1048        try {
1049            return mService.getTetheredIfaces();
1050        } catch (RemoteException e) {
1051            return new String[0];
1052        }
1053    }
1054
1055    /**
1056     * Get the set of interface names which attempted to tether but
1057     * failed.  Re-attempting to tether may cause them to reset to the Tethered
1058     * state.  Alternatively, causing the interface to be destroyed and recreated
1059     * may cause them to reset to the available state.
1060     * {@link ConnectivityManager#getLastTetherError} can be used to get more
1061     * information on the cause of the errors.
1062     *
1063     * @return an array of 0 or more String indicating the interface names
1064     *        which failed to tether.
1065     *
1066     * <p>This method requires the call to hold the permission
1067     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1068     * {@hide}
1069     */
1070    public String[] getTetheringErroredIfaces() {
1071        try {
1072            return mService.getTetheringErroredIfaces();
1073        } catch (RemoteException e) {
1074            return new String[0];
1075        }
1076    }
1077
1078    /**
1079     * Attempt to tether the named interface.  This will setup a dhcp server
1080     * on the interface, forward and NAT IP packets and forward DNS requests
1081     * to the best active upstream network interface.  Note that if no upstream
1082     * IP network interface is available, dhcp will still run and traffic will be
1083     * allowed between the tethered devices and this device, though upstream net
1084     * access will of course fail until an upstream network interface becomes
1085     * active.
1086     *
1087     * @param iface the interface name to tether.
1088     * @return error a {@code TETHER_ERROR} value indicating success or failure type
1089     *
1090     * <p>This method requires the call to hold the permission
1091     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
1092     * {@hide}
1093     */
1094    public int tether(String iface) {
1095        try {
1096            return mService.tether(iface);
1097        } catch (RemoteException e) {
1098            return TETHER_ERROR_SERVICE_UNAVAIL;
1099        }
1100    }
1101
1102    /**
1103     * Stop tethering the named interface.
1104     *
1105     * @param iface the interface name to untether.
1106     * @return error a {@code TETHER_ERROR} value indicating success or failure type
1107     *
1108     * <p>This method requires the call to hold the permission
1109     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
1110     * {@hide}
1111     */
1112    public int untether(String iface) {
1113        try {
1114            return mService.untether(iface);
1115        } catch (RemoteException e) {
1116            return TETHER_ERROR_SERVICE_UNAVAIL;
1117        }
1118    }
1119
1120    /**
1121     * Check if the device allows for tethering.  It may be disabled via
1122     * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
1123     * due to device configuration.
1124     *
1125     * @return a boolean - {@code true} indicating Tethering is supported.
1126     *
1127     * <p>This method requires the call to hold the permission
1128     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1129     * {@hide}
1130     */
1131    public boolean isTetheringSupported() {
1132        try {
1133            return mService.isTetheringSupported();
1134        } catch (RemoteException e) {
1135            return false;
1136        }
1137    }
1138
1139    /**
1140     * Get the list of regular expressions that define any tetherable
1141     * USB network interfaces.  If USB tethering is not supported by the
1142     * device, this list should be empty.
1143     *
1144     * @return an array of 0 or more regular expression Strings defining
1145     *        what interfaces are considered tetherable usb interfaces.
1146     *
1147     * <p>This method requires the call to hold the permission
1148     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1149     * {@hide}
1150     */
1151    public String[] getTetherableUsbRegexs() {
1152        try {
1153            return mService.getTetherableUsbRegexs();
1154        } catch (RemoteException e) {
1155            return new String[0];
1156        }
1157    }
1158
1159    /**
1160     * Get the list of regular expressions that define any tetherable
1161     * Wifi network interfaces.  If Wifi tethering is not supported by the
1162     * device, this list should be empty.
1163     *
1164     * @return an array of 0 or more regular expression Strings defining
1165     *        what interfaces are considered tetherable wifi interfaces.
1166     *
1167     * <p>This method requires the call to hold the permission
1168     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1169     * {@hide}
1170     */
1171    public String[] getTetherableWifiRegexs() {
1172        try {
1173            return mService.getTetherableWifiRegexs();
1174        } catch (RemoteException e) {
1175            return new String[0];
1176        }
1177    }
1178
1179    /**
1180     * Get the list of regular expressions that define any tetherable
1181     * Bluetooth network interfaces.  If Bluetooth tethering is not supported by the
1182     * device, this list should be empty.
1183     *
1184     * @return an array of 0 or more regular expression Strings defining
1185     *        what interfaces are considered tetherable bluetooth interfaces.
1186     *
1187     * <p>This method requires the call to hold the permission
1188     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1189     * {@hide}
1190     */
1191    public String[] getTetherableBluetoothRegexs() {
1192        try {
1193            return mService.getTetherableBluetoothRegexs();
1194        } catch (RemoteException e) {
1195            return new String[0];
1196        }
1197    }
1198
1199    /**
1200     * Attempt to both alter the mode of USB and Tethering of USB.  A
1201     * utility method to deal with some of the complexity of USB - will
1202     * attempt to switch to Rndis and subsequently tether the resulting
1203     * interface on {@code true} or turn off tethering and switch off
1204     * Rndis on {@code false}.
1205     *
1206     * @param enable a boolean - {@code true} to enable tethering
1207     * @return error a {@code TETHER_ERROR} value indicating success or failure type
1208     *
1209     * <p>This method requires the call to hold the permission
1210     * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
1211     * {@hide}
1212     */
1213    public int setUsbTethering(boolean enable) {
1214        try {
1215            return mService.setUsbTethering(enable);
1216        } catch (RemoteException e) {
1217            return TETHER_ERROR_SERVICE_UNAVAIL;
1218        }
1219    }
1220
1221    /** {@hide} */
1222    public static final int TETHER_ERROR_NO_ERROR           = 0;
1223    /** {@hide} */
1224    public static final int TETHER_ERROR_UNKNOWN_IFACE      = 1;
1225    /** {@hide} */
1226    public static final int TETHER_ERROR_SERVICE_UNAVAIL    = 2;
1227    /** {@hide} */
1228    public static final int TETHER_ERROR_UNSUPPORTED        = 3;
1229    /** {@hide} */
1230    public static final int TETHER_ERROR_UNAVAIL_IFACE      = 4;
1231    /** {@hide} */
1232    public static final int TETHER_ERROR_MASTER_ERROR       = 5;
1233    /** {@hide} */
1234    public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
1235    /** {@hide} */
1236    public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
1237    /** {@hide} */
1238    public static final int TETHER_ERROR_ENABLE_NAT_ERROR     = 8;
1239    /** {@hide} */
1240    public static final int TETHER_ERROR_DISABLE_NAT_ERROR    = 9;
1241    /** {@hide} */
1242    public static final int TETHER_ERROR_IFACE_CFG_ERROR      = 10;
1243
1244    /**
1245     * Get a more detailed error code after a Tethering or Untethering
1246     * request asynchronously failed.
1247     *
1248     * @param iface The name of the interface of interest
1249     * @return error The error code of the last error tethering or untethering the named
1250     *               interface
1251     *
1252     * <p>This method requires the call to hold the permission
1253     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1254     * {@hide}
1255     */
1256    public int getLastTetherError(String iface) {
1257        try {
1258            return mService.getLastTetherError(iface);
1259        } catch (RemoteException e) {
1260            return TETHER_ERROR_SERVICE_UNAVAIL;
1261        }
1262    }
1263
1264    /**
1265     * Try to ensure the device stays awake until we connect with the next network.
1266     * Actually just holds a wakelock for a number of seconds while we try to connect
1267     * to any default networks.  This will expire if the timeout passes or if we connect
1268     * to a default after this is called.  For internal use only.
1269     *
1270     * @param forWhom the name of the network going down for logging purposes
1271     * @return {@code true} on success, {@code false} on failure
1272     *
1273     * <p>This method requires the call to hold the permission
1274     * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1275     * {@hide}
1276     */
1277    public boolean requestNetworkTransitionWakelock(String forWhom) {
1278        try {
1279            mService.requestNetworkTransitionWakelock(forWhom);
1280            return true;
1281        } catch (RemoteException e) {
1282            return false;
1283        }
1284    }
1285
1286    /**
1287     * Report network connectivity status.  This is currently used only
1288     * to alter status bar UI.
1289     *
1290     * @param networkType The type of network you want to report on
1291     * @param percentage The quality of the connection 0 is bad, 100 is good
1292     *
1293     * <p>This method requires the call to hold the permission
1294     * {@link android.Manifest.permission#STATUS_BAR}.
1295     * {@hide}
1296     */
1297    public void reportInetCondition(int networkType, int percentage) {
1298        try {
1299            mService.reportInetCondition(networkType, percentage);
1300        } catch (RemoteException e) {
1301        }
1302    }
1303
1304    /**
1305     * Set a network-independent global http proxy.  This is not normally what you want
1306     * for typical HTTP proxies - they are general network dependent.  However if you're
1307     * doing something unusual like general internal filtering this may be useful.  On
1308     * a private network where the proxy is not accessible, you may break HTTP using this.
1309     *
1310     * @param p The a {@link ProxyProperties} object defining the new global
1311     *        HTTP proxy.  A {@code null} value will clear the global HTTP proxy.
1312     *
1313     * <p>This method requires the call to hold the permission
1314     * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1315     * {@hide}
1316     */
1317    public void setGlobalProxy(ProxyProperties p) {
1318        try {
1319            mService.setGlobalProxy(p);
1320        } catch (RemoteException e) {
1321        }
1322    }
1323
1324    /**
1325     * Retrieve any network-independent global HTTP proxy.
1326     *
1327     * @return {@link ProxyProperties} for the current global HTTP proxy or {@code null}
1328     *        if no global HTTP proxy is set.
1329     *
1330     * <p>This method requires the call to hold the permission
1331     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1332     * {@hide}
1333     */
1334    public ProxyProperties getGlobalProxy() {
1335        try {
1336            return mService.getGlobalProxy();
1337        } catch (RemoteException e) {
1338            return null;
1339        }
1340    }
1341
1342    /**
1343     * Get the HTTP proxy settings for the current default network.  Note that
1344     * if a global proxy is set, it will override any per-network setting.
1345     *
1346     * @return the {@link ProxyProperties} for the current HTTP proxy, or {@code null} if no
1347     *        HTTP proxy is active.
1348     *
1349     * <p>This method requires the call to hold the permission
1350     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1351     * {@hide}
1352     */
1353    public ProxyProperties getProxy() {
1354        try {
1355            return mService.getProxy();
1356        } catch (RemoteException e) {
1357            return null;
1358        }
1359    }
1360
1361    /**
1362     * Sets a secondary requirement bit for the given networkType.
1363     * This requirement bit is generally under the control of the carrier
1364     * or its agents and is not directly controlled by the user.
1365     *
1366     * @param networkType The network who's dependence has changed
1367     * @param met Boolean - true if network use is OK, false if not
1368     *
1369     * <p>This method requires the call to hold the permission
1370     * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1371     * {@hide}
1372     */
1373    public void setDataDependency(int networkType, boolean met) {
1374        try {
1375            mService.setDataDependency(networkType, met);
1376        } catch (RemoteException e) {
1377        }
1378    }
1379
1380    /**
1381     * Returns true if the hardware supports the given network type
1382     * else it returns false.  This doesn't indicate we have coverage
1383     * or are authorized onto a network, just whether or not the
1384     * hardware supports it.  For example a GSM phone without a SIM
1385     * should still return {@code true} for mobile data, but a wifi only
1386     * tablet would return {@code false}.
1387     *
1388     * @param networkType The network type we'd like to check
1389     * @return {@code true} if supported, else {@code false}
1390     *
1391     * <p>This method requires the call to hold the permission
1392     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1393     * @hide
1394     */
1395    public boolean isNetworkSupported(int networkType) {
1396        try {
1397            return mService.isNetworkSupported(networkType);
1398        } catch (RemoteException e) {}
1399        return false;
1400    }
1401
1402    /**
1403     * Returns if the currently active data network is metered. A network is
1404     * classified as metered when the user is sensitive to heavy data usage on
1405     * that connection due to monetary costs, data limitations or
1406     * battery/performance issues. You should check this before doing large
1407     * data transfers, and warn the user or delay the operation until another
1408     * network is available.
1409     *
1410     * @return {@code true} if large transfers should be avoided, otherwise
1411     *        {@code false}.
1412     *
1413     * <p>This method requires the call to hold the permission
1414     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1415     */
1416    public boolean isActiveNetworkMetered() {
1417        try {
1418            return mService.isActiveNetworkMetered();
1419        } catch (RemoteException e) {
1420            return false;
1421        }
1422    }
1423
1424    /**
1425     * If the LockdownVpn mechanism is enabled, updates the vpn
1426     * with a reload of its profile.
1427     *
1428     * @return a boolean with {@code} indicating success
1429     *
1430     * <p>This method can only be called by the system UID
1431     * {@hide}
1432     */
1433    public boolean updateLockdownVpn() {
1434        try {
1435            return mService.updateLockdownVpn();
1436        } catch (RemoteException e) {
1437            return false;
1438        }
1439    }
1440
1441    /**
1442     * Signal that the captive portal check on the indicated network
1443     * is complete and whether its a captive portal or not.
1444     *
1445     * @param info the {@link NetworkInfo} object for the networkType
1446     *        in question.
1447     * @param isCaptivePortal true/false.
1448     *
1449     * <p>This method requires the call to hold the permission
1450     * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1451     * {@hide}
1452     */
1453    public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) {
1454        try {
1455            mService.captivePortalCheckCompleted(info, isCaptivePortal);
1456        } catch (RemoteException e) {
1457        }
1458    }
1459
1460    /**
1461     * Supply the backend messenger for a network tracker
1462     *
1463     * @param networkType NetworkType to set
1464     * @param messenger {@link Messenger}
1465     * {@hide}
1466     */
1467    public void supplyMessenger(int networkType, Messenger messenger) {
1468        try {
1469            mService.supplyMessenger(networkType, messenger);
1470        } catch (RemoteException e) {
1471        }
1472    }
1473
1474    /**
1475     * Check mobile provisioning.
1476     *
1477     * @param suggestedTimeOutMs, timeout in milliseconds
1478     *
1479     * @return time out that will be used, maybe less that suggestedTimeOutMs
1480     * -1 if an error.
1481     *
1482     * {@hide}
1483     */
1484    public int checkMobileProvisioning(int suggestedTimeOutMs) {
1485        int timeOutMs = -1;
1486        try {
1487            timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
1488        } catch (RemoteException e) {
1489        }
1490        return timeOutMs;
1491    }
1492
1493    /**
1494     * Get the mobile provisioning url.
1495     * {@hide}
1496     */
1497    public String getMobileProvisioningUrl() {
1498        try {
1499            return mService.getMobileProvisioningUrl();
1500        } catch (RemoteException e) {
1501        }
1502        return null;
1503    }
1504
1505    /**
1506     * Get the mobile redirected provisioning url.
1507     * {@hide}
1508     */
1509    public String getMobileRedirectedProvisioningUrl() {
1510        try {
1511            return mService.getMobileRedirectedProvisioningUrl();
1512        } catch (RemoteException e) {
1513        }
1514        return null;
1515    }
1516
1517    /**
1518     * get the information about a specific network link
1519     * @hide
1520     */
1521    public LinkQualityInfo getLinkQualityInfo(int networkType) {
1522        try {
1523            LinkQualityInfo li = mService.getLinkQualityInfo(networkType);
1524            return li;
1525        } catch (RemoteException e) {
1526            return null;
1527        }
1528    }
1529
1530    /**
1531     * get the information of currently active network link
1532     * @hide
1533     */
1534    public LinkQualityInfo getActiveLinkQualityInfo() {
1535        try {
1536            LinkQualityInfo li = mService.getActiveLinkQualityInfo();
1537            return li;
1538        } catch (RemoteException e) {
1539            return null;
1540        }
1541    }
1542
1543    /**
1544     * get the information of all network links
1545     * @hide
1546     */
1547    public LinkQualityInfo[] getAllLinkQualityInfo() {
1548        try {
1549            LinkQualityInfo[] li = mService.getAllLinkQualityInfo();
1550            return li;
1551        } catch (RemoteException e) {
1552            return null;
1553        }
1554    }
1555
1556    /**
1557     * Set sign in error notification to visible or in visible
1558     *
1559     * @param visible
1560     * @param networkType
1561     *
1562     * {@hide}
1563     */
1564    public void setProvisioningNotificationVisible(boolean visible, int networkType,
1565            String extraInfo, String url) {
1566        try {
1567            mService.setProvisioningNotificationVisible(visible, networkType, extraInfo, url);
1568        } catch (RemoteException e) {
1569        }
1570    }
1571
1572    /**
1573     * Set the value for enabling/disabling airplane mode
1574     *
1575     * @param enable whether to enable airplane mode or not
1576     *
1577     * <p>This method requires the call to hold the permission
1578     * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1579     * @hide
1580     */
1581    public void setAirplaneMode(boolean enable) {
1582        try {
1583            mService.setAirplaneMode(enable);
1584        } catch (RemoteException e) {
1585        }
1586    }
1587}
1588