Adding in-call service code.

Adding the in-call service binding code and the in-call adapter.

Change-Id: I034652001022ac26674096f2a91b09fe12e8fe1a
diff --git a/src/com/android/telecomm/CallsManager.java b/src/com/android/telecomm/CallsManager.java
index ce8674f..29890b6 100644
--- a/src/com/android/telecomm/CallsManager.java
+++ b/src/com/android/telecomm/CallsManager.java
@@ -35,6 +35,11 @@
 
     private static final CallsManager INSTANCE = new CallsManager();
 
+    private final Switchboard mSwitchboard;
+
+    /** Used to control the in-call app. */
+    private final InCallController mInCallController;
+
     /**
      * May be unnecessary per off-line discussions (between santoscordon and gilad) since the set
      * of CallsManager APIs that need to be exposed to the dialer (or any application firing call
@@ -44,8 +49,6 @@
 
     private InCallAdapter mInCallAdapter;
 
-    private Switchboard mSwitchboard;
-
     private CallLogManager mCallLogManager;
 
     private VoicemailManager mVoicemailManager;
@@ -63,6 +66,7 @@
      */
     private CallsManager() {
         mSwitchboard = new Switchboard();
+        mInCallController = new InCallController(this);
     }
 
     /**
@@ -85,4 +89,37 @@
         // No objection to issue the call, proceed with trying to put it through.
         mSwitchboard.placeOutgoingCall(handle, contactInfo, context);
     }
+
+    /**
+     * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
+     * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
+     * the user opting to answer said call.
+     *
+     * @param callId The ID of the call.
+     */
+    void answerCall(String callId) {
+        // TODO(santoscordon): fill in and check that it is in the ringing state.
+    }
+
+    /**
+     * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
+     * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
+     * the user opting to reject said call.
+     *
+     * @param callId The ID of the call.
+     */
+    void rejectCall(String callId) {
+        // TODO(santoscordon): fill in and check that it is in the ringing state.
+    }
+
+    /**
+     * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
+     * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
+     * the user hitting the end-call button.
+     *
+     * @param callId The ID of the call.
+     */
+    void disconnectCall(String callId) {
+        // TODO(santoscordon): fill in and check that the call is in the active state.
+    }
 }
