/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.networkrecommendation.scoring.util; import static android.net.wifi.WifiConfiguration.KeyMgmt.IEEE8021X; import static android.net.wifi.WifiConfiguration.KeyMgmt.WPA_EAP; import static android.net.wifi.WifiConfiguration.KeyMgmt.WPA_PSK; import static com.android.networkrecommendation.util.ScanResultUtil.isScanResultForOpenNetwork; import android.content.Context; import android.net.NetworkKey; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.text.TextUtils; import com.android.networkrecommendation.Constants; import com.android.networkrecommendation.util.Blog; import com.android.networkrecommendation.util.ScanResultUtil; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; /** Network Utils. */ public final class NetworkUtil { private NetworkUtil() { // do not instantiate } /** * Canonicalize the given SSID returned by WifiInfo#getSSID(). * *
This method should only be called once on a given SSID! If an SSID contains outer quotes, * we will strip them twice and change the SSID to a different one. * *
The SSID should be returned surrounded by double quotation marks if it is valid UTF-8. * This behavior was only implemented correctly after * https://googleplex-android-review.googlesource.com/#/c/224602/ which went into JB-MR1. * *
This method does not account for non-UTF-8 SSIDs, which are returned as a string of hex * digits from getSSID(). * *
For more details, see: http://stackoverflow.com/questions/13563032
*/
public static String canonicalizeSsid(String ssid) {
if (ssid == null) {
return null;
}
return removeQuotesIfNeeded(ssid);
}
/** Remove the leading quote and trailing quote. */
private static String removeQuotesIfNeeded(String text) {
if (text.length() > 1 && text.startsWith("\"") && text.endsWith("\"")) {
return text.substring(1, text.length() - 1);
}
return text;
}
/**
* @return a map from NetworkKey to true if that network is open, and false otherwise, for all
* visible networks in the last set of Wi-Fi scan results.
*/
public static Map