diff options
Diffstat (limited to 'services/java/com/android/server/WifiService.java')
-rw-r--r-- | services/java/com/android/server/WifiService.java | 295 |
1 files changed, 51 insertions, 244 deletions
diff --git a/services/java/com/android/server/WifiService.java b/services/java/com/android/server/WifiService.java index b4754b6..67e8cf3 100644 --- a/services/java/com/android/server/WifiService.java +++ b/services/java/com/android/server/WifiService.java @@ -41,6 +41,7 @@ import android.net.wifi.WifiConfiguration; import android.net.wifi.SupplicantState; import android.net.NetworkStateTracker; import android.net.DhcpInfo; +import android.net.NetworkUtils; import android.os.Binder; import android.os.Handler; import android.os.HandlerThread; @@ -142,28 +143,6 @@ public class WifiService extends IWifiManager.Stub { private final WifiHandler mWifiHandler; /* - * Map used to keep track of hidden networks presence, which - * is needed to switch between active and passive scan modes. - * If there is at least one hidden network that is currently - * present (enabled), we want to do active scans instead of - * passive. - */ - private final Map<Integer, Boolean> mIsHiddenNetworkPresent; - /* - * The number of currently present hidden networks. When this - * counter goes from 0 to 1 or from 1 to 0, we change the - * scan mode to active or passive respectively. Initially, we - * set the counter to 0 and we increment it every time we add - * a new present (enabled) hidden network. - */ - private int mNumHiddenNetworkPresent; - /* - * Whether we change the scan mode is due to a hidden network - * (in this class, this is always the case) - */ - private final static boolean SET_DUE_TO_A_HIDDEN_NETWORK = true; - - /* * Cache of scan results objects (size is somewhat arbitrary) */ private static final int SCAN_RESULT_CACHE_SIZE = 80; @@ -195,12 +174,6 @@ public class WifiService extends IWifiManager.Stub { mWifiStateTracker.enableRssiPolling(true); mBatteryStats = BatteryStatsService.getService(); - /* - * Initialize the hidden-networks state - */ - mIsHiddenNetworkPresent = new HashMap<Integer, Boolean>(); - mNumHiddenNetworkPresent = 0; - mScanResultCache = new LinkedHashMap<String, ScanResult>( SCAN_RESULT_CACHE_SIZE, 0.75f, true) { /* @@ -254,155 +227,6 @@ public class WifiService extends IWifiManager.Stub { setWifiEnabledBlocking(wifiEnabled, false, Process.myUid()); } - /** - * Initializes the hidden networks state. Must be called when we - * enable Wi-Fi. - */ - private synchronized void initializeHiddenNetworksState() { - // First, reset the state - resetHiddenNetworksState(); - - // ... then add networks that are marked as hidden - List<WifiConfiguration> networks = getConfiguredNetworks(); - if (!networks.isEmpty()) { - for (WifiConfiguration config : networks) { - if (config != null && config.hiddenSSID) { - addOrUpdateHiddenNetwork( - config.networkId, - config.status != WifiConfiguration.Status.DISABLED); - } - } - - } - } - - /** - * Resets the hidden networks state. - */ - private synchronized void resetHiddenNetworksState() { - mNumHiddenNetworkPresent = 0; - mIsHiddenNetworkPresent.clear(); - } - - /** - * Marks all but netId network as not present. - */ - private synchronized void markAllHiddenNetworksButOneAsNotPresent(int netId) { - for (Map.Entry<Integer, Boolean> entry : mIsHiddenNetworkPresent.entrySet()) { - if (entry != null) { - Integer networkId = entry.getKey(); - if (networkId != netId) { - updateNetworkIfHidden( - networkId, false); - } - } - } - } - - /** - * Updates the netId network presence status if netId is an existing - * hidden network. - */ - private synchronized void updateNetworkIfHidden(int netId, boolean present) { - if (isHiddenNetwork(netId)) { - addOrUpdateHiddenNetwork(netId, present); - } - } - - /** - * Updates the netId network presence status if netId is an existing - * hidden network. If the network does not exist, adds the network. - */ - private synchronized void addOrUpdateHiddenNetwork(int netId, boolean present) { - if (0 <= netId) { - - // If we are adding a new entry or modifying an existing one - Boolean isPresent = mIsHiddenNetworkPresent.get(netId); - if (isPresent == null || isPresent != present) { - if (present) { - incrementHiddentNetworkPresentCounter(); - } else { - // If we add a new hidden network, no need to change - // the counter (it must be 0) - if (isPresent != null) { - decrementHiddentNetworkPresentCounter(); - } - } - mIsHiddenNetworkPresent.put(netId, present); - } - } else { - Log.e(TAG, "addOrUpdateHiddenNetwork(): Invalid (negative) network id!"); - } - } - - /** - * Removes the netId network if it is hidden (being kept track of). - */ - private synchronized void removeNetworkIfHidden(int netId) { - if (isHiddenNetwork(netId)) { - removeHiddenNetwork(netId); - } - } - - /** - * Removes the netId network. For the call to be successful, the network - * must be hidden. - */ - private synchronized void removeHiddenNetwork(int netId) { - if (0 <= netId) { - Boolean isPresent = - mIsHiddenNetworkPresent.remove(netId); - if (isPresent != null) { - // If we remove an existing hidden network that is not - // present, no need to change the counter - if (isPresent) { - decrementHiddentNetworkPresentCounter(); - } - } else { - if (DBG) { - Log.d(TAG, "removeHiddenNetwork(): Removing a non-existent network!"); - } - } - } else { - Log.e(TAG, "removeHiddenNetwork(): Invalid (negative) network id!"); - } - } - - /** - * Returns true if netId is an existing hidden network. - */ - private synchronized boolean isHiddenNetwork(int netId) { - return mIsHiddenNetworkPresent.containsKey(netId); - } - - /** - * Increments the present (enabled) hidden networks counter. If the - * counter value goes from 0 to 1, changes the scan mode to active. - */ - private void incrementHiddentNetworkPresentCounter() { - ++mNumHiddenNetworkPresent; - if (1 == mNumHiddenNetworkPresent) { - // Switch the scan mode to "active" - mWifiStateTracker.setScanMode(true, SET_DUE_TO_A_HIDDEN_NETWORK); - } - } - - /** - * Decrements the present (enabled) hidden networks counter. If the - * counter goes from 1 to 0, changes the scan mode back to passive. - */ - private void decrementHiddentNetworkPresentCounter() { - if (0 < mNumHiddenNetworkPresent) { - --mNumHiddenNetworkPresent; - if (0 == mNumHiddenNetworkPresent) { - // Switch the scan mode to "passive" - mWifiStateTracker.setScanMode(false, SET_DUE_TO_A_HIDDEN_NETWORK); - } - } else { - Log.e(TAG, "Hidden-network counter invariant violation!"); - } - } - private boolean getPersistedWifiEnabled() { final ContentResolver cr = mContext.getContentResolver(); try { @@ -522,7 +346,7 @@ public class WifiService extends IWifiManager.Stub { } // We must reset the interface before we unload the driver - mWifiStateTracker.resetInterface(); + mWifiStateTracker.resetInterface(false); if (!WifiNative.unloadDriver()) { Log.e(TAG, "Failed to unload Wi-Fi driver."); @@ -544,12 +368,10 @@ public class WifiService extends IWifiManager.Stub { setWifiEnabledState(eventualWifiState, uid); /* - * Initialize the hidden networks state and the number of allowed - * radio channels if Wi-Fi is being turned on. + * Initialize the number of allowed radio channels if Wi-Fi is being turned on. */ if (enable) { mWifiStateTracker.setNumAllowedChannels(); - initializeHiddenNetworksState(); } return true; @@ -884,15 +706,6 @@ public class WifiService extends IWifiManager.Stub { } mNeedReconfig = mNeedReconfig || doReconfig; - /* - * If we have hidden networks, we may have to change the scan mode - */ - if (config.hiddenSSID) { - // Mark the network as present unless it is disabled - addOrUpdateHiddenNetwork( - netId, config.status != WifiConfiguration.Status.DISABLED); - } - setVariables: { /* * Note that if a networkId for a non-existent network @@ -1220,11 +1033,6 @@ public class WifiService extends IWifiManager.Stub { public boolean removeNetwork(int netId) { enforceChangePermission(); - /* - * If we have hidden networks, we may have to change the scan mode - */ - removeNetworkIfHidden(netId); - return mWifiStateTracker.removeNetwork(netId); } @@ -1238,18 +1046,14 @@ public class WifiService extends IWifiManager.Stub { public boolean enableNetwork(int netId, boolean disableOthers) { enforceChangePermission(); - /* - * If we have hidden networks, we may have to change the scan mode - */ - synchronized(this) { - if (disableOthers) { - markAllHiddenNetworksButOneAsNotPresent(netId); - } - updateNetworkIfHidden(netId, true); - } - synchronized (mWifiStateTracker) { - return WifiNative.enableNetworkCommand(netId, disableOthers); + String ifname = mWifiStateTracker.getInterfaceName(); + NetworkUtils.enableInterface(ifname); + boolean result = WifiNative.enableNetworkCommand(netId, disableOthers); + if (!result) { + NetworkUtils.disableInterface(ifname); + } + return result; } } @@ -1262,11 +1066,6 @@ public class WifiService extends IWifiManager.Stub { public boolean disableNetwork(int netId) { enforceChangePermission(); - /* - * If we have hidden networks, we may have to change the scan mode - */ - updateNetworkIfHidden(netId, false); - synchronized (mWifiStateTracker) { return WifiNative.disableNetworkCommand(netId); } @@ -1364,39 +1163,42 @@ public class WifiService extends IWifiManager.Stub { level = 0; } - // bssid is the hash key - scanResult = mScanResultCache.get(bssid); - if (scanResult != null) { - scanResult.level = level; - } else { - /* - * The formatting of the results returned by - * wpa_supplicant is intended to make the fields - * line up nicely when printed, - * not to make them easy to parse. So we have to - * apply some heuristics to figure out which field - * is the SSID and which field is the flags. - */ - String ssid; - String flags; - if (result.length == 4) { - if (result[3].charAt(0) == '[') { - flags = result[3]; - ssid = ""; - } else { - flags = ""; - ssid = result[3]; - } - } else if (result.length == 5) { + /* + * The formatting of the results returned by + * wpa_supplicant is intended to make the fields + * line up nicely when printed, + * not to make them easy to parse. So we have to + * apply some heuristics to figure out which field + * is the SSID and which field is the flags. + */ + String ssid; + String flags; + if (result.length == 4) { + if (result[3].charAt(0) == '[') { flags = result[3]; - ssid = result[4]; + ssid = ""; } else { - // Here, we must have 3 fields: no flags and ssid - // set flags = ""; - ssid = ""; + ssid = result[3]; } + } else if (result.length == 5) { + flags = result[3]; + ssid = result[4]; + } else { + // Here, we must have 3 fields: no flags and ssid + // set + flags = ""; + ssid = ""; + } + // bssid is the hash key + scanResult = mScanResultCache.get(bssid); + if (scanResult != null) { + scanResult.level = level; + scanResult.SSID = ssid; + scanResult.capabilities = flags; + scanResult.frequency = frequency; + } else { // Do not add scan results that have no SSID set if (0 < ssid.trim().length()) { scanResult = @@ -1468,14 +1270,17 @@ public class WifiService extends IWifiManager.Stub { * Set the number of radio frequency channels that are allowed to be used * in the current regulatory domain. This method should be used only * if the correct number of channels cannot be determined automatically - * for some reason. If the operation is successful, the new value is + * for some reason. If the operation is successful, the new value may be * persisted as a Secure setting. * @param numChannels the number of allowed channels. Must be greater than 0 * and less than or equal to 16. + * @param persist {@code true} if the setting should be remembered. * @return {@code true} if the operation succeeds, {@code false} otherwise, e.g., * {@code numChannels} is outside the valid range. */ - public boolean setNumAllowedChannels(int numChannels) { + public boolean setNumAllowedChannels(int numChannels, boolean persist) { + Log.i(TAG, "WifiService trying to setNumAllowed to "+numChannels+ + " with persist set to "+persist); enforceChangePermission(); /* * Validate the argument. We'd like to let the Wi-Fi driver do this, @@ -1494,9 +1299,11 @@ public class WifiService extends IWifiManager.Stub { return false; } - Settings.Secure.putInt(mContext.getContentResolver(), - Settings.Secure.WIFI_NUM_ALLOWED_CHANNELS, - numChannels); + if (persist) { + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.WIFI_NUM_ALLOWED_CHANNELS, + numChannels); + } mWifiStateTracker.setNumAllowedChannels(numChannels); return true; } |