-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7db0c8c
commit f8a106a
Showing
2 changed files
with
81 additions
and
0 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
65 changes: 65 additions & 0 deletions
65
library/src/main/java/com/flipkart/okhttpstats/model/ConnectionQuality.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,2 +1,67 @@ | ||
package com.flipkart.okhttpstats.model | ||
|
||
|
||
enum class ConnectionQuality(private val min: Int, private val max: Int) { | ||
/** | ||
* Bandwidth under 150 kbps. | ||
*/ | ||
POOR(0, 150), | ||
/** | ||
* Bandwidth between 150 and 550 kbps. | ||
*/ | ||
MODERATE(151, 550), | ||
/** | ||
* Bandwidth between 550 and 2000 kbps. | ||
*/ | ||
GOOD(551, 2000), | ||
/** | ||
* EXCELLENT - Bandwidth over 2000 kbps. | ||
*/ | ||
EXCELLENT(2001, Int.MAX_VALUE), | ||
/** | ||
* Placeholder for unknown bandwidth. This is the initial value and will stay at this value | ||
* if a bandwidth cannot be accurately found. | ||
*/ | ||
UNKNOWN(0, Int.MAX_VALUE); | ||
|
||
fun inRange(bandWidth: Int): Boolean { | ||
return bandWidth in min until max | ||
} | ||
|
||
override fun toString(): String { | ||
return "$name min = $min max =$max" // NON-NLS | ||
} | ||
|
||
companion object { | ||
/** | ||
* @param networkSpeed in Kbps | ||
* @return ConnectionQuality derived from networkSpeed. | ||
*/ | ||
@JvmStatic | ||
fun getConnectionQualityFromSpeed(networkSpeed: Int): ConnectionQuality { | ||
val bandwidth = networkSpeed * 8 | ||
return getConnectionQualityFromBandWidth(bandwidth) | ||
} | ||
|
||
@JvmStatic | ||
fun getConnectionQualityFromBandWidth(bandwidth: Int): ConnectionQuality { | ||
return when { | ||
POOR.inRange(bandwidth) -> { | ||
POOR | ||
} | ||
MODERATE.inRange(bandwidth) -> { | ||
MODERATE | ||
} | ||
GOOD.inRange(bandwidth) -> { | ||
GOOD | ||
} | ||
EXCELLENT.inRange(bandwidth) -> { | ||
EXCELLENT | ||
} | ||
else -> { | ||
UNKNOWN | ||
} | ||
} | ||
} | ||
} | ||
} |