diff options
Diffstat (limited to 'service/java/com/android/server')
3 files changed, 56 insertions, 10 deletions
diff --git a/service/java/com/android/server/wifi/NetworkSuggestionEvaluator.java b/service/java/com/android/server/wifi/NetworkSuggestionEvaluator.java index c68c8f890..f248e08be 100644 --- a/service/java/com/android/server/wifi/NetworkSuggestionEvaluator.java +++ b/service/java/com/android/server/wifi/NetworkSuggestionEvaluator.java @@ -72,7 +72,8 @@ public class NetworkSuggestionEvaluator implements WifiNetworkSelector.NetworkEv WifiConfiguration currentNetwork, String currentBssid, boolean connected, boolean untrustedNetworkAllowed, @NonNull OnConnectableListener onConnectableListener) { - Map<WifiNetworkSuggestion, ScanResult> candidateNetworkSuggestions = new HashMap<>(); + Map<WifiNetworkSuggestion, ScanResult> candidateNetworkSuggestionToScanResultMap = + new HashMap<>(); for (int i = 0; i < scanDetails.size(); i++) { ScanDetail scanDetail = scanDetails.get(i); ScanResult scanResult = scanDetail.getScanResult(); @@ -89,24 +90,41 @@ public class NetworkSuggestionEvaluator implements WifiNetworkSelector.NetworkEv // All matching network credentials are considered equal. So, put any one of them. WifiNetworkSuggestion matchingNetworkSuggestion = matchingNetworkSuggestions.stream().findAny().get(); - candidateNetworkSuggestions.put(matchingNetworkSuggestion, scanResult); + candidateNetworkSuggestionToScanResultMap.put(matchingNetworkSuggestion, scanResult); onConnectableListener.onConnectable( scanDetail, matchingNetworkSuggestion.wifiConfiguration, 0); } // Pick the matching network suggestion corresponding to the highest RSSI. This will need to // be replaced by a more sophisticated algorithm. - Map.Entry<WifiNetworkSuggestion, ScanResult> candidateNetworkSuggestion = - candidateNetworkSuggestions + Map.Entry<WifiNetworkSuggestion, ScanResult> candidateNetworkSuggestionToScanResult = + candidateNetworkSuggestionToScanResultMap .entrySet() .stream() .max(Comparator.comparing(e -> e.getValue().level)) .orElse(null); - if (candidateNetworkSuggestion == null) { + if (candidateNetworkSuggestionToScanResult == null) { mLocalLog.log("did not see any matching network suggestions."); return null; } - return addCandidateToWifiConfigManager( - candidateNetworkSuggestion.getKey().wifiConfiguration); + WifiNetworkSuggestion candidateNetworkSuggestion = + candidateNetworkSuggestionToScanResult.getKey(); + // Check if we already have a saved network with the same credentials. + // Note: This would not happen in the current architecture of network evaluators because + // saved network evaluator would run first and find the same candidate & not run any of the + // other evaluators. But this architecture could change in the future and we might end + // up running through all the evaluators to find all suitable candidates. + WifiConfiguration existingSavedNetwork = + mWifiConfigManager.getConfiguredNetwork( + candidateNetworkSuggestion.wifiConfiguration.configKey()); + if (existingSavedNetwork != null) { + mLocalLog.log(String.format("network suggestion candidate %s network ID:%d", + WifiNetworkSelector.toScanId(existingSavedNetwork + .getNetworkSelectionStatus() + .getCandidate()), + existingSavedNetwork.networkId)); + return existingSavedNetwork; + } + return addCandidateToWifiConfigManager(candidateNetworkSuggestion.wifiConfiguration); } // Add and enable this network to the central database (i.e WifiConfigManager). diff --git a/service/java/com/android/server/wifi/WifiInjector.java b/service/java/com/android/server/wifi/WifiInjector.java index 5ae41a040..b4275f190 100644 --- a/service/java/com/android/server/wifi/WifiInjector.java +++ b/service/java/com/android/server/wifi/WifiInjector.java @@ -565,7 +565,8 @@ public class WifiInjector { mWifiCoreHandlerThread.getLooper(), mContext, nc, (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE), (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE), - mClock, this, wifiConnectivityManager, mWifiPermissionsUtil); + mClock, this, wifiConnectivityManager, mWifiConfigManager, + mWifiPermissionsUtil); } /** diff --git a/service/java/com/android/server/wifi/WifiNetworkFactory.java b/service/java/com/android/server/wifi/WifiNetworkFactory.java index 04f63c489..ba3407b5f 100644 --- a/service/java/com/android/server/wifi/WifiNetworkFactory.java +++ b/service/java/com/android/server/wifi/WifiNetworkFactory.java @@ -84,6 +84,7 @@ public class WifiNetworkFactory extends NetworkFactory { private final Handler mHandler; private final WifiInjector mWifiInjector; private final WifiConnectivityManager mWifiConnectivityManager; + private final WifiConfigManager mWifiConfigManager; private final WifiPermissionsUtil mWifiPermissionsUtil; private final WifiScanner.ScanSettings mScanSettings; private final NetworkFactoryScanListener mScanListener; @@ -236,6 +237,7 @@ public class WifiNetworkFactory extends NetworkFactory { ActivityManager activityManager, AlarmManager alarmManager, Clock clock, WifiInjector wifiInjector, WifiConnectivityManager connectivityManager, + WifiConfigManager configManager, WifiPermissionsUtil wifiPermissionsUtil) { super(looper, context, TAG, nc); mContext = context; @@ -245,6 +247,7 @@ public class WifiNetworkFactory extends NetworkFactory { mHandler = new Handler(looper); mWifiInjector = wifiInjector; mWifiConnectivityManager = connectivityManager; + mWifiConfigManager = configManager; mWifiPermissionsUtil = wifiPermissionsUtil; // Create the scan settings. mScanSettings = new WifiScanner.ScanSettings(); @@ -496,17 +499,41 @@ public class WifiNetworkFactory extends NetworkFactory { : Process.INVALID_UID; } + // Helper method to add the provided network configuration to WifiConfigManager, if it does not + // already exist & return the allocated network ID. This ID will be used in the CONNECT_NETWORK + // request to ClientModeImpl. + // If the network already exists, just return the network ID of the existing network. + private int addNetworkToWifiConfigManager(@NonNull WifiConfiguration network) { + WifiConfiguration existingSavedNetwork = + mWifiConfigManager.getConfiguredNetwork(network.configKey()); + if (existingSavedNetwork != null) { + return existingSavedNetwork.networkId; + } + NetworkUpdateResult networkUpdateResult = + mWifiConfigManager.addOrUpdateNetwork(network, Process.WIFI_UID); + if (mVerboseLoggingEnabled) { + Log.v(TAG, "Added network to config manager " + networkUpdateResult.netId); + } + return networkUpdateResult.netId; + } + // Helper method to trigger a connection request & schedule a timeout alarm to track the // connection request. private void connectToNetwork(@NonNull WifiConfiguration network) { // Cancel connection timeout alarm for any previous connection attempts. cancelConnectionTimeout(); + // First add the network to WifiConfigManager and then use the obtained networkId + // in the CONNECT_NETWORK request. + // Note: We don't do any error checks on the networkId because ClientModeImpl will do the + // necessary checks when processing CONNECT_NETWORK. + int networkId = addNetworkToWifiConfigManager(network); + // Send the connect request to ClientModeImpl. + // TODO(b/117601161): Refactor this. Message msg = Message.obtain(); msg.what = WifiManager.CONNECT_NETWORK; - msg.arg1 = WifiConfiguration.INVALID_NETWORK_ID; - msg.obj = network; + msg.arg1 = networkId; msg.replyTo = mSrcMessenger; mWifiInjector.getClientModeImpl().sendMessage(msg); |