diff --git a/library/src/main/java/com/flipkart/okhttpstats/handler/PersistentStatsHandler.java b/library/src/main/java/com/flipkart/okhttpstats/handler/PersistentStatsHandler.java index 2dbf26c..d0f2f95 100644 --- a/library/src/main/java/com/flipkart/okhttpstats/handler/PersistentStatsHandler.java +++ b/library/src/main/java/com/flipkart/okhttpstats/handler/PersistentStatsHandler.java @@ -32,6 +32,7 @@ import android.text.TextUtils; import android.util.Log; +import com.flipkart.okhttpstats.model.ConnectionQuality; import com.flipkart.okhttpstats.model.RequestStats; import com.flipkart.okhttpstats.toolbox.NetworkStat; import com.flipkart.okhttpstats.toolbox.PreferenceManager; @@ -65,6 +66,7 @@ public class PersistentStatsHandler implements NetworkRequestStatsHandler { private final NetworkStat mNetworkStat; private float mCurrentAvgSpeed; private final ConnectivityManager mConnectivityManager; + private ConnectionQuality mConnectionQuality; public PersistentStatsHandler(Context context) { this.mPreferenceManager = new PreferenceManager(context); @@ -73,6 +75,7 @@ public PersistentStatsHandler(Context context) { this.mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); this.mNetworkStat = new NetworkStat(); this.mCurrentAvgSpeed = mPreferenceManager.getAverageSpeed(getNetworkKey(getActiveNetworkInfo())); + this.mConnectionQuality = ConnectionQuality.getConnectionQualityFromSpeed((int) mCurrentAvgSpeed); } @VisibleForTesting @@ -83,6 +86,7 @@ public PersistentStatsHandler(Context context) { this.mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); this.mNetworkStat = new NetworkStat(); this.mCurrentAvgSpeed = mPreferenceManager.getAverageSpeed(getNetworkKey(getActiveNetworkInfo())); + this.mConnectionQuality = ConnectionQuality.getConnectionQualityFromSpeed((int) mCurrentAvgSpeed); } /** @@ -137,6 +141,16 @@ public float getAverageNetworkSpeed() { return mCurrentAvgSpeed; } + /** + * Exposed to client to get the current connection quality. + * Possible values include: UNKNOWN, POOR, MODERATE, GOOD, EXCELLENT + * + * @return the current connection quality + */ + public ConnectionQuality getConnectionQuality() { + return this.mConnectionQuality; + } + @Override public void onResponseReceived(final RequestStats requestStats) { if (Utils.isLoggingEnabled) { @@ -157,6 +171,8 @@ public void onResponseReceived(final RequestStats requestStats) { //calculate the new average speed double newAvgSpeed = mNetworkStat.mCurrentAvgSpeed; mCurrentAvgSpeed = (float) ((mCurrentAvgSpeed + newAvgSpeed) / 2); + //calculate the new connection quality + mConnectionQuality = ConnectionQuality.getConnectionQualityFromSpeed((int) mCurrentAvgSpeed); //save it in shared preference String networkKey = getNetworkKey(getActiveNetworkInfo()); mPreferenceManager.setAverageSpeed(networkKey, mCurrentAvgSpeed); diff --git a/library/src/main/java/com/flipkart/okhttpstats/model/ConnectionQuality.kt b/library/src/main/java/com/flipkart/okhttpstats/model/ConnectionQuality.kt index 4dc0962..2c67597 100644 --- a/library/src/main/java/com/flipkart/okhttpstats/model/ConnectionQuality.kt +++ b/library/src/main/java/com/flipkart/okhttpstats/model/ConnectionQuality.kt @@ -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 + } + } + } + } +} \ No newline at end of file