1 /* 2 * Copyright (C) 2021 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 17 package com.android.tv.settings.library.network; 18 19 import android.content.Context; 20 import android.net.wifi.WifiConfiguration; 21 import android.net.wifi.WifiManager; 22 import android.net.wifi.hotspot2.PasspointConfiguration; 23 24 import java.util.ArrayList; 25 import java.util.List; 26 27 /** 28 * Provide utility functions for retrieving saved Wi-Fi configurations. 29 */ 30 public class WifiSavedConfigUtils { 31 /** 32 * Returns all the saved configurations on the device, including both Wi-Fi networks and 33 * Passpoint profiles, represented by {@link .AccessPoint}. 34 * 35 * @param context The application context 36 * @param wifiManager An instance of {@link WifiManager} 37 * @return List of {@link .AccessPoint} 38 */ getAllConfigs(Context context, WifiManager wifiManager)39 public static List<AccessPoint> getAllConfigs(Context context, WifiManager wifiManager) { 40 List<AccessPoint> savedConfigs = new ArrayList<>(); 41 List<WifiConfiguration> savedNetworks = wifiManager.getConfiguredNetworks(); 42 for (WifiConfiguration network : savedNetworks) { 43 // Configuration for Passpoint network is configured temporary by WifiService for 44 // connection attempt only. The underlying configuration is saved as Passpoint 45 // configuration, which will be retrieved with WifiManager#getPasspointConfiguration 46 // call below. 47 if (network.isPasspoint()) { 48 continue; 49 } 50 // Ephemeral networks are not saved to the persistent storage, ignore them. 51 if (network.isEphemeral()) { 52 continue; 53 } 54 savedConfigs.add(new AccessPoint(context, network)); 55 } 56 try { 57 List<PasspointConfiguration> savedPasspointConfigs = 58 wifiManager.getPasspointConfigurations(); 59 if (savedPasspointConfigs != null) { 60 for (PasspointConfiguration config : savedPasspointConfigs) { 61 savedConfigs.add(new AccessPoint(context, config)); 62 } 63 } 64 } catch (UnsupportedOperationException e) { 65 // Passpoint not supported. 66 } 67 return savedConfigs; 68 } 69 70 /** 71 * Returns the count of the saved configurations on the device, including both Wi-Fi networks 72 * and Passpoint profiles. 73 * 74 * @param context The application context 75 * @param wifiManager An instance of {@link WifiManager} 76 * @return count of saved Wi-Fi networks 77 */ getAllConfigsCount(Context context, WifiManager wifiManager)78 public static int getAllConfigsCount(Context context, WifiManager wifiManager) { 79 return getAllConfigs(context, wifiManager).size(); 80 } 81 } 82 83 84