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.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);
}
});
}
/**