Log network usage at shutdown.

This commit is contained in:
akwizgran
2017-11-08 14:46:56 +00:00
parent e6b1597fa7
commit 10f41ef157
2 changed files with 50 additions and 0 deletions

View File

@@ -38,6 +38,8 @@ public class AppModule {
static class EagerSingletons {
@Inject
AndroidNotificationManager androidNotificationManager;
@Inject
NetworkUsageLogger networkUsageLogger;
}
private final Application application;
@@ -173,4 +175,12 @@ public class AppModule {
ScreenFilterMonitorImpl screenFilterMonitor) {
return screenFilterMonitor;
}
@Provides
NetworkUsageLogger provideNetworkUsageLogger(
LifecycleManager lifecycleManager) {
NetworkUsageLogger networkUsageLogger = new NetworkUsageLogger();
lifecycleManager.registerService(networkUsageLogger);
return networkUsageLogger;
}
}

View File

@@ -0,0 +1,40 @@
package org.briarproject.briar.android;
import android.net.TrafficStats;
import android.os.Process;
import org.briarproject.bramble.api.lifecycle.Service;
import org.briarproject.bramble.api.lifecycle.ServiceException;
import java.util.logging.Logger;
import static java.util.logging.Level.INFO;
class NetworkUsageLogger implements Service {
private static final Logger LOG =
Logger.getLogger(NetworkUsageLogger.class.getName());
private volatile long startTime, rxBytes, txBytes;
@Override
public void startService() throws ServiceException {
startTime = System.currentTimeMillis();
int uid = Process.myUid();
rxBytes = TrafficStats.getUidRxBytes(uid);
txBytes = TrafficStats.getUidTxBytes(uid);
}
@Override
public void stopService() throws ServiceException {
if (LOG.isLoggable(INFO)) {
long sessionDuration = System.currentTimeMillis() - startTime;
int uid = Process.myUid();
long rx = TrafficStats.getUidRxBytes(uid) - rxBytes;
long tx = TrafficStats.getUidTxBytes(uid) - txBytes;
LOG.info("Duration " + (sessionDuration / 1000) + " seconds");
LOG.info("Received " + rx + " bytes");
LOG.info("Sent " + tx + " bytes");
}
}
}