WifiAutoJoinController.java revision f57f8918b8c5872ff4bb141fa9e407bec8442e8d
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;
23c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalleimport android.net.wifi.*;
248639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidsonimport android.net.wifi.WifiConfiguration.KeyMgmt;
2516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidsonimport android.os.SystemClock;
2616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidsonimport android.provider.Settings;
27f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalleimport android.os.Process;
28c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalleimport android.text.TextUtils;
29f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.util.Log;
30f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
310c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalleimport java.util.ArrayList;
32f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.Iterator;
33f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.HashMap;
34f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.List;
35f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
36f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle/**
37f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * AutoJoin controller is responsible for WiFi Connect decision
38f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle *
39f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * It runs in the thread context of WifiStateMachine
40f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle *
41f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle */
42f22d23092ab37286a5ef9d257d5bb32c421d2669vandwallepublic class WifiAutoJoinController {
43f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
44f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private Context mContext;
45f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private WifiStateMachine mWifiStateMachine;
46f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private WifiConfigStore mWifiConfigStore;
47f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private WifiNative mWifiNative;
48f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
49f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private NetworkScoreManager scoreManager;
50f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private WifiNetworkScoreCache mNetworkScoreCache;
51f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
52f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String TAG = "WifiAutoJoinController ";
53ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    private static boolean DBG = false;
54ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    private static boolean VDBG = false;
55f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final boolean mStaStaSupported = false;
56f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
57c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    public static int mScanResultMaximumAge = 40000; /* milliseconds unit */
58833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle    public static int mScanResultAutoJoinAge = 5000; /* milliseconds unit */
59c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
60453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle    private String mCurrentConfigurationKey = null; //used by autojoin
61f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
62f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private HashMap<String, ScanResult> scanResultCache =
63f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            new HashMap<String, ScanResult>();
64f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
65c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    private WifiConnectionStatistics mWifiConnectionStatistics;
66c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
678639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    /** Whether to allow connections to untrusted networks. */
688639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    private boolean mAllowUntrustedConnections = false;
698639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson
70c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    /* for debug purpose only : the untrusted SSID we would be connected to if we had VPN */
71c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    String lastUntrustedBSSID = null;
72c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
73c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    /* For debug purpose only: if the scored override a score */
74c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    boolean didOverride = false;
75c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
76931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    // Lose the non-auth failure blacklisting after 8 hours
774dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle    private final static long loseBlackListHardMilli = 1000 * 60 * 60 * 8;
78931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    // Lose some temporary blacklisting after 30 minutes
794dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle    private final static long loseBlackListSoftMilli = 1000 * 60 * 30;
8027355a942653264388e909a4276196ee63e57811vandwalle
8116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson    /** @see android.provider.Settings.Global#WIFI_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS */
8216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson    private static final long DEFAULT_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS = 1000 * 60; // 1 minute
8316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
84b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle    public static final int AUTO_JOIN_IDLE = 0;
85b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle    public static final int AUTO_JOIN_ROAMING = 1;
86b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle    public static final int AUTO_JOIN_EXTENDED_ROAMING = 2;
87b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle    public static final int AUTO_JOIN_OUT_OF_NETWORK_ROAMING = 3;
88b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle
8997b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle    public static final int HIGH_THRESHOLD_MODIFIER = 5;
9097b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle
911ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    // Below are AutoJoin wide parameters indicating if we should be aggressive before joining
921ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    // weak network. Note that we cannot join weak network that are going to be marked as unanted by
931ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    // ConnectivityService because this will trigger link flapping.
941ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    /**
951ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle     * There was a non-blacklisted configuration that we bailed from because of a weak signal
961ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle     */
971ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    boolean didBailDueToWeakRssi = false;
981ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    /**
991ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle     * number of time we consecutively bailed out of an eligible network because its signal
1001ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle     * was too weak
1011ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle     */
1021ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    int weakRssiBailCount = 0;
1031ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle
104f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    WifiAutoJoinController(Context c, WifiStateMachine w, WifiConfigStore s,
105c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                           WifiConnectionStatistics st, WifiNative n) {
106f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mContext = c;
107f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mWifiStateMachine = w;
108f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mWifiConfigStore = s;
109f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mWifiNative = n;
110f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mNetworkScoreCache = null;
111c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        mWifiConnectionStatistics = st;
11221bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle        scoreManager =
11321bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle                (NetworkScoreManager) mContext.getSystemService(Context.NETWORK_SCORE_SERVICE);
114f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (scoreManager == null)
115f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            logDbg("Registered scoreManager NULL " + " service " + Context.NETWORK_SCORE_SERVICE);
116f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
117f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (scoreManager != null) {
118f13817203179f41620514718c8668ae7e418f8afJeff Davidson            mNetworkScoreCache = new WifiNetworkScoreCache(mContext);
119f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            scoreManager.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, mNetworkScoreCache);
120f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } else {
121f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            logDbg("No network score service: Couldnt register as a WiFi score Manager, type="
122f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    + Integer.toString(NetworkKey.TYPE_WIFI)
123f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    + " service " + Context.NETWORK_SCORE_SERVICE);
124f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            mNetworkScoreCache = null;
125f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
126f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
127f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
128ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    void enableVerboseLogging(int verbose) {
129ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        if (verbose > 0 ) {
130abde872adced15dfb6781fb71959453d963326dbYuhao Zheng            DBG = true;
131ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            VDBG = true;
132ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        } else {
133abde872adced15dfb6781fb71959453d963326dbYuhao Zheng            DBG = false;
134ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            VDBG = false;
135ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
136ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
137ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
138931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
139931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     * Flush out scan results older than mScanResultMaximumAge
140ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     *
141931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     */
142f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private void ageScanResultsOut(int delay) {
143f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (delay <= 0) {
144931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            delay = mScanResultMaximumAge; // Something sane
145f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
146b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        long milli = System.currentTimeMillis();
147f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (VDBG) {
148f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            logDbg("ageScanResultsOut delay " + Integer.valueOf(delay) + " size "
149f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    + Integer.valueOf(scanResultCache.size()) + " now " + Long.valueOf(milli));
150f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
151f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
152f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        Iterator<HashMap.Entry<String,ScanResult>> iter = scanResultCache.entrySet().iterator();
153f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        while (iter.hasNext()) {
154f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            HashMap.Entry<String,ScanResult> entry = iter.next();
155f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            ScanResult result = entry.getValue();
156f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
157f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if ((result.seen + delay) < milli) {
158f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                iter.remove();
159f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
160f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
161f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
162f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
163be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle    int addToScanCache(List<ScanResult> scanList) {
164be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        int numScanResultsKnown = 0; // Record number of scan results we knew about
165be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        WifiConfiguration associatedConfig = null;
1667b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        boolean didAssociate = false;
1678242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle        long now = System.currentTimeMillis();
168f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1690c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle        ArrayList<NetworkKey> unknownScanResults = new ArrayList<NetworkKey>();
1700c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle
171f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        for(ScanResult result: scanList) {
1721fcf3c6d2b9ed65573e1e7c55fc5a30ebd364c4fYuhao Zheng            if (result.SSID == null) continue;
173c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
174c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            // Make sure we record the last time we saw this result
175f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            result.seen = System.currentTimeMillis();
176f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
177c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            // Fetch the previous instance for this result
178f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            ScanResult sr = scanResultCache.get(result.BSSID);
179f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (sr != null) {
1808ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                if (mWifiConfigStore.scanResultRssiLevelPatchUp != 0
1818ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                        && result.level == 0
1828ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                        && sr.level < -20) {
1838ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                    // A 'zero' RSSI reading is most likely a chip problem which returns
1848ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                    // an unknown RSSI, hence ignore it
1858ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                    result.level = sr.level;
1868ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                }
1878ccabb81ad304b80dc8eaa162fd322643461529bvandwalle
188931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // If there was a previous cache result for this BSSID, average the RSSI values
189c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                result.averageRssi(sr.level, sr.seen, mScanResultMaximumAge);
190f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
191c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                // Remove the previous Scan Result - this is not necessary
192f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                scanResultCache.remove(result.BSSID);
1938ccabb81ad304b80dc8eaa162fd322643461529bvandwalle            } else if (mWifiConfigStore.scanResultRssiLevelPatchUp != 0 && result.level == 0) {
1948ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                // A 'zero' RSSI reading is most likely a chip problem which returns
1958ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                // an unknown RSSI, hence initialize it to a sane value
1968ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                result.level = mWifiConfigStore.scanResultRssiLevelPatchUp;
197e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle            }
198e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle
199e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle            if (!mNetworkScoreCache.isScoredNetwork(result)) {
200e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                WifiKey wkey;
201e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                // Quoted SSIDs are the only one valid at this stage
202e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                try {
203e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    wkey = new WifiKey("\"" + result.SSID + "\"", result.BSSID);
204e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                } catch (IllegalArgumentException e) {
205e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    logDbg("AutoJoinController: received badly encoded SSID=[" + result.SSID +
206e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                            "] ->skipping this network");
207e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    wkey = null;
208e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                }
209e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                if (wkey != null) {
210e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    NetworkKey nkey = new NetworkKey(wkey);
211e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    //if we don't know this scan result then request a score from the scorer
212e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    unknownScanResults.add(nkey);
213e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                }
214e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                if (VDBG) {
2157b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    String cap = "";
2167b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    if (result.capabilities != null)
2177b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        cap = result.capabilities;
218e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    logDbg(result.SSID + " " + result.BSSID + " rssi="
2197b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                            + result.level + " cap " + cap + " is not scored");
220e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                }
2210c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle            } else {
222e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                if (VDBG) {
2237b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    String cap = "";
2247b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    if (result.capabilities != null)
2257b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        cap = result.capabilities;
226e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    int score = mNetworkScoreCache.getNetworkScore(result);
227e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    logDbg(result.SSID + " " + result.BSSID + " rssi="
2287b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                            + result.level + " cap " + cap + " is scored : " + score);
2290c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                }
230f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
231f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
232e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // scanResultCache.put(result.BSSID, new ScanResult(result));
233e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            scanResultCache.put(result.BSSID, result);
234be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            // Add this BSSID to the scanResultCache of a Saved WifiConfiguration
2357b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            didAssociate = mWifiConfigStore.updateSavedNetworkHistory(result);
236f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
237be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            // If not successful, try to associate this BSSID to an existing Saved WifiConfiguration
2387b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            if (!didAssociate) {
239be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                // We couldn't associate the scan result to a Saved WifiConfiguration
240c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                // Hence it is untrusted
241c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                result.untrusted = true;
242f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                associatedConfig = mWifiConfigStore.associateWithConfiguration(result);
2431fcf3c6d2b9ed65573e1e7c55fc5a30ebd364c4fYuhao Zheng                if (associatedConfig != null && associatedConfig.SSID != null) {
244f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (VDBG) {
245f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        logDbg("addToScanCache save associated config "
2468242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle                                + associatedConfig.SSID + " with " + result.SSID
2478242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle                                + " status " + associatedConfig.autoJoinStatus
2488242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle                                + " reason " + associatedConfig.disableReason
2498242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle                                + " tsp " + associatedConfig.blackListTimestamp
2508242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle                                + " was " + (now - associatedConfig.blackListTimestamp));
251f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
252be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                    mWifiStateMachine.sendMessage(
253be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                            WifiStateMachine.CMD_AUTO_SAVE_NETWORK, associatedConfig);
2547b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    didAssociate = true;
255f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
2560d616ef3bf635dff8722e064c0be842676390ed8vandwalle            } else {
2570d616ef3bf635dff8722e064c0be842676390ed8vandwalle                // If the scan result has been blacklisted fir 18 hours -> unblacklist
2580d616ef3bf635dff8722e064c0be842676390ed8vandwalle                if ((now - result.blackListTimestamp) > loseBlackListHardMilli) {
2590d616ef3bf635dff8722e064c0be842676390ed8vandwalle                    result.setAutoJoinStatus(ScanResult.ENABLED);
2600d616ef3bf635dff8722e064c0be842676390ed8vandwalle                }
261f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
2627b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            if (didAssociate) {
263be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                numScanResultsKnown++;
264a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                result.isAutoJoinCandidate ++;
265a0708b09ad17b086c008ab100aec7143d7613c80vandwalle            } else {
266a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                result.isAutoJoinCandidate = 0;
267be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            }
268f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
2690c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle
2700c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle        if (unknownScanResults.size() != 0) {
2710c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle            NetworkKey[] newKeys =
2720c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                    unknownScanResults.toArray(new NetworkKey[unknownScanResults.size()]);
273931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Kick the score manager, we will get updated scores asynchronously
2740c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle            scoreManager.requestScores(newKeys);
2750c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle        }
276be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        return numScanResultsKnown;
277f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
278f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
279f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void logDbg(String message) {
2800888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        logDbg(message, false);
281ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle    }
282ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle
283ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle    void logDbg(String message, boolean stackTrace) {
284ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        if (stackTrace) {
2852f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle            Log.e(TAG, message + " stack:"
286ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[2].getMethodName() + " - "
287ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[3].getMethodName() + " - "
288ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[4].getMethodName() + " - "
289ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[5].getMethodName());
290ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        } else {
2912f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle            Log.e(TAG, message);
292ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        }
293f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
294f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
295931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    // Called directly from WifiStateMachine
296be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle    int newSupplicantResults(boolean doAutoJoin) {
297be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        int numScanResultsKnown;
298e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        List<ScanResult> scanList = mWifiStateMachine.getScanResultsListNoCopyUnsync();
299be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        numScanResultsKnown = addToScanCache(scanList);
300f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        ageScanResultsOut(mScanResultMaximumAge);
301be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        if (DBG) {
302be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            logDbg("newSupplicantResults size=" + Integer.valueOf(scanResultCache.size())
3032f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        + " known=" + numScanResultsKnown + " "
3049f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        + doAutoJoin);
305be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        }
3067806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle        if (doAutoJoin) {
3077806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            attemptAutoJoin();
3087806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle        }
309005c1ef113192f898499a407dd266393a8d6b076vandwalle        mWifiConfigStore.writeKnownNetworkHistory(false);
310be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        return numScanResultsKnown;
311f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
312f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
313f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
314931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
315931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     * Not used at the moment
316f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * should be a call back from WifiScanner HAL ??
317f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * this function is not hooked and working yet, it will receive scan results from WifiScanners
318f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * with the list of IEs,then populate the capabilities by parsing the IEs and inject the scan
319f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * results as normal.
320f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     */
321f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void newHalScanResults() {
322f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        List<ScanResult> scanList = null;//mWifiScanner.syncGetScanResultsList();
323f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String akm = WifiParser.parse_akm(null, null);
324f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        logDbg(akm);
325f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        addToScanCache(scanList);
326f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        ageScanResultsOut(0);
327f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        attemptAutoJoin();
328005c1ef113192f898499a407dd266393a8d6b076vandwalle        mWifiConfigStore.writeKnownNetworkHistory(false);
329f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
330f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
331931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
332931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     *  network link quality changed, called directly from WifiTrafficPoller,
333931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     * or by listening to Link Quality intent
334931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     */
335f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void linkQualitySignificantChange() {
336f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        attemptAutoJoin();
337f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
338f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
339931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
340f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * compare a WifiConfiguration against the current network, return a delta score
341f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * If not associated, and the candidate will always be better
342f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * For instance if the candidate is a home network versus an unknown public wifi,
343f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * the delta will be infinite, else compare Kepler scores etc…
344b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle     * Negatve return values from this functions are meaningless per se, just trying to
345b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle     * keep them distinct for debug purpose (i.e. -1, -2 etc...)
346931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     */
347e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle    private int compareNetwork(WifiConfiguration candidate,
348e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                               String lastSelectedConfiguration) {
349b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        if (candidate == null)
350b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle            return -3;
351b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle
352f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration currentNetwork = mWifiStateMachine.getCurrentWifiConfiguration();
353b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        if (currentNetwork == null) {
354c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // Return any absurdly high score, if we are not connected there is no current
355c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // network to...
356b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle           return 1000;
357b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        }
358f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
359f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (candidate.configKey(true).equals(currentNetwork.configKey(true))) {
360b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle            return -2;
361f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
362f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
363b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle        if (DBG) {
364e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            logDbg("compareNetwork will compare " + candidate.configKey()
365e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    + " with current " + currentNetwork.configKey());
366b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle        }
367833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle        int order = compareWifiConfigurations(currentNetwork, candidate);
368e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
369e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        // The lastSelectedConfiguration is the configuration the user has manually selected
370e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        // thru WifiPicker, or that a 3rd party app asked us to connect to via the
371e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        // enableNetwork with disableOthers=true WifiManager API
372e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        // As this is a direct user choice, we strongly prefer this configuration,
373e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        // hence give +/-100
374e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        if ((lastSelectedConfiguration != null)
375e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                && currentNetwork.configKey().equals(lastSelectedConfiguration)) {
376e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // currentNetwork is the last selected configuration,
377e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // so keep it above connect choices (+/-60) and
378e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // above RSSI/scorer based selection of linked configuration (+/- 50)
379e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // by reducing order by -100
380e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            order = order - 100;
381e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            if (VDBG)   {
382e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                logDbg("     ...and prefers -100 " + currentNetwork.configKey()
383e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " over " + candidate.configKey()
384e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " because it is the last selected -> "
385e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + Integer.toString(order));
386e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            }
387e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        } else if ((lastSelectedConfiguration != null)
388e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                && candidate.configKey().equals(lastSelectedConfiguration)) {
389e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // candidate is the last selected configuration,
390e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // so keep it above connect choices (+/-60) and
391e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // above RSSI/scorer based selection of linked configuration (+/- 50)
392e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // by increasing order by +100
393e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            order = order + 100;
394e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            if (VDBG)   {
395e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                logDbg("     ...and prefers +100 " + candidate.configKey()
396e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " over " + currentNetwork.configKey()
397e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " because it is the last selected -> "
398e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + Integer.toString(order));
399e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            }
400e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        }
401e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
402ede507649471f1113e9e1919812115ca5a6bc0c8vandwalle        return order;
403f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
404f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
405ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle    /**
406ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * update the network history fields fo that configuration
407ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * - if userTriggered, we mark the configuration as "non selfAdded" since the user has seen it
408ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * and took over management
409ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * - if it is a "connect", remember which network were there at the point of the connect, so
410ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * as those networks get a relative lower score than the selected configuration
41162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle     *
412ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * @param netId
413ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * @param userTriggered : if the update come from WiFiManager
414ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * @param connect : if the update includes a connect
415931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     */
41662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle    public void updateConfigurationHistory(int netId, boolean userTriggered, boolean connect) {
417f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration selected = mWifiConfigStore.getWifiConfiguration(netId);
418f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (selected == null) {
419c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            logDbg("updateConfigurationHistory nid=" + netId + " no selected configuration!");
420f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return;
421f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
422f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
423e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle        if (selected.SSID == null) {
424c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            logDbg("updateConfigurationHistory nid=" + netId +
425c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    " no SSID in selected configuration!");
426e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle            return;
427e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle        }
428e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle
42962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle        if (userTriggered) {
430931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Reenable autojoin for this network,
43162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            // since the user want to connect to this configuration
43227355a942653264388e909a4276196ee63e57811vandwalle            selected.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
43362f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            selected.selfAdded = false;
434e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            selected.dirty = true;
43562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle        }
436f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
437992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle        if (DBG && userTriggered) {
438f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (selected.connectChoices != null) {
439ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                logDbg("updateConfigurationHistory will update "
440f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        + Integer.toString(netId) + " now: "
441992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        + Integer.toString(selected.connectChoices.size())
442992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        + " uid=" + Integer.toString(selected.creatorUid), true);
443f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else {
444ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                logDbg("updateConfigurationHistory will update "
445992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        + Integer.toString(netId)
446992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        + " uid=" + Integer.toString(selected.creatorUid), true);
447f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
448f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
449f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
450ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        if (connect && userTriggered) {
451ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            boolean found = false;
4522451dbcc4f9641df188326215b204b798eb70c46vandwalle            int choice = 0;
453c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            int size = 0;
4549f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
4559f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            // Reset the triggered disabled count, because user wanted to connect to this
4569f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            // configuration, and we were not.
4579f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            selected.numUserTriggeredWifiDisableLowRSSI = 0;
4589f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            selected.numUserTriggeredWifiDisableBadRSSI = 0;
4599f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            selected.numUserTriggeredWifiDisableNotHighRSSI = 0;
4609f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            selected.numUserTriggeredJoinAttempts++;
4619f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
46262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            List<WifiConfiguration> networks =
46362f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    mWifiConfigStore.getRecentConfiguredNetworks(12000, false);
464c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            if (networks != null) size = networks.size();
465c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            logDbg("updateConfigurationHistory found " + size + " networks");
46662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            if (networks != null) {
46762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                for (WifiConfiguration config : networks) {
468992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    if (DBG) {
469992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        logDbg("updateConfigurationHistory got " + config.SSID + " nid="
470992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                + Integer.toString(config.networkId));
471992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    }
472f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
47362f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    if (selected.configKey(true).equals(config.configKey(true))) {
474ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        found = true;
47562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                        continue;
47662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    }
477f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
4782451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // Compare RSSI values so as to evaluate the strength of the user preference
4792451dbcc4f9641df188326215b204b798eb70c46vandwalle                    int order = compareWifiConfigurationsRSSI(config, selected, null);
4802451dbcc4f9641df188326215b204b798eb70c46vandwalle
4812451dbcc4f9641df188326215b204b798eb70c46vandwalle                    if (order < -30) {
4822451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // Selected configuration is worse than the visible configuration
4832451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // hence register a strong choice so as autojoin cannot override this
4842451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // for instance, the user has select a network
4852451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // with 1 bar over a network with 3 bars...
4862451dbcc4f9641df188326215b204b798eb70c46vandwalle                        choice = 60;
4872451dbcc4f9641df188326215b204b798eb70c46vandwalle                    } else if (order < -20) {
4882451dbcc4f9641df188326215b204b798eb70c46vandwalle                        choice = 50;
4892451dbcc4f9641df188326215b204b798eb70c46vandwalle                    } else if (order < -10) {
4902451dbcc4f9641df188326215b204b798eb70c46vandwalle                        choice = 40;
4912f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    } else if (order < 20) {
4922451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // Selected configuration is about same or has a slightly better RSSI
4932451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // hence register a weaker choice, here a difference of at least +/-30 in
4942451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // RSSI comparison triggered by autoJoin will override the choice
4952451dbcc4f9641df188326215b204b798eb70c46vandwalle                        choice = 30;
4962f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    } else {
4972451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // Selected configuration is better than the visible configuration
4982451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // hence we do not know if the user prefers this configuration strongly
4992451dbcc4f9641df188326215b204b798eb70c46vandwalle                        choice = 20;
50062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    }
501f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
502931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // The selected configuration was preferred over a recently seen config
503931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // hence remember the user's choice:
504931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // add the recently seen config to the selected's connectChoices array
505ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle
506ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    if (selected.connectChoices == null) {
507ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        selected.connectChoices = new HashMap<String, Integer>();
508ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    }
509ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle
510ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    logDbg("updateConfigurationHistory add a choice " + selected.configKey(true)
5110888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            + " over " + config.configKey(true)
5122451dbcc4f9641df188326215b204b798eb70c46vandwalle                            + " choice " + Integer.toString(choice));
513cf5b8eb8a08c45bd4a82f1f4bb789c8a1b08744fvandwalle
5142451dbcc4f9641df188326215b204b798eb70c46vandwalle                    Integer currentChoice = selected.connectChoices.get(config.configKey(true));
5152f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    if (currentChoice != null) {
5162f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        // User has made this choice multiple time in a row, so bump up a lot
5172f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        choice += currentChoice.intValue();
5182451dbcc4f9641df188326215b204b798eb70c46vandwalle                    }
5192f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    // Add the visible config to the selected's connect choice list
5202f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    selected.connectChoices.put(config.configKey(true), choice);
521f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
52262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    if (config.connectChoices != null) {
523ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        if (VDBG) {
524ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                            logDbg("updateConfigurationHistory will remove "
52562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                                    + selected.configKey(true) + " from " + config.configKey(true));
526ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        }
527931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                        // Remove the selected from the recently seen config's connectChoice list
52862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                        config.connectChoices.remove(selected.configKey(true));
5290888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle
5300888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        if (selected.linkedConfigurations != null) {
531931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                           // Remove the selected's linked configuration from the
532931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                           // recently seen config's connectChoice list
5330888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                           for (String key : selected.linkedConfigurations.keySet()) {
5340888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                               config.connectChoices.remove(key);
5350888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                           }
5360888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        }
53762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    }
538ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                }
539ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                if (found == false) {
5402451dbcc4f9641df188326215b204b798eb70c46vandwalle                     // We haven't found the configuration that the user just selected in our
5412451dbcc4f9641df188326215b204b798eb70c46vandwalle                     // scan cache.
5422451dbcc4f9641df188326215b204b798eb70c46vandwalle                     // In that case we will need a new scan before attempting to connect to this
5432451dbcc4f9641df188326215b204b798eb70c46vandwalle                     // configuration anyhow and thus we can process the scan results then.
544ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                     logDbg("updateConfigurationHistory try to connect to an old network!! : "
545ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                             + selected.configKey());
54662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                }
547f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
54862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                if (selected.connectChoices != null) {
54962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    if (VDBG)
550ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        logDbg("updateConfigurationHistory " + Integer.toString(netId)
55162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                                + " now: " + Integer.toString(selected.connectChoices.size()));
55262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                }
553f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
554f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
555992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle
556931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        // TODO: write only if something changed
557992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle        if (userTriggered || connect) {
558005c1ef113192f898499a407dd266393a8d6b076vandwalle            mWifiConfigStore.writeKnownNetworkHistory(false);
559992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle        }
560f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
561f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
5622451dbcc4f9641df188326215b204b798eb70c46vandwalle    int getConnectChoice(WifiConfiguration source, WifiConfiguration target) {
5632451dbcc4f9641df188326215b204b798eb70c46vandwalle        Integer choice = null;
5642451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (source == null || target == null) {
5652451dbcc4f9641df188326215b204b798eb70c46vandwalle            return 0;
566f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
567f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
5682451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (source.connectChoices != null
5692451dbcc4f9641df188326215b204b798eb70c46vandwalle                && source.connectChoices.containsKey(target.configKey(true))) {
5702451dbcc4f9641df188326215b204b798eb70c46vandwalle            choice = source.connectChoices.get(target.configKey(true));
5712451dbcc4f9641df188326215b204b798eb70c46vandwalle        } else if (source.linkedConfigurations != null) {
572f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            for (String key : source.linkedConfigurations.keySet()) {
573f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                WifiConfiguration config = mWifiConfigStore.getWifiConfiguration(key);
574f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (config != null) {
575f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (config.connectChoices != null) {
5762451dbcc4f9641df188326215b204b798eb70c46vandwalle                        choice = config.connectChoices.get(target.configKey(true));
577f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
578f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
579f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
5802451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
5812451dbcc4f9641df188326215b204b798eb70c46vandwalle
5822451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (choice == null) {
5832451dbcc4f9641df188326215b204b798eb70c46vandwalle            //We didn't find the connect choice
5842451dbcc4f9641df188326215b204b798eb70c46vandwalle            return 0;
5852451dbcc4f9641df188326215b204b798eb70c46vandwalle        } else {
5862451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (choice.intValue() < 0) {
5872451dbcc4f9641df188326215b204b798eb70c46vandwalle                choice = 20; // Compatibility with older files
5882451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
5892451dbcc4f9641df188326215b204b798eb70c46vandwalle            return choice.intValue();
5902451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
5912451dbcc4f9641df188326215b204b798eb70c46vandwalle    }
5922451dbcc4f9641df188326215b204b798eb70c46vandwalle
593815788ba7838fc54310baed3deb9b95548e0ce69vandwalle    int compareWifiConfigurationsFromVisibility(WifiConfiguration a, int aRssiBoost,
594815788ba7838fc54310baed3deb9b95548e0ce69vandwalle             WifiConfiguration b, int bRssiBoost) {
5952451dbcc4f9641df188326215b204b798eb70c46vandwalle
596815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        int aRssiBoost5 = 0; // 5GHz RSSI boost to apply for purpose band selection (5GHz pref)
597815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        int bRssiBoost5 = 0; // 5GHz RSSI boost to apply for purpose band selection (5GHz pref)
598815788ba7838fc54310baed3deb9b95548e0ce69vandwalle
599815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        int aScore = 0;
600815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        int bScore = 0;
601815788ba7838fc54310baed3deb9b95548e0ce69vandwalle
602815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        boolean aPrefers5GHz = false;
603815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        boolean bPrefers5GHz = false;
6044dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
6052451dbcc4f9641df188326215b204b798eb70c46vandwalle        /**
606815788ba7838fc54310baed3deb9b95548e0ce69vandwalle         * Calculate a boost to apply to RSSI value of configuration we want to join on 5GHz:
607815788ba7838fc54310baed3deb9b95548e0ce69vandwalle         * Boost RSSI value of 5GHz bands iff the base value is better than threshold,
608815788ba7838fc54310baed3deb9b95548e0ce69vandwalle         * penalize the RSSI value of 5GHz band iff the base value is lower than threshold
6092451dbcc4f9641df188326215b204b798eb70c46vandwalle         * This implements band preference where we prefer 5GHz if RSSI5 is good enough, whereas
6102451dbcc4f9641df188326215b204b798eb70c46vandwalle         * we prefer 2.4GHz otherwise.
6112451dbcc4f9641df188326215b204b798eb70c46vandwalle         */
612815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        aRssiBoost5 = rssiBoostFrom5GHzRssi(a.visibility.rssi5, a.configKey() + "->");
613815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        bRssiBoost5 = rssiBoostFrom5GHzRssi(b.visibility.rssi5, b.configKey() + "->");
6142451dbcc4f9641df188326215b204b798eb70c46vandwalle
615815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        // Select which band to use for a
616f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle        if (a.visibility.rssi5 + aRssiBoost5 > a.visibility.rssi24) {
6172451dbcc4f9641df188326215b204b798eb70c46vandwalle            // Prefer a's 5GHz
618815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            aPrefers5GHz = true;
619815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        }
620815788ba7838fc54310baed3deb9b95548e0ce69vandwalle
621815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        // Select which band to use for b
622815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        if (b.visibility.rssi5 + bRssiBoost5 > b.visibility.rssi24) {
623815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            // Prefer b's 5GHz
624815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            bPrefers5GHz = true;
625815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        }
626815788ba7838fc54310baed3deb9b95548e0ce69vandwalle
627815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        if (aPrefers5GHz) {
628815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            if (bPrefers5GHz) {
629815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // If both a and b are on 5GHz then we don't apply the 5GHz RSSI boost to either
630815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // one, but directly compare the RSSI values, this improves stability,
631815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // since the 5GHz RSSI boost can introduce large fluctuations
632815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                aScore = a.visibility.rssi5 + aRssiBoost;
633815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            } else {
634815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // If only a is on 5GHz, then apply the 5GHz preference boost to a
635815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                aScore = a.visibility.rssi5 + aRssiBoost + aRssiBoost5;
636815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            }
6372451dbcc4f9641df188326215b204b798eb70c46vandwalle        } else {
638815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            aScore = a.visibility.rssi24 + aRssiBoost;
639f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
6402451dbcc4f9641df188326215b204b798eb70c46vandwalle
641815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        if (bPrefers5GHz) {
642815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            if (aPrefers5GHz) {
643815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // If both a and b are on 5GHz then we don't apply the 5GHz RSSI boost to either
644815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // one, but directly compare the RSSI values, this improves stability,
645815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // since the 5GHz RSSI boost can introduce large fluctuations
646815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                bScore = b.visibility.rssi5 + bRssiBoost;
647815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            } else {
648815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // If only b is on 5GHz, then apply the 5GHz preference boost to b
649815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                bScore = b.visibility.rssi5 + bRssiBoost + bRssiBoost5;
650815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            }
651815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        } else {
652815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            bScore = b.visibility.rssi24 + bRssiBoost;
653815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        }
654815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        if (VDBG) {
655815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            logDbg("        " + a.configKey() + " is5=" + aPrefers5GHz + " score=" + aScore
656f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                    + " " + b.configKey() + " is5=" + bPrefers5GHz + " score=" + bScore);
657815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        }
658f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle
659f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle        // Debug only, record RSSI comparison parameters
660f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle        if (a.visibility != null) {
661f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle            a.visibility.score = aScore;
662f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle            a.visibility.currentNetworkBoost = aRssiBoost;
663f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle            a.visibility.bandPreferenceBoost = aRssiBoost5;
664f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle        }
665f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle        if (b.visibility != null) {
666f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle            b.visibility.score = bScore;
667f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle            b.visibility.currentNetworkBoost = bRssiBoost;
668f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle            b.visibility.bandPreferenceBoost = bRssiBoost5;
669f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle        }
670f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle
671815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        // Compare a and b
672815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        // If a score is higher then a > b and the order is descending (negative)
673815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        // If b score is higher then a < b and the order is ascending (positive)
674815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        return bScore - aScore;
675f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
676f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
6772451dbcc4f9641df188326215b204b798eb70c46vandwalle    // Compare WifiConfiguration by RSSI, and return a comparison value in the range [-50, +50]
6782451dbcc4f9641df188326215b204b798eb70c46vandwalle    // The result represents "approximately" an RSSI difference measured in dBM
6792451dbcc4f9641df188326215b204b798eb70c46vandwalle    // Adjusted with various parameters:
6802451dbcc4f9641df188326215b204b798eb70c46vandwalle    // +) current network gets a +15 boost
6812451dbcc4f9641df188326215b204b798eb70c46vandwalle    // +) 5GHz signal, if they are strong enough, get a +15 or +25 boost, representing the
6822451dbcc4f9641df188326215b204b798eb70c46vandwalle    // fact that at short range we prefer 5GHz band as it is cleaner of interference and
6832451dbcc4f9641df188326215b204b798eb70c46vandwalle    // provides for wider channels
6842451dbcc4f9641df188326215b204b798eb70c46vandwalle    int compareWifiConfigurationsRSSI(WifiConfiguration a, WifiConfiguration b,
6852451dbcc4f9641df188326215b204b798eb70c46vandwalle                                      String currentConfiguration) {
686f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        int order = 0;
6872451dbcc4f9641df188326215b204b798eb70c46vandwalle
6882451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Boost used so as to favor current config
6892451dbcc4f9641df188326215b204b798eb70c46vandwalle        int aRssiBoost = 0;
6902451dbcc4f9641df188326215b204b798eb70c46vandwalle        int bRssiBoost = 0;
6912451dbcc4f9641df188326215b204b798eb70c46vandwalle
6922451dbcc4f9641df188326215b204b798eb70c46vandwalle        int scoreA;
6932451dbcc4f9641df188326215b204b798eb70c46vandwalle        int scoreB;
6942451dbcc4f9641df188326215b204b798eb70c46vandwalle
6952451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Retrieve the visibility
696f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration.Visibility astatus = a.visibility;
697f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration.Visibility bstatus = b.visibility;
698f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (astatus == null || bstatus == null) {
6992451dbcc4f9641df188326215b204b798eb70c46vandwalle            // Error visibility wasn't set
700b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("    compareWifiConfigurations NULL band status!");
701f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return 0;
702f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
7032451dbcc4f9641df188326215b204b798eb70c46vandwalle
7042451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Apply Hysteresis, boost RSSI of current configuration
7052451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (null != currentConfiguration) {
7062451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (a.configKey().equals(currentConfiguration)) {
70793a1fddee50a244d31036cddae6b7db6630fd93dvandwalle                aRssiBoost = mWifiConfigStore.currentNetworkBoost;
7082451dbcc4f9641df188326215b204b798eb70c46vandwalle            } else if (b.configKey().equals(currentConfiguration)) {
70993a1fddee50a244d31036cddae6b7db6630fd93dvandwalle                bRssiBoost = mWifiConfigStore.currentNetworkBoost;
7102451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
7112451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
7122451dbcc4f9641df188326215b204b798eb70c46vandwalle
7132451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (VDBG)  {
714b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("    compareWifiConfigurationsRSSI: " + a.configKey()
715f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                    + " rssi=" + Integer.toString(astatus.rssi24)
716c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    + "," + Integer.toString(astatus.rssi5)
7172451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " boost=" + Integer.toString(aRssiBoost)
718f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                    + " " + b.configKey() + " rssi="
719c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    + Integer.toString(bstatus.rssi24) + ","
720c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    + Integer.toString(bstatus.rssi5)
7212451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " boost=" + Integer.toString(bRssiBoost)
7222451dbcc4f9641df188326215b204b798eb70c46vandwalle            );
723f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
7242451dbcc4f9641df188326215b204b798eb70c46vandwalle
725815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        order = compareWifiConfigurationsFromVisibility(a, aRssiBoost, b, bRssiBoost);
7262451dbcc4f9641df188326215b204b798eb70c46vandwalle
7272451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Normalize the order to [-50, +50]
7282451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (order > 50) order = 50;
7292451dbcc4f9641df188326215b204b798eb70c46vandwalle        else if (order < -50) order = -50;
7302451dbcc4f9641df188326215b204b798eb70c46vandwalle
7312451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (VDBG) {
7322451dbcc4f9641df188326215b204b798eb70c46vandwalle            String prefer = " = ";
7332451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (order > 0) {
7342451dbcc4f9641df188326215b204b798eb70c46vandwalle                prefer = " < "; // Ascending
7352451dbcc4f9641df188326215b204b798eb70c46vandwalle            } else if (order < 0) {
7362451dbcc4f9641df188326215b204b798eb70c46vandwalle                prefer = " > "; // Descending
7372451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
738b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("    compareWifiConfigurationsRSSI " + a.configKey()
7392451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " rssi=(" + a.visibility.rssi24
7402451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + a.visibility.rssi5
7412451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + ") num=(" + a.visibility.num24
7422451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + a.visibility.num5 + ")"
7432451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + prefer + b.configKey()
7442451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " rssi=(" + b.visibility.rssi24
7452451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + b.visibility.rssi5
7462451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + ") num=(" + b.visibility.num24
7472451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + b.visibility.num5 + ")"
7482451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " -> " + order);
7492451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
7502451dbcc4f9641df188326215b204b798eb70c46vandwalle
751f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        return order;
752f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
753f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
754833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle    /**
755833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle     * b/18490330 only use scorer for untrusted networks
756833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle     *
7572451dbcc4f9641df188326215b204b798eb70c46vandwalle    int compareWifiConfigurationsWithScorer(WifiConfiguration a, WifiConfiguration b) {
7582451dbcc4f9641df188326215b204b798eb70c46vandwalle
75981c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson        boolean aIsActive = false;
76081c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson        boolean bIsActive = false;
7612451dbcc4f9641df188326215b204b798eb70c46vandwalle
7622451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Apply Hysteresis : boost RSSI of current configuration before
7632451dbcc4f9641df188326215b204b798eb70c46vandwalle        // looking up the score
7642451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (null != mCurrentConfigurationKey) {
7652451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (a.configKey().equals(mCurrentConfigurationKey)) {
76681c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                aIsActive = true;
7672451dbcc4f9641df188326215b204b798eb70c46vandwalle            } else if (b.configKey().equals(mCurrentConfigurationKey)) {
76881c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                bIsActive = true;
7692451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
7702451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
771833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle        int scoreA = getConfigNetworkScore(a, mScanResultAutoJoinAge, aIsActive);
772833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle        int scoreB = getConfigNetworkScore(b, mScanResultAutoJoinAge, bIsActive);
7732451dbcc4f9641df188326215b204b798eb70c46vandwalle
7742451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Both configurations need to have a score for the scorer to be used
7752451dbcc4f9641df188326215b204b798eb70c46vandwalle        // ...and the scores need to be different:-)
7762451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (scoreA == WifiNetworkScoreCache.INVALID_NETWORK_SCORE
7772451dbcc4f9641df188326215b204b798eb70c46vandwalle                || scoreB == WifiNetworkScoreCache.INVALID_NETWORK_SCORE) {
778e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle            if (VDBG)  {
779b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    compareWifiConfigurationsWithScorer no-scores: "
780e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                        + a.configKey()
781e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                        + " "
782e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                        + b.configKey());
783e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle            }
7842451dbcc4f9641df188326215b204b798eb70c46vandwalle            return 0;
7852451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
7862451dbcc4f9641df188326215b204b798eb70c46vandwalle
7872451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (VDBG) {
7882451dbcc4f9641df188326215b204b798eb70c46vandwalle            String prefer = " = ";
7892451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (scoreA < scoreB) {
7902451dbcc4f9641df188326215b204b798eb70c46vandwalle                prefer = " < ";
7912451dbcc4f9641df188326215b204b798eb70c46vandwalle            } if (scoreA > scoreB) {
7922451dbcc4f9641df188326215b204b798eb70c46vandwalle                prefer = " > ";
7932451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
794b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("    compareWifiConfigurationsWithScorer " + a.configKey()
7952451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " rssi=(" + a.visibility.rssi24
7962451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + a.visibility.rssi5
7972451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + ") num=(" + a.visibility.num24
7982451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + a.visibility.num5 + ")"
799e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    + " sc=" + scoreA
8002451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + prefer + b.configKey()
8012451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " rssi=(" + b.visibility.rssi24
8022451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + b.visibility.rssi5
8032451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + ") num=(" + b.visibility.num24
8042451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + b.visibility.num5 + ")"
805e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    + " sc=" + scoreB
8062451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " -> " + Integer.toString(scoreB - scoreA));
8072451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
808c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
8092451dbcc4f9641df188326215b204b798eb70c46vandwalle        // If scoreA > scoreB, the comparison is descending hence the return value is negative
8102451dbcc4f9641df188326215b204b798eb70c46vandwalle        return scoreB - scoreA;
8112451dbcc4f9641df188326215b204b798eb70c46vandwalle    }
812833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle     */
8132451dbcc4f9641df188326215b204b798eb70c46vandwalle
814f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    int compareWifiConfigurations(WifiConfiguration a, WifiConfiguration b) {
815f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        int order = 0;
816f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        boolean linked = false;
817f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
818453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        if ((a.linkedConfigurations != null) && (b.linkedConfigurations != null)
819453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                && (a.autoJoinStatus == WifiConfiguration.AUTO_JOIN_ENABLED)
820453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                && (b.autoJoinStatus == WifiConfiguration.AUTO_JOIN_ENABLED)) {
8212451dbcc4f9641df188326215b204b798eb70c46vandwalle            if ((a.linkedConfigurations.get(b.configKey(true)) != null)
8222451dbcc4f9641df188326215b204b798eb70c46vandwalle                    && (b.linkedConfigurations.get(a.configKey(true)) != null)) {
823f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                linked = true;
824f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
825f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
826f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
827f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (a.ephemeral && b.ephemeral == false) {
828f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG) {
829b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    compareWifiConfigurations ephemeral and prefers " + b.configKey()
830453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + " over " + a.configKey());
831f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
832931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            return 1; // b is of higher priority - ascending
833f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
834f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (b.ephemeral && a.ephemeral == false) {
835f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG) {
836b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    compareWifiConfigurations ephemeral and prefers " + a.configKey()
837453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + " over " + b.configKey());
838f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
839931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            return -1; // a is of higher priority - descending
840f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
841f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
8422451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Apply RSSI, in the range [-5, +5]
8432451dbcc4f9641df188326215b204b798eb70c46vandwalle        // after band adjustment, +n difference roughly corresponds to +10xn dBm
8442451dbcc4f9641df188326215b204b798eb70c46vandwalle        order = order + compareWifiConfigurationsRSSI(a, b, mCurrentConfigurationKey);
845f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
8462451dbcc4f9641df188326215b204b798eb70c46vandwalle        // If the configurations are not linked, compare by user's choice, only a
8472451dbcc4f9641df188326215b204b798eb70c46vandwalle        // very high RSSI difference can then override the choice
8482451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (!linked) {
8492451dbcc4f9641df188326215b204b798eb70c46vandwalle            int choice;
850f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
8512451dbcc4f9641df188326215b204b798eb70c46vandwalle            choice = getConnectChoice(a, b);
8522451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (choice > 0) {
853931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // a is of higher priority - descending
8542451dbcc4f9641df188326215b204b798eb70c46vandwalle                order = order - choice;
855f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (VDBG) {
856b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    logDbg("    compareWifiConfigurations prefers " + a.configKey()
857453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                            + " over " + b.configKey()
858b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + " due to user choice of " + choice
859b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + " order -> " + Integer.toString(order));
860f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
861f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                if (a.visibility != null) {
862f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                    a.visibility.lastChoiceBoost = choice;
863f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                    a.visibility.lastChoiceConfig = b.configKey();
864f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                }
8652451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
8662451dbcc4f9641df188326215b204b798eb70c46vandwalle
8672451dbcc4f9641df188326215b204b798eb70c46vandwalle            choice = getConnectChoice(b, a);
8682451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (choice > 0) {
869931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // a is of lower priority - ascending
8702451dbcc4f9641df188326215b204b798eb70c46vandwalle                order = order + choice;
8714dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                if (VDBG) {
872b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    logDbg("    compareWifiConfigurations prefers " + b.configKey() + " over "
873b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + a.configKey() + " due to user choice of " + choice
874b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + " order ->" + Integer.toString(order));
875f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
876f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                if (b.visibility != null) {
877f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                    b.visibility.lastChoiceBoost = choice;
878f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                    b.visibility.lastChoiceConfig = a.configKey();
879f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                }
880f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
881f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
882ede1310be531a84faa08f02c3fd243448dd936ddvandwalle
883f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (order == 0) {
884931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // We don't know anything - pick the last seen i.e. K behavior
885931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // we should do this only for recently picked configurations
886f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (a.priority > b.priority) {
887931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // a is of higher priority - descending
8882451dbcc4f9641df188326215b204b798eb70c46vandwalle                if (VDBG) {
889b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    logDbg("    compareWifiConfigurations prefers -1 " + a.configKey() + " over "
890453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                            + b.configKey() + " due to priority");
891f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
892f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
893f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                order = -1;
894f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else if (a.priority < b.priority) {
895931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // a is of lower priority - ascending
8962451dbcc4f9641df188326215b204b798eb70c46vandwalle                if (VDBG) {
897b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    logDbg("    compareWifiConfigurations prefers +1 " + b.configKey() + " over "
898453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                            + a.configKey() + " due to priority");
899f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
9002451dbcc4f9641df188326215b204b798eb70c46vandwalle                order = 1;
901f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
902f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
903f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
904f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String sorder = " == ";
905931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        if (order > 0) {
906f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            sorder = " < ";
907931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        } else if (order < 0) {
908f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            sorder = " > ";
909931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        }
910f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
9112451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (VDBG) {
912b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("compareWifiConfigurations: " + a.configKey() + sorder
913453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    + b.configKey() + " order " + Integer.toString(order));
914f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
915f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
916f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        return order;
917f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
918f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
919c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    boolean isBadCandidate(int rssi5, int rssi24) {
920c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        return (rssi5 < -80 && rssi24 < -90);
921c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    }
922c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
923833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle    /*
924c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    int compareWifiConfigurationsTop(WifiConfiguration a, WifiConfiguration b) {
925c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        int scorerOrder = compareWifiConfigurationsWithScorer(a, b);
926c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        int order = compareWifiConfigurations(a, b);
927c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
928c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        if (scorerOrder * order < 0) {
929b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            if (VDBG) {
930b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    -> compareWifiConfigurationsTop: " +
931b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                        "scorer override " + scorerOrder + " " + order);
932b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            }
933c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // For debugging purpose, remember that an override happened
934c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // during that autojoin Attempt
935c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            didOverride = true;
936c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            a.numScorerOverride++;
937c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            b.numScorerOverride++;
938c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        }
939c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
940c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        if (scorerOrder != 0) {
941c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // If the scorer came up with a result then use the scorer's result, else use
942c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // the order provided by the base comparison function
943c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            order = scorerOrder;
944c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        }
945c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        return order;
946c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    }
947833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle    */
948c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
9499f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    public int rssiBoostFrom5GHzRssi(int rssi, String dbg) {
9500eebae7334d6129f7ca1344e4b20199794994358vandwalle        if (!mWifiConfigStore.enable5GHzPreference) {
9510eebae7334d6129f7ca1344e4b20199794994358vandwalle            return 0;
9520eebae7334d6129f7ca1344e4b20199794994358vandwalle        }
953e67ec726c07410073575473c0f50dc737629f5davandwalle        if (rssi
9540eebae7334d6129f7ca1344e4b20199794994358vandwalle                > mWifiConfigStore.bandPreferenceBoostThreshold5) {
955e67ec726c07410073575473c0f50dc737629f5davandwalle            // Boost by 2 dB for each point
956e67ec726c07410073575473c0f50dc737629f5davandwalle            //    Start boosting at -65
957e67ec726c07410073575473c0f50dc737629f5davandwalle            //    Boost by 20 if above -55
958e67ec726c07410073575473c0f50dc737629f5davandwalle            //    Boost by 40 if abore -45
9590eebae7334d6129f7ca1344e4b20199794994358vandwalle            int boost = mWifiConfigStore.bandPreferenceBoostFactor5
9600eebae7334d6129f7ca1344e4b20199794994358vandwalle                    *(rssi - mWifiConfigStore.bandPreferenceBoostThreshold5);
961e67ec726c07410073575473c0f50dc737629f5davandwalle            if (boost > 50) {
962815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // 50 dB boost allows jumping from 2.4 to 5GHz
963e67ec726c07410073575473c0f50dc737629f5davandwalle                // consistently
964e67ec726c07410073575473c0f50dc737629f5davandwalle                boost = 50;
965e67ec726c07410073575473c0f50dc737629f5davandwalle            }
9669f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            if (VDBG && dbg != null) {
967f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                logDbg("        " + dbg + ":    rssi5 " + rssi + " 5GHz-boost " + boost);
9689f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            }
969e67ec726c07410073575473c0f50dc737629f5davandwalle            return boost;
970e67ec726c07410073575473c0f50dc737629f5davandwalle        }
971e67ec726c07410073575473c0f50dc737629f5davandwalle
972e67ec726c07410073575473c0f50dc737629f5davandwalle        if (rssi
9730eebae7334d6129f7ca1344e4b20199794994358vandwalle                < mWifiConfigStore.bandPreferencePenaltyThreshold5) {
9740eebae7334d6129f7ca1344e4b20199794994358vandwalle            // penalize if < -75
9750eebae7334d6129f7ca1344e4b20199794994358vandwalle            int boost = mWifiConfigStore.bandPreferencePenaltyFactor5
9760eebae7334d6129f7ca1344e4b20199794994358vandwalle                    *(rssi - mWifiConfigStore.bandPreferencePenaltyThreshold5);
9770eebae7334d6129f7ca1344e4b20199794994358vandwalle            return boost;
978e67ec726c07410073575473c0f50dc737629f5davandwalle        }
979e67ec726c07410073575473c0f50dc737629f5davandwalle        return 0;
980e67ec726c07410073575473c0f50dc737629f5davandwalle    }
981c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        /**
982e67ec726c07410073575473c0f50dc737629f5davandwalle         * attemptRoam() function implements the core of the same SSID switching algorithm
983e67ec726c07410073575473c0f50dc737629f5davandwalle         *
984e67ec726c07410073575473c0f50dc737629f5davandwalle         * Run thru all recent scan result of a WifiConfiguration and select the
985e67ec726c07410073575473c0f50dc737629f5davandwalle         * best one.
986c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle         */
987e67ec726c07410073575473c0f50dc737629f5davandwalle    public ScanResult attemptRoam(ScanResult a,
988e67ec726c07410073575473c0f50dc737629f5davandwalle                                  WifiConfiguration current, int age, String currentBSSID) {
989b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        if (current == null) {
990b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            if (VDBG)   {
991b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                logDbg("attemptRoam not associated");
992b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            }
993e67ec726c07410073575473c0f50dc737629f5davandwalle            return a;
9944dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
995b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        if (current.scanResultCache == null) {
996b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            if (VDBG)   {
997b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                logDbg("attemptRoam no scan cache");
998b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            }
999e67ec726c07410073575473c0f50dc737629f5davandwalle            return a;
10004dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
1001b07da189850a4bfa268f8ab9be7867935eb2ecb5vandwalle        if (current.scanResultCache.size() > 6) {
1002b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            if (VDBG)   {
1003c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                logDbg("attemptRoam scan cache size "
1004c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                        + current.scanResultCache.size() + " --> bail");
1005b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            }
1006931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Implement same SSID roaming only for configurations
1007c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle            // that have less than 4 BSSIDs
1008e67ec726c07410073575473c0f50dc737629f5davandwalle            return a;
10094dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
1010b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle
10112f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle        if (current.BSSID != null && !current.BSSID.equals("any")) {
1012b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            if (DBG)   {
10132f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                logDbg("attemptRoam() BSSID is set "
10142f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        + current.BSSID + " -> bail");
1015b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            }
1016e67ec726c07410073575473c0f50dc737629f5davandwalle            return a;
10174dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
10184dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1019931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        // Determine which BSSID we want to associate to, taking account
1020c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        // relative strength of 5 and 2.4 GHz BSSIDs
10212451dbcc4f9641df188326215b204b798eb70c46vandwalle        long nowMs = System.currentTimeMillis();
10224dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
102397b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle        for (ScanResult b : current.scanResultCache.values()) {
102497b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            int bRssiBoost5 = 0;
102597b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            int aRssiBoost5 = 0;
102697b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            int bRssiBoost = 0;
102797b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            int aRssiBoost = 0;
1028931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            if ((b.seen == 0) || (b.BSSID == null)
10290d616ef3bf635dff8722e064c0be842676390ed8vandwalle                    || ((nowMs - b.seen) > age)
1030e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    || b.autoJoinStatus != ScanResult.ENABLED
1031e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    || b.numIpConfigFailures > 8) {
10324dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                continue;
10333a2a3d226881cce8a4e511302231d843b0def303vandwalle            }
10344dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1035931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Pick first one
10364dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            if (a == null) {
10374dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                a = b;
10384dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                continue;
10394dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
10404dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1041e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            if (b.numIpConfigFailures < (a.numIpConfigFailures - 1)) {
1042e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                // Prefer a BSSID that doesn't have less number of Ip config failures
1043e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                logDbg("attemptRoam: "
1044e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + b.BSSID + " rssi=" + b.level + " ipfail=" +b.numIpConfigFailures
1045e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " freq=" + b.frequency
1046e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " > "
1047e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + a.BSSID + " rssi=" + a.level + " ipfail=" +a.numIpConfigFailures
1048e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " freq=" + a.frequency);
1049e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                a = b;
1050e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                continue;
1051e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            }
1052e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
10532451dbcc4f9641df188326215b204b798eb70c46vandwalle            // Apply hysteresis: we favor the currentBSSID by giving it a boost
10547806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            if (currentBSSID != null && currentBSSID.equals(b.BSSID)) {
1055931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // Reduce the benefit of hysteresis if RSSI <= -75
10560eebae7334d6129f7ca1344e4b20199794994358vandwalle                if (b.level <= mWifiConfigStore.bandPreferencePenaltyThreshold5) {
10570eebae7334d6129f7ca1344e4b20199794994358vandwalle                    bRssiBoost = mWifiConfigStore.associatedHysteresisLow;
1058c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                } else {
10590eebae7334d6129f7ca1344e4b20199794994358vandwalle                    bRssiBoost = mWifiConfigStore.associatedHysteresisHigh;
1060c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                }
10614dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
10627806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            if (currentBSSID != null && currentBSSID.equals(a.BSSID)) {
10630eebae7334d6129f7ca1344e4b20199794994358vandwalle                if (a.level <= mWifiConfigStore.bandPreferencePenaltyThreshold5) {
1064931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // Reduce the benefit of hysteresis if RSSI <= -75
10650eebae7334d6129f7ca1344e4b20199794994358vandwalle                    aRssiBoost = mWifiConfigStore.associatedHysteresisLow;
1066c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                } else {
10670eebae7334d6129f7ca1344e4b20199794994358vandwalle                    aRssiBoost = mWifiConfigStore.associatedHysteresisHigh;
1068c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                }
10694dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
10702451dbcc4f9641df188326215b204b798eb70c46vandwalle
107197b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            // Favor 5GHz: give a boost to 5GHz BSSIDs, with a slightly progressive curve
10722451dbcc4f9641df188326215b204b798eb70c46vandwalle            //   Boost the BSSID if it is on 5GHz, above a threshold
10732451dbcc4f9641df188326215b204b798eb70c46vandwalle            //   But penalize it if it is on 5GHz and below threshold
107497b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            //
107597b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            //   With he current threshold values, 5GHz network with RSSI above -55
107697b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            //   Are given a boost of 30DB which is enough to overcome the current BSSID
107797b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            //   hysteresis (+14) plus 2.4/5 GHz signal strength difference on most cases
10789f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            //
10799f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            // The "current BSSID" Boost must be added to the BSSID's level so as to introduce\
10809f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            // soem amount of hysteresis
1081e67ec726c07410073575473c0f50dc737629f5davandwalle            if (b.is5GHz()) {
10829f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                bRssiBoost5 = rssiBoostFrom5GHzRssi(b.level + bRssiBoost, b.BSSID);
10834dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
1084e67ec726c07410073575473c0f50dc737629f5davandwalle            if (a.is5GHz()) {
10859f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                aRssiBoost5 = rssiBoostFrom5GHzRssi(a.level + aRssiBoost, a.BSSID);
10864dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
10874dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
10884dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            if (VDBG)  {
1089c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                String comp = " < ";
1090c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                if (b.level + bRssiBoost + bRssiBoost5 > a.level +aRssiBoost + aRssiBoost5) {
1091c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                    comp = " > ";
1092c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                }
10934dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                logDbg("attemptRoam: "
1094c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                        + b.BSSID + " rssi=" + b.level + " boost=" + Integer.toString(bRssiBoost)
109597b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle                        + "/" + Integer.toString(bRssiBoost5) + " freq=" + b.frequency
109697b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle                        + comp
1097c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                        + a.BSSID + " rssi=" + a.level + " boost=" + Integer.toString(aRssiBoost)
1098c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                        + "/" + Integer.toString(aRssiBoost5) + " freq=" + a.frequency);
1099c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle            }
1100c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
11012451dbcc4f9641df188326215b204b798eb70c46vandwalle            // Compare the RSSIs after applying the hysteresis boost and the 5GHz
11022451dbcc4f9641df188326215b204b798eb70c46vandwalle            // boost if applicable
1103c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle            if (b.level + bRssiBoost + bRssiBoost5 > a.level +aRssiBoost + aRssiBoost5) {
1104931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // b is the better BSSID
1105c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                a = b;
11064dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
11074dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
11083a2a3d226881cce8a4e511302231d843b0def303vandwalle        if (a != null) {
11093a2a3d226881cce8a4e511302231d843b0def303vandwalle            if (VDBG)  {
11107806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                StringBuilder sb = new StringBuilder();
1111e67ec726c07410073575473c0f50dc737629f5davandwalle                sb.append("attemptRoam: " + current.configKey() +
1112e67ec726c07410073575473c0f50dc737629f5davandwalle                        " Found " + a.BSSID + " rssi=" + a.level + " freq=" + a.frequency);
11137806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                if (currentBSSID != null) {
11147806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                    sb.append(" Current: " + currentBSSID);
11157806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                }
11167806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                sb.append("\n");
11177806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                logDbg(sb.toString());
11183a2a3d226881cce8a4e511302231d843b0def303vandwalle            }
11194dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
11203a2a3d226881cce8a4e511302231d843b0def303vandwalle        return a;
11214dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle    }
11224dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1123931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
11242451dbcc4f9641df188326215b204b798eb70c46vandwalle     * getNetworkScore()
11252451dbcc4f9641df188326215b204b798eb70c46vandwalle     *
11262451dbcc4f9641df188326215b204b798eb70c46vandwalle     * if scorer is present, get the network score of a WifiConfiguration
11272451dbcc4f9641df188326215b204b798eb70c46vandwalle     *
11282451dbcc4f9641df188326215b204b798eb70c46vandwalle     * Note: this should be merge with setVisibility
11292451dbcc4f9641df188326215b204b798eb70c46vandwalle     *
11302451dbcc4f9641df188326215b204b798eb70c46vandwalle     * @param config
11312451dbcc4f9641df188326215b204b798eb70c46vandwalle     * @return score
11322451dbcc4f9641df188326215b204b798eb70c46vandwalle     */
113381c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson    int getConfigNetworkScore(WifiConfiguration config, int age, boolean isActive) {
11342451dbcc4f9641df188326215b204b798eb70c46vandwalle
11352451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (mNetworkScoreCache == null) {
1136e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            if (VDBG) {
1137b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("       getConfigNetworkScore for " + config.configKey()
1138e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle                        + "  -> no scorer, hence no scores");
1139e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            }
11401db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle            return WifiNetworkScoreCache.INVALID_NETWORK_SCORE;
11411db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        }
11421db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        if (config.scanResultCache == null) {
1143e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            if (VDBG) {
1144b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("       getConfigNetworkScore for " + config.configKey()
1145e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle                        + " -> no scan cache");
1146e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            }
11471db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle            return WifiNetworkScoreCache.INVALID_NETWORK_SCORE;
11482451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
11492451dbcc4f9641df188326215b204b798eb70c46vandwalle
11502451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Get current date
11512451dbcc4f9641df188326215b204b798eb70c46vandwalle        long nowMs = System.currentTimeMillis();
11522451dbcc4f9641df188326215b204b798eb70c46vandwalle
11531db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        int startScore = -10000;
11541db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle
11552451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Run thru all cached scan results
11561db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        for (ScanResult result : config.scanResultCache.values()) {
11572451dbcc4f9641df188326215b204b798eb70c46vandwalle            if ((nowMs - result.seen) < age) {
115881c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                int sc = mNetworkScoreCache.getNetworkScore(result, isActive);
11591db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle                if (sc > startScore) {
11601db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle                    startScore = sc;
11612451dbcc4f9641df188326215b204b798eb70c46vandwalle                }
11622451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
11632451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
11641db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        if (startScore == -10000) {
11651db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle            startScore = WifiNetworkScoreCache.INVALID_NETWORK_SCORE;
11661db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        }
1167e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle        if (VDBG) {
1168e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            if (startScore == WifiNetworkScoreCache.INVALID_NETWORK_SCORE) {
1169b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    getConfigNetworkScore for " + config.configKey()
1170e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle                        + " -> no available score");
1171e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            } else {
1172b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    getConfigNetworkScore for " + config.configKey()
117381c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                        + " isActive=" + isActive
1174e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle                        + " score = " + Integer.toString(startScore));
1175e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            }
1176e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle        }
1177e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle
11781db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        return startScore;
11792451dbcc4f9641df188326215b204b798eb70c46vandwalle    }
11802451dbcc4f9641df188326215b204b798eb70c46vandwalle
11812451dbcc4f9641df188326215b204b798eb70c46vandwalle    /**
11828639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson     * Set whether connections to untrusted connections are allowed.
11838639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson     */
11848639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    void setAllowUntrustedConnections(boolean allow) {
11858639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson        boolean changed = mAllowUntrustedConnections != allow;
11868639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson        mAllowUntrustedConnections = allow;
11878639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson        if (changed) {
1188005c1ef113192f898499a407dd266393a8d6b076vandwalle            // Trigger a scan so as to reattempt autojoin
1189005c1ef113192f898499a407dd266393a8d6b076vandwalle            mWifiStateMachine.startScanForUntrustedSettingChange();
11908639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson        }
11918639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    }
11928639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson
11938639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    private boolean isOpenNetwork(ScanResult result) {
11948639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson        return !result.capabilities.contains("WEP") &&
11958639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                !result.capabilities.contains("PSK") &&
11968639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                !result.capabilities.contains("EAP");
11978639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    }
11988639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson
119916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson    private boolean haveRecentlySeenScoredBssid(WifiConfiguration config) {
120016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        long ephemeralOutOfRangeTimeoutMs = Settings.Global.getLong(
120116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                mContext.getContentResolver(),
120216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                Settings.Global.WIFI_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS,
120316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                DEFAULT_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS);
120416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
120516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        // Check whether the currently selected network has a score curve. If
120616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        // ephemeralOutOfRangeTimeoutMs is <= 0, then this is all we check, and we stop here.
120716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        // Otherwise, we stop here if the currently selected network has a score. If it doesn't, we
120816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        // keep going - it could be that another BSSID is in range (has been seen recently) which
120916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        // has a score, even if the one we're immediately connected to doesn't.
121016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        ScanResult currentScanResult =  mWifiStateMachine.getCurrentScanResult();
121116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        boolean currentNetworkHasScoreCurve = mNetworkScoreCache.hasScoreCurve(currentScanResult);
121216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        if (ephemeralOutOfRangeTimeoutMs <= 0 || currentNetworkHasScoreCurve) {
121316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            if (DBG) {
121416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                if (currentNetworkHasScoreCurve) {
121516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    logDbg("Current network has a score curve, keeping network: "
121616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                            + currentScanResult);
121716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                } else {
121816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    logDbg("Current network has no score curve, giving up: " + config.SSID);
121916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                }
122016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            }
122116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            return currentNetworkHasScoreCurve;
122216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        }
122316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
122416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        if (config.scanResultCache == null || config.scanResultCache.isEmpty()) {
122516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            return false;
122616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        }
122716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
122816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        long currentTimeMs = System.currentTimeMillis();
122916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        for (ScanResult result : config.scanResultCache.values()) {
123016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            if (currentTimeMs > result.seen
123116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    && currentTimeMs - result.seen < ephemeralOutOfRangeTimeoutMs
123216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    && mNetworkScoreCache.hasScoreCurve(result)) {
123316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                if (DBG) {
123416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    logDbg("Found scored BSSID, keeping network: " + result.BSSID);
123516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                }
123616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                return true;
123716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            }
123816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        }
123916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
124016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        if (DBG) {
124116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            logDbg("No recently scored BSSID found, giving up connection: " + config.SSID);
124216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        }
124316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        return false;
124416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson    }
124516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
12468639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    /**
1247931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     * attemptAutoJoin() function implements the core of the a network switching algorithm
124868fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle     * Return false if no acceptable networks were found.
1249931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     */
125068fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle    boolean attemptAutoJoin() {
125168fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle        boolean found = false;
1252c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        didOverride = false;
12531ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle        didBailDueToWeakRssi = false;
1254b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        int networkSwitchType = AUTO_JOIN_IDLE;
12554dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
12568242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle        long now = System.currentTimeMillis();
12578242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle
12588c9088d11880553458f09377cc60d6eb7e66747bvandwalle        String lastSelectedConfiguration = mWifiConfigStore.getLastSelectedConfiguration();
12598c9088d11880553458f09377cc60d6eb7e66747bvandwalle
1260931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        // Reset the currentConfiguration Key, and set it only if WifiStateMachine and
1261453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        // supplicant agree
1262453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        mCurrentConfigurationKey = null;
1263453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        WifiConfiguration currentConfiguration = mWifiStateMachine.getCurrentWifiConfiguration();
1264453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle
1265f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration candidate = null;
1266f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1267931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        // Obtain the subset of recently seen networks
1268833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle        List<WifiConfiguration> list =
1269833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                mWifiConfigStore.getRecentConfiguredNetworks(mScanResultAutoJoinAge, false);
1270f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (list == null) {
12712f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle            if (VDBG)  logDbg("attemptAutoJoin nothing known=" +
12722f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    mWifiConfigStore.getconfiguredNetworkSize());
127368fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle            return false;
1274f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1275f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1276931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        // Find the currently connected network: ask the supplicant directly
127799d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        String val = mWifiNative.status(true);
1278f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String status[] = val.split("\\r?\\n");
1279f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (VDBG) {
1280f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            logDbg("attemptAutoJoin() status=" + val + " split="
1281f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    + Integer.toString(status.length));
1282f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1283f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1284b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        int supplicantNetId = -1;
1285f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        for (String key : status) {
1286f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (key.regionMatches(0, "id=", 0, 3)) {
1287f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                int idx = 3;
1288b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle                supplicantNetId = 0;
1289f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                while (idx < key.length()) {
1290f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    char c = key.charAt(idx);
1291f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1292f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if ((c >= 0x30) && (c <= 0x39)) {
1293b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle                        supplicantNetId *= 10;
1294b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle                        supplicantNetId += c - 0x30;
1295f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        idx++;
1296f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    } else {
1297f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        break;
1298f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
1299f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
130056d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle            } else if (key.contains("wpa_state=ASSOCIATING")
130156d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                    || key.contains("wpa_state=ASSOCIATED")
130256d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                    || key.contains("wpa_state=FOUR_WAY_HANDSHAKE")
130356d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                    || key.contains("wpa_state=GROUP_KEY_HANDSHAKE")) {
130456d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                if (DBG) {
130556d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                    logDbg("attemptAutoJoin: bail out due to sup state " + key);
130656d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                }
130756d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // After WifiStateMachine ask the supplicant to associate or reconnect
130856d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // we might still obtain scan results from supplicant
130956d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // however the supplicant state in the mWifiInfo and supplicant state tracker
131056d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // are updated when we get the supplicant state change message which can be
131156d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // processed after the SCAN_RESULT message, so at this point the framework doesn't
131256d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // know that supplicant is ASSOCIATING.
131356d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // A good fix for this race condition would be for the WifiStateMachine to add
131456d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // a new transient state where it expects to get the supplicant message indicating
131556d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // that it started the association process and within which critical operations
131656d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // like autojoin should be deleted.
131756d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle
131856d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // This transient state would remove the need for the roam Wathchdog which
131956d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // basically does that.
132056d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle
132156d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // At the moment, we just query the supplicant state synchronously with the
132256d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // mWifiNative.status() command, which allow us to know that
132356d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // supplicant has started association process, even though we didnt yet get the
132456d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // SUPPLICANT_STATE_CHANGE message.
132568fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle                return false;
1326f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1327f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1328ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        if (DBG) {
13297806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            String conf = "";
1330b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            String last = "";
13317806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            if (currentConfiguration != null) {
1332e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                conf = " current=" + currentConfiguration.configKey();
1333b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            }
1334b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            if (lastSelectedConfiguration != null) {
13352f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                last = " last=" + lastSelectedConfiguration;
13367806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            }
1337ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            logDbg("attemptAutoJoin() num recent config " + Integer.toString(list.size())
1338b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + conf + last
1339b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + " ---> suppNetId=" + Integer.toString(supplicantNetId));
1340ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
1341f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1342453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        if (currentConfiguration != null) {
13432451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (supplicantNetId != currentConfiguration.networkId
1344005c1ef113192f898499a407dd266393a8d6b076vandwalle                    // https://b.corp.google.com/issue?id=16484607
1345005c1ef113192f898499a407dd266393a8d6b076vandwalle                    // mark this condition as an error only if the mismatched networkId are valid
13462451dbcc4f9641df188326215b204b798eb70c46vandwalle                    && supplicantNetId != WifiConfiguration.INVALID_NETWORK_ID
13472451dbcc4f9641df188326215b204b798eb70c46vandwalle                    && currentConfiguration.networkId != WifiConfiguration.INVALID_NETWORK_ID) {
1348453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                logDbg("attemptAutoJoin() ERROR wpa_supplicant out of sync nid="
1349b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle                        + Integer.toString(supplicantNetId) + " WifiStateMachine="
1350453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + Integer.toString(currentConfiguration.networkId));
1351b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                mWifiStateMachine.disconnectCommand();
135268fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle                return false;
13538639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson            } else if (currentConfiguration.ephemeral && (!mAllowUntrustedConnections ||
135416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    !haveRecentlySeenScoredBssid(currentConfiguration))) {
13558639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                // The current connection is untrusted (the framework added it), but we're either
135616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                // no longer allowed to connect to such networks, the score has been nullified
135716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                // since we connected, or the scored BSSID has gone out of range.
135816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                // Drop the current connection and perform the rest of autojoin.
13598639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                logDbg("attemptAutoJoin() disconnecting from unwanted ephemeral network");
1360f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                mWifiStateMachine.disconnectCommand(Process.WIFI_UID,
1361f57f8918b8c5872ff4bb141fa9e407bec8442e8dvandwalle                        mAllowUntrustedConnections ? 1 : 0);
136268fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle                return false;
1363453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            } else {
1364453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                mCurrentConfigurationKey = currentConfiguration.configKey();
1365453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            }
13668c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle        } else {
13678c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            if (supplicantNetId != WifiConfiguration.INVALID_NETWORK_ID) {
13688c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                // Maybe in the process of associating, skip this attempt
136968fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle                return false;
13708c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            }
1371453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        }
1372453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle
1373b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        int currentNetId = -1;
1374b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        if (currentConfiguration != null) {
1375931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // If we are associated to a configuration, it will
1376b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle            // be compared thru the compareNetwork function
1377b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle            currentNetId = currentConfiguration.networkId;
1378b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        }
1379b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle
1380931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        /**
1381931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         * Run thru all visible configurations without looking at the one we
1382c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle         * are currently associated to
13834dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle         * select Best Network candidate from known WifiConfigurations
1384931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         */
1385f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        for (WifiConfiguration config : list) {
1386e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle            if (config.SSID == null) {
1387b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                continue;
1388e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle            }
1389e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle
139027355a942653264388e909a4276196ee63e57811vandwalle            if (config.autoJoinStatus >=
139127355a942653264388e909a4276196ee63e57811vandwalle                    WifiConfiguration.AUTO_JOIN_DISABLED_ON_AUTH_FAILURE) {
1392abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                // Wait for 5 minutes before reenabling config that have known,
1393abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                // repeated connection or DHCP failures
1394abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                if (config.disableReason == WifiConfiguration.DISABLED_DHCP_FAILURE
1395abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        || config.disableReason
1396abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        == WifiConfiguration.DISABLED_ASSOCIATION_REJECT
1397abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        || config.disableReason
1398abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        == WifiConfiguration.DISABLED_AUTH_FAILURE) {
1399abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                    if (config.blackListTimestamp == 0
1400abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                            || (config.blackListTimestamp > now)) {
1401abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        // Sanitize the timestamp
1402abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        config.blackListTimestamp = now;
1403abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                    }
1404abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                    if ((now - config.blackListTimestamp) >
1405abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                            mWifiConfigStore.wifiConfigBlacklistMinTimeMilli) {
1406abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        // Re-enable the WifiConfiguration
1407abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        config.status = WifiConfiguration.Status.ENABLED;
1408abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle
1409abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        // Reset the blacklist condition
1410abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        config.numConnectionFailures = 0;
1411abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        config.numIpConfigFailures = 0;
1412abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        config.numAuthFailures = 0;
1413abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
1414abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle
1415abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        config.dirty = true;
1416abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                    } else {
1417abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        if (VDBG) {
1418abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                            long delay = mWifiConfigStore.wifiConfigBlacklistMinTimeMilli
1419abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                                    - (now - config.blackListTimestamp);
1420abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                            logDbg("attemptautoJoin " + config.configKey()
1421abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                                    + " dont unblacklist yet, waiting for "
1422abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                                    + delay + " ms");
1423abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        }
1424abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                    }
1425abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                }
1426931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // Avoid networks disabled because of AUTH failure altogether
1427ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                if (DBG) {
1428ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                    logDbg("attemptAutoJoin skip candidate due to auto join status "
1429ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                            + Integer.toString(config.autoJoinStatus) + " key "
1430e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            + config.configKey(true)
14318242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle                            + " reason " + config.disableReason);
1432ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                }
1433f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                continue;
1434f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1435f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1436931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Try to un-blacklist based on elapsed time
143727355a942653264388e909a4276196ee63e57811vandwalle            if (config.blackListTimestamp > 0) {
143827355a942653264388e909a4276196ee63e57811vandwalle                if (now < config.blackListTimestamp) {
1439931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    /**
1440931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                     * looks like there was a change in the system clock since we black listed, and
1441931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                     * timestamp is not meaningful anymore, hence lose it.
1442931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                     * this event should be rare enough so that we still want to lose the black list
14432451dbcc4f9641df188326215b204b798eb70c46vandwalle                     */
144427355a942653264388e909a4276196ee63e57811vandwalle                    config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
144527355a942653264388e909a4276196ee63e57811vandwalle                } else {
14464dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                    if ((now - config.blackListTimestamp) > loseBlackListHardMilli) {
1447931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                        // Reenable it after 18 hours, i.e. next day
144827355a942653264388e909a4276196ee63e57811vandwalle                        config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
14494dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                    } else if ((now - config.blackListTimestamp) > loseBlackListSoftMilli) {
1450931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                        // Lose blacklisting due to bad link
145127355a942653264388e909a4276196ee63e57811vandwalle                        config.setAutoJoinStatus(config.autoJoinStatus - 8);
145227355a942653264388e909a4276196ee63e57811vandwalle                    }
145327355a942653264388e909a4276196ee63e57811vandwalle                }
145427355a942653264388e909a4276196ee63e57811vandwalle            }
145527355a942653264388e909a4276196ee63e57811vandwalle
1456931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Try to unblacklist based on good visibility
14578c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            if (config.visibility.rssi5 < mWifiConfigStore.thresholdUnblacklistThreshold5Soft
14588c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    && config.visibility.rssi24
14598c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    < mWifiConfigStore.thresholdUnblacklistThreshold24Soft) {
146027355a942653264388e909a4276196ee63e57811vandwalle                if (DBG) {
1461c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    logDbg("attemptAutoJoin do not unblacklist due to low visibility "
14624dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + config.autoJoinStatus
14634dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + " key " + config.configKey(true)
14644dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + " rssi=(" + config.visibility.rssi24
14654dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + "," + config.visibility.rssi5
14664dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + ") num=(" + config.visibility.num24
14674dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + "," + config.visibility.num5 + ")");
146827355a942653264388e909a4276196ee63e57811vandwalle                }
14698c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            } else if (config.visibility.rssi5 < mWifiConfigStore.thresholdUnblacklistThreshold5Hard
14708c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    && config.visibility.rssi24
14718c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    < mWifiConfigStore.thresholdUnblacklistThreshold24Hard) {
1472931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // If the network is simply temporary disabled, don't allow reconnect until
1473931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // RSSI becomes good enough
147427355a942653264388e909a4276196ee63e57811vandwalle                config.setAutoJoinStatus(config.autoJoinStatus - 1);
147527355a942653264388e909a4276196ee63e57811vandwalle                if (DBG) {
147627355a942653264388e909a4276196ee63e57811vandwalle                    logDbg("attemptAutoJoin good candidate seen, bumped soft -> status="
147727355a942653264388e909a4276196ee63e57811vandwalle                            + config.autoJoinStatus
1478b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + " " + config.configKey(true) + " rssi=("
14794dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + config.visibility.rssi24 + "," + config.visibility.rssi5
14804dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + ") num=(" + config.visibility.num24
14814dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + "," + config.visibility.num5 + ")");
148227355a942653264388e909a4276196ee63e57811vandwalle                }
148327355a942653264388e909a4276196ee63e57811vandwalle            } else {
1484c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                config.setAutoJoinStatus(config.autoJoinStatus - 3);
148527355a942653264388e909a4276196ee63e57811vandwalle                if (DBG) {
148627355a942653264388e909a4276196ee63e57811vandwalle                    logDbg("attemptAutoJoin good candidate seen, bumped hard -> status="
148727355a942653264388e909a4276196ee63e57811vandwalle                            + config.autoJoinStatus
1488b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + " " + config.configKey(true) + " rssi=("
14894dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + config.visibility.rssi24 + "," + config.visibility.rssi5
14904dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + ") num=(" + config.visibility.num24
14914dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + "," + config.visibility.num5 + ")");
149227355a942653264388e909a4276196ee63e57811vandwalle                }
149327355a942653264388e909a4276196ee63e57811vandwalle            }
149427355a942653264388e909a4276196ee63e57811vandwalle
149527355a942653264388e909a4276196ee63e57811vandwalle            if (config.autoJoinStatus >=
149627355a942653264388e909a4276196ee63e57811vandwalle                    WifiConfiguration.AUTO_JOIN_TEMPORARY_DISABLED) {
1497931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // Network is blacklisted, skip
14984dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                if (DBG) {
14994dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                    logDbg("attemptAutoJoin skip blacklisted -> status="
15004dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + config.autoJoinStatus
1501b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + " " + config.configKey(true) + " rssi=("
15024dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + config.visibility.rssi24 + "," + config.visibility.rssi5
15034dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + ") num=(" + config.visibility.num24
15044dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + "," + config.visibility.num5 + ")");
15054dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                }
150627355a942653264388e909a4276196ee63e57811vandwalle                continue;
150727355a942653264388e909a4276196ee63e57811vandwalle            }
1508f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (config.networkId == currentNetId) {
1509ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                if (DBG) {
151021bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle                    logDbg("attemptAutoJoin skip current candidate  "
151121bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle                            + Integer.toString(currentNetId)
1512ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                            + " key " + config.configKey(true));
1513ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                }
1514f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                continue;
1515f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1516f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
15170eebae7334d6129f7ca1344e4b20199794994358vandwalle            boolean isLastSelected = false;
15180eebae7334d6129f7ca1344e4b20199794994358vandwalle            if (lastSelectedConfiguration != null &&
15190eebae7334d6129f7ca1344e4b20199794994358vandwalle                    config.configKey().equals(lastSelectedConfiguration)) {
15200eebae7334d6129f7ca1344e4b20199794994358vandwalle                isLastSelected = true;
15210eebae7334d6129f7ca1344e4b20199794994358vandwalle            }
15220eebae7334d6129f7ca1344e4b20199794994358vandwalle
15230eebae7334d6129f7ca1344e4b20199794994358vandwalle            if (config.visibility == null) {
15240eebae7334d6129f7ca1344e4b20199794994358vandwalle                continue;
15250eebae7334d6129f7ca1344e4b20199794994358vandwalle            }
15261ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            int boost = config.autoJoinUseAggressiveJoinAttemptThreshold + weakRssiBailCount;
15270eebae7334d6129f7ca1344e4b20199794994358vandwalle            if ((config.visibility.rssi5 + boost)
15280eebae7334d6129f7ca1344e4b20199794994358vandwalle                        < mWifiConfigStore.thresholdInitialAutoJoinAttemptMin5RSSI
15290eebae7334d6129f7ca1344e4b20199794994358vandwalle                        && (config.visibility.rssi24 + boost)
15300eebae7334d6129f7ca1344e4b20199794994358vandwalle                        < mWifiConfigStore.thresholdInitialAutoJoinAttemptMin24RSSI) {
15310eebae7334d6129f7ca1344e4b20199794994358vandwalle                if (DBG) {
15320eebae7334d6129f7ca1344e4b20199794994358vandwalle                    logDbg("attemptAutoJoin skip due to low visibility -> status="
15330eebae7334d6129f7ca1344e4b20199794994358vandwalle                            + config.autoJoinStatus
15340eebae7334d6129f7ca1344e4b20199794994358vandwalle                            + " key " + config.configKey(true) + " rssi="
15350eebae7334d6129f7ca1344e4b20199794994358vandwalle                            + config.visibility.rssi24 + ", " + config.visibility.rssi5
15360eebae7334d6129f7ca1344e4b20199794994358vandwalle                            + " num=" + config.visibility.num24
15370eebae7334d6129f7ca1344e4b20199794994358vandwalle                            + ", " + config.visibility.num5);
15380eebae7334d6129f7ca1344e4b20199794994358vandwalle                }
15390eebae7334d6129f7ca1344e4b20199794994358vandwalle
1540c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                // Don't try to autojoin a network that is too far but
1541c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                // If that configuration is a user's choice however, try anyway
15420eebae7334d6129f7ca1344e4b20199794994358vandwalle                if (!isLastSelected) {
15430eebae7334d6129f7ca1344e4b20199794994358vandwalle                    config.autoJoinBailedDueToLowRssi = true;
15441ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                    didBailDueToWeakRssi = true;
15458c9088d11880553458f09377cc60d6eb7e66747bvandwalle                    continue;
15460eebae7334d6129f7ca1344e4b20199794994358vandwalle                } else {
15470eebae7334d6129f7ca1344e4b20199794994358vandwalle                    // Next time, try to be a bit more aggressive in auto-joining
15480eebae7334d6129f7ca1344e4b20199794994358vandwalle                    if (config.autoJoinUseAggressiveJoinAttemptThreshold
1549dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                            < WifiConfiguration.MAX_INITIAL_AUTO_JOIN_RSSI_BOOST
1550dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                            && config.autoJoinBailedDueToLowRssi) {
1551dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                        config.autoJoinUseAggressiveJoinAttemptThreshold += 4;
15524dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                    }
15538c9088d11880553458f09377cc60d6eb7e66747bvandwalle                }
15540eebae7334d6129f7ca1344e4b20199794994358vandwalle            }
1555d30127db46224e45554f8964209221bba8ad41d9vandwalle            if (config.numNoInternetAccessReports > 0
1556d30127db46224e45554f8964209221bba8ad41d9vandwalle                    && !isLastSelected
1557d30127db46224e45554f8964209221bba8ad41d9vandwalle                    && !config.validatedInternetAccess) {
1558d30127db46224e45554f8964209221bba8ad41d9vandwalle                // Avoid autoJoining this network because last time we used it, it didn't
1559d30127db46224e45554f8964209221bba8ad41d9vandwalle                // have internet access, and we never manage to validate internet access on this
1560d30127db46224e45554f8964209221bba8ad41d9vandwalle                // network configuration
15610eebae7334d6129f7ca1344e4b20199794994358vandwalle                if (DBG) {
1562d30127db46224e45554f8964209221bba8ad41d9vandwalle                    logDbg("attemptAutoJoin skip candidate due to no InternetAccess  "
1563d30127db46224e45554f8964209221bba8ad41d9vandwalle                            + config.configKey(true)
1564d30127db46224e45554f8964209221bba8ad41d9vandwalle                            + " num reports " + config.numNoInternetAccessReports);
15659f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                }
15660eebae7334d6129f7ca1344e4b20199794994358vandwalle                continue;
15678c9088d11880553458f09377cc60d6eb7e66747bvandwalle            }
15688c9088d11880553458f09377cc60d6eb7e66747bvandwalle
1569ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            if (DBG) {
1570e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                String cur = "";
1571e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                if (candidate != null) {
1572e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    cur = " current candidate " + candidate.configKey();
1573e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                }
1574e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                logDbg("attemptAutoJoin trying id="
1575c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                        + Integer.toString(config.networkId) + " "
1576b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                        + config.configKey(true)
1577e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " status=" + config.autoJoinStatus
1578e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + cur);
1579ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            }
1580f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1581f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (candidate == null) {
1582f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                candidate = config;
1583f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else {
1584f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (VDBG)  {
1585453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    logDbg("attemptAutoJoin will compare candidate  " + candidate.configKey()
15864dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + " with " + config.configKey());
1587f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
1588833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                int order = compareWifiConfigurations(candidate, config);
15892451dbcc4f9641df188326215b204b798eb70c46vandwalle
15902451dbcc4f9641df188326215b204b798eb70c46vandwalle                // The lastSelectedConfiguration is the configuration the user has manually selected
1591c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                // thru WifiPicker, or that a 3rd party app asked us to connect to via the
15922451dbcc4f9641df188326215b204b798eb70c46vandwalle                // enableNetwork with disableOthers=true WifiManager API
15932451dbcc4f9641df188326215b204b798eb70c46vandwalle                // As this is a direct user choice, we strongly prefer this configuration,
15942451dbcc4f9641df188326215b204b798eb70c46vandwalle                // hence give +/-100
15952451dbcc4f9641df188326215b204b798eb70c46vandwalle                if ((lastSelectedConfiguration != null)
15962451dbcc4f9641df188326215b204b798eb70c46vandwalle                        && candidate.configKey().equals(lastSelectedConfiguration)) {
15972451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // candidate is the last selected configuration,
15982451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // so keep it above connect choices (+/-60) and
15992451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // above RSSI/scorer based selection of linked configuration (+/- 50)
16002451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // by reducing order by -100
16012451dbcc4f9641df188326215b204b798eb70c46vandwalle                    order = order - 100;
16022451dbcc4f9641df188326215b204b798eb70c46vandwalle                    if (VDBG)   {
16032f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        logDbg("     ...and prefers -100 " + candidate.configKey()
16042451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + " over " + config.configKey()
16052451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + " because it is the last selected -> "
16062451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + Integer.toString(order));
16072451dbcc4f9641df188326215b204b798eb70c46vandwalle                    }
16082451dbcc4f9641df188326215b204b798eb70c46vandwalle                } else if ((lastSelectedConfiguration != null)
16092451dbcc4f9641df188326215b204b798eb70c46vandwalle                        && config.configKey().equals(lastSelectedConfiguration)) {
16102451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // config is the last selected configuration,
16112451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // so keep it above connect choices (+/-60) and
16122451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // above RSSI/scorer based selection of linked configuration (+/- 50)
16132451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // by increasing order by +100
16142451dbcc4f9641df188326215b204b798eb70c46vandwalle                    order = order + 100;
16152451dbcc4f9641df188326215b204b798eb70c46vandwalle                    if (VDBG)   {
16162f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        logDbg("     ...and prefers +100 " + config.configKey()
16172451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + " over " + candidate.configKey()
16182451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + " because it is the last selected -> "
16192451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + Integer.toString(order));
16202451dbcc4f9641df188326215b204b798eb70c46vandwalle                    }
16212451dbcc4f9641df188326215b204b798eb70c46vandwalle                }
16222451dbcc4f9641df188326215b204b798eb70c46vandwalle
1623f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (order > 0) {
1624931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // Ascending : candidate < config
1625f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    candidate = config;
1626f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
1627f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1628f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1629f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
16302451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Now, go thru scan result to try finding a better untrusted network
1631833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle        if (mNetworkScoreCache != null && mAllowUntrustedConnections) {
1632f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            int rssi5 = WifiConfiguration.INVALID_RSSI;
1633f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            int rssi24 = WifiConfiguration.INVALID_RSSI;
1634f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (candidate != null) {
1635f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                rssi5 = candidate.visibility.rssi5;
1636f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                rssi24 = candidate.visibility.rssi24;
1637f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1638f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1639931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Get current date
16402451dbcc4f9641df188326215b204b798eb70c46vandwalle            long nowMs = System.currentTimeMillis();
1641c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            int currentScore = -10000;
1642c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            // The untrusted network with highest score
1643c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            ScanResult untrustedCandidate = null;
16442451dbcc4f9641df188326215b204b798eb70c46vandwalle            // Look for untrusted scored network only if the current candidate is bad
1645c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            if (isBadCandidate(rssi24, rssi5)) {
1646f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                for (ScanResult result : scanResultCache.values()) {
1647c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    // We look only at untrusted networks with a valid SSID
1648c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    // A trusted result would have been looked at thru it's Wificonfiguration
16498639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                    if (TextUtils.isEmpty(result.SSID) || !result.untrusted ||
16508639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                            !isOpenNetwork(result)) {
1651c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        continue;
1652c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    }
16532ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle                    if (mWifiConfigStore.mDeletedEphemeralSSIDs.contains
16542ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle                            ("\"" + result.SSID + "\"")) {
16552ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle                        // SSID had been Forgotten by user, then don't score it
16562ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle                        continue;
16572ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle                    }
1658833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                    if ((nowMs - result.seen) < mScanResultAutoJoinAge) {
1659c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        // Increment usage count for the network
1660c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        mWifiConnectionStatistics.incrementOrAddUntrusted(result.SSID, 0, 1);
1661c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
166281c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                        boolean isActiveNetwork = lastUntrustedBSSID != null
166381c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                                && result.BSSID.equals(lastUntrustedBSSID);
166481c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                        int score = mNetworkScoreCache.getNetworkScore(result, isActiveNetwork);
1665c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        if (score != WifiNetworkScoreCache.INVALID_NETWORK_SCORE
1666c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                                && score > currentScore) {
1667c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            // Highest score: Select this candidate
1668c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            currentScore = score;
1669c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            untrustedCandidate = result;
1670c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            if (VDBG) {
1671c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                                logDbg("AutoJoinController: found untrusted candidate "
1672c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                                        + result.SSID
1673c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                                + " RSSI=" + result.level
1674c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                                + " freq=" + result.frequency
1675c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                                + " score=" + score);
1676c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            }
1677f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
1678f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
1679f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
1680f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1681c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            if (untrustedCandidate != null) {
1682c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                if (lastUntrustedBSSID == null
1683c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        || !untrustedCandidate.SSID.equals(lastUntrustedBSSID)) {
1684c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    // We found a new candidate that we are going to connect to, then
1685c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    // increase its connection count
16868c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    mWifiConnectionStatistics.
16878c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            incrementOrAddUntrusted(untrustedCandidate.SSID, 1, 0);
1688c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    // Remember which SSID we are connecting to
1689c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    lastUntrustedBSSID = untrustedCandidate.SSID;
1690c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                }
16918639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson
16928639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                // At this point, we have an untrusted network candidate.
16938639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                // Create the new ephemeral configuration and see if we should switch over
1694833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                candidate =
1695833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                        mWifiConfigStore.wifiConfigurationFromScanResult(untrustedCandidate);
1696833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                candidate.allowedKeyManagement.set(KeyMgmt.NONE);
1697833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                candidate.ephemeral = true;
1698c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            }
1699c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        }
1700b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle
17011ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle        long lastUnwanted =
17021ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                System.currentTimeMillis()
17031ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                        - mWifiConfigStore.lastUnwantedNetworkDisconnectTimestamp;
17041ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle        if (candidate == null
17051ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                && lastSelectedConfiguration == null
17061ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                && currentConfiguration == null
17071ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                && didBailDueToWeakRssi
17081ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                && (mWifiConfigStore.lastUnwantedNetworkDisconnectTimestamp == 0
17091ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                    || lastUnwanted > (1000 * 60 * 60 * 24 * 7))
17101ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                ) {
17111ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // We are bailing out of autojoin although we are seeing a weak configuration, and
17121ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // - we didn't find another valid candidate
17131ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // - we are not connected
17141ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // - without a user network selection choice
17151ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // - ConnectivityService has not triggered an unwanted network disconnect
17161ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            //       on this device for a week (hence most likely there is no SIM card or cellular)
17171ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // If all those conditions are met, then boost the RSSI of the weak networks
17181ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // that we are seeing so as we will eventually pick one
17191ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            if (weakRssiBailCount < 10)
17201ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                weakRssiBailCount += 1;
17211ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle        } else {
17221ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            if (weakRssiBailCount > 0)
17231ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                weakRssiBailCount -= 1;
17241ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle        }
17251ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle
1726931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        /**
1727931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         *  If candidate is found, check the state of the connection so as
1728931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         *  to decide if we should be acting on this candidate and switching over
1729931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         */
1730e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        int networkDelta = compareNetwork(candidate, lastSelectedConfiguration);
1731b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        if (DBG && candidate != null) {
1732b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            String doSwitch = "";
1733b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            String current = "";
1734b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            if (networkDelta < 0) {
1735b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                doSwitch = " -> not switching";
1736b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            }
1737b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            if (currentConfiguration != null) {
17382f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                current = " with current " + currentConfiguration.configKey();
1739b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            }
1740b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("attemptAutoJoin networkSwitching candidate "
1741b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle                    + candidate.configKey()
1742b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + current
1743c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                    + " linked=" + (currentConfiguration != null
1744b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            && currentConfiguration.isLinked(candidate))
1745b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + " : delta="
1746b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + Integer.toString(networkDelta) + " "
1747b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + doSwitch);
1748b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        }
17494dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1750931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        /**
1751931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         * Ask WifiStateMachine permission to switch :
1752931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         * if user is currently streaming voice traffic,
1753931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         * then we should not be allowed to switch regardless of the delta
1754931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         */
1755b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        if (mWifiStateMachine.shouldSwitchNetwork(networkDelta)) {
1756b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            if (mStaStaSupported) {
1757b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                logDbg("mStaStaSupported --> error do nothing now ");
1758b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            } else {
1759b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                if (currentConfiguration != null && currentConfiguration.isLinked(candidate)) {
1760b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    networkSwitchType = AUTO_JOIN_EXTENDED_ROAMING;
1761b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                } else {
1762b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    networkSwitchType = AUTO_JOIN_OUT_OF_NETWORK_ROAMING;
1763b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                }
1764b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                if (DBG) {
1765b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    logDbg("AutoJoin auto connect with netId "
1766b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + Integer.toString(candidate.networkId)
1767b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + " to " + candidate.configKey());
1768b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                }
17692451dbcc4f9641df188326215b204b798eb70c46vandwalle                if (didOverride) {
17702451dbcc4f9641df188326215b204b798eb70c46vandwalle                    candidate.numScorerOverrideAndSwitchedNetwork++;
17712451dbcc4f9641df188326215b204b798eb70c46vandwalle                }
1772c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                candidate.numAssociation++;
1773e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                mWifiConnectionStatistics.numAutoJoinAttempt++;
17749f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
17752f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                if (candidate.BSSID == null || candidate.BSSID.equals("any")) {
17762f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    // First step we selected the configuration we want to connect to
17772f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    // Second step: Look for the best Scan result for this configuration
17782f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    // TODO this algorithm should really be done in one step
17792f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    String currentBSSID = mWifiStateMachine.getCurrentBSSID();
1780833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                    ScanResult roamCandidate =
1781833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                            attemptRoam(null, candidate, mScanResultAutoJoinAge, null);
17822f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    if (roamCandidate != null && currentBSSID != null
17832f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                            && currentBSSID.equals(roamCandidate.BSSID)) {
17842f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        // Sanity, we were already asociated to that candidate
17852f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        roamCandidate = null;
17862f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    }
17872f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    if (roamCandidate != null && roamCandidate.is5GHz()) {
17882f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        // If the configuration hasn't a default BSSID selected, and the best
17892f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        // candidate is 5GHZ, then select this candidate so as WifiStateMachine and
17902f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        // supplicant will pick it first
17912f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        candidate.autoJoinBSSID = roamCandidate.BSSID;
17922f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        if (VDBG) {
17932f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                            logDbg("AutoJoinController: lock to 5GHz "
17942f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                                    + candidate.autoJoinBSSID
17952f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                                    + " RSSI=" + roamCandidate.level
17962f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                                    + " freq=" + roamCandidate.frequency);
17972f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        }
1798448c9536a302c58a79e271b1721c08b8882f800evandwalle                    } else {
1799448c9536a302c58a79e271b1721c08b8882f800evandwalle                        // We couldnt find a roam candidate
1800448c9536a302c58a79e271b1721c08b8882f800evandwalle                        candidate.autoJoinBSSID = "any";
18019f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    }
18029f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                }
1803448c9536a302c58a79e271b1721c08b8882f800evandwalle                mWifiStateMachine.sendMessage(WifiStateMachine.CMD_AUTO_CONNECT,
1804448c9536a302c58a79e271b1721c08b8882f800evandwalle                            candidate.networkId, networkSwitchType, candidate);
180568fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle                found = true;
18064dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
1807b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        }
18084dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1809b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        if (networkSwitchType == AUTO_JOIN_IDLE) {
1810e67ec726c07410073575473c0f50dc737629f5davandwalle            String currentBSSID = mWifiStateMachine.getCurrentBSSID();
1811931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Attempt same WifiConfiguration roaming
1812833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle            ScanResult roamCandidate =
1813833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                    attemptRoam(null, currentConfiguration, mScanResultAutoJoinAge, currentBSSID);
1814e67ec726c07410073575473c0f50dc737629f5davandwalle            /**
1815e67ec726c07410073575473c0f50dc737629f5davandwalle             *  TODO: (post L initial release)
1816e67ec726c07410073575473c0f50dc737629f5davandwalle             *  consider handling linked configurations roaming (i.e. extended Roaming)
1817e67ec726c07410073575473c0f50dc737629f5davandwalle             *  thru the attemptRoam function which makes use of the RSSI roaming threshold.
1818e67ec726c07410073575473c0f50dc737629f5davandwalle             *  At the moment, extended roaming is only handled thru the attemptAutoJoin()
1819e67ec726c07410073575473c0f50dc737629f5davandwalle             *  function which compare configurations.
1820e67ec726c07410073575473c0f50dc737629f5davandwalle             *
1821e67ec726c07410073575473c0f50dc737629f5davandwalle             *  The advantage of making use of attemptRoam function is that this function
1822e67ec726c07410073575473c0f50dc737629f5davandwalle             *  will looks at all the BSSID of each configurations, instead of only looking
1823e67ec726c07410073575473c0f50dc737629f5davandwalle             *  at WifiConfiguration.visibility which keeps trackonly of the RSSI/band of the
1824e67ec726c07410073575473c0f50dc737629f5davandwalle             *  two highest BSSIDs.
1825e67ec726c07410073575473c0f50dc737629f5davandwalle             */
1826e67ec726c07410073575473c0f50dc737629f5davandwalle            // Attempt linked WifiConfiguration roaming
1827e67ec726c07410073575473c0f50dc737629f5davandwalle            /* if (currentConfiguration != null
1828e67ec726c07410073575473c0f50dc737629f5davandwalle                    && currentConfiguration.linkedConfigurations != null) {
1829e67ec726c07410073575473c0f50dc737629f5davandwalle                for (String key : currentConfiguration.linkedConfigurations.keySet()) {
1830e67ec726c07410073575473c0f50dc737629f5davandwalle                    WifiConfiguration link = mWifiConfigStore.getWifiConfiguration(key);
1831e67ec726c07410073575473c0f50dc737629f5davandwalle                    if (link != null) {
1832833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                        roamCandidate = attemptRoam(roamCandidate, link, mScanResultAutoJoinAge,
1833e67ec726c07410073575473c0f50dc737629f5davandwalle                                currentBSSID);
1834e67ec726c07410073575473c0f50dc737629f5davandwalle                    }
1835e67ec726c07410073575473c0f50dc737629f5davandwalle                }
1836e67ec726c07410073575473c0f50dc737629f5davandwalle            }*/
1837e67ec726c07410073575473c0f50dc737629f5davandwalle            if (roamCandidate != null && currentBSSID != null
1838e67ec726c07410073575473c0f50dc737629f5davandwalle                    && currentBSSID.equals(roamCandidate.BSSID)) {
1839e67ec726c07410073575473c0f50dc737629f5davandwalle                roamCandidate = null;
1840e67ec726c07410073575473c0f50dc737629f5davandwalle            }
18412f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle            if (roamCandidate != null && mWifiStateMachine.shouldSwitchNetwork(999)) {
1842b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                if (DBG) {
1843b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    logDbg("AutoJoin auto roam with netId "
1844b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + Integer.toString(currentConfiguration.networkId)
1845b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + " " + currentConfiguration.configKey() + " to BSSID="
1846b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + roamCandidate.BSSID + " freq=" + roamCandidate.frequency
18471ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                            + " RSSI=" + roamCandidate.level);
1848b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                }
1849b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                networkSwitchType = AUTO_JOIN_ROAMING;
1850e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                mWifiConnectionStatistics.numAutoRoamAttempt++;
1851e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle
1852b07da189850a4bfa268f8ab9be7867935eb2ecb5vandwalle                mWifiStateMachine.sendMessage(WifiStateMachine.CMD_AUTO_ROAM,
1853b07da189850a4bfa268f8ab9be7867935eb2ecb5vandwalle                            currentConfiguration.networkId, 1, roamCandidate);
185468fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle                found = true;
1855f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1856f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1857b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        if (VDBG) logDbg("Done attemptAutoJoin status=" + Integer.toString(networkSwitchType));
185868fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle        return found;
1859f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
1860f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle}
1861f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1862