Implement ConnectionService.setAudioModeIsVoip

Change-Id: I29028e614c598d1f2501c4dfdcdbfc929b09f925
diff --git a/src/com/android/telecomm/Call.java b/src/com/android/telecomm/Call.java
index 2810dc8..99e4b07 100644
--- a/src/com/android/telecomm/Call.java
+++ b/src/com/android/telecomm/Call.java
@@ -75,6 +75,7 @@
         void onCallVideoProviderChanged(Call call);
         void onFeaturesChanged(Call call);
         void onCallerInfoChanged(Call call);
+        void onAudioModeIsVoipChanged(Call call);
     }
 
     abstract static class ListenerBase implements Listener {
@@ -110,6 +111,8 @@
         public void onFeaturesChanged(Call call) {}
         @Override
         public void onCallerInfoChanged(Call call) {}
+        @Override
+        public void onAudioModeIsVoipChanged(Call call) {}
     }
 
     private static final OnQueryCompleteListener sCallerInfoQueryListener =
@@ -228,6 +231,8 @@
     /** Features associated with the call which the InCall UI may wish to show icons for. */
     private int mFeatures;
 
+    private boolean mAudioModeIsVoip;
+
     /**
      * Creates an empty call object.
      *
@@ -1058,4 +1063,15 @@
     public void setVideoState(int videoState) {
         mVideoState = videoState;
     }
+
+    public boolean getAudioModeIsVoip() {
+        return mAudioModeIsVoip;
+    }
+
+    public void setAudioModeIsVoip(boolean audioModeIsVoip) {
+        mAudioModeIsVoip = audioModeIsVoip;
+        for (Listener l : mListeners) {
+            l.onAudioModeIsVoipChanged(Call.this);
+        }
+    }
 }
diff --git a/src/com/android/telecomm/CallAudioManager.java b/src/com/android/telecomm/CallAudioManager.java
index 35de0cc..8156db0 100644
--- a/src/com/android/telecomm/CallAudioManager.java
+++ b/src/com/android/telecomm/CallAudioManager.java
@@ -102,6 +102,11 @@
         updateAudioForForegroundCall();
     }
 
+    @Override
+    public void onAudioModeIsVoipChanged(Call call) {
+        updateAudioStreamAndMode();
+    }
+
     void toggleMute() {
         mute(!mAudioState.isMuted);
     }
@@ -277,8 +282,8 @@
         } else {
             Call call = getForegroundCall();
             if (call != null) {
-                int mode = TelephonyUtil.isCurrentlyPSTNCall(call) ?
-                        AudioManager.MODE_IN_CALL : AudioManager.MODE_IN_COMMUNICATION;
+                int mode = call.getAudioModeIsVoip() ?
+                        AudioManager.MODE_IN_COMMUNICATION : AudioManager.MODE_IN_CALL;
                 requestAudioFocusAndSetMode(AudioManager.STREAM_VOICE_CALL, mode);
             } else if (mIsTonePlaying) {
                 // There is no call, however, we are still playing a tone, so keep focus.
diff --git a/src/com/android/telecomm/CallsManager.java b/src/com/android/telecomm/CallsManager.java
index 3884adb..d451162 100644
--- a/src/com/android/telecomm/CallsManager.java
+++ b/src/com/android/telecomm/CallsManager.java
@@ -61,6 +61,7 @@
         void onCannedSmsResponsesLoaded(Call call);
         void onCallVideoProviderChanged(Call call);
         void onFeaturesChanged(Call call);
+        void onAudioModeIsVoipChanged(Call call);
     }
 
     private static final CallsManager INSTANCE = new CallsManager();
@@ -226,6 +227,13 @@
         }
     }
 
+    @Override
+    public void onAudioModeIsVoipChanged(Call call) {
+        for (CallsManagerListener listener : mListeners) {
+            listener.onAudioModeIsVoipChanged(call);
+        }
+    }
+
     ImmutableCollection<Call> getCalls() {
         return ImmutableList.copyOf(mCalls);
     }
diff --git a/src/com/android/telecomm/CallsManagerListenerBase.java b/src/com/android/telecomm/CallsManagerListenerBase.java
index f34bdad..b6eb3b9 100644
--- a/src/com/android/telecomm/CallsManagerListenerBase.java
+++ b/src/com/android/telecomm/CallsManagerListenerBase.java
@@ -83,4 +83,8 @@
     @Override
     public void onFeaturesChanged(Call call) {
     }
+
+    @Override
+    public void onAudioModeIsVoipChanged(Call call) {
+    }
 }
diff --git a/src/com/android/telecomm/ConnectionServiceWrapper.java b/src/com/android/telecomm/ConnectionServiceWrapper.java
index f53eeb3..1bad6fd 100644
--- a/src/com/android/telecomm/ConnectionServiceWrapper.java
+++ b/src/com/android/telecomm/ConnectionServiceWrapper.java
@@ -76,6 +76,7 @@
     private static final int MSG_QUERY_REMOTE_CALL_SERVICES = 16;
     private static final int MSG_SET_CALL_VIDEO_PROVIDER = 17;
     private static final int MSG_SET_FEATURES = 18;
+    private static final int MSG_SET_AUDIO_MODE_IS_VOIP = 19;
 
     private final Handler mHandler = new Handler() {
         @Override
@@ -288,7 +289,7 @@
                     SomeArgs args = (SomeArgs) msg.obj;
                     try {
                         call = mCallIdMapper.getCall(args.arg1);
-                        int features = (int) args.arg2;
+                        int features = (int) args.argi1;
                         if (call != null) {
                             call.setFeatures(features);
                         }
@@ -297,6 +298,19 @@
                     }
                     break;
                 }
+                case MSG_SET_AUDIO_MODE_IS_VOIP: {
+                    SomeArgs args = (SomeArgs) msg.obj;
+                    try {
+                        call = mCallIdMapper.getCall(args.arg1);
+                        boolean isVoip = args.argi1 == 1;
+                        if (call != null) {
+                            call.setAudioModeIsVoip(isVoip);
+                        }
+                    } finally {
+                        args.recycle();
+                    }
+                    break;
+                }
             }
         }
     };
@@ -463,9 +477,19 @@
             mCallIdMapper.checkValidCallId(callId);
             SomeArgs args = SomeArgs.obtain();
             args.arg1 = callId;
-            args.arg2 = features;
+            args.argi1 = features;
             mHandler.obtainMessage(MSG_SET_FEATURES, args).sendToTarget();
         }
+
+        @Override
+        public void setAudioModeIsVoip(String callId, boolean isVoip) {
+            logIncoming("setAudioModeIsVoip %s %d", callId, isVoip);
+            mCallIdMapper.checkValidCallId(callId);
+            SomeArgs args = SomeArgs.obtain();
+            args.arg1 = callId;
+            args.argi1 = isVoip ? 1 : 0;
+            mHandler.obtainMessage(MSG_SET_AUDIO_MODE_IS_VOIP, args).sendToTarget();
+        }
     }
 
     private final Adapter mAdapter = new Adapter();