Merge "Add more logging to Telecom for NYC-DR." into nyc-mr1-dev
diff --git a/src/com/android/server/telecom/Call.java b/src/com/android/server/telecom/Call.java
index ce2c1a5..ba4b0e6 100644
--- a/src/com/android/server/telecom/Call.java
+++ b/src/com/android/server/telecom/Call.java
@@ -1158,7 +1158,8 @@
         setConnectionCapabilities(connection.getConnectionCapabilities());
         setConnectionProperties(connection.getConnectionProperties());
         setVideoProvider(connection.getVideoProvider());
-        setVideoState(connection.getVideoState());
+        setVideoState(mCallsManager.getCheckedVideoState(connection.getVideoState(),
+                connection.getPhoneAccount()));
         setRingbackRequested(connection.isRingbackRequested());
         setIsVoipAudioMode(connection.getIsVoipAudioMode());
         setStatusHints(connection.getStatusHints());
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index 181eba8..2367138 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -802,11 +802,13 @@
 
             // If this is an emergency video call, we need to check if the phone account supports
             // emergency video calling.
-            if (call.isEmergencyCall() && VideoProfile.isVideo(videoState)) {
+            // Also, ensure we don't try to place an outgoing call with video if video is not
+            // supported.
+            if (VideoProfile.isVideo(videoState)) {
                 PhoneAccount account =
                         mPhoneAccountRegistrar.getPhoneAccount(phoneAccountHandle, initiatingUser);
 
-                if (account != null &&
+                if (call.isEmergencyCall() && account != null &&
                         !account.hasCapabilities(PhoneAccount.CAPABILITY_EMERGENCY_VIDEO_CALLING)) {
                     // Phone account doesn't support emergency video calling, so fallback to
                     // audio-only now to prevent the InCall UI from setting up video surfaces
@@ -814,6 +816,12 @@
                     Log.i(this, "startOutgoingCall - emergency video calls not supported; " +
                             "falling back to audio-only");
                     videoState = VideoProfile.STATE_AUDIO_ONLY;
+                } else if (account != null &&
+                        !account.hasCapabilities(PhoneAccount.CAPABILITY_VIDEO_CALLING)) {
+                    // Phone account doesn't support video calling, so fallback to audio-only.
+                    Log.i(this, "startOutgoingCall - video calls not supported; fallback to " +
+                            "audio-only.");
+                    videoState = VideoProfile.STATE_AUDIO_ONLY;
                 }
             }
 
@@ -1390,12 +1398,21 @@
      */
     void markCallAsRemoved(Call call) {
         removeCall(call);
+        Call foregroundCall = mCallAudioManager.getPossiblyHeldForegroundCall();
         if (mLocallyDisconnectingCalls.contains(call)) {
             mLocallyDisconnectingCalls.remove(call);
-            Call foregroundCall = mCallAudioManager.getPossiblyHeldForegroundCall();
             if (foregroundCall != null && foregroundCall.getState() == CallState.ON_HOLD) {
                 foregroundCall.unhold();
             }
+        } else if (foregroundCall != null &&
+                !foregroundCall.can(Connection.CAPABILITY_SUPPORT_HOLD)  &&
+                foregroundCall.getState() == CallState.ON_HOLD) {
+
+            // The new foreground call is on hold, however the carrier does not display the hold
+            // button in the UI.  Therefore, we need to auto unhold the held call since the user has
+            // no means of unholding it themselves.
+            Log.i(this, "Auto-unholding held foreground call (call doesn't support hold)");
+            foregroundCall.unhold();
         }
     }
 
@@ -1452,7 +1469,7 @@
                     mCallAudioManager.toggleMute();
                     return true;
                 } else {
-                    ringingCall.answer(ringingCall.getVideoState());
+                    ringingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
                     return true;
                 }
             } else if (HeadsetMediaButton.LONG_PRESS == type) {
@@ -1631,7 +1648,8 @@
                 "new conference call");
         call.setConnectionCapabilities(parcelableConference.getConnectionCapabilities());
         call.setConnectionProperties(parcelableConference.getConnectionProperties());
-        call.setVideoState(parcelableConference.getVideoState());
+        call.setVideoState(
+                getCheckedVideoState(parcelableConference.getVideoState(), phoneAccount));
         call.setVideoProvider(parcelableConference.getVideoProvider());
         call.setStatusHints(parcelableConference.getStatusHints());
         call.putExtras(Call.SOURCE_CONNECTION_SERVICE, parcelableConference.getExtras());
@@ -2154,4 +2172,27 @@
 
       call.setIntentExtras(extras);
     }
+
+    /**
+     * Given a video state and phone account handle, converts the passed video state to
+     * {@link VideoProfile#STATE_AUDIO_ONLY} if the phone account does not support video calling.
+     *
+     * Used to ensure that calls added by a connection service don't try to use video calling if
+     * they have not advertised that they can.
+     *
+     * @param videoState The video state.
+     * @param phoneAccountHandle The phone account handle.
+     * @return {@link VideoProfile#STATE_AUDIO_ONLY} if the phone account does not support video,
+     *      or the original videoState otherwise.
+     */
+    public int getCheckedVideoState(int videoState, PhoneAccountHandle phoneAccountHandle) {
+        if (VideoProfile.isVideo(videoState) && phoneAccountHandle != null) {
+            PhoneAccount account = mPhoneAccountRegistrar.getPhoneAccountUnchecked(
+                    phoneAccountHandle);
+            if (!account.hasCapabilities(PhoneAccount.CAPABILITY_VIDEO_CALLING)) {
+                return VideoProfile.STATE_AUDIO_ONLY;
+            }
+        }
+        return videoState;
+    }
 }