blob: dedc21c21b7e6b151eaa28e082538f635c6f992a [file] [log] [blame]
Santos Cordone3d76ab2014-01-28 17:25:20 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.telecomm;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
23import android.os.IBinder;
24import android.os.RemoteException;
Santos Cordon049b7b62014-01-30 05:34:26 -080025import android.telecomm.CallInfo;
Santos Cordone3d76ab2014-01-28 17:25:20 -080026import android.telecomm.IInCallService;
27import android.util.Log;
28
29/**
30 * Binds to {@link IInCallService} and provides the service to {@link CallsManager} through which it
31 * can send updates to the in-call app. This class is created and owned by CallsManager and retains
32 * a binding to the {@link IInCallService} (implemented by the in-call app) until CallsManager
33 * explicitly disconnects it. CallsManager starts the connection by calling {@link #connect} and
34 * retains the connection as long as it has calls which need UI. When all calls are disconnected,
35 * CallsManager will invoke {@link #disconnect} to sever the binding until the in-call UI is needed
36 * again.
37 */
38public final class InCallController {
39 /**
40 * Used to bind to the in-call app and triggers the start of communication between
41 * CallsManager and in-call app.
42 */
43 private class InCallServiceConnection implements ServiceConnection {
44 /** {@inheritDoc} */
45 @Override public void onServiceConnected(ComponentName name, IBinder service) {
46 onConnected(service);
47 }
48
49 /** {@inheritDoc} */
50 @Override public void onServiceDisconnected(ComponentName name) {
51 onDisconnected();
52 }
53 }
54
55 private static final String TAG = InCallController.class.getSimpleName();
56
57 /**
58 * Package name of the in-call app. Although in-call code in kept in its own namespace, it is
59 * ultimately compiled into the dialer apk, hence the difference in namespaces between this and
60 * {@link IN_CALL_SERVICE_CLASS_NAME}.
61 * TODO(santoscordon): Change this into config.xml resource entry.
62 */
63 private static final String IN_CALL_PACKAGE_NAME = "com.google.android.dialer";
64
65 /**
66 * Class name of the component within in-call app which implements {@link IInCallService}.
67 */
Santos Cordon049b7b62014-01-30 05:34:26 -080068 private static final String IN_CALL_SERVICE_CLASS_NAME = "com.android.incallui.InCallService";
Santos Cordone3d76ab2014-01-28 17:25:20 -080069
70 /** Maintains a binding connection to the in-call app. */
71 private final InCallServiceConnection mConnection = new InCallServiceConnection();
72
73 private final CallsManager mCallsManager;
74
75 /** The in-call app implementation, see {@link IInCallService}. */
76 private IInCallService mInCallService;
77
78 /**
79 * Persists the specified parameters.
Santos Cordone3d76ab2014-01-28 17:25:20 -080080 */
Santos Cordon681663d2014-01-30 04:32:15 -080081 InCallController() {
82 mCallsManager = CallsManager.getInstance();
Santos Cordone3d76ab2014-01-28 17:25:20 -080083 }
84
85 // TODO(santoscordon): May be better to expose the IInCallService methods directly from this
86 // class as its own method to make the CallsManager code easier to read.
87 IInCallService getService() {
88 return mInCallService;
89 }
90
91 /**
Santos Cordon049b7b62014-01-30 05:34:26 -080092 * Indicates to the in-call app that a new call has been created and an appropriate
93 * user-interface should be built and shown to notify the user. Information about the call
94 * including its current state is passed in through the callInfo object.
Santos Cordone3d76ab2014-01-28 17:25:20 -080095 *
Santos Cordon049b7b62014-01-30 05:34:26 -080096 * @param callInfo Details about the new call.
Santos Cordone3d76ab2014-01-28 17:25:20 -080097 */
Santos Cordon049b7b62014-01-30 05:34:26 -080098 void addCall(CallInfo callInfo) {
99 try {
100 if (mInCallService == null) {
101 bind();
102 } else {
103 // TODO(santoscordon): Protect against logging phone number.
104 Log.i(TAG, "Adding call: " + callInfo);
105 mInCallService.addCall(callInfo);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800106 }
Santos Cordon049b7b62014-01-30 05:34:26 -0800107 } catch (RemoteException e) {
108 Log.e(TAG, "Exception attempting to addCall.", e);
109 }
110 }
111
112 /**
113 * Indicates to the in-call app that a call has moved to the active state.
114 *
115 * @param callId The identifier of the call that became active.
116 */
117 void markCallAsActive(String callId) {
118 try {
119 if (mInCallService != null) {
120 Log.i(TAG, "Mark call as ACTIVE: " + callId);
121 mInCallService.setActive(callId);
122 }
123 } catch (RemoteException e) {
124 Log.e(TAG, "Exception attempting to markCallAsActive.", e);
125 }
126 }
127
128 /**
129 * Indicates to the in-call app that a call has been disconnected and the user should be
130 * notified.
131 *
132 * @param callId The identifier of the call that was disconnected.
133 */
134 void markCallAsDisconnected(String callId) {
135 try {
136 if (mInCallService != null) {
137 Log.i(TAG, "Mark call as DISCONNECTED: " + callId);
138 mInCallService.setDisconnected(callId);
139 }
140 } catch (RemoteException e) {
141 Log.e(TAG, "Exception attempting to markCallAsDisconnected.", e);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800142 }
143 }
144
145 /**
146 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800147 */
Santos Cordon049b7b62014-01-30 05:34:26 -0800148 void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800149 ThreadUtil.checkOnMainThread();
150 if (mInCallService != null) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800151 Log.i(TAG, "Unbinding from InCallService");
152 TelecommApp.getInstance().unbindService(mConnection);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800153 mInCallService = null;
154 }
155 }
156
157 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800158 * Binds to the in-call app if not already connected by binding directly to the saved
159 * component name of the {@link IInCallService} implementation.
160 */
161 private void bind() {
162 ThreadUtil.checkOnMainThread();
163 if (mInCallService == null) {
164 ComponentName component =
165 new ComponentName(IN_CALL_PACKAGE_NAME, IN_CALL_SERVICE_CLASS_NAME);
166 Log.i(TAG, "Attempting to bind to InCallService: " + component);
167
168 Intent serviceIntent = new Intent(IInCallService.class.getName());
169 serviceIntent.setComponent(component);
170
171 Context context = TelecommApp.getInstance();
172 if (!context.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
173 Log.e(TAG, "Could not connect to the in-call app (" + component + ")");
174
175 // TODO(santoscordon): Implement retry or fall-back-to-default logic.
176 }
177 }
178 }
179
180 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800181 * Persists the {@link IInCallService} instance and starts the communication between
182 * CallsManager and in-call app by sending the first update to in-call app. This method is
183 * called after a successful binding connection is established.
184 *
185 * @param service The {@link IInCallService} implementation.
186 */
187 private void onConnected(IBinder service) {
188 ThreadUtil.checkOnMainThread();
189 mInCallService = IInCallService.Stub.asInterface(service);
190
191 try {
192 mInCallService.setInCallAdapter(new InCallAdapter(mCallsManager));
193 } catch (RemoteException e) {
194 Log.e(TAG, "Failed to set the in-call adapter.", e);
195 mInCallService = null;
196 }
197
Santos Cordon049b7b62014-01-30 05:34:26 -0800198 // Upon successful connection, send the state of the world to the in-call app.
199 if (mInCallService != null) {
200 mCallsManager.updateInCall();
201 }
202
Santos Cordone3d76ab2014-01-28 17:25:20 -0800203 }
204
205 /**
206 * Cleans up the instance of in-call app after the service has been unbound.
207 */
208 private void onDisconnected() {
209 ThreadUtil.checkOnMainThread();
210 mInCallService = null;
211 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800212}