Merge "IMS-VT: Moving upgradeVideoRequest handling to InCallPresenter" into nyc-dev am: d1f0d57602
am: aac29b4966

* commit 'aac29b49662558f55d737fe2bb7f5060eddd1092':
  IMS-VT: Moving upgradeVideoRequest handling to InCallPresenter
diff --git a/InCallUI/src/com/android/incallui/AnswerPresenter.java b/InCallUI/src/com/android/incallui/AnswerPresenter.java
index 6e1fb3c..2bd3629 100644
--- a/InCallUI/src/com/android/incallui/AnswerPresenter.java
+++ b/InCallUI/src/com/android/incallui/AnswerPresenter.java
@@ -77,7 +77,7 @@
             showAnswerUi(false);
             Log.d(this, "declining upgrade request id: ");
             CallList.getInstance().removeCallUpdateListener(mCallId, this);
-            InCallPresenter.getInstance().declineUpgradeRequest(getUi().getContext());
+            InCallPresenter.getInstance().declineUpgradeRequest();
         }
         if (!call.getId().equals(mCallId)) {
             // A new call is coming in.
diff --git a/InCallUI/src/com/android/incallui/CallCardPresenter.java b/InCallUI/src/com/android/incallui/CallCardPresenter.java
index de2fb58..518e7d2 100644
--- a/InCallUI/src/com/android/incallui/CallCardPresenter.java
+++ b/InCallUI/src/com/android/incallui/CallCardPresenter.java
@@ -276,7 +276,7 @@
                     mPrimary.getState() == Call.State.INCOMING);
             updatePrimaryDisplayInfo();
             maybeStartSearch(mPrimary, true);
-            mPrimary.setSessionModificationState(Call.SessionModificationState.NO_REQUEST);
+            maybeClearSessionModificationState(mPrimary);
         }
 
         if (previousPrimary != null && mPrimary == null) {
@@ -296,7 +296,7 @@
                     mSecondary.getState() == Call.State.INCOMING);
             updateSecondaryDisplayInfo();
             maybeStartSearch(mSecondary, false);
-            mSecondary.setSessionModificationState(Call.SessionModificationState.NO_REQUEST);
+            maybeClearSessionModificationState(mSecondary);
         }
 
         // Start/stop timers.
@@ -571,6 +571,13 @@
         }
     }
 
+    private void maybeClearSessionModificationState(Call call) {
+        if (call.getSessionModificationState() !=
+                Call.SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
+            call.setSessionModificationState(Call.SessionModificationState.NO_REQUEST);
+        }
+    }
+
     /**
      * Starts a query for more contact data for the save primary and secondary calls.
      */
diff --git a/InCallUI/src/com/android/incallui/InCallPresenter.java b/InCallUI/src/com/android/incallui/InCallPresenter.java
index f50da8d..c51a561 100644
--- a/InCallUI/src/com/android/incallui/InCallPresenter.java
+++ b/InCallUI/src/com/android/incallui/InCallPresenter.java
@@ -78,7 +78,8 @@
  * TODO: This class has become more of a state machine at this point.  Consider renaming.
  */
 public class InCallPresenter implements CallList.Listener,
-        CircularRevealFragment.OnCircularRevealCompleteListener {
+        CircularRevealFragment.OnCircularRevealCompleteListener,
+        InCallVideoCallCallbackNotifier.SessionModificationListener {
 
     private static final String EXTRA_FIRST_TIME_SHOWN =
             "com.android.incallui.intent.extra.FIRST_TIME_SHOWN";
@@ -387,6 +388,7 @@
         mCallList.addListener(this);
 
         VideoPauseController.getInstance().setUp(this);
+        InCallVideoCallCallbackNotifier.getInstance().addSessionModificationListener(this);
 
         mFilteredQueryHandler = new FilteredNumberAsyncQueryHandler(context.getContentResolver());
         mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
@@ -413,6 +415,7 @@
 
         mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
         VideoPauseController.getInstance().tearDown();
+        InCallVideoCallCallbackNotifier.getInstance().removeSessionModificationListener(this);
     }
 
     private void attemptFinishActivity() {
@@ -726,6 +729,17 @@
         }
     }
 
