Skip to content

Commit

Permalink
Removed the custom parser and used TrafficStats and fixed race condit…
Browse files Browse the repository at this point in the history
…ion with starting/stopping
  • Loading branch information
Alex Petrescu authored and Alex Petrescu committed Sep 9, 2015
1 parent 5198dfd commit b4871d9
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 595 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;


public class MainActivity extends Activity {
Expand Down Expand Up @@ -105,7 +106,10 @@ protected Void doInBackground(String... url) {
String imageURL = url[0];
try {
// Open a stream to download the image from our URL.
InputStream input = new URL(imageURL).openStream();
URLConnection connection = new URL(imageURL).openConnection();
connection.setUseCaches(false);
connection.connect();
InputStream input = connection.getInputStream();
try {
byte[] buffer = new byte[1024];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,34 @@

package com.facebook.network.connectionclass;

import android.os.*;
import android.os.Process;

import javax.annotation.Nullable;
import javax.annotation.Nonnull;
import android.net.TrafficStats;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;

import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.Nonnull;

/**
* Class used to read from the file {@code /proc/net/xt_qtaguid/stats} periodically, in order to
* determine a ConnectionClass.
* Class used to read from TrafficStats periodically, in order to determine a ConnectionClass.
*/
public class DeviceBandwidthSampler {

/**
* Time between polls in ms.
*/
static final long SAMPLE_TIME = 1000;

/**
* The DownloadBandwidthManager that keeps track of the moving average and ConnectionClass.
*/
private final ConnectionClassManager mConnectionClassManager;

private AtomicInteger mSamplingCounter;

private Handler mHandler;
private SamplingHandler mHandler;
private HandlerThread mThread;

private long mLastTimeReading;
private static long sPreviousBytes = -1;

// Singleton.
private static class DeviceBandwidthSamplerHolder {
Expand Down Expand Up @@ -70,7 +68,7 @@ private DeviceBandwidthSampler(
*/
public void startSampling() {
if (mSamplingCounter.getAndIncrement() == 0) {
mHandler.sendEmptyMessage(SamplingHandler.MSG_START);
mHandler.startSamplingThread();
mLastTimeReading = SystemClock.elapsedRealtime();
}
}
Expand All @@ -81,57 +79,36 @@ public void startSampling() {
*/
public void stopSampling() {
if (mSamplingCounter.decrementAndGet() == 0) {
mHandler.sendEmptyMessage(SamplingHandler.MSG_STOP);
mHandler.stopSamplingThread();
addFinalSample();
}
}

private class SamplingHandler extends Handler {
static final int MSG_START = 1;
static final int MSG_STOP = 2;

public SamplingHandler(Looper looper) {
super(looper);
}

@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_START:
addSample();
sendEmptyMessageDelayed(MSG_START, SAMPLE_TIME);
break;
case MSG_STOP:
addFinalSample();
removeMessages(MSG_START);
break;
default:
throw new IllegalArgumentException("Unknown what=" + msg.what);
}
}

/**
* Method for polling for the change in total bytes since last update and
* adding it to the BandwidthManager.
*/
private void addSample() {
long byteDiff = QTagParser.getInstance().parseDataUsageForUidAndTag(Process.myUid());
/**
* Method for polling for the change in total bytes since last update and
* adding it to the BandwidthManager.
*/
protected void addSample() {
long newBytes = TrafficStats.getTotalRxBytes();
long byteDiff = newBytes - sPreviousBytes;
if (sPreviousBytes >= 0) {
synchronized (this) {
long curTimeReading = SystemClock.elapsedRealtime();
if (byteDiff != -1) {
mConnectionClassManager.addBandwidth(byteDiff, curTimeReading - mLastTimeReading);
}
mConnectionClassManager.addBandwidth(byteDiff, curTimeReading - mLastTimeReading);

mLastTimeReading = curTimeReading;
}
}
sPreviousBytes = newBytes;
}

/**
* Resets previously read byte count after recording a sample, so that
* we don't count bytes downloaded in between sampling sessions.
*/
private void addFinalSample() {
addSample();
QTagParser.resetPreviousBytes();
}
/**
* Resets previously read byte count after recording a sample, so that
* we don't count bytes downloaded in between sampling sessions.
*/
protected void addFinalSample() {
addSample();
sPreviousBytes = -1;
}

/**
Expand All @@ -140,4 +117,38 @@ private void addFinalSample() {
public boolean isSampling() {
return (mSamplingCounter.get() != 0);
}

private class SamplingHandler extends Handler {
/**
* Time between polls in ms.
*/
static final long SAMPLE_TIME = 1000;

static private final int MSG_START = 1;

public SamplingHandler(Looper looper) {
super(looper);
}

@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_START:
addSample();
sendEmptyMessageDelayed(MSG_START, SAMPLE_TIME);
break;
default:
throw new IllegalArgumentException("Unknown what=" + msg.what);
}
}


public void startSamplingThread() {
sendEmptyMessage(SamplingHandler.MSG_START);
}

public void stopSamplingThread() {
removeMessages(SamplingHandler.MSG_START);
}
}
}

This file was deleted.

Loading

0 comments on commit b4871d9

Please sign in to comment.