diff options
author | Arne Schwabe <arne@rfc2549.org> | 2012-06-20 22:34:08 +0200 |
---|---|---|
committer | Arne Schwabe <arne@rfc2549.org> | 2012-06-20 22:34:08 +0200 |
commit | bc279dfd2cffaf8278ee0750aa2c05af9e4091e0 (patch) | |
tree | 03c5e7d12422cb17d1c1180450b9ee4afbb2cd99 /src/de/blinkt/openvpn/OpenVpnService.java | |
parent | c30014c45e61a0712b5b74d0ecdf0cc44386c50f (diff) | |
parent | 47e1a6d2dd8e1416dcf4944c0258e999e7102677 (diff) |
Merge with 4fe04f90d7c3f24b98e04e3258deb3e6d0ec51d0
Diffstat (limited to 'src/de/blinkt/openvpn/OpenVpnService.java')
-rw-r--r-- | src/de/blinkt/openvpn/OpenVpnService.java | 72 |
1 files changed, 61 insertions, 11 deletions
diff --git a/src/de/blinkt/openvpn/OpenVpnService.java b/src/de/blinkt/openvpn/OpenVpnService.java index 8c172115..fd0b4f2a 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<String> mDnslist=new Vector<String>(); @@ -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,39 @@ 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 tickerText) { 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); + + nbuilder.setContentTitle("OpenVPN - " + mProfile.mName); + nbuilder.setContentText(msg); + nbuilder.setOnlyAlertOnce(true); + nbuilder.setOngoing(true); + nbuilder.setContentIntent(getLogPendingIntent()); + nbuilder.setSmallIcon(icon); + nbuilder.setWhen(when); + + if(tickerText!=null) + nbuilder.setTicker(tickerText); - mNotification = new Notification(icon, tickerText, when); + mNotification = nbuilder.getNotification(); - Context context = getApplicationContext(); - CharSequence contentTitle = "My notification"; - CharSequence contentText = "Hello World!"; - mNotification.setLatestEventInfo(context, contentTitle, contentText, getLogPendingIntent()); + - mNotificationManager.notify(HELLO_ID, mNotification); + mNotificationManager.notify(OPENVPN_STATUS, mNotification); } @@ -135,7 +157,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 +379,32 @@ 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,null); + } + } else { + // Other notifications are shown + String ticker = state.toLowerCase(); + showNotification(state +" " + logmessage,ticker); + } + } + } |