From 428ef31f6a1de54cd6e3c20264f5ab411a076aaa Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Tue, 26 Feb 2013 23:12:44 +0100 Subject: Refactor ConnectionStatus to be an enumeration --HG-- extra : rebase_source : 6cf34d83b33bdb25d2311ad9adaa446ac0aac823 --- src/de/blinkt/openvpn/LogWindow.java | 3 +- src/de/blinkt/openvpn/OpenVPN.java | 93 ++++++++++++---------- src/de/blinkt/openvpn/OpenVPNThread.java | 3 +- src/de/blinkt/openvpn/OpenVpnManagementThread.java | 6 +- src/de/blinkt/openvpn/OpenVpnService.java | 13 +-- 5 files changed, 65 insertions(+), 53 deletions(-) (limited to 'src/de/blinkt') diff --git a/src/de/blinkt/openvpn/LogWindow.java b/src/de/blinkt/openvpn/LogWindow.java index c7bbe6ac..151b2c5b 100644 --- a/src/de/blinkt/openvpn/LogWindow.java +++ b/src/de/blinkt/openvpn/LogWindow.java @@ -34,6 +34,7 @@ import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; +import de.blinkt.openvpn.OpenVPN.ConnectionStatus; import de.blinkt.openvpn.OpenVPN.LogItem; import de.blinkt.openvpn.OpenVPN.LogListener; import de.blinkt.openvpn.OpenVPN.StateListener; @@ -404,7 +405,7 @@ public class LogWindow extends ListActivity implements StateListener { } @Override - public void updateState(final String status,final String logmessage, final int resid, final int level) { + public void updateState(final String status,final String logmessage, final int resid, final ConnectionStatus level) { runOnUiThread(new Runnable() { @Override diff --git a/src/de/blinkt/openvpn/OpenVPN.java b/src/de/blinkt/openvpn/OpenVPN.java index 0dc5a871..4d5ae269 100644 --- a/src/de/blinkt/openvpn/OpenVPN.java +++ b/src/de/blinkt/openvpn/OpenVPN.java @@ -40,21 +40,28 @@ 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; + public enum ConnectionStatus { + LEVEL_NONETWORK (3), + LEVEL_NOTCONNECTED (4), + LEVEL_AUTH_FAILED ( 5), + LEVEL_CONNECTING_SERVER_REPLIED ( 1), + LEVEL_CONNECTING_NO_SERVER_REPLY_YET (2), + LEVEL_CONNECTED (0), UNKNOWN_LEVEL(-1); + + public final int level; + + ConnectionStatus(int level){ + this.level = level; + } + } public static final byte[] officalkey = {-58, -42, -44, -106, 90, -88, -87, -88, -52, -124, 84, 117, 66, 79, -112, -111, -46, 86, -37, 109}; public static final byte[] officaldebugkey = {-99, -69, 45, 71, 114, -116, 82, 66, -99, -122, 50, -70, -56, -111, 98, -35, -65, 105, 82, 43}; public static final byte[] amazonkey = {-116, -115, -118, -89, -116, -112, 120, 55, 79, -8, -119, -23, 106, -114, -85, -56, -4, 105, 26, -57}; - private static int mLastLevel=LEVEL_NOTCONNECTED; - + private static ConnectionStatus mLastLevel=ConnectionStatus.LEVEL_NOTCONNECTED; + static { logbuffer = new LinkedList(); logListener = new Vector(); @@ -153,7 +160,7 @@ public class OpenVPN { if(mArgs !=null) for(Object o:mArgs) str += "|" + o.toString(); - + return str; } } @@ -169,31 +176,31 @@ public class OpenVPN { CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(raw.toByteArray())); MessageDigest md = MessageDigest.getInstance("SHA-1"); - byte[] der = cert.getEncoded(); - md.update(der); - byte[] digest = md.digest(); - - if (Arrays.equals(digest, officalkey)) - apksign = c.getString(R.string.official_build); - else if (Arrays.equals(digest, officaldebugkey)) - apksign = c.getString(R.string.debug_build); - else if (Arrays.equals(digest, amazonkey)) - apksign = "amazon version"; - else - apksign = c.getString(R.string.built_by,cert.getSubjectX500Principal().getName()); - + byte[] der = cert.getEncoded(); + md.update(der); + byte[] digest = md.digest(); + + if (Arrays.equals(digest, officalkey)) + apksign = c.getString(R.string.official_build); + else if (Arrays.equals(digest, officaldebugkey)) + apksign = c.getString(R.string.debug_build); + else if (Arrays.equals(digest, amazonkey)) + apksign = "amazon version"; + else + apksign = c.getString(R.string.built_by,cert.getSubjectX500Principal().getName()); + PackageInfo packageinfo = c.getPackageManager().getPackageInfo(c.getPackageName(), 0); version = packageinfo.versionName; - + } catch (NameNotFoundException e) { } catch (CertificateException e) { } catch (NoSuchAlgorithmException e) { } - + Object[] argsext = Arrays.copyOf(mArgs, mArgs.length+2); argsext[argsext.length-1]=apksign; argsext[argsext.length-2]=version; - + return c.getString(R.string.mobile_info_extended, argsext); } @@ -215,7 +222,7 @@ public class OpenVPN { } public interface StateListener { - void updateState(String state, String logmessage, int localizedResId, int level); + void updateState(String state, String logmessage, int localizedResId, ConnectionStatus level); } public interface ByteCountListener { @@ -295,35 +302,35 @@ public class OpenVPN { } - private static int getLevel(String state){ + private static ConnectionStatus getLevel(String state){ String[] noreplyet = {"CONNECTING","WAIT", "RECONNECTING", "RESOLVE", "TCP_CONNECT"}; String[] reply = {"AUTH","GET_CONFIG","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; - + return ConnectionStatus.LEVEL_CONNECTING_NO_SERVER_REPLY_YET; + for(String x:reply) if(state.equals(x)) - return LEVEL_CONNECTING_SERVER_REPLIED; - + return ConnectionStatus.LEVEL_CONNECTING_SERVER_REPLIED; + for(String x:connected) if(state.equals(x)) - return LEVEL_CONNECTED; - + return ConnectionStatus.LEVEL_CONNECTED; + for(String x:notconnected) if(state.equals(x)) - return LEVEL_NOTCONNECTED; - - return -1; - + return ConnectionStatus.LEVEL_NOTCONNECTED; + + return ConnectionStatus.UNKNOWN_LEVEL; + } - - + + public synchronized static void removeStateListener(StateListener sl) { stateListener.remove(sl); } @@ -352,11 +359,11 @@ public class OpenVPN { public static void updateStateString (String state, String msg) { int rid = getLocalizedState(state); - int level = getLevel(state); + ConnectionStatus level = getLevel(state); updateStateString(state, msg, rid, level); } - public synchronized static void updateStateString(String state, String msg, int resid, int level) { + public synchronized static void updateStateString(String state, String msg, int resid, ConnectionStatus level) { mLaststate= state; mLaststatemsg = msg; mLastStateresid = resid; diff --git a/src/de/blinkt/openvpn/OpenVPNThread.java b/src/de/blinkt/openvpn/OpenVPNThread.java index cbaab234..ca7ed068 100644 --- a/src/de/blinkt/openvpn/OpenVPNThread.java +++ b/src/de/blinkt/openvpn/OpenVPNThread.java @@ -12,6 +12,7 @@ import java.util.LinkedList; import java.util.Locale; import android.util.Log; +import de.blinkt.openvpn.OpenVPN.ConnectionStatus; import de.blinkt.openvpn.OpenVPN.LogItem; public class OpenVPNThread implements Runnable { @@ -57,7 +58,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.LEVEL_NOTCONNECTED); + OpenVPN.updateStateString("NOPROCESS","No process running.", R.string.state_noprocess,ConnectionStatus.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 d17ed6c5..86918358 100644 --- a/src/de/blinkt/openvpn/OpenVpnManagementThread.java +++ b/src/de/blinkt/openvpn/OpenVpnManagementThread.java @@ -11,6 +11,8 @@ import java.util.LinkedList; import java.util.Locale; import java.util.Vector; +import de.blinkt.openvpn.OpenVPN.ConnectionStatus; + import android.content.SharedPreferences; import android.net.LocalServerSocket; import android.net.LocalSocket; @@ -207,7 +209,7 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNMangement { releaseHoldCmd(); } else { mWaitingForRelease=true; - OpenVPN.updateStateString("NONETWORK", "",R.string.state_nonetwork,OpenVPN.LEVEL_NONETWORK); + OpenVPN.updateStateString("NONETWORK", "",R.string.state_nonetwork,ConnectionStatus.LEVEL_NONETWORK); } } private void releaseHoldCmd() { @@ -421,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.LEVEL_AUTH_FAILED); + OpenVPN.updateStateString("AUTH_FAILED", needed + args,R.string.state_auth_failed,ConnectionStatus.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 cecae9d1..c73a1556 100644 --- a/src/de/blinkt/openvpn/OpenVpnService.java +++ b/src/de/blinkt/openvpn/OpenVpnService.java @@ -41,6 +41,7 @@ import android.os.Message; import android.os.ParcelFileDescriptor; import android.preference.PreferenceManager; import de.blinkt.openvpn.OpenVPN.ByteCountListener; +import de.blinkt.openvpn.OpenVPN.ConnectionStatus; import de.blinkt.openvpn.OpenVPN.StateListener; public class OpenVpnService extends VpnService implements StateListener, Callback, ByteCountListener { @@ -127,7 +128,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac } } - private void showNotification(String msg, String tickerText, boolean lowpriority, long when, int level) { + private void showNotification(String msg, String tickerText, boolean lowpriority, long when, ConnectionStatus level) { String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); @@ -144,7 +145,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac nbuilder.setOnlyAlertOnce(true); nbuilder.setOngoing(true); nbuilder.setContentIntent(getLogPendingIntent()); - nbuilder.setSmallIcon(icon,level); + nbuilder.setSmallIcon(icon,level.level); if(when !=0) nbuilder.setWhen(when); @@ -271,7 +272,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac mProfile = ProfileManager.get(profileUUID); showNotification("Starting VPN " + mProfile.mName,"Starting VPN " + mProfile.mName, - false,0,OpenVPN.LEVEL_CONNECTING_NO_SERVER_REPLY_YET); + false,0,ConnectionStatus.LEVEL_CONNECTING_NO_SERVER_REPLY_YET); // Set a flag that we are starting a new VPN mStarting=true; @@ -538,7 +539,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac } @Override - public void updateState(String state,String logmessage, int resid, int level) { + public void updateState(String state,String logmessage, int resid, ConnectionStatus level) { // If the process is not running, ignore any state, // Notification should be invisible in this state if(mProcessThread==null && !mNotificationalwaysVisible) @@ -547,7 +548,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac // Display byte count only after being connected { - if(level == OpenVPN.LEVEL_CONNECTED) { + if(level == ConnectionStatus.LEVEL_CONNECTED) { mDisplayBytecount = true; mConnecttime = System.currentTimeMillis(); } else { @@ -573,7 +574,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac humanReadableByteCount(diffout/OpenVPNMangement.mBytecountinterval, true)); boolean lowpriority = !mNotificationalwaysVisible; - showNotification(netstat,null,lowpriority,mConnecttime, OpenVPN.LEVEL_CONNECTED); + showNotification(netstat,null,lowpriority,mConnecttime, ConnectionStatus.LEVEL_CONNECTED); } } -- cgit v1.2.3