165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/* 265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project 365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * 465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License"); 565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License. 665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at 765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * 865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * http://www.apache.org/licenses/LICENSE-2.0 965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * 1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software 1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS, 1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and 1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License. 1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */ 1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.connectivity; 1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.Comparator; 2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.ScanResult; 2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/** 2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Comparator that sorts Wifi scan results by signal strength and network name. 2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */ 2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class ScanResultComparator implements Comparator<ScanResult> { 2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private final String mConnectedSSID; 2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private final WifiSecurity mConnectedSecurity; 3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public ScanResultComparator(String connectedSSID, WifiSecurity connectedSecurity) { 3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mConnectedSSID = connectedSSID; 3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mConnectedSecurity = connectedSecurity; 3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public ScanResultComparator() { 3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mConnectedSSID = null; 3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mConnectedSecurity = WifiSecurity.NONE; 3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane @Override 4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public int compare(ScanResult result1, ScanResult result2) { 4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (result1 == null) { 4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (result2 == null) { 4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return 0; 4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } else { 4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return 1; 4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } else { 5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (result2 == null) { 5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return -1; 5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } else { 5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane WifiSecurity security1 = WifiSecurity.getSecurity(result1); 5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane WifiSecurity security2 = WifiSecurity.getSecurity(result2); 5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (mConnectedSSID != null) { 5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (result1.SSID.equals(mConnectedSSID) 5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane && security1.equals(mConnectedSecurity)) { 5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return -1; 5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (result2.SSID.equals(mConnectedSSID) 6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane && security2.equals(mConnectedSecurity)) { 6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return 1; 6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane int levelDiff = result2.level - result1.level; 6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (levelDiff != 0) { 6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return levelDiff; 6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (result1.SSID.equals(result2.SSID)) { 7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return security1.compareTo(security2); 7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return result1.SSID.compareTo(result2.SSID); 7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane} 77