WifiAutoJoinController.java revision cb59612d2af82044ee74e8f595c9a40498db14b4
1f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle/*
262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle * Copyright (C) 2014 The Android Open Source Project
3f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle *
4f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * Licensed under the Apache License, Version 2.0 (the "License");
5f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * you may not use this file except in compliance with the License.
6f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * You may obtain a copy of the License at
7f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle *
8f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle *      http://www.apache.org/licenses/LICENSE-2.0
9f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle *
10f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * Unless required by applicable law or agreed to in writing, software
11f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * distributed under the License is distributed on an "AS IS" BASIS,
12f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * See the License for the specific language governing permissions and
14f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * limitations under the License.
15f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle */
16f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
17f22d23092ab37286a5ef9d257d5bb32c421d2669vandwallepackage com.android.server.wifi;
18f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
19f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.content.Context;
20f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.net.NetworkKey;
21f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.net.NetworkScoreManager;
220c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalleimport android.net.WifiKey;
2377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport android.net.wifi.ScanResult;
2477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport android.net.wifi.WifiConfiguration;
258639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidsonimport android.net.wifi.WifiConfiguration.KeyMgmt;
2677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport android.net.wifi.WifiConnectionStatistics;
27f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalleimport android.os.Process;
2877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport android.provider.Settings;
29c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalleimport android.text.TextUtils;
30f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.util.Log;
31e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalleimport android.os.SystemClock;
32f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
33ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.anqp.ANQPElement;
34ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.anqp.Constants;
35ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.ANQPData;
36ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.AnqpCache;
37ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.Chronograph;
38ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.NetworkDetail;
39ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.PasspointMatch;
40ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.PasspointMatchInfo;
41ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.SupplicantBridge;
42ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport com.android.server.wifi.hotspot2.pps.HomeSP;
43ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist
4477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport java.io.BufferedReader;
4577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport java.io.IOException;
4677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport java.io.StringReader;
470c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalleimport java.util.ArrayList;
4877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport java.util.Arrays;
49d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpandeimport java.util.BitSet;
50ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport java.util.Collection;
51dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport java.util.Collections;
52dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalleimport java.util.Comparator;
53f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.HashMap;
5477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvistimport java.util.Iterator;
55f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.List;
56ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvistimport java.util.Map;
57f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
58f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle/**
59f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * AutoJoin controller is responsible for WiFi Connect decision
60f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle *
61f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * It runs in the thread context of WifiStateMachine
62f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle *
63f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle */
64f22d23092ab37286a5ef9d257d5bb32c421d2669vandwallepublic class WifiAutoJoinController {
65f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
66f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private Context mContext;
67f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private WifiStateMachine mWifiStateMachine;
68f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private WifiConfigStore mWifiConfigStore;
69f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private WifiNative mWifiNative;
70f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
71f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private NetworkScoreManager scoreManager;
72f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private WifiNetworkScoreCache mNetworkScoreCache;
73f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
74f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String TAG = "WifiAutoJoinController ";
7531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist    private static boolean DBG = false;
76ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    private static boolean VDBG = false;
77f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final boolean mStaStaSupported = false;
78f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
79c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    public static int mScanResultMaximumAge = 40000; /* milliseconds unit */
80833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle    public static int mScanResultAutoJoinAge = 5000; /* milliseconds unit */
81c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
82453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle    private String mCurrentConfigurationKey = null; //used by autojoin
83f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
8425ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande    private final HashMap<String, ScanDetail> scanResultCache = new HashMap<>();
85f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
86c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    private WifiConnectionStatistics mWifiConnectionStatistics;
87c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
88dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    /**
89dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * Whether to allow connections to untrusted networks.
90dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     */
918639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    private boolean mAllowUntrustedConnections = false;
928639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson
93c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    /* For debug purpose only: if the scored override a score */
94c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    boolean didOverride = false;
95c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
96931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    // Lose the non-auth failure blacklisting after 8 hours
974dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle    private final static long loseBlackListHardMilli = 1000 * 60 * 60 * 8;
98931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    // Lose some temporary blacklisting after 30 minutes
994dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle    private final static long loseBlackListSoftMilli = 1000 * 60 * 30;
10027355a942653264388e909a4276196ee63e57811vandwalle
101dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    /**
102dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * @see android.provider.Settings.Global#WIFI_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS
103dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     */
10416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson    private static final long DEFAULT_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS = 1000 * 60; // 1 minute
10516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
106b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle    public static final int AUTO_JOIN_IDLE = 0;
107b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle    public static final int AUTO_JOIN_ROAMING = 1;
108b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle    public static final int AUTO_JOIN_EXTENDED_ROAMING = 2;
109b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle    public static final int AUTO_JOIN_OUT_OF_NETWORK_ROAMING = 3;
110b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle
11197b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle    public static final int HIGH_THRESHOLD_MODIFIER = 5;
11297b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle
11387df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle    public static final int MAX_RSSI_DELTA = 50;
11487df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle
1151ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    // Below are AutoJoin wide parameters indicating if we should be aggressive before joining
1161ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    // weak network. Note that we cannot join weak network that are going to be marked as unanted by
1171ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    // ConnectivityService because this will trigger link flapping.
1181ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    /**
1191ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle     * There was a non-blacklisted configuration that we bailed from because of a weak signal
1201ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle     */
1211ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    boolean didBailDueToWeakRssi = false;
1221ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    /**
1231ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle     * number of time we consecutively bailed out of an eligible network because its signal
1241ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle     * was too weak
1251ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle     */
1261ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    int weakRssiBailCount = 0;
1271ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle
128f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    WifiAutoJoinController(Context c, WifiStateMachine w, WifiConfigStore s,
129c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                           WifiConnectionStatistics st, WifiNative n) {
130f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mContext = c;
131f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mWifiStateMachine = w;
132f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mWifiConfigStore = s;
133f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mWifiNative = n;
134f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mNetworkScoreCache = null;
135c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        mWifiConnectionStatistics = st;
13621bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle        scoreManager =
13721bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle                (NetworkScoreManager) mContext.getSystemService(Context.NETWORK_SCORE_SERVICE);
138f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (scoreManager == null)
139f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            logDbg("Registered scoreManager NULL " + " service " + Context.NETWORK_SCORE_SERVICE);
140f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
141f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (scoreManager != null) {
142f13817203179f41620514718c8668ae7e418f8afJeff Davidson            mNetworkScoreCache = new WifiNetworkScoreCache(mContext);
143f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            scoreManager.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, mNetworkScoreCache);
144f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } else {
145f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            logDbg("No network score service: Couldnt register as a WiFi score Manager, type="
146f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    + Integer.toString(NetworkKey.TYPE_WIFI)
147f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    + " service " + Context.NETWORK_SCORE_SERVICE);
148f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            mNetworkScoreCache = null;
149f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
150f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
151f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
152ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    void enableVerboseLogging(int verbose) {
153dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (verbose > 0) {
154abde872adced15dfb6781fb71959453d963326dbYuhao Zheng            DBG = true;
155ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            VDBG = true;
156ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        } else {
157abde872adced15dfb6781fb71959453d963326dbYuhao Zheng            DBG = false;
158ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            VDBG = false;
159ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
160ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
161ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
162931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
163931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     * Flush out scan results older than mScanResultMaximumAge
164931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     */
165f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private void ageScanResultsOut(int delay) {
166f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (delay <= 0) {
167931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            delay = mScanResultMaximumAge; // Something sane
168f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
169b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        long milli = System.currentTimeMillis();
170f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (VDBG) {
171f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            logDbg("ageScanResultsOut delay " + Integer.valueOf(delay) + " size "
172f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    + Integer.valueOf(scanResultCache.size()) + " now " + Long.valueOf(milli));
173f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
174f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
175dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        Iterator<HashMap.Entry<String, ScanDetail>> iter = scanResultCache.entrySet().iterator();
176f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        while (iter.hasNext()) {
177dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            HashMap.Entry<String, ScanDetail> entry = iter.next();
17825ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande            ScanDetail scanDetail = entry.getValue();
17925ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande            if ((scanDetail.getSeen() + delay) < milli) {
180f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                iter.remove();
181f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
182f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
183f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
184f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
185ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist
186d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande    void averageRssiAndRemoveFromCache(ScanResult result) {
187d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande        // Fetch the previous instance for this result
18825ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande        ScanDetail sd = scanResultCache.get(result.BSSID);
18925ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande        if (sd != null) {
19025ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande            ScanResult sr = sd.getScanResult();
191d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            if (mWifiConfigStore.scanResultRssiLevelPatchUp != 0
192d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande                    && result.level == 0
193d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande                    && sr.level < -20) {
194d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande                // A 'zero' RSSI reading is most likely a chip problem which returns
195d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande                // an unknown RSSI, hence ignore it
196d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande                result.level = sr.level;
197ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist            }
198ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist
199d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            // If there was a previous cache result for this BSSID, average the RSSI values
200d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            result.averageRssi(sr.level, sr.seen, mScanResultMaximumAge);
201ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist
202d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            // Remove the previous Scan Result - this is not necessary
203d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            scanResultCache.remove(result.BSSID);
204d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande        } else if (mWifiConfigStore.scanResultRssiLevelPatchUp != 0 && result.level == 0) {
205d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            // A 'zero' RSSI reading is most likely a chip problem which returns
206d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            // an unknown RSSI, hence initialize it to a sane value
207d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            result.level = mWifiConfigStore.scanResultRssiLevelPatchUp;
208ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist        }
209ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist    }
210ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist
211d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande    void addToUnscoredNetworks(ScanResult result, List<NetworkKey> unknownScanResults) {
212d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande        WifiKey wkey;
213d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande        // Quoted SSIDs are the only one valid at this stage
214d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande        try {
215d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            wkey = new WifiKey("\"" + result.SSID + "\"", result.BSSID);
216d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande        } catch (IllegalArgumentException e) {
217d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            logDbg("AutoJoinController: received badly encoded SSID=[" + result.SSID +
218d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande                    "] ->skipping this network");
219d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            wkey = null;
220d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande        }
221d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande        if (wkey != null) {
222d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            NetworkKey nkey = new NetworkKey(wkey);
223d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            //if we don't know this scan result then request a score from the scorer
224d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            unknownScanResults.add(nkey);
225ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist        }
226d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande        if (VDBG) {
227d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            String cap = "";
228d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            if (result.capabilities != null)
229d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande                cap = result.capabilities;
230d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            logDbg(result.SSID + " " + result.BSSID + " rssi="
2310510c350db16f6bd2930f0a5ace483efbfa681d5Pierre Vandwalle                    + result.level + " cap " + cap + " tsf " + result.timestamp + " is not scored");
232ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist        }
233ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist    }
234ef1567e413c9ed5f5c4fdb9e354861632f7b2f87Jan Nordqvist
23577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    int addToScanCache(List<ScanDetail> scanList) {
236be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        int numScanResultsKnown = 0; // Record number of scan results we knew about
237be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        WifiConfiguration associatedConfig = null;
2387b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        boolean didAssociate = false;
2398242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle        long now = System.currentTimeMillis();
240f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
2410c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle        ArrayList<NetworkKey> unknownScanResults = new ArrayList<NetworkKey>();
2420c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle
243dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        for (ScanDetail scanDetail : scanList) {
24477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            ScanResult result = scanDetail.getScanResult();
2451fcf3c6d2b9ed65573e1e7c55fc5a30ebd364c4fYuhao Zheng            if (result.SSID == null) continue;
246c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
247e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle            if (VDBG) {
248e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle                logDbg(" addToScanCache " + result.SSID + " " + result.BSSID
249e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle                        + " tsf=" + result.timestamp
250cb59612d2af82044ee74e8f595c9a40498db14b4Pierre Vandwalle                        + " now= " + now + " uptime=" + SystemClock.uptimeMillis()
251cb59612d2af82044ee74e8f595c9a40498db14b4Pierre Vandwalle                        + " elapsed=" + SystemClock.elapsedRealtime());
2520510c350db16f6bd2930f0a5ace483efbfa681d5Pierre Vandwalle            }
2530510c350db16f6bd2930f0a5ace483efbfa681d5Pierre Vandwalle
254c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            // Make sure we record the last time we saw this result
25525ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande            scanDetail.setSeen();
256f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
257d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            averageRssiAndRemoveFromCache(result);
258e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle
259e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle            if (!mNetworkScoreCache.isScoredNetwork(result)) {
260d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande                addToUnscoredNetworks(result, unknownScanResults);
2610c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle            } else {
262e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                if (VDBG) {
2637b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    String cap = "";
2647b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    if (result.capabilities != null)
2657b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        cap = result.capabilities;
266e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    int score = mNetworkScoreCache.getNetworkScore(result);
267e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    logDbg(result.SSID + " " + result.BSSID + " rssi="
2687b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                            + result.level + " cap " + cap + " is scored : " + score);
2690c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                }
270f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
271f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
272e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // scanResultCache.put(result.BSSID, new ScanResult(result));
27325ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande            scanResultCache.put(result.BSSID, scanDetail);
274be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            // Add this BSSID to the scanResultCache of a Saved WifiConfiguration
275d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            didAssociate = mWifiConfigStore.updateSavedNetworkHistory(scanDetail);
276f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
277be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            // If not successful, try to associate this BSSID to an existing Saved WifiConfiguration
2787b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            if (!didAssociate) {
279be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                // We couldn't associate the scan result to a Saved WifiConfiguration
280c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                // Hence it is untrusted
281c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                result.untrusted = true;
2820d616ef3bf635dff8722e064c0be842676390ed8vandwalle            } else {
2830d616ef3bf635dff8722e064c0be842676390ed8vandwalle                // If the scan result has been blacklisted fir 18 hours -> unblacklist
2840d616ef3bf635dff8722e064c0be842676390ed8vandwalle                if ((now - result.blackListTimestamp) > loseBlackListHardMilli) {
2850d616ef3bf635dff8722e064c0be842676390ed8vandwalle                    result.setAutoJoinStatus(ScanResult.ENABLED);
2860d616ef3bf635dff8722e064c0be842676390ed8vandwalle                }
287f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
2887b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            if (didAssociate) {
289be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                numScanResultsKnown++;
290dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                result.isAutoJoinCandidate++;
291a0708b09ad17b086c008ab100aec7143d7613c80vandwalle            } else {
292a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                result.isAutoJoinCandidate = 0;
293be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            }
294f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
2950c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle
2960c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle        if (unknownScanResults.size() != 0) {
2970c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle            NetworkKey[] newKeys =
2980c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                    unknownScanResults.toArray(new NetworkKey[unknownScanResults.size()]);
299931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Kick the score manager, we will get updated scores asynchronously
3000c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle            scoreManager.requestScores(newKeys);
3010c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle        }
302be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        return numScanResultsKnown;
303f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
304f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
305f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void logDbg(String message) {
3060888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        logDbg(message, false);
307ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle    }
308ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle
309ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle    void logDbg(String message, boolean stackTrace) {
310ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        if (stackTrace) {
31133f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande            Log.d(TAG, message + " stack:"
312ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[2].getMethodName() + " - "
313ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[3].getMethodName() + " - "
314ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[4].getMethodName() + " - "
315ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[5].getMethodName());
316ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        } else {
31733f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande            Log.d(TAG, message);
318ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        }
319f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
320f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
321931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    // Called directly from WifiStateMachine
322be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle    int newSupplicantResults(boolean doAutoJoin) {
323be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        int numScanResultsKnown;
32477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        List<ScanDetail> scanList = mWifiStateMachine.getScanResultsListNoCopyUnsync();
325be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        numScanResultsKnown = addToScanCache(scanList);
326f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        ageScanResultsOut(mScanResultMaximumAge);
327be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        if (DBG) {
328be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            logDbg("newSupplicantResults size=" + Integer.valueOf(scanResultCache.size())
329dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    + " known=" + numScanResultsKnown + " "
330dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    + doAutoJoin);
331be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        }
3327806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle        if (doAutoJoin) {
3337806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            attemptAutoJoin();
3347806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle        }
335005c1ef113192f898499a407dd266393a8d6b076vandwalle        mWifiConfigStore.writeKnownNetworkHistory(false);
336be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        return numScanResultsKnown;
337f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
338f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
339f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
340931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
341931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     * Not used at the moment
342f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * should be a call back from WifiScanner HAL ??
343f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * this function is not hooked and working yet, it will receive scan results from WifiScanners
344f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * with the list of IEs,then populate the capabilities by parsing the IEs and inject the scan
345f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * results as normal.
346f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     */
347f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void newHalScanResults() {
34877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        List<ScanDetail> scanList = null;//mWifiScanner.syncGetScanResultsList();
349f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String akm = WifiParser.parse_akm(null, null);
350f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        logDbg(akm);
351f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        addToScanCache(scanList);
352f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        ageScanResultsOut(0);
353f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        attemptAutoJoin();
354005c1ef113192f898499a407dd266393a8d6b076vandwalle        mWifiConfigStore.writeKnownNetworkHistory(false);
355f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
356f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
357931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
358dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * network link quality changed, called directly from WifiTrafficPoller,
359931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     * or by listening to Link Quality intent
360931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     */
361f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void linkQualitySignificantChange() {
362f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        attemptAutoJoin();
363f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
364f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
365931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
366f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * compare a WifiConfiguration against the current network, return a delta score
367f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * If not associated, and the candidate will always be better
368f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * For instance if the candidate is a home network versus an unknown public wifi,
369f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * the delta will be infinite, else compare Kepler scores etc…
370b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle     * Negatve return values from this functions are meaningless per se, just trying to
371e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle     * keep them distinct for debug purpose (i.e. -1, -2 etc...)
372931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     */
373e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle    private int compareNetwork(WifiConfiguration candidate,
374e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                               String lastSelectedConfiguration) {
375b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        if (candidate == null)
376b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle            return -3;
377b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle
378f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration currentNetwork = mWifiStateMachine.getCurrentWifiConfiguration();
379b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        if (currentNetwork == null) {
380c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // Return any absurdly high score, if we are not connected there is no current
381c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // network to...
382dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return 1000;
383b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        }
384f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
385f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (candidate.configKey(true).equals(currentNetwork.configKey(true))) {
386b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle            return -2;
387f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
388f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
389b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle        if (DBG) {
390e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            logDbg("compareNetwork will compare " + candidate.configKey()
391e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    + " with current " + currentNetwork.configKey());
392b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle        }
393833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle        int order = compareWifiConfigurations(currentNetwork, candidate);
394e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
395e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        // The lastSelectedConfiguration is the configuration the user has manually selected
396e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        // thru WifiPicker, or that a 3rd party app asked us to connect to via the
397e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        // enableNetwork with disableOthers=true WifiManager API
398e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        // As this is a direct user choice, we strongly prefer this configuration,
399e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        // hence give +/-100
400e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        if ((lastSelectedConfiguration != null)
401e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                && currentNetwork.configKey().equals(lastSelectedConfiguration)) {
402e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // currentNetwork is the last selected configuration,
403e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // so keep it above connect choices (+/-60) and
404e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // above RSSI/scorer based selection of linked configuration (+/- 50)
405e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // by reducing order by -100
406e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            order = order - 100;
407dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (VDBG) {
408e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                logDbg("     ...and prefers -100 " + currentNetwork.configKey()
409e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " over " + candidate.configKey()
410e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " because it is the last selected -> "
411e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + Integer.toString(order));
412e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            }
413e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        } else if ((lastSelectedConfiguration != null)
414e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                && candidate.configKey().equals(lastSelectedConfiguration)) {
415e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // candidate is the last selected configuration,
416e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // so keep it above connect choices (+/-60) and
417e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // above RSSI/scorer based selection of linked configuration (+/- 50)
418e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // by increasing order by +100
419e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            order = order + 100;
420dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (VDBG) {
421e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                logDbg("     ...and prefers +100 " + candidate.configKey()
422e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " over " + currentNetwork.configKey()
423e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " because it is the last selected -> "
424e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + Integer.toString(order));
425e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            }
426e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        }
427e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
428ede507649471f1113e9e1919812115ca5a6bc0c8vandwalle        return order;
429f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
430f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
431ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle    /**
432ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * update the network history fields fo that configuration
433ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * - if userTriggered, we mark the configuration as "non selfAdded" since the user has seen it
434ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * and took over management
435ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * - if it is a "connect", remember which network were there at the point of the connect, so
436ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * as those networks get a relative lower score than the selected configuration
43762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle     *
438ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * @param netId
439ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * @param userTriggered : if the update come from WiFiManager
440dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * @param connect       : if the update includes a connect
441931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     */
44262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle    public void updateConfigurationHistory(int netId, boolean userTriggered, boolean connect) {
443f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration selected = mWifiConfigStore.getWifiConfiguration(netId);
444f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (selected == null) {
445c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            logDbg("updateConfigurationHistory nid=" + netId + " no selected configuration!");
446f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return;
447f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
448f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
449e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle        if (selected.SSID == null) {
450c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            logDbg("updateConfigurationHistory nid=" + netId +
451c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    " no SSID in selected configuration!");
452e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle            return;
453e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle        }
454e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle
45562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle        if (userTriggered) {
456931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Reenable autojoin for this network,
45762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            // since the user want to connect to this configuration
45827355a942653264388e909a4276196ee63e57811vandwalle            selected.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
45962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            selected.selfAdded = false;
460e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            selected.dirty = true;
46162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle        }
462f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
463992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle        if (DBG && userTriggered) {
464f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (selected.connectChoices != null) {
465ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                logDbg("updateConfigurationHistory will update "
466f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        + Integer.toString(netId) + " now: "
467992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        + Integer.toString(selected.connectChoices.size())
468992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        + " uid=" + Integer.toString(selected.creatorUid), true);
469f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else {
470ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                logDbg("updateConfigurationHistory will update "
471992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        + Integer.toString(netId)
472992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        + " uid=" + Integer.toString(selected.creatorUid), true);
473f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
474f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
475f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
476ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        if (connect && userTriggered) {
477ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            boolean found = false;
4782451dbcc4f9641df188326215b204b798eb70c46vandwalle            int choice = 0;
479c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            int size = 0;
4809f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
4819f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            // Reset the triggered disabled count, because user wanted to connect to this
4829f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            // configuration, and we were not.
4839f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            selected.numUserTriggeredWifiDisableLowRSSI = 0;
4849f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            selected.numUserTriggeredWifiDisableBadRSSI = 0;
4859f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            selected.numUserTriggeredWifiDisableNotHighRSSI = 0;
4869f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            selected.numUserTriggeredJoinAttempts++;
4879f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
48862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            List<WifiConfiguration> networks =
48962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    mWifiConfigStore.getRecentConfiguredNetworks(12000, false);
490c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            if (networks != null) size = networks.size();
491c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            logDbg("updateConfigurationHistory found " + size + " networks");
49262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            if (networks != null) {
49362f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                for (WifiConfiguration config : networks) {
494992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    if (DBG) {
495992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        logDbg("updateConfigurationHistory got " + config.SSID + " nid="
496992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                + Integer.toString(config.networkId));
497992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    }
498f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
49962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    if (selected.configKey(true).equals(config.configKey(true))) {
500ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        found = true;
50162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                        continue;
50262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    }
503f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
50487df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle                    // If the selection was made while config was visible with reasonably good RSSI
50587df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle                    // then register the user preference, else ignore
50687df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle                    if (config.visibility == null ||
50787df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle                            (config.visibility.rssi24 < mWifiConfigStore.thresholdBadRssi24.get()
50887df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle                            && config.visibility.rssi5 < mWifiConfigStore.thresholdBadRssi5.get())
50987df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle                    ) {
51087df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle                        continue;
51162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    }
512f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
51387df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle                    choice = MAX_RSSI_DELTA + 10; // Make sure the choice overrides the RSSI diff
51487df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle
515931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // The selected configuration was preferred over a recently seen config
516931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // hence remember the user's choice:
517931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // add the recently seen config to the selected's connectChoices array
518ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle
519ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    if (selected.connectChoices == null) {
520ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        selected.connectChoices = new HashMap<String, Integer>();
521ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    }
522ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle
523ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    logDbg("updateConfigurationHistory add a choice " + selected.configKey(true)
5240888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            + " over " + config.configKey(true)
5252451dbcc4f9641df188326215b204b798eb70c46vandwalle                            + " choice " + Integer.toString(choice));
526cf5b8eb8a08c45bd4a82f1f4bb789c8a1b08744fvandwalle
5272f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    // Add the visible config to the selected's connect choice list
5282f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    selected.connectChoices.put(config.configKey(true), choice);
529f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
53062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    if (config.connectChoices != null) {
531ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        if (VDBG) {
532ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                            logDbg("updateConfigurationHistory will remove "
53362f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                                    + selected.configKey(true) + " from " + config.configKey(true));
534ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        }
535931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                        // Remove the selected from the recently seen config's connectChoice list
53662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                        config.connectChoices.remove(selected.configKey(true));
5370888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle
5380888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        if (selected.linkedConfigurations != null) {
539dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                            // Remove the selected's linked configuration from the
540dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                            // recently seen config's connectChoice list
541dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                            for (String key : selected.linkedConfigurations.keySet()) {
542dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                                config.connectChoices.remove(key);
543dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                            }
5440888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        }
54562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    }
546ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                }
547ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                if (found == false) {
548dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    // We haven't found the configuration that the user just selected in our
549dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    // scan cache.
550dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    // In that case we will need a new scan before attempting to connect to this
551dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    // configuration anyhow and thus we can process the scan results then.
552dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    logDbg("updateConfigurationHistory try to connect to an old network!! : "
553dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                            + selected.configKey());
55462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                }
555f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
55662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                if (selected.connectChoices != null) {
55762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    if (VDBG)
558ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        logDbg("updateConfigurationHistory " + Integer.toString(netId)
55962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                                + " now: " + Integer.toString(selected.connectChoices.size()));
56062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                }
561f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
562f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
563992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle
564931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        // TODO: write only if something changed
565992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle        if (userTriggered || connect) {
566005c1ef113192f898499a407dd266393a8d6b076vandwalle            mWifiConfigStore.writeKnownNetworkHistory(false);
567992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle        }
568f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
569f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
57021d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle    int getConnectChoice(WifiConfiguration source, WifiConfiguration target, boolean strict) {
57121d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle        int choice = 0;
5722451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (source == null || target == null) {
5732451dbcc4f9641df188326215b204b798eb70c46vandwalle            return 0;
574f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
575f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
5762451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (source.connectChoices != null
5772451dbcc4f9641df188326215b204b798eb70c46vandwalle                && source.connectChoices.containsKey(target.configKey(true))) {
578a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle            Integer val = source.connectChoices.get(target.configKey(true));
579a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle            if (val != null) {
580a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle                choice = val;
581a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle            }
5822451dbcc4f9641df188326215b204b798eb70c46vandwalle        } else if (source.linkedConfigurations != null) {
583f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            for (String key : source.linkedConfigurations.keySet()) {
584f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                WifiConfiguration config = mWifiConfigStore.getWifiConfiguration(key);
585f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (config != null) {
586f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (config.connectChoices != null) {
587a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle                        Integer val = config.connectChoices.get(target.configKey(true));
588a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle                        if (val != null) {
589a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle                            choice = val;
590a20305612380c956fa84b1e80523eb7719f62fd1Pierre Vandwalle                        }
591f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
592f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
593f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
5942451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
5952451dbcc4f9641df188326215b204b798eb70c46vandwalle
59621d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle        if (!strict && choice == 0) {
59733f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande            // We didn't find the connect choice; fallback to some default choices
59833f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande            int sourceScore = getSecurityScore(source);
59933f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande            int targetScore = getSecurityScore(target);
6008eefaf2c281b44fb7a4d951ba587f447800c4b4fVinit Deshpande            choice = sourceScore - targetScore;
60133f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande        }
60233f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande
60333f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande        return choice;
6042451dbcc4f9641df188326215b204b798eb70c46vandwalle    }
6052451dbcc4f9641df188326215b204b798eb70c46vandwalle
60633f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande    int compareWifiConfigurationsFromVisibility(WifiConfiguration.Visibility a, int aRssiBoost,
607dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                                                String dbgA, WifiConfiguration.Visibility b, int bRssiBoost, String dbgB) {
6082451dbcc4f9641df188326215b204b798eb70c46vandwalle
609815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        int aRssiBoost5 = 0; // 5GHz RSSI boost to apply for purpose band selection (5GHz pref)
610815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        int bRssiBoost5 = 0; // 5GHz RSSI boost to apply for purpose band selection (5GHz pref)
611815788ba7838fc54310baed3deb9b95548e0ce69vandwalle
612815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        int aScore = 0;
613815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        int bScore = 0;
614815788ba7838fc54310baed3deb9b95548e0ce69vandwalle
615815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        boolean aPrefers5GHz = false;
616815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        boolean bPrefers5GHz = false;
6174dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
6182451dbcc4f9641df188326215b204b798eb70c46vandwalle        /**
619815788ba7838fc54310baed3deb9b95548e0ce69vandwalle         * Calculate a boost to apply to RSSI value of configuration we want to join on 5GHz:
620815788ba7838fc54310baed3deb9b95548e0ce69vandwalle         * Boost RSSI value of 5GHz bands iff the base value is better than threshold,
621815788ba7838fc54310baed3deb9b95548e0ce69vandwalle         * penalize the RSSI value of 5GHz band iff the base value is lower than threshold
6222451dbcc4f9641df188326215b204b798eb70c46vandwalle         * This implements band preference where we prefer 5GHz if RSSI5 is good enough, whereas
6232451dbcc4f9641df188326215b204b798eb70c46vandwalle         * we prefer 2.4GHz otherwise.
6242451dbcc4f9641df188326215b204b798eb70c46vandwalle         */
62533f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande        aRssiBoost5 = rssiBoostFrom5GHzRssi(a.rssi5, dbgA + "->");
62633f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande        bRssiBoost5 = rssiBoostFrom5GHzRssi(b.rssi5, dbgB + "->");
6272451dbcc4f9641df188326215b204b798eb70c46vandwalle
628815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        // Select which band to use for a
62933f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande        if (a.rssi5 + aRssiBoost5 > a.rssi24) {
6302451dbcc4f9641df188326215b204b798eb70c46vandwalle            // Prefer a's 5GHz
631815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            aPrefers5GHz = true;
632815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        }
633815788ba7838fc54310baed3deb9b95548e0ce69vandwalle
634815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        // Select which band to use for b
63533f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande        if (b.rssi5 + bRssiBoost5 > b.rssi24) {
636815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            // Prefer b's 5GHz
637815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            bPrefers5GHz = true;
638815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        }
639815788ba7838fc54310baed3deb9b95548e0ce69vandwalle
640815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        if (aPrefers5GHz) {
641815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            if (bPrefers5GHz) {
642815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // If both a and b are on 5GHz then we don't apply the 5GHz RSSI boost to either
643815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // one, but directly compare the RSSI values, this improves stability,
644815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // since the 5GHz RSSI boost can introduce large fluctuations
64533f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande                aScore = a.rssi5 + aRssiBoost;
646815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            } else {
647815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // If only a is on 5GHz, then apply the 5GHz preference boost to a
64833f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande                aScore = a.rssi5 + aRssiBoost + aRssiBoost5;
649815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            }
6502451dbcc4f9641df188326215b204b798eb70c46vandwalle        } else {
65133f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande            aScore = a.rssi24 + aRssiBoost;
652f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
6532451dbcc4f9641df188326215b204b798eb70c46vandwalle
654815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        if (bPrefers5GHz) {
655815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            if (aPrefers5GHz) {
656815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // If both a and b are on 5GHz then we don't apply the 5GHz RSSI boost to either
657815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // one, but directly compare the RSSI values, this improves stability,
658815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // since the 5GHz RSSI boost can introduce large fluctuations
65933f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande                bScore = b.rssi5 + bRssiBoost;
660815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            } else {
661815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // If only b is on 5GHz, then apply the 5GHz preference boost to b
66233f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande                bScore = b.rssi5 + bRssiBoost + bRssiBoost5;
663815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            }
664815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        } else {
66533f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande            bScore = b.rssi24 + bRssiBoost;
666815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        }
66733f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande
668815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        if (VDBG) {
66933f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande            logDbg("        " + dbgA + " is5=" + aPrefers5GHz + " score=" + aScore
67033f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande                    + " " + dbgB + " is5=" + bPrefers5GHz + " score=" + bScore);
671815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        }
672f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle
673f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle        // Debug only, record RSSI comparison parameters
67433f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande        if (a != null) {
67533f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande            a.score = aScore;
67633f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande            a.currentNetworkBoost = aRssiBoost;
67733f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande            a.bandPreferenceBoost = aRssiBoost5;
678f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle        }
67933f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande        if (b != null) {
68033f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande            b.score = bScore;
68133f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande            b.currentNetworkBoost = bRssiBoost;
68233f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande            b.bandPreferenceBoost = bRssiBoost5;
683f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle        }
684f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle
685815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        // Compare a and b
686815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        // If a score is higher then a > b and the order is descending (negative)
687815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        // If b score is higher then a < b and the order is ascending (positive)
688815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        return bScore - aScore;
689f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
690f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
6912451dbcc4f9641df188326215b204b798eb70c46vandwalle    // Compare WifiConfiguration by RSSI, and return a comparison value in the range [-50, +50]
6922451dbcc4f9641df188326215b204b798eb70c46vandwalle    // The result represents "approximately" an RSSI difference measured in dBM
6932451dbcc4f9641df188326215b204b798eb70c46vandwalle    // Adjusted with various parameters:
6942451dbcc4f9641df188326215b204b798eb70c46vandwalle    // +) current network gets a +15 boost
6952451dbcc4f9641df188326215b204b798eb70c46vandwalle    // +) 5GHz signal, if they are strong enough, get a +15 or +25 boost, representing the
6962451dbcc4f9641df188326215b204b798eb70c46vandwalle    // fact that at short range we prefer 5GHz band as it is cleaner of interference and
6972451dbcc4f9641df188326215b204b798eb70c46vandwalle    // provides for wider channels
6982451dbcc4f9641df188326215b204b798eb70c46vandwalle    int compareWifiConfigurationsRSSI(WifiConfiguration a, WifiConfiguration b,
6992451dbcc4f9641df188326215b204b798eb70c46vandwalle                                      String currentConfiguration) {
700f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        int order = 0;
7012451dbcc4f9641df188326215b204b798eb70c46vandwalle
7022451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Boost used so as to favor current config
7032451dbcc4f9641df188326215b204b798eb70c46vandwalle        int aRssiBoost = 0;
7042451dbcc4f9641df188326215b204b798eb70c46vandwalle        int bRssiBoost = 0;
7052451dbcc4f9641df188326215b204b798eb70c46vandwalle
7062451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Retrieve the visibility
707f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration.Visibility astatus = a.visibility;
708f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration.Visibility bstatus = b.visibility;
709f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (astatus == null || bstatus == null) {
7102451dbcc4f9641df188326215b204b798eb70c46vandwalle            // Error visibility wasn't set
711b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("    compareWifiConfigurations NULL band status!");
712f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return 0;
713f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
7142451dbcc4f9641df188326215b204b798eb70c46vandwalle
7152451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Apply Hysteresis, boost RSSI of current configuration
7162451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (null != currentConfiguration) {
7172451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (a.configKey().equals(currentConfiguration)) {
71893a1fddee50a244d31036cddae6b7db6630fd93dvandwalle                aRssiBoost = mWifiConfigStore.currentNetworkBoost;
7192451dbcc4f9641df188326215b204b798eb70c46vandwalle            } else if (b.configKey().equals(currentConfiguration)) {
72093a1fddee50a244d31036cddae6b7db6630fd93dvandwalle                bRssiBoost = mWifiConfigStore.currentNetworkBoost;
7212451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
7222451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
7232451dbcc4f9641df188326215b204b798eb70c46vandwalle
724dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (VDBG) {
725b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("    compareWifiConfigurationsRSSI: " + a.configKey()
726dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                            + " rssi=" + Integer.toString(astatus.rssi24)
727dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                            + "," + Integer.toString(astatus.rssi5)
728dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                            + " boost=" + Integer.toString(aRssiBoost)
729dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                            + " " + b.configKey() + " rssi="
730dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                            + Integer.toString(bstatus.rssi24) + ","
731dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                            + Integer.toString(bstatus.rssi5)
732dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                            + " boost=" + Integer.toString(bRssiBoost)
7332451dbcc4f9641df188326215b204b798eb70c46vandwalle            );
734f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
7352451dbcc4f9641df188326215b204b798eb70c46vandwalle
73633f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande        order = compareWifiConfigurationsFromVisibility(
73733f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande                a.visibility, aRssiBoost, a.configKey(),
73833f9ad2442844df078dde3ae67961e7132a8a48dVinit Deshpande                b.visibility, bRssiBoost, b.configKey());
7392451dbcc4f9641df188326215b204b798eb70c46vandwalle
74087df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle        // Normalize the order to [-50, +50] = [ -MAX_RSSI_DELTA, MAX_RSSI_DELTA]
74187df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle        if (order > MAX_RSSI_DELTA) order = MAX_RSSI_DELTA;
74287df79e235c64f7062d0c50658fe1999b1225435Pierre Vandwalle        else if (order < -MAX_RSSI_DELTA) order = -MAX_RSSI_DELTA;
7432451dbcc4f9641df188326215b204b798eb70c46vandwalle
7442451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (VDBG) {
7452451dbcc4f9641df188326215b204b798eb70c46vandwalle            String prefer = " = ";
7462451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (order > 0) {
7472451dbcc4f9641df188326215b204b798eb70c46vandwalle                prefer = " < "; // Ascending
7482451dbcc4f9641df188326215b204b798eb70c46vandwalle            } else if (order < 0) {
7492451dbcc4f9641df188326215b204b798eb70c46vandwalle                prefer = " > "; // Descending
7502451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
751b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("    compareWifiConfigurationsRSSI " + a.configKey()
7522451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " rssi=(" + a.visibility.rssi24
7532451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + a.visibility.rssi5
7542451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + ") num=(" + a.visibility.num24
7552451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + a.visibility.num5 + ")"
7562451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + prefer + b.configKey()
7572451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " rssi=(" + b.visibility.rssi24
7582451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + b.visibility.rssi5
7592451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + ") num=(" + b.visibility.num24
7602451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + b.visibility.num5 + ")"
7612451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " -> " + order);
7622451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
7632451dbcc4f9641df188326215b204b798eb70c46vandwalle
764f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        return order;
765f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
766f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
767833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle    /**
768833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle     * b/18490330 only use scorer for untrusted networks
769dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * <p/>
770dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * int compareWifiConfigurationsWithScorer(WifiConfiguration a, WifiConfiguration b) {
771dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * <p/>
772dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * boolean aIsActive = false;
773dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * boolean bIsActive = false;
774dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * <p/>
775dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * // Apply Hysteresis : boost RSSI of current configuration before
776dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * // looking up the score
777dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * if (null != mCurrentConfigurationKey) {
778dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * if (a.configKey().equals(mCurrentConfigurationKey)) {
779dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * aIsActive = true;
780dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * } else if (b.configKey().equals(mCurrentConfigurationKey)) {
781dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * bIsActive = true;
782dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * }
783dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * }
784dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * int scoreA = getConfigNetworkScore(a, mScanResultAutoJoinAge, aIsActive);
785dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * int scoreB = getConfigNetworkScore(b, mScanResultAutoJoinAge, bIsActive);
786dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * <p/>
787dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * // Both configurations need to have a score for the scorer to be used
788dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * // ...and the scores need to be different:-)
789dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * if (scoreA == WifiNetworkScoreCache.INVALID_NETWORK_SCORE
790dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * || scoreB == WifiNetworkScoreCache.INVALID_NETWORK_SCORE) {
791dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * if (VDBG)  {
792dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * logDbg("    compareWifiConfigurationsWithScorer no-scores: "
793dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * + a.configKey()
794dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * + " "
795dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * + b.configKey());
796dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * }
797dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * return 0;
798dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * }
799dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * <p/>
800dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * if (VDBG) {
801dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * String prefer = " = ";
802dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * if (scoreA < scoreB) {
803dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * prefer = " < ";
804dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * } if (scoreA > scoreB) {
805dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * prefer = " > ";
806dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * }
807dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * logDbg("    compareWifiConfigurationsWithScorer " + a.configKey()
808dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * + " rssi=(" + a.visibility.rssi24
809dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * + "," + a.visibility.rssi5
810dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * + ") num=(" + a.visibility.num24
811dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * + "," + a.visibility.num5 + ")"
812dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * + " sc=" + scoreA
813dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * + prefer + b.configKey()
814dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * + " rssi=(" + b.visibility.rssi24
815dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * + "," + b.visibility.rssi5
816dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * + ") num=(" + b.visibility.num24
817dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * + "," + b.visibility.num5 + ")"
818dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * + " sc=" + scoreB
819dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * + " -> " + Integer.toString(scoreB - scoreA));
820dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * }
821dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * <p/>
822dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * // If scoreA > scoreB, the comparison is descending hence the return value is negative
823dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * return scoreB - scoreA;
824dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * }
825833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle     */
8262451dbcc4f9641df188326215b204b798eb70c46vandwalle
827d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande    int getSecurityScore(WifiConfiguration config) {
828d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande
829d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande        if (TextUtils.isEmpty(config.SSID) == false) {
830d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP)
831d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande                    || config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)
832d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande                    || config.allowedKeyManagement.get(KeyMgmt.WPA2_PSK)) {
833d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande                /* enterprise or PSK networks get highest score */
834d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande                return 100;
835d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            } else if (config.allowedKeyManagement.get(KeyMgmt.NONE)) {
836d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande                /* open networks have lowest score */
837d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande                return 33;
838d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            }
839d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande        } else if (TextUtils.isEmpty(config.FQDN) == false) {
840d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            /* passpoint networks have medium preference */
841d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande            return 66;
842d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande        }
843d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande
844d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande        /* bad network */
845d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande        return 0;
846d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande    }
847d3fb9cbb12d013dd70e672ace5c41ab18a3679a0Vinit Deshpande
848f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    int compareWifiConfigurations(WifiConfiguration a, WifiConfiguration b) {
849f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        int order = 0;
850f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        boolean linked = false;
851f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
852453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        if ((a.linkedConfigurations != null) && (b.linkedConfigurations != null)
853453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                && (a.autoJoinStatus == WifiConfiguration.AUTO_JOIN_ENABLED)
854453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                && (b.autoJoinStatus == WifiConfiguration.AUTO_JOIN_ENABLED)) {
8552451dbcc4f9641df188326215b204b798eb70c46vandwalle            if ((a.linkedConfigurations.get(b.configKey(true)) != null)
8562451dbcc4f9641df188326215b204b798eb70c46vandwalle                    && (b.linkedConfigurations.get(a.configKey(true)) != null)) {
857f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                linked = true;
858f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
859f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
860f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
861f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (a.ephemeral && b.ephemeral == false) {
862f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG) {
863b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    compareWifiConfigurations ephemeral and prefers " + b.configKey()
864453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + " over " + a.configKey());
865f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
866931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            return 1; // b is of higher priority - ascending
867f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
868f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (b.ephemeral && a.ephemeral == false) {
869f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG) {
870b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    compareWifiConfigurations ephemeral and prefers " + a.configKey()
871453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + " over " + b.configKey());
872f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
873931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            return -1; // a is of higher priority - descending
874f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
875f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
8762451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Apply RSSI, in the range [-5, +5]
8772451dbcc4f9641df188326215b204b798eb70c46vandwalle        // after band adjustment, +n difference roughly corresponds to +10xn dBm
8782451dbcc4f9641df188326215b204b798eb70c46vandwalle        order = order + compareWifiConfigurationsRSSI(a, b, mCurrentConfigurationKey);
879f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
8802451dbcc4f9641df188326215b204b798eb70c46vandwalle        // If the configurations are not linked, compare by user's choice, only a
8812451dbcc4f9641df188326215b204b798eb70c46vandwalle        // very high RSSI difference can then override the choice
8822451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (!linked) {
8832451dbcc4f9641df188326215b204b798eb70c46vandwalle            int choice;
884f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
88521d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle            choice = getConnectChoice(a, b, false);
8862451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (choice > 0) {
887931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // a is of higher priority - descending
8882451dbcc4f9641df188326215b204b798eb70c46vandwalle                order = order - choice;
889f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (VDBG) {
890b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    logDbg("    compareWifiConfigurations prefers " + a.configKey()
891453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                            + " over " + b.configKey()
892b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + " due to user choice of " + choice
893b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + " order -> " + Integer.toString(order));
894f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
895f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                if (a.visibility != null) {
896f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                    a.visibility.lastChoiceBoost = choice;
897f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                    a.visibility.lastChoiceConfig = b.configKey();
898f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                }
8992451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
9002451dbcc4f9641df188326215b204b798eb70c46vandwalle
90121d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle            choice = getConnectChoice(b, a, false);
9022451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (choice > 0) {
903931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // a is of lower priority - ascending
9042451dbcc4f9641df188326215b204b798eb70c46vandwalle                order = order + choice;
9054dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                if (VDBG) {
906b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    logDbg("    compareWifiConfigurations prefers " + b.configKey() + " over "
907b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + a.configKey() + " due to user choice of " + choice
908b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + " order ->" + Integer.toString(order));
909f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
910f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                if (b.visibility != null) {
911f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                    b.visibility.lastChoiceBoost = choice;
912f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                    b.visibility.lastChoiceConfig = a.configKey();
913f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                }
914f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
915f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
916ede1310be531a84faa08f02c3fd243448dd936ddvandwalle
917f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (order == 0) {
918931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // We don't know anything - pick the last seen i.e. K behavior
919931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // we should do this only for recently picked configurations
920f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (a.priority > b.priority) {
921931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // a is of higher priority - descending
9222451dbcc4f9641df188326215b204b798eb70c46vandwalle                if (VDBG) {
923b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    logDbg("    compareWifiConfigurations prefers -1 " + a.configKey() + " over "
924453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                            + b.configKey() + " due to priority");
925f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
926f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
927f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                order = -1;
928f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else if (a.priority < b.priority) {
929931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // a is of lower priority - ascending
9302451dbcc4f9641df188326215b204b798eb70c46vandwalle                if (VDBG) {
931b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    logDbg("    compareWifiConfigurations prefers +1 " + b.configKey() + " over "
932453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                            + a.configKey() + " due to priority");
933f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
9342451dbcc4f9641df188326215b204b798eb70c46vandwalle                order = 1;
935f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
936f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
937f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
938f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String sorder = " == ";
939931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        if (order > 0) {
940f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            sorder = " < ";
941931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        } else if (order < 0) {
942f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            sorder = " > ";
943931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        }
944f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
9452451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (VDBG) {
946b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("compareWifiConfigurations: " + a.configKey() + sorder
947453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    + b.configKey() + " order " + Integer.toString(order));
948f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
949f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
950f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        return order;
951f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
952f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
953c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    boolean isBadCandidate(int rssi5, int rssi24) {
954c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        return (rssi5 < -80 && rssi24 < -90);
955c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    }
956c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
957833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle    /*
958c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    int compareWifiConfigurationsTop(WifiConfiguration a, WifiConfiguration b) {
959c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        int scorerOrder = compareWifiConfigurationsWithScorer(a, b);
960c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        int order = compareWifiConfigurations(a, b);
961c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
962c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        if (scorerOrder * order < 0) {
963b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            if (VDBG) {
964b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    -> compareWifiConfigurationsTop: " +
965b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                        "scorer override " + scorerOrder + " " + order);
966b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            }
967c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // For debugging purpose, remember that an override happened
968c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // during that autojoin Attempt
969c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            didOverride = true;
970c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            a.numScorerOverride++;
971c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            b.numScorerOverride++;
972c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        }
973c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
974c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        if (scorerOrder != 0) {
975c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // If the scorer came up with a result then use the scorer's result, else use
976c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // the order provided by the base comparison function
977c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            order = scorerOrder;
978c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        }
979c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        return order;
980c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    }
981833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle    */
982c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
9839f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    public int rssiBoostFrom5GHzRssi(int rssi, String dbg) {
9840eebae7334d6129f7ca1344e4b20199794994358vandwalle        if (!mWifiConfigStore.enable5GHzPreference) {
9850eebae7334d6129f7ca1344e4b20199794994358vandwalle            return 0;
9860eebae7334d6129f7ca1344e4b20199794994358vandwalle        }
987e67ec726c07410073575473c0f50dc737629f5davandwalle        if (rssi
98877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                > mWifiConfigStore.bandPreferenceBoostThreshold5.get()) {
989e67ec726c07410073575473c0f50dc737629f5davandwalle            // Boost by 2 dB for each point
990e67ec726c07410073575473c0f50dc737629f5davandwalle            //    Start boosting at -65
991e67ec726c07410073575473c0f50dc737629f5davandwalle            //    Boost by 20 if above -55
992e67ec726c07410073575473c0f50dc737629f5davandwalle            //    Boost by 40 if abore -45
9930eebae7334d6129f7ca1344e4b20199794994358vandwalle            int boost = mWifiConfigStore.bandPreferenceBoostFactor5
994dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    * (rssi - mWifiConfigStore.bandPreferenceBoostThreshold5.get());
995e67ec726c07410073575473c0f50dc737629f5davandwalle            if (boost > 50) {
996815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // 50 dB boost allows jumping from 2.4 to 5GHz
997e67ec726c07410073575473c0f50dc737629f5davandwalle                // consistently
998e67ec726c07410073575473c0f50dc737629f5davandwalle                boost = 50;
999e67ec726c07410073575473c0f50dc737629f5davandwalle            }
10009f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            if (VDBG && dbg != null) {
1001f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                logDbg("        " + dbg + ":    rssi5 " + rssi + " 5GHz-boost " + boost);
10029f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            }
1003e67ec726c07410073575473c0f50dc737629f5davandwalle            return boost;
1004e67ec726c07410073575473c0f50dc737629f5davandwalle        }
1005e67ec726c07410073575473c0f50dc737629f5davandwalle
1006e67ec726c07410073575473c0f50dc737629f5davandwalle        if (rssi
100777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                < mWifiConfigStore.bandPreferencePenaltyThreshold5.get()) {
10080eebae7334d6129f7ca1344e4b20199794994358vandwalle            // penalize if < -75
10090eebae7334d6129f7ca1344e4b20199794994358vandwalle            int boost = mWifiConfigStore.bandPreferencePenaltyFactor5
1010dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    * (rssi - mWifiConfigStore.bandPreferencePenaltyThreshold5.get());
10110eebae7334d6129f7ca1344e4b20199794994358vandwalle            return boost;
1012e67ec726c07410073575473c0f50dc737629f5davandwalle        }
1013e67ec726c07410073575473c0f50dc737629f5davandwalle        return 0;
1014e67ec726c07410073575473c0f50dc737629f5davandwalle    }
1015dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
1016dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    /**
1017dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * attemptRoam() function implements the core of the same SSID switching algorithm
1018dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * <p/>
1019dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * Run thru all recent scan result of a WifiConfiguration and select the
1020dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * best one.
1021dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     */
1022e67ec726c07410073575473c0f50dc737629f5davandwalle    public ScanResult attemptRoam(ScanResult a,
1023e67ec726c07410073575473c0f50dc737629f5davandwalle                                  WifiConfiguration current, int age, String currentBSSID) {
1024b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        if (current == null) {
1025dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (VDBG) {
1026b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                logDbg("attemptRoam not associated");
1027b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            }
1028e67ec726c07410073575473c0f50dc737629f5davandwalle            return a;
10294dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
103025ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande
10314d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande        ScanDetailCache scanDetailCache =
103225ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande                mWifiConfigStore.getScanDetailCache(current);
103325ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande
103425ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande        if (scanDetailCache == null) {
1035dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (VDBG) {
1036b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                logDbg("attemptRoam no scan cache");
1037b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            }
1038e67ec726c07410073575473c0f50dc737629f5davandwalle            return a;
10394dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
104025ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande        if (scanDetailCache.size() > 6) {
1041dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (VDBG) {
1042c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                logDbg("attemptRoam scan cache size "
104325ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande                        + scanDetailCache.size() + " --> bail");
1044b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            }
1045931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Implement same SSID roaming only for configurations
1046c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle            // that have less than 4 BSSIDs
1047e67ec726c07410073575473c0f50dc737629f5davandwalle            return a;
10484dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
1049b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle
10502f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle        if (current.BSSID != null && !current.BSSID.equals("any")) {
1051dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (DBG) {
10522f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                logDbg("attemptRoam() BSSID is set "
10532f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        + current.BSSID + " -> bail");
1054b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            }
1055e67ec726c07410073575473c0f50dc737629f5davandwalle            return a;
10564dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
10574dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1058931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        // Determine which BSSID we want to associate to, taking account
1059c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        // relative strength of 5 and 2.4 GHz BSSIDs
10602451dbcc4f9641df188326215b204b798eb70c46vandwalle        long nowMs = System.currentTimeMillis();
10614dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
106225ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande        for (ScanDetail sd : scanDetailCache.values()) {
106325ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande            ScanResult b = sd.getScanResult();
106497b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            int bRssiBoost5 = 0;
106597b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            int aRssiBoost5 = 0;
106697b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            int bRssiBoost = 0;
106797b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            int aRssiBoost = 0;
106825ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande            if ((sd.getSeen() == 0) || (b.BSSID == null)
106925ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande                    || ((nowMs - sd.getSeen()) > age)
1070e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    || b.autoJoinStatus != ScanResult.ENABLED
1071e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    || b.numIpConfigFailures > 8) {
10724dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                continue;
10733a2a3d226881cce8a4e511302231d843b0def303vandwalle            }
10744dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1075931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Pick first one
10764dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            if (a == null) {
10774dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                a = b;
10784dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                continue;
10794dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
10804dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1081e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            if (b.numIpConfigFailures < (a.numIpConfigFailures - 1)) {
1082e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                // Prefer a BSSID that doesn't have less number of Ip config failures
1083e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                logDbg("attemptRoam: "
1084dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                        + b.BSSID + " rssi=" + b.level + " ipfail=" + b.numIpConfigFailures
1085e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " freq=" + b.frequency
1086e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " > "
1087dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                        + a.BSSID + " rssi=" + a.level + " ipfail=" + a.numIpConfigFailures
1088e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " freq=" + a.frequency);
1089e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                a = b;
1090e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                continue;
1091e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            }
1092e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
10932451dbcc4f9641df188326215b204b798eb70c46vandwalle            // Apply hysteresis: we favor the currentBSSID by giving it a boost
10947806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            if (currentBSSID != null && currentBSSID.equals(b.BSSID)) {
1095931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // Reduce the benefit of hysteresis if RSSI <= -75
109677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                if (b.level <= mWifiConfigStore.bandPreferencePenaltyThreshold5.get()) {
10970eebae7334d6129f7ca1344e4b20199794994358vandwalle                    bRssiBoost = mWifiConfigStore.associatedHysteresisLow;
1098c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                } else {
10990eebae7334d6129f7ca1344e4b20199794994358vandwalle                    bRssiBoost = mWifiConfigStore.associatedHysteresisHigh;
1100c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                }
11014dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
11027806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            if (currentBSSID != null && currentBSSID.equals(a.BSSID)) {
110377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                if (a.level <= mWifiConfigStore.bandPreferencePenaltyThreshold5.get()) {
1104931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // Reduce the benefit of hysteresis if RSSI <= -75
11050eebae7334d6129f7ca1344e4b20199794994358vandwalle                    aRssiBoost = mWifiConfigStore.associatedHysteresisLow;
1106c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                } else {
11070eebae7334d6129f7ca1344e4b20199794994358vandwalle                    aRssiBoost = mWifiConfigStore.associatedHysteresisHigh;
1108c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                }
11094dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
11102451dbcc4f9641df188326215b204b798eb70c46vandwalle
111197b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            // Favor 5GHz: give a boost to 5GHz BSSIDs, with a slightly progressive curve
11122451dbcc4f9641df188326215b204b798eb70c46vandwalle            //   Boost the BSSID if it is on 5GHz, above a threshold
11132451dbcc4f9641df188326215b204b798eb70c46vandwalle            //   But penalize it if it is on 5GHz and below threshold
111497b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            //
111597b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            //   With he current threshold values, 5GHz network with RSSI above -55
111697b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            //   Are given a boost of 30DB which is enough to overcome the current BSSID
111797b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            //   hysteresis (+14) plus 2.4/5 GHz signal strength difference on most cases
11189f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            //
11199f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            // The "current BSSID" Boost must be added to the BSSID's level so as to introduce\
11209f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            // soem amount of hysteresis
1121e67ec726c07410073575473c0f50dc737629f5davandwalle            if (b.is5GHz()) {
11229f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                bRssiBoost5 = rssiBoostFrom5GHzRssi(b.level + bRssiBoost, b.BSSID);
11234dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
1124e67ec726c07410073575473c0f50dc737629f5davandwalle            if (a.is5GHz()) {
11259f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                aRssiBoost5 = rssiBoostFrom5GHzRssi(a.level + aRssiBoost, a.BSSID);
11264dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
11274dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1128dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (VDBG) {
1129c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                String comp = " < ";
1130dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                if (b.level + bRssiBoost + bRssiBoost5 > a.level + aRssiBoost + aRssiBoost5) {
1131c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                    comp = " > ";
1132c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                }
11334dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                logDbg("attemptRoam: "
1134c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                        + b.BSSID + " rssi=" + b.level + " boost=" + Integer.toString(bRssiBoost)
113597b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle                        + "/" + Integer.toString(bRssiBoost5) + " freq=" + b.frequency
113697b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle                        + comp
1137c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                        + a.BSSID + " rssi=" + a.level + " boost=" + Integer.toString(aRssiBoost)
1138c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                        + "/" + Integer.toString(aRssiBoost5) + " freq=" + a.frequency);
1139c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle            }
1140c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
11412451dbcc4f9641df188326215b204b798eb70c46vandwalle            // Compare the RSSIs after applying the hysteresis boost and the 5GHz
11422451dbcc4f9641df188326215b204b798eb70c46vandwalle            // boost if applicable
1143dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (b.level + bRssiBoost + bRssiBoost5 > a.level + aRssiBoost + aRssiBoost5) {
1144931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // b is the better BSSID
1145c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                a = b;
11464dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
11474dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
11483a2a3d226881cce8a4e511302231d843b0def303vandwalle        if (a != null) {
1149dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (VDBG) {
11507806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                StringBuilder sb = new StringBuilder();
1151e67ec726c07410073575473c0f50dc737629f5davandwalle                sb.append("attemptRoam: " + current.configKey() +
1152e67ec726c07410073575473c0f50dc737629f5davandwalle                        " Found " + a.BSSID + " rssi=" + a.level + " freq=" + a.frequency);
11537806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                if (currentBSSID != null) {
11547806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                    sb.append(" Current: " + currentBSSID);
11557806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                }
11567806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                sb.append("\n");
11577806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                logDbg(sb.toString());
11583a2a3d226881cce8a4e511302231d843b0def303vandwalle            }
11594dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
11603a2a3d226881cce8a4e511302231d843b0def303vandwalle        return a;
11614dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle    }
11624dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1163931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
11642451dbcc4f9641df188326215b204b798eb70c46vandwalle     * getNetworkScore()
1165dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * <p/>
11662451dbcc4f9641df188326215b204b798eb70c46vandwalle     * if scorer is present, get the network score of a WifiConfiguration
1167dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle     * <p/>
11682451dbcc4f9641df188326215b204b798eb70c46vandwalle     * Note: this should be merge with setVisibility
11692451dbcc4f9641df188326215b204b798eb70c46vandwalle     *
11702451dbcc4f9641df188326215b204b798eb70c46vandwalle     * @param config
11712451dbcc4f9641df188326215b204b798eb70c46vandwalle     * @return score
11722451dbcc4f9641df188326215b204b798eb70c46vandwalle     */
117381c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson    int getConfigNetworkScore(WifiConfiguration config, int age, boolean isActive) {
11742451dbcc4f9641df188326215b204b798eb70c46vandwalle
11752451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (mNetworkScoreCache == null) {
1176e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            if (VDBG) {
1177b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("       getConfigNetworkScore for " + config.configKey()
1178e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle                        + "  -> no scorer, hence no scores");
1179e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            }
11801db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle            return WifiNetworkScoreCache.INVALID_NETWORK_SCORE;
11811db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        }
118225ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande
118325ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande        if (mWifiConfigStore.getScanDetailCache(config) == null) {
1184e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            if (VDBG) {
1185b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("       getConfigNetworkScore for " + config.configKey()
1186e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle                        + " -> no scan cache");
1187e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            }
11881db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle            return WifiNetworkScoreCache.INVALID_NETWORK_SCORE;
11892451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
11902451dbcc4f9641df188326215b204b798eb70c46vandwalle
11912451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Get current date
11922451dbcc4f9641df188326215b204b798eb70c46vandwalle        long nowMs = System.currentTimeMillis();
11932451dbcc4f9641df188326215b204b798eb70c46vandwalle
11941db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        int startScore = -10000;
11951db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle
11962451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Run thru all cached scan results
119725ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande        for (ScanDetail sd : mWifiConfigStore.getScanDetailCache(config).values()) {
119825ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande            ScanResult result = sd.getScanResult();
119925ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande            if ((nowMs - sd.getSeen()) < age) {
120081c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                int sc = mNetworkScoreCache.getNetworkScore(result, isActive);
12011db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle                if (sc > startScore) {
12021db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle                    startScore = sc;
12032451dbcc4f9641df188326215b204b798eb70c46vandwalle                }
12042451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
12052451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
12061db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        if (startScore == -10000) {
12071db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle            startScore = WifiNetworkScoreCache.INVALID_NETWORK_SCORE;
12081db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        }
1209e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle        if (VDBG) {
1210e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            if (startScore == WifiNetworkScoreCache.INVALID_NETWORK_SCORE) {
1211b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    getConfigNetworkScore for " + config.configKey()
1212e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle                        + " -> no available score");
1213e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            } else {
1214b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    getConfigNetworkScore for " + config.configKey()
121581c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                        + " isActive=" + isActive
1216e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle                        + " score = " + Integer.toString(startScore));
1217e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            }
1218e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle        }
1219e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle
12201db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        return startScore;
12212451dbcc4f9641df188326215b204b798eb70c46vandwalle    }
12222451dbcc4f9641df188326215b204b798eb70c46vandwalle
12232451dbcc4f9641df188326215b204b798eb70c46vandwalle    /**
12248639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson     * Set whether connections to untrusted connections are allowed.
12258639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson     */
12268639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    void setAllowUntrustedConnections(boolean allow) {
12278639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson        boolean changed = mAllowUntrustedConnections != allow;
12288639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson        mAllowUntrustedConnections = allow;
12298639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson        if (changed) {
1230005c1ef113192f898499a407dd266393a8d6b076vandwalle            // Trigger a scan so as to reattempt autojoin
1231005c1ef113192f898499a407dd266393a8d6b076vandwalle            mWifiStateMachine.startScanForUntrustedSettingChange();
12328639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson        }
12338639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    }
12348639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson
12358639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    private boolean isOpenNetwork(ScanResult result) {
12368639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson        return !result.capabilities.contains("WEP") &&
12378639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                !result.capabilities.contains("PSK") &&
12388639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                !result.capabilities.contains("EAP");
12398639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    }
12408639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson
124116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson    private boolean haveRecentlySeenScoredBssid(WifiConfiguration config) {
124216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        long ephemeralOutOfRangeTimeoutMs = Settings.Global.getLong(
124316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                mContext.getContentResolver(),
124416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                Settings.Global.WIFI_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS,
124516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                DEFAULT_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS);
124616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
124716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        // Check whether the currently selected network has a score curve. If
124816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        // ephemeralOutOfRangeTimeoutMs is <= 0, then this is all we check, and we stop here.
124916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        // Otherwise, we stop here if the currently selected network has a score. If it doesn't, we
125016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        // keep going - it could be that another BSSID is in range (has been seen recently) which
125116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        // has a score, even if the one we're immediately connected to doesn't.
1252dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        ScanResult currentScanResult = mWifiStateMachine.getCurrentScanResult();
125316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        boolean currentNetworkHasScoreCurve = mNetworkScoreCache.hasScoreCurve(currentScanResult);
125416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        if (ephemeralOutOfRangeTimeoutMs <= 0 || currentNetworkHasScoreCurve) {
125516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            if (DBG) {
125616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                if (currentNetworkHasScoreCurve) {
125716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    logDbg("Current network has a score curve, keeping network: "
125816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                            + currentScanResult);
125916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                } else {
126016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    logDbg("Current network has no score curve, giving up: " + config.SSID);
126116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                }
126216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            }
126316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            return currentNetworkHasScoreCurve;
126416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        }
126516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
126625ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande        if (mWifiConfigStore.getScanDetailCache(config) == null
126725ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande                || mWifiConfigStore.getScanDetailCache(config).isEmpty()) {
126816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            return false;
126916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        }
127016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
127116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        long currentTimeMs = System.currentTimeMillis();
127225ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande        for (ScanDetail sd : mWifiConfigStore.getScanDetailCache(config).values()) {
127325ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande            ScanResult result = sd.getScanResult();
127425ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande            if (currentTimeMs > sd.getSeen()
127525ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande                    && currentTimeMs - sd.getSeen() < ephemeralOutOfRangeTimeoutMs
127616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    && mNetworkScoreCache.hasScoreCurve(result)) {
127716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                if (DBG) {
127816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    logDbg("Found scored BSSID, keeping network: " + result.BSSID);
127916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                }
128016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                return true;
128116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            }
128216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        }
128316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
128416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        if (DBG) {
128516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            logDbg("No recently scored BSSID found, giving up connection: " + config.SSID);
128616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        }
128716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        return false;
128816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson    }
128916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
129077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // After WifiStateMachine ask the supplicant to associate or reconnect
129177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // we might still obtain scan results from supplicant
129277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // however the supplicant state in the mWifiInfo and supplicant state tracker
129377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // are updated when we get the supplicant state change message which can be
129477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // processed after the SCAN_RESULT message, so at this point the framework doesn't
129577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // know that supplicant is ASSOCIATING.
129677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // A good fix for this race condition would be for the WifiStateMachine to add
129777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // a new transient state where it expects to get the supplicant message indicating
129877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // that it started the association process and within which critical operations
129977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // like autojoin should be deleted.
130077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
130177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // This transient state would remove the need for the roam Wathchdog which
130277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // basically does that.
130377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
130477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // At the moment, we just query the supplicant state synchronously with the
130577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // mWifiNative.status() command, which allow us to know that
130677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // supplicant has started association process, even though we didnt yet get the
130777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    // SUPPLICANT_STATE_CHANGE message.
130877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
130977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    private static final List<String> ASSOC_STATES = Arrays.asList(
131077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            "ASSOCIATING",
131177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            "ASSOCIATED",
131277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            "FOUR_WAY_HANDSHAKE",
131377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            "GROUP_KEY_HANDSHAKE");
131477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
131577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    private int getNetID(String wpaStatus) {
131677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        if (VDBG) {
131777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            logDbg("attemptAutoJoin() status=" + wpaStatus);
131877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        }
131977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
132077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        try {
132177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            int id = WifiConfiguration.INVALID_NETWORK_ID;
132277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            String state = null;
132377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            BufferedReader br = new BufferedReader(new StringReader(wpaStatus));
132477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            String line;
1325dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            while ((line = br.readLine()) != null) {
132677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                int split = line.indexOf('=');
132777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                if (split < 0) {
132877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    continue;
132977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                }
133077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
133177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                String name = line.substring(0, split);
133277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                if (name.equals("id")) {
133377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    try {
1334dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                        id = Integer.parseInt(line.substring(split + 1));
133577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                        if (state != null) {
133677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                            break;
133777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                        }
1338dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    } catch (NumberFormatException nfe) {
133977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                        return WifiConfiguration.INVALID_NETWORK_ID;
134077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    }
1341dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                } else if (name.equals("wpa_state")) {
1342dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    state = line.substring(split + 1);
134377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    if (ASSOC_STATES.contains(state)) {
134477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                        return WifiConfiguration.INVALID_NETWORK_ID;
1345dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    } else if (id >= 0) {
134677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                        break;
134777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    }
134877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                }
134977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            }
135077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            return id;
1351dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        } catch (IOException ioe) {
135277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            return WifiConfiguration.INVALID_NETWORK_ID;    // Won't happen
135377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        }
135477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
135577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
135677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    private boolean setCurrentConfigurationKey(WifiConfiguration currentConfig,
135777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                                               int supplicantNetId) {
135877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        if (currentConfig != null) {
135977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            if (supplicantNetId != currentConfig.networkId
136077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    // https://b.corp.google.com/issue?id=16484607
136177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    // mark this condition as an error only if the mismatched networkId are valid
136277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    && supplicantNetId != WifiConfiguration.INVALID_NETWORK_ID
136377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    && currentConfig.networkId != WifiConfiguration.INVALID_NETWORK_ID) {
136477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                logDbg("attemptAutoJoin() ERROR wpa_supplicant out of sync nid="
136577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                        + Integer.toString(supplicantNetId) + " WifiStateMachine="
136677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                        + Integer.toString(currentConfig.networkId));
136777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                mWifiStateMachine.disconnectCommand();
136877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                return false;
136977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            } else if (currentConfig.ephemeral && (!mAllowUntrustedConnections ||
137077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    !haveRecentlySeenScoredBssid(currentConfig))) {
137177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                // The current connection is untrusted (the framework added it), but we're either
137277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                // no longer allowed to connect to such networks, the score has been nullified
137377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                // since we connected, or the scored BSSID has gone out of range.
137477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                // Drop the current connection and perform the rest of autojoin.
137577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                logDbg("attemptAutoJoin() disconnecting from unwanted ephemeral network");
137677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                mWifiStateMachine.disconnectCommand(Process.WIFI_UID,
137777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                        mAllowUntrustedConnections ? 1 : 0);
137877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                return false;
137977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            } else {
138077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                mCurrentConfigurationKey = currentConfig.configKey();
138177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                return true;
138277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            }
138377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        } else {
138477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            // If not invalid, then maybe in the process of associating, skip this attempt
138577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            return supplicantNetId == WifiConfiguration.INVALID_NETWORK_ID;
138677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        }
138777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
138877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
138977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    private void updateBlackListStatus(WifiConfiguration config, long now) {
139077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        // Wait for 5 minutes before reenabling config that have known,
139177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        // repeated connection or DHCP failures
139277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        if (config.disableReason == WifiConfiguration.DISABLED_DHCP_FAILURE
139377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                || config.disableReason
139477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                == WifiConfiguration.DISABLED_ASSOCIATION_REJECT
139577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                || config.disableReason
139677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                == WifiConfiguration.DISABLED_AUTH_FAILURE) {
139777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            if (config.blackListTimestamp == 0
139877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    || (config.blackListTimestamp > now)) {
139977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                // Sanitize the timestamp
140077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                config.blackListTimestamp = now;
140177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            }
140277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            if ((now - config.blackListTimestamp) >
140377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    mWifiConfigStore.wifiConfigBlacklistMinTimeMilli) {
140477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                // Re-enable the WifiConfiguration
140577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                config.status = WifiConfiguration.Status.ENABLED;
140677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
140777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                // Reset the blacklist condition
140877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                config.numConnectionFailures = 0;
140977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                config.numIpConfigFailures = 0;
141077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                config.numAuthFailures = 0;
141177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
141277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
141377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                config.dirty = true;
141477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            } else {
141577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                if (VDBG) {
141677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    long delay = mWifiConfigStore.wifiConfigBlacklistMinTimeMilli
141777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                            - (now - config.blackListTimestamp);
141877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    logDbg("attemptautoJoin " + config.configKey()
141977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                            + " dont unblacklist yet, waiting for "
142077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                            + delay + " ms");
142177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                }
142277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            }
142377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        }
142477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        // Avoid networks disabled because of AUTH failure altogether
142577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        if (DBG) {
142677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            logDbg("attemptAutoJoin skip candidate due to auto join status "
142777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    + Integer.toString(config.autoJoinStatus) + " key "
142877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    + config.configKey(true)
142977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    + " reason " + config.disableReason);
143077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        }
143177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
1432dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
143377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    boolean underSoftThreshold(WifiConfiguration config) {
143477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        return config.visibility.rssi24 < mWifiConfigStore.thresholdUnblacklistThreshold24Soft.get()
1435dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                && config.visibility.rssi5 < mWifiConfigStore.thresholdUnblacklistThreshold5Soft.get();
143677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
143777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
143877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    boolean underHardThreshold(WifiConfiguration config) {
143977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        return config.visibility.rssi24 < mWifiConfigStore.thresholdUnblacklistThreshold24Hard.get()
1440dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                && config.visibility.rssi5 < mWifiConfigStore.thresholdUnblacklistThreshold5Hard.get();
144177f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
144277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
144377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    boolean underThreshold(WifiConfiguration config, int rssi24, int rssi5) {
144477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        return config.visibility.rssi24 < rssi24 && config.visibility.rssi5 < rssi5;
144577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
144677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
14478639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    /**
1448931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     * attemptAutoJoin() function implements the core of the a network switching algorithm
144968fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle     * Return false if no acceptable networks were found.
1450931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     */
145168fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle    boolean attemptAutoJoin() {
145268fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle        boolean found = false;
1453c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        didOverride = false;
14541ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle        didBailDueToWeakRssi = false;
1455b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        int networkSwitchType = AUTO_JOIN_IDLE;
1456e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle        int age = mScanResultAutoJoinAge;
14574dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
14588242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle        long now = System.currentTimeMillis();
14598242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle
14608c9088d11880553458f09377cc60d6eb7e66747bvandwalle        String lastSelectedConfiguration = mWifiConfigStore.getLastSelectedConfiguration();
1461e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle        if (lastSelectedConfiguration != null) {
1462e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle            age = 14000;
1463e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle        }
1464931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        // Reset the currentConfiguration Key, and set it only if WifiStateMachine and
1465453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        // supplicant agree
1466453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        mCurrentConfigurationKey = null;
1467453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        WifiConfiguration currentConfiguration = mWifiStateMachine.getCurrentWifiConfiguration();
1468453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle
1469f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration candidate = null;
1470931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        // Obtain the subset of recently seen networks
1471833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle        List<WifiConfiguration> list =
1472e3e4fad6e9b2022018a33507e93a39c6446f9916Pierre Vandwalle                mWifiConfigStore.getRecentConfiguredNetworks(age, false);
1473f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (list == null) {
1474dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            if (VDBG) logDbg("attemptAutoJoin nothing known=" +
147531891afce40b903ada9b24ec12e3648ae6aa27b2Jan Nordqvist                    mWifiConfigStore.getConfiguredNetworksSize());
147668fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle            return false;
1477f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1478f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1479931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        // Find the currently connected network: ask the supplicant directly
148077f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        int supplicantNetId = getNetID(mWifiNative.status(true));
148149bc8dd35845318cd9bc5ce9064fc1ad33bbece8xinhe
1482ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        if (DBG) {
14837806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            String conf = "";
1484b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            String last = "";
14857806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            if (currentConfiguration != null) {
1486e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                conf = " current=" + currentConfiguration.configKey();
1487b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            }
1488b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            if (lastSelectedConfiguration != null) {
14892f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                last = " last=" + lastSelectedConfiguration;
14907806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            }
1491ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            logDbg("attemptAutoJoin() num recent config " + Integer.toString(list.size())
1492b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + conf + last
1493b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + " ---> suppNetId=" + Integer.toString(supplicantNetId));
1494ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
1495f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
149677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        if (!setCurrentConfigurationKey(currentConfiguration, supplicantNetId)) {
149777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            return false;
1498453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        }
1499453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle
1500b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        int currentNetId = -1;
1501b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        if (currentConfiguration != null) {
1502931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // If we are associated to a configuration, it will
1503b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle            // be compared thru the compareNetwork function
1504b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle            currentNetId = currentConfiguration.networkId;
1505b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        }
1506b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle
1507931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        /**
1508931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         * Run thru all visible configurations without looking at the one we
1509c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle         * are currently associated to
15104dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle         * select Best Network candidate from known WifiConfigurations
1511931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         */
1512f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        for (WifiConfiguration config : list) {
1513e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle            if (config.SSID == null) {
1514b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                continue;
1515e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle            }
1516e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle
151777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            if (config.autoJoinStatus >= WifiConfiguration.AUTO_JOIN_DISABLED_ON_AUTH_FAILURE) {
151877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                updateBlackListStatus(config, now);
1519f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                continue;
1520f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1521f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1522e487a4648dd41881e754f1224aaedead78a0777dSky Faber            if (config.userApproved == WifiConfiguration.USER_PENDING ||
1523e487a4648dd41881e754f1224aaedead78a0777dSky Faber                    config.userApproved == WifiConfiguration.USER_BANNED) {
1524e487a4648dd41881e754f1224aaedead78a0777dSky Faber                if (DBG) {
1525e487a4648dd41881e754f1224aaedead78a0777dSky Faber                    logDbg("attemptAutoJoin skip candidate due to user approval status "
1526e487a4648dd41881e754f1224aaedead78a0777dSky Faber                            + WifiConfiguration.userApprovedAsString(config.userApproved) + " key "
1527e487a4648dd41881e754f1224aaedead78a0777dSky Faber                            + config.configKey(true));
1528e487a4648dd41881e754f1224aaedead78a0777dSky Faber                }
1529e487a4648dd41881e754f1224aaedead78a0777dSky Faber                continue;
1530e487a4648dd41881e754f1224aaedead78a0777dSky Faber            }
1531e487a4648dd41881e754f1224aaedead78a0777dSky Faber
1532931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Try to un-blacklist based on elapsed time
153327355a942653264388e909a4276196ee63e57811vandwalle            if (config.blackListTimestamp > 0) {
153427355a942653264388e909a4276196ee63e57811vandwalle                if (now < config.blackListTimestamp) {
1535931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    /**
1536931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                     * looks like there was a change in the system clock since we black listed, and
1537931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                     * timestamp is not meaningful anymore, hence lose it.
1538931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                     * this event should be rare enough so that we still want to lose the black list
15392451dbcc4f9641df188326215b204b798eb70c46vandwalle                     */
154027355a942653264388e909a4276196ee63e57811vandwalle                    config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
154127355a942653264388e909a4276196ee63e57811vandwalle                } else {
15424dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                    if ((now - config.blackListTimestamp) > loseBlackListHardMilli) {
1543931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                        // Reenable it after 18 hours, i.e. next day
154427355a942653264388e909a4276196ee63e57811vandwalle                        config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
15454dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                    } else if ((now - config.blackListTimestamp) > loseBlackListSoftMilli) {
1546931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                        // Lose blacklisting due to bad link
154727355a942653264388e909a4276196ee63e57811vandwalle                        config.setAutoJoinStatus(config.autoJoinStatus - 8);
154827355a942653264388e909a4276196ee63e57811vandwalle                    }
154927355a942653264388e909a4276196ee63e57811vandwalle                }
155027355a942653264388e909a4276196ee63e57811vandwalle            }
155127355a942653264388e909a4276196ee63e57811vandwalle
155277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            if (config.visibility == null) {
155377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                continue;
155477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            }
155577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
1556931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Try to unblacklist based on good visibility
155777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            if (underSoftThreshold(config)) {
155821d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                if (DBG) {
155921d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                    logDbg("attemptAutoJoin do not unblacklist due to low visibility " +
156021d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                            config.configKey() + " status=" + config.autoJoinStatus);
156121d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                }
156277f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            } else if (underHardThreshold(config)) {
1563931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // If the network is simply temporary disabled, don't allow reconnect until
1564931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // RSSI becomes good enough
156527355a942653264388e909a4276196ee63e57811vandwalle                config.setAutoJoinStatus(config.autoJoinStatus - 1);
156621d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                if (DBG) {
156721d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                    logDbg("attemptAutoJoin good candidate seen, bumped soft -> status=" +
156821d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                            config.configKey() + " status=" + config.autoJoinStatus);
156921d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                }
157027355a942653264388e909a4276196ee63e57811vandwalle            } else {
1571c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                config.setAutoJoinStatus(config.autoJoinStatus - 3);
157221d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                if (DBG) {
157321d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                    logDbg("attemptAutoJoin good candidate seen, bumped hard -> status=" +
157421d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                            config.configKey() + " status=" + config.autoJoinStatus);
157521d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                }
157627355a942653264388e909a4276196ee63e57811vandwalle            }
157727355a942653264388e909a4276196ee63e57811vandwalle
157827355a942653264388e909a4276196ee63e57811vandwalle            if (config.autoJoinStatus >=
157927355a942653264388e909a4276196ee63e57811vandwalle                    WifiConfiguration.AUTO_JOIN_TEMPORARY_DISABLED) {
1580931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // Network is blacklisted, skip
158121d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                if (DBG) {
158221d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                    logDbg("attemptAutoJoin skip blacklisted -> status=" +
158321d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                            config.configKey() + " status=" + config.autoJoinStatus);
158421d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                }
158527355a942653264388e909a4276196ee63e57811vandwalle                continue;
158627355a942653264388e909a4276196ee63e57811vandwalle            }
1587f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (config.networkId == currentNetId) {
1588ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                if (DBG) {
158921bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle                    logDbg("attemptAutoJoin skip current candidate  "
159021bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle                            + Integer.toString(currentNetId)
1591ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                            + " key " + config.configKey(true));
1592ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                }
1593f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                continue;
1594f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1595f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
15960eebae7334d6129f7ca1344e4b20199794994358vandwalle            boolean isLastSelected = false;
15970eebae7334d6129f7ca1344e4b20199794994358vandwalle            if (lastSelectedConfiguration != null &&
15980eebae7334d6129f7ca1344e4b20199794994358vandwalle                    config.configKey().equals(lastSelectedConfiguration)) {
15990eebae7334d6129f7ca1344e4b20199794994358vandwalle                isLastSelected = true;
16000eebae7334d6129f7ca1344e4b20199794994358vandwalle            }
16010eebae7334d6129f7ca1344e4b20199794994358vandwalle
16024fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle            if (config.lastRoamingFailure != 0
16034fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                    && currentConfiguration != null
16044fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                    && (lastSelectedConfiguration == null
16054fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                    || !config.configKey().equals(lastSelectedConfiguration))) {
16064fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                // Apply blacklisting for roaming to this config if:
16074fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                //   - the target config had a recent roaming failure
16084fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                //   - we are currently associated
16094fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                //   - the target config is not the last selected
16104fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                if (now > config.lastRoamingFailure
16114fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                        && (now - config.lastRoamingFailure)
16124fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                        < config.roamingFailureBlackListTimeMilli) {
16134fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                    if (DBG) {
16144fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                        logDbg("compareNetwork not switching to " + config.configKey()
16154fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                                + " from current " + currentConfiguration.configKey()
16164fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                                + " because it is blacklisted due to roam failure, "
16174fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                                + " blacklist remain time = "
16184fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                                + (now - config.lastRoamingFailure) + " ms");
16194fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                    }
16204fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                    continue;
16214fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle                }
16224fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle            }
16234fa99f57077ab287d6ed1b51cf308c44ce8bbe0bvandwalle
16241ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            int boost = config.autoJoinUseAggressiveJoinAttemptThreshold + weakRssiBailCount;
162577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            if (underThreshold(config,
162677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    mWifiConfigStore.thresholdInitialAutoJoinAttemptMin24RSSI.get() - boost,
162777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist                    mWifiConfigStore.thresholdInitialAutoJoinAttemptMin5RSSI.get() - boost)) {
162877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
162921d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                if (DBG) {
163021d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                    logDbg("attemptAutoJoin skip due to low visibility " + config.configKey());
163121d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                }
16320eebae7334d6129f7ca1344e4b20199794994358vandwalle
1633c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                // Don't try to autojoin a network that is too far but
1634c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                // If that configuration is a user's choice however, try anyway
16350eebae7334d6129f7ca1344e4b20199794994358vandwalle                if (!isLastSelected) {
16360eebae7334d6129f7ca1344e4b20199794994358vandwalle                    config.autoJoinBailedDueToLowRssi = true;
16371ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                    didBailDueToWeakRssi = true;
16388c9088d11880553458f09377cc60d6eb7e66747bvandwalle                    continue;
16390eebae7334d6129f7ca1344e4b20199794994358vandwalle                } else {
16400eebae7334d6129f7ca1344e4b20199794994358vandwalle                    // Next time, try to be a bit more aggressive in auto-joining
16410eebae7334d6129f7ca1344e4b20199794994358vandwalle                    if (config.autoJoinUseAggressiveJoinAttemptThreshold
1642dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                            < WifiConfiguration.MAX_INITIAL_AUTO_JOIN_RSSI_BOOST
1643dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                            && config.autoJoinBailedDueToLowRssi) {
1644dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                        config.autoJoinUseAggressiveJoinAttemptThreshold += 4;
16454dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                    }
16468c9088d11880553458f09377cc60d6eb7e66747bvandwalle                }
16470eebae7334d6129f7ca1344e4b20199794994358vandwalle            }
1648d30127db46224e45554f8964209221bba8ad41d9vandwalle            if (config.numNoInternetAccessReports > 0
1649d30127db46224e45554f8964209221bba8ad41d9vandwalle                    && !isLastSelected
1650d30127db46224e45554f8964209221bba8ad41d9vandwalle                    && !config.validatedInternetAccess) {
1651d30127db46224e45554f8964209221bba8ad41d9vandwalle                // Avoid autoJoining this network because last time we used it, it didn't
1652d30127db46224e45554f8964209221bba8ad41d9vandwalle                // have internet access, and we never manage to validate internet access on this
1653d30127db46224e45554f8964209221bba8ad41d9vandwalle                // network configuration
16540eebae7334d6129f7ca1344e4b20199794994358vandwalle                if (DBG) {
1655d30127db46224e45554f8964209221bba8ad41d9vandwalle                    logDbg("attemptAutoJoin skip candidate due to no InternetAccess  "
1656d30127db46224e45554f8964209221bba8ad41d9vandwalle                            + config.configKey(true)
1657d30127db46224e45554f8964209221bba8ad41d9vandwalle                            + " num reports " + config.numNoInternetAccessReports);
16589f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                }
16590eebae7334d6129f7ca1344e4b20199794994358vandwalle                continue;
16608c9088d11880553458f09377cc60d6eb7e66747bvandwalle            }
16618c9088d11880553458f09377cc60d6eb7e66747bvandwalle
1662ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            if (DBG) {
1663e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                String cur = "";
1664e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                if (candidate != null) {
1665e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    cur = " current candidate " + candidate.configKey();
1666e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                }
1667e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                logDbg("attemptAutoJoin trying id="
1668c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                        + Integer.toString(config.networkId) + " "
1669b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                        + config.configKey(true)
1670e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " status=" + config.autoJoinStatus
1671e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + cur);
1672ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            }
1673f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1674f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (candidate == null) {
1675f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                candidate = config;
1676f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else {
1677dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                if (VDBG) {
1678453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    logDbg("attemptAutoJoin will compare candidate  " + candidate.configKey()
16794dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + " with " + config.configKey());
1680f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
16814d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande
1682833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                int order = compareWifiConfigurations(candidate, config);
16834d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                if (VDBG) {
16844d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                    logDbg("attemptAutoJoin compareWifiConfigurations returned " + order);
16854d381bc39f5263effdae73ec99065eb299b806caVinit Deshpande                }
16862451dbcc4f9641df188326215b204b798eb70c46vandwalle
16872451dbcc4f9641df188326215b204b798eb70c46vandwalle                // The lastSelectedConfiguration is the configuration the user has manually selected
1688c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                // thru WifiPicker, or that a 3rd party app asked us to connect to via the
16892451dbcc4f9641df188326215b204b798eb70c46vandwalle                // enableNetwork with disableOthers=true WifiManager API
16902451dbcc4f9641df188326215b204b798eb70c46vandwalle                // As this is a direct user choice, we strongly prefer this configuration,
16912451dbcc4f9641df188326215b204b798eb70c46vandwalle                // hence give +/-100
16922451dbcc4f9641df188326215b204b798eb70c46vandwalle                if ((lastSelectedConfiguration != null)
16932451dbcc4f9641df188326215b204b798eb70c46vandwalle                        && candidate.configKey().equals(lastSelectedConfiguration)) {
16942451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // candidate is the last selected configuration,
16952451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // so keep it above connect choices (+/-60) and
16962451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // above RSSI/scorer based selection of linked configuration (+/- 50)
16972451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // by reducing order by -100
16982451dbcc4f9641df188326215b204b798eb70c46vandwalle                    order = order - 100;
1699dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    if (VDBG) {
17002f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        logDbg("     ...and prefers -100 " + candidate.configKey()
17012451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + " over " + config.configKey()
17022451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + " because it is the last selected -> "
17032451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + Integer.toString(order));
17042451dbcc4f9641df188326215b204b798eb70c46vandwalle                    }
17052451dbcc4f9641df188326215b204b798eb70c46vandwalle                } else if ((lastSelectedConfiguration != null)
17062451dbcc4f9641df188326215b204b798eb70c46vandwalle                        && config.configKey().equals(lastSelectedConfiguration)) {
17072451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // config is the last selected configuration,
17082451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // so keep it above connect choices (+/-60) and
17092451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // above RSSI/scorer based selection of linked configuration (+/- 50)
17102451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // by increasing order by +100
17112451dbcc4f9641df188326215b204b798eb70c46vandwalle                    order = order + 100;
1712dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    if (VDBG) {
17132f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        logDbg("     ...and prefers +100 " + config.configKey()
17142451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + " over " + candidate.configKey()
17152451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + " because it is the last selected -> "
17162451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + Integer.toString(order));
17172451dbcc4f9641df188326215b204b798eb70c46vandwalle                    }
17182451dbcc4f9641df188326215b204b798eb70c46vandwalle                }
17192451dbcc4f9641df188326215b204b798eb70c46vandwalle
1720f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (order > 0) {
1721931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // Ascending : candidate < config
1722f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    candidate = config;
1723f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
1724f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1725f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1726f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
17272451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Now, go thru scan result to try finding a better untrusted network
1728833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle        if (mNetworkScoreCache != null && mAllowUntrustedConnections) {
1729f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            int rssi5 = WifiConfiguration.INVALID_RSSI;
1730f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            int rssi24 = WifiConfiguration.INVALID_RSSI;
1731f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (candidate != null) {
1732f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                rssi5 = candidate.visibility.rssi5;
1733f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                rssi24 = candidate.visibility.rssi24;
1734f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1735f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1736931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Get current date
17372451dbcc4f9641df188326215b204b798eb70c46vandwalle            long nowMs = System.currentTimeMillis();
1738c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            int currentScore = -10000;
1739c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            // The untrusted network with highest score
174025ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande            ScanDetail untrustedCandidate = null;
17412451dbcc4f9641df188326215b204b798eb70c46vandwalle            // Look for untrusted scored network only if the current candidate is bad
1742c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            if (isBadCandidate(rssi24, rssi5)) {
174325ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande                for (ScanDetail scanDetail : scanResultCache.values()) {
174425ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande                    ScanResult result = scanDetail.getScanResult();
1745c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    // We look only at untrusted networks with a valid SSID
1746c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    // A trusted result would have been looked at thru it's Wificonfiguration
17478639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                    if (TextUtils.isEmpty(result.SSID) || !result.untrusted ||
17488639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                            !isOpenNetwork(result)) {
1749c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        continue;
1750c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    }
17512c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson                    String quotedSSID = "\"" + result.SSID + "\"";
17522c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson                    if (mWifiConfigStore.mDeletedEphemeralSSIDs.contains(quotedSSID)) {
17532ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle                        // SSID had been Forgotten by user, then don't score it
17542ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle                        continue;
17552ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle                    }
1756833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                    if ((nowMs - result.seen) < mScanResultAutoJoinAge) {
1757c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        // Increment usage count for the network
17582c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson                        mWifiConnectionStatistics.incrementOrAddUntrusted(quotedSSID, 0, 1);
1759c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
17602c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson                        boolean isActiveNetwork = currentConfiguration != null
17612c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson                                && currentConfiguration.SSID.equals(quotedSSID);
176281c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                        int score = mNetworkScoreCache.getNetworkScore(result, isActiveNetwork);
1763c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        if (score != WifiNetworkScoreCache.INVALID_NETWORK_SCORE
1764c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                                && score > currentScore) {
1765c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            // Highest score: Select this candidate
1766c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            currentScore = score;
176725ee2d5d30434712b28aef6eec9460035101e493Vinit Deshpande                            untrustedCandidate = scanDetail;
1768c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            if (VDBG) {
1769c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                                logDbg("AutoJoinController: found untrusted candidate "
1770c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                                        + result.SSID
1771dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                                        + " RSSI=" + result.level
1772dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                                        + " freq=" + result.frequency
1773dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                                        + " score=" + score);
1774c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            }
1775f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
1776f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
1777f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
1778f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1779c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            if (untrustedCandidate != null) {
17808639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                // At this point, we have an untrusted network candidate.
17818639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                // Create the new ephemeral configuration and see if we should switch over
1782833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                candidate =
1783833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                        mWifiConfigStore.wifiConfigurationFromScanResult(untrustedCandidate);
1784833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                candidate.allowedKeyManagement.set(KeyMgmt.NONE);
1785833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                candidate.ephemeral = true;
1786fb650bfb264b7ff31c927498faf8343b5895c6abPierre Vandwalle                candidate.dirty = true;
1787c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            }
1788c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        }
1789b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle
17901ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle        long lastUnwanted =
17911ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                System.currentTimeMillis()
17921ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                        - mWifiConfigStore.lastUnwantedNetworkDisconnectTimestamp;
17931ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle        if (candidate == null
17941ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                && lastSelectedConfiguration == null
17951ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                && currentConfiguration == null
17961ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                && didBailDueToWeakRssi
17971ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                && (mWifiConfigStore.lastUnwantedNetworkDisconnectTimestamp == 0
1798dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                || lastUnwanted > (1000 * 60 * 60 * 24 * 7))
17991ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                ) {
18001ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // We are bailing out of autojoin although we are seeing a weak configuration, and
18011ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // - we didn't find another valid candidate
18021ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // - we are not connected
18031ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // - without a user network selection choice
18041ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // - ConnectivityService has not triggered an unwanted network disconnect
18051ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            //       on this device for a week (hence most likely there is no SIM card or cellular)
18061ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // If all those conditions are met, then boost the RSSI of the weak networks
18071ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // that we are seeing so as we will eventually pick one
18081ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            if (weakRssiBailCount < 10)
18091ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                weakRssiBailCount += 1;
18101ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle        } else {
18111ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            if (weakRssiBailCount > 0)
18121ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                weakRssiBailCount -= 1;
18131ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle        }
18141ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle
1815931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        /**
1816931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         *  If candidate is found, check the state of the connection so as
1817931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         *  to decide if we should be acting on this candidate and switching over
1818931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         */
1819e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        int networkDelta = compareNetwork(candidate, lastSelectedConfiguration);
1820b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        if (DBG && candidate != null) {
1821b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            String doSwitch = "";
1822b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            String current = "";
1823b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            if (networkDelta < 0) {
1824b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                doSwitch = " -> not switching";
1825b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            }
1826b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            if (currentConfiguration != null) {
18272f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                current = " with current " + currentConfiguration.configKey();
1828b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            }
1829b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("attemptAutoJoin networkSwitching candidate "
1830b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle                    + candidate.configKey()
1831b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + current
1832c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                    + " linked=" + (currentConfiguration != null
1833dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    && currentConfiguration.isLinked(candidate))
1834b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + " : delta="
1835b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + Integer.toString(networkDelta) + " "
1836b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + doSwitch);
1837b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        }
18384dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1839931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        /**
1840931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         * Ask WifiStateMachine permission to switch :
1841931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         * if user is currently streaming voice traffic,
1842931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         * then we should not be allowed to switch regardless of the delta
1843931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         */
184477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        if (mWifiStateMachine.shouldSwitchNetwork(networkDelta)) {      // !!! JNo: Here!
1845b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            if (mStaStaSupported) {
1846b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                logDbg("mStaStaSupported --> error do nothing now ");
1847b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            } else {
1848b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                if (currentConfiguration != null && currentConfiguration.isLinked(candidate)) {
1849b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    networkSwitchType = AUTO_JOIN_EXTENDED_ROAMING;
1850b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                } else {
1851b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    networkSwitchType = AUTO_JOIN_OUT_OF_NETWORK_ROAMING;
1852b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                }
1853b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                if (DBG) {
1854b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    logDbg("AutoJoin auto connect with netId "
1855b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + Integer.toString(candidate.networkId)
1856b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + " to " + candidate.configKey());
1857b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                }
18582451dbcc4f9641df188326215b204b798eb70c46vandwalle                if (didOverride) {
18592451dbcc4f9641df188326215b204b798eb70c46vandwalle                    candidate.numScorerOverrideAndSwitchedNetwork++;
18602451dbcc4f9641df188326215b204b798eb70c46vandwalle                }
1861c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                candidate.numAssociation++;
1862e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                mWifiConnectionStatistics.numAutoJoinAttempt++;
18639f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
18642c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson                if (candidate.ephemeral) {
18652c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson                    // We found a new candidate that we are going to connect to, then
18662c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson                    // increase its connection count
18672c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson                    mWifiConnectionStatistics.
18682c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson                            incrementOrAddUntrusted(candidate.SSID, 1, 0);
18692c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson                }
18702c9b6297f3cd74780a084634320d03a413a3b779Jeff Davidson
18712f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                if (candidate.BSSID == null || candidate.BSSID.equals("any")) {
18722f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    // First step we selected the configuration we want to connect to
18732f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    // Second step: Look for the best Scan result for this configuration
18742f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    // TODO this algorithm should really be done in one step
18752f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    String currentBSSID = mWifiStateMachine.getCurrentBSSID();
1876833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                    ScanResult roamCandidate =
1877833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                            attemptRoam(null, candidate, mScanResultAutoJoinAge, null);
18782f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    if (roamCandidate != null && currentBSSID != null
18792f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                            && currentBSSID.equals(roamCandidate.BSSID)) {
18802f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        // Sanity, we were already asociated to that candidate
18812f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        roamCandidate = null;
18822f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    }
18832f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    if (roamCandidate != null && roamCandidate.is5GHz()) {
18842f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        // If the configuration hasn't a default BSSID selected, and the best
18852f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        // candidate is 5GHZ, then select this candidate so as WifiStateMachine and
18862f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        // supplicant will pick it first
18872f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        candidate.autoJoinBSSID = roamCandidate.BSSID;
18882f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        if (VDBG) {
18892f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                            logDbg("AutoJoinController: lock to 5GHz "
18902f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                                    + candidate.autoJoinBSSID
18912f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                                    + " RSSI=" + roamCandidate.level
18922f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                                    + " freq=" + roamCandidate.frequency);
18932f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        }
1894448c9536a302c58a79e271b1721c08b8882f800evandwalle                    } else {
1895448c9536a302c58a79e271b1721c08b8882f800evandwalle                        // We couldnt find a roam candidate
1896448c9536a302c58a79e271b1721c08b8882f800evandwalle                        candidate.autoJoinBSSID = "any";
18979f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    }
18989f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                }
1899448c9536a302c58a79e271b1721c08b8882f800evandwalle                mWifiStateMachine.sendMessage(WifiStateMachine.CMD_AUTO_CONNECT,
1900dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                        candidate.networkId, networkSwitchType, candidate);
190168fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle                found = true;
19024dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
1903b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        }
19044dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1905d4c25fd76644d5490700ae69fada4669de5193a1Pierre Vandwalle        if (networkSwitchType == AUTO_JOIN_IDLE && !mWifiConfigStore.enableHalBasedPno.get()) {
1906e67ec726c07410073575473c0f50dc737629f5davandwalle            String currentBSSID = mWifiStateMachine.getCurrentBSSID();
1907931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Attempt same WifiConfiguration roaming
1908833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle            ScanResult roamCandidate =
1909833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                    attemptRoam(null, currentConfiguration, mScanResultAutoJoinAge, currentBSSID);
1910e67ec726c07410073575473c0f50dc737629f5davandwalle            if (roamCandidate != null && currentBSSID != null
1911e67ec726c07410073575473c0f50dc737629f5davandwalle                    && currentBSSID.equals(roamCandidate.BSSID)) {
1912e67ec726c07410073575473c0f50dc737629f5davandwalle                roamCandidate = null;
1913e67ec726c07410073575473c0f50dc737629f5davandwalle            }
19142f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle            if (roamCandidate != null && mWifiStateMachine.shouldSwitchNetwork(999)) {
1915b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                if (DBG) {
1916b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    logDbg("AutoJoin auto roam with netId "
1917b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + Integer.toString(currentConfiguration.networkId)
1918b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + " " + currentConfiguration.configKey() + " to BSSID="
1919b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + roamCandidate.BSSID + " freq=" + roamCandidate.frequency
19201ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                            + " RSSI=" + roamCandidate.level);
1921b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                }
1922b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                networkSwitchType = AUTO_JOIN_ROAMING;
1923e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                mWifiConnectionStatistics.numAutoRoamAttempt++;
1924e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle
1925b07da189850a4bfa268f8ab9be7867935eb2ecb5vandwalle                mWifiStateMachine.sendMessage(WifiStateMachine.CMD_AUTO_ROAM,
1926dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                        currentConfiguration.networkId, 1, roamCandidate);
192768fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle                found = true;
1928f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1929f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1930b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        if (VDBG) logDbg("Done attemptAutoJoin status=" + Integer.toString(networkSwitchType));
193168fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle        return found;
1932f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
193377f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist
193477f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    private void logDenial(String reason, WifiConfiguration config) {
193577f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        if (!DBG) {
193677f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist            return;
193777f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        }
193877f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist        logDbg(reason + config.toString());
193977f2b82a2e80af8da52c22d69a76def6d4209757Jan Nordqvist    }
1940dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
1941dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    WifiConfiguration getWifiConfiguration(WifiNative.WifiPnoNetwork network) {
1942dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (network.configKey != null) {
1943dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            return mWifiConfigStore.getWifiConfiguration(network.configKey);
1944dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
1945dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        return null;
1946dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
1947dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
1948dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    ArrayList<WifiNative.WifiPnoNetwork> getPnoList(WifiConfiguration current) {
1949dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        int size = -1;
19509ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle        ArrayList<WifiNative.WifiPnoNetwork> list = new ArrayList<WifiNative.WifiPnoNetwork>();
1951dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
1952b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle        if (mWifiConfigStore.mCachedPnoList != null) {
1953b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle            size = mWifiConfigStore.mCachedPnoList.size();
1954dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
1955dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle
195621d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle        if (DBG) {
195721d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle            String s = "";
195821d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle            if (current != null) {
195921d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                s = " for: " + current.configKey();
196021d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle            }
196121d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle            Log.e(TAG, " get Pno List total size:" + size + s);
196221d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle        }
1963dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        if (current != null) {
1964dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            String configKey = current.configKey();
1965dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            /**
1966dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle             * If we are currently associated to a WifiConfiguration then include
1967dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle             * only those networks that have a higher priority
1968dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle             */
1969b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle            for (WifiNative.WifiPnoNetwork network : mWifiConfigStore.mCachedPnoList) {
1970dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                WifiConfiguration config = getWifiConfiguration(network);
19719ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                if (config == null) {
19729ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                    continue;
19739ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                }
19749ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                if (config.autoJoinStatus
19759ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                        >= WifiConfiguration.AUTO_JOIN_DISABLED_NO_CREDENTIALS) {
19769ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                     continue;
19779ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                }
19789ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle
19799ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                if (!configKey.equals(network.configKey)) {
198021d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                    int choice = getConnectChoice(config, current, true);
1981b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle                    if (choice > 0) {
1982b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle                        // config is of higher priority
198321d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                        if (DBG) {
198421d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                            Log.e(TAG, " Pno List adding:" + network.configKey
198521d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                                    + " choice " + choice);
198621d43c487ca1f719cd2c170e1fac88c7b62a836aPierre Vandwalle                        }
1987dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                        list.add(network);
1988b0b0cc202b7d7aaad7b3f69d73e9b58ea2968b05Pierre Vandwalle                        network.rssi_threshold = mWifiConfigStore.thresholdGoodRssi24.get();
1989dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                    }
1990dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle                }
1991dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle            }
1992dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        } else {
19939ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            for (WifiNative.WifiPnoNetwork network : mWifiConfigStore.mCachedPnoList) {
19949ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                WifiConfiguration config = getWifiConfiguration(network);
19959ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                if (config == null) {
19969ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                    continue;
19979ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                }
19989ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                if (config.autoJoinStatus
19999ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                        >= WifiConfiguration.AUTO_JOIN_DISABLED_NO_CREDENTIALS) {
20009ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                    continue;
20019ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                }
20029ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                list.add(network);
20039ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle                network.rssi_threshold = mWifiConfigStore.thresholdGoodRssi24.get();
20049ccffbb6c3fe8eaa7d70cc3076d0dc3c6a9073e7Pierre Vandwalle            }
2005dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        }
2006dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle        return list;
2007dd490cf1df37b70a8625dc4ec8e712f740dd1e4aPierre Vandwalle    }
2008f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle}
2009f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
2010