FileDocCategorySizeDatePackage
AccessPointControllerImpl.javaAPI DocAndroid 5.1 API11494Thu Mar 12 22:22:42 GMT 2015com.android.systemui.statusbar.policy

AccessPointControllerImpl

public class AccessPointControllerImpl extends Object implements NetworkController.AccessPointController

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private static final String
EXTRA_START_CONNECT_SSID
private static final int[]
ICONS
private final android.content.Context
mContext
private final ArrayList
mCallbacks
private final android.net.wifi.WifiManager
mWifiManager
private final android.os.UserManager
mUserManager
private final Receiver
mReceiver
private NetworkControllerImpl
mNetworkController
private boolean
mScanning
private int
mCurrentUser
private final android.net.wifi.WifiManager.ActionListener
mConnectListener
private final Comparator
mByStrength
Constructors Summary
public AccessPointControllerImpl(android.content.Context context)


       
        mContext = context;
        mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
        mCurrentUser = ActivityManager.getCurrentUser();
    
Methods Summary
public voidaddAccessPointCallback(AccessPointCallback callback)

        if (callback == null || mCallbacks.contains(callback)) return;
        if (DEBUG) Log.d(TAG, "addCallback " + callback);
        mCallbacks.add(callback);
        mReceiver.setListening(!mCallbacks.isEmpty());
    
public booleancanConfigWifi()

        return !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI,
                new UserHandle(mCurrentUser));
    
public booleanconnect(AccessPoint ap)

        if (ap == null) return false;
        if (DEBUG) Log.d(TAG, "connect networkId=" + ap.networkId);
        if (ap.networkId < 0) {
            // Unknown network, need to add it.
            if (ap.hasSecurity) {
                Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
                intent.putExtra(EXTRA_START_CONNECT_SSID, ap.ssid);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                fireSettingsIntentCallback(intent);
                return true;
            } else {
                WifiConfiguration config = new WifiConfiguration();
                config.SSID = "\"" + ap.ssid + "\"";
                config.allowedKeyManagement.set(KeyMgmt.NONE);
                mWifiManager.connect(config, mConnectListener);
            }
        } else {
            mWifiManager.connect(ap.networkId, mConnectListener);
        }
        return false;
    
private voidfireAcccessPointsCallback(AccessPoint[] aps)

        for (AccessPointCallback callback : mCallbacks) {
            callback.onAccessPointsChanged(aps);
        }
    
private voidfireSettingsIntentCallback(android.content.Intent intent)

        for (AccessPointCallback callback : mCallbacks) {
            callback.onSettingsActivityTriggered(intent);
        }
    
private android.util.ArrayMapgetConfiguredNetworksBySsid()

        final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
        if (configs == null || configs.size() == 0) return ArrayMap.EMPTY;
        final ArrayMap<String, WifiConfiguration> rt = new ArrayMap<String, WifiConfiguration>();
        for (WifiConfiguration config : configs) {
            rt.put(trimDoubleQuotes(config.SSID), config);
        }
        return rt;
    
private intgetConnectedNetworkId(android.net.wifi.WifiInfo wifiInfo)

        return wifiInfo != null ? wifiInfo.getNetworkId() : AccessPoint.NO_NETWORK;
    
public voidonUserSwitched(int newUserId)

        mCurrentUser = newUserId;
    
public voidremoveAccessPointCallback(AccessPointCallback callback)

        if (callback == null) return;
        if (DEBUG) Log.d(TAG, "removeCallback " + callback);
        mCallbacks.remove(callback);
        mReceiver.setListening(!mCallbacks.isEmpty());
    
public voidscanForAccessPoints()

        if (mScanning) return;
        if (DEBUG) Log.d(TAG, "scan!");
        mScanning = mWifiManager.startScan();
        // Grab current networks immediately while we wait for scan.
        updateAccessPoints();
    
voidsetNetworkController(NetworkControllerImpl networkController)

        mNetworkController = networkController;
    
private static java.lang.StringtrimDoubleQuotes(java.lang.String v)

        return v != null && v.length() >= 2 && v.charAt(0) == '\""
                && v.charAt(v.length() - 1) == '\"" ? v.substring(1, v.length() - 1) : v;
    
private voidupdateAccessPoints()

        final WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
        final int connectedNetworkId = getConnectedNetworkId(wifiInfo);
        if (DEBUG) Log.d(TAG, "connectedNetworkId: " + connectedNetworkId);
        final List<ScanResult> scanResults = mWifiManager.getScanResults();
        final ArrayMap<String, WifiConfiguration> configured = getConfiguredNetworksBySsid();
        if (DEBUG) Log.d(TAG, "scanResults: " + scanResults);
        final List<AccessPoint> aps = new ArrayList<AccessPoint>(scanResults.size());
        final ArraySet<String> ssids = new ArraySet<String>();
        for (ScanResult scanResult : scanResults) {
            if (scanResult == null) {
                continue;
            }
            final String ssid = scanResult.SSID;
            if (TextUtils.isEmpty(ssid) || ssids.contains(ssid)) continue;
            ssids.add(ssid);
            final WifiConfiguration config = configured.get(ssid);
            final int level = WifiManager.calculateSignalLevel(scanResult.level, ICONS.length);
            final AccessPoint ap = new AccessPoint();
            ap.isConfigured = config != null;
            ap.networkId = config != null ? config.networkId : AccessPoint.NO_NETWORK;
            ap.ssid = ssid;
            // Connected if either:
            // -The network ID in the active WifiInfo matches this network's ID.
            // -The network is ephemeral (no configuration) but the SSID matches.
            ap.isConnected = (ap.networkId != AccessPoint.NO_NETWORK
                    && ap.networkId == connectedNetworkId) ||
                    (ap.networkId == WifiConfiguration.INVALID_NETWORK_ID && wifiInfo != null &&
                    ap.ssid.equals(trimDoubleQuotes(wifiInfo.getSSID())));
            if (ap.isConnected && mNetworkController != null) {
                // Ensure we have the connected network's RSSI.
                ap.level = mNetworkController.getConnectedWifiLevel();
            } else {
                ap.level = level;
            }
            ap.iconId = ICONS[ap.level];
            // Based on Settings AccessPoint#getSecurity, keep up to date
            // with better methods of determining no security or not.
            ap.hasSecurity = scanResult.capabilities.contains("WEP")
                    || scanResult.capabilities.contains("PSK")
                    || scanResult.capabilities.contains("EAP");
            aps.add(ap);
        }
        Collections.sort(aps, mByStrength);
        fireAcccessPointsCallback(aps.toArray(new AccessPoint[aps.size()]));