WifiStateTracker.java revision 5de38d11f6bb277ac0930f4264f012dac14a05a9
1/*
2 * Copyright (C) 2010 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.wifi;
18
19
20
21import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.net.ConnectivityManager;
26import android.net.NetworkInfo;
27import android.net.LinkProperties;
28import android.net.NetworkStateTracker;
29import android.os.Handler;
30import android.os.Message;
31
32import java.util.concurrent.atomic.AtomicBoolean;
33import java.util.concurrent.atomic.AtomicInteger;
34
35/**
36 * Track the state of wifi for connectivity service.
37 *
38 * @hide
39 */
40public class WifiStateTracker implements NetworkStateTracker {
41
42    private static final String NETWORKTYPE = "WIFI";
43    private static final String TAG = "WifiStateTracker";
44
45    private AtomicBoolean mTeardownRequested = new AtomicBoolean(false);
46    private AtomicBoolean mPrivateDnsRouteSet = new AtomicBoolean(false);
47    private AtomicInteger mDefaultGatewayAddr = new AtomicInteger(0);
48    private AtomicBoolean mDefaultRouteSet = new AtomicBoolean(false);
49
50    private LinkProperties mLinkProperties;
51    private NetworkInfo mNetworkInfo;
52
53    /* For sending events to connectivity service handler */
54    private Handler mCsHandler;
55    private Context mContext;
56    private BroadcastReceiver mWifiStateReceiver;
57    private WifiManager mWifiManager;
58
59    public WifiStateTracker() {
60        mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, NETWORKTYPE, "");
61        mLinkProperties = new LinkProperties();
62
63        mNetworkInfo.setIsAvailable(false);
64        mLinkProperties.clear();
65        setTeardownRequested(false);
66    }
67
68
69    public void setTeardownRequested(boolean isRequested) {
70        mTeardownRequested.set(isRequested);
71    }
72
73    public boolean isTeardownRequested() {
74        return mTeardownRequested.get();
75    }
76
77    /**
78     * Begin monitoring wifi connectivity
79     */
80    public void startMonitoring(Context context, Handler target) {
81        mCsHandler = target;
82        mContext = context;
83
84        mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
85        IntentFilter filter = new IntentFilter();
86        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
87        filter.addAction(WifiManager.CONFIG_CHANGED_ACTION);
88
89        mWifiStateReceiver = new WifiStateReceiver();
90        mContext.registerReceiver(mWifiStateReceiver, filter);
91    }
92
93    /**
94     * Disable connectivity to a network
95     * TODO: do away with return value after making MobileDataStateTracker async
96     */
97    public boolean teardown() {
98        mTeardownRequested.set(true);
99        mWifiManager.stopWifi();
100        return true;
101    }
102
103    /**
104     * Re-enable connectivity to a network after a {@link #teardown()}.
105     * TODO: do away with return value after making MobileDataStateTracker async
106     */
107    public boolean reconnect() {
108        mTeardownRequested.set(false);
109        mWifiManager.startWifi();
110        return true;
111    }
112
113    /**
114     * Turn the wireless radio off for a network.
115     * @param turnOn {@code true} to turn the radio on, {@code false}
116     * TODO: do away with return value after making MobileDataStateTracker async
117     */
118    public boolean setRadio(boolean turnOn) {
119        mWifiManager.setWifiEnabled(turnOn);
120        return true;
121    }
122
123    /**
124     * Wi-Fi is considered available as long as we have a connection to the
125     * supplicant daemon and there is at least one enabled network. If a teardown
126     * was explicitly requested, then Wi-Fi can be restarted with a reconnect
127     * request, so it is considered available. If the driver has been stopped
128     * for any reason other than a teardown request, Wi-Fi is considered
129     * unavailable.
130     * @return {@code true} if Wi-Fi connections are possible
131     */
132    public boolean isAvailable() {
133        return mNetworkInfo.isAvailable();
134    }
135
136    /**
137     * Tells the underlying networking system that the caller wants to
138     * begin using the named feature. The interpretation of {@code feature}
139     * is completely up to each networking implementation.
140     * @param feature the name of the feature to be used
141     * @param callingPid the process ID of the process that is issuing this request
142     * @param callingUid the user ID of the process that is issuing this request
143     * @return an integer value representing the outcome of the request.
144     * The interpretation of this value is specific to each networking
145     * implementation+feature combination, except that the value {@code -1}
146     * always indicates failure.
147     * TODO: needs to go away
148     */
149    public int startUsingNetworkFeature(String feature, int callingPid, int callingUid) {
150        return -1;
151    }
152
153    /**
154     * Tells the underlying networking system that the caller is finished
155     * using the named feature. The interpretation of {@code feature}
156     * is completely up to each networking implementation.
157     * @param feature the name of the feature that is no longer needed.
158     * @param callingPid the process ID of the process that is issuing this request
159     * @param callingUid the user ID of the process that is issuing this request
160     * @return an integer value representing the outcome of the request.
161     * The interpretation of this value is specific to each networking
162     * implementation+feature combination, except that the value {@code -1}
163     * always indicates failure.
164     * TODO: needs to go away
165     */
166    public int stopUsingNetworkFeature(String feature, int callingPid, int callingUid) {
167        return -1;
168    }
169
170    /**
171     * Check if private DNS route is set for the network
172     */
173    public boolean isPrivateDnsRouteSet() {
174        return mPrivateDnsRouteSet.get();
175    }
176
177    /**
178     * Set a flag indicating private DNS route is set
179     */
180    public void privateDnsRouteSet(boolean enabled) {
181        mPrivateDnsRouteSet.set(enabled);
182    }
183
184    /**
185     * Fetch NetworkInfo for the network
186     */
187    public NetworkInfo getNetworkInfo() {
188        return mNetworkInfo;
189    }
190
191    /**
192     * Fetch LinkProperties for the network
193     */
194    public LinkProperties getLinkProperties() {
195        return new LinkProperties(mLinkProperties);
196    }
197
198    /**
199     * Fetch default gateway address for the network
200     */
201    public int getDefaultGatewayAddr() {
202        return mDefaultGatewayAddr.get();
203    }
204
205    /**
206     * Check if default route is set
207     */
208    public boolean isDefaultRouteSet() {
209        return mDefaultRouteSet.get();
210    }
211
212    /**
213     * Set a flag indicating default route is set for the network
214     */
215    public void defaultRouteSet(boolean enabled) {
216        mDefaultRouteSet.set(enabled);
217    }
218
219    /**
220     * Return the system properties name associated with the tcp buffer sizes
221     * for this network.
222     */
223    public String getTcpBufferSizesPropName() {
224        return "net.tcp.buffersize.wifi";
225    }
226
227    private class WifiStateReceiver extends BroadcastReceiver {
228        @Override
229        public void onReceive(Context context, Intent intent) {
230           if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
231                mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
232                        WifiManager.EXTRA_NETWORK_INFO);
233                mLinkProperties = (LinkProperties) intent.getParcelableExtra(
234                        WifiManager.EXTRA_LINK_PROPERTIES);
235                Message msg = mCsHandler.obtainMessage(EVENT_STATE_CHANGED, mNetworkInfo);
236                msg.sendToTarget();
237            } else if (intent.getAction().equals(WifiManager.CONFIG_CHANGED_ACTION)) {
238                mLinkProperties = (LinkProperties) intent.getParcelableExtra(
239                        WifiManager.EXTRA_LINK_PROPERTIES);
240                Message msg = mCsHandler.obtainMessage(EVENT_CONFIGURATION_CHANGED, mNetworkInfo);
241                msg.sendToTarget();
242            }
243        }
244    }
245
246}
247