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

Commit ca2f8a2b authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "wifi: add utility function to return saved configurations" into oc-dev

parents 8493543c abea7262
Loading
Loading
Loading
Loading
+67 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
package com.android.settingslib.wifi;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.net.wifi.hotspot2.PasspointConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Provide utility functions for retrieving saved Wi-Fi configurations.
 */
public class WifiSavedConfigUtils {
    /**
     * Returns all the saved configurations on the device, including both Wi-Fi networks and
     * Passpoint profiles, represented by {@link AccessPoint}.
     *
     * @param context The application context
     * @param wifiManager An instance of {@link WifiManager}
     * @return List of {@link AccessPoint}
     */
    public static List<AccessPoint> getAllConfigs(Context context, WifiManager wifiManager) {
        List<AccessPoint> savedConfigs = new ArrayList<>();
        List<WifiConfiguration> savedNetworks = wifiManager.getConfiguredNetworks();
        for (WifiConfiguration network : savedNetworks) {
            // Configuration for Passpoint network is configured temporary by WifiService for
            // connection attempt only.  The underlying configuration is saved as Passpoint
            // configuration, which will be retrieved with WifiManager#getPasspointConfiguration
            // call below.
            if (network.isPasspoint()) {
                continue;
            }
            // Ephemeral networks are not saved to the persistent storage, ignore them.
            if (network.isEphemeral()) {
                continue;
            }
            savedConfigs.add(new AccessPoint(context, network));
        }
        try {
            List<PasspointConfiguration> savedPasspointConfigs =
                    wifiManager.getPasspointConfigurations();
            for (PasspointConfiguration config : savedPasspointConfigs) {
                savedConfigs.add(new AccessPoint(context, config));
            }
        } catch (UnsupportedOperationException e) {
            // Passpoint not supported.
        }
        return savedConfigs;
    }
}
+3 −5
Original line number Diff line number Diff line
@@ -95,8 +95,6 @@ public class WifiTracker {

    private WifiTrackerNetworkCallback mNetworkCallback;

    private int mNumSavedNetworks;

    @GuardedBy("mLock")
    private boolean mRegistered;

@@ -399,9 +397,11 @@ public class WifiTracker {
    /**
     * Returns the number of saved networks on the device, regardless of whether the WifiTracker
     * is tracking saved networks.
     * TODO(b/62292448): remove this function and update callsites to use WifiSavedConfigUtils
     * directly.
     */
    public int getNumSavedNetworks() {
        return mNumSavedNetworks;
        return WifiSavedConfigUtils.getAllConfigs(mContext, mWifiManager).size();
    }

    public boolean isConnected() {
@@ -499,12 +499,10 @@ public class WifiTracker {

        final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
        if (configs != null) {
            mNumSavedNetworks = 0;
            for (WifiConfiguration config : configs) {
                if (config.selfAdded && config.numAssociation == 0) {
                    continue;
                }
                mNumSavedNetworks++;
                AccessPoint accessPoint = getCachedOrCreate(config, cachedAccessPoints);
                if (mLastInfo != null && mLastNetworkInfo != null) {
                    accessPoint.update(connectionConfig, mLastInfo, mLastNetworkInfo);
+2 −1
Original line number Diff line number Diff line
@@ -410,6 +410,7 @@ public class WifiTrackerTest {
        validConfig.BSSID = BSSID_1;

        WifiConfiguration selfAddedNoAssociation = new WifiConfiguration();
        selfAddedNoAssociation.ephemeral = true;
        selfAddedNoAssociation.selfAdded = true;
        selfAddedNoAssociation.numAssociation = 0;
        selfAddedNoAssociation.SSID = SSID_2;