package com.upc.pbe.upcnews;

import android.net.TrafficStats;
import android.util.Log;

//Mesura la velocitat (Bandwidth) d'Internet
public class BandwidthMeasurer {
	private final static String TAG = "Mesurador de bandwith";

	public double Measure(long rxBytes, long time) {
		long AfterTime = System.currentTimeMillis();
		double bps;
		long TotalRxAfterTest = TrafficStats.getTotalRxBytes();
		double TimeDifference = AfterTime - time;
		double rxDiff = TotalRxAfterTest - rxBytes;
		Log.d(TAG, "Diferencia de temps = " + TimeDifference / 1000);
		Log.d(TAG, "Diferencia de bytes = " + rxDiff);
		if (rxDiff != 0) {
			bps = ((rxDiff * 8) / (TimeDifference / 1000)); // total de rx en
															// bits per segon.
			Log.d(TAG, "Velocitat actual (KB/s): " + (bps / 8e3));
		} else {
			bps = -1; // No s'han trasmès dades
		}
		return bps;
	}
}
