From 3b68ac89cd679f134681204e0d1bb40d6dbf7879 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Sun, 7 Apr 2013 12:03:53 +0200 Subject: Registering for a state receiver gets the most recent state. Also added the coarse state to the interface. --- .../blinkt/openvpn/api/ExternalOpenVPNService.java | 85 +++++++++++++++++----- .../blinkt/openvpn/api/IOpenVPNStatusCallback.aidl | 2 +- 2 files changed, 68 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/de/blinkt/openvpn/api/ExternalOpenVPNService.java b/src/de/blinkt/openvpn/api/ExternalOpenVPNService.java index 459bf790..fff6c34f 100644 --- a/src/de/blinkt/openvpn/api/ExternalOpenVPNService.java +++ b/src/de/blinkt/openvpn/api/ExternalOpenVPNService.java @@ -2,6 +2,7 @@ package de.blinkt.openvpn.api; import java.io.IOException; import java.io.StringReader; +import java.lang.ref.WeakReference; import java.util.LinkedList; import java.util.List; @@ -15,7 +16,9 @@ import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.net.VpnService; import android.os.Binder; +import android.os.Handler; import android.os.IBinder; +import android.os.Message; import android.os.RemoteCallbackList; import android.os.RemoteException; import de.blinkt.openvpn.R; @@ -32,6 +35,8 @@ import de.blinkt.openvpn.core.VPNLaunchHelper; public class ExternalOpenVPNService extends Service implements StateListener { + private static final int SEND_TOALL = 0; + final RemoteCallbackList mCallbacks = new RemoteCallbackList(); @@ -56,7 +61,7 @@ public class ExternalOpenVPNService extends Service implements StateListener { } }; - + @Override public void onCreate() { super.onCreate(); @@ -117,7 +122,7 @@ public class ExternalOpenVPNService extends Service implements StateListener { public void startVPN(String inlineconfig) throws RemoteException { checkOpenVPNPermission(); - + ConfigParser cp = new ConfigParser(); try { cp.parseConfig(new StringReader(inlineconfig)); @@ -164,7 +169,7 @@ public class ExternalOpenVPNService extends Service implements StateListener { public Intent prepare(String packagename) { if (new ExternalAppDatabase(ExternalOpenVPNService.this).isAllowed(packagename)) return null; - + Intent intent = new Intent(); intent.setClass(ExternalOpenVPNService.this, ConfirmDialog.class); return intent; @@ -183,7 +188,11 @@ public class ExternalOpenVPNService extends Service implements StateListener { throws RemoteException { checkOpenVPNPermission(); - if (cb != null) mCallbacks.register(cb); + if (cb != null) { + cb.newStatus(mMostRecentState.state, + mMostRecentState.logmessage, mMostRecentState.level.name()); + mCallbacks.register(cb); + } } @@ -193,8 +202,8 @@ public class ExternalOpenVPNService extends Service implements StateListener { throws RemoteException { checkOpenVPNPermission(); - if (cb != null) mCallbacks.unregister(cb); - + if (cb != null) + mCallbacks.unregister(cb); } @Override @@ -205,7 +214,9 @@ public class ExternalOpenVPNService extends Service implements StateListener { } }; - @Override + + + private UpdateMessage mMostRecentState; @Override public IBinder onBind(Intent intent) { return mBinder; } @@ -218,21 +229,59 @@ public class ExternalOpenVPNService extends Service implements StateListener { OpenVPN.removeStateListener(this); } + class UpdateMessage { + public String state; + public String logmessage; + public ConnectionStatus level; + + public UpdateMessage(String state, String logmessage, ConnectionStatus level) { + this.state = state; + this.logmessage = logmessage; + this.level = level; + } + } + @Override - public void updateState(String state, String logmessage, int resid, ConnectionStatus level) { - // Broadcast to all clients the new value. - final int N = mCallbacks.beginBroadcast(); - for (int i=0; i service= null; + + @Override public void handleMessage(Message msg) { + + RemoteCallbackList callbacks; + switch (msg.what) { + case SEND_TOALL: + if(service ==null || service.get() == null) + return; + + callbacks = service.get().mCallbacks; + + + // Broadcast to all clients the new value. + final int N = callbacks.beginBroadcast(); + for (int i=0; i