From: Tobias Brunner Date: Fri, 8 Jun 2018 09:17:26 +0000 (+0200) Subject: android: Use a handler to show/remove notification from main UI thread X-Git-Tag: 5.7.0dr5~20^2~53 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=70d6a0cf3305139c6d884540c12ae9e7ec3fbf5d;p=thirdparty%2Fstrongswan.git android: Use a handler to show/remove notification from main UI thread This avoids races that were previously seen (e.g. when disconnecting while connecting, which sometimes showed a "Disconnecting..." notification). --- diff --git a/src/frontends/android/app/src/main/java/org/strongswan/android/logic/CharonVpnService.java b/src/frontends/android/app/src/main/java/org/strongswan/android/logic/CharonVpnService.java index 33edcf7885..c0ec8f552d 100644 --- a/src/frontends/android/app/src/main/java/org/strongswan/android/logic/CharonVpnService.java +++ b/src/frontends/android/app/src/main/java/org/strongswan/android/logic/CharonVpnService.java @@ -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); + } + }); } /**