Merge "Auto downgrade video calls when phone account doesn't support video." into nyc-mr1-dev
diff --git a/src/com/android/server/telecom/Call.java b/src/com/android/server/telecom/Call.java
index c9dc852..7733977 100644
--- a/src/com/android/server/telecom/Call.java
+++ b/src/com/android/server/telecom/Call.java
@@ -1143,7 +1143,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 1e57275..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;
                 }
             }
 
@@ -1640,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());
@@ -2163,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;
+    }
 }