FileDocCategorySizeDatePackage
AccessPointState.javaAPI DocAndroid 1.5 API28124Wed May 06 22:42:48 BST 2009com.android.settings.wifi

AccessPointState

public final class AccessPointState extends Object implements Comparable, android.os.Parcelable

Fields Summary
private static final String
TAG
public static final String
WPA2
public static final String
WPA
public static final String
WEP
public static final String
OPEN
private static final String
ADHOC_CAPABILITY
String present in capabilities if the scan result is ad-hoc
private static final String
ENTERPRISE_CAPABILITY
String present in capabilities if the scan result is enterprise secured
public static final String
BSSID_ANY
public static final int
NETWORK_ID_NOT_SET
static final int
NETWORK_ID_ANY
This should be used with care!
public static final int
MATCH_NONE
public static final int
MATCH_WEAK
public static final int
MATCH_STRONG
public static final int
MATCH_EXACT
public int
networkId
public int
priority
public boolean
hiddenSsid
public int
linkSpeed
public int
ipAddress
public String
bssid
public String
ssid
public int
signal
public boolean
primary
public boolean
seen
public boolean
configured
public NetworkInfo.DetailedState
status
public String
security
public boolean
disabled
private float
signalForSorting
Use this for sorting based on signal strength. It is a heavily-damped time-averaged weighted signal.
private static final float
DAMPING_FACTOR
private String
mPassword
This will be a user entered password, and NOT taken from wpa_supplicant (since it would give us *)
private boolean
mConfigHadPassword
public static final int
WEP_PASSWORD_AUTO
public static final int
WEP_PASSWORD_ASCII
public static final int
WEP_PASSWORD_HEX
private int
mWepPasswordType
private android.content.Context
mContext
private int
mBlockRefresh
If > 0, don't refresh (changes are being batched), use {@link #blockRefresh()} and {@link #unblockRefresh()} only.
private boolean
mNeedsRefresh
This will be set by {@link #requestRefresh} and shouldn't be written to elsewhere.
private AccessPointStateCallback
mCallback
private StringBuilder
mSummaryBuilder
public static final Creator
CREATOR
Implement the Parcelable interface
Constructors Summary
public AccessPointState(android.content.Context context)

    
      
         
    
    
       
        this();
        
        setContext(context);
    
private AccessPointState()

        bssid = BSSID_ANY;
        ssid = "";
        networkId = NETWORK_ID_NOT_SET;
        hiddenSsid = false;
    
Methods Summary
voidblockRefresh()

        mBlockRefresh++;
    
private voidbuildSummary(java.lang.StringBuilder sb, java.lang.String string, boolean autoLowerCaseFirstLetter)

        if (sb.length() == 0) {
            sb.append(string);
        } else {
            sb.append(", ");
            if (autoLowerCaseFirstLetter) {
                // Convert first letter to lowercase
                sb.append(Character.toLowerCase(string.charAt(0))).append(string, 1,
                        string.length());
            } else {
                sb.append(string);
            }
        }
    
public intcompareTo(com.android.settings.wifi.AccessPointState other)

        // This ranks the states for displaying in the AP list, not for
        // connecting to (wpa_supplicant does that using the WifiConfiguration's
        // priority field).
        
        // Clarity > efficiency, of this logic:
        int comparison;
        
        // Primary
        comparison = (other.primary ? 1 : 0) - (primary ? 1 : 0);
        if (comparison != 0) return comparison;
        
        // Currently seen (similar to, but not always the same as within range)
        comparison = (other.seen ? 1 : 0) - (seen ? 1 : 0);
        if (comparison != 0) return comparison;

        // Configured
        comparison = (other.configured ? 1 : 0) - (configured ? 1 : 0);
        if (comparison != 0) return comparison;

        if (!configured) {
            // Neither are configured

            // Open network
            comparison = (hasSecurity() ? 1 : 0) - (other.hasSecurity() ? 1 : 0);
            if (comparison != 0) return comparison;
        }

        // Signal strength
        comparison = (int) (other.signalForSorting - signalForSorting);
        if (comparison != 0) return comparison;

        // Alphabetical
        return ssid.compareToIgnoreCase(other.ssid);
    
public static java.lang.StringconvertToQuotedString(java.lang.String string)

        if (TextUtils.isEmpty(string)) {
            return "";
        }
        
        final int lastPos = string.length() - 1;
        if (lastPos < 0 || (string.charAt(0) == '"" && string.charAt(lastPos) == '"")) {
            return string;
        }
        
        return "\"" + string + "\"";
    
public intdescribeContents()
Implement the Parcelable interface

        return 0;
    
public booleanequals(java.lang.Object o)
{@inheritDoc}

see
#matches(int, String, String)
see
#hashCode()

        if (!o.getClass().equals(getClass())) {
            return false;
        }
        
        final AccessPointState other = (AccessPointState) o;

        // To see which conditions cause two AccessPointStates to be equal, see
        // where #matches returns MATCH_WEAK or greater.
        
        return matches(other.networkId, other.bssid, other.ssid, other.security) >= MATCH_WEAK;
    
public voidforget()
Updates the state as if it were never configured.

Note: This will not pass the forget call to the Wi-Fi API.

        blockRefresh();
        setConfigured(false);
        setNetworkId(NETWORK_ID_NOT_SET);
        setPrimary(false);
        setStatus(null);
        setDisabled(false);
        unblockRefresh();
    
public java.lang.StringgetHumanReadableSecurity()

        if (security.equals(OPEN)) return mContext.getString(R.string.wifi_security_open);
        else if (security.equals(WEP)) return mContext.getString(R.string.wifi_security_wep);
        else if (security.equals(WPA)) return mContext.getString(R.string.wifi_security_wpa);
        else if (security.equals(WPA2)) return mContext.getString(R.string.wifi_security_wpa2);
        
        return mContext.getString(R.string.wifi_security_unknown);
    
public java.lang.StringgetHumanReadableSsid()

        if (TextUtils.isEmpty(ssid)) {
            return "";
        }
        
        final int lastPos = ssid.length() - 1;
        if (ssid.charAt(0) == '"" && ssid.charAt(lastPos) == '"") {
            return ssid.substring(1, lastPos);
        }
        
        return ssid;
    
public static java.lang.StringgetScanResultSecurity(android.net.wifi.ScanResult scanResult)

return
The security of a given {@link ScanResult}.

        final String cap = scanResult.capabilities;
        final String[] securityModes = { WEP, WPA, WPA2 }; 
        for (int i = securityModes.length - 1; i >= 0; i--) {
            if (cap.contains(securityModes[i])) {
                return securityModes[i];
            }
        }
        
        return OPEN;
    
java.lang.StringgetSummarizedStatus()

        StringBuilder sb = mSummaryBuilder;
        sb.delete(0, sb.length());
        
        if (primary && status != null) {
            buildSummary(sb, WifiStatus.getPrintable(mContext, status), true);
            
        } else if (!seen) {
            buildSummary(sb, mContext.getString(R.string.summary_not_in_range), true);

            // Remembered comes second in this case
            if (!primary && configured) {
                buildSummary(sb, mContext.getString(R.string.summary_remembered), true);
            }
            
        } else {
            if (configured && disabled) {
                // The connection failure overrides all in this case
                return mContext.getString(R.string.summary_connection_failed);
            }

            // Remembered comes first in this case
            if (!primary && configured) {
                buildSummary(sb, mContext.getString(R.string.summary_remembered), true);
            }
            
            // If it is seen (and not the primary), show the security type
            String verboseSecurity = getVerboseSecurity();
            if (verboseSecurity != null) {
                buildSummary(sb, verboseSecurity, true);
            }
        }
        
        return sb.toString();
    
private java.lang.StringgetVerboseSecurity()

        if (WEP.equals(security)) {
            return mContext.getString(R.string.wifi_security_verbose_wep);
        } else if (WPA.equals(security)) {
            return mContext.getString(R.string.wifi_security_verbose_wpa);
        } else if (WPA2.equals(security)) {
            return mContext.getString(R.string.wifi_security_verbose_wpa2);
        } else if (OPEN.equals(security)) {
            return mContext.getString(R.string.wifi_security_verbose_open);
        } else {
            return null;
        }
    
public static java.lang.StringgetWifiConfigurationSecurity(android.net.wifi.WifiConfiguration wifiConfig)

return
The security of a given {@link WifiConfiguration}.


        if (wifiConfig.allowedKeyManagement.get(KeyMgmt.NONE)) {
            // If we never set group ciphers, wpa_supplicant puts all of them.
            // For open, we don't set group ciphers.
            // For WEP, we specifically only set WEP40 and WEP104, so CCMP
            // and TKIP should not be there.
            if (!wifiConfig.allowedGroupCiphers.get(GroupCipher.CCMP)
                    && (wifiConfig.allowedGroupCiphers.get(GroupCipher.WEP40)
                            || wifiConfig.allowedGroupCiphers.get(GroupCipher.WEP104))) {
                return WEP;
            } else {
                return OPEN;
            }
        } else if (wifiConfig.allowedProtocols.get(Protocol.RSN)) {
            return WPA2;
        } else if (wifiConfig.allowedProtocols.get(Protocol.WPA)) {
            return WPA;
        } else {
            Log.w(TAG, "Unknown security type from WifiConfiguration, falling back on open.");
            return OPEN;
        }
    
private java.lang.StringgetWpaSupplicantBssid()

        return bssid.equals(BSSID_ANY) ? null : bssid;
    
public booleanhasPassword()

        return !TextUtils.isEmpty(mPassword) || mConfigHadPassword; 
    
private static booleanhasPassword(android.net.wifi.WifiConfiguration wifiConfig)

        return !TextUtils.isEmpty(wifiConfig.preSharedKey)
                || !TextUtils.isEmpty(wifiConfig.wepKeys[0])
                || !TextUtils.isEmpty(wifiConfig.wepKeys[1])
                || !TextUtils.isEmpty(wifiConfig.wepKeys[2])
                || !TextUtils.isEmpty(wifiConfig.wepKeys[3]);        
    
public booleanhasSecurity()

        return security != null && !security.contains(OPEN);
    
public inthashCode()
{@inheritDoc}

see
#matches(int, String, String)
see
#equals(Object)

        // Two equal() objects must have same hashCode.
        // With Wi-Fi, the broadest match is if two SSIDs are the same.  The finer-grained matches
        // imply this (for example, the same network IDs means the same WifiConfiguration which
        // means the same SSID).
        // See #matches for the exact matching algorithm we use.
        return ssid != null ? ssid.hashCode() : 0;
    
public static booleanisAdhoc(android.net.wifi.ScanResult scanResult)

return
Whether the given ScanResult represents an adhoc network.

        return scanResult.capabilities.contains(ADHOC_CAPABILITY);
    
public booleanisConnectable()

return
Whether this AP can be connected to at the moment.

        return !primary && seen;
    
public static booleanisEnterprise(android.net.wifi.ScanResult scanResult)

return
Whether the given ScanResult has enterprise security.

        return scanResult.capabilities.contains(ENTERPRISE_CAPABILITY);
    
public booleanisForgetable()

return
Whether this AP can be forgotten at the moment.

        return configured;
    
private static booleanisHex(java.lang.String key)

        for (int i = key.length() - 1; i >= 0; i--) {
            final char c = key.charAt(i);
            if (!(c >= '0" && c <= '9" || c >= 'A" && c <= 'F" || c >= 'a" && c <= 'f")) {
                return false;
            }
        }
        
        return true;
    
private static booleanisHexWepKey(java.lang.String wepKey)

        final int len = wepKey.length();
        
        // WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?)
        if (len != 10 && len != 26 && len != 58) {
            return false;
        }
        
        return isHex(wepKey);
    
public intmatches(int otherNetworkId, java.lang.String otherBssid, java.lang.String otherSsid, java.lang.String otherSecurity)
{@inheritDoc}

see
#hashCode()
see
#equals(Object)

        
        // Whenever this method is touched, please ensure #equals and #hashCode
        // still work with the changes here!
        
        if (otherSsid == null) {
            if (WifiLayer.LOGV) {
                Log.w(TAG, "BSSID: " + otherBssid + ", SSID: " + otherSsid);
            }
            return MATCH_NONE;
        }

        /*
         * If we both have 'security' set, it must match (an open network still
         * has 'security' set to OPEN)
         */
        if (security != null && otherSecurity != null) {
            if (!security.equals(otherSecurity)) {
                return MATCH_NONE;
            }
        }
        
        // WifiConfiguration gives an empty bssid as a BSSID wildcard
        if (TextUtils.isEmpty(otherBssid)) {
            otherBssid = AccessPointState.BSSID_ANY;
        }

        final boolean networkIdMatches = networkId == otherNetworkId;
        if (!networkIdMatches && networkId != NETWORK_ID_ANY && otherNetworkId != NETWORK_ID_ANY) {
            // Network IDs don't match (e.g., 1 & 2 or unset & 1) and neither is a wildcard
            return MATCH_NONE;
        }
        
        if (networkIdMatches && otherNetworkId != NETWORK_ID_NOT_SET
                && otherNetworkId != NETWORK_ID_ANY) {
            // Network ID matches (they're set to the same ID)
            return MATCH_EXACT;
        }
        
        // So now, network IDs aren't set or at least one is a wildcard 
        
        final boolean bssidMatches = bssid.equals(otherBssid);
        final boolean otherBssidIsWildcard = otherBssid.equals(BSSID_ANY);
        if (bssidMatches && !otherBssidIsWildcard) {
            // BSSID matches (and neither is a wildcard)
            return MATCH_STRONG;
        }

        if (!bssidMatches && !bssid.equals(BSSID_ANY) && !otherBssidIsWildcard) {
            // BSSIDs don't match (e.g., 00:24:21:21:42:12 & 42:12:44:21:22:52)
            // and neither is a wildcard
            return MATCH_NONE;
        }
        
        // So now, BSSIDs are both wildcards
        
        final boolean ssidMatches = ssid.equals(otherSsid); 
        if (ssidMatches) {
            // SSID matches
            return MATCH_WEAK;
        }

        return MATCH_NONE;
    
public intmatchesWifiConfiguration(android.net.wifi.WifiConfiguration wifiConfig)

        String security = getWifiConfigurationSecurity(wifiConfig);
        return matches(wifiConfig.networkId, wifiConfig.BSSID, wifiConfig.SSID, security);
    
private voidparseWifiConfigurationSecurity(android.net.wifi.WifiConfiguration wifiConfig)

        setSecurity(getWifiConfigurationSecurity(wifiConfig));
        mConfigHadPassword = hasPassword(wifiConfig);
    
private voidrequestRefresh()

        if (mBlockRefresh > 0) {
            mNeedsRefresh = true;
            return;
        }
        
        if (mCallback != null) {
            mCallback.refreshAccessPointState();
        }
        
        mNeedsRefresh = false;
    
public voidsetBssid(java.lang.String bssid)

        if (bssid != null) {
            // If the BSSID is a wildcard, do NOT let a specific BSSID replace it
            if (!this.bssid.equals(BSSID_ANY)) {
                this.bssid = bssid;
            }
        }
    
public voidsetCallback(com.android.settings.wifi.AccessPointState$AccessPointStateCallback callback)

        mCallback = callback;
    
public voidsetConfigured(boolean configured)

        if (this.configured != configured) {
            this.configured = configured;
            requestRefresh();
        }
    
voidsetContext(android.content.Context context)

        mContext = context;
    
public voidsetDisabled(boolean disabled)

        if (this.disabled != disabled) {
            this.disabled = disabled;
            requestRefresh();
        }
    
public voidsetHiddenSsid(boolean hiddenSsid)

        if (this.hiddenSsid != hiddenSsid) {
            this.hiddenSsid = hiddenSsid;
            requestRefresh();
        }
    
public voidsetIpAddress(int address)

        if (ipAddress != address) {
            ipAddress = address;
            requestRefresh();
        }
    
public voidsetLinkSpeed(int linkSpeed)

        if (this.linkSpeed != linkSpeed) {
            this.linkSpeed = linkSpeed;
            requestRefresh();
        }
    
public voidsetNetworkId(int networkId)

        this.networkId = networkId;
    
public voidsetPassword(java.lang.String password)

        setPassword(password, WEP_PASSWORD_AUTO);
    
public voidsetPassword(java.lang.String password, int wepPasswordType)

        mPassword = password;
        mWepPasswordType = wepPasswordType;
    
public voidsetPrimary(boolean primary)

        if (this.primary != primary) {
            this.primary = primary;
            requestRefresh();
        }
    
public voidsetPriority(int priority)

        if (this.priority != priority) {
            this.priority = priority;
            requestRefresh();
        }
    
public voidsetSecurity(java.lang.String security)

        if (TextUtils.isEmpty(this.security) || !this.security.equals(security)) {
            this.security = security;
            requestRefresh();
        }
    
public voidsetSeen(boolean seen)

        if (this.seen != seen) {
            this.seen = seen;
            requestRefresh();
        }
    
public voidsetSignal(int signal)


        if (signalForSorting == Float.MIN_VALUE) {
            signalForSorting = signal;
        } else {
            signalForSorting = (DAMPING_FACTOR * signal) + ((1-DAMPING_FACTOR) * signalForSorting);
        }

        if (this.signal != signal) {
            this.signal = signal;
            requestRefresh();
        }
    
public voidsetSsid(java.lang.String ssid)

        if (ssid != null) {
            this.ssid = convertToQuotedString(ssid);
            requestRefresh();
        }
    
public voidsetStatus(NetworkInfo.DetailedState status)

        if (this.status != status) {
            this.status = status;
            requestRefresh();
        }
    
private voidsetupSecurity(android.net.wifi.WifiConfiguration config)

        config.allowedAuthAlgorithms.clear();
        config.allowedGroupCiphers.clear();
        config.allowedKeyManagement.clear();
        config.allowedPairwiseCiphers.clear();
        config.allowedProtocols.clear();
        
        if (TextUtils.isEmpty(security)) {
            security = OPEN;
            Log.w(TAG, "Empty security, assuming open");
        }
        
        if (security.equals(WEP)) {
            
            // If password is empty, it should be left untouched
            if (!TextUtils.isEmpty(mPassword)) {
                if (mWepPasswordType == WEP_PASSWORD_AUTO) {
                    if (isHexWepKey(mPassword)) {
                        config.wepKeys[0] = mPassword;
                    } else {
                        config.wepKeys[0] = convertToQuotedString(mPassword);
                    }
                } else {
                    config.wepKeys[0] = mWepPasswordType == WEP_PASSWORD_ASCII
                            ? convertToQuotedString(mPassword)
                            : mPassword;
                }
            }
            
            config.wepTxKeyIndex = 0;
            
            config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
            config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);

            config.allowedKeyManagement.set(KeyMgmt.NONE);
            
            config.allowedGroupCiphers.set(GroupCipher.WEP40);
            config.allowedGroupCiphers.set(GroupCipher.WEP104);
            
        } else if (security.equals(WPA) || security.equals(WPA2)){
            config.allowedGroupCiphers.set(GroupCipher.TKIP);
            config.allowedGroupCiphers.set(GroupCipher.CCMP);
            
            config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
            
            config.allowedPairwiseCiphers.set(PairwiseCipher.CCMP);
            config.allowedPairwiseCiphers.set(PairwiseCipher.TKIP);

            config.allowedProtocols.set(security.equals(WPA2) ? Protocol.RSN : Protocol.WPA);
            
            // If password is empty, it should be left untouched
            if (!TextUtils.isEmpty(mPassword)) {
                if (mPassword.length() == 64 && isHex(mPassword)) {
                    // Goes unquoted as hex
                    config.preSharedKey = mPassword;
                } else {
                    // Goes quoted as ASCII
                    config.preSharedKey = convertToQuotedString(mPassword);
                }
            }
            
        } else if (security.equals(OPEN)) {
            config.allowedKeyManagement.set(KeyMgmt.NONE);
        }
    