+    @Override
+    public void onUpgradeToVideoRequest(Call call, int videoState) {
+        Log.d(this, "onUpgradeToVideoRequest call = " + call + " video state = " + videoState);
+
+        if (call == null) {
+            return;
+        }
+
+        call.setRequestedVideoState(videoState);
+    }
+
     /**
      * Given the call list, return the state in which the in-call screen should be.
      */
@@ -980,6 +994,14 @@
         }
     }
 
+    /*package*/
+    void declineUpgradeRequest() {
+        // Pass mContext if InCallActivity is destroyed.
+        // Ex: When user pressed back key while in active call and
+        // then modify request is received followed by MT call.
+        declineUpgradeRequest(mInCallActivity != null ? mInCallActivity : mContext);
+    }
+
     /**
      * Returns true if the incall app is the foreground application.
      */
diff --git a/InCallUI/src/com/android/incallui/InCallVideoCallCallback.java b/InCallUI/src/com/android/incallui/InCallVideoCallCallback.java
index 76f8c09..99e6d51 100644
--- a/InCallUI/src/com/android/incallui/InCallVideoCallCallback.java
+++ b/InCallUI/src/com/android/incallui/InCallVideoCallCallback.java
@@ -55,10 +55,8 @@
         boolean wasVideoCall = VideoUtils.isVideoCall(previousVideoState);
         boolean isVideoCall = VideoUtils.isVideoCall(newVideoState);
 
-        // Check for upgrades to video and downgrades to audio.
-        if (wasVideoCall && !isVideoCall) {
-            InCallVideoCallCallbackNotifier.getInstance().downgradeToAudio(mCall);
-        } else if (previousVideoState != newVideoState) {
+        // Check for upgrades to video.
+        if (!wasVideoCall && isVideoCall && previousVideoState != newVideoState) {
             InCallVideoCallCallbackNotifier.getInstance().upgradeToVideoRequest(mCall,
                 newVideoState);
         }
@@ -93,21 +91,8 @@
                             Call.SessionModificationState.REQUEST_FAILED);
                 }
             }
-            InCallVideoCallCallbackNotifier.getInstance().upgradeToVideoFail(status, mCall);
-        } else if (requestedProfile != null && responseProfile != null) {
-            boolean modifySucceeded = requestedProfile.getVideoState() ==
-                    responseProfile.getVideoState();
-            boolean isVideoCall = VideoUtils.isVideoCall(responseProfile.getVideoState());
-            if (modifySucceeded && isVideoCall) {
-                InCallVideoCallCallbackNotifier.getInstance().upgradeToVideoSuccess(mCall);
-            } else if (!modifySucceeded && isVideoCall) {
-                InCallVideoCallCallbackNotifier.getInstance().upgradeToVideoFail(status, mCall);
-            } else if (modifySucceeded && !isVideoCall) {
-                InCallVideoCallCallbackNotifier.getInstance().downgradeToAudio(mCall);
-            }
-        } else {
-            Log.d(this, "onSessionModifyResponseReceived request and response Profiles are null");
         }
+
         // Finally clear the outstanding request.
         mCall.setSessionModificationState(Call.SessionModificationState.NO_REQUEST);
     }
