-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track network connectivity and resolve in with Resolution Strategy
- Loading branch information
Showing
10 changed files
with
370 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
.../java/pro/averin/anton/clean/android/cookbook/data/common/connection/ConnectionChecker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package pro.averin.anton.clean.android.cookbook.data.common.connection | ||
|
||
import android.net.ConnectivityManager | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
|
||
@Singleton | ||
class ConnectionChecker @Inject constructor() { | ||
|
||
@Inject lateinit var connectivityManager: ConnectivityManager | ||
|
||
fun getNetworkStatus(): Int { | ||
val networkInfo = connectivityManager.activeNetworkInfo | ||
if (networkInfo != null && networkInfo.isConnectedOrConnecting) { | ||
when (networkInfo.type) { | ||
ConnectivityManager.TYPE_WIFI -> return WIFI | ||
ConnectivityManager.TYPE_MOBILE -> return MOBILE | ||
else -> return OFFLINE | ||
} | ||
} | ||
|
||
return OFFLINE; | ||
} | ||
|
||
fun isOnline(): Boolean { | ||
val activeNetwork = connectivityManager.activeNetworkInfo | ||
|
||
return activeNetwork != null && activeNetwork.isConnectedOrConnecting | ||
} | ||
|
||
|
||
companion object NetworkStatus { | ||
val OFFLINE = 1 | ||
val MOBILE = 2 | ||
val WIFI = 3 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...rc/main/java/pro/averin/anton/clean/android/cookbook/ui/common/ConnectionEventsHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package pro.averin.anton.clean.android.cookbook.ui.common | ||
|
||
import pro.averin.anton.clean.android.cookbook.data.common.connection.ConnectionChecker | ||
import pro.averin.anton.clean.android.cookbook.data.common.rx.bus.GlobalBusSubscriber | ||
import pro.averin.anton.clean.android.cookbook.data.common.rx.bus.events.ConnectivityAvailableGlobalEvent | ||
import pro.averin.anton.clean.android.cookbook.data.common.rx.bus.events.ConnectivityUnavailableGlobalEvent | ||
import pro.averin.anton.clean.android.cookbook.di.ActivityScope | ||
import pro.averin.anton.clean.android.cookbook.ui.common.resolution.Resolution | ||
import javax.inject.Inject | ||
|
||
@ActivityScope | ||
class ConnectionEventsHandler @Inject constructor( | ||
private val globalBusSubscriber: GlobalBusSubscriber, | ||
private val connectionChecker: ConnectionChecker | ||
) { | ||
|
||
fun start(resolution: Resolution) { | ||
globalBusSubscriber.subscribe(ConnectivityAvailableGlobalEvent::class) { | ||
resolution.onConnectivityAvailable() | ||
} | ||
globalBusSubscriber.subscribe(ConnectivityUnavailableGlobalEvent::class) { | ||
resolution.onConnectivityUnavailable() | ||
} | ||
|
||
if (!connectionChecker.isOnline()) { | ||
resolution.onConnectivityUnavailable() | ||
} else { | ||
resolution.onConnectivityAvailable() | ||
} | ||
|
||
} | ||
|
||
fun stop() { | ||
globalBusSubscriber.unsubscribe() | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...averin/anton/clean/android/cookbook/ui/common/broadcasts/ConnectivityBroadcastReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package pro.averin.anton.clean.android.cookbook.ui.common.broadcasts | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import pro.averin.anton.clean.android.cookbook.BaseContext | ||
import pro.averin.anton.clean.android.cookbook.data.common.connection.ConnectionChecker | ||
import pro.averin.anton.clean.android.cookbook.data.common.rx.bus.GlobalBus | ||
import pro.averin.anton.clean.android.cookbook.data.common.rx.bus.events.ConnectivityAvailableGlobalEvent | ||
import pro.averin.anton.clean.android.cookbook.data.common.rx.bus.events.ConnectivityUnavailableGlobalEvent | ||
import javax.inject.Inject | ||
|
||
class ConnectivityBroadcastReceiver : BroadcastReceiver() { | ||
|
||
@Inject lateinit var connectionChecker: ConnectionChecker | ||
@Inject lateinit var globalBus: GlobalBus | ||
|
||
override fun onReceive(context: Context?, intent: Intent?) { | ||
val baseContext = context?.applicationContext as BaseContext | ||
baseContext.appComponent.injectTo(this) | ||
|
||
if (connectionChecker.isOnline()) { | ||
globalBus.post(ConnectivityAvailableGlobalEvent()) | ||
} else { | ||
globalBus.post(ConnectivityUnavailableGlobalEvent()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...a/pro/averin/anton/clean/android/cookbook/data/common/connection/ConnectionCheckerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package pro.averin.anton.clean.android.cookbook.data.common.connection | ||
|
||
import android.net.ConnectivityManager | ||
import android.net.NetworkInfo | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.InjectMocks | ||
import org.mockito.Mock | ||
import org.powermock.modules.junit4.PowerMockRunner | ||
import pro.averin.anton.clean.android.cookbook.kotlin.test.assertThat | ||
import pro.averin.anton.clean.android.cookbook.kotlin.test.given | ||
import pro.averin.anton.clean.android.cookbook.kotlin.test.isEqualTo | ||
|
||
@RunWith(PowerMockRunner::class) | ||
class ConnectionCheckerTest { | ||
|
||
@Mock lateinit var connectivityManager: ConnectivityManager | ||
@Mock lateinit var activeNetworkInfo: NetworkInfo | ||
|
||
@InjectMocks lateinit var classToTest: ConnectionChecker | ||
|
||
@Test | ||
fun networkStatusOfflineWhenNoNetworksConnected() { | ||
// given | ||
given(connectivityManager.activeNetworkInfo).willReturn(null) | ||
|
||
// when | ||
val status = classToTest.getNetworkStatus() | ||
|
||
// then | ||
assertThat(status, isEqualTo(ConnectionChecker.OFFLINE)) | ||
} | ||
|
||
@Test | ||
fun networkStatusOfflineWhenNotWifiOrMobileNetworkConnected() { | ||
// given | ||
given(connectivityManager.activeNetworkInfo).willReturn(activeNetworkInfo) | ||
given(activeNetworkInfo.isConnectedOrConnecting).willReturn(true) | ||
given(activeNetworkInfo.type).willReturn(ConnectivityManager.TYPE_DUMMY) | ||
|
||
// when | ||
val status = classToTest.getNetworkStatus() | ||
|
||
// then | ||
assertThat(status, isEqualTo(ConnectionChecker.OFFLINE)) | ||
} | ||
|
||
@Test | ||
fun networkStatusWifiWhenWifiConnected() { | ||
// given | ||
given(connectivityManager.activeNetworkInfo).willReturn(activeNetworkInfo) | ||
given(activeNetworkInfo.type).willReturn(ConnectivityManager.TYPE_WIFI) | ||
given(activeNetworkInfo.isConnectedOrConnecting).willReturn(true) | ||
|
||
// when | ||
val status = classToTest.getNetworkStatus() | ||
|
||
// then | ||
assertThat(status, isEqualTo(ConnectionChecker.WIFI)) | ||
} | ||
|
||
@Test | ||
fun offlineWhenActiveNetworkNotConnected() { | ||
// given | ||
given(connectivityManager.activeNetworkInfo).willReturn(activeNetworkInfo) | ||
given(activeNetworkInfo.isConnectedOrConnecting).willReturn(false) | ||
|
||
// when | ||
val result = classToTest.isOnline() | ||
|
||
// then | ||
assertThat(result, isEqualTo(false)) | ||
} | ||
|
||
|
||
@Test | ||
fun onlineWhenActiveNetworkConnected() { | ||
// given | ||
given(connectivityManager.activeNetworkInfo).willReturn(activeNetworkInfo) | ||
given(activeNetworkInfo.isConnectedOrConnecting).willReturn(true) | ||
|
||
// when | ||
val result = classToTest.isOnline() | ||
|
||
// then | ||
assertThat(result, isEqualTo(true)) | ||
} | ||
|
||
@Test | ||
fun networkStatusMobileWhenMobileConnected() { | ||
// given | ||
given(connectivityManager.activeNetworkInfo).willReturn(activeNetworkInfo) | ||
given(activeNetworkInfo.type).willReturn(ConnectivityManager.TYPE_MOBILE) | ||
given(activeNetworkInfo.isConnectedOrConnecting).willReturn(true) | ||
|
||
// when | ||
val status = classToTest.getNetworkStatus() | ||
|
||
// then | ||
assertThat(status, isEqualTo(ConnectionChecker.MOBILE)) | ||
} | ||
} |
Oops, something went wrong.