1 package com.android.server.wifi; 2 3 import android.net.wifi.ScanResult; 4 import android.net.wifi.WifiConfiguration; 5 import android.os.UserHandle; 6 import android.os.UserManager; 7 8 import java.util.Collection; 9 import java.util.HashMap; 10 import java.util.Iterator; 11 import java.util.Map; 12 13 public class ConfigurationMap { 14 private final Map<Integer, WifiConfiguration> mPerID = new HashMap<>(); 15 16 private final Map<Integer, WifiConfiguration> mPerIDForCurrentUser = new HashMap<>(); 17 private final Map<ScanResultMatchInfo, WifiConfiguration> 18 mScanResultMatchInfoMapForCurrentUser = new HashMap<>(); 19 20 private final UserManager mUserManager; 21 22 private int mCurrentUserId = UserHandle.USER_SYSTEM; 23 24 ConfigurationMap(UserManager userManager) { 25 mUserManager = userManager; 26 } 27 28 // RW methods: 29 public WifiConfiguration put(WifiConfiguration config) { 30 final WifiConfiguration current = mPerID.put(config.networkId, config); 31 if (WifiConfigurationUtil.isVisibleToAnyProfile(config, 32 mUserManager.getProfiles(mCurrentUserId))) { 33 mPerIDForCurrentUser.put(config.networkId, config); 34 mScanResultMatchInfoMapForCurrentUser.put( 35 ScanResultMatchInfo.fromWifiConfiguration(config), config); 36 } 37 return current; 38 } 39 40 public WifiConfiguration remove(int netID) { 41 WifiConfiguration config = mPerID.remove(netID); 42 if (config == null) { 43 return null; 44 } 45 46 mPerIDForCurrentUser.remove(netID); 47 48 Iterator<Map.Entry<ScanResultMatchInfo, WifiConfiguration>> scanResultMatchInfoEntries = 49 mScanResultMatchInfoMapForCurrentUser.entrySet().iterator(); 50 while (scanResultMatchInfoEntries.hasNext()) { 51 if (scanResultMatchInfoEntries.next().getValue().networkId == netID) { 52 scanResultMatchInfoEntries.remove(); 53 break; 54 } 55 } 56 return config; 57 } 58 59 public void clear() { 60 mPerID.clear(); 61 mPerIDForCurrentUser.clear(); 62 mScanResultMatchInfoMapForCurrentUser.clear(); 63 } 64 65 /** 66 * Sets the new foreground user ID. 67 * 68 * @param userId the id of the new foreground user 69 */ 70 public void setNewUser(int userId) { 71 mCurrentUserId = userId; 72 } 73 74 // RO methods: 75 public WifiConfiguration getForAllUsers(int netid) { 76 return mPerID.get(netid); 77 } 78 79 public WifiConfiguration getForCurrentUser(int netid) { 80 return mPerIDForCurrentUser.get(netid); 81 } 82 83 public int sizeForAllUsers() { 84 return mPerID.size(); 85 } 86 87 public int sizeForCurrentUser() { 88 return mPerIDForCurrentUser.size(); 89 } 90 91 public WifiConfiguration getByConfigKeyForCurrentUser(String key) { 92 if (key == null) { 93 return null; 94 } 95 for (WifiConfiguration config : mPerIDForCurrentUser.values()) { 96 if (config.configKey().equals(key)) { 97 return config; 98 } 99 } 100 return null; 101 } 102 103 /** 104 * Retrieves the |WifiConfiguration| object matching the provided |scanResult| from the internal 105 * map. 106 * Essentially checks if network config and scan result have the same SSID and encryption type. 107 */ 108 public WifiConfiguration getByScanResultForCurrentUser(ScanResult scanResult) { 109 return mScanResultMatchInfoMapForCurrentUser.get( 110 ScanResultMatchInfo.fromScanResult(scanResult)); 111 } 112 113 public Collection<WifiConfiguration> valuesForAllUsers() { 114 return mPerID.values(); 115 } 116 117 public Collection<WifiConfiguration> valuesForCurrentUser() { 118 return mPerIDForCurrentUser.values(); 119 } 120 } 121