Skip to content

Commit

Permalink
issue141 - Become agnostic to logging implementation (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
isuPatches authored Jan 10, 2020
1 parent 85b9fa7 commit 3897eed
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 379 deletions.
17 changes: 0 additions & 17 deletions wisefy/src/androidTest/java/com/isupatches/wisefy/WiseFyTests.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.isupatches.wisefy

import androidx.test.platform.app.InstrumentationRegistry
import com.isupatches.wisefy.internal.base.BaseInstrumentationTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
Expand All @@ -19,22 +18,6 @@ internal class WiseFyTests : BaseInstrumentationTest() {
private const val EXPECTED_SIGNAL_RESULT = 35
}

@Test
fun brains_loggingFalse() {
val wisefy = WiseFy.Brains(InstrumentationRegistry.getInstrumentation().targetContext)
.logging(false)
.getSmarts()
assertEquals(false, wisefy.isLoggingEnabled())
}

@Test
fun brains_loggingTrue() {
val wisefy = WiseFy.Brains(InstrumentationRegistry.getInstrumentation().targetContext)
.logging(true)
.getSmarts()
assertEquals(true, wisefy.isLoggingEnabled())
}

@Test
fun calculateBars() {
val result = wisefy.calculateBars(TEST_RSSI_LEVEL_HIGH, TEST_NUMBER_OF_BARS)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.isupatches.wisefy.internal

import com.isupatches.wisefy.logging.WiseFyLogger

class TestWiseFyLogger : WiseFyLogger {

override fun i(tag: String, message: String, vararg args: Any) {
// No-op
}

override fun v(tag: String, message: String, vararg args: Any) {
// No-op
}

override fun d(tag: String, message: String, vararg args: Any) {
// No-op
}

override fun w(tag: String, message: String, vararg args: Any) {
// No-op
}

override fun e(tag: String, message: String, vararg args: Any) {
// No-op
}

override fun e(tag: String, throwable: Throwable, message: String, vararg args: Any) {
// No-op
}

override fun wtf(tag: String, message: String, vararg args: Any) {
// No-op
}

override fun wtf(tag: String, throwable: Throwable, message: String, vararg args: Any) {
// No-op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.isupatches.wisefy.WiseFyPrechecks
import com.isupatches.wisefy.callbacks.AddNetworkCallbacks
import com.isupatches.wisefy.connection.WiseFyConnection
import com.isupatches.wisefy.internal.NullCallbackUtil
import com.isupatches.wisefy.internal.TestWiseFyLogger
import com.isupatches.wisefy.internal.VerificationUtil
import com.isupatches.wisefy.internal.mock.MockNetworkUtil
import com.isupatches.wisefy.internal.mock.MockWiseFyConnectionUtil
Expand Down Expand Up @@ -51,13 +52,12 @@ internal abstract class BaseInstrumentationTest {
mockWifiManager = mock(WifiManager::class.java)
mockConnectivityManager = mock(ConnectivityManager::class.java)

wisefy = WiseFy.Brains(InstrumentationRegistry.getInstrumentation().targetContext)
wisefy = WiseFy.Brains(InstrumentationRegistry.getInstrumentation().targetContext, TestWiseFyLogger())
.customConnectivityManager(mockConnectivityManager)
.customWifiManager(mockWifiManager)
.customWiseFyConnection(mockWiseFyConnection)
.customWiseFyPrechecks(mockWiseFyPrechecks)
.customWiseFySearch(mockWiseFySearch)
.logging(true)
.getSmarts()

wisefy.setupWiseFyThread(true)
Expand All @@ -73,7 +73,8 @@ internal abstract class BaseInstrumentationTest {
verificationUtil = VerificationUtil(mockConnectivityManager, mockWifiManager)
}

@After open fun tearDown() {
@After
open fun tearDown() {
wisefy.dump()
}

Expand Down
64 changes: 20 additions & 44 deletions wisefy/src/main/java/com/isupatches/wisefy/WiseFy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ class WiseFy private constructor(
private val wifiManager: WifiManager,
private val wisefyConnection: WiseFyConnection,
private val wisefyPrechecks: WiseFyPrechecks,
private val wisefySearch: WiseFySearch
private val wisefySearch: WiseFySearch,
private val logger: WiseFyLogger?
) : WiseFyPublicApi {

companion object {
Expand Down Expand Up @@ -147,11 +148,12 @@ class WiseFy private constructor(
*/
class Brains @JvmOverloads constructor(
context: Context,
logger: WiseFyLogger? = null,
useLegacyConnection: Boolean = false,
useLegacySearch: Boolean = false
) {

private var loggingEnabled: Boolean = false
private var logger: WiseFyLogger? = null
private var connectivityManager: ConnectivityManager
private var wifiManager: WifiManager
private var wisefyConnection: WiseFyConnection
Expand All @@ -168,33 +170,21 @@ class WiseFy private constructor(
// and "useLegacyConnection" option is not enabled

wisefyConnection = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || useLegacyConnection) {
WiseFyConnectionLegacy.create(connectivityManager, wifiManager)
WiseFyConnectionLegacy.create(connectivityManager, wifiManager, logger)
} else {
WiseFyConnectionSDK23.create(connectivityManager, wifiManager)
WiseFyConnectionSDK23.create(connectivityManager, wifiManager, logger)
}
// We'll use SDK 23 logic for WiseFySearch if client is on at least an SDK 23 device
// and "useLegacySearch" option is not enabled
wisefySearch = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || useLegacySearch) {
WiseFySearchLegacy.create(wifiManager)
WiseFySearchLegacy.create(wifiManager, logger)
} else {
WiseFySearchSDK23.create(wifiManager)
WiseFySearchSDK23.create(wifiManager, logger)
}

wisefyPrechecks = WiseFyPrechecksImpl.create(wisefySearch)
}

/**
* Used to enable/or disable logging for a WiseFy instance.
*
* @param loggingEnabled If logging should be enabled for the WiseFy instance.
*
* @author Patches
* @since 3.0
*/
fun logging(loggingEnabled: Boolean): Brains = apply {
this.loggingEnabled = loggingEnabled
}

/**
* Used internally to set ConnectivityManager in tests.
*
Expand Down Expand Up @@ -268,24 +258,24 @@ class WiseFy private constructor(
/**
* Uses a private constructor and returns a WiseFy instance.
*
* @see [WiseFyLogger.configureWiseFyLoggerImplementation]
* @see [WiseFyConnection.init]
*
* Updates
* - 05/12/2019: Added new call to [WiseFyConnection.init]
* - 01/07/2019: Added logger
*
* @author Patches
* @since 3.0
*/
fun getSmarts(): WiseFy {
WiseFyLogger.configureWiseFyLoggerImplementation(loggingEnabled)
wisefyConnection.init()
return WiseFy(
connectivityManager = connectivityManager,
wifiManager = wifiManager,
wisefyConnection = wisefyConnection,
wisefyPrechecks = wisefyPrechecks,
wisefySearch = wisefySearch
wisefySearch = wisefySearch,
logger = logger
)
}
}
Expand Down Expand Up @@ -736,15 +726,15 @@ class WiseFy private constructor(
it.quit()
}
if (it.isAlive) {
WiseFyLogger.warn(
logger?.w(
TAG,
"WiseFy Thread is still alive. Current status: isAlive(): %b, getState(): %s",
it.isAlive,
it.state
)
it.interrupt()
}
WiseFyLogger.debug(
logger?.d(
TAG,
"WiseFy Thread isAlive(): %b, getState(): %s",
it.isAlive,
Expand All @@ -754,7 +744,7 @@ class WiseFy private constructor(
}
wisefyHandler = null
wisefyConnection.destroy()
WiseFyLogger.debug(TAG, "Cleaned up WiseFy Thread")
logger?.d(TAG, "Cleaned up WiseFy Thread")
}

/**
Expand Down Expand Up @@ -1062,7 +1052,7 @@ class WiseFy private constructor(
try {
return InetAddress.getByAddress(ipAddress).hostAddress
} catch (uhe: UnknownHostException) {
WiseFyLogger.error(TAG, uhe, "UnknownHostException while gathering IP (sync)")
logger?.e(TAG, uhe, "UnknownHostException while gathering IP (sync)")
}
return null
}
Expand Down Expand Up @@ -1099,7 +1089,7 @@ class WiseFy private constructor(
try {
callbacks?.retrievedIP(InetAddress.getByAddress(ipAddress).hostAddress)
} catch (uhe: UnknownHostException) {
WiseFyLogger.error(TAG, uhe, "UnknownHostException while gathering IP (async)")
logger?.e(TAG, uhe, "UnknownHostException while gathering IP (async)")
callbacks?.failureRetrievingIP()
}
}
Expand Down Expand Up @@ -1435,20 +1425,6 @@ class WiseFy private constructor(
return wisefyConnection.isDeviceRoaming()
}

/**
* To query if logging is enabled or disabled for a WiseFy instance.
*
* @return boolean - If logging is enabled for the WiseFy instance
*
* @see [WiseFyLogger.isLoggingEnabled]
*
* @author Patches
* @since 3.0
*/
@Sync
@CallingThread
override fun isLoggingEnabled(): Boolean = WiseFyLogger.isLoggingEnabled()

/**
* To check if the device's current network is 5gHz.
*
Expand Down Expand Up @@ -1681,7 +1657,7 @@ class WiseFy private constructor(
if (wifiConfiguration != null) {
return removeNetworkConfiguration(wifiConfiguration)
} else {
WiseFyLogger.warn(TAG, "SSID to remove: %s was not found in list to remove network", ssidToRemove)
logger?.w(TAG, "SSID to remove: %s was not found in list to remove network", ssidToRemove)
}
return false
}
Expand Down Expand Up @@ -2211,9 +2187,9 @@ class WiseFy private constructor(
*/
private fun addNetworkConfiguration(wifiConfiguration: WifiConfiguration): Int {
val result = wifiManager.addNetwork(wifiConfiguration)
WiseFyLogger.debug(TAG, "Adding network with SSID: %s had result: %d", wifiConfiguration.SSID, result)
logger?.d(TAG, "Adding network with SSID: %s had result: %d", wifiConfiguration.SSID, result)
if (result == WIFI_MANAGER_FAILURE) {
WiseFyLogger.error(TAG, "Error adding network configuration.")
logger?.e(TAG, "Error adding network configuration.")
}
return result
}
Expand Down Expand Up @@ -2289,7 +2265,7 @@ class WiseFy private constructor(
private fun removeNetworkConfiguration(wifiConfiguration: WifiConfiguration): Boolean {
wifiManager.disconnect()
val result = wifiManager.removeNetwork(wifiConfiguration.networkId)
WiseFyLogger.debug(TAG, "Removing network with SSID: %s had result: %b", wifiConfiguration.SSID, result)
logger?.d(TAG, "Removing network with SSID: %s had result: %b", wifiConfiguration.SSID, result)
wifiManager.reconnect()
return result
}
Expand Down
15 changes: 3 additions & 12 deletions wisefy/src/main/java/com/isupatches/wisefy/WisePublicApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ import com.isupatches.wisefy.callbacks.SearchForSavedNetworksCallbacks
* @see [SignalStrengthApi]
* @see [WiseFy]
*
* Updates
* - 01/07/2020: Removed isLoggingEnabled
*
* @author Patches
* @since 3.0
*/
Expand Down Expand Up @@ -87,18 +90,6 @@ interface WiseFyPublicApi : AccessPointApi, AddNetworkApi, ConnectionApi, Device
* @since 3.0
*/
fun getWiseFyLock(): WiseFyLock

/**
* To query if logging is enabled or disabled for a WiseFy instance.
*
* @return boolean - If logging is enabled for the WiseFy instance
*
* @see [WiseFy.isLoggingEnabled]
*
* @author Patches
* @since 3.0
*/
fun isLoggingEnabled(): Boolean
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ import com.isupatches.wisefy.utils.rest
* @see [WifiManager]
* @see [WiseFyConnection]
*
* Updates
* - 01/07/2020: Added WiseFyLogger
*
* @author Patches
* @since 4.0
*/
internal abstract class AbstractWiseFyConnection(
private val wifiManager: WifiManager
private val wifiManager: WifiManager,
private val logger: WiseFyLogger?
) : WiseFyConnection {

internal companion object {
Expand Down Expand Up @@ -65,9 +69,9 @@ internal abstract class AbstractWiseFyConnection(
connectionInfo?.let {
if (!it.ssid.isNullOrEmpty()) {
val currentSSID = it.ssid.replace(QUOTE, "")
WiseFyLogger.debug(TAG, "Current SSID: %s, Desired SSID: %s", currentSSID, ssid)
logger?.d(TAG, "Current SSID: %s, Desired SSID: %s", currentSSID, ssid)
if (currentSSID.equals(ssid, ignoreCase = true) && isNetworkConnected()) {
WiseFyLogger.debug(TAG, "Network is connected")
logger?.d(TAG, "Network is connected")
return true
}
}
Expand All @@ -94,7 +98,7 @@ internal abstract class AbstractWiseFyConnection(
*/
@WaitsForTimeout
override fun waitToConnectToSSID(ssid: String?, timeoutInMillis: Int): Boolean {
WiseFyLogger.debug(
logger?.d(
TAG,
"Waiting %d milliseconds to connect to network with ssid %s",
timeoutInMillis,
Expand All @@ -108,7 +112,7 @@ internal abstract class AbstractWiseFyConnection(
}
rest()
currentTime = System.currentTimeMillis()
WiseFyLogger.debug(TAG, "Current time: %d, End time: %d (waitToConnectToSSID)", currentTime, endTime)
logger?.d(TAG, "Current time: %d, End time: %d (waitToConnectToSSID)", currentTime, endTime)
} while (currentTime < endTime)
return false
}
Expand Down
Loading

0 comments on commit 3897eed

Please sign in to comment.