• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) {
66                 mScanResultMatchInfoMapForCurrentUser.put(
67                         ScanResultMatchInfo.fromWifiConfiguration(config), config);
68             }
69         }
70         return current;
71     }
72 
remove(int netID)73     public WifiConfiguration remove(int netID) {
74         WifiConfiguration config = mPerID.remove(netID);
75         if (config == null) {
76             return null;
77         }
78 
79         mPerIDForCurrentUser.remove(netID);
80 
81         Iterator<Map.Entry<ScanResultMatchInfo, WifiConfiguration>> scanResultMatchInfoEntries =
82                 mScanResultMatchInfoMapForCurrentUser.entrySet().iterator();
83         while (scanResultMatchInfoEntries.hasNext()) {
84             if (scanResultMatchInfoEntries.next().getValue().networkId == netID) {
85                 scanResultMatchInfoEntries.remove();
86                 break;
87             }
88         }
89         return config;
90     }
91 
clear()92     public void clear() {
93         mPerID.clear();
94         mPerIDForCurrentUser.clear();
95         mScanResultMatchInfoMapForCurrentUser.clear();
96     }
97 
98     /**
99      * Sets the new foreground user ID.
100      *
101      * @param userId the id of the new foreground user
102      */
setNewUser(int userId)103     public void setNewUser(int userId) {
104         mCurrentUserId = userId;
105     }
106 
107     // RO methods:
getForAllUsers(int netid)108     public WifiConfiguration getForAllUsers(int netid) {
109         return mPerID.get(netid);
110     }
111 
getForCurrentUser(int netid)112     public WifiConfiguration getForCurrentUser(int netid) {
113         return mPerIDForCurrentUser.get(netid);
114     }
115 
sizeForAllUsers()116     public int sizeForAllUsers() {
117         return mPerID.size();
118     }
119 
sizeForCurrentUser()120     public int sizeForCurrentUser() {
121         return mPerIDForCurrentUser.size();
122     }
123 
getByConfigKeyForCurrentUser(String key)124     public WifiConfiguration getByConfigKeyForCurrentUser(String key) {
125         if (key == null) {
126             return null;
127         }
128         for (WifiConfiguration config : mPerIDForCurrentUser.values()) {
129             if (config.getKey().equals(key)) {
130                 return config;
131             }
132         }
133         return null;
134     }
135 
136     /**
137      * Retrieves the |WifiConfiguration| object matching the provided |scanResult| from the internal
138      * map.
139      * Essentially checks if network config and scan result have the same SSID and encryption type.
140      */
getByScanResultForCurrentUser(ScanResult scanResult)141     public WifiConfiguration getByScanResultForCurrentUser(ScanResult scanResult) {
142         return mScanResultMatchInfoMapForCurrentUser.get(
143                 ScanResultMatchInfo.fromScanResult(scanResult));
144     }
145 
valuesForAllUsers()146     public Collection<WifiConfiguration> valuesForAllUsers() {
147         return mPerID.values();
148     }
149 
valuesForCurrentUser()150     public Collection<WifiConfiguration> valuesForCurrentUser() {
151         return mPerIDForCurrentUser.values();
152     }
153 }
154