1 /* 2 * Copyright (C) 2015 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.server.wifi; 18 19 import android.net.wifi.ScanResult; 20 import android.net.wifi.WifiConfiguration; 21 import android.os.UserHandle; 22 import android.os.UserManager; 23 24 import java.io.FileDescriptor; 25 import java.io.PrintWriter; 26 import java.util.Collection; 27 import java.util.HashMap; 28 import java.util.Iterator; 29 import java.util.Map; 30 31 public class ConfigurationMap { 32 private final Map<Integer, WifiConfiguration> mPerID = new HashMap<>(); 33 34 private final Map<Integer, WifiConfiguration> mPerIDForCurrentUser = new HashMap<>(); 35 private final Map<ScanResultMatchInfo, WifiConfiguration> 36 mScanResultMatchInfoMapForCurrentUser = new HashMap<>(); 37 38 private final UserManager mUserManager; 39 40 private int mCurrentUserId = UserHandle.SYSTEM.getIdentifier(); 41 ConfigurationMap(UserManager userManager)42 ConfigurationMap(UserManager userManager) { 43 mUserManager = userManager; 44 } 45 46 /** Dump internal state for debugging. */ dump(FileDescriptor fd, PrintWriter pw, String[] args)47 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 48 pw.println("mPerId=" + mPerID); 49 pw.println("mPerIDForCurrentUser=" + mPerIDForCurrentUser); 50 pw.println("mScanResultMatchInfoMapForCurrentUser=" 51 + mScanResultMatchInfoMapForCurrentUser); 52 pw.println("mCurrentUserId=" + mCurrentUserId); 53 } 54 55 // RW methods: put(WifiConfiguration config)56 public WifiConfiguration put(WifiConfiguration config) { 57 final WifiConfiguration current = mPerID.put(config.networkId, config); 58 final UserHandle currentUser = UserHandle.of(mCurrentUserId); 59 final UserHandle creatorUser = UserHandle.getUserHandleForUid(config.creatorUid); 60 if (config.shared || currentUser.equals(creatorUser) 61 || mUserManager.isSameProfileGroup(currentUser, creatorUser)) { 62 mPerIDForCurrentUser.put(config.networkId, config); 63 // TODO (b/142035508): Add a more generic fix. This cache should only hold saved 64 // networks. 65 if (!config.fromWifiNetworkSpecifier && !config.fromWifiNetworkSuggestion 66 && !config.isPasspoint()) { 67 mScanResultMatchInfoMapForCurrentUser.put( 68 ScanResultMatchInfo.fromWifiConfiguration(config), config); 69 } 70 } 71 return current; 72 } 73 remove(int netID)74 public WifiConfiguration remove(int netID) { 75 WifiConfiguration config = mPerID.remove(netID); 76 if (config == null) { 77 return null; 78 } 79 80 mPerIDForCurrentUser.remove(netID); 81 82 Iterator<Map.Entry<ScanResultMatchInfo, WifiConfiguration>> scanResultMatchInfoEntries = 83 mScanResultMatchInfoMapForCurrentUser.entrySet().iterator(); 84 while (scanResultMatchInfoEntries.hasNext()) { 85 if (scanResultMatchInfoEntries.next().getValue().networkId == netID) { 86 scanResultMatchInfoEntries.remove(); 87 break; 88 } 89 } 90 return config; 91 } 92 clear()93 public void clear() { 94 mPerID.clear(); 95 mPerIDForCurrentUser.clear(); 96 mScanResultMatchInfoMapForCurrentUser.clear(); 97 } 98 99 /** 100 * Sets the new foreground user ID. 101 * 102 * @param userId the id of the new foreground user 103 */ setNewUser(int userId)104 public void setNewUser(int userId) { 105 mCurrentUserId = userId; 106 } 107 108 // RO methods: getForAllUsers(int netid)109 public WifiConfiguration getForAllUsers(int netid) { 110 return mPerID.get(netid); 111 } 112 getForCurrentUser(int netid)113 public WifiConfiguration getForCurrentUser(int netid) { 114 return mPerIDForCurrentUser.get(netid); 115 } 116 sizeForAllUsers()117 public int sizeForAllUsers() { 118 return mPerID.size(); 119 } 120 sizeForCurrentUser()121 public int sizeForCurrentUser() { 122 return mPerIDForCurrentUser.size(); 123 } 124 getByConfigKeyForCurrentUser(String key)125 public WifiConfiguration getByConfigKeyForCurrentUser(String key) { 126 if (key == null) { 127 return null; 128 } 129 for (WifiConfiguration config : mPerIDForCurrentUser.values()) { 130 if (config.getProfileKey().equals(key)) { 131 return config; 132 } 133 } 134 return null; 135 } 136 137 /** 138 * Retrieves the |WifiConfiguration| object matching the provided |scanResult| from the internal 139 * map. 140 * Essentially checks if network config and scan result have the same SSID and encryption type. 141 */ getByScanResultForCurrentUser(ScanResult scanResult)142 public WifiConfiguration getByScanResultForCurrentUser(ScanResult scanResult) { 143 return mScanResultMatchInfoMapForCurrentUser.get( 144 ScanResultMatchInfo.fromScanResult(scanResult)); 145 } 146 valuesForAllUsers()147 public Collection<WifiConfiguration> valuesForAllUsers() { 148 return mPerID.values(); 149 } 150 valuesForCurrentUser()151 public Collection<WifiConfiguration> valuesForCurrentUser() { 152 return mPerIDForCurrentUser.values(); 153 } 154 } 155