Skip to content

Commit

Permalink
Merge pull request #42 from flipkart-incubator/cleanup
Browse files Browse the repository at this point in the history
Code cleanup and Test Fix
  • Loading branch information
yasirmhd authored Jan 18, 2017
2 parents 022f0e9 + 5be7658 commit 89e3d5b
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 131 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
sudo: true
language: android
jdk:
- oraclejdk8
android:
components:
- tools
- platform-tools
- build-tools-24.0.0
- android-24
- build-tools-25.0.2
- android-25
- extra-android-support
- extra-android-m2repository

- extra-google-m2repository
script:
- ./gradlew test
15 changes: 10 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ repositories {
mavenCentral()
}

allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
compileSdkVersion 25
buildToolsVersion "25.0.2"
lintOptions {
abortOnError false
}
defaultConfig {
minSdkVersion 14
targetSdkVersion 24
targetSdkVersion 25
versionCode 1
versionName "1.0"
useLibrary 'org.apache.http.legacy'
Expand All @@ -28,8 +33,8 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.android.volley:volley:1.0.0'
compile project(':library')
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:2.2.3'

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

dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:2.2.3'
}
Expand All @@ -39,14 +39,14 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

android {
compileSdkVersion 24
buildToolsVersion '24.0.0'
compileSdkVersion 25
buildToolsVersion '25.0.2'
lintOptions {
abortOnError false
}
defaultConfig {
minSdkVersion 14
targetSdkVersion 24
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Expand All @@ -60,11 +60,11 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-annotations:25.0.1'
compile 'com.android.support:support-annotations:25.1.0'
compile 'com.squareup.okhttp3:okhttp:3.4.2'

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'com.squareup.okhttp3:mockwebserver:3.4.2'
testCompile 'org.robolectric:robolectric:3.1.4'
testCompile 'org.robolectric:robolectric:3.2.2'
}

This file was deleted.

14 changes: 2 additions & 12 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.flipkart.okhttpstats">

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true">

</application>

</manifest>
<manifest package="com.flipkart.okhttpstats">
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class NetworkInterceptor implements Interceptor {
private final AtomicInteger mNextRequestId = new AtomicInteger(1);
private boolean mEnabled = true;

private NetworkInterceptor(Builder builder) {
NetworkInterceptor(Builder builder) {
mEnabled = builder.mEnabled;
if (builder.mInterpreter == null) {
throw new IllegalStateException("NetworkInterpreter cannot be null");
Expand Down Expand Up @@ -95,9 +95,9 @@ public static class TimeInfo {
* Builder Pattern for {@link NetworkInterceptor}
*/
public static class Builder {
private boolean mEnabled = true;
private boolean mIsLoggingEnabled = false;
private NetworkInterpreter mInterpreter;
boolean mEnabled = true;
boolean mIsLoggingEnabled = false;
NetworkInterpreter mInterpreter;

/**
* To enable/disable the calls to {@link NetworkInterpreter}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ForwardingResponse implements OnResponseListener {

private OnStatusCodeAwareResponseListener mOnStatusCodeAwareResponseListener;


public ForwardingResponse(OnStatusCodeAwareResponseListener onStatusCodeAwareResponseListener) {
mOnStatusCodeAwareResponseListener = onStatusCodeAwareResponseListener;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class PersistentStatsHandler implements NetworkRequestStatsHandler {
private static final String MOBILE_NETWORK = "mobile";
private static final String UNKNOWN_NETWORK = "unknown";
private final PreferenceManager mPreferenceManager;
private Set<OnResponseListener> mOnResponseListeners = new HashSet<>();
Set<OnResponseListener> mOnResponseListeners = new HashSet<>();
private int mResponseCount = 0;
private int MAX_SIZE;
private WifiManager mWifiManager;
Expand All @@ -85,11 +85,6 @@ public PersistentStatsHandler(Context context) {
this.mCurrentAvgSpeed = mPreferenceManager.getAverageSpeed(getNetworkKey(getActiveNetworkInfo()));
}

@VisibleForTesting
Set<OnResponseListener> getOnResponseListeners() {
return mOnResponseListeners;
}

/**
* Client can call this to get the current network info
*
Expand Down Expand Up @@ -134,7 +129,7 @@ public void setMaxSizeForPersistence(int size) {
}

/**
* Exposed to the client so that he can get the average network speed
* Exposed to the client to get the average network speed
*
* @return avg speed
*/
Expand All @@ -159,21 +154,20 @@ public void onResponseReceived(final RequestStats requestStats) {
synchronized (this) {
mResponseCount += 1;
if (mResponseCount >= MAX_SIZE) {
mCurrentAvgSpeed = calculateNewSpeed(mCurrentAvgSpeed);
saveToSharedPreference(mCurrentAvgSpeed);
//calculate the new average speed
double newAvgSpeed = mNetworkStat.mCurrentAvgSpeed;
mCurrentAvgSpeed = (float) ((mCurrentAvgSpeed + newAvgSpeed) / 2);
//save it in shared preference
String networkKey = getNetworkKey(getActiveNetworkInfo());
mPreferenceManager.setAverageSpeed(networkKey, mCurrentAvgSpeed);
//reset the response count
mResponseCount = 0;
}
}

mNetworkStat.addRequestStat(requestStats);
}

private float calculateNewSpeed(float currentAvgSpeed) {
double newAvgSpeed = mNetworkStat.getCurrentAvgSpeed();
currentAvgSpeed = (float) ((currentAvgSpeed + newAvgSpeed) / 2);
return currentAvgSpeed;
}

@Override
public void onHttpExchangeError(RequestStats requestStats, IOException e) {
if (Utils.isLoggingEnabled) {
Expand All @@ -200,19 +194,6 @@ public void onResponseInputStreamError(RequestStats requestStats, Exception e) {
}
}

/**
* Saves the network avg speed in the Shared Pref
*
* @param currentAvgSpeed : float
*/
private void saveToSharedPreference(float currentAvgSpeed) {
if (Utils.isLoggingEnabled) {
Log.d("avg speed", "saveToSharedPreference: " + mNetworkStat.getCurrentAvgSpeed());
}
String networkKey = getNetworkKey(getActiveNetworkInfo());
mPreferenceManager.setAverageSpeed(networkKey, currentAvgSpeed);
}

/**
* Generates the network key based on the type of network
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
public class DefaultInterpreter implements NetworkInterpreter {
private static final String HOST_NAME = "HOST";
private static final String CONTENT_LENGTH = "Content-Length";
private NetworkEventReporter mEventReporter;
NetworkEventReporter mEventReporter;

public DefaultInterpreter(NetworkEventReporter mEventReporter) {
this.mEventReporter = mEventReporter;
Expand All @@ -60,8 +60,8 @@ public DefaultInterpreter(NetworkEventReporter mEventReporter) {
public Response interpretResponseStream(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, Response response) throws IOException {
ResponseBody responseBody = response.body();

final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request), request.header(HOST_NAME));
final OkHttpInspectorResponse okHttpInspectorResponse = new OkHttpInspectorResponse(requestId, response.code(), Utils.contentLength(response), timeInfo.mStartTime, timeInfo.mEndTime, responseBody);
final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request.headers()), request.url().host());
final OkHttpInspectorResponse okHttpInspectorResponse = new OkHttpInspectorResponse(requestId, response.code(), Utils.contentLength(response.headers()), timeInfo.mStartTime, timeInfo.mEndTime, responseBody);
//if response does not have content length, using CountingInputStream to read its bytes
if (response.header(CONTENT_LENGTH) == null) {
InputStream responseStream = null;
Expand Down Expand Up @@ -94,6 +94,7 @@ public void onEOF(long bytesRead) {
//if response has content length, notify the event reporter that response has been received.
mEventReporter.responseReceived(okHttpInspectorRequest, okHttpInspectorResponse);
}

return response;
}

Expand All @@ -102,7 +103,7 @@ public void interpretError(int requestId, NetworkInterceptor.TimeInfo timeInfo,
if (Utils.isLoggingEnabled) {
Log.d("Error response: ", e.getMessage());
}
final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request), request.header(HOST_NAME));
final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request.headers()), request.header(HOST_NAME));
mEventReporter.httpExchangeError(okHttpInspectorRequest, e);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
/*
* The MIT License
*
* Copyright 2016 Flipkart Internet Pvt. Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
* the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.flipkart.okhttpstats.toolbox;

public class HttpStatusCode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@ public final class NetworkStat {
private double mPeakSpeed = 0;
private Queue<RequestStats> mRequestStatQueue;
private double mTotalSize = 0;
private double mCurrentAvgSpeed = 0;
public double mCurrentAvgSpeed = 0;

public NetworkStat() {
mRequestStatQueue = new LinkedList<>();
}

public double getCurrentAvgSpeed() {
return mCurrentAvgSpeed;
}

public synchronized void addRequestStat(final RequestStats requestStats) {
if (requestStats != null) {
long apiSpeed = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,12 @@
package com.flipkart.okhttpstats.toolbox;

import okhttp3.Headers;
import okhttp3.Request;
import okhttp3.Response;

public class Utils {

public static boolean isLoggingEnabled = false;

public static long contentLength(Request request) {
return contentLength(request.headers());
}

public static long contentLength(Response response) {
return contentLength(response.headers());
}

private static long contentLength(Headers headers) {
public static long contentLength(Headers headers) {
return stringToLong(headers.get("Content-Length"));
}

Expand Down
3 changes: 0 additions & 3 deletions library/src/main/res/values/strings.xml

This file was deleted.

Loading

0 comments on commit 89e3d5b

Please sign in to comment.