Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 34722b1f authored by Giorgos Mavrikas's avatar Giorgos Mavrikas
Browse files

Force SIP service to find the new local IP and reregister all SIP accounts on VPN on/off

Change-Id: I6f973d7f755838ccea20edafd98f9658fa1f8466
parent 81d4483f
Loading
Loading
Loading
Loading
+55 −0
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@ import android.net.sip.SipManager;
import android.net.sip.SipProfile;
import android.net.sip.SipSession;
import android.net.sip.SipSessionAdapter;
import android.net.vpn.VpnManager;
import android.net.vpn.VpnState;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.Bundle;
@@ -94,6 +96,7 @@ public final class SipService extends ISipService.Stub {
            new HashMap<String, ISipSession>();

    private ConnectivityReceiver mConnectivityReceiver;
    private VpnConnectivityReceiver mVpnConnectivityReceiver;
    private boolean mWifiEnabled;
    private SipWakeLock mMyWakeLock;

@@ -113,8 +116,11 @@ public final class SipService extends ISipService.Stub {
        if (DEBUG) Log.d(TAG, " service started!");
        mContext = context;
        mConnectivityReceiver = new ConnectivityReceiver();
        mVpnConnectivityReceiver = new VpnConnectivityReceiver();
        context.registerReceiver(mConnectivityReceiver,
                new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        context.registerReceiver(mVpnConnectivityReceiver,
                new IntentFilter(VpnManager.ACTION_VPN_CONNECTIVITY));
        context.registerReceiver(mWifiStateReceiver,
                new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
        mMyWakeLock = new SipWakeLock((PowerManager)
@@ -1108,6 +1114,55 @@ public final class SipService extends ISipService.Stub {
        }
    }

    private class VpnConnectivityReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            // Run the handler in MyExecutor to be protected by wake lock
            getExecutor().execute(new Runnable() {
                public void run() {
                    onReceiveInternal(context, intent);
                }
            });
        }

        private void onReceiveInternal(Context context, Intent intent) {
            String action = intent.getAction();
            VpnState state = (VpnState)intent.getExtra(VpnManager.BROADCAST_CONNECTION_STATE);
            if (action.equals(VpnManager.ACTION_VPN_CONNECTIVITY)) {
                if (DEBUG) Log.d(TAG, "VpnListener got a CONNECTIVITY_ACTION");
                switch (state) {
                    case IDLE:
                    case CONNECTED:
                        if (DEBUG) Log.d(TAG, "VpnListener:: CONNECTED");
                        onChanged("vpn", true);
                        break;
                    case CONNECTING:
                    case DISCONNECTING:
                    case CANCELLED:
                    case UNUSABLE:
                    case UNKNOWN:
                    default:
                        break;
                }
            } else {
                if (DEBUG) Log.d(TAG, "VpnListener got a non CONNECTIVITY_ACTION intent " + action.toString());
            }
        }

        private NetworkInfo getActiveNetworkInfo() {
            ConnectivityManager cm = (ConnectivityManager)
                    mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            return cm.getActiveNetworkInfo();
        }

        private void onChanged(String type, boolean connected) {
            synchronized (SipService.this) {
                    mMyWakeLock.acquire(this);
                    onConnectivityChanged(type, connected);
                    mMyWakeLock.release(this);
            }
        }
    }
    /**
     * Timer that can schedule events to occur even when the device is in sleep.
     * Only used internally in this package.
+1 −1
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ import android.util.Log;
 */
public class VpnManager {
    // Action for broadcasting a connectivity state.
    private static final String ACTION_VPN_CONNECTIVITY = "vpn.connectivity";
    public static final String ACTION_VPN_CONNECTIVITY = "vpn.connectivity";
    /** Key to the profile name of a connectivity broadcast event. */
    public static final String BROADCAST_PROFILE_NAME = "profile_name";
    /** Key to the connectivity state of a connectivity broadcast event. */