summaryrefslogtreecommitdiff
path: root/src/com/android/networkrecommendation/scoring/util/NetworkUtil.java
blob: b27bbe6668fcb9cc6be275529271887d0c56e2d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * 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().
     *
     * <p>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.
     *
     * <p>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.
     *
     * <p>This method does not account for non-UTF-8 SSIDs, which are returned as a string of hex
     * digits from getSSID().
     *
     * <p>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<NetworkKey, Boolean> getOpenWifiNetworkKeys(Context context) {
        WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        List<ScanResult> scanResults = null;
        if (Util.isScorerActive(context)) {
            try {
                scanResults = wifiMgr.getScanResults();
            } catch (SecurityException e) {
                Blog.w(Constants.TAG, e, "No permission to get scan results");
                scanResults = null;
            }
        }
        if (scanResults == null) {
            return Collections.emptyMap();
        }
        Map<NetworkKey, Boolean> openKeys = new HashMap<>();
        for (int i = 0; i < scanResults.size(); i++) {
            ScanResult scanResult = scanResults.get(i);
            NetworkKey networkKey = ScanResultUtil.createNetworkKey(scanResult);
            if (networkKey != null) {
                openKeys.put(networkKey, isScanResultForOpenNetwork(scanResult));
            }
        }
        return openKeys;
    }

    /** Returns true if the given config is for an "open" network. */
    public static boolean isOpenNetwork(@NonNull WifiConfiguration config) {
        if (config.allowedKeyManagement.get(WPA_PSK) // covers WPA_PSK and WPA2_PSK
                || config.allowedKeyManagement.get(WPA_EAP)
                || config.allowedKeyManagement.get(IEEE8021X)
                || (config.wepKeys != null && config.wepKeys[0] != null)) {
            return false;
        }
        return true;
    }

    @NonNull
    public static String getCurrentWifiSsid(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        return wifiInfo == null ? "" : wifiInfo.getSSID();
    }

    /** Returns the config for the given SSID, or null if one cannot be found. */
    @Nullable
    public static WifiConfiguration getConfig(Context context, String ssid) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();
        if (configs == null) {
            return null;
        }

        WifiConfiguration config;
        for (int i = 0; i < configs.size(); i++) {
            config = configs.get(i);
            if (TextUtils.equals(ssid, config.SSID)) {
                return config;
            }
        }
        return null;
    }
}