From a9aaf6c38d80dda30331275359a554539e3b134b Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Thu, 16 Aug 2012 23:52:03 +0200 Subject: - Fix the problem Android sometimes killing the service in the background causing miniopenvpn to exit - Use setForeground to enable foreground priority, this forces to use a notification icon - Remove the ability to deselect Bytecounter. --- src/de/blinkt/openvpn/LaunchVPN.java | 2 +- src/de/blinkt/openvpn/OpenVPNThread.java | 15 +++--- src/de/blinkt/openvpn/OpenVpnService.java | 85 ++++++++++++++++--------------- 3 files changed, 53 insertions(+), 49 deletions(-) (limited to 'src/de/blinkt') diff --git a/src/de/blinkt/openvpn/LaunchVPN.java b/src/de/blinkt/openvpn/LaunchVPN.java index a2a0de3f..74f569e9 100644 --- a/src/de/blinkt/openvpn/LaunchVPN.java +++ b/src/de/blinkt/openvpn/LaunchVPN.java @@ -91,7 +91,7 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { super.onCreate(icicle); mPM =ProfileManager.getInstance(this); - + } @Override diff --git a/src/de/blinkt/openvpn/OpenVPNThread.java b/src/de/blinkt/openvpn/OpenVPNThread.java index 97e07c9d..0bde3205 100644 --- a/src/de/blinkt/openvpn/OpenVPNThread.java +++ b/src/de/blinkt/openvpn/OpenVPNThread.java @@ -13,17 +13,21 @@ public class OpenVPNThread implements Runnable { private String[] mArgv; private Process mProcess; private String mNativeDir; + private OpenVpnService mService; public OpenVPNThread(OpenVpnService service,String[] argv, String nativelibdir) { mArgv = argv; mNativeDir = nativelibdir; + mService = service; } public void stopProcess() { mProcess.destroy(); } + + @Override public void run() { try { @@ -34,20 +38,19 @@ public class OpenVPNThread implements Runnable { Log.e(TAG, "Got " + e.toString()); } finally { try { - /// mInterface.close(); + } catch (Exception e) { // ignore } - //mInterface = null; - OpenVPN.updateStateString("NOPROCESS","No process running"); - // Not a good place to do it, but will do - OpenVPN.logBuilderConfig(null); + + + mService.processDied(); Log.i(TAG, "Exiting"); } } - + private void startOpenVPNThreadArgs(String[] argv) { LinkedList argvlist = new LinkedList(); diff --git a/src/de/blinkt/openvpn/OpenVpnService.java b/src/de/blinkt/openvpn/OpenVpnService.java index 594b07d4..f7ed10a8 100644 --- a/src/de/blinkt/openvpn/OpenVpnService.java +++ b/src/de/blinkt/openvpn/OpenVpnService.java @@ -36,7 +36,7 @@ import android.preference.PreferenceManager; import de.blinkt.openvpn.OpenVPN.StateListener; public class OpenVpnService extends VpnService implements StateListener { - private Thread mServiceThread; + private Thread mProcessThread; private Vector mDnslist=new Vector(); @@ -58,25 +58,28 @@ public class OpenVpnService extends VpnService implements StateListener { private boolean mDisplayBytecount=false; - private boolean mNotificationvisible; - private static final int OPENVPN_STATUS = 1; @Override public void onRevoke() { OpenVpnManagementThread.stopOpenVPN(); - mServiceThread=null; - stopSelf(); + endVpnService(); + } + + // Similar to revoke but do not try to stop process + public void processDied() { + endVpnService(); + } + + private void endVpnService() { + mProcessThread=null; + OpenVPN.logBuilderConfig(null); ProfileManager.setConntectedVpnProfileDisconnected(this); - }; - - private void hideNotification() { - String ns = Context.NOTIFICATION_SERVICE; - NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); - mNotificationManager.cancel(OPENVPN_STATUS); - mNotificationvisible=false; + stopSelf(); + stopForeground(true); } - private void showNotification(String msg, String tickerText) { + + private Notification showNotification(String msg, String tickerText) { String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); @@ -98,9 +101,10 @@ public class OpenVpnService extends VpnService implements StateListener { nbuilder.setTicker(tickerText); Notification notification = nbuilder.getNotification(); + mNotificationManager.notify(OPENVPN_STATUS, notification); - mNotificationvisible=true; + return notification; } @@ -115,7 +119,6 @@ public class OpenVpnService extends VpnService implements StateListener { } - private LocalServerSocket openManagmentInterface(int tries) { // Could take a while to open connection String socketname = (getCacheDir().getAbsolutePath() + "/" + "mgmtsocket"); @@ -156,6 +159,7 @@ public class OpenVpnService extends VpnService implements StateListener { @Override public int onStartCommand(Intent intent, int flags, int startId) { + // Extract information from the intent. String prefix = getPackageName(); String[] argv = intent.getStringArrayExtra(prefix + ".ARGV"); @@ -163,6 +167,9 @@ public class OpenVpnService extends VpnService implements StateListener { String profileUUID = intent.getStringExtra(prefix + ".profileUUID"); mProfile = ProfileManager.get(profileUUID); + + Notification start = showNotification("Starting VPN " + mProfile.mName,null); + startForeground(OPENVPN_STATUS, start); OpenVPN.addSpeedListener(this); @@ -175,8 +182,8 @@ public class OpenVpnService extends VpnService implements StateListener { } } - if (mServiceThread!=null) { - mServiceThread.interrupt(); + if (mProcessThread!=null) { + mProcessThread.interrupt(); try { Thread.sleep(1000); } catch (InterruptedException e) { @@ -196,10 +203,10 @@ public class OpenVpnService extends VpnService implements StateListener { // Start a new session by creating a new thread. - OpenVPNThread serviceThread = new OpenVPNThread(this, argv,nativelibdir); + OpenVPNThread processThread = new OpenVPNThread(this, argv,nativelibdir); - mServiceThread = new Thread(serviceThread, "OpenVPNServiceThread"); - mServiceThread.start(); + mProcessThread = new Thread(processThread, "OpenVPNProcessThread"); + mProcessThread.start(); ProfileManager.setConnectedVpnProfile(this, mProfile); @@ -208,14 +215,15 @@ public class OpenVpnService extends VpnService implements StateListener { @Override public void onDestroy() { - if (mServiceThread != null) { + if (mProcessThread != null) { mSocketManager.managmentCommand("signal SIGINT\n"); - mServiceThread.interrupt(); + mProcessThread.interrupt(); } if (mNetworkStateReceiver!= null) { this.unregisterReceiver(mNetworkStateReceiver); } + } @@ -386,36 +394,29 @@ public class OpenVpnService extends VpnService implements StateListener { @Override public void updateState(String state,String logmessage) { - if("NOPROCESS".equals(state)) { - hideNotification(); - mDisplayBytecount=false; + // If the process is not running, ignore any state, + // Notification should be invisible in this state + if(mProcessThread==null) return; - } - - if("CONNECTED".equals(state)) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - mDisplayBytecount = prefs.getBoolean("statusafterconnect", false); - if(!mDisplayBytecount) { - hideNotification(); - return; - } - } - // Skip exiting status if the status is already hidden - if(("EXITING SIGINT".equals(state) || "EXITING".equals(state)) - && !mNotificationvisible) { - return; - } + // Display byte count only after being connected - if("BYTECOUNT".equals(state)) { + if("CONNECTED".equals(state)) { + mDisplayBytecount = true; + } else if("BYTECOUNT".equals(state)) { if(mDisplayBytecount) { showNotification(logmessage,null); } } else { - // Other notifications are shown + // Other notifications are shown, + // This also mean we are no longer connected, ignore bytecount messages until next + // CONNECTED String ticker = state.toLowerCase(); showNotification(state +" " + logmessage,ticker); + mDisplayBytecount=false; } } + + } -- cgit v1.2.3