blob: d13aa75333e4ed7eab101bb5d5dc8d307061433b [file] [log] [blame]
Santos Cordon89647a62013-07-16 13:38:09 -07001/*
2 * Copyright (C) 2013 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.phone;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
Santos Cordon89647a62013-07-16 13:38:09 -070023import android.os.Handler;
24import android.os.IBinder;
25import android.os.Message;
26import android.os.RemoteException;
27import android.os.SystemProperties;
28import android.util.Log;
29
Santos Cordon9b7bac72013-08-06 08:04:52 -070030import com.android.phone.AudioRouter.AudioModeListener;
31import com.android.services.telephony.common.AudioMode;
Santos Cordonf4046882013-07-25 18:49:27 -070032import com.android.services.telephony.common.Call;
Santos Cordon345350e2013-07-19 17:16:14 -070033import com.android.services.telephony.common.ICallHandlerService;
Chiao Cheng6c6b2722013-08-22 18:35:54 -070034import com.google.common.collect.Lists;
Santos Cordon89647a62013-07-16 13:38:09 -070035
Santos Cordona3d05142013-07-29 11:25:17 -070036import java.util.List;
37
Santos Cordon89647a62013-07-16 13:38:09 -070038/**
Santos Cordon345350e2013-07-19 17:16:14 -070039 * This class is responsible for passing through call state changes to the CallHandlerService.
Santos Cordon89647a62013-07-16 13:38:09 -070040 */
Chiao Cheng6c6b2722013-08-22 18:35:54 -070041public class CallHandlerServiceProxy extends Handler
42 implements CallModeler.Listener, AudioModeListener {
Santos Cordon89647a62013-07-16 13:38:09 -070043
Santos Cordon345350e2013-07-19 17:16:14 -070044 private static final String TAG = CallHandlerServiceProxy.class.getSimpleName();
Chiao Cheng6c6b2722013-08-22 18:35:54 -070045 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt(
46 "ro.debuggable", 0) == 1);
Chiao Chenge41661c2013-07-23 13:28:26 -070047
Chiao Cheng6c6b2722013-08-22 18:35:54 -070048 public static final int RETRY_DELAY_MILLIS = 2000;
49 private static final int BIND_RETRY_MSG = 1;
50 private static final int MAX_RETRY_COUNT = 5;
Santos Cordon89647a62013-07-16 13:38:09 -070051
Santos Cordon593ab382013-08-06 21:58:23 -070052 private AudioRouter mAudioRouter;
Santos Cordoncba1b442013-07-18 12:43:58 -070053 private CallCommandService mCallCommandService;
Santos Cordon593ab382013-08-06 21:58:23 -070054 private CallModeler mCallModeler;
55 private Context mContext;
Chiao Chengd38eebc2013-08-28 14:38:14 -070056
Chiao Cheng6c6b2722013-08-22 18:35:54 -070057 private ICallHandlerService mCallHandlerServiceGuarded; // Guarded by mServiceAndQueueLock
Chiao Chengd38eebc2013-08-28 14:38:14 -070058 // Single queue to guarantee ordering
59 private List<QueueParams> mQueue; // Guarded by mServiceAndQueueLock
60
Chiao Cheng6c6b2722013-08-22 18:35:54 -070061 private final Object mServiceAndQueueLock = new Object();
62 private int mBindRetryCount = 0;
63
64 @Override
65 public void handleMessage(Message msg) {
66 super.handleMessage(msg);
67
68 switch (msg.what) {
69 case BIND_RETRY_MSG:
70 setupServiceConnection();
71 break;
72 }
73 }
Santos Cordon89647a62013-07-16 13:38:09 -070074
Santos Cordon63a84242013-07-23 13:32:52 -070075 public CallHandlerServiceProxy(Context context, CallModeler callModeler,
Santos Cordon593ab382013-08-06 21:58:23 -070076 CallCommandService callCommandService, AudioRouter audioRouter) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -070077 if (DBG) {
78 Log.d(TAG, "init CallHandlerServiceProxy");
79 }
Santos Cordon89647a62013-07-16 13:38:09 -070080 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070081 mCallCommandService = callCommandService;
Santos Cordon63a84242013-07-23 13:32:52 -070082 mCallModeler = callModeler;
Santos Cordon593ab382013-08-06 21:58:23 -070083 mAudioRouter = audioRouter;
Santos Cordon89647a62013-07-16 13:38:09 -070084
Santos Cordon593ab382013-08-06 21:58:23 -070085 mAudioRouter.addAudioModeListener(this);
Christine Chendaf7bf62013-08-05 19:12:31 -070086 mCallModeler.addListener(this);
Chiao Cheng6c6b2722013-08-22 18:35:54 -070087
88 setupServiceConnection();
Santos Cordon63a84242013-07-23 13:32:52 -070089 }
Santos Cordon89647a62013-07-16 13:38:09 -070090
Santos Cordon63a84242013-07-23 13:32:52 -070091 @Override
Santos Cordon995c8162013-07-29 09:22:22 -070092 public void onDisconnect(Call call) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -070093 try {
94 synchronized (mServiceAndQueueLock) {
95 if (mCallHandlerServiceGuarded == null) {
96 if (DBG) {
97 Log.d(TAG, "CallHandlerService not connected. Enqueue disconnect");
98 }
99 enqueueDisconnect(call);
100 return;
101 }
Santos Cordon63a84242013-07-23 13:32:52 -0700102 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700103 if (DBG) {
104 Log.d(TAG, "onDisconnect: " + call);
105 }
106 mCallHandlerServiceGuarded.onDisconnect(call);
107 } catch (Exception e) {
108 Log.e(TAG, "Remote exception handling onDisconnect ", e);
Santos Cordon89647a62013-07-16 13:38:09 -0700109 }
110 }
111
Santos Cordona3d05142013-07-29 11:25:17 -0700112 @Override
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700113 public void onIncoming(Call call) {
114 try {
115 synchronized (mServiceAndQueueLock) {
116 if (mCallHandlerServiceGuarded == null) {
117 if (DBG) {
118 Log.d(TAG, "CallHandlerService not connected. Enqueue incoming.");
119 }
120 enqueueIncoming(call);
121 return;
122 }
Christine Chenee09a492013-08-06 16:02:29 -0700123 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700124 if (DBG) {
125 Log.d(TAG, "onIncoming: " + call);
126 }
127 // TODO(klp): check RespondViaSmsManager.allowRespondViaSmsForCall()
128 // must refactor call method to accept proper call object.
129 mCallHandlerServiceGuarded.onIncoming(call,
130 RejectWithTextMessageManager.loadCannedResponses());
131 } catch (Exception e) {
132 Log.e(TAG, "Remote exception handling onUpdate", e);
Christine Chenee09a492013-08-06 16:02:29 -0700133 }
134 }
135
136 @Override
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700137 public void onUpdate(List<Call> calls) {
138 try {
139 synchronized (mServiceAndQueueLock) {
140 if (mCallHandlerServiceGuarded == null) {
141 if (DBG) {
142 Log.d(TAG, "CallHandlerService not connected. Enqueue update.");
143 }
144 enqueueUpdate(calls);
145 return;
146 }
Santos Cordona3d05142013-07-29 11:25:17 -0700147 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700148
149 if (DBG) {
150 Log.d(TAG, "onUpdate: " + calls.toString());
151 }
152 mCallHandlerServiceGuarded.onUpdate(calls);
153 } catch (Exception e) {
154 Log.e(TAG, "Remote exception handling onUpdate", e);
Santos Cordona3d05142013-07-29 11:25:17 -0700155 }
156 }
157
Santos Cordon9b7bac72013-08-06 08:04:52 -0700158 @Override
159 public void onAudioModeChange(int previousMode, int newMode) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700160 try {
161 synchronized (mServiceAndQueueLock) {
162 // TODO(klp): does this need to be enqueued?
163 if (mCallHandlerServiceGuarded == null) {
164 if (DBG) {
165 Log.d(TAG, "CallHandlerService not conneccted. Skipping "
166 + "onAudioModeChange().");
167 }
168 return;
169 }
Santos Cordon9b7bac72013-08-06 08:04:52 -0700170 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700171
172 // Just do a simple log for now.
173 Log.i(TAG, "Updating with new audio mode: " + AudioMode.toString(newMode) +
174 " from " + AudioMode.toString(previousMode));
175
176 if (DBG) {
177 Log.d(TAG, "onSupportAudioModeChange");
178 }
179
180 mCallHandlerServiceGuarded.onAudioModeChange(newMode);
181 } catch (Exception e) {
182 Log.e(TAG, "Remote exception handling onAudioModeChange", e);
Santos Cordon9b7bac72013-08-06 08:04:52 -0700183 }
184 }
185
Santos Cordon593ab382013-08-06 21:58:23 -0700186 @Override
187 public void onSupportedAudioModeChange(int modeMask) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700188 try {
189 synchronized (mServiceAndQueueLock) {
190 // TODO(klp): does this need to be enqueued?
191 if (mCallHandlerServiceGuarded == null) {
192 if (DBG) {
193 Log.d(TAG, "CallHandlerService not conneccted. Skipping"
194 + "onSupportedAudioModeChange().");
195 }
196 return;
197 }
198 }
Santos Cordon593ab382013-08-06 21:58:23 -0700199
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700200 if (DBG) {
201 Log.d(TAG, "onSupportAudioModeChange: " + AudioMode.toString(modeMask));
202 }
203
204 mCallHandlerServiceGuarded.onSupportedAudioModeChange(modeMask);
205 } catch (Exception e) {
206 Log.e(TAG, "Remote exception handling onAudioModeChange", e);
207 }
208
209 }
210
211 private ServiceConnection mConnection = new ServiceConnection() {
212 @Override public void onServiceConnected (ComponentName className, IBinder service){
213 if (DBG) {
214 Log.d(TAG, "Service Connected");
215 }
216 onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service));
217 }
218
219 @Override public void onServiceDisconnected (ComponentName className){
220 Log.i(TAG, "Disconnected from UI service.");
221 synchronized (mServiceAndQueueLock) {
222 mCallHandlerServiceGuarded = null;
223
224 // Technically, unbindService is un-necessary since the framework will schedule and
225 // restart the crashed service. But there is a exponential backoff for the restart.
226 // Unbind explicitly and setup again to avoid the backoff since it's important to
227 // always have an in call ui.
228 mContext.unbindService(mConnection);
229 setupServiceConnection();
Santos Cordon593ab382013-08-06 21:58:23 -0700230 }
231 }
Santos Cordon19d814b2013-08-28 14:58:17 -0700232 };
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700233
Santos Cordon406c0342013-08-28 00:07:47 -0700234 public void bringToForeground() {
235 // only support this call if the service is already connected.
Santos Cordon19d814b2013-08-28 14:58:17 -0700236 synchronized (mServiceAndQueueLock) {
237 if (mCallHandlerServiceGuarded != null && mCallModeler.hasLiveCall()) {
238 try {
239 if (DBG) Log.d(TAG, "bringToForeground");
240 mCallHandlerServiceGuarded.bringToForeground();
241 } catch (RemoteException e) {
242 Log.e(TAG, "Exception handling bringToForeground", e);
243 }
Santos Cordon406c0342013-08-28 00:07:47 -0700244 }
245 }
246 }
247
Santos Cordon89647a62013-07-16 13:38:09 -0700248 /**
Santos Cordon345350e2013-07-19 17:16:14 -0700249 * Sets up the connection with ICallHandlerService
Santos Cordon89647a62013-07-16 13:38:09 -0700250 */
251 private void setupServiceConnection() {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700252 synchronized (mServiceAndQueueLock) {
253 if (mCallHandlerServiceGuarded == null) {
254
255
256 final Intent serviceIntent = new Intent(ICallHandlerService.class.getName());
257 final ComponentName component = new ComponentName(mContext.getResources().getString(
258 R.string.incall_ui_default_package), mContext.getResources().getString(
259 R.string.incall_ui_default_class));
260 serviceIntent.setComponent(component);
261
Santos Cordon89647a62013-07-16 13:38:09 -0700262 if (DBG) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700263 Log.d(TAG, "binding to service " + serviceIntent);
Santos Cordon89647a62013-07-16 13:38:09 -0700264 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700265 if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
266 // This happens when the in-call package is in the middle of being installed.
267 // Delay the retry.
268 mBindRetryCount++;
269 if (mBindRetryCount < MAX_RETRY_COUNT) {
270 Log.e(TAG, "bindService failed on " + serviceIntent + ". Retrying in " +
271 RETRY_DELAY_MILLIS + " ms.");
272 sendMessageDelayed(Message.obtain(this, BIND_RETRY_MSG),
273 RETRY_DELAY_MILLIS);
274 } else {
275 Log.wtf(TAG, "Tried to bind to in-call UI " + MAX_RETRY_COUNT + " times."
276 + " Giving up.");
277 }
278 }
Santos Cordonaf763a12013-08-19 20:04:58 -0700279 }
280 }
281 }
282
283 /**
Santos Cordoncba1b442013-07-18 12:43:58 -0700284 * Called when the in-call UI service is connected. Send command interface to in-call.
285 */
Santos Cordon63a84242013-07-23 13:32:52 -0700286 private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) {
Chiao Chengd38eebc2013-08-28 14:38:14 -0700287
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700288 synchronized (mServiceAndQueueLock) {
289 mCallHandlerServiceGuarded = callHandlerService;
290
Santos Cordonad078192013-08-28 15:14:54 -0700291 // Before we send any updates, we need to set up the initial service calls.
292 makeInitialServiceCalls();
293
Chiao Chengd38eebc2013-08-28 14:38:14 -0700294 processQueue();
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700295 }
Santos Cordonad078192013-08-28 15:14:54 -0700296 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700297
Santos Cordonad078192013-08-28 15:14:54 -0700298 /**
299 * Makes initial service calls to set up callcommandservice and audio modes.
300 */
301 private void makeInitialServiceCalls() {
Santos Cordoncba1b442013-07-18 12:43:58 -0700302 try {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700303 mCallHandlerServiceGuarded.setCallCommandService(mCallCommandService);
Santos Cordonad078192013-08-28 15:14:54 -0700304
305 onSupportedAudioModeChange(mAudioRouter.getSupportedAudioModes());
306 final int mode = mAudioRouter.getAudioMode();
307 onAudioModeChange(mode, mode);
Santos Cordoncba1b442013-07-18 12:43:58 -0700308 } catch (RemoteException e) {
Santos Cordon63a84242013-07-23 13:32:52 -0700309 Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e);
Santos Cordon89647a62013-07-16 13:38:09 -0700310 }
311 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700312
Chiao Chengd38eebc2013-08-28 14:38:14 -0700313 private List<QueueParams> getQueue() {
314 if (mQueue == null) {
315 mQueue = Lists.newArrayList();
316 }
317 return mQueue;
318 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700319
320 private void enqueueDisconnect(Call call) {
Chiao Chengd38eebc2013-08-28 14:38:14 -0700321 getQueue().add(new QueueParams(QueueParams.METHOD_DISCONNECT, new Call(call)));
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700322 }
323
324 private void enqueueIncoming(Call call) {
Chiao Chengd38eebc2013-08-28 14:38:14 -0700325 getQueue().add(new QueueParams(QueueParams.METHOD_INCOMING, new Call(call)));
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700326 }
327
328 private void enqueueUpdate(List<Call> calls) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700329 final List<Call> copy = Lists.newArrayList();
330 for (Call call : calls) {
331 copy.add(new Call(call));
332 }
Chiao Chengd38eebc2013-08-28 14:38:14 -0700333 getQueue().add(new QueueParams(QueueParams.METHOD_INCOMING, copy));
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700334 }
335
Chiao Chengd38eebc2013-08-28 14:38:14 -0700336 private void processQueue() {
337 List<QueueParams> queue = getQueue();
338 for (QueueParams params : queue) {
339 switch (params.mMethod) {
340 case QueueParams.METHOD_INCOMING:
341 onIncoming((Call) params.mArg);
342 break;
343 case QueueParams.METHOD_UPDATE:
344 onUpdate((List<Call>) params.mArg);
345 break;
346 case QueueParams.METHOD_DISCONNECT:
347 onDisconnect((Call) params.mArg);
348 break;
349 default:
350 throw new IllegalArgumentException("Method type " + params.mMethod +
351 " not recognized.");
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700352 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700353 }
Chiao Chengd38eebc2013-08-28 14:38:14 -0700354 mQueue.clear();
355 mQueue = null;
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700356 }
357
Chiao Chengd38eebc2013-08-28 14:38:14 -0700358 /**
359 * Holds method parameters.
360 */
361 private static class QueueParams {
362 private static final int METHOD_INCOMING = 1;
363 private static final int METHOD_UPDATE = 2;
364 private static final int METHOD_DISCONNECT = 3;
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700365
Chiao Chengd38eebc2013-08-28 14:38:14 -0700366 private final int mMethod;
367 private final Object mArg;
368
369 private QueueParams(int method, Object arg) {
370 mMethod = method;
371 this.mArg = arg;
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700372 }
373 }
Santos Cordon89647a62013-07-16 13:38:09 -0700374}