• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.networkrecommendation.util;
18 
19 import android.net.wifi.WifiConfiguration;
20 
21 /**
22  * WifiConfiguration utility for any {@link WifiConfiguration} related operations..
23  * TODO(b/34125341): Delete this class once exposed as a SystemApi
24  */
25 public class WifiConfigurationUtil {
26     /**
27      * Checks if the provided |wepKeys| array contains any non-null value;
28      */
hasAnyValidWepKey(String[] wepKeys)29     public static boolean hasAnyValidWepKey(String[] wepKeys) {
30         for (int i = 0; i < wepKeys.length; i++) {
31             if (wepKeys[i] != null) {
32                 return true;
33             }
34         }
35         return false;
36     }
37 
38     /**
39      * Helper method to check if the provided |config| corresponds to a PSK network or not.
40      */
isConfigForPskNetwork(WifiConfiguration config)41     public static boolean isConfigForPskNetwork(WifiConfiguration config) {
42         return config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK);
43     }
44 
45     /**
46      * Helper method to check if the provided |config| corresponds to a EAP network or not.
47      */
isConfigForEapNetwork(WifiConfiguration config)48     public static boolean isConfigForEapNetwork(WifiConfiguration config) {
49         return (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)
50                 || config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X));
51     }
52 
53     /**
54      * Helper method to check if the provided |config| corresponds to a WEP network or not.
55      */
isConfigForWepNetwork(WifiConfiguration config)56     public static boolean isConfigForWepNetwork(WifiConfiguration config) {
57         return (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.NONE)
58                 && hasAnyValidWepKey(config.wepKeys));
59     }
60 
61     /**
62      * Helper method to check if the provided |config| corresponds to an open network or not.
63      */
isConfigForOpenNetwork(WifiConfiguration config)64     public static boolean isConfigForOpenNetwork(WifiConfiguration config) {
65         return !(isConfigForWepNetwork(config) || isConfigForPskNetwork(config)
66                 || isConfigForEapNetwork(config));
67     }
68 
69     /** @return a ssid that can be shown to the user. */
getPrintableSsid(WifiConfiguration config)70     public static String getPrintableSsid(WifiConfiguration config) {
71         if (config.SSID == null) return "";
72         final int length = config.SSID.length();
73         if (length > 2 && (config.SSID.charAt(0) == '"') && config.SSID.charAt(length - 1) == '"') {
74             return config.SSID.substring(1, length - 1);
75         }
76         return config.SSID;
77     }
78 
79     /** Removes " from the ssid in a wifi configuration (to match against a ScanResult). */
removeDoubleQuotes(WifiConfiguration config)80     public static String removeDoubleQuotes(WifiConfiguration config) {
81         if (config.SSID == null) return null;
82         final int length = config.SSID.length();
83         if ((length > 1) && (config.SSID.charAt(0) == '"')
84                 && (config.SSID.charAt(length - 1) == '"')) {
85             return config.SSID.substring(1, length - 1);
86         }
87         return config.SSID;
88     }
89 }
90