Adding CallMonitorService reference into PhoneGlobals.

This searches for and starts the service that is implemented by
InCallUI.

Change-Id: I604f122b1497ebf6d526dda6a6844392255a6916
diff --git a/src/com/android/phone/CallMonitorServiceProxy.java b/src/com/android/phone/CallMonitorServiceProxy.java
new file mode 100644
index 0000000..8865038
--- /dev/null
+++ b/src/com/android/phone/CallMonitorServiceProxy.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2013 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.phone;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.ServiceConnection;
+import android.os.AsyncResult;
+import android.os.Handler;
+import android.os.IBinder;
+import android.os.Message;
+import android.os.RemoteException;
+import android.os.SystemProperties;
+import android.util.Log;
+
+import com.android.services.telephony.common.ICallMonitorService;
+
+/**
+ * This class is responsible for passing through call state changes to the CallMonitorService.
+ */
+public class CallMonitorServiceProxy extends Handler {
+
+    private static final String TAG = CallMonitorServiceProxy.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;
+    private ServiceConnection mConnection;
+    private ICallMonitorService mCallMonitorService;
+
+    public CallMonitorServiceProxy(Context context, CallStateMonitor callStateMonitor) {
+        mContext = context;
+        mCallStateMonitor = callStateMonitor;
+
+        mCallStateMonitor.addListener(this);
+        setupServiceConnection();
+    }
+
+    @Override
+    public void handleMessage(Message msg) {
+        switch (msg.what) {
+            case CallStateMonitor.PHONE_NEW_RINGING_CONNECTION:
+                onNewRingingConnection((AsyncResult) msg.obj);
+                break;
+
+            default:
+                //super.handleMessage(msg);
+                break;
+        }
+    }
+
+    /**
+     * Sets up the connection with ICallMonitorService
+     */
+    private void setupServiceConnection() {
+        mConnection = new ServiceConnection() {
+            @Override
+            public void onServiceConnected(ComponentName className, IBinder service) {
+                if (DBG) {
+                    Log.d(TAG, "Service Connected");
+                }
+                mCallMonitorService = ICallMonitorService.Stub.asInterface(service);
+            }
+
+            @Override
+            public void onServiceDisconnected(ComponentName className) {
+                mCallMonitorService = null;
+            }
+        };
+
+        Intent serviceIntent = new Intent(ICallMonitorService.class.getName());
+        if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
+            Log.e(TAG, "Cound not bind to ICallMonitorService");
+        }
+    }
+
+    /**
+     * Send notification of a new incoming call.
+     */
+    private void onNewRingingConnection(AsyncResult result) {
+        if (mCallMonitorService != null) {
+            try {
+                mCallMonitorService.onIncomingCall(42);
+            } catch (RemoteException e) {
+                Log.e(TAG, "Remote exception handling onIncomingCall:" + e);
+            }
+        }
+    }
+}
diff --git a/src/com/android/phone/CallStateMonitor.java b/src/com/android/phone/CallStateMonitor.java
index 6a03e22..6152277 100644
--- a/src/com/android/phone/CallStateMonitor.java
+++ b/src/com/android/phone/CallStateMonitor.java
@@ -92,6 +92,9 @@
 
     public void addListener(Handler handler) {
         if (handler != null && !registeredHandlers.contains(handler)) {
+            if (DBG) {
+                Log.d(LOG_TAG, "Adding Handler: " + handler);
+            }
             registeredHandlers.add(handler);
         }
     }
diff --git a/src/com/android/phone/PhoneGlobals.java b/src/com/android/phone/PhoneGlobals.java
index 019c74a..49332d1 100644
--- a/src/com/android/phone/PhoneGlobals.java
+++ b/src/com/android/phone/PhoneGlobals.java
@@ -172,6 +172,7 @@
     PhoneInterfaceManager phoneMgr;
     CallManager mCM;
     CallStateMonitor callStateMonitor;
+    CallMonitorServiceProxy callMonitorServiceProxy;
     int mBluetoothHeadsetState = BluetoothProfile.STATE_DISCONNECTED;
     int mBluetoothHeadsetAudioState = BluetoothHeadset.STATE_AUDIO_DISCONNECTED;
     boolean mShowBluetoothIndication = false;
@@ -536,6 +537,9 @@
             // Monitors call activity from the telephony layer
             callStateMonitor = new CallStateMonitor(mCM);
 
+            // Sends call state to the UI
+            callMonitorServiceProxy = new CallMonitorServiceProxy(this, callStateMonitor);
+
             // Create the CallNotifer singleton, which handles
             // asynchronous events from the telephony layer (like
             // launching the incoming-call UI when an incoming call comes