Merge "wifi: add utility function to return saved configurations" into oc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
ca2f8a2b13
@@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -95,8 +95,6 @@ public class WifiTracker {
|
|||||||
|
|
||||||
private WifiTrackerNetworkCallback mNetworkCallback;
|
private WifiTrackerNetworkCallback mNetworkCallback;
|
||||||
|
|
||||||
private int mNumSavedNetworks;
|
|
||||||
|
|
||||||
@GuardedBy("mLock")
|
@GuardedBy("mLock")
|
||||||
private boolean mRegistered;
|
private boolean mRegistered;
|
||||||
|
|
||||||
@@ -399,9 +397,11 @@ public class WifiTracker {
|
|||||||
/**
|
/**
|
||||||
* Returns the number of saved networks on the device, regardless of whether the WifiTracker
|
* Returns the number of saved networks on the device, regardless of whether the WifiTracker
|
||||||
* is tracking saved networks.
|
* is tracking saved networks.
|
||||||
|
* TODO(b/62292448): remove this function and update callsites to use WifiSavedConfigUtils
|
||||||
|
* directly.
|
||||||
*/
|
*/
|
||||||
public int getNumSavedNetworks() {
|
public int getNumSavedNetworks() {
|
||||||
return mNumSavedNetworks;
|
return WifiSavedConfigUtils.getAllConfigs(mContext, mWifiManager).size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isConnected() {
|
public boolean isConnected() {
|
||||||
@@ -499,12 +499,10 @@ public class WifiTracker {
|
|||||||
|
|
||||||
final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
|
final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
|
||||||
if (configs != null) {
|
if (configs != null) {
|
||||||
mNumSavedNetworks = 0;
|
|
||||||
for (WifiConfiguration config : configs) {
|
for (WifiConfiguration config : configs) {
|
||||||
if (config.selfAdded && config.numAssociation == 0) {
|
if (config.selfAdded && config.numAssociation == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
mNumSavedNetworks++;
|
|
||||||
AccessPoint accessPoint = getCachedOrCreate(config, cachedAccessPoints);
|
AccessPoint accessPoint = getCachedOrCreate(config, cachedAccessPoints);
|
||||||
if (mLastInfo != null && mLastNetworkInfo != null) {
|
if (mLastInfo != null && mLastNetworkInfo != null) {
|
||||||
accessPoint.update(connectionConfig, mLastInfo, mLastNetworkInfo);
|
accessPoint.update(connectionConfig, mLastInfo, mLastNetworkInfo);
|
||||||
|
|||||||
@@ -410,6 +410,7 @@ public class WifiTrackerTest {
|
|||||||
validConfig.BSSID = BSSID_1;
|
validConfig.BSSID = BSSID_1;
|
||||||
|
|
||||||
WifiConfiguration selfAddedNoAssociation = new WifiConfiguration();
|
WifiConfiguration selfAddedNoAssociation = new WifiConfiguration();
|
||||||
|
selfAddedNoAssociation.ephemeral = true;
|
||||||
selfAddedNoAssociation.selfAdded = true;
|
selfAddedNoAssociation.selfAdded = true;
|
||||||
selfAddedNoAssociation.numAssociation = 0;
|
selfAddedNoAssociation.numAssociation = 0;
|
||||||
selfAddedNoAssociation.SSID = SSID_2;
|
selfAddedNoAssociation.SSID = SSID_2;
|
||||||
@@ -746,4 +747,4 @@ public class WifiTrackerTest {
|
|||||||
|
|
||||||
verifyNoMoreInteractions(mockWifiListener);
|
verifyNoMoreInteractions(mockWifiListener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user