Wiring video state through from Connection

Bug: 16285417
Bug: 16013178
Change-Id: Ia48959248ca22f4569b0ffd01a1716470aa0a711
diff --git a/api/current.txt b/api/current.txt
index d3a8813..d29b296 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -28224,6 +28224,7 @@
     method public final android.telecomm.Connection getParentConnection();
     method public final int getState();
     method public final android.telecomm.StatusHints getStatusHints();
+    method public final int getVideoState();
     method public final boolean isConferenceConnection();
     method public final boolean isRequestingRingback();
     method protected void onAbort();
@@ -28257,6 +28258,7 @@
     method public final void setRinging();
     method public final void setSignal(android.os.Bundle);
     method public final void setStatusHints(android.telecomm.StatusHints);
+    method public final void setVideoState(int);
     method public static java.lang.String stateToString(int);
   }
 
@@ -28391,12 +28393,13 @@
   }
 
   public class PhoneAccountMetadata implements android.os.Parcelable {
-    ctor public PhoneAccountMetadata(android.telecomm.PhoneAccount, int, java.lang.String, java.lang.String);
+    ctor public PhoneAccountMetadata(android.telecomm.PhoneAccount, int, java.lang.String, java.lang.String, boolean);
     method public int describeContents();
     method public android.telecomm.PhoneAccount getAccount();
     method public android.graphics.drawable.Drawable getIcon(android.content.Context);
     method public java.lang.String getLabel();
     method public java.lang.String getShortDescription();
+    method public boolean isVideoCallingSupported();
     method public void writeToParcel(android.os.Parcel, int);
     field public static final android.os.Parcelable.Creator CREATOR;
   }
@@ -28440,6 +28443,7 @@
     method public int getHandlePresentation();
     method public int getState();
     method public android.telecomm.StatusHints getStatusHints();
+    method public int getVideoState();
     method public void hold();
     method public void playDtmf(char);
     method public void postDialContinue(boolean);
@@ -28462,6 +28466,7 @@
     method public abstract void onRequestingRingback(android.telecomm.RemoteConnection, boolean);
     method public abstract void onStateChanged(android.telecomm.RemoteConnection, int);
     method public abstract void onStatusHintsChanged(android.telecomm.RemoteConnection, android.telecomm.StatusHints);
+    method public abstract void onVideoStateChanged(android.telecomm.RemoteConnection, int);
   }
 
   public abstract interface Response {
diff --git a/telecomm/java/android/telecomm/Connection.java b/telecomm/java/android/telecomm/Connection.java
index 130364f..c050f30 100644
--- a/telecomm/java/android/telecomm/Connection.java
+++ b/telecomm/java/android/telecomm/Connection.java
@@ -37,6 +37,7 @@
         public void onHandleChanged(Connection c, Uri newHandle, int presentation) {}
         public void onCallerDisplayNameChanged(
                 Connection c, String callerDisplayName, int presentation) {}
+        public void onVideoStateChanged(Connection c, int videoState) {}
         public void onSignalChanged(Connection c, Bundle details) {}
         public void onDisconnected(Connection c, int cause, String message) {}
         public void onPostDialWait(Connection c, String remaining) {}
@@ -75,6 +76,7 @@
     private CallVideoProvider mCallVideoProvider;
     private boolean mAudioModeIsVoip;
     private StatusHints mStatusHints;
+    private int mVideoState;
 
     /**
      * Create a new Connection.
@@ -118,6 +120,19 @@
     }
 
     /**
+     * Returns the video state of the call.
+     * Valid values: {@link android.telecomm.VideoCallProfile#VIDEO_STATE_AUDIO_ONLY},
+     * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_BIDIRECTIONAL},
+     * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_TX_ENABLED},
+     * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_RX_ENABLED}.
+     *
+     * @return The video state of the call.
+     */
+    public final int getVideoState() {
+        return mVideoState;
+    }
+
+    /**
      * @return The audio state of the call, describing how its audio is currently
      *         being routed by the system. This is {@code null} if this Connection
      *         does not directly know about its audio state.
@@ -285,6 +300,23 @@
     }
 
     /**
+     * Set the video state for the connection.
+     * Valid values: {@link android.telecomm.VideoCallProfile#VIDEO_STATE_AUDIO_ONLY},
+     * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_BIDIRECTIONAL},
+     * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_TX_ENABLED},
+     * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_RX_ENABLED}.
+     *
+     * @param videoState The new video state.
+     */
+    public final void setVideoState(int videoState) {
+        Log.d(this, "setVideoState %d", videoState);
+        mVideoState = videoState;
+        for (Listener l : mListeners) {
+            l.onVideoStateChanged(this, mVideoState);
+        }
+    }
+
+    /**
      * Sets state to active (e.g., an ongoing call where two or more parties can actively
      * communicate).
      */
diff --git a/telecomm/java/android/telecomm/ConnectionService.java b/telecomm/java/android/telecomm/ConnectionService.java
index 5855470..9e04c6e 100644
--- a/telecomm/java/android/telecomm/ConnectionService.java
+++ b/telecomm/java/android/telecomm/ConnectionService.java
@@ -324,6 +324,13 @@
         }
 
         @Override
