Skip to content

Commit

Permalink
Merge pull request #1 from AshwinN796/detect_network_type
Browse files Browse the repository at this point in the history
updated to receive internet speed-type
  • Loading branch information
AshwinN796 authored Jan 16, 2020
2 parents 7028b76 + 764442a commit b55ba98
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 49 deletions.
86 changes: 51 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,89 @@
## NetworkListener ![](https://jitpack.io/v/AshwinN796/networkListener.svg)
# NetworkListener ![](https://jitpack.io/v/AshwinN796/networkListener.svg)

NetworkLister is the library to check the internet connection status of device at runtime.

## SetUp
##### project level gradle
```

## New Features!
- Now detect runtime internet speed status

### Installation
#### Project level gradle

```Gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```

#### module level gradle
```
implementation 'com.github.AshwinN796:networkListener:1.0.4'

#### Module level gradle
```Gradle
implementation 'com.github.AshwinN796:networkListener:1.1.0'
```

## How to use
##### Implementation
Application class :

#### In your application class
```
```Kotlin
class MyApplication : Application() {

override fun onCreate() {
super.onCreate()
//init network class here
NetworkConfig.initNetworkConfig(this)
}
override fun onCreate() {
super.onCreate()
//init NetworkConfig
NetworkConfig.initNetworkConfig(this)
}

override fun onLowMemory() {
super.onLowMemory()
//Remove all listeners while on low memory
NetworkConfig.getInstance().removeAllNetworkConnectivityListener()
}
}
override fun onLowMemory() {
super.onLowMemory()
//Remove all listeners while on low memory
NetworkConfig.getInstance().removeAllNetworkConnectivityListener()

}
}
```
Activity class :

#### In your activity class
```
```Kotlin
class MainActivity : AppCompatActivity(), NetworkStateListener {

private var networkConfig : NetworkConfig? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//get instance of networkConfig class

//Get instance of networkConfig class
networkConfig = NetworkConfig.getInstance()
//add listener for NetworkConfig

//add connectivity listener
networkConfig!!.addNetworkConnectivityListener(this)
}

override fun onDestroy() {
super.onDestroy()
//remove listener from NetworkConfig
//remove connectivity listener
networkConfig!!.removeNetworkConnectivityListener(this)
}

/*
Do action on network status changed
Here you can perform any action for Network state listener depending on your requirement.
*/
override fun onNetworkStatusChanged(isConnected: Boolean) {
when(isConnected){
true -> Toast.makeText(this@MainActivity,"Internet Connected",Toast.LENGTH_LONG).show()
false -> Toast.makeText(this@MainActivity,"Internet Failed",Toast.LENGTH_LONG).show()

// when(isConnected){
// true -> {//make your action}
// false -> {}
// }
}

override fun onNetworkSpeedChanged(speedType: Int) {
when(speedType) {
NetworkConstant.WIFI_CONNECTED -> {//make your action}
NetworkConstant.FULL_SPEED_CONNECTED -> {}
NetworkConstant.SLOW_CONNECTED -> {}
NetworkConstant.LOW_SPEED_CONNECTED -> {}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ dependencies {
testImplementation testLibraries.junit4
androidTestImplementation androidTestLibraries.testRunner
androidTestImplementation androidTestLibraries.espressoCore
androidTestImplementation androidTestLibraries.androidXJUnit
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Bundle
import android.widget.Toast
import com.kknirmale.networkhandler.config.NetworkConfig
import com.kknirmale.networkhandler.listener.NetworkStateListener
import com.kknirmale.networkhandler.utils.NetworkConstant

class MainActivity : AppCompatActivity(), NetworkStateListener {

Expand Down Expand Up @@ -32,9 +33,19 @@ class MainActivity : AppCompatActivity(), NetworkStateListener {
Here you can perform any action for Network state listener depending on your requirement.
*/
override fun onNetworkStatusChanged(isConnected: Boolean) {
when(isConnected){
true -> Toast.makeText(this@MainActivity,"Internet Connected",Toast.LENGTH_LONG).show()
false -> Toast.makeText(this@MainActivity,"Internet Failed",Toast.LENGTH_LONG).show()

// when(isConnected){
// true -> Toast.makeText(this@MainActivity,"Internet Connected",Toast.LENGTH_LONG).show()
// false -> Toast.makeText(this@MainActivity,"Internet Failed",Toast.LENGTH_LONG).show()
// }
}

override fun onNetworkSpeedChanged(speedType: Int) {
when(speedType) {
NetworkConstant.WIFI_CONNECTED -> Toast.makeText(this@MainActivity,"Wifi Connected",Toast.LENGTH_LONG).show()
NetworkConstant.FULL_SPEED_CONNECTED -> Toast.makeText(this@MainActivity,"Full Speed Connected",Toast.LENGTH_LONG).show()
NetworkConstant.SLOW_CONNECTED -> Toast.makeText(this@MainActivity,"No internet",Toast.LENGTH_LONG).show()
NetworkConstant.LOW_SPEED_CONNECTED -> Toast.makeText(this@MainActivity,"Slow internet",Toast.LENGTH_LONG).show()
}
}
}
12 changes: 8 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ buildscript {

ext {
kotlin_version = '1.3.41'
gradle_version = '3.5.0'
gradle_version = '3.5.2'

appMinSdkVersion = 21
appTargetSdkVersion = 29
appCompileSdkVersion = 29

currentVersionCode = 1
currentVersionName = '1.0.0'
currentVersionCode = 2
currentVersionName = '1.1.0'

androidx_libs_version = '1.1.0'
android_constraint_version = '1.1.3'

junit4Version = '4.12'
roboElectricVersion = '4.3'
andoridxJUnitVersion = '1.1.1'
testRunnerVersion = '1.1.0'
testEspressoVersion = '3.1.0'

Expand All @@ -29,10 +31,12 @@ buildscript {
]

testLibraries = [
junit4 : "junit:junit:" + junit4Version
junit4 : "junit:junit:" + junit4Version,
roboElectric : "org.robolectric:robolectric:" + roboElectricVersion
]

androidTestLibraries = [
androidXJUnit : "androidx.test.ext:junit:" + andoridxJUnitVersion,
testRunner: "androidx.test:runner:" + testRunnerVersion,
espressoCore : "androidx.test.espresso:espresso-core:" + testEspressoVersion
]
Expand Down
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ android.useAndroidX=true
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official

# Added for roboelectric test framework
android.enableUnitTestBinaryResources=true
9 changes: 9 additions & 0 deletions networkhandler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ android {
}
}

testOptions {
unitTests {
includeAndroidResources = true
}
}

}

dependencies {
Expand All @@ -33,6 +39,9 @@ dependencies {
implementation androidxLibraries.kotlin_core

testImplementation testLibraries.junit4
testImplementation testLibraries.roboElectric
androidTestImplementation androidTestLibraries.testRunner
androidTestImplementation androidTestLibraries.espressoCore
androidTestImplementation androidTestLibraries.androidXJUnit

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.kknirmale.networkhandler

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner

import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -13,7 +13,7 @@ import org.junit.Assert.*
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
@RunWith(AndroidJUnit4ClassRunner::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import java.lang.ref.WeakReference
*/
class NetworkConfig(context: Context) : NetworkStateReceiver.InternetCheckListener{

override fun onInternetSpeed(speedType: Int) {
reportNetworkSpeedType(speedType)
}

override fun onComplete(connected: Boolean) {
if (connected){
reportInternetAvailabilityStatus(connected)
Expand Down Expand Up @@ -55,6 +59,7 @@ class NetworkConfig(context: Context) : NetworkStateReceiver.InternetCheckListen
private var networkChangeReceiver : NetworkStateReceiver? = null
private var isNetworkStatusRegistered = false
private var isNetworkConnected = false
private var networkTypeValue = 0

init {
val appContext = context.applicationContext
Expand Down Expand Up @@ -126,7 +131,39 @@ class NetworkConfig(context: Context) : NetworkStateReceiver.InternetCheckListen
}

/*
Attach connectivity listener to listen network state from broadcast receiver
Get report of internet speed value from from listener
*/
private fun reportNetworkSpeedType(speedType : Int) {
networkTypeValue = speedType
if (networkStateListenerWeakReferenceList == null){
return
}

val listenerIterator : MutableIterator<WeakReference<NetworkStateListener>> = networkStateListenerWeakReferenceList!!.iterator()
while (listenerIterator.hasNext()){

val listenerReference : WeakReference<NetworkStateListener> = listenerIterator.next()
if (listenerReference == null){
listenerIterator.remove()
continue
}

val statusListener : NetworkStateListener = listenerReference.get()!!
if (statusListener == null){
listenerIterator.remove()
continue
}

statusListener.onNetworkSpeedChanged(speedType)
}

if (networkStateListenerWeakReferenceList!!.isEmpty()){
unregisterNetworkChangeReceiver()
}
}

/*
Attach all connectivity listener to listen network state from broadcast receiver
*/
fun addNetworkConnectivityListener(statusListener: NetworkStateListener){
if (statusListener == null){
Expand All @@ -140,6 +177,7 @@ class NetworkConfig(context: Context) : NetworkStateReceiver.InternetCheckListen
}

reportInternetAvailabilityStatus(isNetworkConnected)
reportNetworkSpeedType(networkTypeValue)

}

Expand Down Expand Up @@ -205,8 +243,5 @@ class NetworkConfig(context: Context) : NetworkStateReceiver.InternetCheckListen
}


// companion object{
// var lock : Objec
// }

}

Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ Created by Ashwin Nirmale on 17 October,2019
interface NetworkStateListener {

fun onNetworkStatusChanged(isConnected : Boolean)

fun onNetworkSpeedChanged(speedType : Int)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class NetworkStateReceiver : BroadcastReceiver() {
val internetChangeListener : InternetCheckListener = internetListenerReference!!.get()!!

internetChangeListener.onComplete(NetworkUtil(context!!).isConnectedToMobileOrWifi())
internetChangeListener.onInternetSpeed(NetworkUtil(context).isInternetHasSpeed())
}

/*
Expand All @@ -42,6 +43,7 @@ class NetworkStateReceiver : BroadcastReceiver() {

interface InternetCheckListener {
fun onComplete(connected: Boolean)
fun onInternetSpeed(speedType : Int)
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.kknirmale.networkhandler.utils

/**
* Created by Ashwin Nirmale on 2020-01-16.
*/
object NetworkConstant {

const val SLOW_CONNECTED = 0
const val LOW_SPEED_CONNECTED = 1
const val WIFI_CONNECTED = 2
const val FULL_SPEED_CONNECTED =3

}
Loading

0 comments on commit b55ba98

Please sign in to comment.