1/*
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            for (PasspointConfiguration config : savedPasspointConfigs) {
59                savedConfigs.add(new AccessPoint(context, config));
60            }
61        } catch (UnsupportedOperationException e) {
62            // Passpoint not supported.
63        }
64        return savedConfigs;
65    }
66}
67
68