+        public void onVideoStateChanged(Connection c, int videoState) {
+            String id = mIdByConnection.get(c);
+            Log.d(this, "Adapter set video state %d", videoState);
+            mAdapter.setVideoState(id, videoState);
+        }
+
+        @Override
         public void onHandleChanged(Connection c, Uri handle, int presentation) {
             String id = mIdByConnection.get(c);
             mAdapter.setHandle(id, handle, presentation);
diff --git a/telecomm/java/android/telecomm/ConnectionServiceAdapter.java b/telecomm/java/android/telecomm/ConnectionServiceAdapter.java
index a812fa4..63546d3 100644
--- a/telecomm/java/android/telecomm/ConnectionServiceAdapter.java
+++ b/telecomm/java/android/telecomm/ConnectionServiceAdapter.java
@@ -323,4 +323,25 @@
             }
         }
     }
+
+    /**
+     * Sets the video state associated with a call.
+     *
+     * Valid values: {@link android.telecomm.VideoCallProfile#VIDEO_STATE_AUDIO_ONLY},
+     * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_BIDIRECTIONAL},
+     * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_TX_ENABLED},
+     * {@link android.telecomm.VideoCallProfile#VIDEO_STATE_RX_ENABLED}.
+     *
+     * @param callId The unique ID of the call to set the video state for.
+     * @param videoState The video state.
+     */
+    void setVideoState(String callId, int videoState) {
+        Log.v(this, "setVideoState: %d", videoState);
+        for (IConnectionServiceAdapter adapter : mAdapters) {
+            try {
+                adapter.setVideoState(callId, videoState);
+            } catch (RemoteException ignored) {
+            }
+        }
+    }
 }
diff --git a/telecomm/java/android/telecomm/PhoneAccountMetadata.java b/telecomm/java/android/telecomm/PhoneAccountMetadata.java
index 20a4d47..7232218 100644
--- a/telecomm/java/android/telecomm/PhoneAccountMetadata.java
+++ b/telecomm/java/android/telecomm/PhoneAccountMetadata.java
@@ -22,9 +22,6 @@
 import android.os.Parcel;
 import android.os.Parcelable;
 
