ConnectivityManager.java revision 6081084e8831d12d2dd5a9a340ee7d35a54edb8a
1a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten/*
2a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten * Copyright (C) 2008 The Android Open Source Project
3a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten *
4a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten * Licensed under the Apache License, Version 2.0 (the "License");
5a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten * you may not use this file except in compliance with the License.
6a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten * You may obtain a copy of the License at
7a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten *
8a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten *      http://www.apache.org/licenses/LICENSE-2.0
9a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten *
10a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten * Unless required by applicable law or agreed to in writing, software
11a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten * distributed under the License is distributed on an "AS IS" BASIS,
12a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten * See the License for the specific language governing permissions and
14a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten * limitations under the License.
15a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten */
16a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten
17a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kastenpackage android.net;
18a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten
19a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kastenimport android.annotation.SdkConstant;
20a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kastenimport android.annotation.SdkConstant.SdkConstantType;
21ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kastenimport android.os.Binder;
22a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kastenimport android.os.RemoteException;
23a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten
24ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kastenimport java.net.InetAddress;
25ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kastenimport java.net.UnknownHostException;
266a357b8fa57b0bc1557cd5ab9f9fb86aabaaa18cGlenn Kasten
276a357b8fa57b0bc1557cd5ab9f9fb86aabaaa18cGlenn Kasten/**
28ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten * Class that answers queries about the state of network connectivity. It also
296a357b8fa57b0bc1557cd5ab9f9fb86aabaaa18cGlenn Kasten * notifies applications when network connectivity changes. Get an instance
30928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten * of this class by calling
31928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
32928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten * <p>
33928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten * The primary responsibilities of this class are to:
34928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten * <ol>
356a357b8fa57b0bc1557cd5ab9f9fb86aabaaa18cGlenn Kasten * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
36ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten * <li>Send broadcast intents when network connectivity changes</li>
37bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten * <li>Attempt to "fail over" to another network when connectivity to a network
38bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten * is lost</li>
39f51dba65751107c930759938775b75531ec1f330Glenn Kasten * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
40f51dba65751107c930759938775b75531ec1f330Glenn Kasten * state of the available networks</li>
41f51dba65751107c930759938775b75531ec1f330Glenn Kasten * </ol>
42f51dba65751107c930759938775b75531ec1f330Glenn Kasten */
43f51dba65751107c930759938775b75531ec1f330Glenn Kastenpublic class ConnectivityManager
44bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten{
45bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    /**
46ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * A change in network connectivity has occurred. A connection has either
47ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * been established or lost. The NetworkInfo for the affected network is
48ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * sent as an extra; it should be consulted to see what kind of
49928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten     * connectivity event occurred.
50928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten     * <p/>
51928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten     * If this is a connection that was the result of failing over from a
52ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * disconnected network, then the FAILOVER_CONNECTION boolean extra is
53ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * set to true.
54928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten     * <p/>
55ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * For a loss of connectivity, if the connectivity manager is attempting
56ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * to connect (or has already connected) to another network, the
57ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * NetworkInfo for the new network is also passed as an extra. This lets
58928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten     * any receivers of the broadcast know that they should not necessarily
59928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten     * tell the user that no data traffic will be possible. Instead, the
60928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten     * reciever should expect another broadcast soon, indicating either that
61928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten     * the failover attempt succeeded (and so there is still overall data
62ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * connectivity), or that the failover attempt failed, meaning that all
63ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * connectivity has been lost.
64928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten     * <p/>
65ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
66bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten     * is set to {@code true} if there are no connected networks at all.
676a357b8fa57b0bc1557cd5ab9f9fb86aabaaa18cGlenn Kasten     */
68bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
696a357b8fa57b0bc1557cd5ab9f9fb86aabaaa18cGlenn Kasten    /**
70ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * The lookup key for a {@link NetworkInfo} object. Retrieve with
71ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * {@link android.content.Intent#getParcelableExtra(String)}.
72a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten     */
73a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten    public static final String EXTRA_NETWORK_INFO = "networkInfo";
74ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    /**
75d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kasten     * The lookup key for a boolean that indicates whether a connect event
76a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten     * is for a network to which the connectivity manager was failing over
77ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * following a disconnect on another network.
78ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
79ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     */
80ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    public static final String EXTRA_IS_FAILOVER = "isFailover";
81ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    /**
82bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten     * The lookup key for a {@link NetworkInfo} object. This is supplied when
83bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten     * there is another network that it may be possible to connect to. Retrieve with
84bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten     * {@link android.content.Intent#getParcelableExtra(String)}.
85ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     */
86bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
87ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    /**
88ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * The lookup key for a boolean that indicates whether there is a
89ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * complete lack of connectivity, i.e., no network is available.
90ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten     * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
91a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten     */
92a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten    public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
93ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    /**
94a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten     * The lookup key for a string that indicates why an attempt to connect
95a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten     * to a network failed. The string has no particular structure. It is
96a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten     * intended to be used in notifications presented to users. Retrieve
97a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten     * it with {@link android.content.Intent#getStringExtra(String)}.
98a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten     */
99a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten    public static final String EXTRA_REASON = "reason";
100a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten    /**
101bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten     * The lookup key for a string that provides optionally supplied
102bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten     * extra information about the network state. The information
103bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten     * may be passed up from the lower networking layers, and its
104a6d984c32855a239f7f79a3d3b7f2ddfb8cb9697Glenn Kasten     * meaning may be specific to a particular network type. Retrieve
105f51dba65751107c930759938775b75531ec1f330Glenn Kasten     * it with {@link android.content.Intent#getStringExtra(String)}.
106f51dba65751107c930759938775b75531ec1f330Glenn Kasten     */
107f51dba65751107c930759938775b75531ec1f330Glenn Kasten    public static final String EXTRA_EXTRA_INFO = "extraInfo";
108bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    /**
109bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten     * The lookup key for an int that provides information about
110f51dba65751107c930759938775b75531ec1f330Glenn Kasten     * our connection to the internet at large.  0 indicates no connection,
111bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten     * 100 indicates a great connection.  Retrieve it with
112f51dba65751107c930759938775b75531ec1f330Glenn Kasten     * {@link android.content.Intent@getIntExtra(String)}.
113f51dba65751107c930759938775b75531ec1f330Glenn Kasten     * {@hide}
114f51dba65751107c930759938775b75531ec1f330Glenn Kasten     */
115f51dba65751107c930759938775b75531ec1f330Glenn Kasten    public static final String EXTRA_INET_CONDITION = "inetCondition";
116f51dba65751107c930759938775b75531ec1f330Glenn Kasten
117f51dba65751107c930759938775b75531ec1f330Glenn Kasten    /**
118f51dba65751107c930759938775b75531ec1f330Glenn Kasten     * Broadcast Action: The setting for background data usage has changed
119     * values. Use {@link #getBackgroundDataSetting()} to get the current value.
120     * <p>
121     * If an application uses the network in the background, it should listen
122     * for this broadcast and stop using the background data if the value is
123     * false.
124     */
125    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
126    public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
127            "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
128
129
130    /**
131     * Broadcast Action: The network connection may not be good
132     * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
133     * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
134     * the network and it's condition.
135     * @hide
136     */
137    public static final String INET_CONDITION_ACTION =
138            "android.net.conn.INET_CONDITION_ACTION";
139
140    /**
141     * Broadcast Action: A tetherable connection has come or gone
142     * TODO - finish the doc
143     * @hide
144     */
145    public static final String ACTION_TETHER_STATE_CHANGED =
146            "android.net.conn.TETHER_STATE_CHANGED";
147
148    /**
149     * @hide
150     * gives a String[]
151     */
152    public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
153
154    /**
155     * @hide
156     * gives a String[]
157     */
158    public static final String EXTRA_ACTIVE_TETHER = "activeArray";
159
160    /**
161     * @hide
162     * gives a String[]
163     */
164    public static final String EXTRA_ERRORED_TETHER = "erroredArray";
165
166    /**
167     * The Default Mobile data connection.  When active, all data traffic
168     * will use this connection by default.  Should not coexist with other
169     * default connections.
170     */
171    public static final int TYPE_MOBILE      = 0;
172    /**
173     * The Default WIFI data connection.  When active, all data traffic
174     * will use this connection by default.  Should not coexist with other
175     * default connections.
176     */
177    public static final int TYPE_WIFI        = 1;
178    /**
179     * An MMS-specific Mobile data connection.  This connection may be the
180     * same as {@link #TYPE_MOBILE} but it may be different.  This is used
181     * by applications needing to talk to the carrier's Multimedia Messaging
182     * Service servers.  It may coexist with default data connections.
183     */
184    public static final int TYPE_MOBILE_MMS  = 2;
185    /**
186     * A SUPL-specific Mobile data connection.  This connection may be the
187     * same as {@link #TYPE_MOBILE} but it may be different.  This is used
188     * by applications needing to talk to the carrier's Secure User Plane
189     * Location servers for help locating the device.  It may coexist with
190     * default data connections.
191     */
192    public static final int TYPE_MOBILE_SUPL = 3;
193    /**
194     * A DUN-specific Mobile data connection.  This connection may be the
195     * same as {@link #TYPE_MOBILE} but it may be different.  This is used
196     * by applicaitons performing a Dial Up Networking bridge so that
197     * the carrier is aware of DUN traffic.  It may coexist with default data
198     * connections.
199     */
200    public static final int TYPE_MOBILE_DUN  = 4;
201    /**
202     * A High Priority Mobile data connection.  This connection is typically
203     * the same as {@link #TYPE_MOBILE} but the routing setup is different.
204     * Only requesting processes will have access to the Mobile DNS servers
205     * and only IP's explicitly requested via {@link #requestRouteToHost}
206     * will route over this interface if a default route exists.
207     */
208    public static final int TYPE_MOBILE_HIPRI = 5;
209    /**
210     * The Default WiMAX data connection.  When active, all data traffic
211     * will use this connection by default.  Should not coexist with other
212     * default connections.
213     */
214    public static final int TYPE_WIMAX       = 6;
215
216    /**
217     * Bluetooth data connection. This is used for Bluetooth reverse tethering.
218     */
219    public static final int TYPE_BLUETOOTH   = 7;
220
221    /**
222     * Dummy data connection.  This should not be used on shipping devices.
223     */
224    public static final int TYPE_DUMMY       = 8;
225
226    /**
227     * Ethernet data connection.  This may be via USB dongle or more
228     * traditional means.
229     */
230    public static final int TYPE_ETHERNET    = 9;
231
232    /**
233     * Over the air Adminstration.
234     * {@hide}
235     */
236    public static final int TYPE_MOBILE_FOTA = 10;
237
238    /**
239     * IP Multimedia Subsystem
240     * {@hide}
241     */
242    public static final int TYPE_MOBILE_IMS  = 11;
243
244    /**
245     * Carrier Branded Services
246     * {@hide}
247     */
248    public static final int TYPE_MOBILE_CBS  = 12;
249
250    /** {@hide} */
251    public static final int MAX_RADIO_TYPE   = TYPE_MOBILE_CBS;
252
253    /** {@hide} */
254    public static final int MAX_NETWORK_TYPE = TYPE_MOBILE_CBS;
255
256    public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
257
258    private IConnectivityManager mService;
259
260    static public boolean isNetworkTypeValid(int networkType) {
261        return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
262    }
263
264    public void setNetworkPreference(int preference) {
265        try {
266            mService.setNetworkPreference(preference);
267        } catch (RemoteException e) {
268        }
269    }
270
271    public int getNetworkPreference() {
272        try {
273            return mService.getNetworkPreference();
274        } catch (RemoteException e) {
275            return -1;
276        }
277    }
278
279    public NetworkInfo getActiveNetworkInfo() {
280        try {
281            return mService.getActiveNetworkInfo();
282        } catch (RemoteException e) {
283            return null;
284        }
285    }
286
287    public NetworkInfo getNetworkInfo(int networkType) {
288        try {
289            return mService.getNetworkInfo(networkType);
290        } catch (RemoteException e) {
291            return null;
292        }
293    }
294
295    public NetworkInfo[] getAllNetworkInfo() {
296        try {
297            return mService.getAllNetworkInfo();
298        } catch (RemoteException e) {
299            return null;
300        }
301    }
302
303    /** @hide */
304    public LinkProperties getActiveLinkProperties() {
305        try {
306            return mService.getActiveLinkProperties();
307        } catch (RemoteException e) {
308            return null;
309        }
310    }
311
312    /** @hide */
313    public LinkProperties getLinkProperties(int networkType) {
314        try {
315            return mService.getLinkProperties(networkType);
316        } catch (RemoteException e) {
317            return null;
318        }
319    }
320
321    /** {@hide} */
322    public boolean setRadios(boolean turnOn) {
323        try {
324            return mService.setRadios(turnOn);
325        } catch (RemoteException e) {
326            return false;
327        }
328    }
329
330    /** {@hide} */
331    public boolean setRadio(int networkType, boolean turnOn) {
332        try {
333            return mService.setRadio(networkType, turnOn);
334        } catch (RemoteException e) {
335            return false;
336        }
337    }
338
339    /**
340     * Tells the underlying networking system that the caller wants to
341     * begin using the named feature. The interpretation of {@code feature}
342     * is completely up to each networking implementation.
343     * @param networkType specifies which network the request pertains to
344     * @param feature the name of the feature to be used
345     * @return an integer value representing the outcome of the request.
346     * The interpretation of this value is specific to each networking
347     * implementation+feature combination, except that the value {@code -1}
348     * always indicates failure.
349     */
350    public int startUsingNetworkFeature(int networkType, String feature) {
351        try {
352            return mService.startUsingNetworkFeature(networkType, feature,
353                    new Binder());
354        } catch (RemoteException e) {
355            return -1;
356        }
357    }
358
359    /**
360     * Tells the underlying networking system that the caller is finished
361     * using the named feature. The interpretation of {@code feature}
362     * is completely up to each networking implementation.
363     * @param networkType specifies which network the request pertains to
364     * @param feature the name of the feature that is no longer needed
365     * @return an integer value representing the outcome of the request.
366     * The interpretation of this value is specific to each networking
367     * implementation+feature combination, except that the value {@code -1}
368     * always indicates failure.
369     */
370    public int stopUsingNetworkFeature(int networkType, String feature) {
371        try {
372            return mService.stopUsingNetworkFeature(networkType, feature);
373        } catch (RemoteException e) {
374            return -1;
375        }
376    }
377
378    /**
379     * Ensure that a network route exists to deliver traffic to the specified
380     * host via the specified network interface. An attempt to add a route that
381     * already exists is ignored, but treated as successful.
382     * @param networkType the type of the network over which traffic to the specified
383     * host is to be routed
384     * @param hostAddress the IP address of the host to which the route is desired
385     * @return {@code true} on success, {@code false} on failure
386     */
387    public boolean requestRouteToHost(int networkType, int hostAddress) {
388        InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
389
390        if (inetAddress == null) {
391            return false;
392        }
393
394        return requestRouteToHostAddress(networkType, inetAddress);
395    }
396
397    /**
398     * Ensure that a network route exists to deliver traffic to the specified
399     * host via the specified network interface. An attempt to add a route that
400     * already exists is ignored, but treated as successful.
401     * @param networkType the type of the network over which traffic to the specified
402     * host is to be routed
403     * @param hostAddress the IP address of the host to which the route is desired
404     * @return {@code true} on success, {@code false} on failure
405     * @hide
406     */
407    public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
408        byte[] address = hostAddress.getAddress();
409        try {
410            return mService.requestRouteToHostAddress(networkType, address);
411        } catch (RemoteException e) {
412            return false;
413        }
414    }
415
416    /**
417     * Returns the value of the setting for background data usage. If false,
418     * applications should not use the network if the application is not in the
419     * foreground. Developers should respect this setting, and check the value
420     * of this before performing any background data operations.
421     * <p>
422     * All applications that have background services that use the network
423     * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
424     *
425     * @return Whether background data usage is allowed.
426     */
427    public boolean getBackgroundDataSetting() {
428        try {
429            return mService.getBackgroundDataSetting();
430        } catch (RemoteException e) {
431            // Err on the side of safety
432            return false;
433        }
434    }
435
436    /**
437     * Sets the value of the setting for background data usage.
438     *
439     * @param allowBackgroundData Whether an application should use data while
440     *            it is in the background.
441     *
442     * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
443     * @see #getBackgroundDataSetting()
444     * @hide
445     */
446    public void setBackgroundDataSetting(boolean allowBackgroundData) {
447        try {
448            mService.setBackgroundDataSetting(allowBackgroundData);
449        } catch (RemoteException e) {
450        }
451    }
452
453    /**
454     * Gets the value of the setting for enabling Mobile data.
455     *
456     * @return Whether mobile data is enabled.
457     * @hide
458     */
459    public boolean getMobileDataEnabled() {
460        try {
461            return mService.getMobileDataEnabled();
462        } catch (RemoteException e) {
463            return true;
464        }
465    }
466
467    /**
468     * Sets the persisted value for enabling/disabling Mobile data.
469     *
470     * @param enabled Whether the mobile data connection should be
471     *            used or not.
472     * @hide
473     */
474    public void setMobileDataEnabled(boolean enabled) {
475        try {
476            mService.setMobileDataEnabled(enabled);
477        } catch (RemoteException e) {
478        }
479    }
480
481    /**
482     * Don't allow use of default constructor.
483     */
484    @SuppressWarnings({"UnusedDeclaration"})
485    private ConnectivityManager() {
486    }
487
488    /**
489     * {@hide}
490     */
491    public ConnectivityManager(IConnectivityManager service) {
492        if (service == null) {
493            throw new IllegalArgumentException(
494                "ConnectivityManager() cannot be constructed with null service");
495        }
496        mService = service;
497    }
498
499    /**
500     * {@hide}
501     */
502    public String[] getTetherableIfaces() {
503        try {
504            return mService.getTetherableIfaces();
505        } catch (RemoteException e) {
506            return new String[0];
507        }
508    }
509
510    /**
511     * {@hide}
512     */
513    public String[] getTetheredIfaces() {
514        try {
515            return mService.getTetheredIfaces();
516        } catch (RemoteException e) {
517            return new String[0];
518        }
519    }
520
521    /**
522     * {@hide}
523     */
524    public String[] getTetheringErroredIfaces() {
525        try {
526            return mService.getTetheringErroredIfaces();
527        } catch (RemoteException e) {
528            return new String[0];
529        }
530    }
531
532    /**
533     * @return error A TETHER_ERROR value indicating success or failure type
534     * {@hide}
535     */
536    public int tether(String iface) {
537        try {
538            return mService.tether(iface);
539        } catch (RemoteException e) {
540            return TETHER_ERROR_SERVICE_UNAVAIL;
541        }
542    }
543
544    /**
545     * @return error A TETHER_ERROR value indicating success or failure type
546     * {@hide}
547     */
548    public int untether(String iface) {
549        try {
550            return mService.untether(iface);
551        } catch (RemoteException e) {
552            return TETHER_ERROR_SERVICE_UNAVAIL;
553        }
554    }
555
556    /**
557     * {@hide}
558     */
559    public boolean isTetheringSupported() {
560        try {
561            return mService.isTetheringSupported();
562        } catch (RemoteException e) {
563            return false;
564        }
565    }
566
567    /**
568     * {@hide}
569     */
570    public String[] getTetherableUsbRegexs() {
571        try {
572            return mService.getTetherableUsbRegexs();
573        } catch (RemoteException e) {
574            return new String[0];
575        }
576    }
577
578    /**
579     * {@hide}
580     */
581    public String[] getTetherableWifiRegexs() {
582        try {
583            return mService.getTetherableWifiRegexs();
584        } catch (RemoteException e) {
585            return new String[0];
586        }
587    }
588
589    /**
590     * {@hide}
591     */
592    public String[] getTetherableBluetoothRegexs() {
593        try {
594            return mService.getTetherableBluetoothRegexs();
595        } catch (RemoteException e) {
596            return new String[0];
597        }
598    }
599
600    /** {@hide} */
601    public static final int TETHER_ERROR_NO_ERROR           = 0;
602    /** {@hide} */
603    public static final int TETHER_ERROR_UNKNOWN_IFACE      = 1;
604    /** {@hide} */
605    public static final int TETHER_ERROR_SERVICE_UNAVAIL    = 2;
606    /** {@hide} */
607    public static final int TETHER_ERROR_UNSUPPORTED        = 3;
608    /** {@hide} */
609    public static final int TETHER_ERROR_UNAVAIL_IFACE      = 4;
610    /** {@hide} */
611    public static final int TETHER_ERROR_MASTER_ERROR       = 5;
612    /** {@hide} */
613    public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
614    /** {@hide} */
615    public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
616    /** {@hide} */
617    public static final int TETHER_ERROR_ENABLE_NAT_ERROR     = 8;
618    /** {@hide} */
619    public static final int TETHER_ERROR_DISABLE_NAT_ERROR    = 9;
620    /** {@hide} */
621    public static final int TETHER_ERROR_IFACE_CFG_ERROR      = 10;
622
623    /**
624     * @param iface The name of the interface we're interested in
625     * @return error The error code of the last error tethering or untethering the named
626     *               interface
627     * {@hide}
628     */
629    public int getLastTetherError(String iface) {
630        try {
631            return mService.getLastTetherError(iface);
632        } catch (RemoteException e) {
633            return TETHER_ERROR_SERVICE_UNAVAIL;
634        }
635    }
636
637    /**
638     * Ensure the device stays awake until we connect with the next network
639     * @param forWhome The name of the network going down for logging purposes
640     * @return {@code true} on success, {@code false} on failure
641     * {@hide}
642     */
643    public boolean requestNetworkTransitionWakelock(String forWhom) {
644        try {
645            mService.requestNetworkTransitionWakelock(forWhom);
646            return true;
647        } catch (RemoteException e) {
648            return false;
649        }
650    }
651
652    /**
653     * @param networkType The type of network you want to report on
654     * @param percentage The quality of the connection 0 is bad, 100 is good
655     * {@hide}
656     */
657    public void reportInetCondition(int networkType, int percentage) {
658        try {
659            mService.reportInetCondition(networkType, percentage);
660        } catch (RemoteException e) {
661        }
662    }
663
664    /**
665     * @param proxyProperties The definition for the new global http proxy
666     * {@hide}
667     */
668    public void setGlobalProxy(ProxyProperties p) {
669        try {
670            mService.setGlobalProxy(p);
671        } catch (RemoteException e) {
672        }
673    }
674
675    /**
676     * @return proxyProperties for the current global proxy
677     * {@hide}
678     */
679    public ProxyProperties getGlobalProxy() {
680        try {
681            return mService.getGlobalProxy();
682        } catch (RemoteException e) {
683            return null;
684        }
685    }
686
687    /**
688     * @return proxyProperties for the current proxy (global if set, network specific if not)
689     * {@hide}
690     */
691    public ProxyProperties getProxy() {
692        try {
693            return mService.getProxy();
694        } catch (RemoteException e) {
695            return null;
696        }
697    }
698
699    /**
700     * @param networkType The network who's dependence has changed
701     * @param met Boolean - true if network use is ok, false if not
702     * {@hide}
703     */
704    public void setDataDependency(int networkType, boolean met) {
705        try {
706            mService.setDataDependency(networkType, met);
707        } catch (RemoteException e) {
708        }
709    }
710}
711