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