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:
+23
-4
@@ -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 */
|
||||||
@@ -313,8 +318,15 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
|
|||||||
*/
|
*/
|
||||||
private void addNotification()
|
private void addNotification()
|
||||||
{
|
{
|
||||||
mShowNotification = true;
|
mHandler.post(new Runnable()
|
||||||
startForeground(VPN_STATE_NOTIFICATION_ID, buildNotification(false));
|
{
|
||||||
|
@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()
|
private void removeNotification()
|
||||||
{
|
{
|
||||||
mShowNotification = false;
|
mHandler.post(new Runnable()
|
||||||
stopForeground(true);
|
{
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
mShowNotification = false;
|
||||||
|
stopForeground(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user