diff --git a/src/com/android/telecomm/InCallAdapter.java b/src/com/android/telecomm/InCallAdapter.java
index 143e324..eb10318 100644
--- a/src/com/android/telecomm/InCallAdapter.java
+++ b/src/com/android/telecomm/InCallAdapter.java
@@ -1,11 +1,69 @@
+/*
+ * 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;
 
-/** Only exposes the CallsManager APIs that In-Call should have access to. */
-public class InCallAdapter {
-  private CallsManager callsManager;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.RemoteException;
+import android.telecomm.IInCallAdapter;
 
-  /** Package private */
-  InCallAdapter(CallsManager callsManager) {
-    this.callsManager = callsManager;
-  }
+/**
+ * Receives call commands and updates from in-call app and passes them through to CallsManager.
+ * {@link InCallController} creates an instance of this class and passes it to the in-call app after
+ * binding to it. This adapter can receive commands and updates until the in-call app is unbound.
+ */
+class InCallAdapter extends IInCallAdapter.Stub {
+    private final CallsManager mCallsManager;
+
+    private final Handler mHandler = new Handler(Looper.getMainLooper());
+
+    /** Persists the specified parameters. */
+    public InCallAdapter(CallsManager callsManager) {
+        mCallsManager = callsManager;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void answerCall(final String callId) throws RemoteException {
+        mHandler.post(new Runnable() {
+            @Override public void run() {
+                mCallsManager.answerCall(callId);
+            }
+        });
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void rejectCall(final String callId) throws RemoteException {
+        mHandler.post(new Runnable() {
+            @Override public void run() {
+                mCallsManager.rejectCall(callId);
+            }
+        });
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void disconnectCall(final String callId) throws RemoteException {
+        mHandler.post(new Runnable() {
+            @Override public void run() {
+                mCallsManager.disconnectCall(callId);
+            }
+        });
+    }
+
 }
diff --git a/src/com/android/telecomm/InCallController.java b/src/com/android/telecomm/InCallController.java
new file mode 100644
index 0000000..7b48d95
--- /dev/null
+++ b/src/com/android/telecomm/InCallController.java
@@ -0,0 +1,163 @@
+/*
+ * 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.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.ServiceConnection;
+import android.os.IBinder;
+import android.os.RemoteException;
+import android.telecomm.IInCallService;
+import android.util.Log;
+
+/**
+ * Binds to {@link IInCallService} and provides the service to {@link CallsManager} through which it
+ * can send updates to the in-call app. This class is created and owned by CallsManager and retains
+ * a binding to the {@link IInCallService} (implemented by the in-call app) until CallsManager
+ * explicitly disconnects it. CallsManager starts the connection by calling {@link #connect} and
+ * retains the connection as long as it has calls which need UI. When all calls are disconnected,
+ * CallsManager will invoke {@link #disconnect} to sever the binding until the in-call UI is needed
+ * again.
+ */
+public final class InCallController {
+    /**
+     * Used to bind to the in-call app and triggers the start of communication between
+     * CallsManager and in-call app.
+     */
+    private class InCallServiceConnection implements ServiceConnection {
+        /** {@inheritDoc} */
+        @Override public void onServiceConnected(ComponentName name, IBinder service) {
+            onConnected(service);
+        }
+
+        /** {@inheritDoc} */
+        @Override public void onServiceDisconnected(ComponentName name) {
+            onDisconnected();
+        }
+    }
+
+    private static final String TAG = InCallController.class.getSimpleName();
+
+    /**
+     * Package name of the in-call app. Although in-call code in kept in its own namespace, it is
+     * ultimately compiled into the dialer apk, hence the difference in namespaces between this and
+     * {@link IN_CALL_SERVICE_CLASS_NAME}.
+     * TODO(santoscordon): Change this into config.xml resource entry.
+     */
+    private static final String IN_CALL_PACKAGE_NAME = "com.google.android.dialer";
+
+    /**
+     * Class name of the component within in-call app which implements {@link IInCallService}.
+     */
+    private static final String IN_CALL_SERVICE_CLASS_NAME = "com.android.incall.InCallService";
+
+    /** Maintains a binding connection to the in-call app. */
+    private final InCallServiceConnection mConnection = new InCallServiceConnection();
+
+    private final CallsManager mCallsManager;
+
+    /** The in-call app implementation, see {@link IInCallService}. */
+    private IInCallService mInCallService;
+
+    /**
+     * Persists the specified parameters.
+     *
+     * @param callsManager The singleton calls manager instance.
+     */
+    InCallController(CallsManager callsManager) {
+        mCallsManager = callsManager;
+    }
+
+    // TODO(santoscordon): May be better to expose the IInCallService methods directly from this
+    // class as its own method to make the CallsManager code easier to read.
+    IInCallService getService() {
+        return mInCallService;
+    }
+
+    /**
+     * Binds to the in-call app if not already connected by binding directly to the saved
+     * component name of the {@link IInCallService} implementation.
+     *
+     * @param context The application context.
+     */
+    void connect(Context context) {
+        ThreadUtil.checkOnMainThread();
+        if (mInCallService == null) {
+            ComponentName component =
+                    new ComponentName(IN_CALL_PACKAGE_NAME, IN_CALL_SERVICE_CLASS_NAME);
+
+            Intent serviceIntent = new Intent(IInCallService.class.getName());
+            serviceIntent.setComponent(component);
+
+            if (!context.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
+                Log.e(TAG, "Could not connect to the in-call app (" + component + ")");
+
+                // TODO(santoscordon): Implement retry or fall-back-to-default logic.
+            }
+        }
+    }
+
+    /**
+     * Unbinds an existing bound connection to the in-call app.
+     *
+     * @param context The application context.
+     */
+    void disconnect(Context context) {
+        ThreadUtil.checkOnMainThread();
+        if (mInCallService != null) {
+            context.unbindService(mConnection);
+            mInCallService = null;
+        }
+    }
+
+    /**
+     * Persists the {@link IInCallService} instance and starts the communication between
+     * CallsManager and in-call app by sending the first update to in-call app. This method is
+     * called after a successful binding connection is established.
+     *
+     * @param service The {@link IInCallService} implementation.
+     */
+    private void onConnected(IBinder service) {
+        ThreadUtil.checkOnMainThread();
+        mInCallService = IInCallService.Stub.asInterface(service);
+
+        try {
+            mInCallService.setInCallAdapter(new InCallAdapter(mCallsManager));
+        } catch (RemoteException e) {
+            Log.e(TAG, "Failed to set the in-call adapter.", e);
+            mInCallService = null;
+        }
+
+        update();
+    }
+
+    /**
+     * Cleans up the instance of in-call app after the service has been unbound.
+     */
+    private void onDisconnected() {
+        ThreadUtil.checkOnMainThread();
+        mInCallService = null;
+    }
+
+    /**
+     * Gathers the list of current calls from CallsManager and sends them to the in-call app.
+     */
+    private void update() {
+        // TODO(santoscordon): mInCallService.sendCalls(CallsManager.getCallList());
+    }
+}