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  */
16 package com.android.settingslib.wifi;
17 
18 import android.content.Context;
19 import android.net.wifi.WifiConfiguration;
20 import android.net.wifi.WifiManager;
21 import android.net.wifi.hotspot2.PasspointConfiguration;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 
26 /**
27  * Provide utility functions for retrieving saved Wi-Fi configurations.
28  */
29 public 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      */
getAllConfigs(Context context, WifiManager wifiManager)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      */
getAllConfigsCount(Context context, WifiManager wifiManager)77     public static int getAllConfigsCount(Context context, WifiManager wifiManager) {
78         return getAllConfigs(context, wifiManager).size();
79     }
80 }
81 
82