Skip to content

Commit

Permalink
Added ConnectionQuality enum
Browse files Browse the repository at this point in the history
  • Loading branch information
swapnil1104 committed Jun 25, 2020
1 parent 7db0c8c commit f8a106a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
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
}
}
}
}
}

0 comments on commit f8a106a

Please sign in to comment.