diff options
Diffstat (limited to 'src/de/blinkt/openvpn/OpenVPN.java')
-rw-r--r-- | src/de/blinkt/openvpn/OpenVPN.java | 62 |
1 files changed, 47 insertions, 15 deletions
diff --git a/src/de/blinkt/openvpn/OpenVPN.java b/src/de/blinkt/openvpn/OpenVPN.java index 0de4bd11..2ca2d259 100644 --- a/src/de/blinkt/openvpn/OpenVPN.java +++ b/src/de/blinkt/openvpn/OpenVPN.java @@ -4,30 +4,33 @@ import java.util.LinkedList; import java.util.Locale; import java.util.Vector; - import android.content.Context; import android.os.Build; public class OpenVPN { - private static final String NOPROCESS = "NOPROCESS"; public static LinkedList<LogItem> logbuffer; private static Vector<LogListener> logListener; private static Vector<StateListener> stateListener; + private static Vector<ByteCountListener> byteCountListener; + private static String[] mBconfig; - private static String mLaststatemsg; + private static String mLaststatemsg=""; - private static String mLaststate=NOPROCESS; + private static String mLaststate = "NOPROCESS"; private static int mLastStateresid=R.string.state_noprocess; + + private static long mlastByteCount[]={0,0,0,0}; static { logbuffer = new LinkedList<LogItem>(); logListener = new Vector<OpenVPN.LogListener>(); stateListener = new Vector<OpenVPN.StateListener>(); + byteCountListener = new Vector<OpenVPN.ByteCountListener>(); logInformation(); } @@ -84,7 +87,7 @@ public class OpenVPN { String str = String.format(Locale.ENGLISH,"Log (no context) resid %d", mRessourceId); if(mArgs !=null) for(Object o:mArgs) - str += "|" + o.toString(); + str += "|" + o.toString(); return str; } } @@ -108,6 +111,10 @@ public class OpenVPN { public interface StateListener { void updateState(String state, String logmessage, int localizedResId); } + + public interface ByteCountListener { + void updateByteCount(long in, long out, long diffin, long diffout); + } synchronized static void logMessage(int level,String prefix, String message) { @@ -132,14 +139,25 @@ public class OpenVPN { public synchronized static void removeLogListener(LogListener ll) { logListener.remove(ll); } + + public static void addByteCountListener(ByteCountListener bcl) { + bcl.updateByteCount(mlastByteCount[0], mlastByteCount[1], mlastByteCount[2], mlastByteCount[3]); + byteCountListener.add(bcl); + } + + public static void removeByteCountListener(ByteCountListener bcl) { + byteCountListener.remove(bcl); + } public synchronized static void addStateListener(StateListener sl){ - stateListener.add(sl); - if(mLaststate!=null) - sl.updateState(mLaststate, mLaststatemsg, mLastStateresid); + if(!stateListener.contains(sl)){ + stateListener.add(sl); + if(mLaststate!=null) + sl.updateState(mLaststate, mLaststatemsg, mLastStateresid); + } } - + private static int getLocalizedState(String state){ if (state.equals("CONNECTING")) return R.string.state_connecting; @@ -155,6 +173,8 @@ public class OpenVPN { return R.string.state_add_routes; else if (state.equals("CONNECTED")) return R.string.state_connected; + else if (state.equals("DISCONNECTED")) + return R.string.state_disconnected; else if (state.equals("RECONNECTING")) return R.string.state_reconnecting; else if (state.equals("EXITING")) @@ -200,12 +220,10 @@ public class OpenVPN { } public synchronized static void updateStateString(String state, String msg, int resid) { - if (! "BYTECOUNT".equals(state)) { - mLaststate= state; - mLaststatemsg = msg; - mLastStateresid = resid; - } - + mLaststate= state; + mLaststatemsg = msg; + mLastStateresid = resid; + for (StateListener sl : stateListener) { sl.updateState(state,msg,resid); } @@ -241,4 +259,18 @@ public class OpenVPN { newlogItem(new LogItem(LogItem.ERROR, ressourceId,args)); } + public static void updateByteCount(long in, long out) { + long lastIn = mlastByteCount[0]; + long lastOut = mlastByteCount[1]; + long diffin = in - lastIn; + long diffout = out - lastOut; + + mlastByteCount = new long[] {in,out,diffin,diffout}; + for(ByteCountListener bcl:byteCountListener){ + bcl.updateByteCount(in, out, diffin,diffout); + } + } + + + } |