diff --git a/InCallUI/src/com/android/incallui/InCallVideoCallCallbackNotifier.java b/InCallUI/src/com/android/incallui/InCallVideoCallCallbackNotifier.java
index dfb645b..bb75292 100644
--- a/InCallUI/src/com/android/incallui/InCallVideoCallCallbackNotifier.java
+++ b/InCallUI/src/com/android/incallui/InCallVideoCallCallbackNotifier.java
@@ -135,39 +135,6 @@
     }
 
     /**
-     * Inform listeners of a successful response to a video request for a call.
-     *
-     * @param call The call.
-     */
-    public void upgradeToVideoSuccess(Call call) {
-        for (SessionModificationListener listener : mSessionModificationListeners) {
-            listener.onUpgradeToVideoSuccess(call);
-        }
-    }
-
-    /**
-     * Inform listeners of an unsuccessful response to a video request for a call.
-     *
-     * @param call The call.
-     */
-    public void upgradeToVideoFail(int status, Call call) {
-        for (SessionModificationListener listener : mSessionModificationListeners) {
-            listener.onUpgradeToVideoFail(status, call);
-        }
-    }
-
-    /**
-     * Inform listeners of a downgrade to audio.
-     *
-     * @param call The call.
-     */
-    public void downgradeToAudio(Call call) {
-        for (SessionModificationListener listener : mSessionModificationListeners) {
-            listener.onDowngradeToAudio(call);
-        }
-    }
-
-    /**
      * Inform listeners of a call session event.
      *
      * @param event The call session event.
@@ -240,41 +207,16 @@
     }
 
     /**
-     * Listener interface for any class that wants to be notified of upgrade to video and downgrade
-     * to audio session modification requests.
+     * Listener interface for any class that wants to be notified of upgrade to video request.
      */
     public interface SessionModificationListener {
         /**
          * Called when a peer request is received to upgrade an audio-only call to a video call.
          *
          * @param call The call the request was received for.
-         * @param videoState The video state that the request wants to upgrade to.
+         * @param videoState The requested video state.
          */
         public void onUpgradeToVideoRequest(Call call, int videoState);
-
-        /**
-         * Called when a request to a peer to upgrade an audio-only call to a video call is
-         * successful.
-         *
-         * @param call The call the request was successful for.
-         */
-        public void onUpgradeToVideoSuccess(Call call);
-
-        /**
-         * Called when a request to a peer to upgrade an audio-only call to a video call is
-         * NOT successful. This can be if the peer chooses rejects the the video call, or if the
-         * peer does not support video calling, or if there is some error in sending the request.
-         *
-         * @param call The call the request was successful for.
-         */
-        public void onUpgradeToVideoFail(int status, Call call);
-
-        /**
-         * Called when a call has been downgraded to audio-only.
-         *
-         * @param call The call which was downgraded to audio-only.
-         */
-        public void onDowngradeToAudio(Call call);
     }
 
     /**
diff --git a/InCallUI/src/com/android/incallui/VideoCallPresenter.java b/InCallUI/src/com/android/incallui/VideoCallPresenter.java
index ebdf820..2b6bc09 100644
--- a/InCallUI/src/com/android/incallui/VideoCallPresenter.java
+++ b/InCallUI/src/com/android/incallui/VideoCallPresenter.java
@@ -68,7 +68,6 @@
 public class VideoCallPresenter extends Presenter<VideoCallPresenter.VideoCallUi> implements
         IncomingCallListener, InCallOrientationListener, InCallStateListener,
         InCallDetailsListener, SurfaceChangeListener, VideoEventListener,
-        InCallVideoCallCallbackNotifier.SessionModificationListener,
         InCallPresenter.InCallEventListener {
     public static final String TAG = "VideoCallPresenter";
 
@@ -241,7 +240,6 @@
         // Register for surface and video events from {@link InCallVideoCallListener}s.
         InCallVideoCallCallbackNotifier.getInstance().addSurfaceChangeListener(this);
         InCallVideoCallCallbackNotifier.getInstance().addVideoEventListener(this);
-        InCallVideoCallCallbackNotifier.getInstance().addSessionModificationListener(this);
         mCurrentVideoState = VideoProfile.STATE_AUDIO_ONLY;
         mCurrentCallState = Call.State.INVALID;
     }
@@ -268,7 +266,6 @@
 
         InCallVideoCallCallbackNotifier.getInstance().removeSurfaceChangeListener(this);
         InCallVideoCallCallbackNotifier.getInstance().removeVideoEventListener(this);
-        InCallVideoCallCallbackNotifier.getInstance().removeSessionModificationListener(this);
     }
 
     /**
@@ -992,57 +989,6 @@
     }
 
     /**
-     * Handles an incoming upgrade to video request.
-     *
-     * @param call The call the request was received for.
-     * @param videoState The video state that the request wants to upgrade to.
-     */
-    @Override
-    public void onUpgradeToVideoRequest(Call call, int videoState) {
-        Log.d(this, "onUpgradeToVideoRequest call = " + call + " new video state = " + videoState);
-        if (mPrimaryCall == null || !Call.areSame(mPrimaryCall, call)) {
-            Log.w(this, "UpgradeToVideoRequest received for non-primary call");
-        }
-
-        if (call == null) {
-            return;
-        }
-
-        call.setRequestedVideoState(videoState);
-    }
-
-    @Override
-    public void onUpgradeToVideoSuccess(Call call) {
-        Log.d(this, "onUpgradeToVideoSuccess call=" + call);
-        if (mPrimaryCall == null || !Call.areSame(mPrimaryCall, call)) {
-            Log.w(this, "UpgradeToVideoSuccess received for non-primary call");
-        }
-
-        if (call == null) {
-            return;
-        }
-    }
-
-    @Override
-    public void onUpgradeToVideoFail(int status, Call call) {
-        Log.d(this, "onUpgradeToVideoFail call=" + call);
-        if (mPrimaryCall == null || !Call.areSame(mPrimaryCall, call)) {
-            Log.w(this, "UpgradeToVideoFail received for non-primary call");
-        }
-
-        if (call == null) {
-            return;
-        }
-    }
-
-    @Override
-    public void onDowngradeToAudio(Call call) {
-        call.setSessionModificationState(Call.SessionModificationState.NO_REQUEST);
-        // exit video mode
-        exitVideoMode();
-    }
-
-    /**
      * Sets the preview surface size based on the current device orientation.
      * See: {@link InCallOrientationEventListener#SCREEN_ORIENTATION_0},
      * {@link InCallOrientationEventListener#SCREEN_ORIENTATION_90},
diff --git a/InCallUI/src/com/android/incallui/VideoPauseController.java b/InCallUI/src/com/android/incallui/VideoPauseController.java
index 070448e..a529d20 100644
--- a/InCallUI/src/com/android/incallui/VideoPauseController.java
+++ b/InCallUI/src/com/android/incallui/VideoPauseController.java
@@ -27,8 +27,7 @@
  * This class is responsible for generating video pause/resume requests when the InCall UI is sent
  * to the background and subsequently brought back to the foreground.
  */
