]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
android: Use a handler to show/remove notification from main UI thread
authorTobias Brunner <tobias@strongswan.org>
Fri, 8 Jun 2018 09:17:26 +0000 (11:17 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 3 Jul 2018 09:31:34 +0000 (11:31 +0200)
This avoids races that were previously seen (e.g. when disconnecting
while connecting, which sometimes showed a "Disconnecting..."
notification).

src/frontends/android/app/src/main/java/org/strongswan/android/logic/CharonVpnService.java

index 33edcf78859837f62ea7f519a9e33573d3aaac3a..c0ec8f552d7f04c7cafa58d1d9e01b2ce80ca279 100644 (file)
@@ -31,6 +31,7 @@ import android.content.pm.PackageManager;
 import android.net.VpnService;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Handler;
 import android.os.IBinder;
 import android.os.ParcelFileDescriptor;
 import android.security.KeyChain;
@@ -88,6 +89,7 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
        private volatile boolean mTerminate;
        private volatile boolean mIsDisconnecting;
        private volatile boolean mShowNotification;
+       private Handler mHandler;
        private VpnStateService mService;
        private final Object mServiceLock = new Object();
        private final ServiceConnection mServiceConnection = new ServiceConnection() {
@@ -158,6 +160,9 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
                mLogFile = getFilesDir().getAbsolutePath() + File.separator + LOG_FILE;
                mAppDir = getFilesDir().getAbsolutePath();
 
+               /* handler used to do changes in the main UI thread */
+               mHandler = new Handler();
+
                mDataSource = new VpnProfileDataSource(this);
                mDataSource.open();
                /* use a separate thread as main thread for charon */
@@ -313,8 +318,15 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
         */
        private void addNotification()
        {
-               mShowNotification = true;
-               startForeground(VPN_STATE_NOTIFICATION_ID, buildNotification(false));
+               mHandler.post(new Runnable()
+               {
+                       @Override
+                       public void run()
+                       {
+                               mShowNotification = true;
+                               startForeground(VPN_STATE_NOTIFICATION_ID, buildNotification(false));
+                       }
+               });
        }
 
        /**
@@ -322,8 +334,15 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
         */
        private void removeNotification()
        {
-               mShowNotification = false;
-               stopForeground(true);
+               mHandler.post(new Runnable()
+               {
+                       @Override
+                       public void run()
+                       {
+                               mShowNotification = false;
+                               stopForeground(true);
+                       }
+               });
        }
 
        /**