diff --git a/app/src/main/java/com/sun/mail/iap/Protocol.java b/app/src/main/java/com/sun/mail/iap/Protocol.java index cd3baac42c..f85e95e807 100644 --- a/app/src/main/java/com/sun/mail/iap/Protocol.java +++ b/app/src/main/java/com/sun/mail/iap/Protocol.java @@ -635,6 +635,9 @@ public class Protocol { // ignore it } socket = null; + if (traceInput != null && traceOutput != null) + eu.faircode.email.TrafficStatsHelper.report(host, "IMAP", + traceOutput.getSent(), traceInput.getReceived()); } } diff --git a/app/src/main/java/com/sun/mail/pop3/Protocol.java b/app/src/main/java/com/sun/mail/pop3/Protocol.java index 56423d1b20..74ebf647a7 100644 --- a/app/src/main/java/com/sun/mail/pop3/Protocol.java +++ b/app/src/main/java/com/sun/mail/pop3/Protocol.java @@ -806,6 +806,9 @@ class Protocol { try { if (socket != null) socket.close(); + if (traceInput != null && traceOutput != null) + eu.faircode.email.TrafficStatsHelper.report(host, "POP3", + traceOutput.getSent(), traceInput.getReceived()); } catch (IOException ex) { // ignore it } finally { diff --git a/app/src/main/java/com/sun/mail/smtp/SMTPTransport.java b/app/src/main/java/com/sun/mail/smtp/SMTPTransport.java index 826a88189a..666cb0a4df 100644 --- a/app/src/main/java/com/sun/mail/smtp/SMTPTransport.java +++ b/app/src/main/java/com/sun/mail/smtp/SMTPTransport.java @@ -1472,6 +1472,9 @@ public class SMTPTransport extends Transport { serverOutput = null; serverInput = null; lineInputStream = null; + if (traceInput != null && traceOutput != null) + eu.faircode.email.TrafficStatsHelper.report(host, "SMTP", + traceOutput.getSent(), traceInput.getReceived()); if (super.isConnected()) // only notify if already connected super.close(); } diff --git a/app/src/main/java/com/sun/mail/util/TraceInputStream.java b/app/src/main/java/com/sun/mail/util/TraceInputStream.java index fceb7ac45a..5ec063df26 100644 --- a/app/src/main/java/com/sun/mail/util/TraceInputStream.java +++ b/app/src/main/java/com/sun/mail/util/TraceInputStream.java @@ -33,6 +33,8 @@ public class TraceInputStream extends FilterInputStream { private boolean quote = false; private OutputStream traceOut; + private long pos = 0; + /** * Creates an input stream filter built on top of the specified * input stream. @@ -82,6 +84,7 @@ public class TraceInputStream extends FilterInputStream { @Override public int read() throws IOException { int b = in.read(); + pos++; if (trace && b != -1) { if (quote) writeByte(b); @@ -100,6 +103,7 @@ public class TraceInputStream extends FilterInputStream { @Override public int read(byte b[], int off, int len) throws IOException { int count = in.read(b, off, len); + pos += count; if (trace && count != -1) { if (quote) { for (int i = 0; i < count; i++) @@ -137,4 +141,8 @@ public class TraceInputStream extends FilterInputStream { traceOut.write(b); } } + + public long getReceived() { + return pos; + } } diff --git a/app/src/main/java/com/sun/mail/util/TraceOutputStream.java b/app/src/main/java/com/sun/mail/util/TraceOutputStream.java index 61dafec236..e31f754f35 100644 --- a/app/src/main/java/com/sun/mail/util/TraceOutputStream.java +++ b/app/src/main/java/com/sun/mail/util/TraceOutputStream.java @@ -33,7 +33,8 @@ public class TraceOutputStream extends FilterOutputStream { private boolean quote = false; private OutputStream traceOut; - private int pos = 0; + private long pos = 0; + private long base = 0; private int last = 0; private int total = 0; private IReport reporter = null; @@ -153,15 +154,20 @@ public class TraceOutputStream extends FilterOutputStream { } } + public long getSent() { + return pos; + } + void report() { - if (reporter != null && pos - last > 1024) { - last = pos; - reporter.report(pos, total); + int p = (int) (pos - base); + if (reporter != null && p - last > 1024) { + last = p; + reporter.report(p, total); } } public void setReporter(int total, IReport reporter) { - this.pos = 0; + this.base = pos; this.last = 0; this.total = total; this.reporter = reporter; diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index c8df53999a..c680a299ff 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -190,6 +190,7 @@ public class ApplicationEx extends Application if (Helper.hasWebView(this)) CookieManager.getInstance().setAcceptCookie(false); + TrafficStatsHelper.init(this); EncryptionHelper.init(this); MessageHelper.setSystemProperties(this); diff --git a/app/src/main/java/eu/faircode/email/TrafficStatsHelper.java b/app/src/main/java/eu/faircode/email/TrafficStatsHelper.java new file mode 100644 index 0000000000..6fbee5be07 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/TrafficStatsHelper.java @@ -0,0 +1,38 @@ +package eu.faircode.email; + +/* + This file is part of FairEmail. + + FairEmail is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + FairEmail is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with FairEmail. If not, see . + + Copyright 2018-2021 by Marcel Bokhorst (M66B) +*/ + +import android.content.Context; + +public class TrafficStatsHelper { + private static Context ctx; + + static void init(Context context) { + ctx = context; + } + + public static void report(String host, String protocol, long sent, long received) { + String msg = protocol + " " + host + " tx=" + sent + " rx=" + received; + if (ctx == null) + Log.i(msg); + else + EntityLog.log(ctx, EntityLog.Type.Statistics, msg); + } +}