WifiStateTracker.java revision 2338f48ddb3d1746c2d46df85fdc4dcd6cfa240b
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.NetworkProperties; 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 NetworkProperties mNetworkProperties; 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 mNetworkProperties = new NetworkProperties(); 62 63 mNetworkInfo.setIsAvailable(false); 64 mNetworkProperties.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.WIFI_STATE_CHANGED_ACTION); 87 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); 88 filter.addAction(WifiManager.CONFIG_CHANGED_ACTION); 89 filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 90 91 mWifiStateReceiver = new WifiStateReceiver(); 92 mContext.registerReceiver(mWifiStateReceiver, filter); 93 } 94 95 /** 96 * Disable connectivity to a network 97 * TODO: do away with return value after making MobileDataStateTracker async 98 */ 99 public boolean teardown() { 100 mTeardownRequested.set(true); 101 mWifiManager.stopWifi(); 102 return true; 103 } 104 105 /** 106 * Re-enable connectivity to a network after a {@link #teardown()}. 107 * TODO: do away with return value after making MobileDataStateTracker async 108 */ 109 public boolean reconnect() { 110 mTeardownRequested.set(false); 111 mWifiManager.startWifi(); 112 return true; 113 } 114 115 /** 116 * Turn the wireless radio off for a network. 117 * @param turnOn {@code true} to turn the radio on, {@code false} 118 * TODO: do away with return value after making MobileDataStateTracker async 119 */ 120 public boolean setRadio(boolean turnOn) { 121 mWifiManager.setWifiEnabled(turnOn); 122 return true; 123 } 124 125 /** 126 * Wi-Fi is considered available as long as we have a connection to the 127 * supplicant daemon and there is at least one enabled network. If a teardown 128 * was explicitly requested, then Wi-Fi can be restarted with a reconnect 129 * request, so it is considered available. If the driver has been stopped 130 * for any reason other than a teardown request, Wi-Fi is considered 131 * unavailable. 132 * @return {@code true} if Wi-Fi connections are possible 133 */ 134 public boolean isAvailable() { 135 return mNetworkInfo.isAvailable(); 136 } 137 138 /** 139 * Tells the underlying networking system that the caller wants to 140 * begin using the named feature. The interpretation of {@code feature} 141 * is completely up to each networking implementation. 142 * @param feature the name of the feature to be used 143 * @param callingPid the process ID of the process that is issuing this request 144 * @param callingUid the user ID of the process that is issuing this request 145 * @return an integer value representing the outcome of the request. 146 * The interpretation of this value is specific to each networking 147 * implementation+feature combination, except that the value {@code -1} 148 * always indicates failure. 149 * TODO: needs to go away 150 */ 151 public int startUsingNetworkFeature(String feature, int callingPid, int callingUid) { 152 return -1; 153 } 154 155 /** 156 * Tells the underlying networking system that the caller is finished 157 * using the named feature. The interpretation of {@code feature} 158 * is completely up to each networking implementation. 159 * @param feature the name of the feature that is no longer needed. 160 * @param callingPid the process ID of the process that is issuing this request 161 * @param callingUid the user ID of the process that is issuing this request 162 * @return an integer value representing the outcome of the request. 163 * The interpretation of this value is specific to each networking 164 * implementation+feature combination, except that the value {@code -1} 165 * always indicates failure. 166 * TODO: needs to go away 167 */ 168 public int stopUsingNetworkFeature(String feature, int callingPid, int callingUid) { 169 return -1; 170 } 171 172 /** 173 * Check if private DNS route is set for the network 174 */ 175 public boolean isPrivateDnsRouteSet() { 176 return mPrivateDnsRouteSet.get(); 177 } 178 179 /** 180 * Set a flag indicating private DNS route is set 181 */ 182 public void privateDnsRouteSet(boolean enabled) { 183 mPrivateDnsRouteSet.set(enabled); 184 } 185 186 /** 187 * Fetch NetworkInfo for the network 188 */ 189 public NetworkInfo getNetworkInfo() { 190 return mNetworkInfo; 191 } 192 193 /** 194 * Fetch NetworkProperties for the network 195 */ 196 public NetworkProperties getNetworkProperties() { 197 return mNetworkProperties; 198 } 199 200 /** 201 * Fetch default gateway address for the network 202 */ 203 public int getDefaultGatewayAddr() { 204 return mDefaultGatewayAddr.get(); 205 } 206 207 /** 208 * Check if default route is set 209 */ 210 public boolean isDefaultRouteSet() { 211 return mDefaultRouteSet.get(); 212 } 213 214 /** 215 * Set a flag indicating default route is set for the network 216 */ 217 public void defaultRouteSet(boolean enabled) { 218 mDefaultRouteSet.set(enabled); 219 } 220 221 /** 222 * Return the system properties name associated with the tcp buffer sizes 223 * for this network. 224 */ 225 public String getTcpBufferSizesPropName() { 226 return "net.tcp.buffersize.wifi"; 227 } 228 229 private class WifiStateReceiver extends BroadcastReceiver { 230 @Override 231 public void onReceive(Context context, Intent intent) { 232 if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { 233 mNetworkInfo = (NetworkInfo) intent.getParcelableExtra( 234 WifiManager.EXTRA_NETWORK_INFO); 235 mNetworkProperties = (NetworkProperties) intent.getParcelableExtra( 236 WifiManager.EXTRA_NETWORK_PROPERTIES); 237 Message msg = mCsHandler.obtainMessage(EVENT_STATE_CHANGED, mNetworkInfo); 238 msg.sendToTarget(); 239 } else if (intent.getAction().equals(WifiManager.CONFIG_CHANGED_ACTION)) { 240 mNetworkProperties = (NetworkProperties) intent.getParcelableExtra( 241 WifiManager.EXTRA_NETWORK_PROPERTIES); 242 Message msg = mCsHandler.obtainMessage(EVENT_CONFIGURATION_CHANGED, mNetworkInfo); 243 msg.sendToTarget(); 244 } 245 } 246 } 247 248} 249