Skip to content

Commit

Permalink
Merge pull request #55 from swapnil1104/master
Browse files Browse the repository at this point in the history
Added ConnectionQuality enum
  • Loading branch information
anirudhramanan authored Jun 25, 2020
2 parents e0f2bc5 + f8a106a commit dc85e67
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.72'
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
11 changes: 11 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ buildscript {

apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 28
Expand All @@ -57,13 +58,23 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

repositories {
mavenCentral()
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.annotation:annotation:1.0.2'
implementation 'com.squareup.okhttp3:okhttp:3.14.2'

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.19.1'
testImplementation 'com.squareup.okhttp3:mockwebserver:3.6.0'
Expand Down
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
@@ -0,0 +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 dc85e67

Please sign in to comment.