From 06917978c727df00627a5b831526def8d613a280 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Mon, 18 Jun 2012 00:27:03 +0200 Subject: Add a status message which shows the status of the connecting/connected VPN --- src/de/blinkt/openvpn/OpenVpnService.java | 68 ++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 11 deletions(-) (limited to 'src/de/blinkt/openvpn/OpenVpnService.java') diff --git a/src/de/blinkt/openvpn/OpenVpnService.java b/src/de/blinkt/openvpn/OpenVpnService.java index 8c172115..a0d7503d 100644 --- a/src/de/blinkt/openvpn/OpenVpnService.java +++ b/src/de/blinkt/openvpn/OpenVpnService.java @@ -19,19 +19,24 @@ package de.blinkt.openvpn; import java.io.IOException; import java.util.Vector; +import de.blinkt.openvpn.OpenVPN.StateListener; + import android.app.Notification; +import android.app.Notification.Builder; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.net.LocalSocket; import android.net.LocalSocketAddress; import android.net.VpnService; import android.os.ParcelFileDescriptor; +import android.preference.PreferenceManager; -public class OpenVpnService extends VpnService { +public class OpenVpnService extends VpnService implements StateListener { private Thread mServiceThread; private Vector mDnslist=new Vector(); @@ -54,7 +59,9 @@ public class OpenVpnService extends VpnService { private NetworkSateReceiver mNetworkStateReceiver; - private static final int HELLO_ID = 1; + private boolean mDisplayBytecount=false; + + private static final int OPENVPN_STATUS = 1; @Override public void onRevoke() { @@ -63,24 +70,36 @@ public class OpenVpnService extends VpnService { stopSelf(); }; - private void showNotification() { + private void hideNotification() { + String ns = Context.NOTIFICATION_SERVICE; + NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); + mNotificationManager.cancel(OPENVPN_STATUS); + + } + private void showNotification(String msg) { String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); int icon = R.drawable.icon; - CharSequence tickerText = "Hello"; long when = System.currentTimeMillis(); + + android.app.Notification.Builder nbuilder = new Notification.Builder(this); - mNotification = new Notification(icon, tickerText, when); + nbuilder.setContentTitle("OpenVPN - " + mProfile.mName); + nbuilder.setContentText(msg); + nbuilder.setOnlyAlertOnce(true); + nbuilder.setOngoing(true); + nbuilder.setContentIntent(getLogPendingIntent()); + nbuilder.setSmallIcon(icon); + nbuilder.setWhen(when); - Context context = getApplicationContext(); - CharSequence contentTitle = "My notification"; - CharSequence contentText = "Hello World!"; + mNotification = nbuilder.getNotification(); - mNotification.setLatestEventInfo(context, contentTitle, contentText, getLogPendingIntent()); - mNotificationManager.notify(HELLO_ID, mNotification); + + + mNotificationManager.notify(OPENVPN_STATUS, mNotification); } @@ -135,7 +154,7 @@ public class OpenVpnService extends VpnService { String profileUUID = intent.getStringExtra(prefix + ".profileUUID"); mProfile = ProfileManager.get(profileUUID); - //showNotification(); + OpenVPN.addSpeedListener(this); // Stop the previous session by interrupting the thread. if(OpenVpnManagementThread.stopOpenVPN()){ @@ -357,4 +376,31 @@ public class OpenVpnService extends VpnService { mLocalIPv6 = ipv6addr; } + @Override + public void updateState(String state,String logmessage) { + if("NOPROCESS".equals(state)) { + hideNotification(); + mDisplayBytecount=false; + return; + } + + if("CONNECTED".equals(state)) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + mDisplayBytecount = prefs.getBoolean("statusafterconnect", false); + if(!mDisplayBytecount) { + hideNotification(); + return; + } + } + + if("BYTECOUNT".equals(state)) { + if(mDisplayBytecount) { + showNotification(logmessage); + } + } else { + // Other notifications are shown + showNotification(state +" " + logmessage); + } + } + } -- cgit v1.2.3