-import java.io.IOException;
-import java.io.ObjectStreamException;
-import java.io.Serializable;
 import java.util.MissingResourceException;
 
 /**
@@ -35,16 +32,19 @@
     private int mIconResId;
     private String mLabel;
     private String mShortDescription;
+    private boolean mVideoCallingSupported;
 
     public PhoneAccountMetadata(
             PhoneAccount account,
             int iconResId,
             String label,
-            String shortDescription) {
+            String shortDescription,
+            boolean supportsVideoCalling) {
         mAccount = account;
         mIconResId = iconResId;
         mLabel = label;
         mShortDescription = shortDescription;
+        mVideoCallingSupported = supportsVideoCalling;
     }
 
     /**
@@ -101,6 +101,15 @@
         }
     }
 
+    /**
+     * Determines whether this {@code PhoneAccount} supports video calling.
+     *
+     * @return {@code true} if this {@code PhoneAccount} supports video calling.
+     */
+    public boolean isVideoCallingSupported() {
+        return mVideoCallingSupported;
+    }
+
     //
     // Parcelable implementation
     //
@@ -116,6 +125,7 @@
         out.writeInt(mIconResId);
         out.writeString(mLabel);
         out.writeString(mShortDescription);
+        out.writeInt(mVideoCallingSupported ? 1: 0);
     }
 
     public static final Creator<PhoneAccountMetadata> CREATOR
@@ -136,5 +146,6 @@
         mIconResId = in.readInt();
         mLabel = in.readString();
         mShortDescription = in.readString();
+        mVideoCallingSupported = in.readInt() == 1;
     }
 }
diff --git a/telecomm/java/android/telecomm/RemoteConnection.java b/telecomm/java/android/telecomm/RemoteConnection.java
index 5d8579e..d17e62a 100644
--- a/telecomm/java/android/telecomm/RemoteConnection.java
+++ b/telecomm/java/android/telecomm/RemoteConnection.java
@@ -42,6 +42,7 @@
         void onHandleChanged(RemoteConnection connection, Uri handle, int presentation);
         void onCallerDisplayNameChanged(
                 RemoteConnection connection, String callerDisplayName, int presentation);
+        void onVideoStateChanged(RemoteConnection connection, int videoState);
         void onDestroyed(RemoteConnection connection);
     }
 
@@ -55,6 +56,7 @@
     private boolean mRequestingRingback;
     private boolean mConnected;
     private int mCallCapabilities;
+    private int mVideoState;
     private boolean mAudioModeIsVoip;
     private StatusHints mStatusHints;
     private Uri mHandle;
@@ -120,6 +122,10 @@
         return mCallerDisplayNamePresentation;
     }
 