-class VideoPauseController implements InCallStateListener, IncomingCallListener,
-        SessionModificationListener {
+class VideoPauseController implements InCallStateListener, IncomingCallListener {
     private static final String TAG = "VideoPauseController";
 
     /**
@@ -105,7 +104,6 @@
         mInCallPresenter = Preconditions.checkNotNull(inCallPresenter);
         mInCallPresenter.addListener(this);
         mInCallPresenter.addIncomingCallListener(this);
-        InCallVideoCallCallbackNotifier.getInstance().addSessionModificationListener(this);
     }
 
     /**
@@ -114,7 +112,6 @@
      */
     public void tearDown() {
         log("tearDown...");
-        InCallVideoCallCallbackNotifier.getInstance().removeSessionModificationListener(this);
         mInCallPresenter.removeListener(this);
         mInCallPresenter.removeIncomingCallListener(this);
         clear();
@@ -260,46 +257,6 @@
     }
 
     /**
-     * Handles requests to upgrade to video.
-     *
-     * @param call The call the request was received for.
-     * @param videoState The video state that the request wants to upgrade to.
-     */
-    @Override
-    public void onUpgradeToVideoRequest(Call call, int videoState) {
-        // Not used.
-    }
-
-    /**
-     * Handles successful upgrades to video.
-     * @param call The call the request was successful for.
-     */
-    @Override
-    public void onUpgradeToVideoSuccess(Call call) {
-        // Not used.
-    }
-
-    /**
-     * Handles a failure to upgrade a call to video.
-     *
-     * @param status The failure status.
-     * @param call The call the request was successful for.
-     */
-    @Override
-    public void onUpgradeToVideoFail(int status, Call call) {
-        // TODO (ims-vt) Automatically bring in call ui to foreground.
-    }
-
-    /**
-     * Handles a downgrade of a call to audio-only.
-     *
-     * @param call The call which was downgraded to audio-only.
-     */
-    @Override
-    public void onDowngradeToAudio(Call call) {
-    }
-
-    /**
      * Called when UI is brought to the foreground.  Sends a session modification request to resume
      * the outgoing video.
      */