From 2a9f7fcdb1ddf8c2ee39ed87334bf0bc58955808 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Sun, 10 Feb 2013 21:55:46 +0100 Subject: Use different icons in the status bar depending on status. --- src/de/blinkt/openvpn/LogWindow.java | 2 +- src/de/blinkt/openvpn/OpenVPN.java | 53 ++++++++++++++++++++-- src/de/blinkt/openvpn/OpenVPNThread.java | 2 +- src/de/blinkt/openvpn/OpenVpnManagementThread.java | 4 +- src/de/blinkt/openvpn/OpenVpnService.java | 31 ++++--------- 5 files changed, 62 insertions(+), 30 deletions(-) (limited to 'src/de/blinkt') diff --git a/src/de/blinkt/openvpn/LogWindow.java b/src/de/blinkt/openvpn/LogWindow.java index ef29ad5e..4e153357 100644 --- a/src/de/blinkt/openvpn/LogWindow.java +++ b/src/de/blinkt/openvpn/LogWindow.java @@ -392,7 +392,7 @@ public class LogWindow extends ListActivity implements StateListener { } @Override - public void updateState(final String status,final String logmessage, final int resid) { + public void updateState(final String status,final String logmessage, final int resid, final int level) { runOnUiThread(new Runnable() { @Override diff --git a/src/de/blinkt/openvpn/OpenVPN.java b/src/de/blinkt/openvpn/OpenVPN.java index 03db7677..607f87e3 100644 --- a/src/de/blinkt/openvpn/OpenVPN.java +++ b/src/de/blinkt/openvpn/OpenVPN.java @@ -28,6 +28,18 @@ public class OpenVPN { private static long mlastByteCount[]={0,0,0,0}; + + + + static final int LEVEL_NONETWORK = 3; + static final int LEVEL_NOTCONNECTED = 4; + public static final int LEVEL_AUTH_FAILED = 5; + static final int LEVEL_CONNECTING_SERVER_REPLIED = 1; + static final int LEVEL_CONNECTING_NO_SERVER_REPLY_YET = 2; + static final int LEVEL_CONNECTED = 0; + + private static int mLastLevel=LEVEL_NOTCONNECTED; + static { logbuffer = new LinkedList(); logListener = new Vector(); @@ -145,7 +157,7 @@ public class OpenVPN { } public interface StateListener { - void updateState(String state, String logmessage, int localizedResId); + void updateState(String state, String logmessage, int localizedResId, int level); } public interface ByteCountListener { @@ -190,7 +202,7 @@ public class OpenVPN { if(!stateListener.contains(sl)){ stateListener.add(sl); if(mLaststate!=null) - sl.updateState(mLaststate, mLaststatemsg, mLastStateresid); + sl.updateState(mLaststate, mLaststatemsg, mLastStateresid, mLastLevel); } } @@ -224,6 +236,35 @@ public class OpenVPN { } + private static int getLevel(String state){ + String[] noreplyet = {"CONNECTING","WAIT", "RECONNECTING", "RESOLVE", "TCP_CONNECT"}; + String[] reply = {"AUTH","GETCONFIG","ASSIGN_IP","ADD_ROUTES"}; + String[] connected = {"CONNECTED"}; + String[] notconnected = {"DISCONNECTED", "EXITING"}; + + for(String x:noreplyet) + if(state.equals(x)) + return LEVEL_CONNECTING_NO_SERVER_REPLY_YET; + + for(String x:reply) + if(state.equals(x)) + return LEVEL_CONNECTING_SERVER_REPLIED; + + for(String x:connected) + if(state.equals(x)) + return LEVEL_CONNECTED; + + for(String x:notconnected) + if(state.equals(x)) + return LEVEL_NOTCONNECTED; + + return -1; + + } + + + + public synchronized static void removeStateListener(StateListener sl) { stateListener.remove(sl); } @@ -252,16 +293,18 @@ public class OpenVPN { public static void updateStateString (String state, String msg) { int rid = getLocalizedState(state); - updateStateString(state, msg,rid); + int level = getLevel(state); + updateStateString(state, msg, rid, level); } - public synchronized static void updateStateString(String state, String msg, int resid) { + public synchronized static void updateStateString(String state, String msg, int resid, int level) { mLaststate= state; mLaststatemsg = msg; mLastStateresid = resid; + mLastLevel = level; for (StateListener sl : stateListener) { - sl.updateState(state,msg,resid); + sl.updateState(state,msg,resid,level); } } diff --git a/src/de/blinkt/openvpn/OpenVPNThread.java b/src/de/blinkt/openvpn/OpenVPNThread.java index 1ef04f70..67878eb7 100644 --- a/src/de/blinkt/openvpn/OpenVPNThread.java +++ b/src/de/blinkt/openvpn/OpenVPNThread.java @@ -54,7 +54,7 @@ public class OpenVPNThread implements Runnable { if( exitvalue != 0) OpenVPN.logError("Process exited with exit value " + exitvalue); - OpenVPN.updateStateString("NOPROCESS","No process running.", R.string.state_noprocess); + OpenVPN.updateStateString("NOPROCESS","No process running.", R.string.state_noprocess,OpenVPN.LEVEL_NOTCONNECTED); if(mDumpPath!=null) { try { BufferedWriter logout = new BufferedWriter(new FileWriter(mDumpPath + ".log")); diff --git a/src/de/blinkt/openvpn/OpenVpnManagementThread.java b/src/de/blinkt/openvpn/OpenVpnManagementThread.java index e4528132..5cd70987 100644 --- a/src/de/blinkt/openvpn/OpenVpnManagementThread.java +++ b/src/de/blinkt/openvpn/OpenVpnManagementThread.java @@ -208,7 +208,7 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNMangement { releaseHoldCmd(); } else { mWaitingForRelease=true; - OpenVPN.updateStateString("NONETWORK", "",R.string.state_nonetwork); + OpenVPN.updateStateString("NONETWORK", "",R.string.state_nonetwork,OpenVPN.LEVEL_NONETWORK); } } private void releaseHoldCmd() { @@ -423,7 +423,7 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNMangement { private void proccessPWFailed(String needed, String args) { - OpenVPN.updateStateString("AUTH_FAILED", needed + args,R.string.state_auth_failed); + OpenVPN.updateStateString("AUTH_FAILED", needed + args,R.string.state_auth_failed,OpenVPN.LEVEL_AUTH_FAILED); } private void logStatusMessage(String command) { OpenVPN.logMessage(0,"MGMT:", command); diff --git a/src/de/blinkt/openvpn/OpenVpnService.java b/src/de/blinkt/openvpn/OpenVpnService.java index 881eda9d..7e1eebd3 100644 --- a/src/de/blinkt/openvpn/OpenVpnService.java +++ b/src/de/blinkt/openvpn/OpenVpnService.java @@ -77,11 +77,6 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac public static final int PROTECT_FD = 0; - - private static final int LEVEL_OFFLINE = 0; - private static final int LEVEL_NOTCONNECTED = 1; - private static final int LEVEL_CONNECTED = 2; - private static boolean mNotificationalwaysVisible=false; private final IBinder mBinder = new LocalBinder(); @@ -136,7 +131,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); - int icon = R.drawable.notification_icon; + int icon = R.drawable.ic_notification_icon; android.app.Notification.Builder nbuilder = new Notification.Builder(this); if(mProfile!=null) @@ -248,8 +243,8 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac this.unregisterReceiver(mNetworkStateReceiver); mNetworkStateReceiver=null; } - - + + @Override public int onStartCommand(Intent intent, int flags, int startId) { @@ -274,7 +269,8 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac mProfile = ProfileManager.get(profileUUID); - showNotification("Starting VPN " + mProfile.mName,"Starting VPN " + mProfile.mName, false,0,LEVEL_NOTCONNECTED); + showNotification("Starting VPN " + mProfile.mName,"Starting VPN " + mProfile.mName, + false,0,OpenVPN.LEVEL_CONNECTING_NO_SERVER_REPLY_YET); // Set a flag that we are starting a new VPN mStarting=true; @@ -320,11 +316,11 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac Runnable processThread; if(mOvpn3) { - + OpenVPNMangement mOpenVPN3 = instantiateOpenVPN3Core(); processThread = (Runnable) mOpenVPN3; mManagement = mOpenVPN3; - + } else { processThread = new OpenVPNThread(this, argv,nativelibdir); @@ -556,7 +552,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac } @Override - public void updateState(String state,String logmessage, int resid) { + public void updateState(String state,String logmessage, int resid, int level) { // If the process is not running, ignore any state, // Notification should be invisible in this state if(mProcessThread==null && !mNotificationalwaysVisible) @@ -565,17 +561,10 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac // Display byte count only after being connected { - int level; - if("CONNECTED".equals(state)) { + if(level == OpenVPN.LEVEL_CONNECTED) { mDisplayBytecount = true; mConnecttime = System.currentTimeMillis(); - level = LEVEL_CONNECTED; } else { - if ("NONETWORK".equals(state)) { - level = LEVEL_OFFLINE; - } else { - level = LEVEL_NOTCONNECTED; - } mDisplayBytecount = false; } @@ -598,7 +587,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac humanReadableByteCount(diffout, false)); boolean lowpriority = !mNotificationalwaysVisible; - showNotification(netstat,null,lowpriority,mConnecttime, LEVEL_CONNECTED); + showNotification(netstat,null,lowpriority,mConnecttime, OpenVPN.LEVEL_CONNECTED); } } -- cgit v1.2.3