blob: 65c7786235bf96f38812bf2e6aa574ed27ad53a0 [file] [log] [blame]
Alan Viverette3da604b2020-06-10 18:34:39 +00001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16package com.android.settingslib.wifi;
17
18import android.content.Context;
19import android.net.wifi.WifiConfiguration;
20import android.net.wifi.WifiManager;
21import android.net.wifi.hotspot2.PasspointConfiguration;
22
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * Provide utility functions for retrieving saved Wi-Fi configurations.
28 */
29public class WifiSavedConfigUtils {
30 /**
31 * Returns all the saved configurations on the device, including both Wi-Fi networks and
32 * Passpoint profiles, represented by {@link AccessPoint}.
33 *
34 * @param context The application context
35 * @param wifiManager An instance of {@link WifiManager}
36 * @return List of {@link AccessPoint}
37 */
38 public static List<AccessPoint> getAllConfigs(Context context, WifiManager wifiManager) {
39 List<AccessPoint> savedConfigs = new ArrayList<>();
40 List<WifiConfiguration> savedNetworks = wifiManager.getConfiguredNetworks();
41 for (WifiConfiguration network : savedNetworks) {
42 // Configuration for Passpoint network is configured temporary by WifiService for
43 // connection attempt only. The underlying configuration is saved as Passpoint
44 // configuration, which will be retrieved with WifiManager#getPasspointConfiguration
45 // call below.
46 if (network.isPasspoint()) {
47 continue;
48 }
49 // Ephemeral networks are not saved to the persistent storage, ignore them.
50 if (network.isEphemeral()) {
51 continue;
52 }
53 savedConfigs.add(new AccessPoint(context, network));
54 }
55 try {
56 List<PasspointConfiguration> savedPasspointConfigs =
57 wifiManager.getPasspointConfigurations();
58 if (savedPasspointConfigs != null) {
59 for (PasspointConfiguration config : savedPasspointConfigs) {
60 savedConfigs.add(new AccessPoint(context, config));
61 }
62 }
63 } catch (UnsupportedOperationException e) {
64 // Passpoint not supported.
65 }
66 return savedConfigs;
67 }
68
69 /**
70 * Returns the count of the saved configurations on the device, including both Wi-Fi networks
71 * and Passpoint profiles.
72 *
73 * @param context The application context
74 * @param wifiManager An instance of {@link WifiManager}
75 * @return count of saved Wi-Fi networks
76 */
77 public static int getAllConfigsCount(Context context, WifiManager wifiManager) {
78 return getAllConfigs(context, wifiManager).size();
79 }
80}
81