|
|
|
@ -11,24 +11,22 @@ import java.text.DecimalFormat; |
|
|
|
public class Traffic { |
|
|
|
public class Traffic { |
|
|
|
|
|
|
|
|
|
|
|
private static final DecimalFormat format = new DecimalFormat("#.0"); |
|
|
|
private static final DecimalFormat format = new DecimalFormat("#.0"); |
|
|
|
|
|
|
|
private static final int UID = App.get().getApplicationInfo().uid; |
|
|
|
private static final String UNIT_KB = " KB/s"; |
|
|
|
private static final String UNIT_KB = " KB/s"; |
|
|
|
private static final String UNIT_MB = " MB/s"; |
|
|
|
private static final String UNIT_MB = " MB/s"; |
|
|
|
|
|
|
|
|
|
|
|
private static long lastTotalRxBytes; |
|
|
|
private static long lastTotalRxBytes; |
|
|
|
private static long lastTimeStamp; |
|
|
|
private static long lastTimeStamp; |
|
|
|
|
|
|
|
|
|
|
|
public static void setSpeed(TextView view) { |
|
|
|
public static void setSpeed(TextView view) { |
|
|
|
if (unsupported()) return; |
|
|
|
if (TrafficStats.getUidRxBytes(UID) == TrafficStats.UNSUPPORTED) return; |
|
|
|
view.setText(getSpeed()); |
|
|
|
|
|
|
|
view.setVisibility(View.VISIBLE); |
|
|
|
view.setVisibility(View.VISIBLE); |
|
|
|
} |
|
|
|
view.setText(getSpeed()); |
|
|
|
|
|
|
|
|
|
|
|
private static boolean unsupported() { |
|
|
|
|
|
|
|
return TrafficStats.getUidRxBytes(App.get().getApplicationInfo().uid) == TrafficStats.UNSUPPORTED; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static String getSpeed() { |
|
|
|
private static String getSpeed() { |
|
|
|
long nowTimeStamp = System.currentTimeMillis(); |
|
|
|
long nowTimeStamp = System.currentTimeMillis(); |
|
|
|
long nowTotalRxBytes = TrafficStats.getTotalRxBytes() / 1024; |
|
|
|
long nowTotalRxBytes = TrafficStats.getUidRxBytes(UID) / 1024; |
|
|
|
long speed = (nowTotalRxBytes - lastTotalRxBytes) * 1000 / Math.max(nowTimeStamp - lastTimeStamp, 1); |
|
|
|
long speed = (nowTotalRxBytes - lastTotalRxBytes) * 1000 / Math.max(nowTimeStamp - lastTimeStamp, 1); |
|
|
|
lastTimeStamp = nowTimeStamp; |
|
|
|
lastTimeStamp = nowTimeStamp; |
|
|
|
lastTotalRxBytes = nowTotalRxBytes; |
|
|
|
lastTotalRxBytes = nowTotalRxBytes; |
|
|
|
|