+    public int getVideoState() {
+        return mVideoState;
+    }
+
     public void abort() {
         try {
             if (mConnected) {
@@ -297,6 +303,16 @@
         }
     }
 
+    /**
+     * @hide
+     */
+    void setVideoState(int videoState) {
+        mVideoState = videoState;
+        for (Listener l : mListeners) {
+            l.onVideoStateChanged(this, videoState);
+        }
+    }
+
     /** @hide */
     void setAudioModeIsVoip(boolean isVoip) {
         mAudioModeIsVoip = isVoip;
diff --git a/telecomm/java/android/telecomm/RemoteConnectionService.java b/telecomm/java/android/telecomm/RemoteConnectionService.java
index 7fd8f93..25ff5bc 100644
--- a/telecomm/java/android/telecomm/RemoteConnectionService.java
+++ b/telecomm/java/android/telecomm/RemoteConnectionService.java
@@ -167,6 +167,13 @@
         }
 
         @Override
+        public void setVideoState(String connectionId, int videoState) {
+            if (isCurrentConnection(connectionId)) {
+                mConnection.setVideoState(videoState);
+            }
+        }
+
+        @Override
         public final void setAudioModeIsVoip(String connectionId, boolean isVoip) {
             if (isCurrentConnection(connectionId)) {
                 mConnection.setAudioModeIsVoip(isVoip);
diff --git a/telecomm/java/com/android/internal/telecomm/IConnectionServiceAdapter.aidl b/telecomm/java/com/android/internal/telecomm/IConnectionServiceAdapter.aidl
index b36f72c..e12cfca 100644
--- a/telecomm/java/com/android/internal/telecomm/IConnectionServiceAdapter.aidl
+++ b/telecomm/java/com/android/internal/telecomm/IConnectionServiceAdapter.aidl
@@ -64,6 +64,8 @@
 
     void setCallVideoProvider(String callId, ICallVideoProvider callVideoProvider);
 
+    void setVideoState(String callId, int videoState);
+
     void setAudioModeIsVoip(String callId, boolean isVoip);
 
     void setStatusHints(String callId, in StatusHints statusHints);
diff --git a/telephony/java/com/android/ims/ImsCallProfile.java b/telephony/java/com/android/ims/ImsCallProfile.java
index 208f467..6896f4d 100644
--- a/telephony/java/com/android/ims/ImsCallProfile.java
+++ b/telephony/java/com/android/ims/ImsCallProfile.java
@@ -19,6 +19,7 @@
 import android.os.Bundle;
 import android.os.Parcel;
 import android.os.Parcelable;
+import android.telecomm.VideoCallProfile;
 
 /**
  * Parcelable object to handle IMS call profile.
@@ -286,4 +287,63 @@
             return new ImsCallProfile[size];
         }
     };
+
+    /**
+     * Converts from the call types defined in {@link com.android.ims.ImsCallProfile} to the
+     * video state values defined in {@link android.telecomm.VideoCallProfile}.
+     *
+     * @param callType The call type.
+     * @return The video state.
+     */
+    public static int getVideoStateFromCallType(int callType) {
+        switch (callType) {
+            case CALL_TYPE_VT_NODIR:
+                return VideoCallProfile.VIDEO_STATE_PAUSED |
+                        VideoCallProfile.VIDEO_STATE_BIDIRECTIONAL;
+            case CALL_TYPE_VT_TX:
+                return VideoCallProfile.VIDEO_STATE_TX_ENABLED;
+            case CALL_TYPE_VT_RX:
+                return VideoCallProfile.VIDEO_STATE_RX_ENABLED;
+            case CALL_TYPE_VT:
+                return VideoCallProfile.VIDEO_STATE_BIDIRECTIONAL;
+            case CALL_TYPE_VOICE:
+                return VideoCallProfile.VIDEO_STATE_AUDIO_ONLY;
+            default:
+                return VideoCallProfile.VIDEO_STATE_AUDIO_ONLY;
+        }
+    }
+
+    /**
+     * Converts from the video state values defined in {@link android.telecomm.VideoCallProfile}
+     * to the call types defined in {@link ImsCallProfile}.
+     *
+     * @param videoState The video state.
+     * @return The call type.
+     */
+    public static int getCallTypeFromVideoState(int videoState) {
+        boolean videoTx = isVideoStateSet(videoState, VideoCallProfile.VIDEO_STATE_TX_ENABLED);
+        boolean videoRx = isVideoStateSet(videoState, VideoCallProfile.VIDEO_STATE_RX_ENABLED);
+        boolean isPaused = isVideoStateSet(videoState, VideoCallProfile.VIDEO_STATE_PAUSED);
+        if (isPaused) {
+            return ImsCallProfile.CALL_TYPE_VT_NODIR;
+        } else if (videoTx && !videoRx) {
+            return ImsCallProfile.CALL_TYPE_VT_TX;
+        } else if (!videoTx && videoRx) {
+            return ImsCallProfile.CALL_TYPE_VT_RX;
+        } else if (videoTx && videoRx) {
+            return ImsCallProfile.CALL_TYPE_VT;
+        }
+        return ImsCallProfile.CALL_TYPE_VOICE;
+    }
+
+    /**
+     * Determines if a video state is set in a video state bit-mask.
+     *
+     * @param videoState The video state bit mask.
+     * @param videoStateToCheck The particular video state to check.
+     * @return True if the video state is set in the bit-mask.
+     */
+    private static boolean isVideoStateSet(int videoState, int videoStateToCheck) {
+        return (videoState & videoStateToCheck) == videoStateToCheck;
+    }
 }