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

Commit a650639d authored by Chia-chi Yeh's avatar Chia-chi Yeh Committed by Android (Google) Code Review
Browse files

Merge "SipService: grab Wi-Fi lock only when necessary."

parents 2f0d941b c2bd6162
Loading
Loading
Loading
Loading
+41 −46
Original line number Diff line number Diff line
@@ -77,8 +77,7 @@ public final class SipService extends ISipService.Stub {

    private Context mContext;
    private String mLocalIp;
    private String mNetworkType;
    private boolean mConnected;
    private int mNetworkType = -1;
    private SipWakeupTimer mTimer;
    private WifiManager.WifiLock mWifiLock;
    private boolean mSipOnWifiOnly;
@@ -147,9 +146,7 @@ public final class SipService extends ISipService.Stub {
                android.Manifest.permission.USE_SIP, null);
        localProfile.setCallingUid(Binder.getCallingUid());
        try {
            boolean addingFirstProfile = mSipGroups.isEmpty();
            createGroup(localProfile);
            if (addingFirstProfile && !mSipGroups.isEmpty()) registerReceivers();
        } catch (SipException e) {
            Log.e(TAG, "openToMakeCalls()", e);
            // TODO: how to send the exception back
@@ -170,12 +167,11 @@ public final class SipService extends ISipService.Stub {
        if (DEBUG) Log.d(TAG, "open3: " + localProfile.getUriString() + ": "
                + incomingCallPendingIntent + ": " + listener);
        try {
            boolean addingFirstProfile = mSipGroups.isEmpty();
            SipSessionGroupExt group = createGroup(localProfile,
                    incomingCallPendingIntent, listener);
            if (addingFirstProfile && !mSipGroups.isEmpty()) registerReceivers();
            if (localProfile.getAutoRegistration()) {
                group.openToReceiveCalls();
                updateWakeLocks();
            }
        } catch (SipException e) {
            Log.e(TAG, "openToReceiveCalls()", e);
@@ -210,10 +206,7 @@ public final class SipService extends ISipService.Stub {
        notifyProfileRemoved(group.getLocalProfile());
        group.close();

        if (!anyOpenedToReceiveCalls()) {
            unregisterReceivers();
            mMyWakeLock.reset(); // in case there's leak
        }
        updateWakeLocks();
    }

    public synchronized boolean isOpened(String localProfileUri) {
@@ -260,7 +253,7 @@ public final class SipService extends ISipService.Stub {
        mContext.enforceCallingOrSelfPermission(
                android.Manifest.permission.USE_SIP, null);
        localProfile.setCallingUid(Binder.getCallingUid());
        if (!mConnected) return null;
        if (mNetworkType == -1) return null;
        try {
            SipSessionGroupExt group = createGroup(localProfile);
            return group.createSession(listener);
@@ -328,6 +321,9 @@ public final class SipService extends ISipService.Stub {
        Intent intent = new Intent(SipManager.ACTION_SIP_ADD_PHONE);
        intent.putExtra(SipManager.EXTRA_LOCAL_URI, localProfile.getUriString());
        mContext.sendBroadcast(intent);
        if (mSipGroups.size() == 1) {
            registerReceivers();
        }
    }

    private void notifyProfileRemoved(SipProfile localProfile) {
@@ -335,13 +331,9 @@ public final class SipService extends ISipService.Stub {
        Intent intent = new Intent(SipManager.ACTION_SIP_REMOVE_PHONE);
        intent.putExtra(SipManager.EXTRA_LOCAL_URI, localProfile.getUriString());
        mContext.sendBroadcast(intent);
        if (mSipGroups.size() == 0) {
            unregisterReceivers();
        }

    private boolean anyOpenedToReceiveCalls() {
        for (SipSessionGroupExt group : mSipGroups.values()) {
            if (group.isOpenedToReceiveCalls()) return true;
        }
        return false;
    }

    private void stopPortMappingMeasurement() {
@@ -526,7 +518,7 @@ public final class SipService extends ISipService.Stub {

        public void openToReceiveCalls() throws SipException {
            mOpenedToReceiveCalls = true;
            if (mConnected) {
            if (mNetworkType != -1) {
                mSipGroup.openToReceiveCalls(this);
                mAutoRegistration.start(mSipGroup);
            }
@@ -905,7 +897,7 @@ public final class SipService extends ISipService.Stub {
            mMyWakeLock.release(mSession);
            if (mSession != null) {
                mSession.setListener(null);
                if (mConnected && mRegistered) mSession.unregister();
                if (mNetworkType != -1 && mRegistered) mSession.unregister();
            }

            mTimer.cancel(this);
@@ -948,7 +940,7 @@ public final class SipService extends ISipService.Stub {
                            mProxy.onRegistrationFailed(mSession, mErrorCode,
                                    mErrorMessage);
                        }
                    } else if (!mConnected) {
                    } else if (mNetworkType == -1) {
                        mProxy.onRegistrationFailed(mSession,
                                SipErrorCode.DATA_CONNECTION_LOST,
                                "no data connection");
@@ -980,7 +972,7 @@ public final class SipService extends ISipService.Stub {
                mErrorCode = SipErrorCode.NO_ERROR;
                mErrorMessage = null;
                if (DEBUG) Log.d(TAG, "registering");
                if (mConnected) {
                if (mNetworkType != -1) {
                    mMyWakeLock.acquire(mSession);
                    mSession.register(EXPIRY_TIME);
                }
@@ -1134,7 +1126,25 @@ public final class SipService extends ISipService.Stub {

        // Reset variables maintained by ConnectivityReceiver.
        mWifiLock.release();
        mConnected = false;
        mNetworkType = -1;
    }

    private void updateWakeLocks() {
        for (SipSessionGroupExt group : mSipGroups.values()) {
            if (group.isOpenedToReceiveCalls()) {
                // Also grab the WifiLock when we are disconnected, so the
                // system will keep trying to reconnect. It will be released
                // when the system eventually connects to something else.
                if (mNetworkType == ConnectivityManager.TYPE_WIFI || mNetworkType == -1) {
                    mWifiLock.acquire();
                } else {
                    mWifiLock.release();
                }
                return;
            }
        }
        mWifiLock.release();
        mMyWakeLock.reset(); // in case there's a leak
    }

    private synchronized void onConnectivityChanged(NetworkInfo info) {
@@ -1144,8 +1154,7 @@ public final class SipService extends ISipService.Stub {
        // getActiveNetworkInfo(), which is critical to our SIP stack. To
        // solve this, if it is a DISCONNECTED event to our current network,
        // respect it. Otherwise get a new one from getActiveNetworkInfo().
        if (info == null || info.isConnected() ||
                !info.getTypeName().equals(mNetworkType)) {
        if (info == null || info.isConnected() || info.getType() != mNetworkType) {
            ConnectivityManager cm = (ConnectivityManager)
                    mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            info = cm.getActiveNetworkInfo();
@@ -1153,12 +1162,13 @@ public final class SipService extends ISipService.Stub {

        // Some devices limit SIP on Wi-Fi. In this case, if we are not on
        // Wi-Fi, treat it as a DISCONNECTED event.
        boolean connected = (info != null && info.isConnected() &&
                (!mSipOnWifiOnly || info.getType() == ConnectivityManager.TYPE_WIFI));
        String networkType = connected ? info.getTypeName() : "null";
        int networkType = (info != null && info.isConnected()) ? info.getType() : -1;
        if (mSipOnWifiOnly && networkType != ConnectivityManager.TYPE_WIFI) {
            networkType = -1;
        }

        // Ignore the event if the current active network is not changed.
        if (connected == mConnected && networkType.equals(mNetworkType)) {
        if (mNetworkType == networkType) {
            return;
        }
        if (DEBUG) {
@@ -1167,39 +1177,24 @@ public final class SipService extends ISipService.Stub {
        }

        try {
            if (mConnected) {
            if (mNetworkType != -1) {
                mLocalIp = null;
                stopPortMappingMeasurement();
                for (SipSessionGroupExt group : mSipGroups.values()) {
                    group.onConnectivityChanged(false);
                }
            }

            mConnected = connected;
            mNetworkType = networkType;

            if (connected) {
            if (mNetworkType != -1) {
                mLocalIp = determineLocalIp();
                mKeepAliveInterval = -1;
                mLastGoodKeepAliveInterval = DEFAULT_KEEPALIVE_INTERVAL;
                for (SipSessionGroupExt group : mSipGroups.values()) {
                    group.onConnectivityChanged(true);
                }

                // If we are on Wi-Fi, grab the WifiLock. Otherwise release it.
                if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                    mWifiLock.acquire();
                } else {
                    mWifiLock.release();
                }
            } else {
                // Always grab the WifiLock when we are disconnected, so the
                // system will keep trying to reconnect. We will release it
                // if we eventually connect via something else.
                mWifiLock.acquire();

                mMyWakeLock.reset(); // in case there's a leak
            }
            updateWakeLocks();
        } catch (SipException e) {
            Log.e(TAG, "onConnectivityChanged()", e);
        }