1
0
Fork 0
mirror of https://github.com/M66B/NetGuard.git synced 2024-12-30 20:05:04 +00:00

Option to show top network using applications

This commit is contained in:
M66B 2015-12-13 08:56:13 +01:00
parent 3455de44da
commit 6c0dabae3d
3 changed files with 38 additions and 31 deletions

View file

@ -293,11 +293,12 @@ public class SinkholeService extends VpnService {
}
private void updateStats() {
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.traffic);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
// Schedule next update
mServiceHandler.sendEmptyMessageDelayed(MSG_STATS_UPDATE, STATS_FREQUENCY);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.traffic);
// Cleanup
while (gt.size() >= STATS_POINTS) {
gt.remove(0);
@ -321,38 +322,40 @@ public class SinkholeService extends VpnService {
}
// Calculate application speeds
if (app.size() == 0)
for (ApplicationInfo ainfo : getPackageManager().getInstalledApplications(0))
app.put(ainfo, TrafficStats.getUidTxBytes(ainfo.uid) + TrafficStats.getUidRxBytes(ainfo.uid));
if (prefs.getBoolean("show_top", false)) {
if (app.size() == 0)
for (ApplicationInfo ainfo : getPackageManager().getInstalledApplications(0))
app.put(ainfo, TrafficStats.getUidTxBytes(ainfo.uid) + TrafficStats.getUidRxBytes(ainfo.uid));
else if (t > 0) {
TreeMap<Float, ApplicationInfo> mapSpeed = new TreeMap<>();
float dt = (ct - t) / 1000f;
for (ApplicationInfo aInfo : app.keySet()) {
long bytes = TrafficStats.getUidTxBytes(aInfo.uid) + TrafficStats.getUidRxBytes(aInfo.uid);
float speed = (bytes - app.get(aInfo)) / dt;
if (speed > 0) {
mapSpeed.put(speed, aInfo);
app.put(aInfo, bytes);
else if (t > 0) {
TreeMap<Float, ApplicationInfo> mapSpeed = new TreeMap<>();
float dt = (ct - t) / 1000f;
for (ApplicationInfo aInfo : app.keySet()) {
long bytes = TrafficStats.getUidTxBytes(aInfo.uid) + TrafficStats.getUidRxBytes(aInfo.uid);
float speed = (bytes - app.get(aInfo)) / dt;
if (speed > 0) {
mapSpeed.put(speed, aInfo);
app.put(aInfo, bytes);
}
}
}
StringBuilder sb = new StringBuilder();
int i = 0;
for (float s : mapSpeed.keySet()) {
if (i++ >= 3)
break;
if (s < 1000 * 1000)
sb.append(getString(R.string.msg_kbsec, s / 1000));
else
sb.append(getString(R.string.msg_mbsec, s / 1000 / 1000));
sb.append(' ');
sb.append(getPackageManager().getApplicationLabel(mapSpeed.get(s)).toString());
sb.append("\r\n");
StringBuilder sb = new StringBuilder();
int i = 0;
for (float s : mapSpeed.keySet()) {
if (i++ >= 3)
break;
if (s < 1000 * 1000)
sb.append(getString(R.string.msg_kbsec, s / 1000));
else
sb.append(getString(R.string.msg_mbsec, s / 1000 / 1000));
sb.append(' ');
sb.append(getPackageManager().getApplicationLabel(mapSpeed.get(s)).toString());
sb.append("\r\n");
}
if (sb.length() > 0)
sb.setLength(sb.length() - 2);
remoteViews.setTextViewText(R.id.tvTop, sb.toString());
}
if (sb.length() > 0)
sb.setLength(sb.length() - 2);
remoteViews.setTextViewText(R.id.tvTop, sb.toString());
}
t = ct;
@ -368,7 +371,6 @@ public class SinkholeService extends VpnService {
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.TRANSPARENT);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
float base = Integer.parseInt(prefs.getString("stats_base", "5")) * 1000f;
// Determine max

View file

@ -26,6 +26,7 @@ These issues are caused by bugs in Android, or in the software provided by the m
<string name="setting_system">Manage system applications</string>
<string name="setting_stats">Show speed notification</string>
<string name="setting_stats_base">Speed baseline: %s KB/s</string>
<string name="setting_stats_top">Show top network using applications</string>
<string name="setting_auto">Auto enable after %1$s minutes</string>
<string name="setting_dark">Use dark theme</string>
<string name="setting_wifi_home">Wi-Fi home networks: %1$s</string>

View file

@ -41,6 +41,10 @@
android:defaultValue="5"
android:inputType="number"
android:key="stats_base" />
<SwitchPreference
android:defaultValue="false"
android:key="show_top"
android:title="@string/setting_stats_top" />
<EditTextPreference
android:defaultValue="0"
android:inputType="number"