blob: 4da919019f7be3581ec660c488fdf82c3296a3d5 [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;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070026
27import com.android.internal.telecomm.IInCallService;
Santos Cordone3d76ab2014-01-28 17:25:20 -080028
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
Santos Cordone3d76ab2014-01-28 17:25:20 -080055 /**
56 * Package name of the in-call app. Although in-call code in kept in its own namespace, it is
Ben Gilad13329fd2014-02-11 17:20:29 -080057 * ultimately compiled into the dialer APK, hence the difference in namespaces between this and
58 * {@link #IN_CALL_SERVICE_CLASS_NAME}.
Santos Cordone3d76ab2014-01-28 17:25:20 -080059 * TODO(santoscordon): Change this into config.xml resource entry.
60 */
61 private static final String IN_CALL_PACKAGE_NAME = "com.google.android.dialer";
62
63 /**
64 * Class name of the component within in-call app which implements {@link IInCallService}.
65 */
Sailesh Nepala439e1b2014-03-11 18:19:58 -070066 private static final String IN_CALL_SERVICE_CLASS_NAME =
67 "com.android.incallui.InCallServiceImpl";
Santos Cordone3d76ab2014-01-28 17:25:20 -080068
69 /** Maintains a binding connection to the in-call app. */
70 private final InCallServiceConnection mConnection = new InCallServiceConnection();
71
72 private final CallsManager mCallsManager;
73
74 /** The in-call app implementation, see {@link IInCallService}. */
75 private IInCallService mInCallService;
76
77 /**
78 * Persists the specified parameters.
Santos Cordone3d76ab2014-01-28 17:25:20 -080079 */
Santos Cordon6242b132014-02-07 16:24:42 -080080 InCallController(CallsManager callsManager) {
81 mCallsManager = callsManager;
Santos Cordone3d76ab2014-01-28 17:25:20 -080082 }
83
84 // TODO(santoscordon): May be better to expose the IInCallService methods directly from this
85 // class as its own method to make the CallsManager code easier to read.
86 IInCallService getService() {
87 return mInCallService;
88 }
89
90 /**
Santos Cordon049b7b62014-01-30 05:34:26 -080091 * Indicates to the in-call app that a new call has been created and an appropriate
92 * user-interface should be built and shown to notify the user. Information about the call
93 * including its current state is passed in through the callInfo object.
Santos Cordone3d76ab2014-01-28 17:25:20 -080094 *
Santos Cordon049b7b62014-01-30 05:34:26 -080095 * @param callInfo Details about the new call.
Santos Cordone3d76ab2014-01-28 17:25:20 -080096 */
Santos Cordon049b7b62014-01-30 05:34:26 -080097 void addCall(CallInfo callInfo) {
98 try {
99 if (mInCallService == null) {
100 bind();
101 } else {
102 // TODO(santoscordon): Protect against logging phone number.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800103 Log.i(this, "Adding call: %s", callInfo);
Santos Cordon049b7b62014-01-30 05:34:26 -0800104 mInCallService.addCall(callInfo);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800105 }
Santos Cordon049b7b62014-01-30 05:34:26 -0800106 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800107 Log.e(this, e, "Exception attempting to addCall.");
Santos Cordon049b7b62014-01-30 05:34:26 -0800108 }
109 }
110
111 /**
112 * Indicates to the in-call app that a call has moved to the active state.
113 *
114 * @param callId The identifier of the call that became active.
115 */
116 void markCallAsActive(String callId) {
117 try {
118 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800119 Log.i(this, "Mark call as ACTIVE: %s", callId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800120 mInCallService.setActive(callId);
121 }
122 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800123 Log.e(this, e, "Exception attempting to markCallAsActive.");
Santos Cordon049b7b62014-01-30 05:34:26 -0800124 }
125 }
126
127 /**
128 * Indicates to the in-call app that a call has been disconnected and the user should be
129 * notified.
130 *
131 * @param callId The identifier of the call that was disconnected.
132 */
133 void markCallAsDisconnected(String callId) {
134 try {
135 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800136 Log.i(this, "Mark call as DISCONNECTED: %s", callId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800137 mInCallService.setDisconnected(callId);
138 }
139 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800140 Log.e(this, e, "Exception attempting to markCallAsDisconnected.");
Santos Cordone3d76ab2014-01-28 17:25:20 -0800141 }
142 }
143
144 /**
145 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800146 */
Santos Cordon049b7b62014-01-30 05:34:26 -0800147 void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800148 ThreadUtil.checkOnMainThread();
149 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800150 Log.i(this, "Unbinding from InCallService");
Santos Cordon049b7b62014-01-30 05:34:26 -0800151 TelecommApp.getInstance().unbindService(mConnection);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800152 mInCallService = null;
153 }
154 }
155
156 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800157 * Binds to the in-call app if not already connected by binding directly to the saved
158 * component name of the {@link IInCallService} implementation.
159 */
160 private void bind() {
161 ThreadUtil.checkOnMainThread();
162 if (mInCallService == null) {
163 ComponentName component =
164 new ComponentName(IN_CALL_PACKAGE_NAME, IN_CALL_SERVICE_CLASS_NAME);
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800165 Log.i(this, "Attempting to bind to InCallService: %s", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800166
167 Intent serviceIntent = new Intent(IInCallService.class.getName());
168 serviceIntent.setComponent(component);
169
170 Context context = TelecommApp.getInstance();
171 if (!context.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800172 Log.w(this, "Could not connect to the in-call app (%s)", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800173
174 // TODO(santoscordon): Implement retry or fall-back-to-default logic.
175 }
176 }
177 }
178
179 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800180 * Persists the {@link IInCallService} instance and starts the communication between
181 * CallsManager and in-call app by sending the first update to in-call app. This method is
182 * called after a successful binding connection is established.
183 *
184 * @param service The {@link IInCallService} implementation.
185 */
186 private void onConnected(IBinder service) {
187 ThreadUtil.checkOnMainThread();
188 mInCallService = IInCallService.Stub.asInterface(service);
189
190 try {
191 mInCallService.setInCallAdapter(new InCallAdapter(mCallsManager));
192 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800193 Log.e(this, e, "Failed to set the in-call adapter.");
Santos Cordone3d76ab2014-01-28 17:25:20 -0800194 mInCallService = null;
195 }
196
Santos Cordon049b7b62014-01-30 05:34:26 -0800197 // Upon successful connection, send the state of the world to the in-call app.
198 if (mInCallService != null) {
199 mCallsManager.updateInCall();
200 }
201
Santos Cordone3d76ab2014-01-28 17:25:20 -0800202 }
203
204 /**
205 * Cleans up the instance of in-call app after the service has been unbound.
206 */
207 private void onDisconnected() {
208 ThreadUtil.checkOnMainThread();
209 mInCallService = null;
210 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800211}