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).
This commit is contained in:
Tobias Brunner
2018-07-03 11:31:34 +02:00
parent 58d139dad3
commit 70d6a0cf33
@@ -31,6 +31,7 @@ import android.content.pm.PackageManager;
import android.net.VpnService; import android.net.VpnService;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.security.KeyChain; import android.security.KeyChain;
@@ -88,6 +89,7 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
private volatile boolean mTerminate; private volatile boolean mTerminate;
private volatile boolean mIsDisconnecting; private volatile boolean mIsDisconnecting;
private volatile boolean mShowNotification; private volatile boolean mShowNotification;
private Handler mHandler;
private VpnStateService mService; private VpnStateService mService;
private final Object mServiceLock = new Object(); private final Object mServiceLock = new Object();
private final ServiceConnection mServiceConnection = new ServiceConnection() { 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; mLogFile = getFilesDir().getAbsolutePath() + File.separator + LOG_FILE;
mAppDir = getFilesDir().getAbsolutePath(); mAppDir = getFilesDir().getAbsolutePath();
/* handler used to do changes in the main UI thread */
mHandler = new Handler();
mDataSource = new VpnProfileDataSource(this); mDataSource = new VpnProfileDataSource(this);
mDataSource.open(); mDataSource.open();
/* use a separate thread as main thread for charon */ /* use a separate thread as main thread for charon */
@@ -312,19 +317,33 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
* the system when low on memory. * the system when low on memory.
*/ */
private void addNotification() private void addNotification()
{
mHandler.post(new Runnable()
{
@Override
public void run()
{ {
mShowNotification = true; mShowNotification = true;
startForeground(VPN_STATE_NOTIFICATION_ID, buildNotification(false)); startForeground(VPN_STATE_NOTIFICATION_ID, buildNotification(false));
} }
});
}
/** /**
* Remove the permanent notification. * Remove the permanent notification.
*/ */
private void removeNotification() private void removeNotification()
{
mHandler.post(new Runnable()
{
@Override
public void run()
{ {
mShowNotification = false; mShowNotification = false;
stopForeground(true); stopForeground(true);
} }
});
}
/** /**
* Create a notification channel for Android 8+ * Create a notification channel for Android 8+