diff options
3 files changed, 72 insertions, 6 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/wifi/WifiSavedConfigUtils.java b/packages/SettingsLib/src/com/android/settingslib/wifi/WifiSavedConfigUtils.java new file mode 100644 index 000000000000..159b2a1047d9 --- /dev/null +++ b/packages/SettingsLib/src/com/android/settingslib/wifi/WifiSavedConfigUtils.java @@ -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; + } +} + diff --git a/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java b/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java index 7a63b8acc9e1..20cc5a623358 100644 --- a/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java +++ b/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java @@ -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); diff --git a/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/WifiTrackerTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/WifiTrackerTest.java index d4ce40c7c0a8..086e10cda499 100644 --- a/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/WifiTrackerTest.java +++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/WifiTrackerTest.java @@ -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; @@ -746,4 +747,4 @@ public class WifiTrackerTest { verifyNoMoreInteractions(mockWifiListener); } -}
\ No newline at end of file +} |