-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from Manabu-GT/develop
Develop
- Loading branch information
Showing
18 changed files
with
254 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
Network Statistics Extension Module | ||
=================================== | ||
|
||
**NetStatsModule** is an extension module which shows the total network usage of the application. | ||
The stats include all network interfaces, and both TCP and UDP usage. | ||
|
||
<img src="../art/overlay_with_netstats_module_small.png" width="50%" alt="DebugOverlay Screen Capture"> | ||
|
||
Setup | ||
----- | ||
|
||
Gradle: | ||
|
||
```groovy | ||
dependencies { | ||
debugCompile 'com.ms-square:debugoverlay:1.1.3' | ||
releaseCompile 'com.ms-square:debugoverlay-no-op:1.1.3' | ||
testCompile 'com.ms-square:debugoverlay-no-op:1.1.3' | ||
compile ('com.ms-square:debugoverlay-ext-netstats:1.1.3') { | ||
exclude module: 'debugoverlay' | ||
} | ||
} | ||
``` | ||
|
||
or | ||
|
||
```groovy | ||
dependencies { | ||
// this will use a full debugoverlay lib even in the test/release build | ||
compile 'com.ms-square:debugoverlay-ext-netstats:1.1.3' | ||
} | ||
``` | ||
|
||
Usage | ||
----- | ||
|
||
### Simple Example | ||
|
||
In your `Application` class: | ||
|
||
```java | ||
public class ExampleApplication extends Application { | ||
|
||
@Override public void onCreate() { | ||
super.onCreate(); | ||
new DebugOverlay.Builder(this) | ||
.modules(new CpuUsageModule(), | ||
new MemInfoModule(this), | ||
new FpsModule(), | ||
new NetStatsModule()) | ||
.build() | ||
.install(); | ||
// Normal app init code... | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
apply plugin: 'com.android.library' | ||
|
||
android { | ||
compileSdkVersion rootProject.ext.compileSdkVersion | ||
buildToolsVersion rootProject.ext.buildToolsVersion | ||
|
||
defaultConfig { | ||
minSdkVersion rootProject.ext.minSdkVersion | ||
targetSdkVersion rootProject.ext.targetSdkVersion | ||
} | ||
} | ||
|
||
dependencies { | ||
compile project(':debugoverlay') | ||
testCompile deps.junit | ||
} | ||
|
||
// for maven central deployment | ||
apply from: 'https://raw.githubusercontent.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# For maven central | ||
POM_ARTIFACT_ID=debugoverlay-ext-netstats | ||
POM_NAME=Network Statistics DebugOverlay extension for Android |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<manifest package="com.ms_square.debugoverlay_ext_netstats" /> |
95 changes: 95 additions & 0 deletions
95
...xt-netstats/src/main/java/com/ms_square/debugoverlay_ext_netstats/NetStatsDataModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.ms_square.debugoverlay_ext_netstats; | ||
|
||
import android.net.TrafficStats; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
import android.util.Log; | ||
|
||
import com.ms_square.debugoverlay.modules.BaseDataModule; | ||
|
||
import java.util.Locale; | ||
|
||
class NetStatsDataModule extends BaseDataModule<String> { | ||
|
||
private static final String TAG = "NetStatsDataModule"; | ||
|
||
private static final double BYTES_PER_GIGABYTE = 1000000000f; | ||
|
||
private static final double BYTES_PER_MEGABYTE = 1000000f; | ||
|
||
private static final double BYTES_PER_KILOBYTE = 1000f; | ||
|
||
private static final String HEADER = "Received: %8s/s\nSent: %12s/s"; | ||
|
||
private final Handler handler = new Handler(Looper.getMainLooper()); | ||
|
||
private final Runnable networkStatisticsQueryRunnable = new Runnable() { | ||
|
||
@Override | ||
public void run() { | ||
|
||
double totalBytesReceived = TrafficStats.getUidRxBytes(uid); | ||
double totalBytesSent = TrafficStats.getUidTxBytes(uid); | ||
|
||
if (totalBytesReceived == TrafficStats.UNSUPPORTED || totalBytesSent == TrafficStats.UNSUPPORTED) { | ||
Log.w(TAG, "The use of TrafficStats is not supported on this device."); | ||
return; | ||
} | ||
|
||
if (previousReceived >= 0 && previousSent >= 0) { | ||
received = (totalBytesReceived - previousReceived) / intervalSeconds; | ||
sent = (totalBytesSent - previousSent) / intervalSeconds; | ||
notifyObservers(); | ||
} | ||
previousReceived = totalBytesReceived; | ||
previousSent = totalBytesSent; | ||
|
||
handler.postDelayed(this, intervalMilliseconds); | ||
} | ||
}; | ||
|
||
private final int intervalMilliseconds; | ||
private final int uid; | ||
private final double intervalSeconds; | ||
|
||
private double previousReceived; | ||
private double previousSent; | ||
private double received; | ||
private double sent; | ||
|
||
public NetStatsDataModule(int intervalMilliseconds) { | ||
this.uid = android.os.Process.myUid(); | ||
this.intervalMilliseconds = intervalMilliseconds; | ||
this.intervalSeconds = intervalMilliseconds * 0.001; | ||
} | ||
|
||
@Override | ||
protected String getLatestData() { | ||
return String.format( | ||
Locale.US, HEADER, bytesToPrettyString(received), bytesToPrettyString(sent)); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
previousReceived = -1f; | ||
previousSent = -1f; | ||
handler.post(networkStatisticsQueryRunnable); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
handler.removeCallbacks(networkStatisticsQueryRunnable); | ||
} | ||
|
||
private String bytesToPrettyString(double bytes) { | ||
if (bytes >= BYTES_PER_GIGABYTE) { | ||
return String.format(Locale.US, "%.1f GB", bytes / BYTES_PER_GIGABYTE); | ||
} else if (bytes >= BYTES_PER_MEGABYTE) { | ||
return String.format(Locale.US, "%.1f MB", bytes / BYTES_PER_MEGABYTE); | ||
} else if (bytes >= BYTES_PER_KILOBYTE) { | ||
return String.format(Locale.US, "%.1f kB", bytes / BYTES_PER_KILOBYTE); | ||
} else { | ||
return String.format(Locale.US, "%.1f B", bytes); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ay-ext-netstats/src/main/java/com/ms_square/debugoverlay_ext_netstats/NetStatsModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.ms_square.debugoverlay_ext_netstats; | ||
|
||
import com.ms_square.debugoverlay.OverlayModule; | ||
import com.ms_square.debugoverlay.modules.SimpleViewModule; | ||
|
||
public class NetStatsModule extends OverlayModule<String> { | ||
|
||
private static final int DEFAULT_INTERVAL = 1000; // ms | ||
|
||
public NetStatsModule() { | ||
this(DEFAULT_INTERVAL); | ||
} | ||
|
||
public NetStatsModule(int interval) { | ||
super(new NetStatsDataModule(interval), new SimpleViewModule(R.layout.debugoverlay_netstats)); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
debugoverlay-ext-netstats/src/main/res/layout/debugoverlay_netstats.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:id="@id/debugoverlay_overlay_text" | ||
style="@style/debugoverlay_OverlayText" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:maxLines="2"> | ||
</TextView> |
17 changes: 17 additions & 0 deletions
17
...y-ext-netstats/src/test/java/com/ms_square/debugoverlay_ext_netstats/ExampleUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.ms_square.debugoverlay_ext_netstats; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Example local unit test, which will execute on the development machine (host). | ||
* | ||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
*/ | ||
public class ExampleUnitTest { | ||
@Test | ||
public void addition_isCorrect() throws Exception { | ||
assertEquals(4, 2 + 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
include ':sample', ':debugoverlay', ':debugoverlay-no-op', ':debugoverlay-ext-timber' | ||
include ':sample', ':debugoverlay', ':debugoverlay-no-op', ':debugoverlay-ext-timber', ':debugoverlay-ext-netstats' |