WifiAutoJoinController.java revision 16fdf07021858fd116d96a5fb00ddb3c166d5ae6
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;
27c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalleimport android.text.TextUtils;
28f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport android.util.Log;
29f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
300c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalleimport java.util.ArrayList;
31f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.Iterator;
32f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.HashMap;
33f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalleimport java.util.List;
34f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
35f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle/**
36f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * AutoJoin controller is responsible for WiFi Connect decision
37f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle *
38f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle * It runs in the thread context of WifiStateMachine
39f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle *
40f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle */
41f22d23092ab37286a5ef9d257d5bb32c421d2669vandwallepublic class WifiAutoJoinController {
42f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
43f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private Context mContext;
44f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private WifiStateMachine mWifiStateMachine;
45f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private WifiConfigStore mWifiConfigStore;
46f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private WifiNative mWifiNative;
47f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
48f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private NetworkScoreManager scoreManager;
49f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private WifiNetworkScoreCache mNetworkScoreCache;
50f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
51f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final String TAG = "WifiAutoJoinController ";
52ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    private static boolean DBG = false;
53ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    private static boolean VDBG = false;
54f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private static final boolean mStaStaSupported = false;
55f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
56c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    public static int mScanResultMaximumAge = 40000; /* milliseconds unit */
57833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle    public static int mScanResultAutoJoinAge = 5000; /* milliseconds unit */
58c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
59453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle    private String mCurrentConfigurationKey = null; //used by autojoin
60f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
61f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private HashMap<String, ScanResult> scanResultCache =
62f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            new HashMap<String, ScanResult>();
63f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
64c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    private WifiConnectionStatistics mWifiConnectionStatistics;
65c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
668639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    /** Whether to allow connections to untrusted networks. */
678639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    private boolean mAllowUntrustedConnections = false;
688639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson
69c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    /* for debug purpose only : the untrusted SSID we would be connected to if we had VPN */
70c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    String lastUntrustedBSSID = null;
71c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
72c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    /* For debug purpose only: if the scored override a score */
73c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    boolean didOverride = false;
74c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
75931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    // Lose the non-auth failure blacklisting after 8 hours
764dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle    private final static long loseBlackListHardMilli = 1000 * 60 * 60 * 8;
77931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    // Lose some temporary blacklisting after 30 minutes
784dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle    private final static long loseBlackListSoftMilli = 1000 * 60 * 30;
7927355a942653264388e909a4276196ee63e57811vandwalle
8016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson    /** @see android.provider.Settings.Global#WIFI_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS */
8116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson    private static final long DEFAULT_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS = 1000 * 60; // 1 minute
8216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
83b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle    public static final int AUTO_JOIN_IDLE = 0;
84b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle    public static final int AUTO_JOIN_ROAMING = 1;
85b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle    public static final int AUTO_JOIN_EXTENDED_ROAMING = 2;
86b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle    public static final int AUTO_JOIN_OUT_OF_NETWORK_ROAMING = 3;
87b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle
8897b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle    public static final int HIGH_THRESHOLD_MODIFIER = 5;
8997b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle
901ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    // Below are AutoJoin wide parameters indicating if we should be aggressive before joining
911ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    // weak network. Note that we cannot join weak network that are going to be marked as unanted by
921ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    // ConnectivityService because this will trigger link flapping.
931ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    /**
941ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle     * There was a non-blacklisted configuration that we bailed from because of a weak signal
951ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle     */
961ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    boolean didBailDueToWeakRssi = false;
971ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    /**
981ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle     * number of time we consecutively bailed out of an eligible network because its signal
991ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle     * was too weak
1001ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle     */
1011ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle    int weakRssiBailCount = 0;
1021ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle
103f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    WifiAutoJoinController(Context c, WifiStateMachine w, WifiConfigStore s,
104c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                           WifiConnectionStatistics st, WifiNative n) {
105f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mContext = c;
106f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mWifiStateMachine = w;
107f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mWifiConfigStore = s;
108f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mWifiNative = n;
109f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        mNetworkScoreCache = null;
110c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        mWifiConnectionStatistics = st;
11121bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle        scoreManager =
11221bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle                (NetworkScoreManager) mContext.getSystemService(Context.NETWORK_SCORE_SERVICE);
113f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (scoreManager == null)
114f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            logDbg("Registered scoreManager NULL " + " service " + Context.NETWORK_SCORE_SERVICE);
115f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
116f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (scoreManager != null) {
117f13817203179f41620514718c8668ae7e418f8afJeff Davidson            mNetworkScoreCache = new WifiNetworkScoreCache(mContext);
118f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            scoreManager.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, mNetworkScoreCache);
119f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        } else {
120f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            logDbg("No network score service: Couldnt register as a WiFi score Manager, type="
121f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    + Integer.toString(NetworkKey.TYPE_WIFI)
122f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    + " service " + Context.NETWORK_SCORE_SERVICE);
123f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            mNetworkScoreCache = null;
124f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
125f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
126f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
127ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    void enableVerboseLogging(int verbose) {
128ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        if (verbose > 0 ) {
129abde872adced15dfb6781fb71959453d963326dbYuhao Zheng            DBG = true;
130ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            VDBG = true;
131ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        } else {
132abde872adced15dfb6781fb71959453d963326dbYuhao Zheng            DBG = false;
133ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            VDBG = false;
134ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
135ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle    }
136ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle
137931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
138931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     * Flush out scan results older than mScanResultMaximumAge
139ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     *
140931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     */
141f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    private void ageScanResultsOut(int delay) {
142f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (delay <= 0) {
143931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            delay = mScanResultMaximumAge; // Something sane
144f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
145b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        long milli = System.currentTimeMillis();
146f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (VDBG) {
147f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            logDbg("ageScanResultsOut delay " + Integer.valueOf(delay) + " size "
148f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    + Integer.valueOf(scanResultCache.size()) + " now " + Long.valueOf(milli));
149f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
150f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
151f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        Iterator<HashMap.Entry<String,ScanResult>> iter = scanResultCache.entrySet().iterator();
152f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        while (iter.hasNext()) {
153f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            HashMap.Entry<String,ScanResult> entry = iter.next();
154f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            ScanResult result = entry.getValue();
155f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
156f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if ((result.seen + delay) < milli) {
157f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                iter.remove();
158f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
159f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
160f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
161f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
162be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle    int addToScanCache(List<ScanResult> scanList) {
163be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        int numScanResultsKnown = 0; // Record number of scan results we knew about
164be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        WifiConfiguration associatedConfig = null;
1657b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle        boolean didAssociate = false;
1668242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle        long now = System.currentTimeMillis();
167f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1680c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle        ArrayList<NetworkKey> unknownScanResults = new ArrayList<NetworkKey>();
1690c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle
170f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        for(ScanResult result: scanList) {
1711fcf3c6d2b9ed65573e1e7c55fc5a30ebd364c4fYuhao Zheng            if (result.SSID == null) continue;
172c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
173c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            // Make sure we record the last time we saw this result
174f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            result.seen = System.currentTimeMillis();
175f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
176c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            // Fetch the previous instance for this result
177f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            ScanResult sr = scanResultCache.get(result.BSSID);
178f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (sr != null) {
1798ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                if (mWifiConfigStore.scanResultRssiLevelPatchUp != 0
1808ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                        && result.level == 0
1818ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                        && sr.level < -20) {
1828ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                    // A 'zero' RSSI reading is most likely a chip problem which returns
1838ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                    // an unknown RSSI, hence ignore it
1848ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                    result.level = sr.level;
1858ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                }
1868ccabb81ad304b80dc8eaa162fd322643461529bvandwalle
187931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // If there was a previous cache result for this BSSID, average the RSSI values
188c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                result.averageRssi(sr.level, sr.seen, mScanResultMaximumAge);
189f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
190c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                // Remove the previous Scan Result - this is not necessary
191f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                scanResultCache.remove(result.BSSID);
1928ccabb81ad304b80dc8eaa162fd322643461529bvandwalle            } else if (mWifiConfigStore.scanResultRssiLevelPatchUp != 0 && result.level == 0) {
1938ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                // A 'zero' RSSI reading is most likely a chip problem which returns
1948ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                // an unknown RSSI, hence initialize it to a sane value
1958ccabb81ad304b80dc8eaa162fd322643461529bvandwalle                result.level = mWifiConfigStore.scanResultRssiLevelPatchUp;
196e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle            }
197e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle
198e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle            if (!mNetworkScoreCache.isScoredNetwork(result)) {
199e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                WifiKey wkey;
200e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                // Quoted SSIDs are the only one valid at this stage
201e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                try {
202e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    wkey = new WifiKey("\"" + result.SSID + "\"", result.BSSID);
203e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                } catch (IllegalArgumentException e) {
204e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    logDbg("AutoJoinController: received badly encoded SSID=[" + result.SSID +
205e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                            "] ->skipping this network");
206e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    wkey = null;
207e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                }
208e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                if (wkey != null) {
209e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    NetworkKey nkey = new NetworkKey(wkey);
210e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    //if we don't know this scan result then request a score from the scorer
211e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    unknownScanResults.add(nkey);
212e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                }
213e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                if (VDBG) {
2147b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    String cap = "";
2157b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    if (result.capabilities != null)
2167b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        cap = result.capabilities;
217e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    logDbg(result.SSID + " " + result.BSSID + " rssi="
2187b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                            + result.level + " cap " + cap + " is not scored");
219e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                }
2200c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle            } else {
221e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                if (VDBG) {
2227b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    String cap = "";
2237b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    if (result.capabilities != null)
2247b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                        cap = result.capabilities;
225e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    int score = mNetworkScoreCache.getNetworkScore(result);
226e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    logDbg(result.SSID + " " + result.BSSID + " rssi="
2277b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                            + result.level + " cap " + cap + " is scored : " + score);
2280c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                }
229f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
230f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
231e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // scanResultCache.put(result.BSSID, new ScanResult(result));
232e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            scanResultCache.put(result.BSSID, result);
233be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            // Add this BSSID to the scanResultCache of a Saved WifiConfiguration
2347b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            didAssociate = mWifiConfigStore.updateSavedNetworkHistory(result);
235f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
236be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            // If not successful, try to associate this BSSID to an existing Saved WifiConfiguration
2377b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            if (!didAssociate) {
238be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                // We couldn't associate the scan result to a Saved WifiConfiguration
239c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                // Hence it is untrusted
240c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                result.untrusted = true;
241f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                associatedConfig = mWifiConfigStore.associateWithConfiguration(result);
2421fcf3c6d2b9ed65573e1e7c55fc5a30ebd364c4fYuhao Zheng                if (associatedConfig != null && associatedConfig.SSID != null) {
243f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (VDBG) {
244f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        logDbg("addToScanCache save associated config "
2458242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle                                + associatedConfig.SSID + " with " + result.SSID
2468242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle                                + " status " + associatedConfig.autoJoinStatus
2478242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle                                + " reason " + associatedConfig.disableReason
2488242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle                                + " tsp " + associatedConfig.blackListTimestamp
2498242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle                                + " was " + (now - associatedConfig.blackListTimestamp));
250f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
251be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                    mWifiStateMachine.sendMessage(
252be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                            WifiStateMachine.CMD_AUTO_SAVE_NETWORK, associatedConfig);
2537b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle                    didAssociate = true;
254f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
2550d616ef3bf635dff8722e064c0be842676390ed8vandwalle            } else {
2560d616ef3bf635dff8722e064c0be842676390ed8vandwalle                // If the scan result has been blacklisted fir 18 hours -> unblacklist
2570d616ef3bf635dff8722e064c0be842676390ed8vandwalle                if ((now - result.blackListTimestamp) > loseBlackListHardMilli) {
2580d616ef3bf635dff8722e064c0be842676390ed8vandwalle                    result.setAutoJoinStatus(ScanResult.ENABLED);
2590d616ef3bf635dff8722e064c0be842676390ed8vandwalle                }
260f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
2617b581f46f6c9bc6edf0edd287d47106712fb2144vandwalle            if (didAssociate) {
262be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle                numScanResultsKnown++;
263a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                result.isAutoJoinCandidate ++;
264a0708b09ad17b086c008ab100aec7143d7613c80vandwalle            } else {
265a0708b09ad17b086c008ab100aec7143d7613c80vandwalle                result.isAutoJoinCandidate = 0;
266be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            }
267f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
2680c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle
2690c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle        if (unknownScanResults.size() != 0) {
2700c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle            NetworkKey[] newKeys =
2710c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle                    unknownScanResults.toArray(new NetworkKey[unknownScanResults.size()]);
272931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Kick the score manager, we will get updated scores asynchronously
2730c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle            scoreManager.requestScores(newKeys);
2740c8b99a3b78e458a5617cc449e2efe69c5bdd531vandwalle        }
275be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        return numScanResultsKnown;
276f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
277f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
278f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void logDbg(String message) {
2790888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle        logDbg(message, false);
280ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle    }
281ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle
282ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle    void logDbg(String message, boolean stackTrace) {
283ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        if (stackTrace) {
2842f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle            Log.e(TAG, message + " stack:"
285ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[2].getMethodName() + " - "
286ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[3].getMethodName() + " - "
287ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[4].getMethodName() + " - "
288ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    + Thread.currentThread().getStackTrace()[5].getMethodName());
289ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        } else {
2902f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle            Log.e(TAG, message);
291ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        }
292f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
293f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
294931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    // Called directly from WifiStateMachine
295be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle    int newSupplicantResults(boolean doAutoJoin) {
296be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        int numScanResultsKnown;
297e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        List<ScanResult> scanList = mWifiStateMachine.getScanResultsListNoCopyUnsync();
298be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        numScanResultsKnown = addToScanCache(scanList);
299f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        ageScanResultsOut(mScanResultMaximumAge);
300be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        if (DBG) {
301be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle            logDbg("newSupplicantResults size=" + Integer.valueOf(scanResultCache.size())
3022f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        + " known=" + numScanResultsKnown + " "
3039f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                        + doAutoJoin);
304be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        }
3057806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle        if (doAutoJoin) {
3067806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            attemptAutoJoin();
3077806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle        }
308005c1ef113192f898499a407dd266393a8d6b076vandwalle        mWifiConfigStore.writeKnownNetworkHistory(false);
309be3095ed758fca076b9ccb9fdae48f7f865c078avandwalle        return numScanResultsKnown;
310f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
311f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
312f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
313931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
314931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     * Not used at the moment
315f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * should be a call back from WifiScanner HAL ??
316f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * this function is not hooked and working yet, it will receive scan results from WifiScanners
317f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * with the list of IEs,then populate the capabilities by parsing the IEs and inject the scan
318f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * results as normal.
319f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     */
320f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void newHalScanResults() {
321f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        List<ScanResult> scanList = null;//mWifiScanner.syncGetScanResultsList();
322f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String akm = WifiParser.parse_akm(null, null);
323f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        logDbg(akm);
324f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        addToScanCache(scanList);
325f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        ageScanResultsOut(0);
326f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        attemptAutoJoin();
327005c1ef113192f898499a407dd266393a8d6b076vandwalle        mWifiConfigStore.writeKnownNetworkHistory(false);
328f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
329f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
330931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
331931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     *  network link quality changed, called directly from WifiTrafficPoller,
332931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     * or by listening to Link Quality intent
333931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     */
334f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    void linkQualitySignificantChange() {
335f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        attemptAutoJoin();
336f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
337f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
338931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
339f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * compare a WifiConfiguration against the current network, return a delta score
340f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * If not associated, and the candidate will always be better
341f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * For instance if the candidate is a home network versus an unknown public wifi,
342f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle     * the delta will be infinite, else compare Kepler scores etc…
343b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle     * Negatve return values from this functions are meaningless per se, just trying to
344b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle     * keep them distinct for debug purpose (i.e. -1, -2 etc...)
345931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     */
346e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle    private int compareNetwork(WifiConfiguration candidate,
347e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                               String lastSelectedConfiguration) {
348b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        if (candidate == null)
349b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle            return -3;
350b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle
351f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration currentNetwork = mWifiStateMachine.getCurrentWifiConfiguration();
352b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        if (currentNetwork == null) {
353c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // Return any absurdly high score, if we are not connected there is no current
354c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // network to...
355b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle           return 1000;
356b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        }
357f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
358f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (candidate.configKey(true).equals(currentNetwork.configKey(true))) {
359b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle            return -2;
360f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
361f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
362b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle        if (DBG) {
363e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            logDbg("compareNetwork will compare " + candidate.configKey()
364e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    + " with current " + currentNetwork.configKey());
365b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle        }
366833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle        int order = compareWifiConfigurations(currentNetwork, candidate);
367e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
368e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        // The lastSelectedConfiguration is the configuration the user has manually selected
369e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        // thru WifiPicker, or that a 3rd party app asked us to connect to via the
370e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        // enableNetwork with disableOthers=true WifiManager API
371e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        // As this is a direct user choice, we strongly prefer this configuration,
372e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        // hence give +/-100
373e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        if ((lastSelectedConfiguration != null)
374e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                && currentNetwork.configKey().equals(lastSelectedConfiguration)) {
375e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // currentNetwork is the last selected configuration,
376e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // so keep it above connect choices (+/-60) and
377e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // above RSSI/scorer based selection of linked configuration (+/- 50)
378e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // by reducing order by -100
379e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            order = order - 100;
380e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            if (VDBG)   {
381e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                logDbg("     ...and prefers -100 " + currentNetwork.configKey()
382e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " over " + candidate.configKey()
383e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " because it is the last selected -> "
384e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + Integer.toString(order));
385e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            }
386e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        } else if ((lastSelectedConfiguration != null)
387e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                && candidate.configKey().equals(lastSelectedConfiguration)) {
388e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // candidate is the last selected configuration,
389e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // so keep it above connect choices (+/-60) and
390e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // above RSSI/scorer based selection of linked configuration (+/- 50)
391e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            // by increasing order by +100
392e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            order = order + 100;
393e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            if (VDBG)   {
394e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                logDbg("     ...and prefers +100 " + candidate.configKey()
395e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " over " + currentNetwork.configKey()
396e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " because it is the last selected -> "
397e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + Integer.toString(order));
398e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            }
399e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        }
400e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
401ede507649471f1113e9e1919812115ca5a6bc0c8vandwalle        return order;
402f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
403f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
404ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle    /**
405ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * update the network history fields fo that configuration
406ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * - if userTriggered, we mark the configuration as "non selfAdded" since the user has seen it
407ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * and took over management
408ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * - if it is a "connect", remember which network were there at the point of the connect, so
409ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * as those networks get a relative lower score than the selected configuration
41062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle     *
411ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * @param netId
412ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * @param userTriggered : if the update come from WiFiManager
413ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle     * @param connect : if the update includes a connect
414931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     */
41562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle    public void updateConfigurationHistory(int netId, boolean userTriggered, boolean connect) {
416f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration selected = mWifiConfigStore.getWifiConfiguration(netId);
417f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (selected == null) {
418c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            logDbg("updateConfigurationHistory nid=" + netId + " no selected configuration!");
419f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return;
420f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
421f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
422e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle        if (selected.SSID == null) {
423c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            logDbg("updateConfigurationHistory nid=" + netId +
424c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    " no SSID in selected configuration!");
425e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle            return;
426e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle        }
427e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle
42862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle        if (userTriggered) {
429931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Reenable autojoin for this network,
43062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            // since the user want to connect to this configuration
43127355a942653264388e909a4276196ee63e57811vandwalle            selected.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
43262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            selected.selfAdded = false;
433e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            selected.dirty = true;
43462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle        }
435f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
436992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle        if (DBG && userTriggered) {
437f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (selected.connectChoices != null) {
438ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                logDbg("updateConfigurationHistory will update "
439f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        + Integer.toString(netId) + " now: "
440992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        + Integer.toString(selected.connectChoices.size())
441992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        + " uid=" + Integer.toString(selected.creatorUid), true);
442f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else {
443ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                logDbg("updateConfigurationHistory will update "
444992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        + Integer.toString(netId)
445992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        + " uid=" + Integer.toString(selected.creatorUid), true);
446f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
447f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
448f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
449ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle        if (connect && userTriggered) {
450ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle            boolean found = false;
4512451dbcc4f9641df188326215b204b798eb70c46vandwalle            int choice = 0;
452c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            int size = 0;
4539f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
4549f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            // Reset the triggered disabled count, because user wanted to connect to this
4559f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            // configuration, and we were not.
4569f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            selected.numUserTriggeredWifiDisableLowRSSI = 0;
4579f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            selected.numUserTriggeredWifiDisableBadRSSI = 0;
4589f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            selected.numUserTriggeredWifiDisableNotHighRSSI = 0;
4599f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            selected.numUserTriggeredJoinAttempts++;
4609f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
46162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            List<WifiConfiguration> networks =
46262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    mWifiConfigStore.getRecentConfiguredNetworks(12000, false);
463c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            if (networks != null) size = networks.size();
464c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            logDbg("updateConfigurationHistory found " + size + " networks");
46562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle            if (networks != null) {
46662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                for (WifiConfiguration config : networks) {
467992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    if (DBG) {
468992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                        logDbg("updateConfigurationHistory got " + config.SSID + " nid="
469992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                                + Integer.toString(config.networkId));
470992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle                    }
471f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
47262f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    if (selected.configKey(true).equals(config.configKey(true))) {
473ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        found = true;
47462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                        continue;
47562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    }
476f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
4772451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // Compare RSSI values so as to evaluate the strength of the user preference
4782451dbcc4f9641df188326215b204b798eb70c46vandwalle                    int order = compareWifiConfigurationsRSSI(config, selected, null);
4792451dbcc4f9641df188326215b204b798eb70c46vandwalle
4802451dbcc4f9641df188326215b204b798eb70c46vandwalle                    if (order < -30) {
4812451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // Selected configuration is worse than the visible configuration
4822451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // hence register a strong choice so as autojoin cannot override this
4832451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // for instance, the user has select a network
4842451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // with 1 bar over a network with 3 bars...
4852451dbcc4f9641df188326215b204b798eb70c46vandwalle                        choice = 60;
4862451dbcc4f9641df188326215b204b798eb70c46vandwalle                    } else if (order < -20) {
4872451dbcc4f9641df188326215b204b798eb70c46vandwalle                        choice = 50;
4882451dbcc4f9641df188326215b204b798eb70c46vandwalle                    } else if (order < -10) {
4892451dbcc4f9641df188326215b204b798eb70c46vandwalle                        choice = 40;
4902f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    } else if (order < 20) {
4912451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // Selected configuration is about same or has a slightly better RSSI
4922451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // hence register a weaker choice, here a difference of at least +/-30 in
4932451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // RSSI comparison triggered by autoJoin will override the choice
4942451dbcc4f9641df188326215b204b798eb70c46vandwalle                        choice = 30;
4952f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    } else {
4962451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // Selected configuration is better than the visible configuration
4972451dbcc4f9641df188326215b204b798eb70c46vandwalle                        // hence we do not know if the user prefers this configuration strongly
4982451dbcc4f9641df188326215b204b798eb70c46vandwalle                        choice = 20;
49962f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    }
500f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
501931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // The selected configuration was preferred over a recently seen config
502931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // hence remember the user's choice:
503931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // add the recently seen config to the selected's connectChoices array
504ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle
505ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    if (selected.connectChoices == null) {
506ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        selected.connectChoices = new HashMap<String, Integer>();
507ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    }
508ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle
509ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                    logDbg("updateConfigurationHistory add a choice " + selected.configKey(true)
5100888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                            + " over " + config.configKey(true)
5112451dbcc4f9641df188326215b204b798eb70c46vandwalle                            + " choice " + Integer.toString(choice));
512cf5b8eb8a08c45bd4a82f1f4bb789c8a1b08744fvandwalle
5132451dbcc4f9641df188326215b204b798eb70c46vandwalle                    Integer currentChoice = selected.connectChoices.get(config.configKey(true));
5142f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    if (currentChoice != null) {
5152f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        // User has made this choice multiple time in a row, so bump up a lot
5162f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        choice += currentChoice.intValue();
5172451dbcc4f9641df188326215b204b798eb70c46vandwalle                    }
5182f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    // Add the visible config to the selected's connect choice list
5192f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    selected.connectChoices.put(config.configKey(true), choice);
520f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
52162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    if (config.connectChoices != null) {
522ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        if (VDBG) {
523ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                            logDbg("updateConfigurationHistory will remove "
52462f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                                    + selected.configKey(true) + " from " + config.configKey(true));
525ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        }
526931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                        // Remove the selected from the recently seen config's connectChoice list
52762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                        config.connectChoices.remove(selected.configKey(true));
5280888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle
5290888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        if (selected.linkedConfigurations != null) {
530931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                           // Remove the selected's linked configuration from the
531931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                           // recently seen config's connectChoice list
5320888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                           for (String key : selected.linkedConfigurations.keySet()) {
5330888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                               config.connectChoices.remove(key);
5340888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                           }
5350888ce6f90bdaeee799dd8361ea4781e23a33b87vandwalle                        }
53662f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    }
537ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                }
538ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                if (found == false) {
5392451dbcc4f9641df188326215b204b798eb70c46vandwalle                     // We haven't found the configuration that the user just selected in our
5402451dbcc4f9641df188326215b204b798eb70c46vandwalle                     // scan cache.
5412451dbcc4f9641df188326215b204b798eb70c46vandwalle                     // In that case we will need a new scan before attempting to connect to this
5422451dbcc4f9641df188326215b204b798eb70c46vandwalle                     // configuration anyhow and thus we can process the scan results then.
543ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                     logDbg("updateConfigurationHistory try to connect to an old network!! : "
544ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                             + selected.configKey());
54562f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                }
546f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
54762f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                if (selected.connectChoices != null) {
54862f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                    if (VDBG)
549ecd2b88214b5d214fd1f63a9560caff9058912ddvandwalle                        logDbg("updateConfigurationHistory " + Integer.toString(netId)
55062f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                                + " now: " + Integer.toString(selected.connectChoices.size()));
55162f1d0ca8ea4466628f6ff179b1f20e1279fa7e0vandwalle                }
552f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
553f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
554992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle
555931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        // TODO: write only if something changed
556992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle        if (userTriggered || connect) {
557005c1ef113192f898499a407dd266393a8d6b076vandwalle            mWifiConfigStore.writeKnownNetworkHistory(false);
558992ae00f25a9cc22cf5db3261bd7e72927069cf7vandwalle        }
559f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
560f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
5612451dbcc4f9641df188326215b204b798eb70c46vandwalle    int getConnectChoice(WifiConfiguration source, WifiConfiguration target) {
5622451dbcc4f9641df188326215b204b798eb70c46vandwalle        Integer choice = null;
5632451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (source == null || target == null) {
5642451dbcc4f9641df188326215b204b798eb70c46vandwalle            return 0;
565f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
566f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
5672451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (source.connectChoices != null
5682451dbcc4f9641df188326215b204b798eb70c46vandwalle                && source.connectChoices.containsKey(target.configKey(true))) {
5692451dbcc4f9641df188326215b204b798eb70c46vandwalle            choice = source.connectChoices.get(target.configKey(true));
5702451dbcc4f9641df188326215b204b798eb70c46vandwalle        } else if (source.linkedConfigurations != null) {
571f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            for (String key : source.linkedConfigurations.keySet()) {
572f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                WifiConfiguration config = mWifiConfigStore.getWifiConfiguration(key);
573f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (config != null) {
574f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if (config.connectChoices != null) {
5752451dbcc4f9641df188326215b204b798eb70c46vandwalle                        choice = config.connectChoices.get(target.configKey(true));
576f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
577f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
578f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
5792451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
5802451dbcc4f9641df188326215b204b798eb70c46vandwalle
5812451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (choice == null) {
5822451dbcc4f9641df188326215b204b798eb70c46vandwalle            //We didn't find the connect choice
5832451dbcc4f9641df188326215b204b798eb70c46vandwalle            return 0;
5842451dbcc4f9641df188326215b204b798eb70c46vandwalle        } else {
5852451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (choice.intValue() < 0) {
5862451dbcc4f9641df188326215b204b798eb70c46vandwalle                choice = 20; // Compatibility with older files
5872451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
5882451dbcc4f9641df188326215b204b798eb70c46vandwalle            return choice.intValue();
5892451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
5902451dbcc4f9641df188326215b204b798eb70c46vandwalle    }
5912451dbcc4f9641df188326215b204b798eb70c46vandwalle
592815788ba7838fc54310baed3deb9b95548e0ce69vandwalle    int compareWifiConfigurationsFromVisibility(WifiConfiguration a, int aRssiBoost,
593815788ba7838fc54310baed3deb9b95548e0ce69vandwalle             WifiConfiguration b, int bRssiBoost) {
5942451dbcc4f9641df188326215b204b798eb70c46vandwalle
595815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        int aRssiBoost5 = 0; // 5GHz RSSI boost to apply for purpose band selection (5GHz pref)
596815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        int bRssiBoost5 = 0; // 5GHz RSSI boost to apply for purpose band selection (5GHz pref)
597815788ba7838fc54310baed3deb9b95548e0ce69vandwalle
598815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        int aScore = 0;
599815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        int bScore = 0;
600815788ba7838fc54310baed3deb9b95548e0ce69vandwalle
601815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        boolean aPrefers5GHz = false;
602815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        boolean bPrefers5GHz = false;
6034dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
6042451dbcc4f9641df188326215b204b798eb70c46vandwalle        /**
605815788ba7838fc54310baed3deb9b95548e0ce69vandwalle         * Calculate a boost to apply to RSSI value of configuration we want to join on 5GHz:
606815788ba7838fc54310baed3deb9b95548e0ce69vandwalle         * Boost RSSI value of 5GHz bands iff the base value is better than threshold,
607815788ba7838fc54310baed3deb9b95548e0ce69vandwalle         * penalize the RSSI value of 5GHz band iff the base value is lower than threshold
6082451dbcc4f9641df188326215b204b798eb70c46vandwalle         * This implements band preference where we prefer 5GHz if RSSI5 is good enough, whereas
6092451dbcc4f9641df188326215b204b798eb70c46vandwalle         * we prefer 2.4GHz otherwise.
6102451dbcc4f9641df188326215b204b798eb70c46vandwalle         */
611815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        aRssiBoost5 = rssiBoostFrom5GHzRssi(a.visibility.rssi5, a.configKey() + "->");
612815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        bRssiBoost5 = rssiBoostFrom5GHzRssi(b.visibility.rssi5, b.configKey() + "->");
6132451dbcc4f9641df188326215b204b798eb70c46vandwalle
614815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        // Select which band to use for a
615815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        if (a.visibility.rssi5 + aRssiBoost5 > b.visibility.rssi24) {
6162451dbcc4f9641df188326215b204b798eb70c46vandwalle            // Prefer a's 5GHz
617815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            aPrefers5GHz = true;
618815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        }
619815788ba7838fc54310baed3deb9b95548e0ce69vandwalle
620815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        // Select which band to use for b
621815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        if (b.visibility.rssi5 + bRssiBoost5 > b.visibility.rssi24) {
622815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            // Prefer b's 5GHz
623815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            bPrefers5GHz = true;
624815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        }
625815788ba7838fc54310baed3deb9b95548e0ce69vandwalle
626815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        if (aPrefers5GHz) {
627815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            if (bPrefers5GHz) {
628815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // If both a and b are on 5GHz then we don't apply the 5GHz RSSI boost to either
629815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // one, but directly compare the RSSI values, this improves stability,
630815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // since the 5GHz RSSI boost can introduce large fluctuations
631815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                aScore = a.visibility.rssi5 + aRssiBoost;
632815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            } else {
633815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // If only a is on 5GHz, then apply the 5GHz preference boost to a
634815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                aScore = a.visibility.rssi5 + aRssiBoost + aRssiBoost5;
635815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            }
6362451dbcc4f9641df188326215b204b798eb70c46vandwalle        } else {
637815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            aScore = a.visibility.rssi24 + aRssiBoost;
638f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
6392451dbcc4f9641df188326215b204b798eb70c46vandwalle
640815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        if (bPrefers5GHz) {
641815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            if (aPrefers5GHz) {
642815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // If both a and b are on 5GHz then we don't apply the 5GHz RSSI boost to either
643815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // one, but directly compare the RSSI values, this improves stability,
644815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // since the 5GHz RSSI boost can introduce large fluctuations
645815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                bScore = b.visibility.rssi5 + bRssiBoost;
646815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            } else {
647815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // If only b is on 5GHz, then apply the 5GHz preference boost to b
648815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                bScore = b.visibility.rssi5 + bRssiBoost + bRssiBoost5;
649815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            }
650815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        } else {
651815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            bScore = b.visibility.rssi24 + bRssiBoost;
652815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        }
653815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        if (VDBG) {
654815788ba7838fc54310baed3deb9b95548e0ce69vandwalle            logDbg("        " + a.configKey() + " is5=" + aPrefers5GHz + " score=" + aScore
655815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                    + b.configKey() + " is5=" + bPrefers5GHz + " score=" + bScore);
656815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        }
657815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        // Compare a and b
658815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        // If a score is higher then a > b and the order is descending (negative)
659815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        // If b score is higher then a < b and the order is ascending (positive)
660815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        return bScore - aScore;
661f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
662f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
6632451dbcc4f9641df188326215b204b798eb70c46vandwalle    // Compare WifiConfiguration by RSSI, and return a comparison value in the range [-50, +50]
6642451dbcc4f9641df188326215b204b798eb70c46vandwalle    // The result represents "approximately" an RSSI difference measured in dBM
6652451dbcc4f9641df188326215b204b798eb70c46vandwalle    // Adjusted with various parameters:
6662451dbcc4f9641df188326215b204b798eb70c46vandwalle    // +) current network gets a +15 boost
6672451dbcc4f9641df188326215b204b798eb70c46vandwalle    // +) 5GHz signal, if they are strong enough, get a +15 or +25 boost, representing the
6682451dbcc4f9641df188326215b204b798eb70c46vandwalle    // fact that at short range we prefer 5GHz band as it is cleaner of interference and
6692451dbcc4f9641df188326215b204b798eb70c46vandwalle    // provides for wider channels
6702451dbcc4f9641df188326215b204b798eb70c46vandwalle    int compareWifiConfigurationsRSSI(WifiConfiguration a, WifiConfiguration b,
6712451dbcc4f9641df188326215b204b798eb70c46vandwalle                                      String currentConfiguration) {
672f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        int order = 0;
6732451dbcc4f9641df188326215b204b798eb70c46vandwalle
6742451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Boost used so as to favor current config
6752451dbcc4f9641df188326215b204b798eb70c46vandwalle        int aRssiBoost = 0;
6762451dbcc4f9641df188326215b204b798eb70c46vandwalle        int bRssiBoost = 0;
6772451dbcc4f9641df188326215b204b798eb70c46vandwalle
6782451dbcc4f9641df188326215b204b798eb70c46vandwalle        int scoreA;
6792451dbcc4f9641df188326215b204b798eb70c46vandwalle        int scoreB;
6802451dbcc4f9641df188326215b204b798eb70c46vandwalle
6812451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Retrieve the visibility
682f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration.Visibility astatus = a.visibility;
683f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration.Visibility bstatus = b.visibility;
684f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (astatus == null || bstatus == null) {
6852451dbcc4f9641df188326215b204b798eb70c46vandwalle            // Error visibility wasn't set
686b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("    compareWifiConfigurations NULL band status!");
687f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            return 0;
688f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
6892451dbcc4f9641df188326215b204b798eb70c46vandwalle
6902451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Apply Hysteresis, boost RSSI of current configuration
6912451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (null != currentConfiguration) {
6922451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (a.configKey().equals(currentConfiguration)) {
69393a1fddee50a244d31036cddae6b7db6630fd93dvandwalle                aRssiBoost = mWifiConfigStore.currentNetworkBoost;
6942451dbcc4f9641df188326215b204b798eb70c46vandwalle            } else if (b.configKey().equals(currentConfiguration)) {
69593a1fddee50a244d31036cddae6b7db6630fd93dvandwalle                bRssiBoost = mWifiConfigStore.currentNetworkBoost;
6962451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
6972451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
6982451dbcc4f9641df188326215b204b798eb70c46vandwalle
6992451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (VDBG)  {
700b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("    compareWifiConfigurationsRSSI: " + a.configKey()
701c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    + " " + Integer.toString(astatus.rssi24)
702c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    + "," + Integer.toString(astatus.rssi5)
7032451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " boost=" + Integer.toString(aRssiBoost)
704c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    + " " + b.configKey() + " "
705c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    + Integer.toString(bstatus.rssi24) + ","
706c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    + Integer.toString(bstatus.rssi5)
7072451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " boost=" + Integer.toString(bRssiBoost)
7082451dbcc4f9641df188326215b204b798eb70c46vandwalle            );
709f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
7102451dbcc4f9641df188326215b204b798eb70c46vandwalle
711815788ba7838fc54310baed3deb9b95548e0ce69vandwalle        order = compareWifiConfigurationsFromVisibility(a, aRssiBoost, b, bRssiBoost);
7122451dbcc4f9641df188326215b204b798eb70c46vandwalle
7132451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Normalize the order to [-50, +50]
7142451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (order > 50) order = 50;
7152451dbcc4f9641df188326215b204b798eb70c46vandwalle        else if (order < -50) order = -50;
7162451dbcc4f9641df188326215b204b798eb70c46vandwalle
7172451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (VDBG) {
7182451dbcc4f9641df188326215b204b798eb70c46vandwalle            String prefer = " = ";
7192451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (order > 0) {
7202451dbcc4f9641df188326215b204b798eb70c46vandwalle                prefer = " < "; // Ascending
7212451dbcc4f9641df188326215b204b798eb70c46vandwalle            } else if (order < 0) {
7222451dbcc4f9641df188326215b204b798eb70c46vandwalle                prefer = " > "; // Descending
7232451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
724b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("    compareWifiConfigurationsRSSI " + a.configKey()
7252451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " rssi=(" + a.visibility.rssi24
7262451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + a.visibility.rssi5
7272451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + ") num=(" + a.visibility.num24
7282451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + a.visibility.num5 + ")"
7292451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + prefer + b.configKey()
7302451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " rssi=(" + b.visibility.rssi24
7312451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + b.visibility.rssi5
7322451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + ") num=(" + b.visibility.num24
7332451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + b.visibility.num5 + ")"
7342451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " -> " + order);
7352451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
7362451dbcc4f9641df188326215b204b798eb70c46vandwalle
737f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        return order;
738f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
739f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
740833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle    /**
741833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle     * b/18490330 only use scorer for untrusted networks
742833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle     *
7432451dbcc4f9641df188326215b204b798eb70c46vandwalle    int compareWifiConfigurationsWithScorer(WifiConfiguration a, WifiConfiguration b) {
7442451dbcc4f9641df188326215b204b798eb70c46vandwalle
74581c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson        boolean aIsActive = false;
74681c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson        boolean bIsActive = false;
7472451dbcc4f9641df188326215b204b798eb70c46vandwalle
7482451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Apply Hysteresis : boost RSSI of current configuration before
7492451dbcc4f9641df188326215b204b798eb70c46vandwalle        // looking up the score
7502451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (null != mCurrentConfigurationKey) {
7512451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (a.configKey().equals(mCurrentConfigurationKey)) {
75281c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                aIsActive = true;
7532451dbcc4f9641df188326215b204b798eb70c46vandwalle            } else if (b.configKey().equals(mCurrentConfigurationKey)) {
75481c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                bIsActive = true;
7552451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
7562451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
757833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle        int scoreA = getConfigNetworkScore(a, mScanResultAutoJoinAge, aIsActive);
758833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle        int scoreB = getConfigNetworkScore(b, mScanResultAutoJoinAge, bIsActive);
7592451dbcc4f9641df188326215b204b798eb70c46vandwalle
7602451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Both configurations need to have a score for the scorer to be used
7612451dbcc4f9641df188326215b204b798eb70c46vandwalle        // ...and the scores need to be different:-)
7622451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (scoreA == WifiNetworkScoreCache.INVALID_NETWORK_SCORE
7632451dbcc4f9641df188326215b204b798eb70c46vandwalle                || scoreB == WifiNetworkScoreCache.INVALID_NETWORK_SCORE) {
764e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle            if (VDBG)  {
765b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    compareWifiConfigurationsWithScorer no-scores: "
766e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                        + a.configKey()
767e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                        + " "
768e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                        + b.configKey());
769e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle            }
7702451dbcc4f9641df188326215b204b798eb70c46vandwalle            return 0;
7712451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
7722451dbcc4f9641df188326215b204b798eb70c46vandwalle
7732451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (VDBG) {
7742451dbcc4f9641df188326215b204b798eb70c46vandwalle            String prefer = " = ";
7752451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (scoreA < scoreB) {
7762451dbcc4f9641df188326215b204b798eb70c46vandwalle                prefer = " < ";
7772451dbcc4f9641df188326215b204b798eb70c46vandwalle            } if (scoreA > scoreB) {
7782451dbcc4f9641df188326215b204b798eb70c46vandwalle                prefer = " > ";
7792451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
780b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("    compareWifiConfigurationsWithScorer " + a.configKey()
7812451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " rssi=(" + a.visibility.rssi24
7822451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + a.visibility.rssi5
7832451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + ") num=(" + a.visibility.num24
7842451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + a.visibility.num5 + ")"
785e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    + " sc=" + scoreA
7862451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + prefer + b.configKey()
7872451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " rssi=(" + b.visibility.rssi24
7882451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + b.visibility.rssi5
7892451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + ") num=(" + b.visibility.num24
7902451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + "," + b.visibility.num5 + ")"
791e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                    + " sc=" + scoreB
7922451dbcc4f9641df188326215b204b798eb70c46vandwalle                    + " -> " + Integer.toString(scoreB - scoreA));
7932451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
794c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
7952451dbcc4f9641df188326215b204b798eb70c46vandwalle        // If scoreA > scoreB, the comparison is descending hence the return value is negative
7962451dbcc4f9641df188326215b204b798eb70c46vandwalle        return scoreB - scoreA;
7972451dbcc4f9641df188326215b204b798eb70c46vandwalle    }
798833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle     */
7992451dbcc4f9641df188326215b204b798eb70c46vandwalle
800f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    int compareWifiConfigurations(WifiConfiguration a, WifiConfiguration b) {
801f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        int order = 0;
802f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        boolean linked = false;
803f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
804453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        if ((a.linkedConfigurations != null) && (b.linkedConfigurations != null)
805453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                && (a.autoJoinStatus == WifiConfiguration.AUTO_JOIN_ENABLED)
806453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                && (b.autoJoinStatus == WifiConfiguration.AUTO_JOIN_ENABLED)) {
8072451dbcc4f9641df188326215b204b798eb70c46vandwalle            if ((a.linkedConfigurations.get(b.configKey(true)) != null)
8082451dbcc4f9641df188326215b204b798eb70c46vandwalle                    && (b.linkedConfigurations.get(a.configKey(true)) != null)) {
809f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                linked = true;
810f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
811f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
812f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
813f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (a.ephemeral && b.ephemeral == false) {
814f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG) {
815b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    compareWifiConfigurations ephemeral and prefers " + b.configKey()
816453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + " over " + a.configKey());
817f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
818931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            return 1; // b is of higher priority - ascending
819f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
820f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (b.ephemeral && a.ephemeral == false) {
821f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (VDBG) {
822b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    compareWifiConfigurations ephemeral and prefers " + a.configKey()
823453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + " over " + b.configKey());
824f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
825931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            return -1; // a is of higher priority - descending
826f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
827f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
8282451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Apply RSSI, in the range [-5, +5]
8292451dbcc4f9641df188326215b204b798eb70c46vandwalle        // after band adjustment, +n difference roughly corresponds to +10xn dBm
8302451dbcc4f9641df188326215b204b798eb70c46vandwalle        order = order + compareWifiConfigurationsRSSI(a, b, mCurrentConfigurationKey);
831f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
8322451dbcc4f9641df188326215b204b798eb70c46vandwalle        // If the configurations are not linked, compare by user's choice, only a
8332451dbcc4f9641df188326215b204b798eb70c46vandwalle        // very high RSSI difference can then override the choice
8342451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (!linked) {
8352451dbcc4f9641df188326215b204b798eb70c46vandwalle            int choice;
836f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
8372451dbcc4f9641df188326215b204b798eb70c46vandwalle            choice = getConnectChoice(a, b);
8382451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (choice > 0) {
839931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // a is of higher priority - descending
8402451dbcc4f9641df188326215b204b798eb70c46vandwalle                order = order - choice;
841f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (VDBG) {
842b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    logDbg("    compareWifiConfigurations prefers " + a.configKey()
843453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                            + " over " + b.configKey()
844b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + " due to user choice of " + choice
845b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + " order -> " + Integer.toString(order));
846f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
8472451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
8482451dbcc4f9641df188326215b204b798eb70c46vandwalle
8492451dbcc4f9641df188326215b204b798eb70c46vandwalle            choice = getConnectChoice(b, a);
8502451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (choice > 0) {
851931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // a is of lower priority - ascending
8522451dbcc4f9641df188326215b204b798eb70c46vandwalle                order = order + choice;
8534dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                if (VDBG) {
854b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    logDbg("    compareWifiConfigurations prefers " + b.configKey() + " over "
855b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + a.configKey() + " due to user choice of " + choice
856b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + " order ->" + Integer.toString(order));
857f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
858f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
859f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
860ede1310be531a84faa08f02c3fd243448dd936ddvandwalle
861f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (order == 0) {
862931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // We don't know anything - pick the last seen i.e. K behavior
863931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // we should do this only for recently picked configurations
864f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (a.priority > b.priority) {
865931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // a is of higher priority - descending
8662451dbcc4f9641df188326215b204b798eb70c46vandwalle                if (VDBG) {
867b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    logDbg("    compareWifiConfigurations prefers -1 " + a.configKey() + " over "
868453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                            + b.configKey() + " due to priority");
869f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
870f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
871f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                order = -1;
872f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else if (a.priority < b.priority) {
873931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // a is of lower priority - ascending
8742451dbcc4f9641df188326215b204b798eb70c46vandwalle                if (VDBG) {
875b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    logDbg("    compareWifiConfigurations prefers +1 " + b.configKey() + " over "
876453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                            + a.configKey() + " due to priority");
877f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
8782451dbcc4f9641df188326215b204b798eb70c46vandwalle                order = 1;
879f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
880f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
881f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
882f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String sorder = " == ";
883931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        if (order > 0) {
884f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            sorder = " < ";
885931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        } else if (order < 0) {
886f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            sorder = " > ";
887931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        }
888f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
8892451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (VDBG) {
890b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("compareWifiConfigurations: " + a.configKey() + sorder
891453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    + b.configKey() + " order " + Integer.toString(order));
892f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
893f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
894f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        return order;
895f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
896f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
897c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    boolean isBadCandidate(int rssi5, int rssi24) {
898c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        return (rssi5 < -80 && rssi24 < -90);
899c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle    }
900c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
901833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle    /*
902c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    int compareWifiConfigurationsTop(WifiConfiguration a, WifiConfiguration b) {
903c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        int scorerOrder = compareWifiConfigurationsWithScorer(a, b);
904c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        int order = compareWifiConfigurations(a, b);
905c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
906c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        if (scorerOrder * order < 0) {
907b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            if (VDBG) {
908b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    -> compareWifiConfigurationsTop: " +
909b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                        "scorer override " + scorerOrder + " " + order);
910b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            }
911c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // For debugging purpose, remember that an override happened
912c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // during that autojoin Attempt
913c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            didOverride = true;
914c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            a.numScorerOverride++;
915c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            b.numScorerOverride++;
916c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        }
917c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
918c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        if (scorerOrder != 0) {
919c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // If the scorer came up with a result then use the scorer's result, else use
920c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            // the order provided by the base comparison function
921c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle            order = scorerOrder;
922c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        }
923c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        return order;
924c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle    }
925833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle    */
926c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle
9279f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle    public int rssiBoostFrom5GHzRssi(int rssi, String dbg) {
9280eebae7334d6129f7ca1344e4b20199794994358vandwalle        if (!mWifiConfigStore.enable5GHzPreference) {
9290eebae7334d6129f7ca1344e4b20199794994358vandwalle            return 0;
9300eebae7334d6129f7ca1344e4b20199794994358vandwalle        }
931e67ec726c07410073575473c0f50dc737629f5davandwalle        if (rssi
9320eebae7334d6129f7ca1344e4b20199794994358vandwalle                > mWifiConfigStore.bandPreferenceBoostThreshold5) {
933e67ec726c07410073575473c0f50dc737629f5davandwalle            // Boost by 2 dB for each point
934e67ec726c07410073575473c0f50dc737629f5davandwalle            //    Start boosting at -65
935e67ec726c07410073575473c0f50dc737629f5davandwalle            //    Boost by 20 if above -55
936e67ec726c07410073575473c0f50dc737629f5davandwalle            //    Boost by 40 if abore -45
9370eebae7334d6129f7ca1344e4b20199794994358vandwalle            int boost = mWifiConfigStore.bandPreferenceBoostFactor5
9380eebae7334d6129f7ca1344e4b20199794994358vandwalle                    *(rssi - mWifiConfigStore.bandPreferenceBoostThreshold5);
939e67ec726c07410073575473c0f50dc737629f5davandwalle            if (boost > 50) {
940815788ba7838fc54310baed3deb9b95548e0ce69vandwalle                // 50 dB boost allows jumping from 2.4 to 5GHz
941e67ec726c07410073575473c0f50dc737629f5davandwalle                // consistently
942e67ec726c07410073575473c0f50dc737629f5davandwalle                boost = 50;
943e67ec726c07410073575473c0f50dc737629f5davandwalle            }
9449f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            if (VDBG && dbg != null) {
945b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("        " + dbg + ":    rssi5 " + rssi + " boost " + boost);
9469f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            }
947e67ec726c07410073575473c0f50dc737629f5davandwalle            return boost;
948e67ec726c07410073575473c0f50dc737629f5davandwalle        }
949e67ec726c07410073575473c0f50dc737629f5davandwalle
950e67ec726c07410073575473c0f50dc737629f5davandwalle        if (rssi
9510eebae7334d6129f7ca1344e4b20199794994358vandwalle                < mWifiConfigStore.bandPreferencePenaltyThreshold5) {
9520eebae7334d6129f7ca1344e4b20199794994358vandwalle            // penalize if < -75
9530eebae7334d6129f7ca1344e4b20199794994358vandwalle            int boost = mWifiConfigStore.bandPreferencePenaltyFactor5
9540eebae7334d6129f7ca1344e4b20199794994358vandwalle                    *(rssi - mWifiConfigStore.bandPreferencePenaltyThreshold5);
9550eebae7334d6129f7ca1344e4b20199794994358vandwalle            return boost;
956e67ec726c07410073575473c0f50dc737629f5davandwalle        }
957e67ec726c07410073575473c0f50dc737629f5davandwalle        return 0;
958e67ec726c07410073575473c0f50dc737629f5davandwalle    }
959c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        /**
960e67ec726c07410073575473c0f50dc737629f5davandwalle         * attemptRoam() function implements the core of the same SSID switching algorithm
961e67ec726c07410073575473c0f50dc737629f5davandwalle         *
962e67ec726c07410073575473c0f50dc737629f5davandwalle         * Run thru all recent scan result of a WifiConfiguration and select the
963e67ec726c07410073575473c0f50dc737629f5davandwalle         * best one.
964c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle         */
965e67ec726c07410073575473c0f50dc737629f5davandwalle    public ScanResult attemptRoam(ScanResult a,
966e67ec726c07410073575473c0f50dc737629f5davandwalle                                  WifiConfiguration current, int age, String currentBSSID) {
967b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        if (current == null) {
968b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            if (VDBG)   {
969b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                logDbg("attemptRoam not associated");
970b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            }
971e67ec726c07410073575473c0f50dc737629f5davandwalle            return a;
9724dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
973b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        if (current.scanResultCache == null) {
974b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            if (VDBG)   {
975b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                logDbg("attemptRoam no scan cache");
976b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            }
977e67ec726c07410073575473c0f50dc737629f5davandwalle            return a;
9784dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
979b07da189850a4bfa268f8ab9be7867935eb2ecb5vandwalle        if (current.scanResultCache.size() > 6) {
980b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            if (VDBG)   {
981c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                logDbg("attemptRoam scan cache size "
982c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                        + current.scanResultCache.size() + " --> bail");
983b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            }
984931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Implement same SSID roaming only for configurations
985c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle            // that have less than 4 BSSIDs
986e67ec726c07410073575473c0f50dc737629f5davandwalle            return a;
9874dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
988b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle
9892f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle        if (current.BSSID != null && !current.BSSID.equals("any")) {
990b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            if (DBG)   {
9912f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                logDbg("attemptRoam() BSSID is set "
9922f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        + current.BSSID + " -> bail");
993b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            }
994e67ec726c07410073575473c0f50dc737629f5davandwalle            return a;
9954dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
9964dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
997931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        // Determine which BSSID we want to associate to, taking account
998c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle        // relative strength of 5 and 2.4 GHz BSSIDs
9992451dbcc4f9641df188326215b204b798eb70c46vandwalle        long nowMs = System.currentTimeMillis();
10004dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
100197b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle        for (ScanResult b : current.scanResultCache.values()) {
100297b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            int bRssiBoost5 = 0;
100397b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            int aRssiBoost5 = 0;
100497b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            int bRssiBoost = 0;
100597b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            int aRssiBoost = 0;
1006931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            if ((b.seen == 0) || (b.BSSID == null)
10070d616ef3bf635dff8722e064c0be842676390ed8vandwalle                    || ((nowMs - b.seen) > age)
1008e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    || b.autoJoinStatus != ScanResult.ENABLED
1009e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    || b.numIpConfigFailures > 8) {
10104dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                continue;
10113a2a3d226881cce8a4e511302231d843b0def303vandwalle            }
10124dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1013931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Pick first one
10144dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            if (a == null) {
10154dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                a = b;
10164dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                continue;
10174dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
10184dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1019e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            if (b.numIpConfigFailures < (a.numIpConfigFailures - 1)) {
1020e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                // Prefer a BSSID that doesn't have less number of Ip config failures
1021e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                logDbg("attemptRoam: "
1022e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + b.BSSID + " rssi=" + b.level + " ipfail=" +b.numIpConfigFailures
1023e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " freq=" + b.frequency
1024e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " > "
1025e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + a.BSSID + " rssi=" + a.level + " ipfail=" +a.numIpConfigFailures
1026e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " freq=" + a.frequency);
1027e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                a = b;
1028e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                continue;
1029e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle            }
1030e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle
10312451dbcc4f9641df188326215b204b798eb70c46vandwalle            // Apply hysteresis: we favor the currentBSSID by giving it a boost
10327806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            if (currentBSSID != null && currentBSSID.equals(b.BSSID)) {
1033931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // Reduce the benefit of hysteresis if RSSI <= -75
10340eebae7334d6129f7ca1344e4b20199794994358vandwalle                if (b.level <= mWifiConfigStore.bandPreferencePenaltyThreshold5) {
10350eebae7334d6129f7ca1344e4b20199794994358vandwalle                    bRssiBoost = mWifiConfigStore.associatedHysteresisLow;
1036c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                } else {
10370eebae7334d6129f7ca1344e4b20199794994358vandwalle                    bRssiBoost = mWifiConfigStore.associatedHysteresisHigh;
1038c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                }
10394dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
10407806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            if (currentBSSID != null && currentBSSID.equals(a.BSSID)) {
10410eebae7334d6129f7ca1344e4b20199794994358vandwalle                if (a.level <= mWifiConfigStore.bandPreferencePenaltyThreshold5) {
1042931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // Reduce the benefit of hysteresis if RSSI <= -75
10430eebae7334d6129f7ca1344e4b20199794994358vandwalle                    aRssiBoost = mWifiConfigStore.associatedHysteresisLow;
1044c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                } else {
10450eebae7334d6129f7ca1344e4b20199794994358vandwalle                    aRssiBoost = mWifiConfigStore.associatedHysteresisHigh;
1046c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                }
10474dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
10482451dbcc4f9641df188326215b204b798eb70c46vandwalle
104997b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            // Favor 5GHz: give a boost to 5GHz BSSIDs, with a slightly progressive curve
10502451dbcc4f9641df188326215b204b798eb70c46vandwalle            //   Boost the BSSID if it is on 5GHz, above a threshold
10512451dbcc4f9641df188326215b204b798eb70c46vandwalle            //   But penalize it if it is on 5GHz and below threshold
105297b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            //
105397b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            //   With he current threshold values, 5GHz network with RSSI above -55
105497b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            //   Are given a boost of 30DB which is enough to overcome the current BSSID
105597b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle            //   hysteresis (+14) plus 2.4/5 GHz signal strength difference on most cases
10569f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            //
10579f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            // The "current BSSID" Boost must be added to the BSSID's level so as to introduce\
10589f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle            // soem amount of hysteresis
1059e67ec726c07410073575473c0f50dc737629f5davandwalle            if (b.is5GHz()) {
10609f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                bRssiBoost5 = rssiBoostFrom5GHzRssi(b.level + bRssiBoost, b.BSSID);
10614dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
1062e67ec726c07410073575473c0f50dc737629f5davandwalle            if (a.is5GHz()) {
10639f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                aRssiBoost5 = rssiBoostFrom5GHzRssi(a.level + aRssiBoost, a.BSSID);
10644dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
10654dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
10664dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            if (VDBG)  {
1067c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                String comp = " < ";
1068c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                if (b.level + bRssiBoost + bRssiBoost5 > a.level +aRssiBoost + aRssiBoost5) {
1069c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                    comp = " > ";
1070c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                }
10714dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                logDbg("attemptRoam: "
1072c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                        + b.BSSID + " rssi=" + b.level + " boost=" + Integer.toString(bRssiBoost)
107397b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle                        + "/" + Integer.toString(bRssiBoost5) + " freq=" + b.frequency
107497b9c4fef6e372d1f19b333c7a67ff27ef80baf0vandwalle                        + comp
1075c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                        + a.BSSID + " rssi=" + a.level + " boost=" + Integer.toString(aRssiBoost)
1076c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                        + "/" + Integer.toString(aRssiBoost5) + " freq=" + a.frequency);
1077c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle            }
1078c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle
10792451dbcc4f9641df188326215b204b798eb70c46vandwalle            // Compare the RSSIs after applying the hysteresis boost and the 5GHz
10802451dbcc4f9641df188326215b204b798eb70c46vandwalle            // boost if applicable
1081c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle            if (b.level + bRssiBoost + bRssiBoost5 > a.level +aRssiBoost + aRssiBoost5) {
1082931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // b is the better BSSID
1083c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                a = b;
10844dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
10854dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
10863a2a3d226881cce8a4e511302231d843b0def303vandwalle        if (a != null) {
10873a2a3d226881cce8a4e511302231d843b0def303vandwalle            if (VDBG)  {
10887806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                StringBuilder sb = new StringBuilder();
1089e67ec726c07410073575473c0f50dc737629f5davandwalle                sb.append("attemptRoam: " + current.configKey() +
1090e67ec726c07410073575473c0f50dc737629f5davandwalle                        " Found " + a.BSSID + " rssi=" + a.level + " freq=" + a.frequency);
10917806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                if (currentBSSID != null) {
10927806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                    sb.append(" Current: " + currentBSSID);
10937806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                }
10947806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                sb.append("\n");
10957806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle                logDbg(sb.toString());
10963a2a3d226881cce8a4e511302231d843b0def303vandwalle            }
10974dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle        }
10983a2a3d226881cce8a4e511302231d843b0def303vandwalle        return a;
10994dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle    }
11004dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1101931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle    /**
11022451dbcc4f9641df188326215b204b798eb70c46vandwalle     * getNetworkScore()
11032451dbcc4f9641df188326215b204b798eb70c46vandwalle     *
11042451dbcc4f9641df188326215b204b798eb70c46vandwalle     * if scorer is present, get the network score of a WifiConfiguration
11052451dbcc4f9641df188326215b204b798eb70c46vandwalle     *
11062451dbcc4f9641df188326215b204b798eb70c46vandwalle     * Note: this should be merge with setVisibility
11072451dbcc4f9641df188326215b204b798eb70c46vandwalle     *
11082451dbcc4f9641df188326215b204b798eb70c46vandwalle     * @param config
11092451dbcc4f9641df188326215b204b798eb70c46vandwalle     * @return score
11102451dbcc4f9641df188326215b204b798eb70c46vandwalle     */
111181c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson    int getConfigNetworkScore(WifiConfiguration config, int age, boolean isActive) {
11122451dbcc4f9641df188326215b204b798eb70c46vandwalle
11132451dbcc4f9641df188326215b204b798eb70c46vandwalle        if (mNetworkScoreCache == null) {
1114e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            if (VDBG) {
1115b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("       getConfigNetworkScore for " + config.configKey()
1116e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle                        + "  -> no scorer, hence no scores");
1117e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            }
11181db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle            return WifiNetworkScoreCache.INVALID_NETWORK_SCORE;
11191db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        }
11201db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        if (config.scanResultCache == null) {
1121e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            if (VDBG) {
1122b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("       getConfigNetworkScore for " + config.configKey()
1123e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle                        + " -> no scan cache");
1124e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            }
11251db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle            return WifiNetworkScoreCache.INVALID_NETWORK_SCORE;
11262451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
11272451dbcc4f9641df188326215b204b798eb70c46vandwalle
11282451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Get current date
11292451dbcc4f9641df188326215b204b798eb70c46vandwalle        long nowMs = System.currentTimeMillis();
11302451dbcc4f9641df188326215b204b798eb70c46vandwalle
11311db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        int startScore = -10000;
11321db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle
11332451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Run thru all cached scan results
11341db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        for (ScanResult result : config.scanResultCache.values()) {
11352451dbcc4f9641df188326215b204b798eb70c46vandwalle            if ((nowMs - result.seen) < age) {
113681c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                int sc = mNetworkScoreCache.getNetworkScore(result, isActive);
11371db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle                if (sc > startScore) {
11381db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle                    startScore = sc;
11392451dbcc4f9641df188326215b204b798eb70c46vandwalle                }
11402451dbcc4f9641df188326215b204b798eb70c46vandwalle            }
11412451dbcc4f9641df188326215b204b798eb70c46vandwalle        }
11421db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        if (startScore == -10000) {
11431db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle            startScore = WifiNetworkScoreCache.INVALID_NETWORK_SCORE;
11441db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        }
1145e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle        if (VDBG) {
1146e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            if (startScore == WifiNetworkScoreCache.INVALID_NETWORK_SCORE) {
1147b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    getConfigNetworkScore for " + config.configKey()
1148e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle                        + " -> no available score");
1149e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            } else {
1150b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                logDbg("    getConfigNetworkScore for " + config.configKey()
115181c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                        + " isActive=" + isActive
1152e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle                        + " score = " + Integer.toString(startScore));
1153e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle            }
1154e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle        }
1155e6574ec7b6b2e7a678da7f77bdaaf31463852b2fvandwalle
11561db63db890fcb9051f402fdfd449eb0b80e2053cvandwalle        return startScore;
11572451dbcc4f9641df188326215b204b798eb70c46vandwalle    }
11582451dbcc4f9641df188326215b204b798eb70c46vandwalle
11592451dbcc4f9641df188326215b204b798eb70c46vandwalle    /**
11608639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson     * Set whether connections to untrusted connections are allowed.
11618639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson     */
11628639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    void setAllowUntrustedConnections(boolean allow) {
11638639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson        boolean changed = mAllowUntrustedConnections != allow;
11648639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson        mAllowUntrustedConnections = allow;
11658639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson        if (changed) {
1166005c1ef113192f898499a407dd266393a8d6b076vandwalle            // Trigger a scan so as to reattempt autojoin
1167005c1ef113192f898499a407dd266393a8d6b076vandwalle            mWifiStateMachine.startScanForUntrustedSettingChange();
11688639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson        }
11698639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    }
11708639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson
11718639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    private boolean isOpenNetwork(ScanResult result) {
11728639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson        return !result.capabilities.contains("WEP") &&
11738639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                !result.capabilities.contains("PSK") &&
11748639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                !result.capabilities.contains("EAP");
11758639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    }
11768639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson
117716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson    private boolean haveRecentlySeenScoredBssid(WifiConfiguration config) {
117816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        long ephemeralOutOfRangeTimeoutMs = Settings.Global.getLong(
117916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                mContext.getContentResolver(),
118016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                Settings.Global.WIFI_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS,
118116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                DEFAULT_EPHEMERAL_OUT_OF_RANGE_TIMEOUT_MS);
118216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
118316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        // Check whether the currently selected network has a score curve. If
118416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        // ephemeralOutOfRangeTimeoutMs is <= 0, then this is all we check, and we stop here.
118516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        // Otherwise, we stop here if the currently selected network has a score. If it doesn't, we
118616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        // keep going - it could be that another BSSID is in range (has been seen recently) which
118716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        // has a score, even if the one we're immediately connected to doesn't.
118816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        ScanResult currentScanResult =  mWifiStateMachine.getCurrentScanResult();
118916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        boolean currentNetworkHasScoreCurve = mNetworkScoreCache.hasScoreCurve(currentScanResult);
119016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        if (ephemeralOutOfRangeTimeoutMs <= 0 || currentNetworkHasScoreCurve) {
119116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            if (DBG) {
119216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                if (currentNetworkHasScoreCurve) {
119316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    logDbg("Current network has a score curve, keeping network: "
119416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                            + currentScanResult);
119516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                } else {
119616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    logDbg("Current network has no score curve, giving up: " + config.SSID);
119716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                }
119816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            }
119916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            return currentNetworkHasScoreCurve;
120016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        }
120116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
120216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        if (config.scanResultCache == null || config.scanResultCache.isEmpty()) {
120316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            return false;
120416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        }
120516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
120616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        long currentTimeMs = System.currentTimeMillis();
120716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        for (ScanResult result : config.scanResultCache.values()) {
120816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            if (currentTimeMs > result.seen
120916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    && currentTimeMs - result.seen < ephemeralOutOfRangeTimeoutMs
121016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    && mNetworkScoreCache.hasScoreCurve(result)) {
121116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                if (DBG) {
121216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    logDbg("Found scored BSSID, keeping network: " + result.BSSID);
121316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                }
121416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                return true;
121516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            }
121616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        }
121716fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
121816fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        if (DBG) {
121916fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson            logDbg("No recently scored BSSID found, giving up connection: " + config.SSID);
122016fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        }
122116fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson        return false;
122216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson    }
122316fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson
12248639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson    /**
1225931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     * attemptAutoJoin() function implements the core of the a network switching algorithm
122668fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle     * Return false if no acceptable networks were found.
1227931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle     */
122868fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle    boolean attemptAutoJoin() {
122968fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle        boolean found = false;
1230c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle        didOverride = false;
12311ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle        didBailDueToWeakRssi = false;
1232b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        int networkSwitchType = AUTO_JOIN_IDLE;
12334dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
12348242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle        long now = System.currentTimeMillis();
12358242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle
12368c9088d11880553458f09377cc60d6eb7e66747bvandwalle        String lastSelectedConfiguration = mWifiConfigStore.getLastSelectedConfiguration();
12378c9088d11880553458f09377cc60d6eb7e66747bvandwalle
1238931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        // Reset the currentConfiguration Key, and set it only if WifiStateMachine and
1239453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        // supplicant agree
1240453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        mCurrentConfigurationKey = null;
1241453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        WifiConfiguration currentConfiguration = mWifiStateMachine.getCurrentWifiConfiguration();
1242453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle
1243f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        WifiConfiguration candidate = null;
1244f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1245931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        // Obtain the subset of recently seen networks
1246833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle        List<WifiConfiguration> list =
1247833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                mWifiConfigStore.getRecentConfiguredNetworks(mScanResultAutoJoinAge, false);
1248f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (list == null) {
12492f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle            if (VDBG)  logDbg("attemptAutoJoin nothing known=" +
12502f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    mWifiConfigStore.getconfiguredNetworkSize());
125168fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle            return false;
1252f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1253f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1254931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        // Find the currently connected network: ask the supplicant directly
125599d385e3b4d34841d6efcfd7cc9bf1d5ae25de14vandwalle        String val = mWifiNative.status(true);
1256f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        String status[] = val.split("\\r?\\n");
1257f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        if (VDBG) {
1258f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            logDbg("attemptAutoJoin() status=" + val + " split="
1259f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    + Integer.toString(status.length));
1260f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1261f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1262b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        int supplicantNetId = -1;
1263f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        for (String key : status) {
1264f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (key.regionMatches(0, "id=", 0, 3)) {
1265f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                int idx = 3;
1266b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle                supplicantNetId = 0;
1267f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                while (idx < key.length()) {
1268f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    char c = key.charAt(idx);
1269f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1270f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    if ((c >= 0x30) && (c <= 0x39)) {
1271b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle                        supplicantNetId *= 10;
1272b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle                        supplicantNetId += c - 0x30;
1273f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        idx++;
1274f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    } else {
1275f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        break;
1276f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
1277f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
127856d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle            } else if (key.contains("wpa_state=ASSOCIATING")
127956d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                    || key.contains("wpa_state=ASSOCIATED")
128056d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                    || key.contains("wpa_state=FOUR_WAY_HANDSHAKE")
128156d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                    || key.contains("wpa_state=GROUP_KEY_HANDSHAKE")) {
128256d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                if (DBG) {
128356d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                    logDbg("attemptAutoJoin: bail out due to sup state " + key);
128456d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                }
128556d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // After WifiStateMachine ask the supplicant to associate or reconnect
128656d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // we might still obtain scan results from supplicant
128756d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // however the supplicant state in the mWifiInfo and supplicant state tracker
128856d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // are updated when we get the supplicant state change message which can be
128956d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // processed after the SCAN_RESULT message, so at this point the framework doesn't
129056d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // know that supplicant is ASSOCIATING.
129156d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // A good fix for this race condition would be for the WifiStateMachine to add
129256d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // a new transient state where it expects to get the supplicant message indicating
129356d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // that it started the association process and within which critical operations
129456d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // like autojoin should be deleted.
129556d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle
129656d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // This transient state would remove the need for the roam Wathchdog which
129756d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // basically does that.
129856d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle
129956d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // At the moment, we just query the supplicant state synchronously with the
130056d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // mWifiNative.status() command, which allow us to know that
130156d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // supplicant has started association process, even though we didnt yet get the
130256d0178183460eed9afbd85e5c0d215e27d5f5bcvandwalle                // SUPPLICANT_STATE_CHANGE message.
130368fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle                return false;
1304f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1305f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1306ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        if (DBG) {
13077806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            String conf = "";
1308b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            String last = "";
13097806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            if (currentConfiguration != null) {
1310e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                conf = " current=" + currentConfiguration.configKey();
1311b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            }
1312b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            if (lastSelectedConfiguration != null) {
13132f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                last = " last=" + lastSelectedConfiguration;
13147806f8c800754da0f76d7a0c1a6a590381dac7a8vandwalle            }
1315ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            logDbg("attemptAutoJoin() num recent config " + Integer.toString(list.size())
1316b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + conf + last
1317b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + " ---> suppNetId=" + Integer.toString(supplicantNetId));
1318ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle        }
1319f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1320453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        if (currentConfiguration != null) {
13212451dbcc4f9641df188326215b204b798eb70c46vandwalle            if (supplicantNetId != currentConfiguration.networkId
1322005c1ef113192f898499a407dd266393a8d6b076vandwalle                    // https://b.corp.google.com/issue?id=16484607
1323005c1ef113192f898499a407dd266393a8d6b076vandwalle                    // mark this condition as an error only if the mismatched networkId are valid
13242451dbcc4f9641df188326215b204b798eb70c46vandwalle                    && supplicantNetId != WifiConfiguration.INVALID_NETWORK_ID
13252451dbcc4f9641df188326215b204b798eb70c46vandwalle                    && currentConfiguration.networkId != WifiConfiguration.INVALID_NETWORK_ID) {
1326453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                logDbg("attemptAutoJoin() ERROR wpa_supplicant out of sync nid="
1327b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle                        + Integer.toString(supplicantNetId) + " WifiStateMachine="
1328453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                        + Integer.toString(currentConfiguration.networkId));
1329b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                mWifiStateMachine.disconnectCommand();
133068fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle                return false;
13318639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson            } else if (currentConfiguration.ephemeral && (!mAllowUntrustedConnections ||
133216fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                    !haveRecentlySeenScoredBssid(currentConfiguration))) {
13338639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                // The current connection is untrusted (the framework added it), but we're either
133416fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                // no longer allowed to connect to such networks, the score has been nullified
133516fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                // since we connected, or the scored BSSID has gone out of range.
133616fdf07021858fd116d96a5fb00ddb3c166d5ae6Jeff Davidson                // Drop the current connection and perform the rest of autojoin.
13378639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                logDbg("attemptAutoJoin() disconnecting from unwanted ephemeral network");
13388639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                mWifiStateMachine.disconnectCommand();
133968fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle                return false;
1340453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            } else {
1341453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                mCurrentConfigurationKey = currentConfiguration.configKey();
1342453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle            }
13438c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle        } else {
13448c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            if (supplicantNetId != WifiConfiguration.INVALID_NETWORK_ID) {
13458c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                // Maybe in the process of associating, skip this attempt
134668fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle                return false;
13478c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            }
1348453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle        }
1349453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle
1350b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        int currentNetId = -1;
1351b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        if (currentConfiguration != null) {
1352931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // If we are associated to a configuration, it will
1353b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle            // be compared thru the compareNetwork function
1354b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle            currentNetId = currentConfiguration.networkId;
1355b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        }
1356b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle
1357931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        /**
1358931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         * Run thru all visible configurations without looking at the one we
1359c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle         * are currently associated to
13604dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle         * select Best Network candidate from known WifiConfigurations
1361931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         */
1362f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        for (WifiConfiguration config : list) {
1363e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle            if (config.SSID == null) {
1364b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                continue;
1365e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle            }
1366e86c962bb99a8b126ed64ddcc6b112161549e26dvandwalle
136727355a942653264388e909a4276196ee63e57811vandwalle            if (config.autoJoinStatus >=
136827355a942653264388e909a4276196ee63e57811vandwalle                    WifiConfiguration.AUTO_JOIN_DISABLED_ON_AUTH_FAILURE) {
1369abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                // Wait for 5 minutes before reenabling config that have known,
1370abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                // repeated connection or DHCP failures
1371abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                if (config.disableReason == WifiConfiguration.DISABLED_DHCP_FAILURE
1372abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        || config.disableReason
1373abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        == WifiConfiguration.DISABLED_ASSOCIATION_REJECT
1374abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        || config.disableReason
1375abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        == WifiConfiguration.DISABLED_AUTH_FAILURE) {
1376abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                    if (config.blackListTimestamp == 0
1377abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                            || (config.blackListTimestamp > now)) {
1378abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        // Sanitize the timestamp
1379abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        config.blackListTimestamp = now;
1380abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                    }
1381abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                    if ((now - config.blackListTimestamp) >
1382abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                            mWifiConfigStore.wifiConfigBlacklistMinTimeMilli) {
1383abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        // Re-enable the WifiConfiguration
1384abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        config.status = WifiConfiguration.Status.ENABLED;
1385abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle
1386abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        // Reset the blacklist condition
1387abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        config.numConnectionFailures = 0;
1388abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        config.numIpConfigFailures = 0;
1389abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        config.numAuthFailures = 0;
1390abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
1391abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle
1392abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        config.dirty = true;
1393abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                    } else {
1394abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        if (VDBG) {
1395abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                            long delay = mWifiConfigStore.wifiConfigBlacklistMinTimeMilli
1396abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                                    - (now - config.blackListTimestamp);
1397abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                            logDbg("attemptautoJoin " + config.configKey()
1398abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                                    + " dont unblacklist yet, waiting for "
1399abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                                    + delay + " ms");
1400abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                        }
1401abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                    }
1402abd1740f753ac14e9dec8fced8d3de5059122c2avandwalle                }
1403931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // Avoid networks disabled because of AUTH failure altogether
1404ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                if (DBG) {
1405ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                    logDbg("attemptAutoJoin skip candidate due to auto join status "
1406ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                            + Integer.toString(config.autoJoinStatus) + " key "
1407e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                            + config.configKey(true)
14088242cc81341c80ab5bc057ffdad99a3a1d95be5cvandwalle                            + " reason " + config.disableReason);
1409ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                }
1410f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                continue;
1411f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1412f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1413931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Try to un-blacklist based on elapsed time
141427355a942653264388e909a4276196ee63e57811vandwalle            if (config.blackListTimestamp > 0) {
141527355a942653264388e909a4276196ee63e57811vandwalle                if (now < config.blackListTimestamp) {
1416931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    /**
1417931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                     * looks like there was a change in the system clock since we black listed, and
1418931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                     * timestamp is not meaningful anymore, hence lose it.
1419931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                     * this event should be rare enough so that we still want to lose the black list
14202451dbcc4f9641df188326215b204b798eb70c46vandwalle                     */
142127355a942653264388e909a4276196ee63e57811vandwalle                    config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
142227355a942653264388e909a4276196ee63e57811vandwalle                } else {
14234dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                    if ((now - config.blackListTimestamp) > loseBlackListHardMilli) {
1424931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                        // Reenable it after 18 hours, i.e. next day
142527355a942653264388e909a4276196ee63e57811vandwalle                        config.setAutoJoinStatus(WifiConfiguration.AUTO_JOIN_ENABLED);
14264dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                    } else if ((now - config.blackListTimestamp) > loseBlackListSoftMilli) {
1427931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                        // Lose blacklisting due to bad link
142827355a942653264388e909a4276196ee63e57811vandwalle                        config.setAutoJoinStatus(config.autoJoinStatus - 8);
142927355a942653264388e909a4276196ee63e57811vandwalle                    }
143027355a942653264388e909a4276196ee63e57811vandwalle                }
143127355a942653264388e909a4276196ee63e57811vandwalle            }
143227355a942653264388e909a4276196ee63e57811vandwalle
1433931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Try to unblacklist based on good visibility
14348c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            if (config.visibility.rssi5 < mWifiConfigStore.thresholdUnblacklistThreshold5Soft
14358c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    && config.visibility.rssi24
14368c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    < mWifiConfigStore.thresholdUnblacklistThreshold24Soft) {
143727355a942653264388e909a4276196ee63e57811vandwalle                if (DBG) {
1438c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    logDbg("attemptAutoJoin do not unblacklist due to low visibility "
14394dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + config.autoJoinStatus
14404dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + " key " + config.configKey(true)
14414dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + " rssi=(" + config.visibility.rssi24
14424dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + "," + config.visibility.rssi5
14434dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + ") num=(" + config.visibility.num24
14444dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + "," + config.visibility.num5 + ")");
144527355a942653264388e909a4276196ee63e57811vandwalle                }
14468c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle            } else if (config.visibility.rssi5 < mWifiConfigStore.thresholdUnblacklistThreshold5Hard
14478c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    && config.visibility.rssi24
14488c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    < mWifiConfigStore.thresholdUnblacklistThreshold24Hard) {
1449931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // If the network is simply temporary disabled, don't allow reconnect until
1450931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // RSSI becomes good enough
145127355a942653264388e909a4276196ee63e57811vandwalle                config.setAutoJoinStatus(config.autoJoinStatus - 1);
145227355a942653264388e909a4276196ee63e57811vandwalle                if (DBG) {
145327355a942653264388e909a4276196ee63e57811vandwalle                    logDbg("attemptAutoJoin good candidate seen, bumped soft -> status="
145427355a942653264388e909a4276196ee63e57811vandwalle                            + config.autoJoinStatus
1455b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + " " + config.configKey(true) + " rssi=("
14564dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + config.visibility.rssi24 + "," + config.visibility.rssi5
14574dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + ") num=(" + config.visibility.num24
14584dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + "," + config.visibility.num5 + ")");
145927355a942653264388e909a4276196ee63e57811vandwalle                }
146027355a942653264388e909a4276196ee63e57811vandwalle            } else {
1461c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                config.setAutoJoinStatus(config.autoJoinStatus - 3);
146227355a942653264388e909a4276196ee63e57811vandwalle                if (DBG) {
146327355a942653264388e909a4276196ee63e57811vandwalle                    logDbg("attemptAutoJoin good candidate seen, bumped hard -> status="
146427355a942653264388e909a4276196ee63e57811vandwalle                            + config.autoJoinStatus
1465b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + " " + config.configKey(true) + " rssi=("
14664dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + config.visibility.rssi24 + "," + config.visibility.rssi5
14674dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + ") num=(" + config.visibility.num24
14684dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + "," + config.visibility.num5 + ")");
146927355a942653264388e909a4276196ee63e57811vandwalle                }
147027355a942653264388e909a4276196ee63e57811vandwalle            }
147127355a942653264388e909a4276196ee63e57811vandwalle
147227355a942653264388e909a4276196ee63e57811vandwalle            if (config.autoJoinStatus >=
147327355a942653264388e909a4276196ee63e57811vandwalle                    WifiConfiguration.AUTO_JOIN_TEMPORARY_DISABLED) {
1474931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                // Network is blacklisted, skip
14754dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                if (DBG) {
14764dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                    logDbg("attemptAutoJoin skip blacklisted -> status="
14774dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + config.autoJoinStatus
1478b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            + " " + config.configKey(true) + " rssi=("
14794dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + config.visibility.rssi24 + "," + config.visibility.rssi5
14804dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + ") num=(" + config.visibility.num24
14814dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + "," + config.visibility.num5 + ")");
14824dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                }
148327355a942653264388e909a4276196ee63e57811vandwalle                continue;
148427355a942653264388e909a4276196ee63e57811vandwalle            }
1485f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (config.networkId == currentNetId) {
1486ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                if (DBG) {
148721bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle                    logDbg("attemptAutoJoin skip current candidate  "
148821bc54cb37a0085b1c909cb4d55ebb12a2facefbvandwalle                            + Integer.toString(currentNetId)
1489ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                            + " key " + config.configKey(true));
1490ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle                }
1491f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                continue;
1492f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1493f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
14940eebae7334d6129f7ca1344e4b20199794994358vandwalle            boolean isLastSelected = false;
14950eebae7334d6129f7ca1344e4b20199794994358vandwalle            if (lastSelectedConfiguration != null &&
14960eebae7334d6129f7ca1344e4b20199794994358vandwalle                    config.configKey().equals(lastSelectedConfiguration)) {
14970eebae7334d6129f7ca1344e4b20199794994358vandwalle                isLastSelected = true;
14980eebae7334d6129f7ca1344e4b20199794994358vandwalle            }
14990eebae7334d6129f7ca1344e4b20199794994358vandwalle
15000eebae7334d6129f7ca1344e4b20199794994358vandwalle            if (config.visibility == null) {
15010eebae7334d6129f7ca1344e4b20199794994358vandwalle                continue;
15020eebae7334d6129f7ca1344e4b20199794994358vandwalle            }
15031ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            int boost = config.autoJoinUseAggressiveJoinAttemptThreshold + weakRssiBailCount;
15040eebae7334d6129f7ca1344e4b20199794994358vandwalle            if ((config.visibility.rssi5 + boost)
15050eebae7334d6129f7ca1344e4b20199794994358vandwalle                        < mWifiConfigStore.thresholdInitialAutoJoinAttemptMin5RSSI
15060eebae7334d6129f7ca1344e4b20199794994358vandwalle                        && (config.visibility.rssi24 + boost)
15070eebae7334d6129f7ca1344e4b20199794994358vandwalle                        < mWifiConfigStore.thresholdInitialAutoJoinAttemptMin24RSSI) {
15080eebae7334d6129f7ca1344e4b20199794994358vandwalle                if (DBG) {
15090eebae7334d6129f7ca1344e4b20199794994358vandwalle                    logDbg("attemptAutoJoin skip due to low visibility -> status="
15100eebae7334d6129f7ca1344e4b20199794994358vandwalle                            + config.autoJoinStatus
15110eebae7334d6129f7ca1344e4b20199794994358vandwalle                            + " key " + config.configKey(true) + " rssi="
15120eebae7334d6129f7ca1344e4b20199794994358vandwalle                            + config.visibility.rssi24 + ", " + config.visibility.rssi5
15130eebae7334d6129f7ca1344e4b20199794994358vandwalle                            + " num=" + config.visibility.num24
15140eebae7334d6129f7ca1344e4b20199794994358vandwalle                            + ", " + config.visibility.num5);
15150eebae7334d6129f7ca1344e4b20199794994358vandwalle                }
15160eebae7334d6129f7ca1344e4b20199794994358vandwalle
1517c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                // Don't try to autojoin a network that is too far but
1518c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                // If that configuration is a user's choice however, try anyway
15190eebae7334d6129f7ca1344e4b20199794994358vandwalle                if (!isLastSelected) {
15200eebae7334d6129f7ca1344e4b20199794994358vandwalle                    config.autoJoinBailedDueToLowRssi = true;
15211ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                    didBailDueToWeakRssi = true;
15228c9088d11880553458f09377cc60d6eb7e66747bvandwalle                    continue;
15230eebae7334d6129f7ca1344e4b20199794994358vandwalle                } else {
15240eebae7334d6129f7ca1344e4b20199794994358vandwalle                    // Next time, try to be a bit more aggressive in auto-joining
15250eebae7334d6129f7ca1344e4b20199794994358vandwalle                    if (config.autoJoinUseAggressiveJoinAttemptThreshold
1526dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                            < WifiConfiguration.MAX_INITIAL_AUTO_JOIN_RSSI_BOOST
1527dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                            && config.autoJoinBailedDueToLowRssi) {
1528dd0c558776fcfba3f754bb0cd6533f2c9c23ec1evandwalle                        config.autoJoinUseAggressiveJoinAttemptThreshold += 4;
15294dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                    }
15308c9088d11880553458f09377cc60d6eb7e66747bvandwalle                }
15310eebae7334d6129f7ca1344e4b20199794994358vandwalle            }
1532d30127db46224e45554f8964209221bba8ad41d9vandwalle            if (config.numNoInternetAccessReports > 0
1533d30127db46224e45554f8964209221bba8ad41d9vandwalle                    && !isLastSelected
1534d30127db46224e45554f8964209221bba8ad41d9vandwalle                    && !config.validatedInternetAccess) {
1535d30127db46224e45554f8964209221bba8ad41d9vandwalle                // Avoid autoJoining this network because last time we used it, it didn't
1536d30127db46224e45554f8964209221bba8ad41d9vandwalle                // have internet access, and we never manage to validate internet access on this
1537d30127db46224e45554f8964209221bba8ad41d9vandwalle                // network configuration
15380eebae7334d6129f7ca1344e4b20199794994358vandwalle                if (DBG) {
1539d30127db46224e45554f8964209221bba8ad41d9vandwalle                    logDbg("attemptAutoJoin skip candidate due to no InternetAccess  "
1540d30127db46224e45554f8964209221bba8ad41d9vandwalle                            + config.configKey(true)
1541d30127db46224e45554f8964209221bba8ad41d9vandwalle                            + " num reports " + config.numNoInternetAccessReports);
15429f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                }
15430eebae7334d6129f7ca1344e4b20199794994358vandwalle                continue;
15448c9088d11880553458f09377cc60d6eb7e66747bvandwalle            }
15458c9088d11880553458f09377cc60d6eb7e66747bvandwalle
1546ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            if (DBG) {
1547e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                String cur = "";
1548e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                if (candidate != null) {
1549e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                    cur = " current candidate " + candidate.configKey();
1550e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                }
1551e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                logDbg("attemptAutoJoin trying id="
1552c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                        + Integer.toString(config.networkId) + " "
1553b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                        + config.configKey(true)
1554e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + " status=" + config.autoJoinStatus
1555e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle                        + cur);
1556ed9938883ae2dade81c8be6cd6ceaef3febd5239vandwalle            }
1557f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1558f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (candidate == null) {
1559f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                candidate = config;
1560f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            } else {
1561f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (VDBG)  {
1562453aee50caf7e332e77ab3d995d7c87a958e4fd4vandwalle                    logDbg("attemptAutoJoin will compare candidate  " + candidate.configKey()
15634dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle                            + " with " + config.configKey());
1564f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
1565833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                int order = compareWifiConfigurations(candidate, config);
15662451dbcc4f9641df188326215b204b798eb70c46vandwalle
15672451dbcc4f9641df188326215b204b798eb70c46vandwalle                // The lastSelectedConfiguration is the configuration the user has manually selected
1568c290d8dff6172d5fde7b9dfd74d3a20785dab246vandwalle                // thru WifiPicker, or that a 3rd party app asked us to connect to via the
15692451dbcc4f9641df188326215b204b798eb70c46vandwalle                // enableNetwork with disableOthers=true WifiManager API
15702451dbcc4f9641df188326215b204b798eb70c46vandwalle                // As this is a direct user choice, we strongly prefer this configuration,
15712451dbcc4f9641df188326215b204b798eb70c46vandwalle                // hence give +/-100
15722451dbcc4f9641df188326215b204b798eb70c46vandwalle                if ((lastSelectedConfiguration != null)
15732451dbcc4f9641df188326215b204b798eb70c46vandwalle                        && candidate.configKey().equals(lastSelectedConfiguration)) {
15742451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // candidate is the last selected configuration,
15752451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // so keep it above connect choices (+/-60) and
15762451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // above RSSI/scorer based selection of linked configuration (+/- 50)
15772451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // by reducing order by -100
15782451dbcc4f9641df188326215b204b798eb70c46vandwalle                    order = order - 100;
15792451dbcc4f9641df188326215b204b798eb70c46vandwalle                    if (VDBG)   {
15802f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        logDbg("     ...and prefers -100 " + candidate.configKey()
15812451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + " over " + config.configKey()
15822451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + " because it is the last selected -> "
15832451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + Integer.toString(order));
15842451dbcc4f9641df188326215b204b798eb70c46vandwalle                    }
15852451dbcc4f9641df188326215b204b798eb70c46vandwalle                } else if ((lastSelectedConfiguration != null)
15862451dbcc4f9641df188326215b204b798eb70c46vandwalle                        && config.configKey().equals(lastSelectedConfiguration)) {
15872451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // config is the last selected configuration,
15882451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // so keep it above connect choices (+/-60) and
15892451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // above RSSI/scorer based selection of linked configuration (+/- 50)
15902451dbcc4f9641df188326215b204b798eb70c46vandwalle                    // by increasing order by +100
15912451dbcc4f9641df188326215b204b798eb70c46vandwalle                    order = order + 100;
15922451dbcc4f9641df188326215b204b798eb70c46vandwalle                    if (VDBG)   {
15932f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        logDbg("     ...and prefers +100 " + config.configKey()
15942451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + " over " + candidate.configKey()
15952451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + " because it is the last selected -> "
15962451dbcc4f9641df188326215b204b798eb70c46vandwalle                                + Integer.toString(order));
15972451dbcc4f9641df188326215b204b798eb70c46vandwalle                    }
15982451dbcc4f9641df188326215b204b798eb70c46vandwalle                }
15992451dbcc4f9641df188326215b204b798eb70c46vandwalle
1600f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                if (order > 0) {
1601931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle                    // Ascending : candidate < config
1602f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    candidate = config;
1603f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
1604f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1605f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1606f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
16072451dbcc4f9641df188326215b204b798eb70c46vandwalle        // Now, go thru scan result to try finding a better untrusted network
1608833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle        if (mNetworkScoreCache != null && mAllowUntrustedConnections) {
1609f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            int rssi5 = WifiConfiguration.INVALID_RSSI;
1610f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            int rssi24 = WifiConfiguration.INVALID_RSSI;
1611f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            if (candidate != null) {
1612f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                rssi5 = candidate.visibility.rssi5;
1613f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                rssi24 = candidate.visibility.rssi24;
1614f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1615f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1616931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Get current date
16172451dbcc4f9641df188326215b204b798eb70c46vandwalle            long nowMs = System.currentTimeMillis();
1618c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            int currentScore = -10000;
1619c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            // The untrusted network with highest score
1620c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            ScanResult untrustedCandidate = null;
16212451dbcc4f9641df188326215b204b798eb70c46vandwalle            // Look for untrusted scored network only if the current candidate is bad
1622c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            if (isBadCandidate(rssi24, rssi5)) {
1623f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                for (ScanResult result : scanResultCache.values()) {
1624c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    // We look only at untrusted networks with a valid SSID
1625c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    // A trusted result would have been looked at thru it's Wificonfiguration
16268639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                    if (TextUtils.isEmpty(result.SSID) || !result.untrusted ||
16278639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                            !isOpenNetwork(result)) {
1628c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        continue;
1629c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    }
16302ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle                    if (mWifiConfigStore.mDeletedEphemeralSSIDs.contains
16312ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle                            ("\"" + result.SSID + "\"")) {
16322ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle                        // SSID had been Forgotten by user, then don't score it
16332ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle                        continue;
16342ce99b40c36ed0352b31aa85d5f9383d5f0506f5vandwalle                    }
1635833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                    if ((nowMs - result.seen) < mScanResultAutoJoinAge) {
1636c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        // Increment usage count for the network
1637c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        mWifiConnectionStatistics.incrementOrAddUntrusted(result.SSID, 0, 1);
1638c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle
163981c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                        boolean isActiveNetwork = lastUntrustedBSSID != null
164081c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                                && result.BSSID.equals(lastUntrustedBSSID);
164181c9ea6c343bc7f8d87095237e59844a974d0b70Jeff Davidson                        int score = mNetworkScoreCache.getNetworkScore(result, isActiveNetwork);
1642c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        if (score != WifiNetworkScoreCache.INVALID_NETWORK_SCORE
1643c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                                && score > currentScore) {
1644c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            // Highest score: Select this candidate
1645c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            currentScore = score;
1646c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            untrustedCandidate = result;
1647c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            if (VDBG) {
1648c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                                logDbg("AutoJoinController: found untrusted candidate "
1649c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                                        + result.SSID
1650c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                                + " RSSI=" + result.level
1651c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                                + " freq=" + result.frequency
1652c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                                + " score=" + score);
1653c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                            }
1654f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                        }
1655f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                    }
1656f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle                }
1657f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1658c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            if (untrustedCandidate != null) {
1659c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                if (lastUntrustedBSSID == null
1660c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                        || !untrustedCandidate.SSID.equals(lastUntrustedBSSID)) {
1661c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    // We found a new candidate that we are going to connect to, then
1662c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    // increase its connection count
16638c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                    mWifiConnectionStatistics.
16648c0a54e9b0d3713cab52d06ad8fd7f3a1b6f73a8vandwalle                            incrementOrAddUntrusted(untrustedCandidate.SSID, 1, 0);
1665c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    // Remember which SSID we are connecting to
1666c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                    lastUntrustedBSSID = untrustedCandidate.SSID;
1667c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                }
16688639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson
16698639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                // At this point, we have an untrusted network candidate.
16708639f6266cb70bf92d1561af43ac2d7b2b97298eJeff Davidson                // Create the new ephemeral configuration and see if we should switch over
1671833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                candidate =
1672833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                        mWifiConfigStore.wifiConfigurationFromScanResult(untrustedCandidate);
1673833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                candidate.allowedKeyManagement.set(KeyMgmt.NONE);
1674833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                candidate.ephemeral = true;
1675c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle            }
1676c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle        }
1677b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle
16781ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle        long lastUnwanted =
16791ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                System.currentTimeMillis()
16801ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                        - mWifiConfigStore.lastUnwantedNetworkDisconnectTimestamp;
16811ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle        if (candidate == null
16821ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                && lastSelectedConfiguration == null
16831ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                && currentConfiguration == null
16841ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                && didBailDueToWeakRssi
16851ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                && (mWifiConfigStore.lastUnwantedNetworkDisconnectTimestamp == 0
16861ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                    || lastUnwanted > (1000 * 60 * 60 * 24 * 7))
16871ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                ) {
16881ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // We are bailing out of autojoin although we are seeing a weak configuration, and
16891ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // - we didn't find another valid candidate
16901ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // - we are not connected
16911ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // - without a user network selection choice
16921ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // - ConnectivityService has not triggered an unwanted network disconnect
16931ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            //       on this device for a week (hence most likely there is no SIM card or cellular)
16941ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // If all those conditions are met, then boost the RSSI of the weak networks
16951ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            // that we are seeing so as we will eventually pick one
16961ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            if (weakRssiBailCount < 10)
16971ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                weakRssiBailCount += 1;
16981ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle        } else {
16991ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle            if (weakRssiBailCount > 0)
17001ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                weakRssiBailCount -= 1;
17011ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle        }
17021ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle
1703931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        /**
1704931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         *  If candidate is found, check the state of the connection so as
1705931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         *  to decide if we should be acting on this candidate and switching over
1706931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         */
1707e0aa0a004d161173992a0e9af1b431fae91f4a71vandwalle        int networkDelta = compareNetwork(candidate, lastSelectedConfiguration);
1708b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle        if (DBG && candidate != null) {
1709b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            String doSwitch = "";
1710b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            String current = "";
1711b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            if (networkDelta < 0) {
1712b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                doSwitch = " -> not switching";
1713b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            }
1714b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            if (currentConfiguration != null) {
17152f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                current = " with current " + currentConfiguration.configKey();
1716b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            }
1717b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle            logDbg("attemptAutoJoin networkSwitching candidate "
1718b57df70bdf17ba45ef4d18b11414cb24dcbe1fb9vandwalle                    + candidate.configKey()
1719b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + current
1720c6f06c628ee3583b60ff31a7da442e0ac7b26d97vandwalle                    + " linked=" + (currentConfiguration != null
1721b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                            && currentConfiguration.isLinked(candidate))
1722b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + " : delta="
1723b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + Integer.toString(networkDelta) + " "
1724b664cfeab6f02e24376ea0a15beb83d142f0b14dvandwalle                    + doSwitch);
1725b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        }
17264dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1727931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle        /**
1728931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         * Ask WifiStateMachine permission to switch :
1729931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         * if user is currently streaming voice traffic,
1730931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         * then we should not be allowed to switch regardless of the delta
1731931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle         */
1732b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        if (mWifiStateMachine.shouldSwitchNetwork(networkDelta)) {
1733b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            if (mStaStaSupported) {
1734b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                logDbg("mStaStaSupported --> error do nothing now ");
1735b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle            } else {
1736b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                if (currentConfiguration != null && currentConfiguration.isLinked(candidate)) {
1737b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    networkSwitchType = AUTO_JOIN_EXTENDED_ROAMING;
1738b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                } else {
1739b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    networkSwitchType = AUTO_JOIN_OUT_OF_NETWORK_ROAMING;
1740b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                }
1741b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                if (DBG) {
1742b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    logDbg("AutoJoin auto connect with netId "
1743b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + Integer.toString(candidate.networkId)
1744b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + " to " + candidate.configKey());
1745b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                }
17462451dbcc4f9641df188326215b204b798eb70c46vandwalle                if (didOverride) {
17472451dbcc4f9641df188326215b204b798eb70c46vandwalle                    candidate.numScorerOverrideAndSwitchedNetwork++;
17482451dbcc4f9641df188326215b204b798eb70c46vandwalle                }
1749c298087de50ea56c31a4ade7ee1e83b313bb63c7vandwalle                candidate.numAssociation++;
1750e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                mWifiConnectionStatistics.numAutoJoinAttempt++;
17519f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle
17522f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                if (candidate.BSSID == null || candidate.BSSID.equals("any")) {
17532f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    // First step we selected the configuration we want to connect to
17542f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    // Second step: Look for the best Scan result for this configuration
17552f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    // TODO this algorithm should really be done in one step
17562f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    String currentBSSID = mWifiStateMachine.getCurrentBSSID();
1757833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                    ScanResult roamCandidate =
1758833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                            attemptRoam(null, candidate, mScanResultAutoJoinAge, null);
17592f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    if (roamCandidate != null && currentBSSID != null
17602f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                            && currentBSSID.equals(roamCandidate.BSSID)) {
17612f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        // Sanity, we were already asociated to that candidate
17622f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        roamCandidate = null;
17632f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    }
17642f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                    if (roamCandidate != null && roamCandidate.is5GHz()) {
17652f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        // If the configuration hasn't a default BSSID selected, and the best
17662f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        // candidate is 5GHZ, then select this candidate so as WifiStateMachine and
17672f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        // supplicant will pick it first
17682f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        candidate.autoJoinBSSID = roamCandidate.BSSID;
17692f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        if (VDBG) {
17702f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                            logDbg("AutoJoinController: lock to 5GHz "
17712f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                                    + candidate.autoJoinBSSID
17722f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                                    + " RSSI=" + roamCandidate.level
17732f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                                    + " freq=" + roamCandidate.frequency);
17742f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle                        }
1775448c9536a302c58a79e271b1721c08b8882f800evandwalle                    } else {
1776448c9536a302c58a79e271b1721c08b8882f800evandwalle                        // We couldnt find a roam candidate
1777448c9536a302c58a79e271b1721c08b8882f800evandwalle                        candidate.autoJoinBSSID = "any";
17789f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                    }
17799f3349fa2cd39d690d1e2b7c3b71ced412e24f2cvandwalle                }
1780448c9536a302c58a79e271b1721c08b8882f800evandwalle                mWifiStateMachine.sendMessage(WifiStateMachine.CMD_AUTO_CONNECT,
1781448c9536a302c58a79e271b1721c08b8882f800evandwalle                            candidate.networkId, networkSwitchType, candidate);
178268fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle                found = true;
17834dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle            }
1784b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        }
17854dc6f3a322806b25d50039614cde1b94fe91ab17vandwalle
1786b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        if (networkSwitchType == AUTO_JOIN_IDLE) {
1787e67ec726c07410073575473c0f50dc737629f5davandwalle            String currentBSSID = mWifiStateMachine.getCurrentBSSID();
1788931338d1533d1bd11ba0e5aebb4e4b7b2c8ab056vandwalle            // Attempt same WifiConfiguration roaming
1789833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle            ScanResult roamCandidate =
1790833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                    attemptRoam(null, currentConfiguration, mScanResultAutoJoinAge, currentBSSID);
1791e67ec726c07410073575473c0f50dc737629f5davandwalle            /**
1792e67ec726c07410073575473c0f50dc737629f5davandwalle             *  TODO: (post L initial release)
1793e67ec726c07410073575473c0f50dc737629f5davandwalle             *  consider handling linked configurations roaming (i.e. extended Roaming)
1794e67ec726c07410073575473c0f50dc737629f5davandwalle             *  thru the attemptRoam function which makes use of the RSSI roaming threshold.
1795e67ec726c07410073575473c0f50dc737629f5davandwalle             *  At the moment, extended roaming is only handled thru the attemptAutoJoin()
1796e67ec726c07410073575473c0f50dc737629f5davandwalle             *  function which compare configurations.
1797e67ec726c07410073575473c0f50dc737629f5davandwalle             *
1798e67ec726c07410073575473c0f50dc737629f5davandwalle             *  The advantage of making use of attemptRoam function is that this function
1799e67ec726c07410073575473c0f50dc737629f5davandwalle             *  will looks at all the BSSID of each configurations, instead of only looking
1800e67ec726c07410073575473c0f50dc737629f5davandwalle             *  at WifiConfiguration.visibility which keeps trackonly of the RSSI/band of the
1801e67ec726c07410073575473c0f50dc737629f5davandwalle             *  two highest BSSIDs.
1802e67ec726c07410073575473c0f50dc737629f5davandwalle             */
1803e67ec726c07410073575473c0f50dc737629f5davandwalle            // Attempt linked WifiConfiguration roaming
1804e67ec726c07410073575473c0f50dc737629f5davandwalle            /* if (currentConfiguration != null
1805e67ec726c07410073575473c0f50dc737629f5davandwalle                    && currentConfiguration.linkedConfigurations != null) {
1806e67ec726c07410073575473c0f50dc737629f5davandwalle                for (String key : currentConfiguration.linkedConfigurations.keySet()) {
1807e67ec726c07410073575473c0f50dc737629f5davandwalle                    WifiConfiguration link = mWifiConfigStore.getWifiConfiguration(key);
1808e67ec726c07410073575473c0f50dc737629f5davandwalle                    if (link != null) {
1809833dcce8f6712f7594f06ea33208e3e106c15afcvandwalle                        roamCandidate = attemptRoam(roamCandidate, link, mScanResultAutoJoinAge,
1810e67ec726c07410073575473c0f50dc737629f5davandwalle                                currentBSSID);
1811e67ec726c07410073575473c0f50dc737629f5davandwalle                    }
1812e67ec726c07410073575473c0f50dc737629f5davandwalle                }
1813e67ec726c07410073575473c0f50dc737629f5davandwalle            }*/
1814e67ec726c07410073575473c0f50dc737629f5davandwalle            if (roamCandidate != null && currentBSSID != null
1815e67ec726c07410073575473c0f50dc737629f5davandwalle                    && currentBSSID.equals(roamCandidate.BSSID)) {
1816e67ec726c07410073575473c0f50dc737629f5davandwalle                roamCandidate = null;
1817e67ec726c07410073575473c0f50dc737629f5davandwalle            }
18182f2cf21662275a0e93d7d7a6ad3d98b4c596dcf0vandwalle            if (roamCandidate != null && mWifiStateMachine.shouldSwitchNetwork(999)) {
1819b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                if (DBG) {
1820b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                    logDbg("AutoJoin auto roam with netId "
1821b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + Integer.toString(currentConfiguration.networkId)
1822b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + " " + currentConfiguration.configKey() + " to BSSID="
1823b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                            + roamCandidate.BSSID + " freq=" + roamCandidate.frequency
18241ec92c57244311c7fca3ab6b244a06c2b2b58902vandwalle                            + " RSSI=" + roamCandidate.level);
1825b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                }
1826b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle                networkSwitchType = AUTO_JOIN_ROAMING;
1827e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle                mWifiConnectionStatistics.numAutoRoamAttempt++;
1828e8c89583e489d451880471b7cc7659bd9fa802f4vandwalle
1829b07da189850a4bfa268f8ab9be7867935eb2ecb5vandwalle                mWifiStateMachine.sendMessage(WifiStateMachine.CMD_AUTO_ROAM,
1830b07da189850a4bfa268f8ab9be7867935eb2ecb5vandwalle                            currentConfiguration.networkId, 1, roamCandidate);
183168fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle                found = true;
1832f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle            }
1833f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle        }
1834b97e66604f472f67c233bb8f8d9630bb36131e2cvandwalle        if (VDBG) logDbg("Done attemptAutoJoin status=" + Integer.toString(networkSwitchType));
183568fee36dac1dda5c596c00ef33fdbc0962e9ec9fvandwalle        return found;
1836f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle    }
1837f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle}
1838f22d23092ab37286a5ef9d257d5bb32c421d2669vandwalle
1839