From db218ba7122453097b696d45bc3a43d9d05ade9c Mon Sep 17 00:00:00 2001 From: isuPatches Date: Sun, 2 Jul 2017 14:57:17 -0500 Subject: [PATCH] Add more tests, switch to String.format, update more libs, and restore travis-ci functionality --- build.gradle | 4 +- .../com/isupatches/wisefy/WiseFyTest.java | 226 ++++++++++++++++-- .../com/isupatches/wisefy/base/TestUtils.java | 8 +- .../java/com/isupatches/wisefy/WiseFy.java | 94 ++++---- 4 files changed, 264 insertions(+), 68 deletions(-) diff --git a/build.gradle b/build.gradle index 0b9440d7..a7ce65d1 100644 --- a/build.gradle +++ b/build.gradle @@ -6,8 +6,8 @@ buildscript { } } dependencies { - classpath 'com.android.tools.build:gradle:2.3.0' - classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.3' + classpath 'com.android.tools.build:gradle:2.3.2' + classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.7.1' classpath "com.github.dcendents:android-maven-gradle-plugin:1.5" classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' classpath "org.jacoco:org.jacoco.core:0.7.8" diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/WiseFyTest.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/WiseFyTest.java index 72b79cfb..c5c83d10 100644 --- a/wisefy/src/androidTest/java/com/isupatches/wisefy/WiseFyTest.java +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/WiseFyTest.java @@ -53,7 +53,6 @@ public void addOpenNetwork_failure() { when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(WiseFy.WIFI_MANAGER_FAILURE); int result = mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID); assertEquals(WiseFy.WIFI_MANAGER_FAILURE, result); - assertEquals(1, 2); } @Test @@ -77,6 +76,25 @@ public void addOpenNetwork_success() { assertNotEquals(WiseFy.WIFI_MANAGER_FAILURE, result); } + @Test + public void addOpenNetwork_success_alreadyConfigured() { + setManagers(); + + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = OPEN_NETWORK_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(OPEN_NETWORK_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + int result = mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID); + assertNotEquals(WiseFy.WIFI_MANAGER_FAILURE, result); + } + @Test public void addWEPNetwork_failure() { setManagers(); @@ -112,6 +130,24 @@ public void addWEPNetwork_success() { assertNotEquals(WiseFy.WIFI_MANAGER_FAILURE, result); } + @Test + public void addWEPNetwork_success_alreadyConfigured() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = WEP_NETWORK_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(WEP_NETWORK_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + int result = mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD); + assertNotEquals(WiseFy.WIFI_MANAGER_FAILURE, result); + } + @Test public void addWPA2Network_failure() { when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(WiseFy.WIFI_MANAGER_FAILURE); @@ -146,6 +182,26 @@ public void addWPA2Network_success() { assertNotEquals(WiseFy.WIFI_MANAGER_FAILURE, result); } + @Test + public void addWPA2Network_success_alreadyConfigured() { + setManagers(); + + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = WPA2_NETWORK_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(WPA2_NETWORK_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + int result = mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD); + assertNotEquals(WiseFy.WIFI_MANAGER_FAILURE, result); + } + @Test public void builder_loggingFalse() { WiseFy wiseFy = new WiseFy.withContext(mActivityTestRule.getActivity()).logging(false).getSmarts(); @@ -171,8 +227,9 @@ public void compareSignalLevel() { } @Test - public void connectToNetwork_failure_noList() { + public void connectToNetwork_failure_emptyList() { setManagers(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(new ArrayList()); boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); assertEquals(false, result); } @@ -225,6 +282,14 @@ public void connectToNetwork_failure_notConnected() { assertEquals(false, result); } + @Test + public void connectToNetwork_failure_nullList() { + setManagers(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(null); + boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); + assertEquals(false, result); + } + @Test public void connectToNetwork_failure_nullWifiManager() { setManagersToNull(); @@ -362,10 +427,10 @@ public void getFrequency_currentNetwork_success() { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { setManagers(); WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_24GHZ); when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); - assertEquals(TEST_NETWORK_FREQUENCY, mWiseFy.getFrequency()); + assertEquals(TEST_NETWORK_FREQUENCY_24GHZ, mWiseFy.getFrequency()); } } @@ -381,10 +446,10 @@ public void getFrequency_provideWifiInfo_success() { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { setManagers(); WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_24GHZ); when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); - assertEquals(TEST_NETWORK_FREQUENCY, mWiseFy.getFrequency(mockWifiInfo)); + assertEquals(TEST_NETWORK_FREQUENCY_24GHZ, mWiseFy.getFrequency(mockWifiInfo)); } } @@ -536,7 +601,16 @@ public void isDeviceConnectedToMobileNetwork_failure_notConnected() { @Test public void isDeviceConnectedToMobileNetwork_failure_nullConfigurationManager() { - setManagersToNull(); + setManagers(); + setConnectivityManagerToNull(); + boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); + assertEquals(false, result); + } + + @Test + public void isDeviceConnectedToMobileNetwork_failure_nullActiveNetworkInfo() { + setManagers(); + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(null); boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); assertEquals(false, result); } @@ -685,7 +759,7 @@ public void isDeviceConnectedToSSID_failure_differentSSID() { } @Test - public void isDeviceConnectedToSSID_failure_NotAvailable() { + public void isDeviceConnectedToSSID_failure_notAvailable() { setManagers(); List wifiList = new ArrayList<>(); WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); @@ -709,7 +783,7 @@ public void isDeviceConnectedToSSID_failure_NotAvailable() { } @Test - public void isDeviceConnectedToSSID_failure_NotConnected() { + public void isDeviceConnectedToSSID_failure_notConnected() { setManagers(); List wifiList = new ArrayList<>(); WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); @@ -733,7 +807,79 @@ public void isDeviceConnectedToSSID_failure_NotConnected() { } @Test - public void isDeviceConnectedToSSID_failure_NullWifiManager() { + public void isDeviceConnectedToSSID_failure_nullActiveNetworkInfo() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(null); + + boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); + assertEquals(false, result); + } + + @Test + public void isDeviceConnectedToSSID_failure_nullConnectionInfo() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(null); + + boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); + assertEquals(false, result); + } + + @Test + public void isDeviceConnectedToSSID_failure_nullConnectionInfoSSID() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(null); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); + assertEquals(false, result); + } + + @Test + public void isDeviceConnectedToSSID_failure_nullConnectivityManagerManager() { + setManagers(); + setConnectivityManagerToNull(); + + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); + assertEquals(false, result); + } + + @Test + public void isDeviceConnectedToSSID_failure_nullWifiManager() { setManagersToNull(); boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); assertEquals(false, result); @@ -791,9 +937,18 @@ public void isDeviceConnectedToWifiNetwork_failure_notConnected() { assertEquals(false, result); } + @Test + public void isDeviceConnectedToWifiNetwork_failure_nullActiveNetworkInfo() { + setManagers(); + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(null); + boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); + assertEquals(false, result); + } + @Test public void isDeviceConnectedToWifiNetwork_failure_nullConfigurationManager() { - setManagersToNull(); + setManagers(); + setConnectivityManagerToNull(); boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); assertEquals(false, result); } @@ -813,7 +968,7 @@ public void isDeviceConnectedToWifiNetwork_failure_wrongType() { } @Test - public void isDeviceConnectedToWifiNetwork_Success() { + public void isDeviceConnectedToWifiNetwork_success() { setManagers(); NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); when(mockNetworkInfo.getTypeName()).thenReturn("WIFI"); @@ -827,11 +982,23 @@ public void isDeviceConnectedToWifiNetwork_Success() { } @Test - public void isNetwork5gHz_currentNetwork_failure() { + public void isNetwork5gHz_currentNetwork_failure_above5ghz() { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { setManagers(); WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_ABOVE_5GHZ); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + + assertEquals(false, mWiseFy.isNetwork5gHz()); + } + } + + @Test + public void isNetwork5gHz_currentNetwork_failure_below5ghz() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_BELOW_5GHZ); when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); assertEquals(false, mWiseFy.isNetwork5gHz()); @@ -843,7 +1010,7 @@ public void isNetwork5gHz_currentNetwork_success() { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { setManagers(); WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_5G); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_5GHZ); when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); assertEquals(true, mWiseFy.isNetwork5gHz()); @@ -851,11 +1018,23 @@ public void isNetwork5gHz_currentNetwork_success() { } @Test - public void isNetwork5gHz_provideWifiInfo_failure() { + public void isNetwork5gHz_provideWifiInfo_failure_above5ghz() { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { setManagers(); WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_ABOVE_5GHZ); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + + assertEquals(false, mWiseFy.isNetwork5gHz(mockWifiInfo)); + } + } + + @Test + public void isNetwork5gHz_provideWifiInfo_failure_below5ghz() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_BELOW_5GHZ); when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); assertEquals(false, mWiseFy.isNetwork5gHz(mockWifiInfo)); @@ -867,7 +1046,7 @@ public void isNetwork5gHz_provideWifiInfo_success() { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { setManagers(); WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_5G); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_5GHZ); when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); assertEquals(true, mWiseFy.isNetwork5gHz(mockWifiInfo)); @@ -939,6 +1118,11 @@ public void isNetworkSecure_failure_nullCapabilities() { assertEquals(false, mWiseFy.isNetworkSecure(scanResult)); } + @Test + public void isNetworkSecure_failure_nullScanResult() { + assertEquals(false, mWiseFy.isNetworkSecure(null)); + } + @Test public void isWifiEnabled_failure() { setManagers(); @@ -954,7 +1138,7 @@ public void isWifiEnabled_success() { } @Test - public void isWifiEnabled_failure_NullWifiManager() { + public void isWifiEnabled_failure_nullWifiManager() { setManagersToNull(); assertEquals(false, mWiseFy.isWifiEnabled()); } @@ -1046,4 +1230,8 @@ private void setManagersToNull() { mWiseFy.mWifiManager = null; mWiseFy.mConnectivityManager = null; } + + private void setConnectivityManagerToNull() { + mWiseFy.mConnectivityManager = null; + } } \ No newline at end of file diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/base/TestUtils.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/base/TestUtils.java index 0217ad4f..7f59b5f9 100644 --- a/wisefy/src/androidTest/java/com/isupatches/wisefy/base/TestUtils.java +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/base/TestUtils.java @@ -22,9 +22,13 @@ public class TestUtils { public static final String TEST_SSID2 = "Test Network 2"; - public static final int TEST_NETWORK_FREQUENCY = 2400; + public static final int TEST_NETWORK_FREQUENCY_BELOW_5GHZ = 2400; - public static final int TEST_NETWORK_FREQUENCY_5G = 5000; + public static final int TEST_NETWORK_FREQUENCY_ABOVE_5GHZ = 6000; + + public static final int TEST_NETWORK_FREQUENCY_24GHZ = 2400; + + public static final int TEST_NETWORK_FREQUENCY_5GHZ = 5000; public static List getTestWifiConfiguration() { List wifiList = new ArrayList<>(); diff --git a/wisefy/src/main/java/com/isupatches/wisefy/WiseFy.java b/wisefy/src/main/java/com/isupatches/wisefy/WiseFy.java index 89e1136c..eba602ea 100644 --- a/wisefy/src/main/java/com/isupatches/wisefy/WiseFy.java +++ b/wisefy/src/main/java/com/isupatches/wisefy/WiseFy.java @@ -47,6 +47,10 @@ public class WiseFy { public static final int WIFI_MANAGER_FAILURE = -1; + public static final int MIN_FREQUENCY_5GHZ = 4900; + + public static final int MAX_FREQUENCY_5GHZ = 5900; + private SSIDUtil mSSIDUtil = SSIDUtil.getInstance(); ConnectivityManager mConnectivityManager; @@ -146,7 +150,7 @@ int addOpenNetwork(String ssid) { if (!ssidAlreadyConfigured) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "Adding open network with SSID " + ssid); + Log.d(TAG, String.format("Adding open network with SSID %s", ssid)); } WifiConfiguration wifiConfig = new WifiConfiguration(); @@ -167,7 +171,7 @@ int addOpenNetwork(String ssid) { } } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mWifiManager to connect to add open network"); + Log.e(TAG, "No WifiManager to add open network"); } } return WISEFY_FAILURE; @@ -184,7 +188,7 @@ int addOpenNetwork(String ssid) { int addWEPNetwork(String ssid, String password) { if (TextUtils.isEmpty(ssid) || TextUtils.isEmpty(password)) { if (LogUtil.isLoggable(TAG, Log.WARN, mLoggingEnabled)) { - Log.w(TAG, "Breaking due to missing ssid or password. ssid: " + ssid + ", password: " + password); + Log.w(TAG, String.format("Breaking due to missing ssid or password. ssid: %s, password: %s", ssid, password)); } return WISEFY_FAILURE; } @@ -193,7 +197,7 @@ int addWEPNetwork(String ssid, String password) { if (!ssidAlreadyConfigured) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "Adding WEP network with SSID " + ssid); + Log.d(TAG, String.format("Adding WEP network with SSID %s", ssid)); } WifiConfiguration wifiConfig = new WifiConfiguration(); @@ -213,7 +217,7 @@ int addWEPNetwork(String ssid, String password) { } } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mWifiManager to connect to add WEP network"); + Log.e(TAG, "No WifiManager to add WEP network"); } } return WISEFY_FAILURE; @@ -230,7 +234,7 @@ int addWEPNetwork(String ssid, String password) { int addWPA2Network(String ssid, String password) { if (TextUtils.isEmpty(ssid) || TextUtils.isEmpty(password)) { if (LogUtil.isLoggable(TAG, Log.WARN, mLoggingEnabled)) { - Log.w(TAG, "Breaking due to missing ssid or password. ssid: " + ssid + ", password: " + password); + Log.w(TAG, String.format("Breaking due to missing ssid or password. ssid: %s, password: %s", ssid, password)); } return WISEFY_FAILURE; } @@ -239,7 +243,7 @@ int addWPA2Network(String ssid, String password) { if (!ssidAlreadyConfigured) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "Adding WPA2 network with SSID " + ssid); + Log.d(TAG, String.format("Adding WPA2 network with SSID %s", ssid)); } WifiConfiguration wifiConfig = new WifiConfiguration(); @@ -262,7 +266,7 @@ int addWPA2Network(String ssid, String password) { } } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mWifiManager to connect to add WPA2 network"); + Log.e(TAG, "No WifiManager to add WPA2 network"); } } return WISEFY_FAILURE; @@ -305,7 +309,7 @@ int compareSignalLevel(int rssi1, int rssi2) { */ boolean connectToNetwork(String ssidToConnectTo, int timeoutInMillis) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "Connecting to network: " + ssidToConnectTo); + Log.d(TAG, String.format("Connecting to network: %s", ssidToConnectTo)); } if (mWifiManager != null) { List list = mWifiManager.getConfiguredNetworks(); @@ -316,11 +320,11 @@ boolean connectToNetwork(String ssidToConnectTo, int timeoutInMillis) { String ssidInList = wifiConfiguration.SSID.replaceAll("\"", ""); if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "Configured WiFi Network {index:" + i + ", ssidInList:" + ssidInList + "}"); + Log.d(TAG, String.format("Configured WiFi Network { index: %d, ssidInList: %s }", i, ssidInList)); } if (ssidInList.equals(ssidToConnectTo)) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "ssidToReconnectTo: " + ssidToConnectTo + " matches ssidInList:" + ssidInList); + Log.d(TAG, String.format("ssidToReconnectTo: %s matches ssidInList: %s", ssidToConnectTo, ssidInList)); } mWifiManager.disconnect(); mWifiManager.enableNetwork(wifiConfiguration.networkId, true); @@ -331,11 +335,11 @@ boolean connectToNetwork(String ssidToConnectTo, int timeoutInMillis) { } } if (LogUtil.isLoggable(TAG, Log.WARN, mLoggingEnabled)) { - Log.w(TAG, "ssidToReconnectTo: " + ssidToConnectTo + " was not found in list to connect to"); + Log.w(TAG, String.format("ssidToReconnectTo: %s was not found in list to connect to", ssidToConnectTo)); } } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mWifiManager to connect to network. SSID: " + ssidToConnectTo); + Log.e(TAG, String.format("No WifiManager to connect to network. SSID: %s", ssidToConnectTo)); } } return false; @@ -354,7 +358,7 @@ boolean disableWifi() { return mWifiManager.setWifiEnabled(false); } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mWifiManager to disable Wifi"); + Log.e(TAG, "No WifiManager to disable Wifi"); } } return false; @@ -373,7 +377,7 @@ boolean disconnectFromCurrentNetwork() { return mWifiManager.disconnect(); } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mWifiManager to disconnect from current network"); + Log.e(TAG, "No WifiManager to disconnect from current network"); } } return false; @@ -392,7 +396,7 @@ boolean enableWifi() { return mWifiManager.setWifiEnabled(true); } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mWifiManager to enable wifi"); + Log.e(TAG, "No WifiManager to enable wifi"); } } return false; @@ -408,7 +412,7 @@ WifiInfo getCurrentNetwork() { return mWifiManager.getConnectionInfo(); } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mWifiManager to get current network"); + Log.e(TAG, "No WifiManager to get current network"); } } return null; @@ -472,15 +476,15 @@ List getNearbyAccessPoints(boolean filterDuplicates) { for (int i = 0; i < scanResultsToReturn.size(); i++) { ScanResult scanResult = scanResultsToReturn.get(i); if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "SSID 1: " + newScanResult.SSID + ". SSID 2: " + scanResult.SSID); + Log.d(TAG, String.format("SSID 1: %s, SSID 2: %s", newScanResult.SSID, scanResult.SSID)); } if (newScanResult.SSID.equalsIgnoreCase(scanResult.SSID)) { found = true; if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { Log.d(TAG, "SSID did match"); - Log.d(TAG, "Current level: " + scanResult.level); - Log.d(TAG, "New level: " + newScanResult.level); - Log.d(TAG, "comparison result: " + WifiManager.compareSignalLevel(newScanResult.level, scanResult.level)); + Log.d(TAG, String.format("Current level: %d", scanResult.level)); + Log.d(TAG, String.format("New level: %d", newScanResult.level)); + Log.d(TAG, String.format("comparison result: %d", WifiManager.compareSignalLevel(newScanResult.level, scanResult.level))); } if (WifiManager.compareSignalLevel(newScanResult.level, scanResult.level) > 0) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { @@ -506,7 +510,7 @@ List getNearbyAccessPoints(boolean filterDuplicates) { } } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mWifiManager to get nearby access points"); + Log.e(TAG, "No WifiManager to get nearby access points"); } } return null; @@ -522,7 +526,7 @@ List getSavedNetworks() { return mWifiManager.getConfiguredNetworks(); } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mWifiManager to get saved networks"); + Log.e(TAG, "No WifiManager to get saved networks"); } } return null; @@ -543,7 +547,7 @@ boolean isDeviceConnectedToMobileNetwork() { } } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mConnectivityManager check if device is connected to mobile network"); + Log.e(TAG, "No ConnectivityManager check if device is connected to mobile network"); } } return false; @@ -562,7 +566,7 @@ boolean isDeviceConnectedToMobileOrWifiNetwork() { } } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mConnectivityManager check if device is connected to mobile or wifi network"); + Log.e(TAG, "No ConnectivityManager check if device is connected to mobile or wifi network"); } } return false; @@ -583,7 +587,7 @@ boolean isDeviceConnectedToSSID(String ssid) { if (connectionInfo != null && connectionInfo.getSSID() != null) { String currentSSID = connectionInfo.getSSID().replaceAll("\"", ""); if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "Current SSID: " + currentSSID); + Log.d(TAG, String.format("Current SSID: %s", currentSSID)); } if (currentSSID.equals(ssid)) { @@ -603,7 +607,7 @@ boolean isDeviceConnectedToSSID(String ssid) { } } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mWifiManager check if device is connected to SSID: " + ssid); + Log.e(TAG, String.format("No WifiManager check if device is connected to SSID: %s", ssid)); } } return false; @@ -624,7 +628,7 @@ boolean isDeviceConnectedToWifiNetwork() { } } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mConnectivityManager check if device is connected to wifi network"); + Log.e(TAG, "No ConnectivityManager check if device is connected to wifi network"); } } return false; @@ -648,7 +652,7 @@ boolean isLoggingEnabled() { */ boolean isNetwork5gHz() { int frequency = getFrequency(); - return frequency > 4900 && frequency < 5900; + return frequency > MIN_FREQUENCY_5GHZ && frequency < MAX_FREQUENCY_5GHZ; } /** @@ -662,7 +666,7 @@ boolean isNetwork5gHz() { */ boolean isNetwork5gHz(WifiInfo network) { int frequency = getFrequency(network); - return frequency > 4900 && frequency < 5900; + return frequency > MIN_FREQUENCY_5GHZ && frequency < MAX_FREQUENCY_5GHZ; } /** @@ -677,7 +681,7 @@ boolean isNetworkInConfigurationList(String ssid) { return checkIfNetworkInConfigurationList(ssid); } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mWifiManager to check if network is in the configuration list"); + Log.e(TAG, "No WifiManager to check if network is in the configuration list"); } } return false; @@ -710,7 +714,7 @@ boolean isWifiEnabled() { return mWifiManager.isWifiEnabled(); } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mWifiManager to check if wifi is enabled"); + Log.e(TAG, "No WifiManager to check if wifi is enabled"); } } return false; @@ -732,11 +736,11 @@ boolean removeNetwork(String ssidToRemove) { if (wifiConfiguration != null && wifiConfiguration.SSID != null) { String ssidInList = wifiConfiguration.SSID.replaceAll("\"", ""); if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "Configured WiFi Network {index:" + i + ", ssidInList:" + ssidInList + "}"); + Log.d(TAG, String.format("Configured WiFi Network { index: %d, ssidInList: %s }", i, ssidInList)); } if (ssidInList.equals(ssidToRemove)) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "Removing network: " + ssidToRemove); + Log.d(TAG, String.format("Removing network: %s", ssidToRemove)); } mWifiManager.disconnect(); boolean result = mWifiManager.removeNetwork(wifiConfiguration.networkId); @@ -757,11 +761,11 @@ boolean removeNetwork(String ssidToRemove) { } } if (LogUtil.isLoggable(TAG, Log.WARN, mLoggingEnabled)) { - Log.w(TAG, "SSID to remove: " + ssidToRemove + " was not found in list to remove network"); + Log.w(TAG, String.format("SSID to remove: %s was not found in list to remove network", ssidToRemove)); } } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mWifiManager to remove network. SSID: " + ssidToRemove); + Log.e(TAG, String.format("No WifiManager to remove network. SSID: %s", ssidToRemove)); } } return false; @@ -781,21 +785,21 @@ String searchForSSID(String ssidToSearchFor, int timeoutInMillis) { long currentTime; long endTime = System.currentTimeMillis() + timeoutInMillis; if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "End time (searchForSSID): " + endTime); + Log.d(TAG, String.format("End time (searchForSSID): %d", endTime)); } do { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "Scanning SSIDs, pass " + scanPass); + Log.d(TAG, String.format("Scanning SSIDs, pass %d", scanPass)); } mWifiManager.startScan(); List networks = mWifiManager.getScanResults(); for (ScanResult scanResult : networks) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "scanResult.SSID: " + scanResult.SSID); + Log.d(TAG, String.format("scanResult.SSID: %s", scanResult.SSID)); } if (scanResult.SSID != null && (scanResult.SSID.toUpperCase(Locale.getDefault()).contains(ssidToSearchFor.toUpperCase(Locale.getDefault())))) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "Found match, SSID: " + scanResult.SSID); + Log.d(TAG, String.format("Found match, SSID: %s", scanResult.SSID)); } return scanResult.SSID; } @@ -808,13 +812,13 @@ String searchForSSID(String ssidToSearchFor, int timeoutInMillis) { } currentTime = System.currentTimeMillis(); if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "Current time (searchForSSID): " + endTime); + Log.d(TAG, String.format("Current time (searchForSSID): %d", endTime)); } scanPass++; } while (currentTime < endTime); } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, "No mWifiManager to search for network. SSID: " + ssidToSearchFor); + Log.e(TAG, String.format("No WifiManager to search for network. SSID: %s", ssidToSearchFor)); } } return null; @@ -871,7 +875,7 @@ private boolean checkIfNetworkInConfigurationList(String ssid) { if (wifiConfiguration != null && wifiConfiguration.SSID != null) { String ssidInList = wifiConfiguration.SSID.replaceAll("\"", ""); if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "SSID in list: " + ssidInList + ", SSID: " + ssid); + Log.d(TAG, String.format("SSID in list: %s, SSID: %s", ssidInList, ssid)); } if (ssidInList.equals(ssid)) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { @@ -902,7 +906,7 @@ private boolean waitToConnectToSSID(String ssid, int timeoutInMillis) { long currentTime; long endTime = System.currentTimeMillis() + timeoutInMillis; if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "End time (waitToConnectToSSID): " + endTime); + Log.d(TAG, String.format("End time (waitToConnectToSSID): %d", endTime)); } do { boolean result = isDeviceConnectedToSSID(ssid); @@ -916,7 +920,7 @@ private boolean waitToConnectToSSID(String ssid, int timeoutInMillis) { } currentTime = System.currentTimeMillis(); if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "Current time (waitToConnectToSSID): " + endTime); + Log.d(TAG, String.format("Current time (waitToConnectToSSID): %d", endTime)); } } while (currentTime < endTime); return false;