1155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/*
2155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Copyright (C) 2010 The Android Open Source Project
3155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
4155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Licensed under the Apache License, Version 2.0 (the "License");
5155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * you may not use this file except in compliance with the License.
6155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * You may obtain a copy of the License at
7155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
8155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *      http://www.apache.org/licenses/LICENSE-2.0
9155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
10155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * Unless required by applicable law or agreed to in writing, software
11155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * distributed under the License is distributed on an "AS IS" BASIS,
12155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * See the License for the specific language governing permissions and
14155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * limitations under the License.
15155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
16155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
17155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandepackage com.android.server.wifi;
18155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
19155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.Context;
20155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.content.Intent;
213ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.net.IpConfiguration;
223ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.net.IpConfiguration.IpAssignment;
233ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.net.IpConfiguration.ProxySettings;
24155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.LinkAddress;
25b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kimimport android.net.NetworkInfo.DetailedState;
26d4d0c9ad1e3289702ec341521b9bd01b52e9c1d8Jason Monkimport android.net.ProxyInfo;
27155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.RouteInfo;
283b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colittiimport android.net.StaticIpConfiguration;
29155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WifiConfiguration;
30155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WifiConfiguration.KeyMgmt;
31155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WifiConfiguration.Status;
32155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport static android.net.wifi.WifiConfiguration.INVALID_NETWORK_ID;
33155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
34155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WifiEnterpriseConfig;
35155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WifiManager;
36155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WifiSsid;
37155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WpsInfo;
38155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.net.wifi.WpsResult;
39f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.net.wifi.ScanResult;
4027355a942653264388e909a4276196ee63e57811vandwalleimport android.net.wifi.WifiInfo;
41f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
42155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.Environment;
43155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.FileObserver;
44155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.Process;
45f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.os.SystemClock;
46155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.os.UserHandle;
47c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalleimport android.provider.Settings;
48155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.security.Credentials;
49155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.security.KeyChain;
50155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.security.KeyStore;
51155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.text.TextUtils;
52155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.LocalLog;
53155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport android.util.Log;
543ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport android.util.SparseArray;
553ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim
563ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport com.android.server.net.DelayedDiskWrite;
573ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport com.android.server.net.IpConfigStore;
58b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalleimport com.android.internal.R;
59155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
60155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.BufferedReader;
613ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimimport java.io.BufferedInputStream;
62b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kimimport java.io.DataInputStream;
63b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kimimport java.io.DataOutputStream;
64b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kimimport java.io.EOFException;
65155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.File;
66155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.FileDescriptor;
67b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kimimport java.io.FileInputStream;
68155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.FileNotFoundException;
69155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.FileReader;
70155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.IOException;
71155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.io.PrintWriter;
723137b27bbbaaf87267dea3bf95d1c027931b0b6aNavtej Singh Mannimport java.math.BigInteger;
73155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.net.InetAddress;
743137b27bbbaaf87267dea3bf95d1c027931b0b6aNavtej Singh Mannimport java.nio.charset.Charset;
75155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.security.PrivateKey;
76155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.security.cert.Certificate;
77155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpandeimport java.security.cert.CertificateException;
78f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.text.SimpleDateFormat;
79f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.text.DateFormat;
802451dbcc4f9641df188326215b204b798eb70c46vandwalleimport java.util.regex.Matcher;
812451dbcc4f9641df188326215b204b798eb70c46vandwalleimport java.util.regex.Pattern;
824dc6f3a322806b25d50039614cde1b94fe91ab17vandwalleimport java.util.*;
83155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
84155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande/**
85155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * This class provides the API to manage configured
86155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * wifi networks. The API is not thread safe is being
87155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * used only from WifiStateMachine.
88155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
89155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * It deals with the following
90155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * - Add/update/remove a WifiConfiguration
91155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *   The configuration contains two types of information.
92155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *     = IP and proxy configuration that is handled by WifiConfigStore and
93155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       is saved to disk on any change.
94155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
95155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       The format of configuration file is as follows:
96155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       <version>
97155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       <netA_key1><netA_value1><netA_key2><netA_value2>...<EOS>
98155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       <netB_key1><netB_value1><netB_key2><netB_value2>...<EOS>
99155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       ..
100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       (key, value) pairs for a given network are grouped together and can
102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       be in any order. A EOS at the end of a set of (key, value) pairs
103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       indicates that the next set of (key, value) pairs are for a new
104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       network. A network is identified by a unique ID_KEY. If there is no
105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       ID_KEY in the (key, value) pairs, the data is discarded.
106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       An invalid version on read would result in discarding the contents of
108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       the file. On the next write, the latest version is written to file.
109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       Any failures during read or write to the configuration file are ignored
111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       without reporting to the user since the likelihood of these errors are
112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       low and the impact on connectivity is low.
113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *     = SSID & security details that is pushed to the supplicant.
115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       supplicant saves these details to the disk on calling
116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       saveConfigCommand().
117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *       We have two kinds of APIs exposed:
119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *        > public API calls that provide fine grained control
120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *          - enableNetwork, disableNetwork, addOrUpdateNetwork(),
121155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *          removeNetwork(). For these calls, the config is not persisted
122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *          to the disk. (TODO: deprecate these calls in WifiManager)
123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *        > The new API calls - selectNetwork(), saveNetwork() & forgetNetwork().
124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *          These calls persist the supplicant config to disk.
125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande * - Maintain a list of configured networks for quick access
127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande *
128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande */
1293ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kimpublic class WifiConfigStore extends IpConfigStore {
130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private Context mContext;
132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final String TAG = "WifiConfigStore";
133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final boolean DBG = true;
134ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    private static boolean VDBG = false;
1359f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    private static boolean VVDBG = false;
136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final String SUPPLICANT_CONFIG_FILE = "/data/misc/wifi/wpa_supplicant.conf";
138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* configured networks with network id as the key */
140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private HashMap<Integer, WifiConfiguration> mConfiguredNetworks =
141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            new HashMap<Integer, WifiConfiguration>();
142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* A network id is a unique identifier for a network configured in the
144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * supplicant. Network ids are generated when the supplicant reads
145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * the configuration file at start and can thus change for networks.
146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * We store the IP configuration for networks along with a unique id
147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * that is generated from SSID and security type of the network. A mapping
148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * from the generated unique id to network id of the network is needed to
149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * map supplicant config to IP configuration. */
150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private HashMap<Integer, Integer> mNetworkIds =
151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            new HashMap<Integer, Integer>();
152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Tracks the highest priority of configured networks */
154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int mLastPriority = -1;
155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final String ipConfigFile = Environment.getDataDirectory() +
157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            "/misc/wifi/ipconfig.txt";
158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
159f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String networkHistoryConfigFile = Environment.getDataDirectory() +
160f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            "/misc/wifi/networkHistory.txt";
161f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1628c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String autoJoinConfigFile = Environment.getDataDirectory() +
1638c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            "/misc/wifi/autojoinconfig.txt";
1648c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
165f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    /* Network History Keys */
166f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String SSID_KEY = "SSID:  ";
167f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String CONFIG_KEY = "CONFIG:  ";
168f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String CHOICE_KEY = "CHOICE:  ";
169f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String LINK_KEY = "LINK:  ";
170f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String BSSID_KEY = "BSSID:  ";
171f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String BSSID_KEY_END = "/BSSID:  ";
172f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String RSSI_KEY = "RSSI:  ";
173f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String FREQ_KEY = "FREQ:  ";
174f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String DATE_KEY = "DATE:  ";
175f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String MILLI_KEY = "MILLI:  ";
17627355a942653264388e909a4276196ee63e57811vandwalle    private static final String BLACKLIST_MILLI_KEY = "BLACKLIST_MILLI:  ";
177f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String NETWORK_ID_KEY = "ID:  ";
178f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String PRIORITY_KEY = "PRIORITY:  ";
179f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String DEFAULT_GW_KEY = "DEFAULT_GW:  ";
180f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String AUTH_KEY = "AUTH:  ";
181f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String SEPARATOR_KEY = "\n";
182f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String STATUS_KEY = "AUTO_JOIN_STATUS:  ";
18340ff222cec1bd05879edb53abc75c6deead734cavandwalle    private static final String BSSID_STATUS_KEY = "BSSID_STATUS:  ";
184453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle    private static final String SELF_ADDED_KEY = "SELF_ADDED:  ";
1854bcdcfd19e3eaaacc54d8ec65149c6d7faa181afvandwalle    private static final String FAILURE_KEY = "FAILURE:  ";
186992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle    private static final String DID_SELF_ADD_KEY = "DID_SELF_ADD:  ";
1870e3c67280365eb82fc15362a3b98a2c978bde766vandwalle    private static final String PEER_CONFIGURATION_KEY = "PEER_CONFIGURATION:  ";
188992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle    private static final String CREATOR_UID_KEY = "CREATOR_UID_KEY:  ";
189992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle    private static final String CONNECT_UID_KEY = "CONNECT_UID_KEY:  ";
190992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle    private static final String UPDATE_UID_KEY = "UPDATE_UID:  ";
191ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    private static final String SUPPLICANT_STATUS_KEY = "SUP_STATUS:  ";
192ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle    private static final String SUPPLICANT_DISABLE_REASON_KEY = "SUP_DIS_REASON:  ";
1932451dbcc4f9641df188326215b204b798eb70c46vandwalle    private static final String FQDN_KEY = "FQDN:  ";
1942451dbcc4f9641df188326215b204b798eb70c46vandwalle    private static final String NUM_CONNECTION_FAILURES_KEY = "CONNECT_FAILURES:  ";
195e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle    private static final String NUM_IP_CONFIG_FAILURES_KEY = "IP_CONFIG_FAILURES:  ";
196e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle    private static final String NUM_AUTH_FAILURES_KEY = "AUTH_FAILURES:  ";
1972451dbcc4f9641df188326215b204b798eb70c46vandwalle    private static final String SCORER_OVERRIDE_KEY = "SCORER_OVERRIDE:  ";
1982451dbcc4f9641df188326215b204b798eb70c46vandwalle    private static final String SCORER_OVERRIDE_AND_SWITCH_KEY = "SCORER_OVERRIDE_AND_SWITCH:  ";
1999f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    private static final String NO_INTERNET_ACCESS_KEY = "NO_INTERNET_ACCESS:  ";
200b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle    private static final String EPHEMERAL_KEY = "EPHEMERAL:   ";
201c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    private static final String NUM_ASSOCIATION_KEY = "NUM_ASSOCIATION:  ";
202dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle    private static final String JOIN_ATTEMPT_BOOST_KEY = "JOIN_ATTEMPT_BOOST:  ";
2038c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String THRESHOLD_INITIAL_AUTO_JOIN_ATTEMPT_RSSI_MIN_5G_KEY
2048c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = "THRESHOLD_INITIAL_AUTO_JOIN_ATTEMPT_RSSI_MIN_5G:  ";
2058c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String THRESHOLD_INITIAL_AUTO_JOIN_ATTEMPT_RSSI_MIN_24G_KEY
2068c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = "THRESHOLD_INITIAL_AUTO_JOIN_ATTEMPT_RSSI_MIN_24G:  ";
2078c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String THRESHOLD_UNBLACKLIST_HARD_5G_KEY
2088c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = "THRESHOLD_UNBLACKLIST_HARD_5G:  ";
2098c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String THRESHOLD_UNBLACKLIST_SOFT_5G_KEY
2108c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = "THRESHOLD_UNBLACKLIST_SOFT_5G:  ";
2118c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String THRESHOLD_UNBLACKLIST_HARD_24G_KEY
2128c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = "THRESHOLD_UNBLACKLIST_HARD_24G:  ";
2138c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String THRESHOLD_UNBLACKLIST_SOFT_24G_KEY
2148c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = "THRESHOLD_UNBLACKLIST_SOFT_24G:  ";
2158c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String THRESHOLD_GOOD_RSSI_5_KEY
2168c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = "THRESHOLD_GOOD_RSSI_5:  ";
2178c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String THRESHOLD_LOW_RSSI_5_KEY
2188c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = "THRESHOLD_LOW_RSSI_5:  ";
2198c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String THRESHOLD_BAD_RSSI_5_KEY
2208c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = "THRESHOLD_BAD_RSSI_5:  ";
2218c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String THRESHOLD_GOOD_RSSI_24_KEY
2228c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = "THRESHOLD_GOOD_RSSI_24:  ";
2238c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String THRESHOLD_LOW_RSSI_24_KEY
2248c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = "THRESHOLD_LOW_RSSI_24:  ";
2258c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String THRESHOLD_BAD_RSSI_24_KEY
2268c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = "THRESHOLD_BAD_RSSI_24:  ";
2279f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
2288c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String THRESHOLD_MAX_TX_PACKETS_FOR_NETWORK_SWITCHING_KEY
2298c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = "THRESHOLD_MAX_TX_PACKETS_FOR_NETWORK_SWITCHING:   ";
2308c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String THRESHOLD_MAX_RX_PACKETS_FOR_NETWORK_SWITCHING_KEY
2318c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = "THRESHOLD_MAX_RX_PACKETS_FOR_NETWORK_SWITCHING:   ";
2328c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
2339f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    private static final String THRESHOLD_MAX_TX_PACKETS_FOR_FULL_SCANS_KEY
2349f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            = "THRESHOLD_MAX_TX_PACKETS_FOR_FULL_SCANS:   ";
2359f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    private static final String THRESHOLD_MAX_RX_PACKETS_FOR_FULL_SCANS_KEY
2369f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            = "THRESHOLD_MAX_RX_PACKETS_FOR_FULL_SCANS:   ";
2379f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
2389f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    private static final String THRESHOLD_MAX_TX_PACKETS_FOR_PARTIAL_SCANS_KEY
2399f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            = "THRESHOLD_MAX_TX_PACKETS_FOR_PARTIAL_SCANS:   ";
2409f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    private static final String THRESHOLD_MAX_RX_PACKETS_FOR_PARTIAL_SCANS_KEY
2419f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            = "THRESHOLD_MAX_RX_PACKETS_FOR_PARTIAL_SCANS:   ";
2429f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
2439f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    private static final String MAX_NUM_ACTIVE_CHANNELS_FOR_PARTIAL_SCANS_KEY
2449f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            = "MAX_NUM_ACTIVE_CHANNELS_FOR_PARTIAL_SCANS:   ";
2459f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    private static final String MAX_NUM_PASSIVE_CHANNELS_FOR_PARTIAL_SCANS_KEY
2469f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            = "MAX_NUM_PASSIVE_CHANNELS_FOR_PARTIAL_SCANS:   ";
2479f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
2488c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String A_BAND_PREFERENCE_RSSI_THRESHOLD_LOW_KEY =
2498c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            "A_BAND_PREFERENCE_RSSI_THRESHOLD_LOW:   ";
2508c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String A_BAND_PREFERENCE_RSSI_THRESHOLD_KEY =
2518c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            "A_BAND_PREFERENCE_RSSI_THRESHOLD:   ";
2528c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String G_BAND_PREFERENCE_RSSI_THRESHOLD_KEY =
2538c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            "G_BAND_PREFERENCE_RSSI_THRESHOLD:   ";
2548c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
2558c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private static final String ENABLE_AUTOJOIN_WHILE_ASSOCIATED_KEY
2568c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = "ENABLE_AUTOJOIN_WHILE_ASSOCIATED:   ";
2578c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
258e67ec726c07410073575473c0f50dc737629f5davandwalle    private static final String ASSOCIATED_PARTIAL_SCAN_PERIOD_KEY
259e67ec726c07410073575473c0f50dc737629f5davandwalle            = "ASSOCIATED_PARTIAL_SCAN_PERIOD:   ";
260e67ec726c07410073575473c0f50dc737629f5davandwalle    private static final String ASSOCIATED_FULL_SCAN_BACKOFF_KEY
261e67ec726c07410073575473c0f50dc737629f5davandwalle            = "ASSOCIATED_FULL_SCAN_BACKOFF_PERIOD:   ";
2629f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    private static final String ALWAYS_ENABLE_SCAN_WHILE_ASSOCIATED_KEY
2639f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            = "ALWAYS_ENABLE_SCAN_WHILE_ASSOCIATED:   ";
264448c9536a302c58a79e271b1721c08b8882f800evandwalle    private static final String ONLY_LINK_SAME_CREDENTIAL_CONFIGURATIONS_KEY
265448c9536a302c58a79e271b1721c08b8882f800evandwalle            = "ONLY_LINK_SAME_CREDENTIAL_CONFIGURATIONS:   ";
26670468b47454c8657e8963932f2e08a3f4d7e3881vandwalle
267e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle    private static final String ENABLE_FULL_BAND_SCAN_WHEN_ASSOCIATED_KEY
268e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle            = "ENABLE_FULL_BAND_SCAN_WHEN_ASSOCIATED:   ";
269e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle
27070468b47454c8657e8963932f2e08a3f4d7e3881vandwalle    // The three below configurations are mainly for power stats and CPU usage tracking
27170468b47454c8657e8963932f2e08a3f4d7e3881vandwalle    // allowing to incrementally disable framework features
27270468b47454c8657e8963932f2e08a3f4d7e3881vandwalle    private static final String ENABLE_AUTO_JOIN_SCAN_WHILE_ASSOCIATED_KEY
27370468b47454c8657e8963932f2e08a3f4d7e3881vandwalle            = "ENABLE_AUTO_JOIN_SCAN_WHILE_ASSOCIATED:   ";
27470468b47454c8657e8963932f2e08a3f4d7e3881vandwalle    private static final String ENABLE_AUTO_JOIN_WHILE_ASSOCIATED_KEY
27570468b47454c8657e8963932f2e08a3f4d7e3881vandwalle            = "ENABLE_AUTO_JOIN_WHILE_ASSOCIATED:   ";
27670468b47454c8657e8963932f2e08a3f4d7e3881vandwalle    private static final String ENABLE_CHIP_WAKE_UP_WHILE_ASSOCIATED_KEY
27770468b47454c8657e8963932f2e08a3f4d7e3881vandwalle            = "ENABLE_CHIP_WAKE_UP_WHILE_ASSOCIATED:   ";
27870468b47454c8657e8963932f2e08a3f4d7e3881vandwalle    private static final String ENABLE_RSSI_POLL_WHILE_ASSOCIATED_KEY
27970468b47454c8657e8963932f2e08a3f4d7e3881vandwalle            = "ENABLE_RSSI_POLL_WHILE_ASSOCIATED_KEY:   ";
28070468b47454c8657e8963932f2e08a3f4d7e3881vandwalle
281e67ec726c07410073575473c0f50dc737629f5davandwalle    // The Wifi verbose log is provided as a way to persist the verbose logging settings
282e67ec726c07410073575473c0f50dc737629f5davandwalle    // for testing purpose.
283e67ec726c07410073575473c0f50dc737629f5davandwalle    // It is not intended for normal use.
284e67ec726c07410073575473c0f50dc737629f5davandwalle    private static final String WIFI_VERBOSE_LOGS_KEY
285e67ec726c07410073575473c0f50dc737629f5davandwalle            = "WIFI_VERBOSE_LOGS:   ";
28670468b47454c8657e8963932f2e08a3f4d7e3881vandwalle
28744d0d289bc2a959a18af3870844d221c93301659vandwalle    // As we keep deleted PSK WifiConfiguration for a while, the PSK of
28844d0d289bc2a959a18af3870844d221c93301659vandwalle    // those deleted WifiConfiguration is set to this random unused PSK
28944d0d289bc2a959a18af3870844d221c93301659vandwalle    private static final String DELETED_CONFIG_PSK = "Mjkd86jEMGn79KhKll298Uu7-deleted";
29044d0d289bc2a959a18af3870844d221c93301659vandwalle
29170468b47454c8657e8963932f2e08a3f4d7e3881vandwalle    public boolean enableAutoJoinScanWhenAssociated = true;
29270468b47454c8657e8963932f2e08a3f4d7e3881vandwalle    public boolean enableAutoJoinWhenAssociated = true;
29370468b47454c8657e8963932f2e08a3f4d7e3881vandwalle    public boolean enableChipWakeUpWhenAssociated = true;
29470468b47454c8657e8963932f2e08a3f4d7e3881vandwalle    public boolean enableRssiPollWhenAssociated = true;
2958c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
2968c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    public int maxTxPacketForNetworkSwitching = 40;
2978c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    public int maxRxPacketForNetworkSwitching = 80;
2988c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
2999f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    public int maxTxPacketForFullScans = 8;
3009f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    public int maxRxPacketForFullScans = 16;
3019f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
3029f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    public int maxTxPacketForPartialScans = 40;
3039f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    public int maxRxPacketForPartialScans = 80;
3049f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
305e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle    public boolean enableFullBandScanWhenAssociated = true;
306e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle
3078c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    public int thresholdInitialAutoJoinAttemptMin5RSSI
3088c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = WifiConfiguration.INITIAL_AUTO_JOIN_ATTEMPT_MIN_5;
3098c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    public int thresholdInitialAutoJoinAttemptMin24RSSI
3108c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = WifiConfiguration.INITIAL_AUTO_JOIN_ATTEMPT_MIN_24;
3118c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
3128c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    public int thresholdBadRssi5 = WifiConfiguration.BAD_RSSI_5;
3138c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    public int thresholdLowRssi5 = WifiConfiguration.LOW_RSSI_5;
3148c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    public int thresholdGoodRssi5 = WifiConfiguration.GOOD_RSSI_5;
3158c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    public int thresholdBadRssi24 = WifiConfiguration.BAD_RSSI_24;
3168c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    public int thresholdLowRssi24 = WifiConfiguration.LOW_RSSI_24;
3178c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    public int thresholdGoodRssi24 = WifiConfiguration.GOOD_RSSI_24;
3188c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
319e67ec726c07410073575473c0f50dc737629f5davandwalle    public int associatedFullScanBackoff = 12; // Will be divided by 8 by WifiStateMachine
3200eebae7334d6129f7ca1344e4b20199794994358vandwalle    public int associatedFullScanMaxIntervalMilli = 300000;
321e67ec726c07410073575473c0f50dc737629f5davandwalle
3220eebae7334d6129f7ca1344e4b20199794994358vandwalle    public int associatedPartialScanPeriodMilli;
3230eebae7334d6129f7ca1344e4b20199794994358vandwalle
3240eebae7334d6129f7ca1344e4b20199794994358vandwalle    public int bandPreferenceBoostFactor5 = 5; // Boost by 5 dB per dB above threshold
3250eebae7334d6129f7ca1344e4b20199794994358vandwalle    public int bandPreferencePenaltyFactor5 = 2; // Penalize by 2 dB per dB below threshold
3260eebae7334d6129f7ca1344e4b20199794994358vandwalle    public int bandPreferencePenaltyThreshold5 = WifiConfiguration.G_BAND_PREFERENCE_RSSI_THRESHOLD;
3270eebae7334d6129f7ca1344e4b20199794994358vandwalle    public int bandPreferenceBoostThreshold5 = WifiConfiguration.A_BAND_PREFERENCE_RSSI_THRESHOLD;
3280eebae7334d6129f7ca1344e4b20199794994358vandwalle
32917e828848eebb4eb11d27ce32d02716334dd6011vandwalle    public int badLinkSpeed24 = 6;
33017e828848eebb4eb11d27ce32d02716334dd6011vandwalle    public int badLinkSpeed5 = 12;
33117e828848eebb4eb11d27ce32d02716334dd6011vandwalle    public int goodLinkSpeed24 = 24;
33217e828848eebb4eb11d27ce32d02716334dd6011vandwalle    public int goodLinkSpeed5 = 36;
33317e828848eebb4eb11d27ce32d02716334dd6011vandwalle
334e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle    public int maxAuthErrorsToBlacklist = 4;
335e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle    public int maxConnectionErrorsToBlacklist = 4;
336a5283c01ac413e378e2261050cac3a6d6e9cf385vandwalle    public int wifiConfigBlacklistMinTimeMilli = 1000 * 60 * 5;
337e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
3380eebae7334d6129f7ca1344e4b20199794994358vandwalle    // Boost RSSI values of associated networks
3390eebae7334d6129f7ca1344e4b20199794994358vandwalle    public int associatedHysteresisHigh = +14;
3400eebae7334d6129f7ca1344e4b20199794994358vandwalle    public int associatedHysteresisLow = +8;
3418c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
3428c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    public int thresholdUnblacklistThreshold5Hard
3438c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = WifiConfiguration.UNBLACKLIST_THRESHOLD_5_HARD;
3448c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    public int thresholdUnblacklistThreshold5Soft
3458c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = WifiConfiguration.UNBLACKLIST_THRESHOLD_5_SOFT;
3468c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    public int thresholdUnblacklistThreshold24Hard
3478c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = WifiConfiguration.UNBLACKLIST_THRESHOLD_24_HARD;
3488c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    public int thresholdUnblacklistThreshold24Soft
3498c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            = WifiConfiguration.UNBLACKLIST_THRESHOLD_24_SOFT;
350e67ec726c07410073575473c0f50dc737629f5davandwalle    public int enableVerboseLogging = 0;
3517b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle    boolean showNetworks = true; // TODO set this back to false, used for debugging 17516271
3528c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
3539f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    public int alwaysEnableScansWhileAssociated = 0;
3549f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
3559f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    public int maxNumActiveChannelsForPartialScans = 6;
3569f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    public int maxNumPassiveChannelsForPartialScans = 2;
3579f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
3589f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    public boolean roamOnAny = false;
359e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle    public boolean onlyLinkSameCredentialConfigurations = true;
360448c9536a302c58a79e271b1721c08b8882f800evandwalle
3610eebae7334d6129f7ca1344e4b20199794994358vandwalle    public boolean enableLinkDebouncing = true;
3620eebae7334d6129f7ca1344e4b20199794994358vandwalle    public boolean enable5GHzPreference = true;
36317e828848eebb4eb11d27ce32d02716334dd6011vandwalle    public boolean enableWifiCellularHandoverUserTriggeredAdjustment = true;
3649f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
3652451dbcc4f9641df188326215b204b798eb70c46vandwalle    /**
3662451dbcc4f9641df188326215b204b798eb70c46vandwalle     * Regex pattern for extracting a connect choice.
3672451dbcc4f9641df188326215b204b798eb70c46vandwalle     * Matches a strings like the following:
3682451dbcc4f9641df188326215b204b798eb70c46vandwalle     * <configKey>=([0:9]+)
3692451dbcc4f9641df188326215b204b798eb70c46vandwalle     */
3702451dbcc4f9641df188326215b204b798eb70c46vandwalle    private static Pattern mConnectChoice =
3712451dbcc4f9641df188326215b204b798eb70c46vandwalle            Pattern.compile("(.*)=([0-9]+)");
3722451dbcc4f9641df188326215b204b798eb70c46vandwalle
3732451dbcc4f9641df188326215b204b798eb70c46vandwalle
374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Enterprise configuration keys */
375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * In old configurations, the "private_key" field was used. However, newer
377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * configurations use the key_id field with the engine_id set to "keystore".
378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * If this field is found in the configuration, the migration code is
379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * triggered.
380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public static final String OLD_PRIVATE_KEY_NAME = "private_key";
382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
383931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
384931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     * This represents an empty value of an enterprise field.
385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * NULL is used at wpa_supplicant to indicate an empty value
386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static final String EMPTY_VALUE = "NULL";
388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
389931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    // Internal use only
390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static final String[] ENTERPRISE_CONFIG_SUPPLICANT_KEYS = new String[] {
391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            WifiEnterpriseConfig.EAP_KEY, WifiEnterpriseConfig.PHASE2_KEY,
392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            WifiEnterpriseConfig.IDENTITY_KEY, WifiEnterpriseConfig.ANON_IDENTITY_KEY,
393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            WifiEnterpriseConfig.PASSWORD_KEY, WifiEnterpriseConfig.CLIENT_CERT_KEY,
394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            WifiEnterpriseConfig.CA_CERT_KEY, WifiEnterpriseConfig.SUBJECT_MATCH_KEY,
395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            WifiEnterpriseConfig.ENGINE_KEY, WifiEnterpriseConfig.ENGINE_ID_KEY,
396155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY };
397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
398c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
399c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    /**
40044d0d289bc2a959a18af3870844d221c93301659vandwalle     * If Connectivity Service has triggered an unwanted network disconnect
40144d0d289bc2a959a18af3870844d221c93301659vandwalle     */
40244d0d289bc2a959a18af3870844d221c93301659vandwalle    public long lastUnwantedNetworkDisconnectTimestamp = 0;
40344d0d289bc2a959a18af3870844d221c93301659vandwalle
40444d0d289bc2a959a18af3870844d221c93301659vandwalle    /**
405c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle     * The maximum number of times we will retry a connection to an access point
406c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle     * for which we have failed in acquiring an IP address from DHCP. A value of
407c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle     * N means that we will make N+1 connection attempts in all.
408c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle     * <p>
409c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle     * See {@link Settings.Secure#WIFI_MAX_DHCP_RETRY_COUNT}. This is the default
410c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle     * value if a Settings value is not present.
411c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle     */
412c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    private static final int DEFAULT_MAX_DHCP_RETRIES = 9;
413c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
414c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
415155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final LocalLog mLocalLog;
416155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final WpaConfigFileObserver mFileObserver;
417155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
418155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private WifiNative mWifiNative;
419155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private final KeyStore mKeyStore = KeyStore.getInstance();
420155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
421931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
4228c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle     * The lastSelectedConfiguration is used to remember which network
4238c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle     * was selected last by the user.
424117be607246604e875de62aa8cdd99700b77a2b4vandwalle     * The connection to this network may not be successful, as well
425117be607246604e875de62aa8cdd99700b77a2b4vandwalle     * the selection (i.e. network priority) might not be persisted.
426117be607246604e875de62aa8cdd99700b77a2b4vandwalle     * WiFi state machine is the only object that sets this variable.
427117be607246604e875de62aa8cdd99700b77a2b4vandwalle     */
428117be607246604e875de62aa8cdd99700b77a2b4vandwalle    private String lastSelectedConfiguration = null;
429117be607246604e875de62aa8cdd99700b77a2b4vandwalle
430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    WifiConfigStore(Context c, WifiNative wn) {
431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext = c;
432155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiNative = wn;
433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4347b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        if (showNetworks) {
435155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mLocalLog = mWifiNative.getLocalLog();
436155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mFileObserver = new WpaConfigFileObserver();
437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mFileObserver.startWatching();
438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
439155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mLocalLog = null;
440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mFileObserver = null;
441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
4422f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle
4430eebae7334d6129f7ca1344e4b20199794994358vandwalle        associatedPartialScanPeriodMilli = mContext.getResources().getInteger(
4442f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                R.integer.config_wifi_framework_associated_scan_interval);
4450eebae7334d6129f7ca1344e4b20199794994358vandwalle        loge("associatedPartialScanPeriodMilli set to " + associatedPartialScanPeriodMilli);
446448c9536a302c58a79e271b1721c08b8882f800evandwalle
447448c9536a302c58a79e271b1721c08b8882f800evandwalle        onlyLinkSameCredentialConfigurations = mContext.getResources().getBoolean(
448448c9536a302c58a79e271b1721c08b8882f800evandwalle                R.bool.config_wifi_only_link_same_credential_configurations);
4490eebae7334d6129f7ca1344e4b20199794994358vandwalle        maxNumActiveChannelsForPartialScans = mContext.getResources().getInteger(
4500eebae7334d6129f7ca1344e4b20199794994358vandwalle                R.integer.config_wifi_framework_associated_partial_scan_max_num_active_channels);
4510eebae7334d6129f7ca1344e4b20199794994358vandwalle        maxNumPassiveChannelsForPartialScans = mContext.getResources().getInteger(
4520eebae7334d6129f7ca1344e4b20199794994358vandwalle                R.integer.config_wifi_framework_associated_partial_scan_max_num_passive_channels);
4530eebae7334d6129f7ca1344e4b20199794994358vandwalle        associatedFullScanMaxIntervalMilli = mContext.getResources().getInteger(
4547b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                R.integer.config_wifi_framework_associated_full_scan_max_interval);
4550eebae7334d6129f7ca1344e4b20199794994358vandwalle        associatedFullScanBackoff = mContext.getResources().getInteger(
4560eebae7334d6129f7ca1344e4b20199794994358vandwalle                R.integer.config_wifi_framework_associated_full_scan_backoff);
4570eebae7334d6129f7ca1344e4b20199794994358vandwalle        enableLinkDebouncing = mContext.getResources().getBoolean(
4580eebae7334d6129f7ca1344e4b20199794994358vandwalle                R.bool.config_wifi_enable_disconnection_debounce);
4590eebae7334d6129f7ca1344e4b20199794994358vandwalle
4600eebae7334d6129f7ca1344e4b20199794994358vandwalle        enable5GHzPreference = mContext.getResources().getBoolean(
4610eebae7334d6129f7ca1344e4b20199794994358vandwalle                R.bool.config_wifi_enable_5GHz_preference);
4620eebae7334d6129f7ca1344e4b20199794994358vandwalle
4630eebae7334d6129f7ca1344e4b20199794994358vandwalle        bandPreferenceBoostFactor5 = mContext.getResources().getInteger(
4640eebae7334d6129f7ca1344e4b20199794994358vandwalle                R.integer.config_wifi_framework_5GHz_preference_boost_factor);
4650eebae7334d6129f7ca1344e4b20199794994358vandwalle        bandPreferencePenaltyFactor5 = mContext.getResources().getInteger(
4660eebae7334d6129f7ca1344e4b20199794994358vandwalle                R.integer.config_wifi_framework_5GHz_preference_penalty_factor);
4670eebae7334d6129f7ca1344e4b20199794994358vandwalle
4680eebae7334d6129f7ca1344e4b20199794994358vandwalle        bandPreferencePenaltyThreshold5 = mContext.getResources().getInteger(
4690eebae7334d6129f7ca1344e4b20199794994358vandwalle                R.integer.config_wifi_framework_5GHz_preference_penalty_threshold);
4700eebae7334d6129f7ca1344e4b20199794994358vandwalle        bandPreferenceBoostThreshold5 = mContext.getResources().getInteger(
4710eebae7334d6129f7ca1344e4b20199794994358vandwalle                R.integer.config_wifi_framework_5GHz_preference_boost_threshold);
4720eebae7334d6129f7ca1344e4b20199794994358vandwalle
4730eebae7334d6129f7ca1344e4b20199794994358vandwalle        associatedHysteresisHigh = mContext.getResources().getInteger(
4740eebae7334d6129f7ca1344e4b20199794994358vandwalle                R.integer.config_wifi_framework_current_association_hysteresis_high);
4750eebae7334d6129f7ca1344e4b20199794994358vandwalle        associatedHysteresisLow = mContext.getResources().getInteger(
4760eebae7334d6129f7ca1344e4b20199794994358vandwalle                R.integer.config_wifi_framework_current_association_hysteresis_low);
47717e828848eebb4eb11d27ce32d02716334dd6011vandwalle
47817e828848eebb4eb11d27ce32d02716334dd6011vandwalle        thresholdBadRssi5 = mContext.getResources().getInteger(
47917e828848eebb4eb11d27ce32d02716334dd6011vandwalle                R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_5GHz);
48017e828848eebb4eb11d27ce32d02716334dd6011vandwalle        thresholdLowRssi5 = mContext.getResources().getInteger(
48117e828848eebb4eb11d27ce32d02716334dd6011vandwalle                R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz);
48217e828848eebb4eb11d27ce32d02716334dd6011vandwalle        thresholdGoodRssi5 = mContext.getResources().getInteger(
48317e828848eebb4eb11d27ce32d02716334dd6011vandwalle                R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_5GHz);
48417e828848eebb4eb11d27ce32d02716334dd6011vandwalle        thresholdBadRssi24 = mContext.getResources().getInteger(
48599d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle                R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_24GHz);
48617e828848eebb4eb11d27ce32d02716334dd6011vandwalle        thresholdLowRssi24 = mContext.getResources().getInteger(
48799d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle                R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz);
48817e828848eebb4eb11d27ce32d02716334dd6011vandwalle        thresholdGoodRssi24 = mContext.getResources().getInteger(
48999d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle                R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_24GHz);
49017e828848eebb4eb11d27ce32d02716334dd6011vandwalle
49117e828848eebb4eb11d27ce32d02716334dd6011vandwalle        enableWifiCellularHandoverUserTriggeredAdjustment = mContext.getResources().getBoolean(
49217e828848eebb4eb11d27ce32d02716334dd6011vandwalle                R.bool.config_wifi_framework_cellular_handover_enable_user_triggered_adjustment);
49317e828848eebb4eb11d27ce32d02716334dd6011vandwalle
49417e828848eebb4eb11d27ce32d02716334dd6011vandwalle        badLinkSpeed24 = mContext.getResources().getInteger(
49517e828848eebb4eb11d27ce32d02716334dd6011vandwalle                R.integer.config_wifi_framework_wifi_score_bad_link_speed_24);
49617e828848eebb4eb11d27ce32d02716334dd6011vandwalle        badLinkSpeed5 = mContext.getResources().getInteger(
49717e828848eebb4eb11d27ce32d02716334dd6011vandwalle                R.integer.config_wifi_framework_wifi_score_bad_link_speed_5);
49817e828848eebb4eb11d27ce32d02716334dd6011vandwalle        goodLinkSpeed24 = mContext.getResources().getInteger(
49917e828848eebb4eb11d27ce32d02716334dd6011vandwalle                R.integer.config_wifi_framework_wifi_score_good_link_speed_24);
50017e828848eebb4eb11d27ce32d02716334dd6011vandwalle        goodLinkSpeed5 = mContext.getResources().getInteger(
50117e828848eebb4eb11d27ce32d02716334dd6011vandwalle                R.integer.config_wifi_framework_wifi_score_good_link_speed_5);
50217e828848eebb4eb11d27ce32d02716334dd6011vandwalle
503e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        maxAuthErrorsToBlacklist = mContext.getResources().getInteger(
504e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                R.integer.config_wifi_framework_max_auth_errors_to_blacklist);
505e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        maxConnectionErrorsToBlacklist = mContext.getResources().getInteger(
506e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                R.integer.config_wifi_framework_max_connection_errors_to_blacklist);
507e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        wifiConfigBlacklistMinTimeMilli = mContext.getResources().getInteger(
508e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                R.integer.config_wifi_framework_network_black_list_min_time_milli);
50970468b47454c8657e8963932f2e08a3f4d7e3881vandwalle
51070468b47454c8657e8963932f2e08a3f4d7e3881vandwalle
51170468b47454c8657e8963932f2e08a3f4d7e3881vandwalle        enableAutoJoinScanWhenAssociated = mContext.getResources().getBoolean(
51270468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                R.bool.config_wifi_framework_enable_associated_autojoin_scan);
51370468b47454c8657e8963932f2e08a3f4d7e3881vandwalle
51470468b47454c8657e8963932f2e08a3f4d7e3881vandwalle        enableAutoJoinWhenAssociated = mContext.getResources().getBoolean(
51570468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                R.bool.config_wifi_framework_enable_associated_network_selection);
516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
518ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    void enableVerboseLogging(int verbose) {
5199e806a8d9f297e4de826e65322859a89e4d95c8bvandwalle        enableVerboseLogging = verbose;
520ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        if (verbose > 0) {
521ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            VDBG = true;
5227b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            showNetworks = true;
523ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        } else {
524ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            VDBG = false;
525ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
5267b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        if (verbose > 1) {
5277b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            VVDBG = true;
5287b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        } else {
5297b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            VVDBG = false;
5307b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        }
531ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
532ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    class WpaConfigFileObserver extends FileObserver {
534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public WpaConfigFileObserver() {
536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            super(SUPPLICANT_CONFIG_FILE, CLOSE_WRITE);
537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        @Override
540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        public void onEvent(int event, String path) {
541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (event == CLOSE_WRITE) {
542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                File file = new File(SUPPLICANT_CONFIG_FILE);
543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (VDBG) localLog("wpa_supplicant.conf changed; new size = " + file.length());
544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Fetch the list of configured networks
551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * and enable all stored networks in supplicant.
552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    void loadAndEnableAllNetworks() {
5540888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (DBG) log("Loading config and enabling all networks ");
555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        loadConfiguredNetworks();
556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enableAllNetworks();
557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
559931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    int getConfiguredNetworksSize() {
560931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        return mConfiguredNetworks.size();
561931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    }
562931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle
5639878c61bbd81176561991be025af44efc67332feWenchao Tong    private List<WifiConfiguration> getConfiguredNetworks(Map<String, String> pskMap) {
5649878c61bbd81176561991be025af44efc67332feWenchao Tong        List<WifiConfiguration> networks = new ArrayList<>();
565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for(WifiConfiguration config : mConfiguredNetworks.values()) {
5669878c61bbd81176561991be025af44efc67332feWenchao Tong            WifiConfiguration newConfig = new WifiConfiguration(config);
5670e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            if (config.autoJoinStatus == WifiConfiguration.AUTO_JOIN_DELETED) {
568a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                // Do not enumerate and return this configuration to any one,
569a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                // for instance WiFi Picker.
570a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                // instead treat it as unknown. the configuration can still be retrieved
571a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                // directly by the key or networkId
5720e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                continue;
5730e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            }
574e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
5759878c61bbd81176561991be025af44efc67332feWenchao Tong            if (pskMap != null && config.allowedKeyManagement != null
5769878c61bbd81176561991be025af44efc67332feWenchao Tong                    && config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)
5779878c61bbd81176561991be025af44efc67332feWenchao Tong                    && pskMap.containsKey(config.SSID)) {
5789878c61bbd81176561991be025af44efc67332feWenchao Tong                newConfig.preSharedKey = pskMap.get(config.SSID);
5799878c61bbd81176561991be025af44efc67332feWenchao Tong            }
5809878c61bbd81176561991be025af44efc67332feWenchao Tong            networks.add(newConfig);
581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return networks;
583155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
584155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
585155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
5869878c61bbd81176561991be025af44efc67332feWenchao Tong     * Fetch the list of currently configured networks
5879878c61bbd81176561991be025af44efc67332feWenchao Tong     * @return List of networks
5889878c61bbd81176561991be025af44efc67332feWenchao Tong     */
5899878c61bbd81176561991be025af44efc67332feWenchao Tong    List<WifiConfiguration> getConfiguredNetworks() {
5909878c61bbd81176561991be025af44efc67332feWenchao Tong        return getConfiguredNetworks(null);
5919878c61bbd81176561991be025af44efc67332feWenchao Tong    }
5929878c61bbd81176561991be025af44efc67332feWenchao Tong
5939878c61bbd81176561991be025af44efc67332feWenchao Tong    /**
5949878c61bbd81176561991be025af44efc67332feWenchao Tong     * Fetch the list of currently configured networks, filled with real preSharedKeys
5959878c61bbd81176561991be025af44efc67332feWenchao Tong     * @return List of networks
5969878c61bbd81176561991be025af44efc67332feWenchao Tong     */
5979878c61bbd81176561991be025af44efc67332feWenchao Tong    List<WifiConfiguration> getPrivilegedConfiguredNetworks() {
5989878c61bbd81176561991be025af44efc67332feWenchao Tong        Map<String, String> pskMap = getCredentialsBySsidMap();
5999878c61bbd81176561991be025af44efc67332feWenchao Tong        return getConfiguredNetworks(pskMap);
6009878c61bbd81176561991be025af44efc67332feWenchao Tong    }
6019878c61bbd81176561991be025af44efc67332feWenchao Tong
6029878c61bbd81176561991be025af44efc67332feWenchao Tong    /**
6039878c61bbd81176561991be025af44efc67332feWenchao Tong     * Fetch the preSharedKeys for all networks.
6049878c61bbd81176561991be025af44efc67332feWenchao Tong     * @return a map from Ssid to preSharedKey.
6059878c61bbd81176561991be025af44efc67332feWenchao Tong     */
6069878c61bbd81176561991be025af44efc67332feWenchao Tong    private Map<String, String> getCredentialsBySsidMap() {
6079878c61bbd81176561991be025af44efc67332feWenchao Tong        return readNetworkVariablesFromSupplicantFile("psk");
6089878c61bbd81176561991be025af44efc67332feWenchao Tong    }
6099878c61bbd81176561991be025af44efc67332feWenchao Tong
6102f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle    int getconfiguredNetworkSize() {
6112f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle        if (mConfiguredNetworks == null)
6122f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle            return 0;
6132f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle        return mConfiguredNetworks.size();
6142f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle    }
6152f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle
6169878c61bbd81176561991be025af44efc67332feWenchao Tong    /**
617f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * Fetch the list of currently configured networks that were recently seen
618f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     *
619f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * @return List of networks
620f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     */
621f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    List<WifiConfiguration> getRecentConfiguredNetworks(int milli, boolean copy) {
622f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        List<WifiConfiguration> networks = null;
623f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
624f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        for (WifiConfiguration config : mConfiguredNetworks.values()) {
6250e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            if (config.autoJoinStatus == WifiConfiguration.AUTO_JOIN_DELETED) {
626c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                // Do not enumerate and return this configuration to any one,
627c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                // instead treat it as unknown. the configuration can still be retrieved
628c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                // directly by the key or networkId
6290e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                continue;
6300e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            }
6310e3c67280365eb82fc15362a3b98a2c978bde766vandwalle
632c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            // Calculate the RSSI for scan results that are more recent than milli
633f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            config.setVisibility(milli);
634f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (config.visibility == null) {
635f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                continue;
636f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
637f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (config.visibility.rssi5 == WifiConfiguration.INVALID_RSSI &&
638f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    config.visibility.rssi24 == WifiConfiguration.INVALID_RSSI) {
639f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                continue;
640f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
641f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (networks == null)
642f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                networks = new ArrayList<WifiConfiguration>();
643f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (copy) {
644f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                networks.add(new WifiConfiguration(config));
645f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else {
646f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                networks.add(config);
647f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
648f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
649f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        return networks;
650f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
651f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
652f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    /**
653c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle     *  Update the configuration and BSSID with latest RSSI value.
654c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle     */
655c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    void updateConfiguration(WifiInfo info) {
656c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        WifiConfiguration config = getWifiConfiguration(info.getNetworkId());
657c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        if (config != null && config.scanResultCache != null) {
658c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            ScanResult result = config.scanResultCache.get(info.getBSSID());
659c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            if (result != null) {
660c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                long previousSeen = result.seen;
661c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                int previousRssi = result.level;
662c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
663c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                // Update the scan result
664c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                result.seen = System.currentTimeMillis();
665c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                result.level = info.getRssi();
666c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
667c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                // Average the RSSI value
668c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                result.averageRssi(previousRssi, previousSeen,
669c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        WifiAutoJoinController.mScanResultMaximumAge);
670ede507649471f1113e9e1919812115ca5a6bc0c8vandwalle                if (VDBG) {
671c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                    loge("updateConfiguration freq=" + result.frequency
672ede507649471f1113e9e1919812115ca5a6bc0c8vandwalle                        + " BSSID=" + result.BSSID
673ede507649471f1113e9e1919812115ca5a6bc0c8vandwalle                        + " RSSI=" + result.level
674ede507649471f1113e9e1919812115ca5a6bc0c8vandwalle                        + " " + config.configKey());
675ede507649471f1113e9e1919812115ca5a6bc0c8vandwalle                }
676c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            }
677c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        }
678c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    }
679c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
680c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    /**
681f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * get the Wificonfiguration for this netId
682f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     *
683f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * @return Wificonfiguration
684f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     */
685f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    WifiConfiguration getWifiConfiguration(int netId) {
686f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (mConfiguredNetworks == null)
687f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return null;
688f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        return mConfiguredNetworks.get(netId);
689f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
690f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
691f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    /**
692c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle     * Get the Wificonfiguration for this key
693f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * @return Wificonfiguration
694f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     */
695f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    WifiConfiguration getWifiConfiguration(String key) {
696f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (key == null)
697f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return null;
698f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        int hash = key.hashCode();
699f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (mNetworkIds == null)
700f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return null;
701952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle        Integer n = mNetworkIds.get(hash);
702952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle        if (n == null)
703952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle            return null;
704952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle        int netId = n.intValue();
705f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        return getWifiConfiguration(netId);
706f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
707f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
708f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    /**
709c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle     * Enable all networks and save config. This will be a no-op if the list
710155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * of configured networks indicates all networks as being enabled
711155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
712155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    void enableAllNetworks() {
713e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        long now = System.currentTimeMillis();
714155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean networkEnabledStateChanged = false;
715e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle
716155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for(WifiConfiguration config : mConfiguredNetworks.values()) {
717a5283c01ac413e378e2261050cac3a6d6e9cf385vandwalle
718f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if(config != null && config.status == Status.DISABLED
719be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                    && (config.autoJoinStatus
720be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                    <= WifiConfiguration.AUTO_JOIN_DISABLED_ON_AUTH_FAILURE)) {
721e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
722e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                // Wait for 20 minutes before reenabling config that have known, repeated connection
723e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                // or DHCP failures
724e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                if (config.disableReason == WifiConfiguration.DISABLED_DHCP_FAILURE
725e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        || config.disableReason == WifiConfiguration.DISABLED_ASSOCIATION_REJECT
726e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        || config.disableReason == WifiConfiguration.DISABLED_AUTH_FAILURE) {
727e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    if (config.blackListTimestamp != 0
728e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                           && now > config.blackListTimestamp
729e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                           && (now - config.blackListTimestamp) < wifiConfigBlacklistMinTimeMilli) {
730e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        continue;
731e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    }
732e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                }
733e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
734155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if(mWifiNative.enableNetwork(config.networkId, false)) {
735155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    networkEnabledStateChanged = true;
736155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    config.status = Status.ENABLED;
737a5283c01ac413e378e2261050cac3a6d6e9cf385vandwalle
738e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                    // Reset the blacklist condition
739e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                    config.numConnectionFailures = 0;
740e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                    config.numIpConfigFailures = 0;
741e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                    config.numAuthFailures = 0;
742a5283c01ac413e378e2261050cac3a6d6e9cf385vandwalle
743a5283c01ac413e378e2261050cac3a6d6e9cf385vandwalle                    // Reenable the wifi configuration
744a5283c01ac413e378e2261050cac3a6d6e9cf385vandwalle                    config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
745155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
746155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    loge("Enable network failed on " + config.networkId);
747155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
748155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
749155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
750155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
751155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (networkEnabledStateChanged) {
752155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiNative.saveConfig();
753155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            sendConfiguredNetworksChangedBroadcast();
754155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
755155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
756155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
757155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
758155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
759155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Selects the specified network for connection. This involves
760155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * updating the priority of all the networks and enabling the given
761155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * network while disabling others.
762155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
763155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Selecting a network will leave the other networks disabled and
764155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * a call to enableAllNetworks() needs to be issued upon a connection
765155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * or a failure event from supplicant
766155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
767155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId network to select for connection
768155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return false if the network id is invalid
769155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
770155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    boolean selectNetwork(int netId) {
771155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (VDBG) localLog("selectNetwork", netId);
772155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (netId == INVALID_NETWORK_ID) return false;
773155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
774155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Reset the priority of each network at start or if it goes too high.
775155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mLastPriority == -1 || mLastPriority > 1000000) {
776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for(WifiConfiguration config : mConfiguredNetworks.values()) {
777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (config.networkId != INVALID_NETWORK_ID) {
778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    config.priority = 0;
7790eebae7334d6129f7ca1344e4b20199794994358vandwalle                    addOrUpdateNetworkNative(config, -1);
780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
782155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mLastPriority = 0;
783155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
785155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Set to the highest priority and save the configuration.
786155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiConfiguration config = new WifiConfiguration();
787155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        config.networkId = netId;
788155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        config.priority = ++mLastPriority;
789155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
7900eebae7334d6129f7ca1344e4b20199794994358vandwalle        addOrUpdateNetworkNative(config, -1);
791155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiNative.saveConfig();
792155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
793155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* Enable the given network while disabling all other networks */
794155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        enableNetworkWithoutBroadcast(netId, true);
795155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
796155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande       /* Avoid saving the config & sending a broadcast to prevent settings
797155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        * from displaying a disabled list of networks */
798155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
799155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
800155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
801155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
802155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Add/update the specified configuration and save config
803155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
804155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param config WifiConfiguration to be saved
805155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return network update result
806155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
8070eebae7334d6129f7ca1344e4b20199794994358vandwalle    NetworkUpdateResult saveNetwork(WifiConfiguration config, int uid) {
808952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle        WifiConfiguration conf;
809f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
810155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // A new network cannot have null SSID
811155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config == null || (config.networkId == INVALID_NETWORK_ID &&
812155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                config.SSID == null)) {
813155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return new NetworkUpdateResult(INVALID_NETWORK_ID);
814155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
815f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (VDBG) localLog("WifiConfigStore: saveNetwork netId", config.networkId);
816f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (VDBG) {
817f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            loge("WifiConfigStore saveNetwork, size=" + mConfiguredNetworks.size()
818992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    + " SSID=" + config.SSID
819992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    + " Uid=" + Integer.toString(config.creatorUid)
820992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    + "/" + Integer.toString(config.lastUpdateUid));
821f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
822155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean newNetwork = (config.networkId == INVALID_NETWORK_ID);
8230eebae7334d6129f7ca1344e4b20199794994358vandwalle        NetworkUpdateResult result = addOrUpdateNetworkNative(config, uid);
824155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int netId = result.getNetworkId();
825f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
826f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (VDBG) localLog("WifiConfigStore: saveNetwork got it back netId=", netId);
827f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
828155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* enable a new network */
829155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (newNetwork && netId != INVALID_NETWORK_ID) {
830f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG) localLog("WifiConfigStore: will enable netId=", netId);
831f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
832155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiNative.enableNetwork(netId, false);
833952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle            conf = mConfiguredNetworks.get(netId);
834952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle            if (conf != null)
835952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle                conf.status = Status.ENABLED;
836155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
837f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
838952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle        conf = mConfiguredNetworks.get(netId);
839f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (conf != null) {
840f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (conf.autoJoinStatus != WifiConfiguration.AUTO_JOIN_ENABLED) {
841f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (VDBG) localLog("WifiConfigStore: re-enabling: " + conf.SSID);
842f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
843f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                // reenable autojoin, since new information has been provided
84427355a942653264388e909a4276196ee63e57811vandwalle                conf.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
845f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                enableNetworkWithoutBroadcast(conf.networkId, false);
846f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
847be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            if (VDBG) {
848be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                loge("WifiConfigStore: saveNetwork got config back netId="
849be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                        + Integer.toString(netId)
850be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                        + " uid=" + Integer.toString(config.creatorUid));
851be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            }
852f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
853f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
854155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiNative.saveConfig();
855e61237672316b46d7bec960a199bfbd5bfcca257Vinit Deshpande        sendConfiguredNetworksChangedBroadcast(conf, result.isNewNetwork() ?
856155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                WifiManager.CHANGE_REASON_ADDED : WifiManager.CHANGE_REASON_CONFIG_CHANGE);
857155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return result;
858155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
859155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
86044d0d289bc2a959a18af3870844d221c93301659vandwalle    /**
86144d0d289bc2a959a18af3870844d221c93301659vandwalle     * Firmware is roaming away from this BSSID, and this BSSID was on 5GHz, and it's RSSI was good,
86244d0d289bc2a959a18af3870844d221c93301659vandwalle     * this means we have a situation where we would want to remain on this BSSID but firmware
86344d0d289bc2a959a18af3870844d221c93301659vandwalle     * is not successful at it.
86444d0d289bc2a959a18af3870844d221c93301659vandwalle     * This situation is observed on a small number of Access Points, b/17960587
86544d0d289bc2a959a18af3870844d221c93301659vandwalle     * In that situation, blacklist this BSSID really hard so as framework will not attempt to
86644d0d289bc2a959a18af3870844d221c93301659vandwalle     * roam to it for the next 8 hours. We do not to keep flipping between 2.4 and 5GHz band..
86744d0d289bc2a959a18af3870844d221c93301659vandwalle     * TODO: review the blacklisting strategy so as to make it softer and adaptive
86844d0d289bc2a959a18af3870844d221c93301659vandwalle     * @param info
86944d0d289bc2a959a18af3870844d221c93301659vandwalle     */
87044d0d289bc2a959a18af3870844d221c93301659vandwalle    void driverRoamedFrom(WifiInfo info) {
87144d0d289bc2a959a18af3870844d221c93301659vandwalle        if (info != null
87244d0d289bc2a959a18af3870844d221c93301659vandwalle            && info.getBSSID() != null
87344d0d289bc2a959a18af3870844d221c93301659vandwalle            && ScanResult.is5GHz(info.getFrequency())
87444d0d289bc2a959a18af3870844d221c93301659vandwalle            && info.getRssi() > (bandPreferenceBoostThreshold5 + 3)) {
87544d0d289bc2a959a18af3870844d221c93301659vandwalle            WifiConfiguration config = getWifiConfiguration(info.getNetworkId());
87644d0d289bc2a959a18af3870844d221c93301659vandwalle            if (config != null) {
87744d0d289bc2a959a18af3870844d221c93301659vandwalle                if (config.scanResultCache != null) {
87844d0d289bc2a959a18af3870844d221c93301659vandwalle                    ScanResult result = config.scanResultCache.get(info.getBSSID());
87944d0d289bc2a959a18af3870844d221c93301659vandwalle                    if (result != null) {
88044d0d289bc2a959a18af3870844d221c93301659vandwalle                        result.setAutoJoinStatus(ScanResult.AUTO_ROAM_DISABLED + 1);
88144d0d289bc2a959a18af3870844d221c93301659vandwalle                    }
88244d0d289bc2a959a18af3870844d221c93301659vandwalle                }
88344d0d289bc2a959a18af3870844d221c93301659vandwalle            }
88444d0d289bc2a959a18af3870844d221c93301659vandwalle        }
88544d0d289bc2a959a18af3870844d221c93301659vandwalle    }
88644d0d289bc2a959a18af3870844d221c93301659vandwalle
8879f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    void saveWifiConfigBSSID(WifiConfiguration config) {
8882f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle        // Sanity check the config is valid
8899f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle        if (config == null || (config.networkId == INVALID_NETWORK_ID &&
8909f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                config.SSID == null)) {
8919f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            return;
8929f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle        }
8939f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
8942f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle        // If an app specified a BSSID then dont over-write it
8952f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle        if (config.BSSID != null && config.BSSID != "any") {
8962f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle            return;
8979f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle        }
8982f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle
8992f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle        // If autojoin specified a BSSID then write it in the network block
9002f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle        if (config.autoJoinBSSID != null) {
9012f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle            loge("saveWifiConfigBSSID Setting BSSID for " + config.configKey()
9022f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    + " to " + config.autoJoinBSSID);
9032f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle            if (!mWifiNative.setNetworkVariable(
9042f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    config.networkId,
9052f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    WifiConfiguration.bssidVarName,
9062f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    config.autoJoinBSSID)) {
9072f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                loge("failed to set BSSID: " + config.autoJoinBSSID);
9082f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle            } else if (config.autoJoinBSSID.equals("any")) {
9092f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                // Paranoia, we just want to make sure that we restore the config to normal
9102f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                mWifiNative.saveConfig();
9112f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle            }
9129f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle        }
9139f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    }
9149f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
9159f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
916155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    void updateStatus(int netId, DetailedState state) {
917155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (netId != INVALID_NETWORK_ID) {
918155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            WifiConfiguration config = mConfiguredNetworks.get(netId);
919155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (config == null) return;
920155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            switch (state) {
921155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case CONNECTED:
922155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    config.status = Status.CURRENT;
92327355a942653264388e909a4276196ee63e57811vandwalle                    //we successfully connected, hence remove the blacklist
92427355a942653264388e909a4276196ee63e57811vandwalle                    config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
925155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
926155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                case DISCONNECTED:
927155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    //If network is already disabled, keep the status
928155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (config.status == Status.CURRENT) {
929155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        config.status = Status.ENABLED;
930155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
931155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
932155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                default:
933155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    //do nothing, retain the existing state
934155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break;
935155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
936155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
937155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
938155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
939155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
940155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Forget the specified network and save config
941155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
942155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId network to forget
943155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if it succeeds, {@code false} otherwise
944155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
945155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    boolean forgetNetwork(int netId) {
9467b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        if (showNetworks) localLog("forgetNetwork", netId);
9470e3c67280365eb82fc15362a3b98a2c978bde766vandwalle
9480e3c67280365eb82fc15362a3b98a2c978bde766vandwalle        boolean remove = removeConfigAndSendBroadcastIfNeeded(netId);
9490e3c67280365eb82fc15362a3b98a2c978bde766vandwalle        if (!remove) {
9500e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            //success but we dont want to remove the network from supplicant conf file
9510e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            return true;
9520e3c67280365eb82fc15362a3b98a2c978bde766vandwalle        }
953155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiNative.removeNetwork(netId)) {
954155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mWifiNative.saveConfig();
955155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return true;
956155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
957155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            loge("Failed to remove network " + netId);
958155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
959155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
960155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
961155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
962155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
963155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Add/update a network. Note that there is no saveConfig operation.
964155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * This function is retained for compatibility with the public
965155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * API. The more powerful saveNetwork() is used by the
966155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * state machine
967155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
968155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param config wifi configuration to add/update
969155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return network Id
970155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
9712f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle    int addOrUpdateNetwork(WifiConfiguration config, int uid) {
9727b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        if (showNetworks) localLog("addOrUpdateNetwork id=", config.networkId);
973117be607246604e875de62aa8cdd99700b77a2b4vandwalle        //adding unconditional message to chase b/15111865
974992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle        Log.e(TAG, " key=" + config.configKey() + " netId=" + Integer.toString(config.networkId)
975992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                + " uid=" + Integer.toString(config.creatorUid)
976992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                + "/" + Integer.toString(config.lastUpdateUid));
9770eebae7334d6129f7ca1344e4b20199794994358vandwalle        NetworkUpdateResult result = addOrUpdateNetworkNative(config, uid);
978155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (result.getNetworkId() != WifiConfiguration.INVALID_NETWORK_ID) {
979952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle            WifiConfiguration conf = mConfiguredNetworks.get(result.getNetworkId());
980952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle            if (conf != null) {
981952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle                sendConfiguredNetworksChangedBroadcast(conf,
982155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    result.isNewNetwork ? WifiManager.CHANGE_REASON_ADDED :
983155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            WifiManager.CHANGE_REASON_CONFIG_CHANGE);
984952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle            }
985155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
986155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return result.getNetworkId();
987155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
988155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
989155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
990155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Remove a network. Note that there is no saveConfig operation.
991155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * This function is retained for compatibility with the public
992155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * API. The more powerful forgetNetwork() is used by the
993155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * state machine for network removal
994155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
995155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId network to be removed
996155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if it succeeds, {@code false} otherwise
997155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
998155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    boolean removeNetwork(int netId) {
9997b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        if (showNetworks) localLog("removeNetwork", netId);
1000155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean ret = mWifiNative.removeNetwork(netId);
1001155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ret) {
1002155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            removeConfigAndSendBroadcastIfNeeded(netId);
1003155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1004155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return ret;
1005155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1006155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
10070e3c67280365eb82fc15362a3b98a2c978bde766vandwalle    private boolean removeConfigAndSendBroadcastIfNeeded(int netId) {
10080e3c67280365eb82fc15362a3b98a2c978bde766vandwalle        boolean remove = true;
1009155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiConfiguration config = mConfiguredNetworks.get(netId);
1010155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config != null) {
1011f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG) {
1012f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                loge("removeNetwork " + Integer.toString(netId) + " key=" +
1013f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        config.configKey() + " config.id=" + Integer.toString(config.networkId));
1014f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1015f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1016117be607246604e875de62aa8cdd99700b77a2b4vandwalle            // cancel the last user choice
1017117be607246604e875de62aa8cdd99700b77a2b4vandwalle            if (config.configKey().equals(lastSelectedConfiguration)) {
1018117be607246604e875de62aa8cdd99700b77a2b4vandwalle                lastSelectedConfiguration = null;
1019117be607246604e875de62aa8cdd99700b77a2b4vandwalle            }
1020117be607246604e875de62aa8cdd99700b77a2b4vandwalle
1021155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // Remove any associated keys
1022155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (config.enterpriseConfig != null) {
1023155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                removeKeys(config.enterpriseConfig);
1024155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1025f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
102670468b47454c8657e8963932f2e08a3f4d7e3881vandwalle            if (config.selfAdded || config.linkedConfigurations != null
1027be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                    || config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
1028be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                remove = false;
1029be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                loge("removeNetwork " + Integer.toString(netId)
1030be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                        + " key=" + config.configKey()
1031be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                        + " config.id=" + Integer.toString(config.networkId)
1032be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                        + " -> mark as deleted");
1033f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1034155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
10350e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            if (remove) {
10360e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                mConfiguredNetworks.remove(netId);
10370e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                mNetworkIds.remove(configKey(config));
10380e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            } else {
1039be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                /**
1040be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                 * We can't directly remove the configuration since we could re-add it ourselves,
1041be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                 * and that would look weird to the user.
1042be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                 * Instead mark it as deleted and completely hide it from the rest of the system.
10430e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                 */
104427355a942653264388e909a4276196ee63e57811vandwalle                config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_DELETED);
1045448c9536a302c58a79e271b1721c08b8882f800evandwalle                // Disable
10460e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                mWifiNative.disableNetwork(config.networkId);
1047448c9536a302c58a79e271b1721c08b8882f800evandwalle                config.status = WifiConfiguration.Status.DISABLED;
1048448c9536a302c58a79e271b1721c08b8882f800evandwalle                // Since we don't delete the configuration, clean it up and loose the history
1049be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                config.linkedConfigurations = null;
1050be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                config.scanResultCache = null;
1051be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                config.connectChoices = null;
1052be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                config.defaultGwMacAddress = null;
1053be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                config.setIpConfiguration(new IpConfiguration());
1054be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                // Loose the PSK
1055be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                if (!mWifiNative.setNetworkVariable(
1056be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                        config.networkId,
1057be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                        WifiConfiguration.pskVarName,
105844d0d289bc2a959a18af3870844d221c93301659vandwalle                        "\"" + DELETED_CONFIG_PSK + "\"")) {
1059be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                    loge("removeNetwork, failed to clear PSK, nid=" + config.networkId);
1060448c9536a302c58a79e271b1721c08b8882f800evandwalle                }
1061448c9536a302c58a79e271b1721c08b8882f800evandwalle                // Loose the BSSID
1062448c9536a302c58a79e271b1721c08b8882f800evandwalle                config.BSSID = null;
1063448c9536a302c58a79e271b1721c08b8882f800evandwalle                config.autoJoinBSSID = null;
1064448c9536a302c58a79e271b1721c08b8882f800evandwalle                if (!mWifiNative.setNetworkVariable(
1065448c9536a302c58a79e271b1721c08b8882f800evandwalle                        config.networkId,
1066448c9536a302c58a79e271b1721c08b8882f800evandwalle                        WifiConfiguration.bssidVarName,
1067448c9536a302c58a79e271b1721c08b8882f800evandwalle                        "any")) {
1068448c9536a302c58a79e271b1721c08b8882f800evandwalle                    loge("removeNetwork, failed to remove BSSID");
1069448c9536a302c58a79e271b1721c08b8882f800evandwalle                }
1070448c9536a302c58a79e271b1721c08b8882f800evandwalle                // Loose the hiddenSSID flag
1071448c9536a302c58a79e271b1721c08b8882f800evandwalle                config.hiddenSSID = false;
1072448c9536a302c58a79e271b1721c08b8882f800evandwalle                if (!mWifiNative.setNetworkVariable(
1073448c9536a302c58a79e271b1721c08b8882f800evandwalle                        config.networkId,
1074448c9536a302c58a79e271b1721c08b8882f800evandwalle                        WifiConfiguration.hiddenSSIDVarName,
1075448c9536a302c58a79e271b1721c08b8882f800evandwalle                        Integer.toString(0))) {
1076448c9536a302c58a79e271b1721c08b8882f800evandwalle                    loge("removeNetwork, failed to remove hiddenSSID");
1077448c9536a302c58a79e271b1721c08b8882f800evandwalle                }
1078448c9536a302c58a79e271b1721c08b8882f800evandwalle
1079be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                mWifiNative.saveConfig();
10800e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            }
10810e3c67280365eb82fc15362a3b98a2c978bde766vandwalle
1082155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            writeIpAndProxyConfigurations();
1083155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            sendConfiguredNetworksChangedBroadcast(config, WifiManager.CHANGE_REASON_REMOVED);
1084f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            writeKnownNetworkHistory();
1085155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
10860e3c67280365eb82fc15362a3b98a2c978bde766vandwalle        return remove;
1087155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1088155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1089155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1090155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Enable a network. Note that there is no saveConfig operation.
1091155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * This function is retained for compatibility with the public
1092155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * API. The more powerful selectNetwork()/saveNetwork() is used by the
1093155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * state machine for connecting to a network
1094155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
1095155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId network to be enabled
1096155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if it succeeds, {@code false} otherwise
1097155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1098155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    boolean enableNetwork(int netId, boolean disableOthers) {
1099155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean ret = enableNetworkWithoutBroadcast(netId, disableOthers);
1100155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (disableOthers) {
1101155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (VDBG) localLog("enableNetwork(disableOthers=true) ", netId);
1102155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            sendConfiguredNetworksChangedBroadcast();
1103155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1104155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (VDBG) localLog("enableNetwork(disableOthers=false) ", netId);
1105155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            WifiConfiguration enabledNetwork = null;
1106155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            synchronized(mConfiguredNetworks) {
1107155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                enabledNetwork = mConfiguredNetworks.get(netId);
1108155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1109155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // check just in case the network was removed by someone else.
1110155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (enabledNetwork != null) {
1111155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                sendConfiguredNetworksChangedBroadcast(enabledNetwork,
1112155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        WifiManager.CHANGE_REASON_CONFIG_CHANGE);
1113155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1114155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1115155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return ret;
1116155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1117155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1118155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    boolean enableNetworkWithoutBroadcast(int netId, boolean disableOthers) {
1119155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean ret = mWifiNative.enableNetwork(netId, disableOthers);
1120155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1121155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiConfiguration config = mConfiguredNetworks.get(netId);
1122155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config != null) config.status = Status.ENABLED;
1123155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1124155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (disableOthers) {
1125155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            markAllNetworksDisabledExcept(netId);
1126155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1127155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return ret;
1128155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1129155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1130155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    void disableAllNetworks() {
1131155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (VDBG) localLog("disableAllNetworks");
1132155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean networkDisabled = false;
1133155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for(WifiConfiguration config : mConfiguredNetworks.values()) {
1134155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if(config != null && config.status != Status.DISABLED) {
1135155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if(mWifiNative.disableNetwork(config.networkId)) {
1136155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    networkDisabled = true;
1137155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    config.status = Status.DISABLED;
1138155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
1139155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    loge("Disable network failed on " + config.networkId);
1140155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1141155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1142155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1143155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1144155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (networkDisabled) {
1145155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            sendConfiguredNetworksChangedBroadcast();
1146155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1147155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1148155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1149155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Disable a network. Note that there is no saveConfig operation.
1150155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId network to be disabled
1151155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if it succeeds, {@code false} otherwise
1152155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1153155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    boolean disableNetwork(int netId) {
1154155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return disableNetwork(netId, WifiConfiguration.DISABLED_UNKNOWN_REASON);
1155155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1156155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1157155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1158155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Disable a network. Note that there is no saveConfig operation.
1159155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param netId network to be disabled
1160155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param reason reason code network was disabled
1161155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if it succeeds, {@code false} otherwise
1162155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1163155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    boolean disableNetwork(int netId, int reason) {
1164155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (VDBG) localLog("disableNetwork", netId);
1165155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean ret = mWifiNative.disableNetwork(netId);
1166155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiConfiguration network = null;
1167155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiConfiguration config = mConfiguredNetworks.get(netId);
1168f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1169f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (VDBG) {
1170f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (config != null) {
1171f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                loge("disableNetwork netId=" + Integer.toString(netId)
1172f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        + " SSID=" + config.SSID
1173f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        + " disabled=" + (config.status == Status.DISABLED)
1174f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        + " reason=" + Integer.toString(config.disableReason));
1175f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1176f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
11770eebae7334d6129f7ca1344e4b20199794994358vandwalle        /* Only change the reason if the network was not previously disabled
11780eebae7334d6129f7ca1344e4b20199794994358vandwalle        /* and the reason is not DISABLED_BY_WIFI_MANAGER, that is, if a 3rd party
11790eebae7334d6129f7ca1344e4b20199794994358vandwalle         * set its configuration as disabled, then leave it disabled */
11800eebae7334d6129f7ca1344e4b20199794994358vandwalle        if (config != null && config.status != Status.DISABLED
11810eebae7334d6129f7ca1344e4b20199794994358vandwalle                && config.disableReason != WifiConfiguration.DISABLED_BY_WIFI_MANAGER) {
1182155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            config.status = Status.DISABLED;
1183155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            config.disableReason = reason;
1184155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            network = config;
1185155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
11860eebae7334d6129f7ca1344e4b20199794994358vandwalle        if (reason == WifiConfiguration.DISABLED_BY_WIFI_MANAGER) {
11870eebae7334d6129f7ca1344e4b20199794994358vandwalle            // Make sure autojoin wont reenable this configuration without further user
11880eebae7334d6129f7ca1344e4b20199794994358vandwalle            // intervention
118911116da6bceae3759f9f2d75cd412fce541fc001Vinit Deshpande            config.status = Status.DISABLED;
11900eebae7334d6129f7ca1344e4b20199794994358vandwalle            config.autoJoinStatus = WifiConfiguration.AUTO_JOIN_DISABLED_USER_ACTION;
11910eebae7334d6129f7ca1344e4b20199794994358vandwalle        }
1192155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (network != null) {
1193155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            sendConfiguredNetworksChangedBroadcast(network,
1194155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    WifiManager.CHANGE_REASON_CONFIG_CHANGE);
1195155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1196155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return ret;
1197155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1198155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1199155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1200155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Save the configured networks in supplicant to disk
1201155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if it succeeds, {@code false} otherwise
1202155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1203155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    boolean saveConfig() {
1204155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return mWifiNative.saveConfig();
1205155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1206155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1207155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1208155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start WPS pin method configuration with pin obtained
1209155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * from the access point
1210155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param config WPS configuration
1211155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return Wps result containing status and pin
1212155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1213155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    WpsResult startWpsWithPinFromAccessPoint(WpsInfo config) {
1214155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WpsResult result = new WpsResult();
1215155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiNative.startWpsRegistrar(config.BSSID, config.pin)) {
1216155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            /* WPS leaves all networks disabled */
1217155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            markAllNetworksDisabled();
1218155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            result.status = WpsResult.Status.SUCCESS;
1219155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1220155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            loge("Failed to start WPS pin method configuration");
1221155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            result.status = WpsResult.Status.FAILURE;
1222155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1223155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return result;
1224155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1225155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1226155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1227155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start WPS pin method configuration with pin obtained
1228155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * from the device
1229155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return WpsResult indicating status and pin
1230155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1231155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    WpsResult startWpsWithPinFromDevice(WpsInfo config) {
1232155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WpsResult result = new WpsResult();
1233155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        result.pin = mWifiNative.startWpsPinDisplay(config.BSSID);
1234155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* WPS leaves all networks disabled */
1235155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(result.pin)) {
1236155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            markAllNetworksDisabled();
1237155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            result.status = WpsResult.Status.SUCCESS;
1238155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1239155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            loge("Failed to start WPS pin method configuration");
1240155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            result.status = WpsResult.Status.FAILURE;
1241155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1242155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return result;
1243155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1244155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1245155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1246155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Start WPS push button configuration
1247155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param config WPS configuration
1248155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return WpsResult indicating status and pin
1249155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1250155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    WpsResult startWpsPbc(WpsInfo config) {
1251155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WpsResult result = new WpsResult();
1252155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mWifiNative.startWpsPbc(config.BSSID)) {
1253155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            /* WPS leaves all networks disabled */
1254155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            markAllNetworksDisabled();
1255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            result.status = WpsResult.Status.SUCCESS;
1256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
1257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            loge("Failed to start WPS push button configuration");
1258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            result.status = WpsResult.Status.FAILURE;
1259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return result;
1261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1262155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1263155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
12643b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti     * Fetch the static IP configuration for a given network id
1265155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
12663b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti    StaticIpConfiguration getStaticIpConfiguration(int netId) {
1267155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiConfiguration config = mConfiguredNetworks.get(netId);
12683b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        if (config != null) {
12693b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti            return config.getStaticIpConfiguration();
12703b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        }
1271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
1272155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1273155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1274155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
12753b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti     * Set the static IP configuration for a given network id
1276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
12773b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti    void setStaticIpConfiguration(int netId, StaticIpConfiguration staticIpConfiguration) {
1278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiConfiguration config = mConfiguredNetworks.get(netId);
1279155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config != null) {
12803b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti            config.setStaticIpConfiguration(staticIpConfiguration);
1281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1285f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * set default GW MAC address
1286f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     */
1287f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void setDefaultGwMacAddress(int netId, String macAddress) {
1288f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration config = mConfiguredNetworks.get(netId);
1289f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (config != null) {
1290f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            //update defaultGwMacAddress
1291f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            config.defaultGwMacAddress = macAddress;
1292f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1293f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
1294f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1295f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1296f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    /**
1297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Fetch the proxy properties for a given network id
1298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param network id
1299d4d0c9ad1e3289702ec341521b9bd01b52e9c1d8Jason Monk     * @return ProxyInfo for the network id
1300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1301d4d0c9ad1e3289702ec341521b9bd01b52e9c1d8Jason Monk    ProxyInfo getProxyProperties(int netId) {
13023b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        WifiConfiguration config = mConfiguredNetworks.get(netId);
13033b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        if (config != null) {
13043b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti            return config.getHttpProxy();
1305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return null;
1307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Return if the specified network is using static IP
1311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param network id
1312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return {@code true} if using static ip for netId
1313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    boolean isUsingStaticIp(int netId) {
1315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiConfiguration config = mConfiguredNetworks.get(netId);
13163ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim        if (config != null && config.getIpAssignment() == IpAssignment.STATIC) {
1317155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return true;
1318155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1319155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1320155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1321155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1322155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1323155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Should be called when a single network configuration is made.
1324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param network The network configuration that changed.
1325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param reason The reason for the change, should be one of WifiManager.CHANGE_REASON_ADDED,
1326155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * WifiManager.CHANGE_REASON_REMOVED, or WifiManager.CHANGE_REASON_CHANGE.
1327155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1328155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void sendConfiguredNetworksChangedBroadcast(WifiConfiguration network,
1329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            int reason) {
1330155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Intent intent = new Intent(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION);
1331155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1332155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intent.putExtra(WifiManager.EXTRA_MULTIPLE_NETWORKS_CHANGED, false);
1333155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intent.putExtra(WifiManager.EXTRA_WIFI_CONFIGURATION, network);
1334155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intent.putExtra(WifiManager.EXTRA_CHANGE_REASON, reason);
1335155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1336155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1337155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1338155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
1339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Should be called when multiple network configuration changes are made.
1340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
1341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void sendConfiguredNetworksChangedBroadcast() {
1342155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        Intent intent = new Intent(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION);
1343155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1344155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        intent.putExtra(WifiManager.EXTRA_MULTIPLE_NETWORKS_CHANGED, true);
1345155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1346155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1347155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1348155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    void loadConfiguredNetworks() {
1349155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String listStr = mWifiNative.listNetworks();
1350155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mLastPriority = 0;
1351155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1352155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mConfiguredNetworks.clear();
1353155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mNetworkIds.clear();
1354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (listStr == null)
1356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return;
1357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String[] lines = listStr.split("\n");
13590888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle
13607b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        if (showNetworks) {
13617b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog("WifiConfigStore: loadConfiguredNetworks:  ");
13627b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            for (String net : lines) {
13637b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                localLog(net);
13647b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            }
13650888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        }
13660888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle
1367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Skip the first line, which is a header
1368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (int i = 1; i < lines.length; i++) {
1369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String[] result = lines[i].split("\t");
1370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // network-id | ssid | bssid | flags
1371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            WifiConfiguration config = new WifiConfiguration();
1372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
1373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                config.networkId = Integer.parseInt(result[0]);
1374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch(NumberFormatException e) {
1375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                loge("Failed to read network-id '" + result[0] + "'");
1376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                continue;
1377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (result.length > 3) {
1379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (result[3].indexOf("[CURRENT]") != -1)
1380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    config.status = WifiConfiguration.Status.CURRENT;
1381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                else if (result[3].indexOf("[DISABLED]") != -1)
1382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    config.status = WifiConfiguration.Status.DISABLED;
1383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                else
1384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    config.status = WifiConfiguration.Status.ENABLED;
1385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
1386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                config.status = WifiConfiguration.Status.ENABLED;
1387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
138844d0d289bc2a959a18af3870844d221c93301659vandwalle
1389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            readNetworkVariables(config);
139044d0d289bc2a959a18af3870844d221c93301659vandwalle
139144d0d289bc2a959a18af3870844d221c93301659vandwalle            String psk = readNetworkVariableFromSupplicantFile(config.SSID, "psk");
139244d0d289bc2a959a18af3870844d221c93301659vandwalle            if (psk!= null && psk.equals(DELETED_CONFIG_PSK)) {
139344d0d289bc2a959a18af3870844d221c93301659vandwalle                // This is a config we previously deleted, ignore it
139444d0d289bc2a959a18af3870844d221c93301659vandwalle                if (showNetworks) {
139544d0d289bc2a959a18af3870844d221c93301659vandwalle                    localLog("found deleted network " + config.SSID + " ", config.networkId);
139644d0d289bc2a959a18af3870844d221c93301659vandwalle                }
139744d0d289bc2a959a18af3870844d221c93301659vandwalle                config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_DELETED);
139844d0d289bc2a959a18af3870844d221c93301659vandwalle                config.priority = 0;
139944d0d289bc2a959a18af3870844d221c93301659vandwalle            }
140044d0d289bc2a959a18af3870844d221c93301659vandwalle
1401155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (config.priority > mLastPriority) {
1402155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mLastPriority = config.priority;
1403155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
14043ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim
14053ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim            config.setIpAssignment(IpAssignment.DHCP);
14063ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim            config.setProxySettings(ProxySettings.NONE);
1407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1408155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (mNetworkIds.containsKey(configKey(config))) {
1409155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // That SSID is already known, just ignore this duplicate entry
14107b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                if (showNetworks) localLog("discarded duplicate network ", config.networkId);
1411e65cebc4284fd320a155a274cb647f73cecabd11Navtej Singh Mann            } else if(config.isValid()){
1412155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mConfiguredNetworks.put(config.networkId, config);
1413155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mNetworkIds.put(configKey(config), config.networkId);
14147b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                if (showNetworks) localLog("loaded configured network", config.networkId);
1415e65cebc4284fd320a155a274cb647f73cecabd11Navtej Singh Mann            } else {
14167b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                if (showNetworks) log("Ignoring loaded configured for network " + config.networkId
14170888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    + " because config are not valid");
1418155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1419155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1420155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1421155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        readIpAndProxyConfigurations();
1422f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        readNetworkHistory();
14238c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle        readAutoJoinConfig();
1424f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1425155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        sendConfiguredNetworksChangedBroadcast();
1426155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
14277b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        if (showNetworks) localLog("loadConfiguredNetworks loaded " + mNetworkIds.size() + " networks");
1428155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1429155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mNetworkIds.size() == 0) {
1430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // no networks? Lets log if the wpa_supplicant.conf file contents
1431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            BufferedReader reader = null;
1432155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
1433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                reader = new BufferedReader(new FileReader(SUPPLICANT_CONFIG_FILE));
14347b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                if (DBG) {
14357b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    localLog("--- Begin wpa_supplicant.conf Contents ---", true);
14367b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    for (String line = reader.readLine(); line != null; line = reader.readLine()) {
14377b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        localLog(line, true);
14387b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    }
14397b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    localLog("--- End wpa_supplicant.conf Contents ---", true);
1440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (FileNotFoundException e) {
14427b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                localLog("Could not open " + SUPPLICANT_CONFIG_FILE + ", " + e, true);
1443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (IOException e) {
14447b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                localLog("Could not read " + SUPPLICANT_CONFIG_FILE + ", " + e, true);
1445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } finally {
1446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                try {
1447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (reader != null) {
1448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        reader.close();
1449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
1450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } catch (IOException e) {
1451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // Just ignore the fact that we couldn't close
1452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
14579878c61bbd81176561991be025af44efc67332feWenchao Tong    private Map<String, String> readNetworkVariablesFromSupplicantFile(String key) {
14589878c61bbd81176561991be025af44efc67332feWenchao Tong        Map<String, String> result = new HashMap<>();
1459f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        BufferedReader reader = null;
14609878c61bbd81176561991be025af44efc67332feWenchao Tong        if (VDBG) loge("readNetworkVariablesFromSupplicantFile key=" + key);
1461f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1462f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        try {
1463f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            reader = new BufferedReader(new FileReader(SUPPLICANT_CONFIG_FILE));
1464f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            boolean found = false;
14659878c61bbd81176561991be025af44efc67332feWenchao Tong            String networkSsid = null;
14669878c61bbd81176561991be025af44efc67332feWenchao Tong            String value = null;
14679878c61bbd81176561991be025af44efc67332feWenchao Tong
1468f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            for (String line = reader.readLine(); line != null; line = reader.readLine()) {
1469f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1470f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (line.matches("[ \\t]*network=\\{")) {
1471f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    found = true;
14729878c61bbd81176561991be025af44efc67332feWenchao Tong                    networkSsid = null;
14739878c61bbd81176561991be025af44efc67332feWenchao Tong                    value = null;
1474f2899b9c94fd1abbd01625dfe320dd3f1bc9cc9cWenchao Tong                } else if (line.matches("[ \\t]*\\}")) {
1475f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    found = false;
14769878c61bbd81176561991be025af44efc67332feWenchao Tong                    networkSsid = null;
14779878c61bbd81176561991be025af44efc67332feWenchao Tong                    value = null;
1478f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
1479f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1480f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (found) {
1481f2899b9c94fd1abbd01625dfe320dd3f1bc9cc9cWenchao Tong                    String trimmedLine = line.trim();
1482f2899b9c94fd1abbd01625dfe320dd3f1bc9cc9cWenchao Tong                    if (trimmedLine.startsWith("ssid=")) {
1483f2899b9c94fd1abbd01625dfe320dd3f1bc9cc9cWenchao Tong                        networkSsid = trimmedLine.substring(5);
1484f2899b9c94fd1abbd01625dfe320dd3f1bc9cc9cWenchao Tong                    } else if (trimmedLine.startsWith(key + "=")) {
1485f2899b9c94fd1abbd01625dfe320dd3f1bc9cc9cWenchao Tong                        value = trimmedLine.substring(key.length() + 1);
1486f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
1487f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
14889878c61bbd81176561991be025af44efc67332feWenchao Tong                    if (networkSsid != null && value != null) {
14899878c61bbd81176561991be025af44efc67332feWenchao Tong                        result.put(networkSsid, value);
1490f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
1491f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
1492f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1493f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } catch (FileNotFoundException e) {
1494f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG) loge("Could not open " + SUPPLICANT_CONFIG_FILE + ", " + e);
1495f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } catch (IOException e) {
1496f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG) loge("Could not read " + SUPPLICANT_CONFIG_FILE + ", " + e);
1497f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } finally {
1498f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            try {
1499f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (reader != null) {
1500f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    reader.close();
1501f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
1502f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } catch (IOException e) {
1503f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                // Just ignore the fact that we couldn't close
1504f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1505f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1506f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
15079878c61bbd81176561991be025af44efc67332feWenchao Tong        return result;
15089878c61bbd81176561991be025af44efc67332feWenchao Tong    }
15099878c61bbd81176561991be025af44efc67332feWenchao Tong
15109878c61bbd81176561991be025af44efc67332feWenchao Tong    private String readNetworkVariableFromSupplicantFile(String ssid, String key) {
15119878c61bbd81176561991be025af44efc67332feWenchao Tong        Map<String, String> data = readNetworkVariablesFromSupplicantFile(key);
15129878c61bbd81176561991be025af44efc67332feWenchao Tong        if (VDBG) loge("readNetworkVariableFromSupplicantFile ssid=[" + ssid + "] key=" + key);
15139878c61bbd81176561991be025af44efc67332feWenchao Tong        return data.get(ssid);
1514f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
1515f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Mark all networks except specified netId as disabled */
1517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void markAllNetworksDisabledExcept(int netId) {
1518155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for(WifiConfiguration config : mConfiguredNetworks.values()) {
1519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if(config != null && config.networkId != netId) {
1520155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (config.status != Status.DISABLED) {
1521155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    config.status = Status.DISABLED;
1522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    config.disableReason = WifiConfiguration.DISABLED_UNKNOWN_REASON;
1523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1528155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void markAllNetworksDisabled() {
1529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        markAllNetworksDisabledExcept(INVALID_NETWORK_ID);
1530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1532155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    boolean needsUnlockedKeyStore() {
1533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Any network using certificates to authenticate access requires
1535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // unlocked key store; unless the certificates can be stored with
1536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // hardware encryption
1537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for(WifiConfiguration config : mConfiguredNetworks.values()) {
1539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP)
1541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    && config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {
1542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (needsSoftwareBackedKeyStore(config.enterpriseConfig)) {
1544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return true;
1545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
1546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
1547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
1548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
1550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
1551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
1552f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    public void writeKnownNetworkHistory() {
1553e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        boolean needUpdate = false;
1554f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1555f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        /* Make a copy */
15563ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim        final List<WifiConfiguration> networks = new ArrayList<WifiConfiguration>();
1557f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        for (WifiConfiguration config : mConfiguredNetworks.values()) {
1558f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            networks.add(new WifiConfiguration(config));
1559e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            if (config.dirty == true) {
1560e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                config.dirty = false;
1561e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                needUpdate = true;
1562e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            }
1563e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        }
1564e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        if (VDBG) {
1565e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            loge(" writeKnownNetworkHistory() num networks:" +
1566e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    mConfiguredNetworks.size() + " needWrite=" + needUpdate);
1567e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        }
1568e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        if (needUpdate == false) {
1569e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            return;
1570f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
15713ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim        mWriter.write(networkHistoryConfigFile, new DelayedDiskWrite.Writer() {
15723ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim            public void onWriteCalled(DataOutputStream out) throws IOException {
15733ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                for (WifiConfiguration config : networks) {
15743ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                    //loge("onWriteCalled write SSID: " + config.SSID);
15753ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                   /* if (config.getLinkProperties() != null)
15763ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                        loge(" lp " + config.getLinkProperties().toString());
15773ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                    else
15783ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                        loge("attempt config w/o lp");
15793ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                    */
15803ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim
1581ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                    if (VDBG) {
15820888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        int num = 0;
1583be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                        int numlink = 0;
15840888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        if (config.connectChoices != null) {
15850888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            num = config.connectChoices.size();
15860888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        }
1587be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                        if (config.linkedConfigurations != null) {
1588be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                            numlink = config.linkedConfigurations.size();
1589be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                        }
1590453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        loge("saving network history: " + config.configKey()  + " gw: " +
15917b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                                config.defaultGwMacAddress + " autojoin-status: " +
15920888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                                config.autoJoinStatus + " ephemeral=" + config.ephemeral
1593be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                                + " choices:" + Integer.toString(num)
15947b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                                + " link:" + Integer.toString(numlink)
15957b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                                + " status:" + Integer.toString(config.status)
15967b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                                + " nid:" + Integer.toString(config.networkId));
1597f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
15983ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim
1599e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle                    if (config.isValid() == false)
1600e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle                        continue;
1601e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle
1602e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle                    if (config.SSID == null) {
1603e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle                        if (VDBG) {
1604e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle                            loge("writeKnownNetworkHistory trying to write config with null SSID");
1605e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle                        }
1606e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle                        continue;
1607e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle                    }
1608be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                    if (VDBG) {
1609be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                        loge("writeKnownNetworkHistory write config " + config.configKey());
1610be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                    }
16110888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    out.writeUTF(CONFIG_KEY + config.configKey() + SEPARATOR_KEY);
16120888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle
16130888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    out.writeUTF(SSID_KEY + config.SSID + SEPARATOR_KEY);
16142451dbcc4f9641df188326215b204b798eb70c46vandwalle                    out.writeUTF(FQDN_KEY + config.FQDN + SEPARATOR_KEY);
16150888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle
16160888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    out.writeUTF(PRIORITY_KEY + Integer.toString(config.priority) + SEPARATOR_KEY);
16170888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    out.writeUTF(STATUS_KEY + Integer.toString(config.autoJoinStatus)
16180888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            + SEPARATOR_KEY);
1619ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle                    out.writeUTF(SUPPLICANT_STATUS_KEY + Integer.toString(config.status)
1620ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle                            + SEPARATOR_KEY);
1621ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle                    out.writeUTF(SUPPLICANT_DISABLE_REASON_KEY
1622ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle                            + Integer.toString(config.disableReason)
1623ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle                            + SEPARATOR_KEY);
16240888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    out.writeUTF(NETWORK_ID_KEY + Integer.toString(config.networkId)
16250888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            + SEPARATOR_KEY);
16260888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    out.writeUTF(SELF_ADDED_KEY + Boolean.toString(config.selfAdded)
16270888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            + SEPARATOR_KEY);
1628992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    out.writeUTF(DID_SELF_ADD_KEY + Boolean.toString(config.didSelfAdd)
1629992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                            + SEPARATOR_KEY);
16309f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    out.writeUTF(NO_INTERNET_ACCESS_KEY
16319f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                            + Boolean.toString(config.noInternetAccess)
16329f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                            + SEPARATOR_KEY);
1633b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    out.writeUTF(EPHEMERAL_KEY
1634b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + Boolean.toString(config.ephemeral)
1635b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + SEPARATOR_KEY);
16360e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                    if (config.peerWifiConfiguration != null) {
16370e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                        out.writeUTF(PEER_CONFIGURATION_KEY + config.peerWifiConfiguration
16380e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                                + SEPARATOR_KEY);
16390e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                    }
16402451dbcc4f9641df188326215b204b798eb70c46vandwalle                    out.writeUTF(NUM_CONNECTION_FAILURES_KEY
16412451dbcc4f9641df188326215b204b798eb70c46vandwalle                            + Integer.toString(config.numConnectionFailures)
16422451dbcc4f9641df188326215b204b798eb70c46vandwalle                            + SEPARATOR_KEY);
1643e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    out.writeUTF(NUM_AUTH_FAILURES_KEY
1644e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            + Integer.toString(config.numAuthFailures)
1645e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            + SEPARATOR_KEY);
1646e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    out.writeUTF(NUM_IP_CONFIG_FAILURES_KEY
1647e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            + Integer.toString(config.numIpConfigFailures)
1648e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            + SEPARATOR_KEY);
16492451dbcc4f9641df188326215b204b798eb70c46vandwalle                    out.writeUTF(SCORER_OVERRIDE_KEY + Integer.toString(config.numScorerOverride)
16502451dbcc4f9641df188326215b204b798eb70c46vandwalle                            + SEPARATOR_KEY);
16512451dbcc4f9641df188326215b204b798eb70c46vandwalle                    out.writeUTF(SCORER_OVERRIDE_AND_SWITCH_KEY
16522451dbcc4f9641df188326215b204b798eb70c46vandwalle                            + Integer.toString(config.numScorerOverrideAndSwitchedNetwork)
16532451dbcc4f9641df188326215b204b798eb70c46vandwalle                            + SEPARATOR_KEY);
1654c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    out.writeUTF(NUM_ASSOCIATION_KEY
1655c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            + Integer.toString(config.numAssociation)
1656c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            + SEPARATOR_KEY);
1657dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                    out.writeUTF(JOIN_ATTEMPT_BOOST_KEY
1658dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                            + Integer.toString(config.autoJoinUseAggressiveJoinAttemptThreshold)
1659dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                            + SEPARATOR_KEY);
1660e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    //out.writeUTF(BLACKLIST_MILLI_KEY + Long.toString(config.blackListTimestamp)
1661e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    //        + SEPARATOR_KEY);
1662992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    out.writeUTF(CREATOR_UID_KEY + Integer.toString(config.creatorUid)
1663992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                            + SEPARATOR_KEY);
1664992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    out.writeUTF(CONNECT_UID_KEY + Integer.toString(config.lastConnectUid)
1665992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                            + SEPARATOR_KEY);
1666992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    out.writeUTF(UPDATE_UID_KEY + Integer.toString(config.lastUpdateUid)
1667992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                            + SEPARATOR_KEY);
16683ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                    String allowedKeyManagementString =
16693ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                            makeString(config.allowedKeyManagement,
16703ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                                    WifiConfiguration.KeyMgmt.strings);
16710888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    out.writeUTF(AUTH_KEY + allowedKeyManagementString + SEPARATOR_KEY);
16723ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim
16733ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                    if (config.connectChoices != null) {
16743ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                        for (String key : config.connectChoices.keySet()) {
16752451dbcc4f9641df188326215b204b798eb70c46vandwalle                            Integer choice = config.connectChoices.get(key);
16762451dbcc4f9641df188326215b204b798eb70c46vandwalle                            out.writeUTF(CHOICE_KEY + key + "="
16772451dbcc4f9641df188326215b204b798eb70c46vandwalle                                    + choice.toString() + SEPARATOR_KEY);
1678f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
16793ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                    }
16803ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                    if (config.linkedConfigurations != null) {
1681be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                        loge("writeKnownNetworkHistory write linked "
1682be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                                + config.linkedConfigurations.size());
1683be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle
16843ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                        for (String key : config.linkedConfigurations.keySet()) {
16850888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            out.writeUTF(LINK_KEY + key + SEPARATOR_KEY);
1686f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
16873ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                    }
1688f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
16893b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti                    String macAddress = config.defaultGwMacAddress;
16903b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti                    if (macAddress != null) {
16913b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti                        out.writeUTF(DEFAULT_GW_KEY + macAddress + SEPARATOR_KEY);
16923ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                    }
1693f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
16943ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                    if (config.scanResultCache != null) {
16953ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                        for (ScanResult result : config.scanResultCache.values()) {
16960888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            out.writeUTF(BSSID_KEY + result.BSSID + SEPARATOR_KEY);
1697f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
16980888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            out.writeUTF(FREQ_KEY + Integer.toString(result.frequency)
16990888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                                    + SEPARATOR_KEY);
1700f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
17010888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            out.writeUTF(RSSI_KEY + Integer.toString(result.level)
17020888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                                    + SEPARATOR_KEY);
1703f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
17040d616ef3bf635dff8722e064c0be842676390ed8vandwalle                            out.writeUTF(BSSID_STATUS_KEY
17050d616ef3bf635dff8722e064c0be842676390ed8vandwalle                                    + Integer.toString(result.autoJoinStatus)
170640ff222cec1bd05879edb53abc75c6deead734cavandwalle                                    + SEPARATOR_KEY);
170740ff222cec1bd05879edb53abc75c6deead734cavandwalle
1708e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            //if (result.seen != 0) {
1709e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            //    out.writeUTF(MILLI_KEY + Long.toString(result.seen)
1710e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            //            + SEPARATOR_KEY);
1711e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            //}
17120888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            out.writeUTF(BSSID_KEY_END + SEPARATOR_KEY);
1713b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim                        }
1714b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim                    }
17154bcdcfd19e3eaaacc54d8ec65149c6d7faa181afvandwalle                    if (config.lastFailure != null) {
17160888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        out.writeUTF(FAILURE_KEY + config.lastFailure + SEPARATOR_KEY);
17174bcdcfd19e3eaaacc54d8ec65149c6d7faa181afvandwalle                    }
17180888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    out.writeUTF(SEPARATOR_KEY);
171970468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    // Add extra blank lines for clarity
172070468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    out.writeUTF(SEPARATOR_KEY);
172170468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    out.writeUTF(SEPARATOR_KEY);
1722b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim                }
1723b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim            }
1724b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim
17253ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim        });
1726b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim    }
1727b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim
1728117be607246604e875de62aa8cdd99700b77a2b4vandwalle    public void setLastSelectedConfiguration(int netId) {
1729117be607246604e875de62aa8cdd99700b77a2b4vandwalle        if (DBG) {
1730117be607246604e875de62aa8cdd99700b77a2b4vandwalle            loge("setLastSelectedConfiguration " + Integer.toString(netId));
1731117be607246604e875de62aa8cdd99700b77a2b4vandwalle        }
1732117be607246604e875de62aa8cdd99700b77a2b4vandwalle        if (netId == WifiConfiguration.INVALID_NETWORK_ID) {
1733117be607246604e875de62aa8cdd99700b77a2b4vandwalle            lastSelectedConfiguration = null;
1734117be607246604e875de62aa8cdd99700b77a2b4vandwalle        } else {
1735117be607246604e875de62aa8cdd99700b77a2b4vandwalle            WifiConfiguration selected = getWifiConfiguration(netId);
1736117be607246604e875de62aa8cdd99700b77a2b4vandwalle            if (selected == null) {
1737117be607246604e875de62aa8cdd99700b77a2b4vandwalle                lastSelectedConfiguration = null;
1738117be607246604e875de62aa8cdd99700b77a2b4vandwalle            } else {
1739117be607246604e875de62aa8cdd99700b77a2b4vandwalle                lastSelectedConfiguration = selected.configKey();
1740e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                selected.numConnectionFailures = 0;
1741e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                selected.numIpConfigFailures = 0;
1742e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                selected.numAuthFailures = 0;
1743ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                if (VDBG) {
1744ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                    loge("setLastSelectedConfiguration now: " + lastSelectedConfiguration);
1745ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                }
1746117be607246604e875de62aa8cdd99700b77a2b4vandwalle            }
1747117be607246604e875de62aa8cdd99700b77a2b4vandwalle        }
1748117be607246604e875de62aa8cdd99700b77a2b4vandwalle    }
1749117be607246604e875de62aa8cdd99700b77a2b4vandwalle
1750117be607246604e875de62aa8cdd99700b77a2b4vandwalle    public String getLastSelectedConfiguration() {
1751117be607246604e875de62aa8cdd99700b77a2b4vandwalle        return lastSelectedConfiguration;
1752117be607246604e875de62aa8cdd99700b77a2b4vandwalle    }
1753117be607246604e875de62aa8cdd99700b77a2b4vandwalle
1754c9b047b4cb480edbb490516746cfcbb4a4eedeeavandwalle    public boolean isLastSelectedConfiguration(WifiConfiguration config) {
1755c9b047b4cb480edbb490516746cfcbb4a4eedeeavandwalle        return (lastSelectedConfiguration != null
1756c9b047b4cb480edbb490516746cfcbb4a4eedeeavandwalle                && config != null
1757c9b047b4cb480edbb490516746cfcbb4a4eedeeavandwalle                && lastSelectedConfiguration.equals(config.configKey()));
1758c9b047b4cb480edbb490516746cfcbb4a4eedeeavandwalle    }
1759c9b047b4cb480edbb490516746cfcbb4a4eedeeavandwalle
1760ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    private void readNetworkHistory() {
17617b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        if (showNetworks) {
17627b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            localLog("readNetworkHistory() path:" + networkHistoryConfigFile);
17630888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        }
1764f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        DataInputStream in = null;
1765f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        try {
1766f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            in = new DataInputStream(new BufferedInputStream(new FileInputStream(
1767f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    networkHistoryConfigFile)));
1768f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            WifiConfiguration config = null;
1769f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            while (true) {
1770f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                int id = -1;
1771f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                String key = in.readUTF();
1772f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                String bssid = null;
1773f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                String ssid = null;
1774f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1775f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                int freq = 0;
177640ff222cec1bd05879edb53abc75c6deead734cavandwalle                int status = 0;
1777f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                long seen = 0;
17780888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                int rssi = WifiConfiguration.INVALID_RSSI;
1779f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                String caps = null;
1780f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (key.startsWith(CONFIG_KEY)) {
1781f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1782f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (config != null) {
1783f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        config = null;
1784f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
1785f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    String configKey = key.replace(CONFIG_KEY, "");
17860888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    configKey = configKey.replace(SEPARATOR_KEY, "");
1787952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle                    // get the networkId for that config Key
1788952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle                    Integer n = mNetworkIds.get(configKey.hashCode());
1789952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle                    // skip reading that configuration data
1790952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle                    // since we don't have a corresponding network ID
17910888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    if (n == null) {
17927b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        localLog("readNetworkHistory didnt find netid for hash="
17930888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                                + Integer.toString(configKey.hashCode())
17940888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                                + " key: " + configKey);
17950888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        continue;
17960888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    }
1797952d359198cb1f04c180fc0f795dc316fe97cb5evandwalle                    config = mConfiguredNetworks.get(n);
17980888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    if (config == null) {
17997b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        localLog("readNetworkHistory didnt find config for netid="
18000888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                                + n.toString()
18010888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                                + " key: " + configKey);
18020888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    }
180340ff222cec1bd05879edb53abc75c6deead734cavandwalle                    status = 0;
1804f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    ssid = null;
1805f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    bssid = null;
1806f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    freq = 0;
1807f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    seen = 0;
18080888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    rssi = WifiConfiguration.INVALID_RSSI;
1809f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    caps = null;
1810f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1811f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                } else if (config != null) {
1812f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (key.startsWith(SSID_KEY)) {
1813f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        ssid = key.replace(SSID_KEY, "");
18140888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        ssid = ssid.replace(SEPARATOR_KEY, "");
18150888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        if (config.SSID != null && !config.SSID.equals(ssid)) {
18160888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            loge("Error parsing network history file, mismatched SSIDs");
1817f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            config = null; //error
1818f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            ssid = null;
18190888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        } else {
18200888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            config.SSID = ssid;
1821f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
1822f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
1823f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
18242451dbcc4f9641df188326215b204b798eb70c46vandwalle                    if (key.startsWith(FQDN_KEY)) {
18252451dbcc4f9641df188326215b204b798eb70c46vandwalle                        String fqdn = key.replace(FQDN_KEY, "");
18262451dbcc4f9641df188326215b204b798eb70c46vandwalle                        fqdn = fqdn.replace(SEPARATOR_KEY, "");
18272451dbcc4f9641df188326215b204b798eb70c46vandwalle                        config.FQDN = fqdn;
18282451dbcc4f9641df188326215b204b798eb70c46vandwalle                    }
18292451dbcc4f9641df188326215b204b798eb70c46vandwalle
1830f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (key.startsWith(DEFAULT_GW_KEY)) {
1831f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        String gateway = key.replace(DEFAULT_GW_KEY, "");
18320888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        gateway = gateway.replace(SEPARATOR_KEY, "");
1833f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        config.defaultGwMacAddress = gateway;
1834f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
1835f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1836f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (key.startsWith(STATUS_KEY)) {
183740ff222cec1bd05879edb53abc75c6deead734cavandwalle                        String st = key.replace(STATUS_KEY, "");
183840ff222cec1bd05879edb53abc75c6deead734cavandwalle                        st = st.replace(SEPARATOR_KEY, "");
183940ff222cec1bd05879edb53abc75c6deead734cavandwalle                        config.autoJoinStatus = Integer.parseInt(st);
1840f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
1841f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1842ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle                    if (key.startsWith(SUPPLICANT_DISABLE_REASON_KEY)) {
1843ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle                        String reason = key.replace(SUPPLICANT_DISABLE_REASON_KEY, "");
1844ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle                        reason = reason.replace(SEPARATOR_KEY, "");
1845ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle                        config.disableReason = Integer.parseInt(reason);
1846e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    }
1847ad7319939c424d42fa6a3791c47f613db8ef3cd8vandwalle
1848453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    if (key.startsWith(SELF_ADDED_KEY)) {
1849992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        String selfAdded = key.replace(SELF_ADDED_KEY, "");
1850992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        selfAdded = selfAdded.replace(SEPARATOR_KEY, "");
1851992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        config.selfAdded = Boolean.parseBoolean(selfAdded);
1852992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    }
1853992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle
1854992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    if (key.startsWith(DID_SELF_ADD_KEY)) {
1855992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        String didSelfAdd = key.replace(DID_SELF_ADD_KEY, "");
1856992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        didSelfAdd = didSelfAdd.replace(SEPARATOR_KEY, "");
1857992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        config.didSelfAdd = Boolean.parseBoolean(didSelfAdd);
1858992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    }
1859992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle
18609f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    if (key.startsWith(NO_INTERNET_ACCESS_KEY)) {
18619f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        String access = key.replace(NO_INTERNET_ACCESS_KEY, "");
18629f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        access = access.replace(SEPARATOR_KEY, "");
18639f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        config.noInternetAccess = Boolean.parseBoolean(access);
18649f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    }
18659f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
1866b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    if (key.startsWith(EPHEMERAL_KEY)) {
1867b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                        String access = key.replace(EPHEMERAL_KEY, "");
1868b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                        access = access.replace(SEPARATOR_KEY, "");
1869b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                        config.ephemeral = Boolean.parseBoolean(access);
1870b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    }
1871b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle
1872992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    if (key.startsWith(CREATOR_UID_KEY)) {
1873992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        String uid = key.replace(CREATOR_UID_KEY, "");
1874992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        uid = uid.replace(SEPARATOR_KEY, "");
1875992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        config.creatorUid = Integer.parseInt(uid);
1876992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    }
1877992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle
187827355a942653264388e909a4276196ee63e57811vandwalle                    if (key.startsWith(BLACKLIST_MILLI_KEY)) {
187927355a942653264388e909a4276196ee63e57811vandwalle                        String milli = key.replace(BLACKLIST_MILLI_KEY, "");
188027355a942653264388e909a4276196ee63e57811vandwalle                        milli = milli.replace(SEPARATOR_KEY, "");
188127355a942653264388e909a4276196ee63e57811vandwalle                        config.blackListTimestamp = Long.parseLong(milli);
188227355a942653264388e909a4276196ee63e57811vandwalle                    }
188327355a942653264388e909a4276196ee63e57811vandwalle
18842451dbcc4f9641df188326215b204b798eb70c46vandwalle                    if (key.startsWith(NUM_CONNECTION_FAILURES_KEY)) {
18852451dbcc4f9641df188326215b204b798eb70c46vandwalle                        String num = key.replace(NUM_CONNECTION_FAILURES_KEY, "");
18862451dbcc4f9641df188326215b204b798eb70c46vandwalle                        num = num.replace(SEPARATOR_KEY, "");
18872451dbcc4f9641df188326215b204b798eb70c46vandwalle                        config.numConnectionFailures = Integer.parseInt(num);
18882451dbcc4f9641df188326215b204b798eb70c46vandwalle                    }
18892451dbcc4f9641df188326215b204b798eb70c46vandwalle
1890e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    if (key.startsWith(NUM_IP_CONFIG_FAILURES_KEY)) {
1891e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        String num = key.replace(NUM_IP_CONFIG_FAILURES_KEY, "");
1892e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        num = num.replace(SEPARATOR_KEY, "");
1893e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        config.numIpConfigFailures = Integer.parseInt(num);
1894e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    }
1895e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
1896e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    if (key.startsWith(NUM_AUTH_FAILURES_KEY)) {
1897e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        String num = key.replace(NUM_AUTH_FAILURES_KEY, "");
1898e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        num = num.replace(SEPARATOR_KEY, "");
1899e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        config.numIpConfigFailures = Integer.parseInt(num);
1900e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    }
1901e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
19022451dbcc4f9641df188326215b204b798eb70c46vandwalle                    if (key.startsWith(SCORER_OVERRIDE_KEY)) {
19032451dbcc4f9641df188326215b204b798eb70c46vandwalle                        String num = key.replace(SCORER_OVERRIDE_KEY, "");
19042451dbcc4f9641df188326215b204b798eb70c46vandwalle                        num = num.replace(SEPARATOR_KEY, "");
19052451dbcc4f9641df188326215b204b798eb70c46vandwalle                        config.numScorerOverride = Integer.parseInt(num);
19062451dbcc4f9641df188326215b204b798eb70c46vandwalle                    }
19072451dbcc4f9641df188326215b204b798eb70c46vandwalle
19082451dbcc4f9641df188326215b204b798eb70c46vandwalle                    if (key.startsWith(SCORER_OVERRIDE_AND_SWITCH_KEY)) {
19092451dbcc4f9641df188326215b204b798eb70c46vandwalle                        String num = key.replace(SCORER_OVERRIDE_AND_SWITCH_KEY, "");
19102451dbcc4f9641df188326215b204b798eb70c46vandwalle                        num = num.replace(SEPARATOR_KEY, "");
19112451dbcc4f9641df188326215b204b798eb70c46vandwalle                        config.numScorerOverrideAndSwitchedNetwork = Integer.parseInt(num);
19122451dbcc4f9641df188326215b204b798eb70c46vandwalle                    }
19132451dbcc4f9641df188326215b204b798eb70c46vandwalle
1914c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    if (key.startsWith(NUM_ASSOCIATION_KEY)) {
1915c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        String num = key.replace(NUM_ASSOCIATION_KEY, "");
1916c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        num = num.replace(SEPARATOR_KEY, "");
1917c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        config.numAssociation = Integer.parseInt(num);
1918c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    }
1919c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
1920dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                    if (key.startsWith(JOIN_ATTEMPT_BOOST_KEY)) {
1921dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                        String num = key.replace(JOIN_ATTEMPT_BOOST_KEY, "");
1922dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                        num = num.replace(SEPARATOR_KEY, "");
1923dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                        config.autoJoinUseAggressiveJoinAttemptThreshold = Integer.parseInt(num);
1924dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                    }
1925dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle
1926992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    if (key.startsWith(CONNECT_UID_KEY)) {
1927992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        String uid = key.replace(CONNECT_UID_KEY, "");
1928992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        uid = uid.replace(SEPARATOR_KEY, "");
1929992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        config.lastConnectUid = Integer.parseInt(uid);
1930992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    }
1931992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle
1932992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    if (key.startsWith(UPDATE_UID_KEY)) {
1933992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        String uid = key.replace(UPDATE_UID_KEY, "");
1934992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        uid = uid.replace(SEPARATOR_KEY, "");
1935992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        config.lastUpdateUid = Integer.parseInt(uid);
1936453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    }
1937453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle
19384bcdcfd19e3eaaacc54d8ec65149c6d7faa181afvandwalle                    if (key.startsWith(FAILURE_KEY)) {
19394bcdcfd19e3eaaacc54d8ec65149c6d7faa181afvandwalle                        config.lastFailure = key.replace(FAILURE_KEY, "");
19400888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        config.lastFailure = config.lastFailure.replace(SEPARATOR_KEY, "");
19414bcdcfd19e3eaaacc54d8ec65149c6d7faa181afvandwalle                    }
19424bcdcfd19e3eaaacc54d8ec65149c6d7faa181afvandwalle
19430e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                    if (key.startsWith(PEER_CONFIGURATION_KEY)) {
19440e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                        config.peerWifiConfiguration = key.replace(PEER_CONFIGURATION_KEY, "");
19450e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                        config.peerWifiConfiguration =
19460e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                                config.peerWifiConfiguration.replace(SEPARATOR_KEY, "");
19470e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                    }
19480e3c67280365eb82fc15362a3b98a2c978bde766vandwalle
1949f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (key.startsWith(CHOICE_KEY)) {
19502451dbcc4f9641df188326215b204b798eb70c46vandwalle                        String choiceStr = key.replace(CHOICE_KEY, "");
19512451dbcc4f9641df188326215b204b798eb70c46vandwalle                        choiceStr = choiceStr.replace(SEPARATOR_KEY, "");
19522451dbcc4f9641df188326215b204b798eb70c46vandwalle                        String configKey = "";
19532451dbcc4f9641df188326215b204b798eb70c46vandwalle                        int choice = 0;
19542451dbcc4f9641df188326215b204b798eb70c46vandwalle                        Matcher match = mConnectChoice.matcher(choiceStr);
19552451dbcc4f9641df188326215b204b798eb70c46vandwalle                        if (!match.find()) {
19562451dbcc4f9641df188326215b204b798eb70c46vandwalle                            if (DBG) Log.d(TAG, "WifiConfigStore: connectChoice: " +
19572451dbcc4f9641df188326215b204b798eb70c46vandwalle                                    " Couldnt match pattern : " + choiceStr);
19582451dbcc4f9641df188326215b204b798eb70c46vandwalle                        } else {
19592451dbcc4f9641df188326215b204b798eb70c46vandwalle                            configKey = match.group(1);
19602451dbcc4f9641df188326215b204b798eb70c46vandwalle                            try {
19612451dbcc4f9641df188326215b204b798eb70c46vandwalle                                choice = Integer.parseInt(match.group(2));
19622451dbcc4f9641df188326215b204b798eb70c46vandwalle                            } catch (NumberFormatException e) {
19632451dbcc4f9641df188326215b204b798eb70c46vandwalle                                choice = 0;
19642451dbcc4f9641df188326215b204b798eb70c46vandwalle                            }
19652451dbcc4f9641df188326215b204b798eb70c46vandwalle                            if (choice > 0) {
19662451dbcc4f9641df188326215b204b798eb70c46vandwalle                                if (config.connectChoices == null) {
19672451dbcc4f9641df188326215b204b798eb70c46vandwalle                                    config.connectChoices = new HashMap<String, Integer>();
19682451dbcc4f9641df188326215b204b798eb70c46vandwalle                                }
19692451dbcc4f9641df188326215b204b798eb70c46vandwalle                                config.connectChoices.put(configKey, choice);
19702451dbcc4f9641df188326215b204b798eb70c46vandwalle                            }
1971f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
1972f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
1973f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1974f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (key.startsWith(LINK_KEY)) {
1975f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        String configKey = key.replace(LINK_KEY, "");
19760888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        configKey = configKey.replace(SEPARATOR_KEY, "");
1977f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        if (config.linkedConfigurations == null) {
1978f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            config.linkedConfigurations = new HashMap<String, Integer>();
1979f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
1980f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        if (config.linkedConfigurations != null) {
1981f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            config.linkedConfigurations.put(configKey, -1);
1982f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
1983f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
1984f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1985f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (key.startsWith(BSSID_KEY)) {
1986f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        if (key.startsWith(BSSID_KEY)) {
1987f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            bssid = key.replace(BSSID_KEY, "");
19880888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            bssid = bssid.replace(SEPARATOR_KEY, "");
1989f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            freq = 0;
1990f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            seen = 0;
19910888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            rssi = WifiConfiguration.INVALID_RSSI;
1992f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            caps = "";
199340ff222cec1bd05879edb53abc75c6deead734cavandwalle                            status = 0;
1994f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
1995f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1996f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        if (key.startsWith(RSSI_KEY)) {
1997f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            String lvl = key.replace(RSSI_KEY, "");
19980888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            lvl = lvl.replace(SEPARATOR_KEY, "");
19990888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            rssi = Integer.parseInt(lvl);
2000f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
20010888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle
200240ff222cec1bd05879edb53abc75c6deead734cavandwalle                        if (key.startsWith(BSSID_STATUS_KEY)) {
200340ff222cec1bd05879edb53abc75c6deead734cavandwalle                            String st = key.replace(BSSID_STATUS_KEY, "");
200440ff222cec1bd05879edb53abc75c6deead734cavandwalle                            st = st.replace(SEPARATOR_KEY, "");
200540ff222cec1bd05879edb53abc75c6deead734cavandwalle                            status = Integer.parseInt(st);
200640ff222cec1bd05879edb53abc75c6deead734cavandwalle                        }
200740ff222cec1bd05879edb53abc75c6deead734cavandwalle
2008f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        if (key.startsWith(FREQ_KEY)) {
20090888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            String channel = key.replace(FREQ_KEY, "");
20100888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            channel = channel.replace(SEPARATOR_KEY, "");
20110888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            freq = Integer.parseInt(channel);
2012f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
20130888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle
2014f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        if (key.startsWith(DATE_KEY)) {
2015f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        /*
2016f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                         * when reading the configuration from file we don't update the date
2017f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                         * so as to avoid reading back stale or non-sensical data that would
2018f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                         * depend on network time.
2019f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                         * The date of a WifiConfiguration should only come from actual scan result.
2020f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                         *
2021f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        String s = key.replace(FREQ_KEY, "");
2022f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        seen = Integer.getInteger(s);
2023f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        */
2024f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
2025f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
2026f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        if (key.startsWith(BSSID_KEY_END)) {
2027f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
2028f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            if ((bssid != null) && (ssid != null)) {
2029f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
2030f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                if (config.scanResultCache == null) {
2031f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                    config.scanResultCache = new HashMap<String, ScanResult>();
2032f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                }
2033f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                WifiSsid wssid = WifiSsid.createFromAsciiEncoded(ssid);
2034f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                ScanResult result = new ScanResult(wssid, bssid,
2035f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                        caps, rssi, freq, (long) 0);
2036f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                result.seen = seen;
2037f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                config.scanResultCache.put(bssid, result);
20380d616ef3bf635dff8722e064c0be842676390ed8vandwalle                                result.autoJoinStatus = status;
2039f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            }
2040f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
2041f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
2042f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
2043f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
2044f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } catch (EOFException ignore) {
2045f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (in != null) {
2046f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                try {
2047f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    in.close();
2048f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                } catch (Exception e) {
20490888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    loge("readNetworkHistory: Error reading file" + e);
2050f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
2051f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
2052f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } catch (IOException e) {
20538c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            loge("readNetworkHistory: No config file, revert to default" + e);
2054f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
2055f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
2056f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if(in!=null) {
2057f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            try {
2058f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                in.close();
2059f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } catch (Exception e) {
20600888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                loge("readNetworkHistory: Error closing file" + e);
2061f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
2062f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
2063f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
2064f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
20658c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    private void readAutoJoinConfig() {
20668c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle        BufferedReader reader = null;
20678c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle        try {
20688c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
20698c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            reader = new BufferedReader(new FileReader(autoJoinConfigFile));
20708c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
20718c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            for (String key = reader.readLine(); key != null; key = reader.readLine()) {
20728c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key != null) {
20738c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    Log.d(TAG, "readAutoJoinConfig line: " + key);
20748c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
207570468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                if (key.startsWith(ENABLE_AUTO_JOIN_WHILE_ASSOCIATED_KEY)) {
207670468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    String st = key.replace(ENABLE_AUTO_JOIN_WHILE_ASSOCIATED_KEY, "");
20778c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
20788c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
207970468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                        enableAutoJoinWhenAssociated = Integer.parseInt(st) != 0;
208070468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                        Log.d(TAG,"readAutoJoinConfig: enabled = " + enableAutoJoinWhenAssociated);
20818c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
20828c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
20838c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
20848c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
20858c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
2086e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                if (key.startsWith(ENABLE_FULL_BAND_SCAN_WHEN_ASSOCIATED_KEY)) {
2087e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                    String st = key.replace(ENABLE_FULL_BAND_SCAN_WHEN_ASSOCIATED_KEY, "");
2088e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                    st = st.replace(SEPARATOR_KEY, "");
2089e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                    try {
2090e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                        enableFullBandScanWhenAssociated = Integer.parseInt(st) != 0;
2091e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                        Log.d(TAG,"readAutoJoinConfig: enableFullBandScanWhenAssociated = "
2092e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                                + enableFullBandScanWhenAssociated);
2093e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                    } catch (NumberFormatException e) {
2094e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
2095e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                    }
2096e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                }
2097e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle
209870468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                if (key.startsWith(ENABLE_AUTO_JOIN_SCAN_WHILE_ASSOCIATED_KEY)) {
209970468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    String st = key.replace(ENABLE_AUTO_JOIN_SCAN_WHILE_ASSOCIATED_KEY, "");
2100448c9536a302c58a79e271b1721c08b8882f800evandwalle                    st = st.replace(SEPARATOR_KEY, "");
2101448c9536a302c58a79e271b1721c08b8882f800evandwalle                    try {
210270468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                        enableAutoJoinScanWhenAssociated = Integer.parseInt(st) != 0;
2103448c9536a302c58a79e271b1721c08b8882f800evandwalle                        Log.d(TAG,"readAutoJoinConfig: enabled = "
210470468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                                + enableAutoJoinScanWhenAssociated);
210570468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    } catch (NumberFormatException e) {
210670468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
210770468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    }
210870468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                }
210970468b47454c8657e8963932f2e08a3f4d7e3881vandwalle
211070468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                if (key.startsWith(ENABLE_CHIP_WAKE_UP_WHILE_ASSOCIATED_KEY)) {
211170468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    String st = key.replace(ENABLE_CHIP_WAKE_UP_WHILE_ASSOCIATED_KEY, "");
211270468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    st = st.replace(SEPARATOR_KEY, "");
211370468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    try {
211470468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                        enableChipWakeUpWhenAssociated = Integer.parseInt(st) != 0;
211570468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                        Log.d(TAG,"readAutoJoinConfig: enabled = "
211670468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                                + enableChipWakeUpWhenAssociated);
211770468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    } catch (NumberFormatException e) {
211870468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
211970468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    }
212070468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                }
212170468b47454c8657e8963932f2e08a3f4d7e3881vandwalle
212270468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                if (key.startsWith(ENABLE_RSSI_POLL_WHILE_ASSOCIATED_KEY)) {
212370468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    String st = key.replace(ENABLE_RSSI_POLL_WHILE_ASSOCIATED_KEY, "");
212470468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    st = st.replace(SEPARATOR_KEY, "");
212570468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    try {
212670468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                        enableRssiPollWhenAssociated = Integer.parseInt(st) != 0;
212770468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                        Log.d(TAG,"readAutoJoinConfig: enabled = "
212870468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                                + enableRssiPollWhenAssociated);
2129448c9536a302c58a79e271b1721c08b8882f800evandwalle                    } catch (NumberFormatException e) {
2130448c9536a302c58a79e271b1721c08b8882f800evandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
2131448c9536a302c58a79e271b1721c08b8882f800evandwalle                    }
2132448c9536a302c58a79e271b1721c08b8882f800evandwalle                }
2133448c9536a302c58a79e271b1721c08b8882f800evandwalle
21348c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(THRESHOLD_INITIAL_AUTO_JOIN_ATTEMPT_RSSI_MIN_5G_KEY)) {
21358c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st =
21368c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            key.replace(THRESHOLD_INITIAL_AUTO_JOIN_ATTEMPT_RSSI_MIN_5G_KEY, "");
21378c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
21388c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
21398c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        thresholdInitialAutoJoinAttemptMin5RSSI = Integer.parseInt(st);
21408c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: thresholdInitialAutoJoinAttemptMin5RSSI = "
21418c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                                + Integer.toString(thresholdInitialAutoJoinAttemptMin5RSSI));
21428c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
21438c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
21448c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
21458c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
21468c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
21478c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(THRESHOLD_INITIAL_AUTO_JOIN_ATTEMPT_RSSI_MIN_24G_KEY)) {
21488c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st =
21498c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            key.replace(THRESHOLD_INITIAL_AUTO_JOIN_ATTEMPT_RSSI_MIN_24G_KEY, "");
21508c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
21518c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
21528c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        thresholdInitialAutoJoinAttemptMin24RSSI = Integer.parseInt(st);
21538c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: thresholdInitialAutoJoinAttemptMin24RSSI = "
21548c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                                + Integer.toString(thresholdInitialAutoJoinAttemptMin24RSSI));
21558c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
21568c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
21578c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
21588c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
21598c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
21608c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(THRESHOLD_UNBLACKLIST_HARD_5G_KEY)) {
21618c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st = key.replace(THRESHOLD_UNBLACKLIST_HARD_5G_KEY, "");
21628c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
21638c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
21648c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        thresholdUnblacklistThreshold5Hard = Integer.parseInt(st);
21658c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: thresholdUnblacklistThreshold5Hard = "
21668c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            + Integer.toString(thresholdUnblacklistThreshold5Hard));
21678c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
21688c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
21698c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
21708c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
21718c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(THRESHOLD_UNBLACKLIST_SOFT_5G_KEY)) {
21728c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st = key.replace(THRESHOLD_UNBLACKLIST_SOFT_5G_KEY, "");
21738c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
21748c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
21758c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        thresholdUnblacklistThreshold5Soft = Integer.parseInt(st);
21768c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: thresholdUnblacklistThreshold5Soft = "
21778c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            + Integer.toString(thresholdUnblacklistThreshold5Soft));
21788c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
21798c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
21808c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
21818c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
21828c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(THRESHOLD_UNBLACKLIST_HARD_24G_KEY)) {
21838c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st = key.replace(THRESHOLD_UNBLACKLIST_HARD_24G_KEY, "");
21848c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
21858c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
21868c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        thresholdUnblacklistThreshold24Hard = Integer.parseInt(st);
21878c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: thresholdUnblacklistThreshold24Hard = "
21888c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            + Integer.toString(thresholdUnblacklistThreshold24Hard));
21898c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
21908c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
21918c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
21928c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
21938c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(THRESHOLD_UNBLACKLIST_SOFT_24G_KEY)) {
21948c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st = key.replace(THRESHOLD_UNBLACKLIST_SOFT_24G_KEY, "");
21958c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
21968c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
21978c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        thresholdUnblacklistThreshold24Soft = Integer.parseInt(st);
21988c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: thresholdUnblacklistThreshold24Soft = "
21998c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            + Integer.toString(thresholdUnblacklistThreshold24Soft));
22008c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
22018c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
22028c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
22038c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
22048c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
22058c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(THRESHOLD_GOOD_RSSI_5_KEY)) {
22068c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st = key.replace(THRESHOLD_GOOD_RSSI_5_KEY, "");
22078c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
22088c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
22098c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        thresholdGoodRssi5 = Integer.parseInt(st);
22108c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: thresholdGoodRssi5 = "
22118c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            + Integer.toString(thresholdGoodRssi5));
22128c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
22138c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
22148c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
22158c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
22168c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(THRESHOLD_LOW_RSSI_5_KEY)) {
22178c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st = key.replace(THRESHOLD_LOW_RSSI_5_KEY, "");
22188c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
22198c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
22208c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        thresholdLowRssi5 = Integer.parseInt(st);
22218c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: thresholdLowRssi5 = "
22228c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            + Integer.toString(thresholdLowRssi5));
22238c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
22248c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
22258c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
22268c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
22278c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(THRESHOLD_BAD_RSSI_5_KEY)) {
22288c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st = key.replace(THRESHOLD_BAD_RSSI_5_KEY, "");
22298c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
22308c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
22318c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        thresholdBadRssi5 = Integer.parseInt(st);
22328c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: thresholdBadRssi5 = "
22338c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            + Integer.toString(thresholdBadRssi5));
22348c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
22358c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
22368c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
22378c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
22388c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
22398c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(THRESHOLD_GOOD_RSSI_24_KEY)) {
22408c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st = key.replace(THRESHOLD_GOOD_RSSI_24_KEY, "");
22418c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
22428c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
22438c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        thresholdGoodRssi24 = Integer.parseInt(st);
22448c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: thresholdGoodRssi24 = "
22458c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            + Integer.toString(thresholdGoodRssi24));
22468c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
22478c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
22488c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
22498c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
22508c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(THRESHOLD_LOW_RSSI_24_KEY)) {
22518c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st = key.replace(THRESHOLD_LOW_RSSI_24_KEY, "");
22528c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
22538c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
22548c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        thresholdLowRssi24 = Integer.parseInt(st);
22558c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: thresholdLowRssi24 = "
22568c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            + Integer.toString(thresholdLowRssi24));
22578c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
22588c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
22598c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
22608c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
22618c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(THRESHOLD_BAD_RSSI_24_KEY)) {
22628c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st = key.replace(THRESHOLD_BAD_RSSI_24_KEY, "");
22638c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
22648c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
22658c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        thresholdBadRssi24 = Integer.parseInt(st);
22668c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: thresholdBadRssi24 = "
22678c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            + Integer.toString(thresholdBadRssi24));
22688c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
22698c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
22708c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
22718c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
22728c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
22738c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(THRESHOLD_MAX_TX_PACKETS_FOR_NETWORK_SWITCHING_KEY)) {
22748c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st = key.replace(THRESHOLD_MAX_TX_PACKETS_FOR_NETWORK_SWITCHING_KEY, "");
22758c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
22768c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
22778c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        maxTxPacketForNetworkSwitching = Integer.parseInt(st);
22788c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: maxTxPacketForNetworkSwitching = "
22798c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            + Integer.toString(maxTxPacketForNetworkSwitching));
22808c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
22818c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
22828c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
22838c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
22848c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(THRESHOLD_MAX_RX_PACKETS_FOR_NETWORK_SWITCHING_KEY)) {
22858c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st = key.replace(THRESHOLD_MAX_RX_PACKETS_FOR_NETWORK_SWITCHING_KEY, "");
22868c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
22878c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
22888c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        maxRxPacketForNetworkSwitching = Integer.parseInt(st);
22898c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: maxRxPacketForNetworkSwitching = "
22908c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            + Integer.toString(maxRxPacketForNetworkSwitching));
22918c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
22928c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
22938c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
22948c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
22958c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
22969f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                if (key.startsWith(THRESHOLD_MAX_TX_PACKETS_FOR_FULL_SCANS_KEY)) {
22979f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    String st = key.replace(THRESHOLD_MAX_TX_PACKETS_FOR_FULL_SCANS_KEY, "");
22989f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    st = st.replace(SEPARATOR_KEY, "");
22999f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    try {
23009f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        maxTxPacketForNetworkSwitching = Integer.parseInt(st);
23019f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        Log.d(TAG,"readAutoJoinConfig: maxTxPacketForFullScans = "
23029f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                                + Integer.toString(maxTxPacketForFullScans));
23039f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    } catch (NumberFormatException e) {
23049f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
23059f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    }
23069f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                }
23079f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                if (key.startsWith(THRESHOLD_MAX_RX_PACKETS_FOR_FULL_SCANS_KEY)) {
23089f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    String st = key.replace(THRESHOLD_MAX_RX_PACKETS_FOR_FULL_SCANS_KEY, "");
23099f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    st = st.replace(SEPARATOR_KEY, "");
23109f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    try {
23119f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        maxRxPacketForNetworkSwitching = Integer.parseInt(st);
23129f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        Log.d(TAG,"readAutoJoinConfig: maxRxPacketForFullScans = "
23139f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                                + Integer.toString(maxRxPacketForFullScans));
23149f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    } catch (NumberFormatException e) {
23159f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
23169f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    }
23179f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                }
23189f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
23199f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                if (key.startsWith(THRESHOLD_MAX_TX_PACKETS_FOR_PARTIAL_SCANS_KEY)) {
23209f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    String st = key.replace(THRESHOLD_MAX_TX_PACKETS_FOR_PARTIAL_SCANS_KEY, "");
23219f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    st = st.replace(SEPARATOR_KEY, "");
23229f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    try {
23239f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        maxTxPacketForNetworkSwitching = Integer.parseInt(st);
23249f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        Log.d(TAG,"readAutoJoinConfig: maxTxPacketForPartialScans = "
23259f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                                + Integer.toString(maxTxPacketForPartialScans));
23269f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    } catch (NumberFormatException e) {
23279f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
23289f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    }
23299f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                }
23309f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                if (key.startsWith(THRESHOLD_MAX_RX_PACKETS_FOR_PARTIAL_SCANS_KEY)) {
23319f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    String st = key.replace(THRESHOLD_MAX_RX_PACKETS_FOR_PARTIAL_SCANS_KEY, "");
23329f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    st = st.replace(SEPARATOR_KEY, "");
23339f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    try {
23349f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        maxRxPacketForNetworkSwitching = Integer.parseInt(st);
23359f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        Log.d(TAG,"readAutoJoinConfig: maxRxPacketForPartialScans = "
23369f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                                + Integer.toString(maxRxPacketForPartialScans));
23379f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    } catch (NumberFormatException e) {
23389f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
23399f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    }
23409f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                }
23419f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
2342e67ec726c07410073575473c0f50dc737629f5davandwalle                if (key.startsWith(WIFI_VERBOSE_LOGS_KEY)) {
2343e67ec726c07410073575473c0f50dc737629f5davandwalle                    String st = key.replace(WIFI_VERBOSE_LOGS_KEY, "");
2344e67ec726c07410073575473c0f50dc737629f5davandwalle                    st = st.replace(SEPARATOR_KEY, "");
2345e67ec726c07410073575473c0f50dc737629f5davandwalle                    try {
2346e67ec726c07410073575473c0f50dc737629f5davandwalle                        enableVerboseLogging = Integer.parseInt(st);
2347e67ec726c07410073575473c0f50dc737629f5davandwalle                        Log.d(TAG,"readAutoJoinConfig: enable verbose logs = "
2348e67ec726c07410073575473c0f50dc737629f5davandwalle                                + Integer.toString(enableVerboseLogging));
2349e67ec726c07410073575473c0f50dc737629f5davandwalle                    } catch (NumberFormatException e) {
2350e67ec726c07410073575473c0f50dc737629f5davandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
2351e67ec726c07410073575473c0f50dc737629f5davandwalle                    }
2352e67ec726c07410073575473c0f50dc737629f5davandwalle                }
23538c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(A_BAND_PREFERENCE_RSSI_THRESHOLD_KEY)) {
23548c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st = key.replace(A_BAND_PREFERENCE_RSSI_THRESHOLD_KEY, "");
23558c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
23568c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
23570eebae7334d6129f7ca1344e4b20199794994358vandwalle                        bandPreferenceBoostThreshold5 = Integer.parseInt(st);
23580eebae7334d6129f7ca1344e4b20199794994358vandwalle                        Log.d(TAG,"readAutoJoinConfig: bandPreferenceBoostThreshold5 = "
23590eebae7334d6129f7ca1344e4b20199794994358vandwalle                            + Integer.toString(bandPreferenceBoostThreshold5));
23608c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
23618c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
23628c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
23638c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
2364e67ec726c07410073575473c0f50dc737629f5davandwalle                if (key.startsWith(ASSOCIATED_PARTIAL_SCAN_PERIOD_KEY)) {
2365e67ec726c07410073575473c0f50dc737629f5davandwalle                    String st = key.replace(ASSOCIATED_PARTIAL_SCAN_PERIOD_KEY, "");
2366e67ec726c07410073575473c0f50dc737629f5davandwalle                    st = st.replace(SEPARATOR_KEY, "");
2367e67ec726c07410073575473c0f50dc737629f5davandwalle                    try {
23680eebae7334d6129f7ca1344e4b20199794994358vandwalle                        associatedPartialScanPeriodMilli = Integer.parseInt(st);
2369e67ec726c07410073575473c0f50dc737629f5davandwalle                        Log.d(TAG,"readAutoJoinConfig: associatedScanPeriod = "
23700eebae7334d6129f7ca1344e4b20199794994358vandwalle                                + Integer.toString(associatedPartialScanPeriodMilli));
2371e67ec726c07410073575473c0f50dc737629f5davandwalle                    } catch (NumberFormatException e) {
2372e67ec726c07410073575473c0f50dc737629f5davandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
2373e67ec726c07410073575473c0f50dc737629f5davandwalle                    }
2374e67ec726c07410073575473c0f50dc737629f5davandwalle                }
2375e67ec726c07410073575473c0f50dc737629f5davandwalle                if (key.startsWith(ASSOCIATED_FULL_SCAN_BACKOFF_KEY)) {
2376e67ec726c07410073575473c0f50dc737629f5davandwalle                    String st = key.replace(ASSOCIATED_FULL_SCAN_BACKOFF_KEY, "");
2377e67ec726c07410073575473c0f50dc737629f5davandwalle                    st = st.replace(SEPARATOR_KEY, "");
2378e67ec726c07410073575473c0f50dc737629f5davandwalle                    try {
2379e67ec726c07410073575473c0f50dc737629f5davandwalle                        associatedFullScanBackoff = Integer.parseInt(st);
2380e67ec726c07410073575473c0f50dc737629f5davandwalle                        Log.d(TAG,"readAutoJoinConfig: associatedFullScanBackoff = "
2381e67ec726c07410073575473c0f50dc737629f5davandwalle                                + Integer.toString(associatedFullScanBackoff));
2382e67ec726c07410073575473c0f50dc737629f5davandwalle                    } catch (NumberFormatException e) {
2383e67ec726c07410073575473c0f50dc737629f5davandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
2384e67ec726c07410073575473c0f50dc737629f5davandwalle                    }
2385e67ec726c07410073575473c0f50dc737629f5davandwalle                }
23868c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                if (key.startsWith(G_BAND_PREFERENCE_RSSI_THRESHOLD_KEY)) {
23878c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    String st = key.replace(G_BAND_PREFERENCE_RSSI_THRESHOLD_KEY, "");
23888c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    st = st.replace(SEPARATOR_KEY, "");
23898c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    try {
23900eebae7334d6129f7ca1344e4b20199794994358vandwalle                        bandPreferencePenaltyThreshold5 = Integer.parseInt(st);
23910eebae7334d6129f7ca1344e4b20199794994358vandwalle                        Log.d(TAG,"readAutoJoinConfig: bandPreferencePenaltyThreshold5 = "
23920eebae7334d6129f7ca1344e4b20199794994358vandwalle                            + Integer.toString(bandPreferencePenaltyThreshold5));
23938c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    } catch (NumberFormatException e) {
23948c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
23958c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    }
23968c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
23979f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                if (key.startsWith(ALWAYS_ENABLE_SCAN_WHILE_ASSOCIATED_KEY)) {
23989f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    String st = key.replace(ALWAYS_ENABLE_SCAN_WHILE_ASSOCIATED_KEY, "");
23999f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    st = st.replace(SEPARATOR_KEY, "");
24009f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    try {
24019f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        alwaysEnableScansWhileAssociated = Integer.parseInt(st);
24029f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        Log.d(TAG,"readAutoJoinConfig: alwaysEnableScansWhileAssociated = "
24039f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                                + Integer.toString(alwaysEnableScansWhileAssociated));
24049f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    } catch (NumberFormatException e) {
24059f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
24069f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    }
24079f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                }
24089f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                if (key.startsWith(MAX_NUM_PASSIVE_CHANNELS_FOR_PARTIAL_SCANS_KEY)) {
24099f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    String st = key.replace(MAX_NUM_PASSIVE_CHANNELS_FOR_PARTIAL_SCANS_KEY, "");
24109f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    st = st.replace(SEPARATOR_KEY, "");
24119f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    try {
24129f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        maxNumPassiveChannelsForPartialScans = Integer.parseInt(st);
24139f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        Log.d(TAG,"readAutoJoinConfig: maxNumPassiveChannelsForPartialScans = "
24149f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                                + Integer.toString(maxNumPassiveChannelsForPartialScans));
24159f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    } catch (NumberFormatException e) {
24169f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
24179f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    }
24189f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                }
24199f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                if (key.startsWith(MAX_NUM_ACTIVE_CHANNELS_FOR_PARTIAL_SCANS_KEY)) {
24209f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    String st = key.replace(MAX_NUM_ACTIVE_CHANNELS_FOR_PARTIAL_SCANS_KEY, "");
24219f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    st = st.replace(SEPARATOR_KEY, "");
24229f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    try {
24239f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        maxNumActiveChannelsForPartialScans = Integer.parseInt(st);
24249f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        Log.d(TAG,"readAutoJoinConfig: maxNumActiveChannelsForPartialScans = "
24259f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                                + Integer.toString(maxNumActiveChannelsForPartialScans));
24269f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    } catch (NumberFormatException e) {
24279f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        Log.d(TAG,"readAutoJoinConfig: incorrect format :" + key);
24289f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    }
24299f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                }
24308c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            }
24318c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle        } catch (EOFException ignore) {
24328c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            if (reader != null) {
24338c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                try {
24348c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    reader.close();
24358c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    reader = null;
24368c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                } catch (Exception e) {
24378c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    loge("readAutoJoinStatus: Error closing file" + e);
24388c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                }
24398c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            }
24409f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle        } catch (FileNotFoundException ignore) {
24419f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            if (reader != null) {
24429f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                try {
24439f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    reader.close();
24449f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    reader = null;
24459f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                } catch (Exception e) {
24469f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    loge("readAutoJoinStatus: Error closing file" + e);
24479f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                }
24489f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            }
24498c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle        } catch (IOException e) {
24508c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            loge("readAutoJoinStatus: Error parsing configuration" + e);
24518c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle        }
24528c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
24538c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle        if (reader!=null) {
24548c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle           try {
24558c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle               reader.close();
24568c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle           } catch (Exception e) {
24578c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle               loge("readAutoJoinStatus: Error closing file" + e);
24588c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle           }
24598c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle        }
24608c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle    }
24618c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
24628c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
24633ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim    private void writeIpAndProxyConfigurations() {
24643ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim        final SparseArray<IpConfiguration> networks = new SparseArray<IpConfiguration>();
24653ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim        for(WifiConfiguration config : mConfiguredNetworks.values()) {
24660e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            if (!config.ephemeral && config.autoJoinStatus != WifiConfiguration.AUTO_JOIN_DELETED) {
24673ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                networks.put(configKey(config), config.getIpConfiguration());
24683ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim            }
24693ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim        }
2470f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
24713ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim        super.writeIpAndProxyConfigurations(ipConfigFile, networks);
24723ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim    }
2473b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim
24743ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim    private void readIpAndProxyConfigurations() {
24753ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim        SparseArray<IpConfiguration> networks = super.readIpAndProxyConfigurations(ipConfigFile);
2476b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim
24775f128f9e971b7120a776f8fafde3a0db889f847dLorenzo Colitti        if (networks.size() == 0) {
24783ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim            // IpConfigStore.readIpAndProxyConfigurations has already logged an error.
24793ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim            return;
24803ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim        }
2481b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim
24823ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim        for (int i = 0; i < networks.size(); i++) {
24834d3bff566b2cd0c3fd4d118eb6aae42ab3e9b5ddLorenzo Colitti            int id = networks.keyAt(i);
24843ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim            WifiConfiguration config = mConfiguredNetworks.get(mNetworkIds.get(id));
2485b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim
2486cf5b8eb8a08c45bd4a82f1f4bb789c8a1b08744fvandwalle
24870e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            if (config == null || config.autoJoinStatus == WifiConfiguration.AUTO_JOIN_DELETED) {
24884d3bff566b2cd0c3fd4d118eb6aae42ab3e9b5ddLorenzo Colitti                loge("configuration found for missing network, nid=" + id
24890888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        +", ignored, networks.size=" + Integer.toString(networks.size()));
24903ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim            } else {
24913ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                config.setIpConfiguration(networks.valueAt(i));
2492155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2493155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
2494155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
2495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
24963137b27bbbaaf87267dea3bf95d1c027931b0b6aNavtej Singh Mann    /*
24973137b27bbbaaf87267dea3bf95d1c027931b0b6aNavtej Singh Mann     * Convert string to Hexadecimal before passing to wifi native layer
24983137b27bbbaaf87267dea3bf95d1c027931b0b6aNavtej Singh Mann     * In native function "doCommand()" have trouble in converting Unicode character string to UTF8
24993137b27bbbaaf87267dea3bf95d1c027931b0b6aNavtej Singh Mann     * conversion to hex is required because SSIDs can have space characters in them;
25003137b27bbbaaf87267dea3bf95d1c027931b0b6aNavtej Singh Mann     * and that can confuses the supplicant because it uses space charaters as delimiters
25013137b27bbbaaf87267dea3bf95d1c027931b0b6aNavtej Singh Mann     */
25023137b27bbbaaf87267dea3bf95d1c027931b0b6aNavtej Singh Mann
25033137b27bbbaaf87267dea3bf95d1c027931b0b6aNavtej Singh Mann    private String encodeSSID(String str){
25043137b27bbbaaf87267dea3bf95d1c027931b0b6aNavtej Singh Mann        String tmp = removeDoubleQuotes(str);
25053137b27bbbaaf87267dea3bf95d1c027931b0b6aNavtej Singh Mann        return String.format("%x", new BigInteger(1, tmp.getBytes(Charset.forName("UTF-8"))));
25063137b27bbbaaf87267dea3bf95d1c027931b0b6aNavtej Singh Mann    }
25073137b27bbbaaf87267dea3bf95d1c027931b0b6aNavtej Singh Mann
25080eebae7334d6129f7ca1344e4b20199794994358vandwalle    private NetworkUpdateResult addOrUpdateNetworkNative(WifiConfiguration config, int uid) {
2509155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
2510155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * If the supplied networkId is INVALID_NETWORK_ID, we create a new empty
2511155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * network configuration. Otherwise, the networkId should
2512155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * refer to an existing configuration.
2513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
2514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2515155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (VDBG) localLog("addOrUpdateNetworkNative " + config.getPrintableSsid());
2516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int netId = config.networkId;
2518155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean newNetwork = false;
2519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // networkId of INVALID_NETWORK_ID means we want to create a new network
2520155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (netId == INVALID_NETWORK_ID) {
2521155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Integer savedNetId = mNetworkIds.get(configKey(config));
2522e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // Check if either we have a network Id or a WifiConfiguration
2523e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // matching the one we are trying to add.
25240e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            if (savedNetId == null) {
25250e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                for (WifiConfiguration test : mConfiguredNetworks.values()) {
25260e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                    if (test.configKey().equals(config.configKey())) {
25270e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                        savedNetId = test.networkId;
25280e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                        loge("addOrUpdateNetworkNative " + config.configKey()
25290e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                                + " was found, but no network Id");
25300e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                        break;
25310e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                    }
25320e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                }
25330e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            }
2534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (savedNetId != null) {
2535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                netId = savedNetId;
2536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
2537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                newNetwork = true;
2538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                netId = mWifiNative.addNetwork();
2539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (netId < 0) {
2540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    loge("Failed to add a network!");
2541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    return new NetworkUpdateResult(INVALID_NETWORK_ID);
2542f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                } else {
2543f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    loge("addOrUpdateNetworkNative created netId=" + netId);
2544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
2545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
2547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean updateFailed = true;
2549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        setVariables: {
2551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (config.SSID != null &&
2553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    !mWifiNative.setNetworkVariable(
2554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        netId,
2555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        WifiConfiguration.ssidVarName,
25563137b27bbbaaf87267dea3bf95d1c027931b0b6aNavtej Singh Mann                        encodeSSID(config.SSID))) {
2557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                loge("failed to set SSID: "+config.SSID);
2558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break setVariables;
2559155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2560155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2561b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            if (config.BSSID != null) {
2562b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                loge("Setting BSSID for " + config.configKey() + " to " + config.BSSID);
2563b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                if (!mWifiNative.setNetworkVariable(
2564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        netId,
2565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        WifiConfiguration.bssidVarName,
2566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        config.BSSID)) {
2567b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    loge("failed to set BSSID: " + config.BSSID);
2568b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    break setVariables;
2569b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                }
2570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2572155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String allowedKeyManagementString =
2573155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                makeString(config.allowedKeyManagement, WifiConfiguration.KeyMgmt.strings);
2574155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (config.allowedKeyManagement.cardinality() != 0 &&
2575155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    !mWifiNative.setNetworkVariable(
2576155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        netId,
2577155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        WifiConfiguration.KeyMgmt.varName,
2578155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        allowedKeyManagementString)) {
2579155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                loge("failed to set key_mgmt: "+
2580155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        allowedKeyManagementString);
2581155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break setVariables;
2582155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2583155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2584155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String allowedProtocolsString =
2585155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                makeString(config.allowedProtocols, WifiConfiguration.Protocol.strings);
2586155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (config.allowedProtocols.cardinality() != 0 &&
2587155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    !mWifiNative.setNetworkVariable(
2588155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        netId,
2589155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        WifiConfiguration.Protocol.varName,
2590155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        allowedProtocolsString)) {
2591155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                loge("failed to set proto: "+
2592155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        allowedProtocolsString);
2593155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break setVariables;
2594155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2595155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2596155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String allowedAuthAlgorithmsString =
2597155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                makeString(config.allowedAuthAlgorithms, WifiConfiguration.AuthAlgorithm.strings);
2598155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (config.allowedAuthAlgorithms.cardinality() != 0 &&
2599155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    !mWifiNative.setNetworkVariable(
2600155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        netId,
2601155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        WifiConfiguration.AuthAlgorithm.varName,
2602155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        allowedAuthAlgorithmsString)) {
2603155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                loge("failed to set auth_alg: "+
2604155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        allowedAuthAlgorithmsString);
2605155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break setVariables;
2606155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2607155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2608155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String allowedPairwiseCiphersString =
2609155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    makeString(config.allowedPairwiseCiphers,
2610155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    WifiConfiguration.PairwiseCipher.strings);
2611155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (config.allowedPairwiseCiphers.cardinality() != 0 &&
2612155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    !mWifiNative.setNetworkVariable(
2613155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        netId,
2614155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        WifiConfiguration.PairwiseCipher.varName,
2615155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        allowedPairwiseCiphersString)) {
2616155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                loge("failed to set pairwise: "+
2617155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        allowedPairwiseCiphersString);
2618155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break setVariables;
2619155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2620155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2621155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String allowedGroupCiphersString =
2622155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                makeString(config.allowedGroupCiphers, WifiConfiguration.GroupCipher.strings);
2623155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (config.allowedGroupCiphers.cardinality() != 0 &&
2624155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    !mWifiNative.setNetworkVariable(
2625155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        netId,
2626155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        WifiConfiguration.GroupCipher.varName,
2627155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        allowedGroupCiphersString)) {
2628155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                loge("failed to set group: "+
2629155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        allowedGroupCiphersString);
2630155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break setVariables;
2631155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2632155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2633155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // Prevent client screw-up by passing in a WifiConfiguration we gave it
2634155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // by preventing "*" as a key.
2635155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (config.preSharedKey != null && !config.preSharedKey.equals("*") &&
2636155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    !mWifiNative.setNetworkVariable(
2637155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        netId,
2638155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        WifiConfiguration.pskVarName,
2639155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        config.preSharedKey)) {
2640155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                loge("failed to set psk");
2641155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break setVariables;
2642155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2643155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2644155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            boolean hasSetKey = false;
2645155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (config.wepKeys != null) {
2646155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                for (int i = 0; i < config.wepKeys.length; i++) {
2647155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // Prevent client screw-up by passing in a WifiConfiguration we gave it
2648155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // by preventing "*" as a key.
2649155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (config.wepKeys[i] != null && !config.wepKeys[i].equals("*")) {
2650155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (!mWifiNative.setNetworkVariable(
2651155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                    netId,
2652155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                    WifiConfiguration.wepKeyVarNames[i],
2653155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                    config.wepKeys[i])) {
2654155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            loge("failed to set wep_key" + i + ": " + config.wepKeys[i]);
2655155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            break setVariables;
2656155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        }
2657155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        hasSetKey = true;
2658155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
2659155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
2660155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2661155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2662155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (hasSetKey) {
2663155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (!mWifiNative.setNetworkVariable(
2664155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            netId,
2665155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            WifiConfiguration.wepTxKeyIdxVarName,
2666155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            Integer.toString(config.wepTxKeyIndex))) {
2667155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    loge("failed to set wep_tx_keyidx: " + config.wepTxKeyIndex);
2668155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    break setVariables;
2669155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
2670155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2671155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2672155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (!mWifiNative.setNetworkVariable(
2673155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        netId,
2674155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        WifiConfiguration.priorityVarName,
2675155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Integer.toString(config.priority))) {
2676155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                loge(config.SSID + ": failed to set priority: "
2677155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        +config.priority);
2678155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break setVariables;
2679155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2680155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2681155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (config.hiddenSSID && !mWifiNative.setNetworkVariable(
2682155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        netId,
2683155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        WifiConfiguration.hiddenSSIDVarName,
2684155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Integer.toString(config.hiddenSSID ? 1 : 0))) {
2685155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                loge(config.SSID + ": failed to set hiddenSSID: "+
2686155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        config.hiddenSSID);
2687155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break setVariables;
2688155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2689155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
269096d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng            if (config.requirePMF && !mWifiNative.setNetworkVariable(
269196d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng                        netId,
269296d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng                        WifiConfiguration.pmfVarName,
269396d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng                        "2")) {
269496d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng                loge(config.SSID + ": failed to set requirePMF: "+
269596d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng                        config.requirePMF);
269696d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng                break setVariables;
269796d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng            }
269896d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng
269996d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng            if (config.updateIdentifier != null && !mWifiNative.setNetworkVariable(
270096d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng                    netId,
270196d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng                    WifiConfiguration.updateIdentiferVarName,
270296d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng                    config.updateIdentifier)) {
270396d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng                loge(config.SSID + ": failed to set updateIdentifier: "+
270496d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng                        config.updateIdentifier);
270596d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng                break setVariables;
270696d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng            }
270796d15e49bb2d9f22b307b4c9cafd5f51b87667a1Yuhao Zheng
2708155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (config.enterpriseConfig != null &&
2709155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    config.enterpriseConfig.getEapMethod() != WifiEnterpriseConfig.Eap.NONE) {
2710155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2711155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                WifiEnterpriseConfig enterpriseConfig = config.enterpriseConfig;
2712155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2713155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (needsKeyStore(enterpriseConfig)) {
2714155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    /**
2715155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                     * Keyguard settings may eventually be controlled by device policy.
2716155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                     * We check here if keystore is unlocked before installing
2717155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                     * credentials.
2718155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                     * TODO: Do we need a dialog here ?
2719155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                     */
2720155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (mKeyStore.state() != KeyStore.State.UNLOCKED) {
2721155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        loge(config.SSID + ": key store is locked");
2722155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        break setVariables;
2723155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
2724155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2725155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    try {
2726155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        /* config passed may include only fields being updated.
2727155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                         * In order to generate the key id, fetch uninitialized
2728155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                         * fields from the currently tracked configuration
2729155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                         */
2730155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        WifiConfiguration currentConfig = mConfiguredNetworks.get(netId);
2731155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        String keyId = config.getKeyIdForCredentials(currentConfig);
2732155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2733155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (!installKeys(enterpriseConfig, keyId)) {
2734155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            loge(config.SSID + ": failed to install keys");
2735155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            break setVariables;
2736155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        }
2737155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    } catch (IllegalStateException e) {
2738155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        loge(config.SSID + " invalid config for key installation");
2739155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        break setVariables;
2740155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    }
2741155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
2742155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2743155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                HashMap<String, String> enterpriseFields = enterpriseConfig.getFields();
2744155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                for (String key : enterpriseFields.keySet()) {
2745155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        String value = enterpriseFields.get(key);
2746d31e487847b954a9e763c5dd72adbf5efd590814vandwalle                        if (key.equals("password") && value != null && value.equals("*")) {
2747be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                            // No need to try to set an obfuscated password, which will fail
2748d31e487847b954a9e763c5dd72adbf5efd590814vandwalle                            continue;
2749d31e487847b954a9e763c5dd72adbf5efd590814vandwalle                        }
2750155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        if (!mWifiNative.setNetworkVariable(
2751155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                    netId,
2752155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                    key,
2753155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                    value)) {
2754155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            removeKeys(enterpriseConfig);
2755155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            loge(config.SSID + ": failed to set " + key +
2756155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                                    ": " + value);
2757155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            break setVariables;
2758155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        }
2759155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
2760155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2761155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            updateFailed = false;
2762be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        } // End of setVariables
2763155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2764155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (updateFailed) {
2765155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (newNetwork) {
2766155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mWifiNative.removeNetwork(netId);
2767155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                loge("Failed to set a network variable, removed network: " + netId);
2768155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
2769155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return new NetworkUpdateResult(INVALID_NETWORK_ID);
2770155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
2771155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2772155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* An update of the network variables requires reading them
2773155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * back from the supplicant to update mConfiguredNetworks.
2774155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * This is because some of the variables (SSID, wep keys &
2775155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * passphrases) reflect different values when read back than
2776155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * when written. For example, wep key is stored as * irrespective
2777155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * of the value sent to the supplicant
2778155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
2779155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiConfiguration currentConfig = mConfiguredNetworks.get(netId);
2780155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (currentConfig == null) {
2781155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            currentConfig = new WifiConfiguration();
27823ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim            currentConfig.setIpAssignment(IpAssignment.DHCP);
27833ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim            currentConfig.setProxySettings(ProxySettings.NONE);
2784155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            currentConfig.networkId = netId;
2785992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            if (config != null) {
27860eebae7334d6129f7ca1344e4b20199794994358vandwalle                // Carry over the creation parameters
2787992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                currentConfig.selfAdded = config.selfAdded;
2788992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                currentConfig.didSelfAdd = config.didSelfAdd;
27890eebae7334d6129f7ca1344e4b20199794994358vandwalle                currentConfig.ephemeral = config.ephemeral;
27900eebae7334d6129f7ca1344e4b20199794994358vandwalle                currentConfig.autoJoinUseAggressiveJoinAttemptThreshold
27910eebae7334d6129f7ca1344e4b20199794994358vandwalle                        = config.autoJoinUseAggressiveJoinAttemptThreshold;
2792992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                currentConfig.lastConnectUid = config.lastConnectUid;
2793992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                currentConfig.lastUpdateUid = config.lastUpdateUid;
2794992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                currentConfig.creatorUid = config.creatorUid;
27950e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                currentConfig.peerWifiConfiguration = config.peerWifiConfiguration;
2796992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            }
2797992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            if (DBG) {
2798992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                loge("created new config netId=" + Integer.toString(netId)
2799992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        + " uid=" + Integer.toString(currentConfig.creatorUid));
2800992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle            }
2801155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
2802155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
28030eebae7334d6129f7ca1344e4b20199794994358vandwalle        if (uid >= 0) {
28040eebae7334d6129f7ca1344e4b20199794994358vandwalle            if (newNetwork) {
28050eebae7334d6129f7ca1344e4b20199794994358vandwalle                currentConfig.creatorUid = uid;
28060eebae7334d6129f7ca1344e4b20199794994358vandwalle            } else {
28070eebae7334d6129f7ca1344e4b20199794994358vandwalle                currentConfig.lastUpdateUid = uid;
28080eebae7334d6129f7ca1344e4b20199794994358vandwalle            }
28090eebae7334d6129f7ca1344e4b20199794994358vandwalle        }
28100eebae7334d6129f7ca1344e4b20199794994358vandwalle
2811e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        if (newNetwork) {
2812e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            currentConfig.dirty = true;
2813e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        }
2814e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
28150e3c67280365eb82fc15362a3b98a2c978bde766vandwalle        if (currentConfig.autoJoinStatus == WifiConfiguration.AUTO_JOIN_DELETED) {
2816be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            // Make sure the configuration is not deleted anymore since we just
2817be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            // added or modified it.
281827355a942653264388e909a4276196ee63e57811vandwalle            currentConfig.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
28190e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            currentConfig.selfAdded = false;
28200e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            currentConfig.didSelfAdd = false;
28217b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            if (DBG) {
28227b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                loge("remove deleted status netId=" + Integer.toString(netId)
28237b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        + " " + currentConfig.configKey());
28247b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            }
28250e3c67280365eb82fc15362a3b98a2c978bde766vandwalle        }
28260e3c67280365eb82fc15362a3b98a2c978bde766vandwalle
28270eebae7334d6129f7ca1344e4b20199794994358vandwalle        if (currentConfig.status == WifiConfiguration.Status.ENABLED) {
28280eebae7334d6129f7ca1344e4b20199794994358vandwalle            // Make sure autojoin remain in sync with user modifying the configuration
28290eebae7334d6129f7ca1344e4b20199794994358vandwalle            currentConfig.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
28300eebae7334d6129f7ca1344e4b20199794994358vandwalle        }
28310eebae7334d6129f7ca1344e4b20199794994358vandwalle
2832f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (DBG) loge("will read network variables netId=" + Integer.toString(netId));
2833f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
2834155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        readNetworkVariables(currentConfig);
2835155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2836155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mConfiguredNetworks.put(netId, currentConfig);
2837155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mNetworkIds.put(configKey(currentConfig), netId);
2838155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2839155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        NetworkUpdateResult result = writeIpAndProxyConfigurationsOnChange(currentConfig, config);
2840155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        result.setIsNewNetwork(newNetwork);
2841155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        result.setNetworkId(netId);
2842e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
2843e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        writeKnownNetworkHistory();
2844e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
2845155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return result;
2846155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
2847155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
2848f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
2849448c9536a302c58a79e271b1721c08b8882f800evandwalle    /**
2850448c9536a302c58a79e271b1721c08b8882f800evandwalle     * This function run thru the Saved WifiConfigurations and check if some should be linked.
2851448c9536a302c58a79e271b1721c08b8882f800evandwalle     * @param config
2852448c9536a302c58a79e271b1721c08b8882f800evandwalle     */
2853f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    public void linkConfiguration(WifiConfiguration config) {
2854448c9536a302c58a79e271b1721c08b8882f800evandwalle
28558c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle        if (config.scanResultCache != null && config.scanResultCache.size() > 6) {
28568c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            // Ignore configurations with large number of BSSIDs
28578c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            return;
28588c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle        }
28598c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle        if (!config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
28608c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            // Only link WPA_PSK config
28618c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            return;
28628c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle        }
2863f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        for (WifiConfiguration link : mConfiguredNetworks.values()) {
2864f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            boolean doLink = false;
2865f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
2866f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (link.configKey().equals(config.configKey())) {
2867f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                continue;
2868f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
2869f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
28700e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            if (link.autoJoinStatus == WifiConfiguration.AUTO_JOIN_DELETED) {
28710e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                continue;
28720e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            }
28730e3c67280365eb82fc15362a3b98a2c978bde766vandwalle
28748c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            // Autojoin will be allowed to dynamically jump from a linked configuration
28758c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            // to another, hence only link configurations that have equivalent level of security
2876d2569dc2b19c401200fd2672acf83cfc35f1eb12vandwalle            if (!link.allowedKeyManagement.equals(config.allowedKeyManagement)) {
2877d2569dc2b19c401200fd2672acf83cfc35f1eb12vandwalle                continue;
2878d2569dc2b19c401200fd2672acf83cfc35f1eb12vandwalle            }
2879d2569dc2b19c401200fd2672acf83cfc35f1eb12vandwalle
28808c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            if (link.scanResultCache != null && link.scanResultCache.size() > 6) {
28818c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                // Ignore configurations with large number of BSSIDs
28828c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                continue;
28838c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            }
28848c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle
2885f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (config.defaultGwMacAddress != null && link.defaultGwMacAddress != null) {
2886448c9536a302c58a79e271b1721c08b8882f800evandwalle                // If both default GW are known, link only if they are equal
2887f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (config.defaultGwMacAddress.equals(link.defaultGwMacAddress)) {
2888f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (VDBG) {
28899f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        loge("linkConfiguration link due to same gw " + link.SSID +
2890f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                " and " + config.SSID + " GW " + config.defaultGwMacAddress);
2891f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
2892f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    doLink = true;
2893f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
2894f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else {
28958c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                // We do not know BOTH default gateways hence we will try to link
28968c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                // hoping that WifiConfigurations are indeed behind the same gateway.
28978c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                // once both WifiConfiguration have been tried and thus once both efault gateways
28988c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                // are known we will revisit the choice of linking them
2899b07da189850a4bfa268f8ab9be7867935eb2ecb5vandwalle                if ((config.scanResultCache != null) && (config.scanResultCache.size() <= 6)
2900a2903b7c6fc2419d48fd9976fa24852370653c4avandwalle                        && (link.scanResultCache != null) && (link.scanResultCache.size() <= 6)) {
29018c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    for (String abssid : config.scanResultCache.keySet()) {
29028c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        for (String bbssid : link.scanResultCache.keySet()) {
29032f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                            if (VVDBG) {
29048c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                                loge("linkConfiguration try to link due to DBDC BSSID match "
29058c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                                        + link.SSID +
29068c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                                        " and " + config.SSID + " bssida " + abssid
29078c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                                        + " bssidb " + bbssid);
29088c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            }
29098c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            if (abssid.regionMatches(true, 0, bbssid, 0, 16)) {
29108c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                                // If first 16 ascii characters of BSSID matches,
29118c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                                // we assume this is a DBDC
29128c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                                doLink = true;
29138c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            }
29148c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                        }
2915f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
2916f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
2917f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
2918f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
2919448c9536a302c58a79e271b1721c08b8882f800evandwalle            if (doLink == true && onlyLinkSameCredentialConfigurations) {
2920448c9536a302c58a79e271b1721c08b8882f800evandwalle                String apsk = readNetworkVariableFromSupplicantFile(link.SSID, "psk");
2921448c9536a302c58a79e271b1721c08b8882f800evandwalle                String bpsk = readNetworkVariableFromSupplicantFile(config.SSID, "psk");
2922e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                if (apsk == null || bpsk == null
2923e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        || TextUtils.isEmpty(apsk) || TextUtils.isEmpty(apsk)
292444d0d289bc2a959a18af3870844d221c93301659vandwalle                        || apsk.equals("*") || apsk.equals(DELETED_CONFIG_PSK)
2925e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        || !apsk.equals(bpsk)) {
2926448c9536a302c58a79e271b1721c08b8882f800evandwalle                    doLink = false;
2927448c9536a302c58a79e271b1721c08b8882f800evandwalle                }
2928448c9536a302c58a79e271b1721c08b8882f800evandwalle            }
2929448c9536a302c58a79e271b1721c08b8882f800evandwalle
2930f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (doLink) {
29310888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                if (VDBG) {
2932448c9536a302c58a79e271b1721c08b8882f800evandwalle                   loge("linkConfiguration: will link " + link.configKey()
2933448c9536a302c58a79e271b1721c08b8882f800evandwalle                           + " and " + config.configKey());
29340888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                }
2935f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (link.linkedConfigurations == null) {
2936f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    link.linkedConfigurations = new HashMap<String, Integer>();
2937f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
2938f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (config.linkedConfigurations == null) {
2939f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    config.linkedConfigurations = new HashMap<String, Integer>();
2940f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
2941e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                if (link.linkedConfigurations.get(config.configKey()) == null) {
2942e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    link.linkedConfigurations.put(config.configKey(), Integer.valueOf(1));
2943e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    link.dirty = true;
2944e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                }
2945e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                if (config.linkedConfigurations.get(link.configKey()) == null) {
2946e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    config.linkedConfigurations.put(link.configKey(), Integer.valueOf(1));
2947e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    config.dirty = true;
2948e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                }
2949f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else {
29509f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                if (link.linkedConfigurations != null
29519f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        && (link.linkedConfigurations.get(config.configKey()) != null)) {
29529f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    if (VDBG) {
29539f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        loge("linkConfiguration: un-link " + config.configKey()
29549f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                                + " from " + link.configKey());
29559f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    }
2956e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    link.dirty = true;
2957be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                    link.linkedConfigurations.remove(config.configKey());
2958be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                }
29599f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                if (config.linkedConfigurations != null
29609f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        && (config.linkedConfigurations.get(link.configKey()) != null)) {
29619f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    if (VDBG) {
29629f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        loge("linkConfiguration: un-link " + link.configKey()
29639f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                                + " from " + config.configKey());
29649f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    }
2965e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    config.dirty = true;
2966be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                    config.linkedConfigurations.remove(link.configKey());
2967be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                }
2968f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
2969f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
2970f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
2971f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
2972f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    /*
29738c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle     * We try to link a scan result with a WifiConfiguration for which SSID and
29748c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle     * key management dont match,
29758c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle     * for instance, we try identify the 5GHz SSID of a DBDC AP,
29768c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle     * even though we know only of the 2.4GHz
2977f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     *
2978f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * Obviously, this function is not optimal since it is used to compare every scan
2979f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * result with every Saved WifiConfiguration, with a string.equals operation.
2980f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * As a speed up, might be better to implement the mConfiguredNetworks store as a
2981f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * <String, WifiConfiguration> object instead of a <Integer, WifiConfiguration> object
2982f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * so as to speed this up. Also to prevent the tiny probability of hash collision.
2983f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     *
2984f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     */
2985f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    public WifiConfiguration associateWithConfiguration(ScanResult result) {
2986ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        String configKey = WifiConfiguration.configKey(result);
2987f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (configKey == null) {
2988f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (DBG) loge("associateWithConfiguration(): no config key " );
2989f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return null;
2990f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
2991f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
29927b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        // Need to compare with quoted string
2993f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String SSID = "\"" + result.SSID + "\"";
2994f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
29959f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle        if (VVDBG) {
2996e67ec726c07410073575473c0f50dc737629f5davandwalle            loge("associateWithConfiguration(): try " + configKey);
2997e67ec726c07410073575473c0f50dc737629f5davandwalle        }
2998e67ec726c07410073575473c0f50dc737629f5davandwalle
2999f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration config = null;
3000f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        for (WifiConfiguration link : mConfiguredNetworks.values()) {
3001f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            boolean doLink = false;
3002f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
300370468b47454c8657e8963932f2e08a3f4d7e3881vandwalle            if (link.autoJoinStatus == WifiConfiguration.AUTO_JOIN_DELETED || link.selfAdded) {
30049f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                if (VVDBG) loge("associateWithConfiguration(): skip selfadd " + link.configKey() );
3005e67ec726c07410073575473c0f50dc737629f5davandwalle                // Make sure we dont associate the scan result to a deleted config
3006e67ec726c07410073575473c0f50dc737629f5davandwalle                continue;
3007e67ec726c07410073575473c0f50dc737629f5davandwalle            }
3008e67ec726c07410073575473c0f50dc737629f5davandwalle
3009e67ec726c07410073575473c0f50dc737629f5davandwalle            if (!link.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
30109f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                if (VVDBG) loge("associateWithConfiguration(): skip non-PSK " + link.configKey() );
3011e67ec726c07410073575473c0f50dc737629f5davandwalle                // Make sure we dont associate the scan result to a non-PSK config
30120e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                continue;
30130e3c67280365eb82fc15362a3b98a2c978bde766vandwalle            }
30140e3c67280365eb82fc15362a3b98a2c978bde766vandwalle
3015f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (configKey.equals(link.configKey())) {
30169f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                if (VVDBG) loge("associateWithConfiguration(): found it!!! " + configKey );
3017e67ec726c07410073575473c0f50dc737629f5davandwalle                return link; // Found it exactly
3018f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
3019f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3020e67ec726c07410073575473c0f50dc737629f5davandwalle            if ((link.scanResultCache != null) && (link.scanResultCache.size() <= 6)) {
3021e67ec726c07410073575473c0f50dc737629f5davandwalle                for (String bssid : link.scanResultCache.keySet()) {
3022e67ec726c07410073575473c0f50dc737629f5davandwalle                    if (result.BSSID.regionMatches(true, 0, bssid, 0, 16)
3023e67ec726c07410073575473c0f50dc737629f5davandwalle                            && SSID.regionMatches(false, 0, link.SSID, 0, 4)) {
3024e67ec726c07410073575473c0f50dc737629f5davandwalle                        // If first 16 ascii characters of BSSID matches, and first 3
3025e67ec726c07410073575473c0f50dc737629f5davandwalle                        // characters of SSID match, we assume this is a home setup
3026e67ec726c07410073575473c0f50dc737629f5davandwalle                        // and thus we will try to transfer the password from the known
3027e67ec726c07410073575473c0f50dc737629f5davandwalle                        // BSSID/SSID to the recently found BSSID/SSID
3028f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3029e67ec726c07410073575473c0f50dc737629f5davandwalle                        // If (VDBG)
3030e67ec726c07410073575473c0f50dc737629f5davandwalle                        //    loge("associateWithConfiguration OK " );
3031e67ec726c07410073575473c0f50dc737629f5davandwalle                        doLink = true;
3032e67ec726c07410073575473c0f50dc737629f5davandwalle                        break;
3033e67ec726c07410073575473c0f50dc737629f5davandwalle                    }
3034f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
3035f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
3036f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3037f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (doLink) {
3038e67ec726c07410073575473c0f50dc737629f5davandwalle                // Try to make a non verified WifiConfiguration, but only if the original
3039e67ec726c07410073575473c0f50dc737629f5davandwalle                // configuration was not self already added
3040f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (VDBG) {
3041f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    loge("associateWithConfiguration: will create " +
3042f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            result.SSID + " and associate it with: " + link.SSID);
3043f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
3044f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                config = wifiConfigurationFromScanResult(result);
3045f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (config != null) {
3046992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    config.selfAdded = true;
3047992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    config.didSelfAdd = true;
3048e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    config.dirty = true;
30490e3c67280365eb82fc15362a3b98a2c978bde766vandwalle                    config.peerWifiConfiguration = link.configKey();
3050f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (config.allowedKeyManagement.equals(link.allowedKeyManagement) &&
3051f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
3052e67ec726c07410073575473c0f50dc737629f5davandwalle                        // Transfer the credentials from the configuration we are linking from
3053f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        String psk = readNetworkVariableFromSupplicantFile(link.SSID, "psk");
3054f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        if (psk != null) {
3055f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            config.preSharedKey = psk;
3056f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            if (VDBG) {
3057f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                if (config.preSharedKey != null)
3058f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                    loge(" transfer PSK : " + config.preSharedKey);
3059f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            }
3060f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3061e67ec726c07410073575473c0f50dc737629f5davandwalle                            // Link configurations
3062f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            if (link.linkedConfigurations == null) {
3063f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                link.linkedConfigurations = new HashMap<String, Integer>();
3064f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            }
3065f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            if (config.linkedConfigurations == null) {
3066f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                                config.linkedConfigurations = new HashMap<String, Integer>();
3067f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            }
3068f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            link.linkedConfigurations.put(config.configKey(), Integer.valueOf(1));
3069f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            config.linkedConfigurations.put(link.configKey(), Integer.valueOf(1));
3070f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        } else {
3071f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                            config = null;
3072f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
3073992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    } else {
3074992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        config = null;
3075f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
3076e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    if (config != null) break;
3077f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
3078f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
3079f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
3080f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        return config;
3081f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
3082f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3083c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle    public HashSet<Integer> makeChannelList(WifiConfiguration config, int age, boolean restrict) {
30844dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        if (config == null)
30854dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            return null;
30864dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        long now_ms = System.currentTimeMillis();
30874dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
30884dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        HashSet<Integer> channels = new HashSet<Integer>();
30894dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
30904dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        //get channels for this configuration, if there are at least 2 BSSIDs
30914dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        if (config.scanResultCache == null && config.linkedConfigurations == null) {
30924dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            return null;
30934dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
30944dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
30954dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        if (VDBG) {
30964dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            StringBuilder dbg = new StringBuilder();
30978c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            dbg.append("makeChannelList age=" + Integer.toString(age)
30987b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    + " for " + config.configKey()
30997b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    + " max=" + maxNumActiveChannelsForPartialScans);
31004dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            if (config.scanResultCache != null) {
31014dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                dbg.append(" bssids=" + config.scanResultCache.size());
31024dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
31034dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            if (config.linkedConfigurations != null) {
31044dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                dbg.append(" linked=" + config.linkedConfigurations.size());
31054dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
31064dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            loge(dbg.toString());
31074dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
3108b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle
31099f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle        int numChannels = 0;
31104dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        if (config.scanResultCache != null && config.scanResultCache.size() > 0) {
31114dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            for (ScanResult result : config.scanResultCache.values()) {
31129f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                //TODO : cout active and passive channels separately
31139f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                if (numChannels > maxNumActiveChannelsForPartialScans) {
31149f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    break;
31159f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                }
3116b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                if (VDBG) {
3117b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    boolean test = (now_ms - result.seen) < age;
3118b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    loge("has " + result.BSSID + " freq=" + Integer.toString(result.frequency)
3119b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + " age=" + Long.toString(now_ms - result.seen) + " ?=" + test);
3120b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                }
3121c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                if (((now_ms - result.seen) < age)/*||(!restrict || result.is24GHz())*/) {
31224dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                    channels.add(result.frequency);
31239f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    numChannels++;
31244dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                }
31254dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
31264dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
3127b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle
31284dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        //get channels for linked configurations
31294dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        if (config.linkedConfigurations != null) {
31304dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            for (String key : config.linkedConfigurations.keySet()) {
31314dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                WifiConfiguration linked = getWifiConfiguration(key);
31324dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                if (linked == null)
31334dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                    continue;
31344dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                if (linked.scanResultCache == null) {
3135b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    continue;
31364dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                }
31374dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                for (ScanResult result : linked.scanResultCache.values()) {
3138b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    if (VDBG) {
3139c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                        loge("has link: " + result.BSSID
3140c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                                + " freq=" + Integer.toString(result.frequency)
3141b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                                + " age=" + Long.toString(now_ms - result.seen));
3142b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    }
31439f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    if (numChannels > maxNumActiveChannelsForPartialScans) {
31449f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        break;
31459f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    }
3146c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                    if (((now_ms - result.seen) < age)/*||(!restrict || result.is24GHz())*/) {
31474dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                        channels.add(result.frequency);
31489f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        numChannels++;
31494dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                    }
31504dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                }
31514dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
31524dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
31534dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        return channels;
31544dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle    }
3155f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
31567b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle    // Update the WifiConfiguration database with the new scan result
31577b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle    // A scan result can be associated to multiple WifiConfigurations
31587b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle    public boolean updateSavedNetworkHistory(ScanResult scanResult) {
31597b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        int numConfigFound = 0;
3160f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (scanResult == null)
31617b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            return false;
3162f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
31637b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        String SSID = "\"" + scanResult.SSID + "\"";
3164f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
31657b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        for (WifiConfiguration config : mConfiguredNetworks.values()) {
31667b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            boolean found = false;
3167f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
31687b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            if (config.SSID == null || !config.SSID.equals(SSID)) {
31697b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                // SSID mismatch
31707b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                if (VVDBG) {
31717b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    loge("updateSavedNetworkHistory(): SSID mismatch " + config.configKey()
31727b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                            + " SSID=" + config.SSID + " " + SSID);
31737b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                }
31747b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                continue;
31757b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            }
31767b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            if (VDBG) {
31777b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                loge("updateSavedNetworkHistory(): try " + config.configKey()
31787b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        + " SSID=" + config.SSID + " " + scanResult.SSID
317970468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                        + " " + scanResult.capabilities
318070468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                        + " ajst=" + config.autoJoinStatus);
31817b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            }
31827b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            if (scanResult.capabilities.contains("WEP")
31837b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    && config.configKey().contains("WEP")) {
31847b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                found = true;
31857b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            } else if (scanResult.capabilities.contains("PSK")
31867b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    && config.configKey().contains("PSK")) {
31877b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                found = true;
31887b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            } else if (scanResult.capabilities.contains("EAP")
31897b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    && config.configKey().contains("EAP")) {
31907b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                found = true;
31917b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            } else if (!scanResult.capabilities.contains("WEP")
31927b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                && !scanResult.capabilities.contains("PSK")
31937b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                && !scanResult.capabilities.contains("EAP")
31947b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                && !config.configKey().contains("WEP")
31957b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    && !config.configKey().contains("PSK")
31967b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    && !config.configKey().contains("EAP")) {
31977b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                found = true;
31987b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            }
31997b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle
32007b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            if (found) {
320170468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                numConfigFound ++;
320270468b47454c8657e8963932f2e08a3f4d7e3881vandwalle
3203078a7527951b1e8fb7722ab930760730cf54e572vandwalle                if (config.autoJoinStatus >= WifiConfiguration.AUTO_JOIN_DELETED) {
320470468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    if (VVDBG) {
320570468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                        loge("updateSavedNetworkHistory(): found a deleted, skip it...  "
320670468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                                + config.configKey());
320770468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    }
320870468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    // The scan result belongs to a deleted config:
320970468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    //   - increment numConfigFound to remember that we found a config
321070468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    //            matching for this scan result
321170468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    //   - dont do anything since the config was deleted, just skip...
321270468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    continue;
321370468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                }
321470468b47454c8657e8963932f2e08a3f4d7e3881vandwalle
32157b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                if (config.scanResultCache == null) {
32167b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    config.scanResultCache = new HashMap<String, ScanResult>();
32177b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                }
3218e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
3219e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                // Adding a new BSSID
3220078a7527951b1e8fb7722ab930760730cf54e572vandwalle                ScanResult result = config.scanResultCache.get(scanResult.BSSID);
3221078a7527951b1e8fb7722ab930760730cf54e572vandwalle                if (result == null) {
3222e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    config.dirty = true;
3223078a7527951b1e8fb7722ab930760730cf54e572vandwalle                } else {
3224078a7527951b1e8fb7722ab930760730cf54e572vandwalle                    // transfer the black list status
3225078a7527951b1e8fb7722ab930760730cf54e572vandwalle                    scanResult.autoJoinStatus = result.autoJoinStatus;
3226078a7527951b1e8fb7722ab930760730cf54e572vandwalle                    scanResult.blackListTimestamp = result.blackListTimestamp;
3227078a7527951b1e8fb7722ab930760730cf54e572vandwalle                    scanResult.numIpConfigFailures = result.numIpConfigFailures;
3228078a7527951b1e8fb7722ab930760730cf54e572vandwalle                    scanResult.numConnection = result.numConnection;
3229078a7527951b1e8fb7722ab930760730cf54e572vandwalle                    scanResult.isAutoJoinCandidate = result.isAutoJoinCandidate;
3230e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                }
3231e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
32327b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                // Add the scan result to this WifiConfiguration
32337b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                config.scanResultCache.put(scanResult.BSSID, scanResult);
32347b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                // Since we added a scan result to this configuration, re-attempt linking
32357b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                linkConfiguration(config);
32367b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            }
32377b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle
32387b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            if (VDBG && found) {
32397b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                String status = "";
32407b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                if (scanResult.autoJoinStatus > 0) {
32417b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    status = " status=" + Integer.toString(scanResult.autoJoinStatus);
32427b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                }
32437b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                loge("        got known scan result " +
32447b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        scanResult.BSSID + " key : "
32457b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        + config.configKey() + " num: " +
32467b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        Integer.toString(config.scanResultCache.size())
32477b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        + " rssi=" + Integer.toString(scanResult.level)
32487b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        + " freq=" + Integer.toString(scanResult.frequency)
32497b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        + status);
32507b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            }
32517b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        }
32527b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        return numConfigFound != 0;
3253f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
3254f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3255155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Compare current and new configuration and write to file on change */
3256155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private NetworkUpdateResult writeIpAndProxyConfigurationsOnChange(
3257155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            WifiConfiguration currentConfig,
3258155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            WifiConfiguration newConfig) {
3259155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean ipChanged = false;
3260155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean proxyChanged = false;
3261155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3262f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (VDBG) {
3263f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            loge("writeIpAndProxyConfigurationsOnChange: " + currentConfig.SSID + " -> " +
3264f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    newConfig.SSID + " path: " + ipConfigFile);
3265f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
3266f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3267f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
32683ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim        switch (newConfig.getIpAssignment()) {
3269155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case STATIC:
32703b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti                if (currentConfig.getIpAssignment() != newConfig.getIpAssignment()) {
3271155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    ipChanged = true;
32723b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti                } else {
32733b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti                    ipChanged = !Objects.equals(
32743b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti                            currentConfig.getStaticIpConfiguration(),
32753b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti                            newConfig.getStaticIpConfiguration());
3276155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
3277155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
3278155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case DHCP:
32793ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                if (currentConfig.getIpAssignment() != newConfig.getIpAssignment()) {
3280155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    ipChanged = true;
3281155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
3282155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
3283155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case UNASSIGNED:
3284155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                /* Ignore */
3285155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
3286155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
3287155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                loge("Ignore invalid ip assignment during write");
3288155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
3289155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3290155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
32913ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim        switch (newConfig.getProxySettings()) {
3292155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case STATIC:
3293155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case PAC:
32943b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti                ProxyInfo newHttpProxy = newConfig.getHttpProxy();
32953b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti                ProxyInfo currentHttpProxy = currentConfig.getHttpProxy();
3296155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3297155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (newHttpProxy != null) {
3298155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    proxyChanged = !newHttpProxy.equals(currentHttpProxy);
3299155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
3300155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    proxyChanged = (currentHttpProxy != null);
3301155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
3302155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
3303155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case NONE:
33043ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim                if (currentConfig.getProxySettings() != newConfig.getProxySettings()) {
3305155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    proxyChanged = true;
3306155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
3307155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
3308155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            case UNASSIGNED:
3309155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                /* Ignore */
3310155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
3311155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            default:
3312155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                loge("Ignore invalid proxy configuration during write");
3313155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                break;
3314155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3315155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
33163b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        if (ipChanged) {
33173ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim            currentConfig.setIpAssignment(newConfig.getIpAssignment());
33183b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti            currentConfig.setStaticIpConfiguration(newConfig.getStaticIpConfiguration());
33190ebd6d71cecb5147fad1ca7a7f807aec7ffeddd9Lorenzo Colitti            log("IP config changed SSID = " + currentConfig.SSID);
33200ebd6d71cecb5147fad1ca7a7f807aec7ffeddd9Lorenzo Colitti            if (currentConfig.getStaticIpConfiguration() != null) {
33210ebd6d71cecb5147fad1ca7a7f807aec7ffeddd9Lorenzo Colitti                log(" static configuration: " +
33220ebd6d71cecb5147fad1ca7a7f807aec7ffeddd9Lorenzo Colitti                    currentConfig.getStaticIpConfiguration().toString());
33230ebd6d71cecb5147fad1ca7a7f807aec7ffeddd9Lorenzo Colitti            }
3324155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3325155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
33263b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti        if (proxyChanged) {
33273ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim            currentConfig.setProxySettings(newConfig.getProxySettings());
33283b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti            currentConfig.setHttpProxy(newConfig.getHttpProxy());
3329155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            log("proxy changed SSID = " + currentConfig.SSID);
33303b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti            if (currentConfig.getHttpProxy() != null) {
33313b26801d62a06475b722bbf29cba7f48f376654eLorenzo Colitti                log(" proxyProperties: " + currentConfig.getHttpProxy().toString());
3332155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3333155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3334155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3335155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (ipChanged || proxyChanged) {
3336155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            writeIpAndProxyConfigurations();
3337155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            sendConfiguredNetworksChangedBroadcast(currentConfig,
3338155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    WifiManager.CHANGE_REASON_CONFIG_CHANGE);
3339155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3340155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return new NetworkUpdateResult(ipChanged, proxyChanged);
3341155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
3342155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
33433249e2d02cd39f6e2fee093d6d8f4833a51001d5Lorenzo Colitti    /** Returns true if a particular config key needs to be quoted when passed to the supplicant. */
33443249e2d02cd39f6e2fee093d6d8f4833a51001d5Lorenzo Colitti    private boolean enterpriseConfigKeyShouldBeQuoted(String key) {
33453249e2d02cd39f6e2fee093d6d8f4833a51001d5Lorenzo Colitti        switch (key) {
33463249e2d02cd39f6e2fee093d6d8f4833a51001d5Lorenzo Colitti            case WifiEnterpriseConfig.EAP_KEY:
33473249e2d02cd39f6e2fee093d6d8f4833a51001d5Lorenzo Colitti            case WifiEnterpriseConfig.ENGINE_KEY:
33483249e2d02cd39f6e2fee093d6d8f4833a51001d5Lorenzo Colitti                return false;
33493249e2d02cd39f6e2fee093d6d8f4833a51001d5Lorenzo Colitti            default:
33503249e2d02cd39f6e2fee093d6d8f4833a51001d5Lorenzo Colitti                return true;
33513249e2d02cd39f6e2fee093d6d8f4833a51001d5Lorenzo Colitti        }
33523249e2d02cd39f6e2fee093d6d8f4833a51001d5Lorenzo Colitti    }
33533249e2d02cd39f6e2fee093d6d8f4833a51001d5Lorenzo Colitti
3354155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /**
3355155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * Read the variables from the supplicant daemon that are needed to
3356155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * fill in the WifiConfiguration object.
3357155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     *
3358155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @param config the {@link WifiConfiguration} object to be filled in.
3359155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
3360155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void readNetworkVariables(WifiConfiguration config) {
3361155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3362155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int netId = config.networkId;
3363155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (netId < 0)
3364155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return;
3365155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3366155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
3367155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * TODO: maybe should have a native method that takes an array of
3368155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * variable names and returns an array of values. But we'd still
3369155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * be doing a round trip to the supplicant daemon for each variable.
3370155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
3371155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String value;
3372155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3373155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        value = mWifiNative.getNetworkVariable(netId, WifiConfiguration.ssidVarName);
3374155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(value)) {
3375155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (value.charAt(0) != '"') {
3376155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                config.SSID = "\"" + WifiSsid.createFromHex(value).toString() + "\"";
3377155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //TODO: convert a hex string that is not UTF-8 decodable to a P-formatted
3378155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                //supplicant string
3379155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
3380155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                config.SSID = value;
3381155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3382155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
3383155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            config.SSID = null;
3384155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3385155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3386155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        value = mWifiNative.getNetworkVariable(netId, WifiConfiguration.bssidVarName);
3387155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(value)) {
3388155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            config.BSSID = value;
3389155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
3390155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            config.BSSID = null;
3391155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3392155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3393155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        value = mWifiNative.getNetworkVariable(netId, WifiConfiguration.priorityVarName);
3394155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        config.priority = -1;
3395155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(value)) {
3396155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
3397155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                config.priority = Integer.parseInt(value);
3398155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (NumberFormatException ignore) {
3399155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3400155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3401155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3402155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        value = mWifiNative.getNetworkVariable(netId, WifiConfiguration.hiddenSSIDVarName);
3403155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        config.hiddenSSID = false;
3404155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(value)) {
3405155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
3406155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                config.hiddenSSID = Integer.parseInt(value) != 0;
3407155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (NumberFormatException ignore) {
3408155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3409155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3410155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3411155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        value = mWifiNative.getNetworkVariable(netId, WifiConfiguration.wepTxKeyIdxVarName);
3412155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        config.wepTxKeyIndex = -1;
3413155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(value)) {
3414155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            try {
3415155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                config.wepTxKeyIndex = Integer.parseInt(value);
3416155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } catch (NumberFormatException ignore) {
3417155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3418155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3419155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3420155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (int i = 0; i < 4; i++) {
3421155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            value = mWifiNative.getNetworkVariable(netId,
3422155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    WifiConfiguration.wepKeyVarNames[i]);
3423155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (!TextUtils.isEmpty(value)) {
3424155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                config.wepKeys[i] = value;
3425155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
3426155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                config.wepKeys[i] = null;
3427155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3428155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3429155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3430155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        value = mWifiNative.getNetworkVariable(netId, WifiConfiguration.pskVarName);
3431155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(value)) {
3432155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            config.preSharedKey = value;
3433155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
3434155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            config.preSharedKey = null;
3435155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3436155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3437155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        value = mWifiNative.getNetworkVariable(config.networkId,
3438155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                WifiConfiguration.Protocol.varName);
3439155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(value)) {
3440155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String vals[] = value.split(" ");
3441155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (String val : vals) {
3442155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int index =
3443155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    lookupString(val, WifiConfiguration.Protocol.strings);
3444155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (0 <= index) {
3445155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    config.allowedProtocols.set(index);
3446155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
3447155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3448155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3449155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3450155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        value = mWifiNative.getNetworkVariable(config.networkId,
3451155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                WifiConfiguration.KeyMgmt.varName);
3452155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(value)) {
3453155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String vals[] = value.split(" ");
3454155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (String val : vals) {
3455155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int index =
3456155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    lookupString(val, WifiConfiguration.KeyMgmt.strings);
3457155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (0 <= index) {
3458155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    config.allowedKeyManagement.set(index);
3459155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
3460155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3461155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3462155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3463155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        value = mWifiNative.getNetworkVariable(config.networkId,
3464155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                WifiConfiguration.AuthAlgorithm.varName);
3465155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(value)) {
3466155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String vals[] = value.split(" ");
3467155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (String val : vals) {
3468155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int index =
3469155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    lookupString(val, WifiConfiguration.AuthAlgorithm.strings);
3470155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (0 <= index) {
3471155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    config.allowedAuthAlgorithms.set(index);
3472155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
3473155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3474155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3475155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3476155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        value = mWifiNative.getNetworkVariable(config.networkId,
3477155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                WifiConfiguration.PairwiseCipher.varName);
3478155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(value)) {
3479155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String vals[] = value.split(" ");
3480155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (String val : vals) {
3481155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int index =
3482155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    lookupString(val, WifiConfiguration.PairwiseCipher.strings);
3483155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (0 <= index) {
3484155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    config.allowedPairwiseCiphers.set(index);
3485155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
3486155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3487155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3488155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3489155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        value = mWifiNative.getNetworkVariable(config.networkId,
3490155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                WifiConfiguration.GroupCipher.varName);
3491155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(value)) {
3492155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            String vals[] = value.split(" ");
3493155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            for (String val : vals) {
3494155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                int index =
3495155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    lookupString(val, WifiConfiguration.GroupCipher.strings);
3496155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (0 <= index) {
3497155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    config.allowedGroupCiphers.set(index);
3498155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
3499155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3500155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3501155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3502155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.enterpriseConfig == null) {
3503155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            config.enterpriseConfig = new WifiEnterpriseConfig();
3504155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3505155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        HashMap<String, String> enterpriseFields = config.enterpriseConfig.getFields();
3506155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (String key : ENTERPRISE_CONFIG_SUPPLICANT_KEYS) {
3507155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            value = mWifiNative.getNetworkVariable(netId, key);
3508155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (!TextUtils.isEmpty(value)) {
35093249e2d02cd39f6e2fee093d6d8f4833a51001d5Lorenzo Colitti                if (!enterpriseConfigKeyShouldBeQuoted(key)) {
35103249e2d02cd39f6e2fee093d6d8f4833a51001d5Lorenzo Colitti                    value = removeDoubleQuotes(value);
35113249e2d02cd39f6e2fee093d6d8f4833a51001d5Lorenzo Colitti                }
35123249e2d02cd39f6e2fee093d6d8f4833a51001d5Lorenzo Colitti                enterpriseFields.put(key, value);
3513155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
3514155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                enterpriseFields.put(key, EMPTY_VALUE);
3515155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3516155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3517155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3518155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (migrateOldEapTlsNative(config.enterpriseConfig, netId)) {
3519155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            saveConfig();
3520155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3521155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3522155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        migrateCerts(config.enterpriseConfig);
3523155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // initializeSoftwareKeystoreFlag(config.enterpriseConfig, mKeyStore);
3524155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
3525155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3526155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static String removeDoubleQuotes(String string) {
3527155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int length = string.length();
3528155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if ((length > 1) && (string.charAt(0) == '"')
3529155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                && (string.charAt(length - 1) == '"')) {
3530155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return string.substring(1, length - 1);
3531155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3532155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return string;
3533155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
3534155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3535155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static String makeString(BitSet set, String[] strings) {
3536155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        StringBuffer buf = new StringBuffer();
3537155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int nextSetBit = -1;
3538155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3539155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /* Make sure all set bits are in [0, strings.length) to avoid
3540155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * going out of bounds on strings.  (Shouldn't happen, but...) */
3541155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        set = set.get(0, strings.length);
3542155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3543155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        while ((nextSetBit = set.nextSetBit(nextSetBit + 1)) != -1) {
3544155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            buf.append(strings[nextSetBit].replace('_', '-')).append(' ');
3545155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3546155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3547155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // remove trailing space
3548155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (set.cardinality() > 0) {
3549155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            buf.setLength(buf.length() - 1);
3550155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3551155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3552155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return buf.toString();
3553155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
3554155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3555155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private int lookupString(String string, String[] strings) {
3556155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        int size = strings.length;
3557155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3558155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        string = string.replace('-', '_');
3559155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3560155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (int i = 0; i < size; i++)
3561155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (string.equals(strings[i]))
3562155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return i;
3563155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3564155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // if we ever get here, we should probably add the
3565155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // value to WifiConfiguration to reflect that it's
3566155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // supported by the WPA supplicant
3567155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        loge("Failed to look-up a string: " + string);
3568155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3569155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return -1;
3570155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
3571155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3572f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    /* return the allowed key management based on a scan result */
3573f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3574f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    public WifiConfiguration wifiConfigurationFromScanResult(ScanResult result) {
3575f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration config = new WifiConfiguration();
3576f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3577f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        config.SSID = "\"" + result.SSID + "\"";
3578f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3579f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (VDBG) {
3580f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            loge("WifiConfiguration from scan results " +
3581f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    config.SSID + " cap " + result.capabilities);
3582f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
3583f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (result.capabilities.contains("WEP")) {
3584f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            config.allowedKeyManagement.set(KeyMgmt.NONE);
3585f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); //?
3586f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
3587f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
3588f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3589f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (result.capabilities.contains("PSK")) {
3590f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
3591f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
3592f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3593f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (result.capabilities.contains("EAP")) {
3594f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            //this is probably wrong, as we don't have a way to enter the enterprise config
3595f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
3596f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            config.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
3597f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
3598f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3599f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        config.scanResultCache = new HashMap<String, ScanResult>();
3600f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (config.scanResultCache == null)
3601f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return null;
3602f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        config.scanResultCache.put(result.BSSID, result);
3603f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3604f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        return config;
3605f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
3606f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3607f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3608155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /* Returns a unique for a given configuration */
3609155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private static int configKey(WifiConfiguration config) {
3610f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String key = config.configKey();
3611155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return key.hashCode();
3612155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
3613155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3614155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3615a0708b09ad17b086c008ab100aec7143d7613c80vandwalle        pw.println("Dump of WifiConfigStore");
3616155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("mLastPriority " + mLastPriority);
3617155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println("Configured networks");
3618155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        for (WifiConfiguration conf : getConfiguredNetworks()) {
3619155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            pw.println(conf);
3620155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3621155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        pw.println();
3622155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3623155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mLocalLog != null) {
3624155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            pw.println("WifiConfigStore - Log Begin ----");
3625155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mLocalLog.dump(fd, pw, args);
3626155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            pw.println("WifiConfigStore - Log End ----");
3627155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3628155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
3629155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3630155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    public String getConfigFile() {
3631155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return ipConfigFile;
3632155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
3633155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
36343ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim    protected void loge(String s) {
36350888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        loge(s, false);
36360888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle    }
36370888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle
36380888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle    protected void loge(String s, boolean stack) {
36390888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (stack) {
36400888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            Log.e(TAG, s + " stack:" + Thread.currentThread().getStackTrace()[2].getMethodName()
36410888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    + " - " + Thread.currentThread().getStackTrace()[3].getMethodName()
36420888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    + " - " + Thread.currentThread().getStackTrace()[4].getMethodName()
36430888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    + " - " + Thread.currentThread().getStackTrace()[5].getMethodName());
36440888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        } else {
36450888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            Log.e(TAG, s);
36460888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        }
3647b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim    }
3648b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim
36493ecf5a032e94b6538a56f94a5b33e50cbc464007Jaewan Kim    protected void log(String s) {
3650b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim        Log.d(TAG, s);
3651b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim    }
3652b872cd1d58e1666a9066b136e28c49ed62f825edJaewan Kim
3653155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void localLog(String s) {
3654155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mLocalLog != null) {
3655155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mLocalLog.log(s);
3656155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3657155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
3658155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
36597b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle    private void localLog(String s, boolean force) {
36607b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        localLog(s);
36617b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        if (force) loge(s);
36627b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle    }
36637b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle
3664155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private void localLog(String s, int netId) {
3665155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (mLocalLog == null) {
3666155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return;
3667155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3668155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3669155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        WifiConfiguration config;
3670155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        synchronized(mConfiguredNetworks) {
3671155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            config = mConfiguredNetworks.get(netId);
3672155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3673155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3674155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config != null) {
3675e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle            mLocalLog.log(s + " " + config.getPrintableSsid() + " " + netId
3676e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                    + " status=" + config.status
3677e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                    + " key=" + config.configKey());
3678155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
3679155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mLocalLog.log(s + " " + netId);
3680155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3681155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
3682155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3683155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    // Certificate and private key management for EnterpriseConfig
3684155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static boolean needsKeyStore(WifiEnterpriseConfig config) {
3685155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Has no keys to be installed
3686155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.getClientCertificate() == null && config.getCaCertificate() == null)
3687155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
3688155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
3689155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
3690155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3691155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static boolean isHardwareBackedKey(PrivateKey key) {
3692155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return KeyChain.isBoundKeyAlgorithm(key.getAlgorithm());
3693155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
3694155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3695155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static boolean hasHardwareBackedKey(Certificate certificate) {
3696155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return KeyChain.isBoundKeyAlgorithm(certificate.getPublicKey().getAlgorithm());
3697155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
3698155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3699155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    static boolean needsSoftwareBackedKeyStore(WifiEnterpriseConfig config) {
3700155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String client = config.getClientCertificateAlias();
3701155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(client)) {
3702155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // a valid client certificate is configured
3703155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3704155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // BUGBUG: keyStore.get() never returns certBytes; because it is not
3705155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // taking WIFI_UID as a parameter. It always looks for certificate
3706155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // with SYSTEM_UID, and never finds any Wifi certificates. Assuming that
3707155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // all certificates need software keystore until we get the get() API
3708155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // fixed.
3709155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3710155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return true;
3711155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3712155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3713155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
3714155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
3715155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3716155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) Slog.d(TAG, "Loading client certificate " + Credentials
3717155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    .USER_CERTIFICATE + client);
3718155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3719155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            CertificateFactory factory = CertificateFactory.getInstance("X.509");
3720155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (factory == null) {
3721155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Slog.e(TAG, "Error getting certificate factory");
3722155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return;
3723155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3724155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3725155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            byte[] certBytes = keyStore.get(Credentials.USER_CERTIFICATE + client);
3726155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (certBytes != null) {
3727155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Certificate cert = (X509Certificate) factory.generateCertificate(
3728155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        new ByteArrayInputStream(certBytes));
3729155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3730155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (cert != null) {
3731155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mNeedsSoftwareKeystore = hasHardwareBackedKey(cert);
3732155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3733155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (DBG) Slog.d(TAG, "Loaded client certificate " + Credentials
3734155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            .USER_CERTIFICATE + client);
3735155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    if (DBG) Slog.d(TAG, "It " + (mNeedsSoftwareKeystore ? "needs" :
3736155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                            "does not need" ) + " software key store");
3737155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                } else {
3738155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    Slog.d(TAG, "could not generate certificate");
3739155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
3740155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
3741155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                Slog.e(TAG, "Could not load client certificate " + Credentials
3742155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        .USER_CERTIFICATE + client);
3743155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mNeedsSoftwareKeystore = true;
3744155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3745155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3746155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch(CertificateException e) {
3747155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            Slog.e(TAG, "Could not read certificates");
3748155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mCaCert = null;
3749155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mClientCertificate = null;
3750155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3751155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        */
3752155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3753155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return false;
3754155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
3755155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
375627355a942653264388e909a4276196ee63e57811vandwalle    /** called when CS ask WiFistateMachine to disconnect the current network
375727355a942653264388e909a4276196ee63e57811vandwalle     * because the score is bad.
375827355a942653264388e909a4276196ee63e57811vandwalle     */
375927355a942653264388e909a4276196ee63e57811vandwalle    void handleBadNetworkDisconnectReport(int netId, WifiInfo info) {
376027355a942653264388e909a4276196ee63e57811vandwalle        /* TODO verify the bad network is current */
376127355a942653264388e909a4276196ee63e57811vandwalle        WifiConfiguration config = mConfiguredNetworks.get(netId);
376227355a942653264388e909a4276196ee63e57811vandwalle        if (config != null) {
376327355a942653264388e909a4276196ee63e57811vandwalle            if ((info.getRssi() < WifiConfiguration.UNWANTED_BLACKLIST_SOFT_RSSI_24
376427355a942653264388e909a4276196ee63e57811vandwalle                    && info.is24GHz()) || (info.getRssi() <
376527355a942653264388e909a4276196ee63e57811vandwalle                            WifiConfiguration.UNWANTED_BLACKLIST_SOFT_RSSI_5 && info.is5GHz())) {
3766be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                // We got disconnected and RSSI was bad, so disable light
376727355a942653264388e909a4276196ee63e57811vandwalle                config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_TEMPORARY_DISABLED
376827355a942653264388e909a4276196ee63e57811vandwalle                        + WifiConfiguration.UNWANTED_BLACKLIST_SOFT_BUMP);
376927355a942653264388e909a4276196ee63e57811vandwalle                loge("handleBadNetworkDisconnectReport (+4) "
377027355a942653264388e909a4276196ee63e57811vandwalle                        + Integer.toString(netId) + " " + info);
377127355a942653264388e909a4276196ee63e57811vandwalle            } else {
3772be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                // We got disabled but RSSI is good, so disable hard
377327355a942653264388e909a4276196ee63e57811vandwalle                config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_TEMPORARY_DISABLED
377427355a942653264388e909a4276196ee63e57811vandwalle                        + WifiConfiguration.UNWANTED_BLACKLIST_HARD_BUMP);
377527355a942653264388e909a4276196ee63e57811vandwalle                loge("handleBadNetworkDisconnectReport (+8) "
377627355a942653264388e909a4276196ee63e57811vandwalle                        + Integer.toString(netId) + " " + info);
377727355a942653264388e909a4276196ee63e57811vandwalle            }
377827355a942653264388e909a4276196ee63e57811vandwalle        }
377944d0d289bc2a959a18af3870844d221c93301659vandwalle        // Record last time Connectivity Service switched us away from WiFi and onto Cell
378044d0d289bc2a959a18af3870844d221c93301659vandwalle        lastUnwantedNetworkDisconnectTimestamp = System.currentTimeMillis();
378127355a942653264388e909a4276196ee63e57811vandwalle    }
378227355a942653264388e909a4276196ee63e57811vandwalle
37832451dbcc4f9641df188326215b204b798eb70c46vandwalle    boolean handleBSSIDBlackList(int netId, String BSSID, boolean enable) {
37842451dbcc4f9641df188326215b204b798eb70c46vandwalle        boolean found = false;
378540ff222cec1bd05879edb53abc75c6deead734cavandwalle        if (BSSID == null)
37862451dbcc4f9641df188326215b204b798eb70c46vandwalle            return found;
37872451dbcc4f9641df188326215b204b798eb70c46vandwalle
37882451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Look for the BSSID in our config store
37892451dbcc4f9641df188326215b204b798eb70c46vandwalle        for (WifiConfiguration config : mConfiguredNetworks.values()) {
37902451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (config.scanResultCache != null) {
37912451dbcc4f9641df188326215b204b798eb70c46vandwalle                for (ScanResult result: config.scanResultCache.values()) {
37922451dbcc4f9641df188326215b204b798eb70c46vandwalle                    if (result.BSSID.equals(BSSID)) {
37932451dbcc4f9641df188326215b204b798eb70c46vandwalle                        if (enable) {
37940d616ef3bf635dff8722e064c0be842676390ed8vandwalle                            result.setAutoJoinStatus(ScanResult.ENABLED);
37952451dbcc4f9641df188326215b204b798eb70c46vandwalle                        } else {
37960d616ef3bf635dff8722e064c0be842676390ed8vandwalle                            // Black list the BSSID we were trying to join
37972451dbcc4f9641df188326215b204b798eb70c46vandwalle                            // so as the Roam state machine
37982451dbcc4f9641df188326215b204b798eb70c46vandwalle                            // doesn't pick it up over and over
37990d616ef3bf635dff8722e064c0be842676390ed8vandwalle                            result.setAutoJoinStatus(ScanResult.AUTO_ROAM_DISABLED);
38002451dbcc4f9641df188326215b204b798eb70c46vandwalle                            found = true;
38012451dbcc4f9641df188326215b204b798eb70c46vandwalle                        }
380240ff222cec1bd05879edb53abc75c6deead734cavandwalle                    }
380340ff222cec1bd05879edb53abc75c6deead734cavandwalle                }
380440ff222cec1bd05879edb53abc75c6deead734cavandwalle            }
380540ff222cec1bd05879edb53abc75c6deead734cavandwalle        }
38062451dbcc4f9641df188326215b204b798eb70c46vandwalle        return found;
380740ff222cec1bd05879edb53abc75c6deead734cavandwalle    }
380840ff222cec1bd05879edb53abc75c6deead734cavandwalle
3809c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    int getMaxDhcpRetries() {
3810c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        return Settings.Global.getInt(mContext.getContentResolver(),
3811c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                Settings.Global.WIFI_MAX_DHCP_RETRY_COUNT,
3812c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                DEFAULT_MAX_DHCP_RETRIES);
3813c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    }
3814c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
3815e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle    void handleSSIDStateChange(int netId, boolean enabled, String message, String BSSID) {
3816f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration config = mConfiguredNetworks.get(netId);
38170888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        if (config != null) {
38180888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle            if (enabled) {
38190888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                loge("SSID re-enabled for  " + config.configKey() +
38200888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        " had autoJoinStatus=" + Integer.toString(config.autoJoinStatus)
38210888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        + " self added " + config.selfAdded + " ephemeral " + config.ephemeral);
3822931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                //TODO: http://b/16381983 Fix Wifi Network Blacklisting
382340ff222cec1bd05879edb53abc75c6deead734cavandwalle                //TODO: really I don't know if re-enabling is right but we
382440ff222cec1bd05879edb53abc75c6deead734cavandwalle                //TODO: should err on the side of trying to connect
38254dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                //TODO: even if the attempt will fail
38260888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                if (config.autoJoinStatus == WifiConfiguration.AUTO_JOIN_DISABLED_ON_AUTH_FAILURE) {
382727355a942653264388e909a4276196ee63e57811vandwalle                    config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
38280888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                }
3829453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            } else {
38300888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                loge("SSID temp disabled for  " + config.configKey() +
38310888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        " had autoJoinStatus=" + Integer.toString(config.autoJoinStatus)
38320888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        + " self added " + config.selfAdded + " ephemeral " + config.ephemeral);
38334bcdcfd19e3eaaacc54d8ec65149c6d7faa181afvandwalle                if (message != null) {
383440ff222cec1bd05879edb53abc75c6deead734cavandwalle                    loge(" message=" + message);
38350888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                }
38364dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                if (config.selfAdded && config.lastConnected == 0) {
3837931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // This is a network we self added, and we never succeeded,
3838931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // the user did not create this network and never entered its credentials,
3839931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // so we want to be very aggressive in disabling it completely.
384070468b47454c8657e8963932f2e08a3f4d7e3881vandwalle                    removeConfigAndSendBroadcastIfNeeded(config.networkId);
38410888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                } else {
38420888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                    if (message != null) {
38430eebae7334d6129f7ca1344e4b20199794994358vandwalle                        if (message.contains("no identity")) {
38440eebae7334d6129f7ca1344e4b20199794994358vandwalle                            config.setAutoJoinStatus(
38450eebae7334d6129f7ca1344e4b20199794994358vandwalle                                    WifiConfiguration.AUTO_JOIN_DISABLED_NO_CREDENTIALS);
38460eebae7334d6129f7ca1344e4b20199794994358vandwalle                            if (DBG) {
38470eebae7334d6129f7ca1344e4b20199794994358vandwalle                                loge("no identity blacklisted " + config.configKey() + " to "
38480eebae7334d6129f7ca1344e4b20199794994358vandwalle                                        + Integer.toString(config.autoJoinStatus));
38490eebae7334d6129f7ca1344e4b20199794994358vandwalle                            }
38500eebae7334d6129f7ca1344e4b20199794994358vandwalle                        } else if (message.contains("WRONG_KEY")
38510888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                                || message.contains("AUTH_FAILED")) {
3852931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                            // This configuration has received an auth failure, so disable it
3853931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                            // temporarily because we don't want auto-join to try it out.
3854931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                            // this network may be re-enabled by the "usual"
38550888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            // enableAllNetwork function
3856e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            config.numAuthFailures++;
3857e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            if (config.numAuthFailures > maxAuthErrorsToBlacklist) {
3858e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                config.setAutoJoinStatus
3859e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                        (WifiConfiguration.AUTO_JOIN_DISABLED_ON_AUTH_FAILURE);
3860e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                disableNetwork(netId,
3861e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                        WifiConfiguration.DISABLED_AUTH_FAILURE);
3862e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                loge("Authentication failure, blacklist " + config.configKey() + " "
3863e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                            + Integer.toString(config.networkId)
3864e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                            + " num failures " + config.numAuthFailures);
3865b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            }
3866c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                        } else if (message.contains("DHCP FAILURE")) {
3867e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            config.numIpConfigFailures++;
3868c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                            config.lastConnectionFailure = System.currentTimeMillis();
3869c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                            int maxRetries = getMaxDhcpRetries();
3870c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                            // maxRetries == 0 means keep trying forever
3871e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            if (maxRetries > 0 && config.numIpConfigFailures > maxRetries) {
3872c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                                /**
3873c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                                 * If we've exceeded the maximum number of retries for DHCP
3874c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                                 * to a given network, disable the network
3875c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                                 */
38768c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                                config.setAutoJoinStatus
38778c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                                        (WifiConfiguration.AUTO_JOIN_DISABLED_ON_AUTH_FAILURE);
3878c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                                disableNetwork(netId, WifiConfiguration.DISABLED_DHCP_FAILURE);
3879e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                loge("DHCP failure, blacklist " + config.configKey() + " "
3880e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                        + Integer.toString(config.networkId)
3881e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                        + " num failures " + config.numIpConfigFailures);
3882c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                            }
3883e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
3884e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            // Also blacklist the BSSId if we find it
3885e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            ScanResult result = null;
3886e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            String bssidDbg = "";
3887e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            if (config.scanResultCache != null && BSSID != null) {
3888e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                result = config.scanResultCache.get(BSSID);
3889e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            }
3890e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            if (result != null) {
3891e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                result.numIpConfigFailures ++;
3892e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                bssidDbg = BSSID + " ipfail=" + result.numIpConfigFailures;
3893e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                if (result.numIpConfigFailures > 3) {
3894e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                    // Tell supplicant to stop trying this BSSID
3895a5283c01ac413e378e2261050cac3a6d6e9cf385vandwalle                                    mWifiNative.addToBlacklist(BSSID);
3896e0ba94ba9abde13173ae0de8c8939aa4eb9a9085vandwalle                                    result.setAutoJoinStatus(ScanResult.AUTO_JOIN_DISABLED);
3897e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                }
3898e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            }
3899e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
3900c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                            if (DBG) {
3901c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                                loge("blacklisted " + config.configKey() + " to "
3902c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                                        + config.autoJoinStatus
3903e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                        + " due to IP config failures, count="
3904a5283c01ac413e378e2261050cac3a6d6e9cf385vandwalle                                        + config.numIpConfigFailures
3905a5283c01ac413e378e2261050cac3a6d6e9cf385vandwalle                                        + " disableReason=" + config.disableReason
3906a5283c01ac413e378e2261050cac3a6d6e9cf385vandwalle                                        + " " + bssidDbg);
3907e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            }
3908e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        } else if (message.contains("CONN_FAILED")) {
3909e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            config.numConnectionFailures++;
3910e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            if (config.numConnectionFailures > maxConnectionErrorsToBlacklist) {
3911e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                config.setAutoJoinStatus
3912e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                        (WifiConfiguration.AUTO_JOIN_DISABLED_ON_AUTH_FAILURE);
3913e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                disableNetwork(netId,
3914e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                        WifiConfiguration.DISABLED_ASSOCIATION_REJECT);
3915e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                loge("Connection failure, blacklist " + config.configKey() + " "
3916e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                        + config.networkId
3917e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                                        + " num failures " + config.numConnectionFailures);
3918c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                            }
39194bcdcfd19e3eaaacc54d8ec65149c6d7faa181afvandwalle                        }
39200888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        message.replace("\n", "");
39210888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        message.replace("\r", "");
39220888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        config.lastFailure = message;
3923453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    }
3924453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                }
3925ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            }
3926f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
3927f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
3928f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
3929155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    boolean installKeys(WifiEnterpriseConfig config, String name) {
3930155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        boolean ret = true;
3931155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String privKeyName = Credentials.USER_PRIVATE_KEY + name;
3932155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String userCertName = Credentials.USER_CERTIFICATE + name;
3933155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String caCertName = Credentials.CA_CERTIFICATE + name;
3934155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.getClientCertificate() != null) {
3935155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            byte[] privKeyData = config.getClientPrivateKey().getEncoded();
3936155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (isHardwareBackedKey(config.getClientPrivateKey())) {
3937155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // Hardware backed key store is secure enough to store keys un-encrypted, this
3938155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // removes the need for user to punch a PIN to get access to these keys
3939155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (DBG) Log.d(TAG, "importing keys " + name + " in hardware backed store");
3940155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ret = mKeyStore.importKey(privKeyName, privKeyData, android.os.Process.WIFI_UID,
3941155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        KeyStore.FLAG_NONE);
3942155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            } else {
3943155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // Software backed key store is NOT secure enough to store keys un-encrypted.
3944155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // Save keys encrypted so they are protected with user's PIN. User will
3945155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // have to unlock phone before being able to use these keys and connect to
3946155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // networks.
3947155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (DBG) Log.d(TAG, "importing keys " + name + " in software backed store");
3948155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                ret = mKeyStore.importKey(privKeyName, privKeyData, Process.WIFI_UID,
3949155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        KeyStore.FLAG_ENCRYPTED);
3950155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3951155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (ret == false) {
3952155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return ret;
3953155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3954155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3955155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            ret = putCertInKeyStore(userCertName, config.getClientCertificate());
3956155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (ret == false) {
3957155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                // Remove private key installed
3958155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mKeyStore.delKey(privKeyName, Process.WIFI_UID);
3959155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return ret;
3960155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3961155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3962155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3963155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.getCaCertificate() != null) {
3964155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            ret = putCertInKeyStore(caCertName, config.getCaCertificate());
3965155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (ret == false) {
3966155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                if (config.getClientCertificate() != null) {
3967155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    // Remove client key+cert
3968155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mKeyStore.delKey(privKeyName, Process.WIFI_UID);
3969155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    mKeyStore.delete(userCertName, Process.WIFI_UID);
3970155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                }
3971155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return ret;
3972155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
3973155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3974155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3975155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Set alias names
3976155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.getClientCertificate() != null) {
3977155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            config.setClientCertificateAlias(name);
3978155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            config.resetClientKeyEntry();
3979155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3980155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3981155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (config.getCaCertificate() != null) {
3982155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            config.setCaCertificateAlias(name);
3983155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            config.resetCaCertificate();
3984155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
3985155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3986155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return ret;
3987155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
3988155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3989155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    private boolean putCertInKeyStore(String name, Certificate cert) {
3990155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        try {
3991155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            byte[] certData = Credentials.convertToPem(cert);
3992155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) Log.d(TAG, "putting certificate " + name + " in keystore");
3993155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return mKeyStore.put(name, certData, Process.WIFI_UID, KeyStore.FLAG_NONE);
3994155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
3995155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (IOException e1) {
3996155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
3997155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } catch (CertificateException e2) {
3998155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
3999155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
4000155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
4001155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4002155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    void removeKeys(WifiEnterpriseConfig config) {
4003155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String client = config.getClientCertificateAlias();
4004155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // a valid client certificate is configured
4005155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(client)) {
4006155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) Log.d(TAG, "removing client private key and user cert");
4007155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mKeyStore.delKey(Credentials.USER_PRIVATE_KEY + client, Process.WIFI_UID);
4008155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mKeyStore.delete(Credentials.USER_CERTIFICATE + client, Process.WIFI_UID);
4009155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
4010155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4011155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String ca = config.getCaCertificateAlias();
4012155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // a valid ca certificate is configured
4013155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(ca)) {
4014155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (DBG) Log.d(TAG, "removing CA cert");
4015155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            mKeyStore.delete(Credentials.CA_CERTIFICATE + ca, Process.WIFI_UID);
4016155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
4017155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
4018155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4019155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4020155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Migrates the old style TLS config to the new config style. This should only be used
4021155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * when restoring an old wpa_supplicant.conf or upgrading from a previous
4022155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * platform version.
4023155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @return true if the config was updated
4024155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     * @hide
4025155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande     */
4026155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    boolean migrateOldEapTlsNative(WifiEnterpriseConfig config, int netId) {
4027155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String oldPrivateKey = mWifiNative.getNetworkVariable(netId, OLD_PRIVATE_KEY_NAME);
4028155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
4029155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * If the old configuration value is not present, then there is nothing
4030155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         * to do.
4031155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande         */
4032155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (TextUtils.isEmpty(oldPrivateKey)) {
4033155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            return false;
4034155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
4035155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            // Also ignore it if it's empty quotes.
4036155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            oldPrivateKey = removeDoubleQuotes(oldPrivateKey);
4037155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (TextUtils.isEmpty(oldPrivateKey)) {
4038155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                return false;
4039155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
4040155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
4041155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4042155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        config.setFieldValue(WifiEnterpriseConfig.ENGINE_KEY, WifiEnterpriseConfig.ENGINE_ENABLE);
4043155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        config.setFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY,
4044155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                WifiEnterpriseConfig.ENGINE_ID_KEYSTORE);
4045155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4046155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        /*
4047155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        * The old key started with the keystore:// URI prefix, but we don't
4048155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        * need that anymore. Trim it off if it exists.
4049155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        */
4050155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        final String keyName;
4051155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (oldPrivateKey.startsWith(WifiEnterpriseConfig.KEYSTORE_URI)) {
4052155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            keyName = new String(
4053155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                    oldPrivateKey.substring(WifiEnterpriseConfig.KEYSTORE_URI.length()));
4054155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        } else {
4055155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            keyName = oldPrivateKey;
4056155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
4057155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        config.setFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY, keyName);
4058155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4059155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiNative.setNetworkVariable(netId, WifiEnterpriseConfig.ENGINE_KEY,
4060155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                config.getFieldValue(WifiEnterpriseConfig.ENGINE_KEY, ""));
4061155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4062155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiNative.setNetworkVariable(netId, WifiEnterpriseConfig.ENGINE_ID_KEY,
4063155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                config.getFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY, ""));
4064155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4065155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiNative.setNetworkVariable(netId, WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY,
4066155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                config.getFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY, ""));
4067155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4068155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // Remove old private_key string so we don't run this again.
4069155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        mWifiNative.setNetworkVariable(netId, OLD_PRIVATE_KEY_NAME, EMPTY_VALUE);
4070155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4071155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        return true;
4072155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
4073155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4074155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    /** Migrate certs from global pool to wifi UID if not already done */
4075155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    void migrateCerts(WifiEnterpriseConfig config) {
4076155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String client = config.getClientCertificateAlias();
4077155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // a valid client certificate is configured
4078155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(client)) {
4079155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (!mKeyStore.contains(Credentials.USER_PRIVATE_KEY + client, Process.WIFI_UID)) {
4080155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mKeyStore.duplicate(Credentials.USER_PRIVATE_KEY + client, -1,
4081155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Credentials.USER_PRIVATE_KEY + client, Process.WIFI_UID);
4082155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mKeyStore.duplicate(Credentials.USER_CERTIFICATE + client, -1,
4083155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Credentials.USER_CERTIFICATE + client, Process.WIFI_UID);
4084155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
4085155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
4086155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4087155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        String ca = config.getCaCertificateAlias();
4088155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        // a valid ca certificate is configured
4089155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        if (!TextUtils.isEmpty(ca)) {
4090155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            if (!mKeyStore.contains(Credentials.CA_CERTIFICATE + ca, Process.WIFI_UID)) {
4091155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                mKeyStore.duplicate(Credentials.CA_CERTIFICATE + ca, -1,
4092155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande                        Credentials.CA_CERTIFICATE + ca, Process.WIFI_UID);
4093155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande            }
4094155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande        }
4095155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande    }
4096155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande
4097155b9d09ef9b8ead3ca617afdd91e74070d3f0cbVinit Deshpande}
4098