-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from rommansabbir/dev
Dev
- Loading branch information
Showing
12 changed files
with
377 additions
and
22 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
NetworkX/src/main/java/com/rommansabbir/networkx/LastKnownSpeed.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,3 @@ | ||
package com.rommansabbir.networkx | ||
|
||
data class LastKnownSpeed(val speed: String, val networkTypeNetwork: String, val simplifiedSpeed : String) |
7 changes: 7 additions & 0 deletions
7
NetworkX/src/main/java/com/rommansabbir/networkx/NetworkSpeedType.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,7 @@ | ||
package com.rommansabbir.networkx | ||
|
||
object NetworkSpeedType { | ||
const val GB = "GB" | ||
const val MB = "MB" | ||
const val KB = "KB" | ||
} |
45 changes: 45 additions & 0 deletions
45
NetworkX/src/main/java/com/rommansabbir/networkx/NetworkXConfig.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,45 @@ | ||
package com.rommansabbir.networkx | ||
|
||
import android.app.Application | ||
import com.rommansabbir.networkx.NetworkXConfig.Builder | ||
import com.rommansabbir.networkx.exceptions.NetworkXNotInitializedException | ||
|
||
/** | ||
* This class represent the configurations for NetworkX. | ||
* | ||
* Use the [Builder] class to initialize [NetworkXConfig]. | ||
* | ||
* @param application, [Application] reference to initialize NetworkX properly | ||
* @param enableSpeedMeter, determine if to enable monitoring network speed | ||
*/ | ||
class NetworkXConfig private constructor( | ||
val application: Application, | ||
val enableSpeedMeter: Boolean | ||
) { | ||
// Use the builder class to initialize NetworkXConfig | ||
class Builder { | ||
// States | ||
private lateinit var application: Application | ||
private var isSpeedMeterEnabled: Boolean = false | ||
|
||
// Hold application reference | ||
fun withApplication(application: Application): Builder { | ||
this.application = application | ||
return this | ||
} | ||
|
||
// Hold enable speed meter status reference | ||
fun withEnableSpeedMeter(value: Boolean): Builder { | ||
this.isSpeedMeterEnabled = value | ||
return this | ||
} | ||
|
||
// Return an new instance of NetworkXConfig | ||
fun build(): NetworkXConfig { | ||
if (!this::application.isInitialized) { | ||
throw NetworkXNotInitializedException() | ||
} | ||
return NetworkXConfig(application, isSpeedMeterEnabled) | ||
} | ||
} | ||
} |
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
97 changes: 97 additions & 0 deletions
97
NetworkX/src/main/java/com/rommansabbir/networkx/TrafficUtils.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,97 @@ | ||
package com.rommansabbir.networkx | ||
|
||
import android.net.TrafficStats | ||
import kotlinx.coroutines.* | ||
import java.util.* | ||
|
||
/** | ||
* To monitor current network speed. | ||
* All the work should be done by using [CoroutineScope]. | ||
* So that all ongoing operations can be cancelled on demand. | ||
*/ | ||
class TrafficUtils { | ||
private var ioScope: CoroutineScope? = null | ||
private var mainScope: CoroutineScope? = null | ||
|
||
// Initialize all scopes | ||
fun enabledScopes() { | ||
synchronized(Any()) { | ||
ioScope?.cancel() | ||
ioScope = CoroutineScope(Dispatchers.IO) | ||
mainScope?.cancel() | ||
mainScope = CoroutineScope(Dispatchers.Main) | ||
} | ||
} | ||
|
||
// Release all scopes | ||
fun releaseScopes() { | ||
synchronized(Any()) { | ||
ioScope?.cancel() | ||
ioScope = null | ||
mainScope?.cancel() | ||
mainScope = null | ||
} | ||
} | ||
|
||
private val gb: Long = 1000000000 | ||
private val mb: Long = 1000000 | ||
private val kb: Long = 1000 | ||
|
||
/** | ||
* Get current network speed which return an instance of [LastKnownSpeed] instance using the callback. | ||
* | ||
* @param onUpdate, callback for [LastKnownSpeed] update | ||
*/ | ||
fun getNetworkSpeed(onUpdate: (lastKnownSpeed: LastKnownSpeed) -> Unit) { | ||
try { | ||
if (ioScope == null || mainScope == null) { | ||
releaseScopes() | ||
enabledScopes() | ||
} | ||
ioScope?.launch { | ||
val downloadSpeedOutput: String | ||
val units: String | ||
val mBytesPrevious = TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes() | ||
delay(1000) | ||
val mBytesCurrent = TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes() | ||
val mNetworkSpeed = mBytesCurrent - mBytesPrevious | ||
val mDownloadSpeedWithDecimals: Float | ||
val networkSpeedType: String | ||
when { | ||
mNetworkSpeed >= gb -> { | ||
mDownloadSpeedWithDecimals = mNetworkSpeed.toFloat() / gb.toFloat() | ||
units = " ${NetworkSpeedType.GB}" | ||
networkSpeedType = NetworkSpeedType.GB | ||
} | ||
mNetworkSpeed >= mb -> { | ||
mDownloadSpeedWithDecimals = mNetworkSpeed.toFloat() / mb.toFloat() | ||
units = " ${NetworkSpeedType.MB}" | ||
networkSpeedType = NetworkSpeedType.MB | ||
} | ||
else -> { | ||
mDownloadSpeedWithDecimals = mNetworkSpeed.toFloat() / kb.toFloat() | ||
units = " ${NetworkSpeedType.KB}" | ||
networkSpeedType = NetworkSpeedType.KB | ||
} | ||
} | ||
downloadSpeedOutput = | ||
if (units != " ${NetworkSpeedType.KB}" && mDownloadSpeedWithDecimals < 100) { | ||
String.format(Locale.US, "%.1f", mDownloadSpeedWithDecimals) | ||
} else { | ||
mDownloadSpeedWithDecimals.toInt().toString() | ||
} | ||
mainScope?.launch { | ||
onUpdate.invoke( | ||
LastKnownSpeed( | ||
downloadSpeedOutput, | ||
networkSpeedType, | ||
downloadSpeedOutput + units | ||
) | ||
) | ||
} | ||
} | ||
} catch (e: Exception) { | ||
throw e | ||
} | ||
} | ||
} |
4 changes: 1 addition & 3 deletions
4
...rkX/src/main/java/com/rommansabbir/networkx/exceptions/NetworkXNotInitializedException.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 |
---|---|---|
@@ -1,6 +1,4 @@ | ||
package com.rommansabbir.networkx.exceptions | ||
|
||
@Deprecated("Deprecated. Use NetworkXProvider instead") | ||
class NetworkXNotInitializedException : | ||
Exception("Did you initialized NetworkXCore from onCreate() in your application class?") { | ||
} | ||
Exception("Did you initialized NetworkX with NetworkXConfig from onCreate() in your application class?") |
Oops, something went wrong.