Add more capabilities to TestVideoProvider

* Support outgoing video calls with TestConnectionService
* Add the ability to upgrade/downgrade between video/voice, with
a simulated delay.

Change-Id: Ie2408fcbf255023a55139618f1cc48f5ad4dcbce
diff --git a/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java b/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java
index d71ef9d..3eb0bc5 100644
--- a/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java
+++ b/testapps/src/com/android/server/telecom/testapps/TestConnectionService.java
@@ -125,7 +125,7 @@
         }
     }
 
-    private final class TestConnection extends Connection {
+    final class TestConnection extends Connection {
         private final boolean mIsIncoming;
 
         /** Used to cleanup camera and media when done with connection. */
@@ -136,6 +136,8 @@
             // Assume all calls are video capable.
             int capabilities = getConnectionCapabilities();
             capabilities |= CAPABILITY_SUPPORTS_VT_LOCAL;
+            capabilities |= CAPABILITY_SUPPORTS_VT_REMOTE;
+            capabilities |= CAPABILITY_CAN_UPGRADE_TO_VIDEO;
             capabilities |= CAPABILITY_MUTE;
             capabilities |= CAPABILITY_SUPPORT_HOLD;
             capabilities |= CAPABILITY_HOLD;
@@ -212,6 +214,7 @@
         @Override
         public void onAudioStateChanged(AudioState state) { }
 
+
         public void setTestVideoCallProvider(TestVideoProvider testVideoCallProvider) {
             mTestVideoCallProvider = testVideoCallProvider;
         }
@@ -281,7 +284,8 @@
                     handle.getSchemeSpecificPart() + "..", ""),
                     originalRequest.getExtras(),
                     originalRequest.getVideoState());
-
+            connection.setVideoState(originalRequest.getVideoState());
+            addVideoProvider(connection);
             addCall(connection);
             connection.startOutgoing();
 
@@ -312,14 +316,6 @@
             Uri address = providedHandle == null ?
                     Uri.fromParts(PhoneAccount.SCHEME_TEL, getDummyNumber(isVideoCall), null)
                     : providedHandle;
-            if (isVideoCall) {
-                TestVideoProvider testVideoCallProvider =
-                        new TestVideoProvider(getApplicationContext());
-                connection.setVideoProvider(testVideoCallProvider);
-
-                // Keep reference to original so we can clean up the media players later.
-                connection.setTestVideoCallProvider(testVideoCallProvider);
-            }
 
             int videoState = isVideoCall ?
                     VideoProfile.VideoState.BIDIRECTIONAL :
@@ -327,6 +323,8 @@
             connection.setVideoState(videoState);
             connection.setAddress(address, TelecomManager.PRESENTATION_ALLOWED);
 
+            addVideoProvider(connection);
+
             addCall(connection);
 
             ConnectionRequest newRequest = new ConnectionRequest(
@@ -367,6 +365,15 @@
         }
     }
 
+    private void addVideoProvider(TestConnection connection) {
+        TestVideoProvider testVideoCallProvider =
+                new TestVideoProvider(getApplicationContext(), connection);
+        connection.setVideoProvider(testVideoCallProvider);
+
+        // Keep reference to original so we can clean up the media players later.
+        connection.setTestVideoCallProvider(testVideoCallProvider);
+    }
+
     private void activateCall(TestConnection connection) {
         if (mMediaPlayer == null) {
             mMediaPlayer = createMediaPlayer();
diff --git a/testapps/src/com/android/server/telecom/testapps/TestVideoProvider.java b/testapps/src/com/android/server/telecom/testapps/TestVideoProvider.java
index 655d73b..35aaad6 100644
--- a/testapps/src/com/android/server/telecom/testapps/TestVideoProvider.java
+++ b/testapps/src/com/android/server/telecom/testapps/TestVideoProvider.java
@@ -20,6 +20,7 @@
 import com.android.ex.camera2.blocking.BlockingCameraManager.BlockingOpenException;
 import com.android.ex.camera2.blocking.BlockingSessionCallback;
 import com.android.server.telecom.testapps.R;
+import com.android.server.telecom.testapps.TestConnectionService.TestConnection;
 
 import android.content.Context;
 import android.graphics.SurfaceTexture;
@@ -52,6 +53,7 @@
  * Implements the VideoCallProvider.
  */
 public class TestVideoProvider extends Connection.VideoProvider {
+    private TestConnection mConnection;
     private CameraCapabilities mCameraCapabilities;
     private Random random;
     private Surface mDisplaySurface;
@@ -65,11 +67,14 @@
     private CameraCaptureSession mCameraSession;
     private CameraThread mLooperThread;
 
+    private final Handler mHandler = new Handler();
+
     private String mCameraId;
 
     private static final long SESSION_TIMEOUT_MS = 2000;
 
-    public TestVideoProvider(Context context) {
+    public TestVideoProvider(Context context, TestConnection connection) {
+        mConnection = connection;
         mContext = context;
         random = new Random();
         mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
@@ -140,15 +145,22 @@
      * the response back via the CallVideoClient.
      */
     @Override
-    public void onSendSessionModifyRequest(VideoProfile requestProfile) {
+    public void onSendSessionModifyRequest(final VideoProfile requestProfile) {
         log("Sent session modify request");
 
-        VideoProfile responseProfile = new VideoProfile(
-                requestProfile.getVideoState(), requestProfile.getQuality());
-        receiveSessionModifyResponse(
-                SESSION_MODIFY_REQUEST_SUCCESS,
-                requestProfile,
-                responseProfile);
+        mHandler.postDelayed(new Runnable() {
+            @Override
+            public void run() {
+                final VideoProfile responseProfile = new VideoProfile(
+                        requestProfile.getVideoState(), requestProfile.getQuality());
+                mConnection.setVideoState(requestProfile.getVideoState());
+
+                receiveSessionModifyResponse(
+                        SESSION_MODIFY_REQUEST_SUCCESS,
+                        requestProfile,
+                        responseProfile);
+            }
+        }, 2000);
     }
 
     @Override