public java.lang.StringtoString()

        return ssid + " (" + bssid + ", " + networkId + ", " + super.toString() + ")";
    
voidunblockRefresh()

        if (--mBlockRefresh == 0 && mNeedsRefresh) {
            requestRefresh();
        }
    
public voidupdateFromScanResult(android.net.wifi.ScanResult scanResult)

        blockRefresh();
        
        // We don't keep specific AP BSSIDs and instead leave that as wildcard
        
        setSeen(true);
        setSsid(scanResult.SSID);
        if (networkId == NETWORK_ID_NOT_SET) {
            // Since ScanResults don't cross-reference network ID, we set it as a wildcard
            setNetworkId(NETWORK_ID_ANY);
        }
        setSignal(scanResult.level);
        setSecurity(getScanResultSecurity(scanResult));
        unblockRefresh();
    
public voidupdateFromWifiConfiguration(android.net.wifi.WifiConfiguration wifiConfig)

        if (wifiConfig != null) {
            blockRefresh();
            setBssid(wifiConfig.BSSID);
            setNetworkId(wifiConfig.networkId);
            setPriority(wifiConfig.priority);
            setHiddenSsid(wifiConfig.hiddenSSID);
            setSsid(wifiConfig.SSID);
            setConfigured(true);
            setDisabled(wifiConfig.status == WifiConfiguration.Status.DISABLED);
            parseWifiConfigurationSecurity(wifiConfig);
            unblockRefresh();
        }
    
