Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0d492e83 authored by Sundeep Ghuman's avatar Sundeep Ghuman
Browse files

Clean up updateAccessPoints.

Remove obsolete logic, refactor for efficiency and readability. No
longer make redundant, duplicate IPCs.

Bug: b/68030053
Test: runtest --path
frameworks/base/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/WifiTrackerTest.java

Change-Id: I1a1077c1adaacbd759521451a994a7655befdf58
parent 8d157e38
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -622,6 +622,19 @@ public class AccessPoint implements Comparable<AccessPoint> {
        return builder.toString();
    }

    public static String getKey(WifiConfiguration config) {
        StringBuilder builder = new StringBuilder();

        if (TextUtils.isEmpty(config.SSID)) {
            builder.append(config.BSSID);
        } else {
            builder.append(removeDoubleQuotes(config.SSID));
        }

        builder.append(',').append(getSecurity(config));
        return builder.toString();
    }

    public String getKey() {
        return mKey;
    }
+15 −63
Original line number Diff line number Diff line
@@ -146,7 +146,6 @@ public class WifiTracker implements LifecycleObserver, OnStart, OnStop, OnDestro

    // TODO(sghuman): Change this to be keyed on AccessPoint.getKey
    private final HashMap<String, ScanResult> mScanResultCache = new HashMap<>();
    private Integer mScanId = 0;

    private NetworkInfo mLastNetworkInfo;
    private WifiInfo mLastInfo;
@@ -450,7 +449,6 @@ public class WifiTracker implements LifecycleObserver, OnStart, OnStop, OnDestro
    private void handleResume() {
        mScanResultCache.clear();
        mSeenBssids.clear();
        mScanId = 0;
    }

    private Collection<ScanResult> updateScanResultCache(final List<ScanResult> newResults) {
@@ -525,7 +523,7 @@ public class WifiTracker implements LifecycleObserver, OnStart, OnStop, OnDestro
    /**
     * Update the internal list of access points.
     *
     * <p>Do not called directly (except for forceUpdate), use {@link #updateAccessPoints()} which
     * <p>Do not call directly (except for forceUpdate), use {@link #updateAccessPoints()} which
     * respects {@link #mStaleScanResults}.
     */
    @GuardedBy("mLock")
@@ -534,7 +532,7 @@ public class WifiTracker implements LifecycleObserver, OnStart, OnStop, OnDestro
        WifiConfiguration connectionConfig = null;
        if (mLastInfo != null) {
            connectionConfig = getWifiConfigurationForNetworkId(
                    mLastInfo.getNetworkId(), mWifiManager.getConfiguredNetworks());
                    mLastInfo.getNetworkId(), configs);
        }

        // Swap the current access points into a cached list.
@@ -546,38 +544,12 @@ public class WifiTracker implements LifecycleObserver, OnStart, OnStop, OnDestro
            accessPoint.clearConfig();
        }

    /* Lookup table to more quickly update AccessPoints by only considering objects with the
     * correct SSID.  Maps SSID -> List of AccessPoints with the given SSID.  */
        Multimap<String, AccessPoint> existingApMap = new Multimap<String, AccessPoint>();

        final Collection<ScanResult> results = updateScanResultCache(newScanResults);

        // TODO(sghuman): This entire block only exists to populate the WifiConfiguration for
        // APs, remove and refactor
        final Map<String, WifiConfiguration> configsByKey = new ArrayMap(configs.size());
        if (configs != null) {
            for (WifiConfiguration config : configs) {
                if (config.selfAdded && config.numAssociation == 0) {
                    continue;
                }
                AccessPoint accessPoint = getCachedOrCreate(config, cachedAccessPoints);
                if (mLastInfo != null && mLastNetworkInfo != null) {
                    accessPoint.update(connectionConfig, mLastInfo, mLastNetworkInfo);
                }

                // If saved network not present in scan result then set its Rssi to
                // UNREACHABLE_RSSI
                boolean apFound = false;
                for (ScanResult result : results) {
                    if (result.SSID.equals(accessPoint.getSsidStr())) {
                        apFound = true;
                        break;
                    }
                }
                if (!apFound) {
                    accessPoint.setUnreachable();
                }
                accessPoints.add(accessPoint);
                existingApMap.put(accessPoint.getSsidStr(), accessPoint);
                configsByKey.put(AccessPoint.getKey(config), config);
            }
        }

@@ -613,42 +585,22 @@ public class WifiTracker implements LifecycleObserver, OnStart, OnStop, OnDestro
            for (Map.Entry<String, List<ScanResult>> entry : scanResultsByApKey.entrySet()) {
                // List can not be empty as it is dynamically constructed on each iteration
                ScanResult firstResult = entry.getValue().get(0);
                boolean found = false;
                for (AccessPoint accessPoint : existingApMap.getAll(firstResult.SSID)) {
                    accessPoint.setScanResults(entry.getValue());
                    found = true;
                    break;
                }

                // Only create a new AP / add to the list if it wasn't already in the saved configs
                if (!found) {
                AccessPoint accessPoint =
                        getCachedOrCreate(entry.getValue(), cachedAccessPoints);
                if (mLastInfo != null && mLastNetworkInfo != null) {
                    accessPoint.update(connectionConfig, mLastInfo, mLastNetworkInfo);
                }

                    // TODO(sghuman): Move isPasspointNetwork logic into AccessPoint.java
                    if (firstResult.isPasspointNetwork()) {
                        // Retrieve a WifiConfiguration for a Passpoint provider that matches
                        // the given ScanResult.  This is used for showing that a given AP
                        // (ScanResult) is available via a Passpoint provider (provider friendly
                        // name).
                        try {
                            WifiConfiguration config =
                                    mWifiManager.getMatchingWifiConfig(firstResult);
                // Update the matching config if there is one, to populate saved network info
                WifiConfiguration config = configsByKey.get(entry.getKey());
                if (config != null) {
                    accessPoint.update(config);
                }
                        } catch (UnsupportedOperationException e) {
                            // Passpoint not supported on the device.
                        }
                    }

                accessPoints.add(accessPoint);
            }
        }
        }

        requestScoresForNetworkKeys(scoresToRequest);
        for (AccessPoint ap : accessPoints) {
+2 −4
Original line number Diff line number Diff line
@@ -685,6 +685,7 @@ public class WifiTrackerTest {
     */
    @Test
    public void trackPasspointApWithPasspointDisabled() throws Exception {
        // TODO(sghuman): Delete this test and replace with a passpoint test
        WifiTracker tracker = createMockedWifiTracker();

        // Add a Passpoint AP to the scan results.
@@ -707,10 +708,7 @@ public class WifiTrackerTest {
        when(mockWifiManager.getConfiguredNetworks())
                .thenReturn(new ArrayList<WifiConfiguration>());
        when(mockWifiManager.getScanResults()).thenReturn(results);
        doThrow(new UnsupportedOperationException())
                .when(mockWifiManager).getMatchingWifiConfig(any(ScanResult.class));
        tracker.forceUpdate();
        verify(mockWifiManager).getMatchingWifiConfig(any(ScanResult.class));
    }

    @Test
@@ -756,7 +754,7 @@ public class WifiTrackerTest {
        tracker.forceUpdate();

        verify(mockWifiManager).getConnectionInfo();
        verify(mockWifiManager, times(2)).getConfiguredNetworks();
        verify(mockWifiManager, times(1)).getConfiguredNetworks();
        verify(mockConnectivityManager).getNetworkInfo(any(Network.class));

        verify(mockWifiListener, never()).onAccessPointsChanged(); // mStaleAccessPoints is true