Log number of connections per domain per applications

This commit is contained in:
M66B 2016-02-19 20:58:11 +01:00
parent 6f6b761ebd
commit 45fe542e91
2 changed files with 22 additions and 6 deletions

View File

@ -51,6 +51,7 @@ public class AdapterAccess extends CursorAdapter {
private int colBlock;
private int colSent;
private int colReceived;
private int colConnections;
private int colorText;
private int colorOn;
@ -68,6 +69,7 @@ public class AdapterAccess extends CursorAdapter {
colBlock = cursor.getColumnIndex("block");
colSent = cursor.getColumnIndex("sent");
colReceived = cursor.getColumnIndex("received");
colConnections = cursor.getColumnIndex("connections");
TypedArray ta = context.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorSecondary});
try {
@ -101,6 +103,7 @@ public class AdapterAccess extends CursorAdapter {
int block = cursor.getInt(colBlock);
long sent = cursor.isNull(colSent) ? -1 : cursor.getLong(colSent);
long received = cursor.isNull(colReceived) ? -1 : cursor.getLong(colReceived);
int connections = cursor.isNull(colConnections) ? -1 : cursor.getInt(colConnections);
// Get views
TextView tvTime = (TextView) view.findViewById(R.id.tvTime);
@ -157,18 +160,21 @@ public class AdapterAccess extends CursorAdapter {
else
tvDest.setTextColor(colorOff);
tvTraffic.setVisibility(sent > 0 || received > 0 ? View.VISIBLE : View.GONE);
tvTraffic.setVisibility(connections > 0 || sent > 0 || received > 0 ? View.VISIBLE : View.GONE);
StringBuilder sb = new StringBuilder();
sb.append('#').append(connections).append(' ');
if (sent > 1024 * 1204 * 1024L || received > 1024 * 1024 * 1024L)
tvTraffic.setText(context.getString(R.string.msg_gb,
sb.append(context.getString(R.string.msg_gb,
(sent > 0 ? sent / (1024 * 1024 * 1024f) : 0),
(received > 0 ? received / (1024 * 1024 * 1024f) : 0)));
else if (sent > 1204 * 1024L || received > 1024 * 1024L)
tvTraffic.setText(context.getString(R.string.msg_mb,
sb.append(context.getString(R.string.msg_mb,
(sent > 0 ? sent / (1024 * 1024f) : 0),
(received > 0 ? received / (1024 * 1024f) : 0)));
else
tvTraffic.setText(context.getString(R.string.msg_kb,
sb.append(context.getString(R.string.msg_kb,
(sent > 0 ? sent / 1024f : 0),
(received > 0 ? received / 1024f : 0)));
tvTraffic.setText(sb.toString());
}
}

View File

@ -41,7 +41,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "NetGuard.Database";
private static final String DB_NAME = "Netguard";
private static final int DB_VERSION = 18;
private static final int DB_VERSION = 19;
private static boolean once = true;
private static List<LogChangedListener> logChangedListeners = new ArrayList<>();
@ -156,6 +156,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
", block INTEGER NOT NULL" +
", sent INTEGER NULL" +
", received INTEGER NULL" +
", connections INTEGER NULL" +
");");
db.execSQL("CREATE UNIQUE INDEX idx_access ON access(uid, version, protocol, daddr, dport)");
db.execSQL("CREATE INDEX idx_access_block ON access(block)");
@ -302,6 +303,11 @@ public class DatabaseHelper extends SQLiteOpenHelper {
db.execSQL("CREATE INDEX IF NOT EXISTS idx_dns_resource ON dns(resource)");
oldVersion = 18;
}
if (oldVersion < 19) {
if (!columnExists(db, "access", "connections"))
db.execSQL("ALTER TABLE access ADD COLUMN connections INTEGER NULL");
oldVersion = 19;
}
if (oldVersion == DB_VERSION) {
db.setVersion(oldVersion);
@ -511,20 +517,24 @@ public class DatabaseHelper extends SQLiteOpenHelper {
Integer.toString(usage.DPort)
};
Cursor cursor = db.query("access", new String[]{"sent", "received"}, selection, selectionArgs, null, null, null);
Cursor cursor = db.query("access", new String[]{"sent", "received", "connections"}, selection, selectionArgs, null, null, null);
long sent = 0;
long received = 0;
int connections = 0;
int colSent = cursor.getColumnIndex("sent");
int colReceived = cursor.getColumnIndex("received");
int colConnections = cursor.getColumnIndex("connections");
if (cursor.moveToNext()) {
sent = cursor.isNull(colSent) ? 0 : cursor.getLong(colSent);
received = cursor.isNull(colReceived) ? 0 : cursor.getLong(colReceived);
connections = cursor.isNull(colConnections) ? 0 : cursor.getInt(colConnections);
}
cursor.close();
ContentValues cv = new ContentValues();
cv.put("sent", sent + usage.Sent);
cv.put("received", received + usage.Received);
cv.put("connections", connections + 1);
int rows = db.update("access", cv, selection, selectionArgs);
if (rows != 1)