public voidupdateFromWifiInfo(android.net.wifi.WifiInfo wifiInfo, NetworkInfo.DetailedState state)

        if (wifiInfo != null) {
            blockRefresh();
            setBssid(wifiInfo.getBSSID());
            setLinkSpeed(wifiInfo.getLinkSpeed());
            setNetworkId(wifiInfo.getNetworkId());
            setIpAddress(wifiInfo.getIpAddress());
            setSsid(wifiInfo.getSSID());
            if (state != null) {
                setStatus(state);
            }
            setHiddenSsid(wifiInfo.getHiddenSSID());
            unblockRefresh();
        }
    
public voidupdateWifiConfiguration(android.net.wifi.WifiConfiguration config)

        config.BSSID = getWpaSupplicantBssid();
        config.priority = priority;
        config.hiddenSSID = hiddenSsid;
        config.SSID = convertToQuotedString(ssid);
        
        setupSecurity(config);
    
public voidwriteToParcel(android.os.Parcel dest, int flags)
Implement the Parcelable interface

        dest.writeString(bssid);
        dest.writeInt(configured ? 1 : 0);
        dest.writeInt(ipAddress);
        dest.writeInt(linkSpeed);
        dest.writeInt(networkId);
        dest.writeInt(primary ? 1 : 0);
        dest.writeInt(priority);
        dest.writeInt(hiddenSsid ? 1 : 0);
        dest.writeString(security);
        dest.writeInt(seen ? 1 : 0);
        dest.writeInt(disabled ? 1 : 0);
        dest.writeInt(signal);
        dest.writeString(ssid);
        dest.writeString(status != null ? status.toString() : null);
        dest.writeString(mPassword);
        dest.writeInt(mConfigHadPassword ? 1 : 0);
        dest.writeInt(mWepPasswordType);