| /* |
| * Copyright (C) 2010 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package android.net.wifi; |
| |
| |
| |
| import android.content.BroadcastReceiver; |
| import android.content.Context; |
| import android.content.Intent; |
| import android.content.IntentFilter; |
| import android.net.ConnectivityManager; |
| import android.net.NetworkInfo; |
| import android.net.LinkProperties; |
| import android.net.NetworkStateTracker; |
| import android.os.Handler; |
| import android.os.Message; |
| |
| import java.util.concurrent.atomic.AtomicBoolean; |
| import java.util.concurrent.atomic.AtomicInteger; |
| |
| /** |
| * Track the state of wifi for connectivity service. |
| * |
| * @hide |
| */ |
| public class WifiStateTracker implements NetworkStateTracker { |
| |
| private static final String NETWORKTYPE = "WIFI"; |
| private static final String TAG = "WifiStateTracker"; |
| |
| private AtomicBoolean mTeardownRequested = new AtomicBoolean(false); |
| private AtomicBoolean mPrivateDnsRouteSet = new AtomicBoolean(false); |
| private AtomicInteger mDefaultGatewayAddr = new AtomicInteger(0); |
| private AtomicBoolean mDefaultRouteSet = new AtomicBoolean(false); |
| |
| private LinkProperties mLinkProperties; |
| private NetworkInfo mNetworkInfo; |
| |
| /* For sending events to connectivity service handler */ |
| private Handler mCsHandler; |
| private Context mContext; |
| private BroadcastReceiver mWifiStateReceiver; |
| private WifiManager mWifiManager; |
| |
| public WifiStateTracker() { |
| mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, NETWORKTYPE, ""); |
| mLinkProperties = new LinkProperties(); |
| |
| mNetworkInfo.setIsAvailable(false); |
| mLinkProperties.clear(); |
| setTeardownRequested(false); |
| } |
| |
| |
| public void setTeardownRequested(boolean isRequested) { |
| mTeardownRequested.set(isRequested); |
| } |
| |
| public boolean isTeardownRequested() { |
| return mTeardownRequested.get(); |
| } |
| |
| /** |
| * Begin monitoring wifi connectivity |
| */ |
| public void startMonitoring(Context context, Handler target) { |
| mCsHandler = target; |
| mContext = context; |
| |
| mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); |
| IntentFilter filter = new IntentFilter(); |
| filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); |
| filter.addAction(WifiManager.CONFIG_CHANGED_ACTION); |
| |
| mWifiStateReceiver = new WifiStateReceiver(); |
| mContext.registerReceiver(mWifiStateReceiver, filter); |
| } |
| |
| /** |
| * Disable connectivity to a network |
| * TODO: do away with return value after making MobileDataStateTracker async |
| */ |
| public boolean teardown() { |
| mTeardownRequested.set(true); |
| mWifiManager.stopWifi(); |
| return true; |
| } |
| |
| /** |
| * Re-enable connectivity to a network after a {@link #teardown()}. |
| * TODO: do away with return value after making MobileDataStateTracker async |
| */ |
| public boolean reconnect() { |
| mTeardownRequested.set(false); |
| mWifiManager.startWifi(); |
| return true; |
| } |
| |
| /** |
| * Turn the wireless radio off for a network. |
| * @param turnOn {@code true} to turn the radio on, {@code false} |
| * TODO: do away with return value after making MobileDataStateTracker async |
| */ |
| public boolean setRadio(boolean turnOn) { |
| mWifiManager.setWifiEnabled(turnOn); |
| return true; |
| } |
| |
| /** |
| * Wi-Fi is considered available as long as we have a connection to the |
| * supplicant daemon and there is at least one enabled network. If a teardown |
| * was explicitly requested, then Wi-Fi can be restarted with a reconnect |
| * request, so it is considered available. If the driver has been stopped |
| * for any reason other than a teardown request, Wi-Fi is considered |
| * unavailable. |
| * @return {@code true} if Wi-Fi connections are possible |
| */ |
| public boolean isAvailable() { |
| return mNetworkInfo.isAvailable(); |
| } |
| |
| /** |
| * Tells the underlying networking system that the caller wants to |
| * begin using the named feature. The interpretation of {@code feature} |
| * is completely up to each networking implementation. |
| * @param feature the name of the feature to be used |
| * @param callingPid the process ID of the process that is issuing this request |
| * @param callingUid the user ID of the process that is issuing this request |
| * @return an integer value representing the outcome of the request. |
| * The interpretation of this value is specific to each networking |
| * implementation+feature combination, except that the value {@code -1} |
| * always indicates failure. |
| * TODO: needs to go away |
| */ |
| public int startUsingNetworkFeature(String feature, int callingPid, int callingUid) { |
| return -1; |
| } |
| |
| /** |
| * Tells the underlying networking system that the caller is finished |
| * using the named feature. The interpretation of {@code feature} |
| * is completely up to each networking implementation. |
| * @param feature the name of the feature that is no longer needed. |
| * @param callingPid the process ID of the process that is issuing this request |
| * @param callingUid the user ID of the process that is issuing this request |
| * @return an integer value representing the outcome of the request. |
| * The interpretation of this value is specific to each networking |
| * implementation+feature combination, except that the value {@code -1} |
| * always indicates failure. |
| * TODO: needs to go away |
| */ |
| public int stopUsingNetworkFeature(String feature, int callingPid, int callingUid) { |
| return -1; |
| } |
| |
| /** |
| * Check if private DNS route is set for the network |
| */ |
| public boolean isPrivateDnsRouteSet() { |
| return mPrivateDnsRouteSet.get(); |
| } |
| |
| /** |
| * Set a flag indicating private DNS route is set |
| */ |
| public void privateDnsRouteSet(boolean enabled) { |
| mPrivateDnsRouteSet.set(enabled); |
| } |
| |
| /** |
| * Fetch NetworkInfo for the network |
| */ |
| public NetworkInfo getNetworkInfo() { |
| return mNetworkInfo; |
| } |
| |
| /** |
| * Fetch LinkProperties for the network |
| */ |
| public LinkProperties getLinkProperties() { |
| return new LinkProperties(mLinkProperties); |
| } |
| |
| /** |
| * Fetch default gateway address for the network |
| */ |
| public int getDefaultGatewayAddr() { |
| return mDefaultGatewayAddr.get(); |
| } |
| |
| /** |
| * Check if default route is set |
| */ |
| public boolean isDefaultRouteSet() { |
| return mDefaultRouteSet.get(); |
| } |
| |
| /** |
| * Set a flag indicating default route is set for the network |
| */ |
| public void defaultRouteSet(boolean enabled) { |
| mDefaultRouteSet.set(enabled); |
| } |
| |
| /** |
| * Return the system properties name associated with the tcp buffer sizes |
| * for this network. |
| */ |
| public String getTcpBufferSizesPropName() { |
| return "net.tcp.buffersize.wifi"; |
| } |
| |
| private class WifiStateReceiver extends BroadcastReceiver { |
| @Override |
| public void onReceive(Context context, Intent intent) { |
| if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { |
| mNetworkInfo = (NetworkInfo) intent.getParcelableExtra( |
| WifiManager.EXTRA_NETWORK_INFO); |
| mLinkProperties = (LinkProperties) intent.getParcelableExtra( |
| WifiManager.EXTRA_LINK_PROPERTIES); |
| Message msg = mCsHandler.obtainMessage(EVENT_STATE_CHANGED, mNetworkInfo); |
| msg.sendToTarget(); |
| } else if (intent.getAction().equals(WifiManager.CONFIG_CHANGED_ACTION)) { |
| mLinkProperties = (LinkProperties) intent.getParcelableExtra( |
| WifiManager.EXTRA_LINK_PROPERTIES); |
| Message msg = mCsHandler.obtainMessage(EVENT_CONFIGURATION_CHANGED, mNetworkInfo); |
| msg.sendToTarget(); |
| } |
| } |
| } |
| |
| } |