Add CallsManagerListener

Also contains some minor bug fixes:
- add an incoming field to Call
- correcty log failed outgoing calls (previously these were mostly dropped)
- log missed incoming calls

Change-Id: I72dc39efd519302c1f765f4f9c9d04c5095e45a6
diff --git a/src/com/android/telecomm/CallAudioManager.java b/src/com/android/telecomm/CallAudioManager.java
new file mode 100644
index 0000000..07d5dbe
--- /dev/null
+++ b/src/com/android/telecomm/CallAudioManager.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.telecomm;
+
+import android.content.Context;
+import android.media.AudioManager;
+import android.telecomm.CallState;
+
+/**
+ * This class manages audio modes, streams and other properties.
+ */
+final class CallAudioManager extends CallsManagerListenerBase {
+    @Override
+    public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
+        switch (newState) {
+            case ACTIVE:
+                updateAudioFocusForActiveCall(call);
+                break;
+            case DISCONNECTED:
+                updateAudioFocusForNoCall();
+                break;
+            default:
+                break;
+        }
+    }
+
+    private void updateAudioFocusForActiveCall(Call call) {
+        Log.v(this, "onForegroundCallChanged, requesting audio focus");
+        Context context = TelecommApp.getInstance();
+        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        audioManager.requestAudioFocusForCall(AudioManager.STREAM_VOICE_CALL,
+                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
+        audioManager.setMode(AudioManager.MODE_IN_CALL);
+        audioManager.setMicrophoneMute(false);
+        audioManager.setSpeakerphoneOn(false);
+    }
+
+    private void updateAudioFocusForNoCall() {
+        Log.v(this, "updateAudioFocusForNoCall, abandoning audio focus");
+        Context context = TelecommApp.getInstance().getApplicationContext();
+        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        audioManager.setMode(AudioManager.MODE_NORMAL);
+        audioManager.abandonAudioFocusForCall();
+    }
+}