am 45470559: (-s ours) am 12b7da69: am 6df477be: Merge "Rename (IF_)LOGE(_IF) to (IF_)ALOGE(_IF)"

* commit '45470559cd4a499fd5d38cf708f0b531dc081fed':
  Rename (IF_)LOGE(_IF) to (IF_)ALOGE(_IF)
diff --git a/java/com/android/server/sip/SipService.java b/java/com/android/server/sip/SipService.java
index 38a683e..97afc81 100644
--- a/java/com/android/server/sip/SipService.java
+++ b/java/com/android/server/sip/SipService.java
@@ -77,8 +77,7 @@
 
     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 @@
                 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 @@
         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 @@
         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 @@
         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 @@
         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 @@
         Intent intent = new Intent(SipManager.ACTION_SIP_REMOVE_PHONE);
         intent.putExtra(SipManager.EXTRA_LOCAL_URI, localProfile.getUriString());
         mContext.sendBroadcast(intent);
-    }
-
-    private boolean anyOpenedToReceiveCalls() {
-        for (SipSessionGroupExt group : mSipGroups.values()) {
-            if (group.isOpenedToReceiveCalls()) return true;
+        if (mSipGroups.size() == 0) {
+            unregisterReceivers();
         }
-        return false;
     }
 
     private void stopPortMappingMeasurement() {
@@ -526,7 +518,7 @@
 
         public void openToReceiveCalls() throws SipException {
             mOpenedToReceiveCalls = true;
-            if (mConnected) {
+            if (mNetworkType != -1) {
                 mSipGroup.openToReceiveCalls(this);
                 mAutoRegistration.start(mSipGroup);
             }
@@ -905,7 +897,7 @@
             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 @@
                             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 @@
                 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 @@
 
         // 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 @@
         // 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 @@
 
         // 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 @@
         }
 
         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);
         }
diff --git a/jni/rtp/AudioGroup.cpp b/jni/rtp/AudioGroup.cpp
index 4db5738..1139577 100644
--- a/jni/rtp/AudioGroup.cpp
+++ b/jni/rtp/AudioGroup.cpp
@@ -1008,7 +1008,7 @@
     delete stream;
     delete codec;
     close(socket);
-    env->SetIntField(thiz, gNative, NULL);
+    env->SetIntField(thiz, gNative, 0);
 }
 
 void remove(JNIEnv *env, jobject thiz, jint socket)
@@ -1017,7 +1017,7 @@
     if (group) {
         if (socket == -1 || !group->remove(socket)) {
             delete group;
-            env->SetIntField(thiz, gNative, NULL);
+            env->SetIntField(thiz, gNative, 0);
         }
     }
 }