Adding mute and speaker methods to CallCommandService.

Catching all exceptions from command service since they won't be sent over ipc.

Change-Id: Icdd11a9fe7d4041b228bf2543a8bf0b1ef7b1651
diff --git a/common/src/com/android/services/telephony/common/ICallCommandService.aidl b/common/src/com/android/services/telephony/common/ICallCommandService.aidl
index 81e8ab5..220b14c 100644
--- a/common/src/com/android/services/telephony/common/ICallCommandService.aidl
+++ b/common/src/com/android/services/telephony/common/ICallCommandService.aidl
@@ -39,4 +39,14 @@
      */
     void disconnectCall(int callId);
 
+    /**
+     * Mute the phone.
+     */
+    void mute(boolean onOff);
+
+    /**
+     * Turn on or off speaker.
+     */
+    void speaker(boolean onOff);
+
 }
diff --git a/src/com/android/phone/CallCommandService.java b/src/com/android/phone/CallCommandService.java
index c0f8c67..6a0ef2f 100644
--- a/src/com/android/phone/CallCommandService.java
+++ b/src/com/android/phone/CallCommandService.java
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (C) 2013 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -11,24 +11,30 @@
  * 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.
+ * limitations under the License
  */
 
 package com.android.phone;
 
+import android.content.Context;
+import android.util.Log;
+
 import com.android.internal.telephony.CallManager;
 import com.android.services.telephony.common.ICallCommandService;
 
 /**
- * Service interface used by in-call ui to control phone calls using commands
- * exposed as methods.  Instances of this class are handed to in-call UI via
- * CallMonitorService.
+ * Service interface used by in-call ui to control phone calls using commands exposed as methods.
+ * Instances of this class are handed to in-call UI via CallMonitorService.
  */
 class CallCommandService extends ICallCommandService.Stub {
 
+    private static final String TAG = CallCommandService.class.getSimpleName();
+
+    private Context mContext;
     private CallManager mCallManager;
 
-    public CallCommandService(CallManager callManager) {
+    public CallCommandService(Context context, CallManager callManager) {
+        mContext = context;
         mCallManager = callManager;
     }
 
@@ -37,8 +43,12 @@
      */
     @Override
     public void answerCall(int callId) {
-        // TODO(klp): Change to using the callId and logic from InCallScreen::internalAnswerCall
-        PhoneUtils.answerCall(mCallManager.getFirstActiveRingingCall());
+        try {
+            // TODO(klp): Change to using the callId and logic from InCallScreen::internalAnswerCall
+            PhoneUtils.answerCall(mCallManager.getFirstActiveRingingCall());
+        } catch (Exception e) {
+            Log.e(TAG, "Error during answerCall().", e);
+        }
     }
 
     /**
@@ -46,13 +56,40 @@
      */
     @Override
     public void rejectCall(int callId) {
-        // TODO(klp): Change to using the callId
-        PhoneUtils.hangupRingingCall(mCallManager.getFirstActiveRingingCall());
+        try {
+            // TODO(klp): Change to using the callId
+            PhoneUtils.hangupRingingCall(mCallManager.getFirstActiveRingingCall());
+        } catch (Exception e) {
+            Log.e(TAG, "Error during rejectCall().", e);
+        }
     }
 
     @Override
     public void disconnectCall(int callId) {
-        // TODO(klp): Change to using the callId
-        PhoneUtils.hangup(mCallManager);
+        try {
+            // TODO(klp): Change to using the callId
+            PhoneUtils.hangup(mCallManager);
+        } catch (Exception e) {
+            Log.e(TAG, "Error during disconnectCall().", e);
+        }
+    }
+
+    @Override
+    public void mute(boolean onOff) {
+        try {
+            PhoneUtils.setMute(onOff);
+        } catch (Exception e) {
+            Log.e(TAG, "Error during mute().", e);
+        }
+    }
+
+    @Override
+    public void speaker(boolean onOff) {
+        try {
+            // TODO(klp): add bluetooth logic from InCallScreen.toggleSpeaker()
+            PhoneUtils.turnOnSpeaker(mContext, onOff, true);
+        } catch (Exception e) {
+            Log.e(TAG, "Error during speaker().", e);
+        }
     }
 }
diff --git a/src/com/android/phone/CallHandlerServiceProxy.java b/src/com/android/phone/CallHandlerServiceProxy.java
index cb1e188..a9f6b21 100644
--- a/src/com/android/phone/CallHandlerServiceProxy.java
+++ b/src/com/android/phone/CallHandlerServiceProxy.java
@@ -39,7 +39,7 @@
     private static final String TAG = CallHandlerServiceProxy.class.getSimpleName();
     private static final boolean DBG =
             (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
-    private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2);
+
 
     private Context mContext;
     private CallStateMonitor mCallStateMonitor;
@@ -85,6 +85,11 @@
 
             @Override
             public void onServiceDisconnected(ComponentName className) {
+                // TODO(klp): handle the case where the in call ui crashed or gets destroyed.
+                // In the near term, we need to re-bind to the service when ever it's gone.
+                // Longer term, we need a way to catch the crash and allow the users to choose
+                // a different in-call screen.
+                Log.e(TAG, "Yikes! no in call ui!");
                 mCallHandlerService = null;
             }
         };
@@ -118,6 +123,8 @@
             } catch (RemoteException e) {
                 Log.e(TAG, "Remote exception handling onIncomingCall:" + e);
             }
+        } else {
+            Log.wtf(TAG, "Call handle service has not connected!  Cannot accept incoming call.");
         }
     }
 }
diff --git a/src/com/android/phone/PhoneGlobals.java b/src/com/android/phone/PhoneGlobals.java
index 6cc687d..2dbbd36 100644
--- a/src/com/android/phone/PhoneGlobals.java
+++ b/src/com/android/phone/PhoneGlobals.java
@@ -539,7 +539,7 @@
             callStateMonitor = new CallStateMonitor(mCM);
 
             // Service used by in-call UI to control calls
-            callCommandService = new CallCommandService(mCM);
+            callCommandService = new CallCommandService(this, mCM);
 
             // Sends call state to the UI
             CallHandlerServiceProxy = new CallHandlerServiceProxy(this, callStateMonitor,